Monday, July 18, 2011

Webflow - Step 3. Creating JAVA files

The following steps takes you through the Java files needed for the AddUser-Flow.xml defined in the previous topic Step 2. Creating Flows and View files.

UserService.java

This class contains the procedure to validate the model object.

package org.mybusiness.webflowtut.services;

import org.mybusiness.webflowtut.dtos.UserInfo;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;

public class UserService {

    public UserInfo createUserInfo() {
        return new UserInfo();
    }

    public boolean validateUserInfo(UserInfo userInfo, MessageContext context) {
        boolean validationSuccess = true;

        String id = userInfo.getId();
        if (id == null || id.trim().length() == 0) {
            context.addMessage(new MessageBuilder().error().source("id")
                    .defaultText("Invalid Id").build());
            validationSuccess = false;
        }

        String name = userInfo.getName();
        if (name == null || name.trim().length() == 0) {
            context.addMessage(new MessageBuilder().error().source("name")
                    .defaultText("Invalid Name").build());
            validationSuccess = false;
        }

        String age = userInfo.getAge();
        try {
            if (age == null || age.trim().length() == 0) {
                context.addMessage(new MessageBuilder().error().source("age")
                        .defaultText("Invalid Age").build());
                validationSuccess = false;
            } else {
                Integer.parseInt(age);
            }
        } catch (NumberFormatException e) {
            context.addMessage(new MessageBuilder().error().source("age")
                    .defaultText("Invalid Age").build());
            validationSuccess = false;
        }

        return validationSuccess;
    }
}

UserInfo.java

This class is the model object used in the AddUser example.

package org.mybusiness.webflowtut.dtos;

import java.io.Serializable;

public class UserInfo implements Serializable {

    private String id;
    private String name;
    private String age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

Proceed to Step 4: Screenshots

No comments:

Post a Comment