Author: vbriand Date: 2011-03-24 18:08:31 +0100 (Thu, 24 Mar 2011) New Revision: 41 Url: http://chorem.org/repositories/revision/chorem/41 Log: New class with validation for adding a person Added: trunk/chorem-web/src/main/java/org/chorem/bonzoms/action/PersonAction.java trunk/chorem-web/src/main/resources/org/chorem/bonzoms/action/PersonAction-validation.xml Modified: trunk/chorem-web/src/main/java/org/chorem/bonzoms/action/CompanyAction.java trunk/chorem-web/src/main/resources/i18n/chorem-web_en_GB.properties trunk/chorem-web/src/main/resources/i18n/chorem-web_fr_FR.properties trunk/chorem-web/src/main/webapp/WEB-INF/jsp/bonzoms/addPerson.jsp Modified: trunk/chorem-web/src/main/java/org/chorem/bonzoms/action/CompanyAction.java =================================================================== --- trunk/chorem-web/src/main/java/org/chorem/bonzoms/action/CompanyAction.java 2011-03-24 16:17:08 UTC (rev 40) +++ trunk/chorem-web/src/main/java/org/chorem/bonzoms/action/CompanyAction.java 2011-03-24 17:08:31 UTC (rev 41) @@ -56,7 +56,7 @@ proxy.store(newCompany); } catch (Exception e) { result = false; - addActionError(getText(n_("chorem.gepeto.company.create.error"))); + addActionError(getText(n_("chorem.bonzoms.company.create.error"))); log.error("An error occured while creating a new company", e); } return result; @@ -68,18 +68,21 @@ public String getName() { return name; } + /** * @param name the name to set */ public void setName(String name) { this.name = name; } + /** * @return the type */ public String getType() { return type; } + /** * @param type the type to set */ Added: trunk/chorem-web/src/main/java/org/chorem/bonzoms/action/PersonAction.java =================================================================== --- trunk/chorem-web/src/main/java/org/chorem/bonzoms/action/PersonAction.java (rev 0) +++ trunk/chorem-web/src/main/java/org/chorem/bonzoms/action/PersonAction.java 2011-03-24 17:08:31 UTC (rev 41) @@ -0,0 +1,145 @@ +package org.chorem.bonzoms.action; + +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.ChoremProxy; +import org.chorem.action.BaseAction; +import org.chorem.entities.PersonImpl; + +import static org.nuiton.i18n.I18n.n_; + +/** + * Person management class + * + * @author vbriand + */ +public class PersonAction extends BaseAction { + + private static final long serialVersionUID = -5757750975509965421L; + + private static final Log log = LogFactory.getLog(PersonAction.class); + + /** + * Adds a new person + * + * @return INPUT if the mandatory fields haven't all been filled in + * @return SUCCESS if the person has been added successfully + * @return ERROR if an error occurred + */ + public String add() { + String result = INPUT; + + if (firstName != null && lastName != null && email != null && + birthDate != null) { + if (!firstName.isEmpty() && !lastName.isEmpty() && + !email.isEmpty() && !birthDate.isEmpty()) { + //If the person has been added successfully + if (addPerson()) { + result = SUCCESS; + } else { + result = ERROR; + } + } + } + return result; + } + + protected String firstName; + protected String lastName; + protected String email; + protected String birthDate; + + /** + * Stores the new person through the proxy + * + * @return true if the person has been stored properly + * @return false if a problem occurred + */ + protected boolean addPerson() { + boolean result = true; + + try { + ChoremProxy proxy = getChoremProxy(); + PersonImpl newPerson = new PersonImpl(); + SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); + + newPerson.setFirstName(firstName); + newPerson.setLastName(lastName); + newPerson.setEmail(email); + try { + newPerson.setBirthDate(formatter.parse(birthDate)); + } catch (ParseException e) { + result = false; + addActionError(getText(n_("chorem.date.wrongFormat"))); + } + //If everything went smoothly + if (result) { + proxy.store(newPerson); + } + } catch (Exception e) { + result = false; + addActionError(getText(n_("chorem.bonzoms.person.create.error"))); + log.error("An error occured while creating a new person", e); + } + return result; + } + + /** + * @return the firstName + */ + public String getFirstName() { + return firstName; + } + + /** + * @param firstName the firstName to set + */ + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + /** + * @return the lastName + */ + public String getLastName() { + return lastName; + } + + /** + * @param lastName the lastName to set + */ + public void setLastName(String lastName) { + this.lastName = lastName; + } + + /** + * @return the email + */ + public String getEmail() { + return email; + } + + /** + * @param email the email to set + */ + public void setEmail(String email) { + this.email = email; + } + + /** + * @return the birthDate + */ + public String getBirthDate() { + return birthDate; + } + + /** + * @param birthDate the birthDate to set + */ + public void setBirthDate(String birthDate) { + this.birthDate = birthDate; + } +} Property changes on: trunk/chorem-web/src/main/java/org/chorem/bonzoms/action/PersonAction.java ___________________________________________________________________ Added: svn:mime-type + text/plain Modified: trunk/chorem-web/src/main/resources/i18n/chorem-web_en_GB.properties =================================================================== --- trunk/chorem-web/src/main/resources/i18n/chorem-web_en_GB.properties 2011-03-24 16:17:08 UTC (rev 40) +++ trunk/chorem-web/src/main/resources/i18n/chorem-web_en_GB.properties 2011-03-24 17:08:31 UTC (rev 41) @@ -1,6 +1,7 @@ chorem.beginDate.beforeToday=The date cannot be set before today''s date chorem.bonzoms.company=Company {0} chorem.bonzoms.company.add=Add a new company +chorem.bonzoms.company.create.error=An error occurred while creating your new company, please try again. If the problem persists, please contact an administrator chorem.bonzoms.company.name=Company name chorem.bonzoms.company.type=Company type chorem.bonzoms.contactDetails=Contact details @@ -12,14 +13,19 @@ chorem.bonzoms.person=Person chorem.bonzoms.person.add=Add a new person chorem.bonzoms.person.birthDate=Birth date +chorem.bonzoms.person.birthDate.required=You must enter the person birth date +chorem.bonzoms.person.create.error=An error occurred while adding the person, please try again. If the problem persists, please contact an administrator chorem.bonzoms.person.email=Email +chorem.bonzoms.person.email.required=You must enter the person email +chorem.bonzoms.person.email.wrongFormat=The email format is invalid chorem.bonzoms.person.firstName=First name +chorem.bonzoms.person.firstName.required=You must enter the person first name chorem.bonzoms.person.lastName=Last name +chorem.bonzoms.person.lastName.required=You must enter the person last name chorem.config.configFileName.description=chorem''s configuration filename chorem.date.wrongFormat=The date must be in the following format \: dd/mm/yyyy chorem.endDate.afterBegin=The end date cannot be posterior to the begin date chorem.error.internal=An internal error occurred -chorem.gepeto.company.create.error=An error occurred while creating your new company, please try again. If the problem persists, please contact an administrator chorem.gepeto.home=Home chorem.gepeto.project=Project {0} chorem.gepeto.project.add=Add a new project Modified: trunk/chorem-web/src/main/resources/i18n/chorem-web_fr_FR.properties =================================================================== --- trunk/chorem-web/src/main/resources/i18n/chorem-web_fr_FR.properties 2011-03-24 16:17:08 UTC (rev 40) +++ trunk/chorem-web/src/main/resources/i18n/chorem-web_fr_FR.properties 2011-03-24 17:08:31 UTC (rev 41) @@ -1,6 +1,7 @@ chorem.beginDate.beforeToday=La date entr\u00E9e ne peut \u00EAtre ant\u00E9rieure \u00E0 la date du jour chorem.bonzoms.company=Soci\u00E9t\u00E9 chorem.bonzoms.company.add=Ajouter une nouvelle soci\u00E9t\u00E9 +chorem.bonzoms.company.create.error=Une erreur s''est produite lors de la cr\u00E9ation de votre nouvelle soci\u00E9t\u00E9, merci d''essayer \u00E0 nouveau. Si le probl\u00E8me se reproduit, merci de contacter un administrateur chorem.bonzoms.company.name=Nom de la soci\u00E9t\u00E9 chorem.bonzoms.company.name.required=Vous devez entrer le nom de la soci\u00E9t\u00E9 chorem.bonzoms.company.type=Type de la soci\u00E9t\u00E9 @@ -14,14 +15,19 @@ chorem.bonzoms.person=Personne chorem.bonzoms.person.add=Ajouter une nouvelle personne chorem.bonzoms.person.birthDate=Date de naissance +chorem.bonzoms.person.birthDate.required=Vous devez entrer la date de naissance de la personne +chorem.bonzoms.person.create.error= chorem.bonzoms.person.email=Email +chorem.bonzoms.person.email.required=Vous devez entrer l''adresse email de la personne +chorem.bonzoms.person.email.wrongFormat=L''adresse email que vous avez entrée est incorrecte chorem.bonzoms.person.firstName=Pr\u00E9nom +chorem.bonzoms.person.firstName.required=Vous devez entrer le prénom de la personne chorem.bonzoms.person.lastName=Nom +chorem.bonzoms.person.lastName.required=Vous devez entrer le nom de la personne chorem.config.configFileName.description=Nom du fichier de configuration de chorem chorem.date.wrongFormat=La date doit respecter le format suivant \: jj/mm/yyyy chorem.endDate.afterBegin=La date de fin ne doit pas pas \u00EAtre ant\u00E9rieure \u00E0 la date de d\u00E9but chorem.error.internal=Une erreur interne s''est produite -chorem.gepeto.company.create.error=Une erreur s''est produite lors de la cr\u00E9ation de votre nouvelle soci\u00E9t\u00E9, merci d''essayer \u00E0 nouveau. Si le probl\u00E8me se reproduit, merci de contacter un administrateur chorem.gepeto.home=Accueil chorem.gepeto.project=Projet {0} chorem.gepeto.project.add=Ajouter un nouveau projet Added: trunk/chorem-web/src/main/resources/org/chorem/bonzoms/action/PersonAction-validation.xml =================================================================== --- trunk/chorem-web/src/main/resources/org/chorem/bonzoms/action/PersonAction-validation.xml (rev 0) +++ trunk/chorem-web/src/main/resources/org/chorem/bonzoms/action/PersonAction-validation.xml 2011-03-24 17:08:31 UTC (rev 41) @@ -0,0 +1,29 @@ +<!DOCTYPE validators PUBLIC + "-//OpenSymphony Group//XWork Validator 1.0.2//EN" + "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> + +<validators> + <field name="firstName"> + <field-validator type="requiredstring"> + <message key="chorem.bonzoms.person.firstName.required" /> + </field-validator> + </field> + <field name="lastName"> + <field-validator type="requiredstring"> + <message key="chorem.bonzoms.person.lastName.required" /> + </field-validator> + </field> + <field name="email"> + <field-validator type="requiredstring"> + <message key="chorem.bonzoms.person.email.required" /> + </field-validator> + <field-validator type="email"> + <message key="chorem.bonzoms.person.email.wrongFormat" /> + </field-validator> + </field> + <field name="birthDate"> + <field-validator type="requiredstring"> + <message key="chorem.bonzoms.person.birthDate.required" /> + </field-validator> + </field> +</validators> Property changes on: trunk/chorem-web/src/main/resources/org/chorem/bonzoms/action/PersonAction-validation.xml ___________________________________________________________________ Added: svn:mime-type + text/plain Modified: trunk/chorem-web/src/main/webapp/WEB-INF/jsp/bonzoms/addPerson.jsp =================================================================== --- trunk/chorem-web/src/main/webapp/WEB-INF/jsp/bonzoms/addPerson.jsp 2011-03-24 16:17:08 UTC (rev 40) +++ trunk/chorem-web/src/main/webapp/WEB-INF/jsp/bonzoms/addPerson.jsp 2011-03-24 17:08:31 UTC (rev 41) @@ -9,6 +9,7 @@ </head> <body> <h2><s:text name="chorem.bonzoms.person.add" /></h2> + <s:actionerror /> <s:form action="addPerson" method="post"> <fieldset> <legend><s:text name="chorem.bonzoms.person" /></legend>