View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.control.form;
16  
17  import org.figure8.join.control.JoinForm;
18  import org.figure8.join.control.action.ReleaseActions;
19  
20  import org.apache.struts.action.ActionMapping;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import java.util.Date;
25  import java.text.SimpleDateFormat;
26  /**
27   * Form object used for manipulating Releases into Join application.
28   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
29   * @version $Revision: 1.1 $
30   *
31   * @struts.form name="releaseForm"
32   */
33  public class ReleaseForm extends JoinForm{
34  
35     // Attributes ---------------------------------------------------------------
36  
37     /** The name of this release */
38     private String name;
39     /** The major number of version number of release */
40     private int major = -1;
41     /** The minor number of version number of release */
42     private int minor = -1;
43     /** The date of shipping for this release */
44     private Date shippingDate;
45  
46     /** String representation of <b>major</b> */
47     private String majorStr;
48     /** String representation of <b>minor</b> */
49     private String minorStr;
50     /** String representation of <b>shippingDate</b> */
51     private String shippingDateStr;
52  
53  
54     // Constructors -------------------------------------------------------------
55  
56     /** Creates a new instance of ReleaseForm */
57     public ReleaseForm(){
58     }
59  
60  
61     // Public -------------------------------------------------------------------
62  
63     /** @return Name of current release */
64     public String getName(){
65        return name;
66     }
67     /** @param name Name of current release */
68     public void setName(String name){
69        this.name = name;
70     }
71     /** @return Major number of current release */
72     public int getMajor(){
73        return major;
74     }
75     /** @param major Major number of current release */
76     public void setMajor(int major){
77        this.major = major;
78        this.majorStr = String.valueOf(major);
79     }
80     /** @return Minor number of current release */
81     public int getMinor(){
82        return minor;
83     }
84     /** @param minor Minor number of current release */
85     public void setMinor(int minor){
86        this.minor = minor;
87        this.minorStr = String.valueOf(minor);
88     }
89     /** @return Shipping date for this release */
90     public Date getShippingDate(){
91        return shippingDate;
92     }
93     /** @param shippingDate Production shipping date of this release */
94     public void setShippingDate(Date shippingDate){
95        this.shippingDate = shippingDate;
96        // Format to string according to locale.
97        SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
98        this.shippingDateStr = format.format(shippingDate);
99     }
100 
101    /** @return Major number of current release (should be an integer) */
102    public String getMajorStr(){
103       return majorStr;
104    }
105    /** @param majorStr Major number of current release (should be an integer) */
106    public void setMajorStr(String majorStr){
107       this.majorStr = majorStr;
108    }
109    /** @return Minor number of current release (should be an integer) */
110    public String getMinorStr(){
111       return minorStr;
112    }
113    /** @param minorStr Minor number of current release (should be an integer) */
114    public void setMinorStr(String minorStr){
115       this.minorStr = minorStr;
116    }
117    /** @return Date of production shipping for this release (should be a date) */
118    public String getShippingDateStr(){
119       return shippingDateStr;
120    }
121    /** @param shippingDateStr Production shipping date for this release (should be a date) */
122    public void setShippingDateStr(String shippingDateStr){
123       this.shippingDateStr = shippingDateStr;
124    }
125 
126 
127    // Implementation of JoinForm -----------------------------------------------
128 
129    /**
130     * Validation of the form attributes.
131     * @param operation String representing the operation to invoke on Action
132     * @param mapping Mapping between forwards name and path for this action
133     * @param request The servlet container request wrapper
134     */
135    public void doValidate(String operation, ActionMapping mapping, HttpServletRequest request){
136 
137       if (ReleaseActions.LOAD_OP.equals(operation)){
138          // Check and see if name is missing.
139          if (name == null || name.length() == 0)
140             addActionError("errors.required", getGuiMessageResources().getMessage("label.release.name"));
141       }
142       else if (ReleaseActions.SAVE_OP.equals(operation)){
143          // Check and see if name is missing.
144          if (name == null || name.length() == 0)
145             addActionError("errors.required", getGuiMessageResources().getMessage("label.release.name"));
146          // Check and see if major is missing.
147          if (majorStr == null || majorStr.length() == 0)
148             addActionError("errors.required", getGuiMessageResources().getMessage("label.release.major"));
149          else{
150             // Check and see if major is an integer.
151             try {major = Integer.valueOf(majorStr).intValue();}
152             catch (Exception e){
153                addActionError("errors.integer", getGuiMessageResources().getMessage("label.release.major"));
154             }
155          }
156          // Check and see if minor is missing.
157          if (minorStr == null || minorStr.length() == 0)
158             addActionError("errors.required", getGuiMessageResources().getMessage("label.release.minor"));
159          else{
160             // Check and see if minor is an integer.
161             try {minor = Integer.valueOf(minorStr).intValue();}
162             catch (Exception e){
163                addActionError("errors.integer", getGuiMessageResources().getMessage("label.release.minor"));
164             }
165          }
166          // Check and see if shipping date is missing.
167          if (shippingDateStr == null || shippingDateStr.length() == 0)
168             addActionError("errors.required", getGuiMessageResources().getMessage("label.release.shipdate"));
169          else{
170             // Build a format using a locale pattern.
171             SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
172             // Check if it's parsable.
173             try {shippingDate = format.parse(shippingDateStr);}
174             catch (Exception e){
175                addActionError("errors.format", getGuiMessageResources().getMessage("label.release.shipdate"));
176             }
177          }
178       }
179    }
180 
181 
182    // Override of ActionForm ---------------------------------------------------
183 
184    /**
185     * Reset form attributes.
186     * @param mapping Mapping between forwards name and path for this action
187     * @param request The servlet container request wrapper
188     */
189    public void reset(ActionMapping mapping, HttpServletRequest request){
190       super.reset(mapping, request);
191       name = null;
192       major = -1;
193       minor = -1;
194       shippingDate = null;
195       majorStr = null;
196       minorStr = null;
197       shippingDateStr = null;
198    }
199 }