package com.dotj.web.struts;
import org.apache.struts.action.*;
import org.apache.struts.util.MessageResources;
import org.apache.struts.Globals;
import javax.servlet.http.HttpServletRequest;
import com.dotj.form.interfaces.IFormValidator;
import com.dotj.form.FormValidatorTag;
import com.dotj.form.dotJFormErrors;
import com.dotj.form.dotJFormError;
import java.util.Iterator;
/**
* Test JavaBean for the CustomerForm object.
*/
public final class CustomerForm extends ActionForm {
private long id = 0;
private String lastName = null;
private String firstName = null;
private String city = null;
private String state = null;
private String zipCode = null;
private String country = null;
private String gender = null;
private String employed = null;
private String comments = null;
private String approved = null;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmployed() {
return employed;
}
public void setEmployed(String employed) {
this.employed = employed;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getApproved() {
return approved;
}
public void setApproved(String approved) {
this.approved = approved;
}
/**
* Reset all properties to their default values.
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.id = 0;
this.lastName = null;
this.firstName = null;
this.city = null;
this.state = null;
this.zipCode = null;
this.country = null;
this.gender = null;
this.employed = null;
this.comments = null;
this.approved = null;
}
/**
* Validate the properties that have been set from this HTTP request,
* and return an <code>ActionErrors</code> object that encapsulates any
* validation errors that have been found. If no errors are found, return
* <code>null</code> or an <code>ActionErrors</code> object with no
* recorded error messages.
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// Perform validator framework validations
ActionErrors errors = super.validate(mapping, request);
if (errors == null) errors = new ActionErrors();
IFormValidator formValidator = FormValidatorTag.getDotJFormValidator(request);
formValidator.validate();
// Add a custom error to the dotJ form validator.
MessageResources bundle = ((MessageResources) request.getAttribute(Globals.MESSAGES_KEY));
if (state.equals("ME") || state.equals("CA")) {
String msg = bundle.getMessage("error.msg.state.invalid");
formValidator.addFieldError("state", "error.msg.state.invalid.li", msg);
}
if (country.equals("US")) {
String msg = bundle.getMessage("error.msg.country.invalid");
formValidator.addFieldError("country", "error.msg.country.invalid.li", msg);
}
// To integrate with the Struts <html:errors> tag, copy the errors into the ActionErrors object.
dotJFormErrors dotJErrors = formValidator.getErrors();
Iterator iter = dotJErrors.get();
while (iter.hasNext()) {
dotJFormError error = (dotJFormError) iter.next();
errors.add(error.getFieldName(), new ActionError(error.getErrorMsgKey()));
}
return errors;
}
}