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.PhysicalEnvironmentActions;
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 and validating PhysicalEnvironments
28   * into the Join application.
29   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
30   * @version $Revision: 1.3 $
31   *
32   * @struts.form name="physicalEnvForm"
33   */
34  public class PhysicalEnvironmentForm extends JoinForm{
35  
36     // Attributes ---------------------------------------------------------------
37  
38     /** The key of current physical environment. */
39     private String key;
40     /** The name of current physical environment. */
41     private String name;
42  
43     /** The starting date of changes tracking period. */
44     private Date startDate;
45     /** The ending date of changes tracking period. */
46     private Date endDate;
47     /** The hour of day for configuration time */
48     private int startHour;
49     /** The minute of hour for configuration time */
50     private int startMin;
51  
52     /** Wether the requested operation is made from the logical environment point of view */
53     private boolean fromLogical = false;
54  
55     /** The String representation of changes tracking period start date. */
56     private String startDateStr = null;
57     /** The String representation of changes tracking period start hour. */
58     private String startHourStr = null;
59     /** The String representation of changes tracking period start minutes. */
60     private String startMinStr = null;
61     /** The String representation of changes tracking period end date. */
62     private String endDateStr = null;
63  
64  
65     // Constructors -------------------------------------------------------------
66  
67     /** Creates a new instance of PhysicalEnvironmentForm. */
68     public PhysicalEnvironmentForm(){
69     }
70  
71  
72     // Public -------------------------------------------------------------------
73  
74     /** @return The key of the current physical environment. */
75     public String getKey(){
76        return key;
77     }
78     /** @param key The key of the current physical environment. */
79     public void setKey(String key){
80        this.key = key;
81     }
82     /** @return The name of the physical environment denoted by form. */
83     public String getName(){
84        return name;
85     }
86     /** @param name The name of the physical environment denoted by form. */
87     public void setName(String name){
88        this.name = name;
89     }
90  
91     /** @return The starting date of changes tracking period */
92     public Date getStartDate(){
93        return startDate;
94     }
95     /** @param startDate The starting date of changes tracking period */
96     public void setStartDate(Date startDate){
97        this.startDate = startDate;
98        // Format to string according to locale.
99        SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
100       this.startDateStr = format.format(startDate);
101    }
102    /** @return The ending date of changes tracking period */
103    public Date getEndDate(){
104       return endDate;
105    }
106    /** @param endDate The ending date of changes tracking period */
107    public void setEndDate(Date endDate){
108       this.endDate = endDate;
109       // Format to string according to locale.
110       SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
111       this.endDateStr = format.format(endDate);
112    }
113 
114    /** @return The configuration tracking start hour */
115    public int getStartHour(){
116       return startHour;
117    }
118    /** @return The configuration tracking start minute */
119    public int getStartMin(){
120       return startMin;
121    }
122 
123    /** @return Wether the requested operation is made from the logical environment point of view */
124    public boolean isFromLogical(){
125       return fromLogical;
126    }
127    /** @param fromLogical Wether the requested operation is made from the logical environment point of view */
128    public void setFromLogical(boolean fromLogical){
129       this.fromLogical = fromLogical;
130    }
131 
132    /** @param startDateStr The String representation of changes tracking period start date (must be a date) */
133    public void setStartDateStr(String startDateStr){
134       this.startDateStr = startDateStr;
135    }
136    /** @param startHourStr The String representation of changes tracking period start hour (must be an integer) */
137    public void setStartHourStr(String startHourStr){
138       this.startHourStr = startHourStr;
139    }
140    /** @param startMinStr The String representation of changes tracking period start minute (must be an integer) */
141    public void setStartMinStr(String startMinStr){
142       this.startMinStr = startMinStr;
143    }
144    /** @param endDateStr The String representation of changes tracking period start date (must be a date) */
145    public void setEndDateStr(String endDateStr){
146       this.endDateStr = endDateStr;
147    }
148 
149 
150    // Implementation of JoinForm -----------------------------------------------
151 
152    /**
153     * Validation of form attributes.
154     * @param operation String representing the operation to invoke on Action
155     * @param mapping Mapping between forwards name and path for this action
156     * @param request The servlet container request wrapper
157     */
158    public void doValidate(String operation, ActionMapping mapping, HttpServletRequest request){
159 
160       if (PhysicalEnvironmentActions.LOAD_OP.equals(operation) || PhysicalEnvironmentActions.DETAILS_OP.equals(operation)){
161          // Check and see if id is missing.
162          validateEntityObjectId();
163       }
164       else if (PhysicalEnvironmentActions.SAVE_OP.equals(operation)){
165          // Check and see if key is missing.
166          if (key == null || key.length() == 0)
167             addActionError("errors.required", getGuiMessageResources().getMessage("label.physicalenv.key"));
168          // Check and see if name is missing.
169          if (name == null || name.length() == 0)
170             addActionError("errors.required", getGuiMessageResources().getMessage("label.physicalenv.name"));
171       }
172       else if (PhysicalEnvironmentActions.CONFIG_OP.equals(operation)){
173          // Check and see if key is missing.
174          if (key == null || key.length() == 0)
175             addActionError("errors.required", getGuiMessageResources().getMessage("label.physicalenv.key"));
176          // Check and see if starting date is missing.
177          if (startDateStr == null || startDateStr.length() == 0)
178             addActionError("errors.required", getGuiMessageResources().getMessage("label.physicalenv.history.startdate"));
179          else{
180             // Build a format using a locale pattern.
181             SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
182             // Check if it's parsable.
183             try {startDate = format.parse(startDateStr);}
184             catch (Exception e){
185                addActionError("errors.format", getGuiMessageResources().getMessage("label.physicalenv.history.startdate"));
186             }
187          }
188          // Check and see if start hour is missing.
189          if (startHourStr == null || startHourStr.length() == 0)
190             addActionError("errors.required", getGuiMessageResources().getMessage("label.physicalenv.history.starthour"));
191          else{
192             // Check and see if start hour is an integer.
193             try {startHour = Integer.valueOf(startHourStr).intValue();}
194             catch (Exception e){
195                addActionError("errors.integer", getGuiMessageResources().getMessage("label.physicalenv.history.starthour"));
196             }
197             // Check that it is in correct range.
198             if (startHour < 0 || startHour > 23)
199                addActionError("errors.range", getGuiMessageResources().getMessage("label.physicalenv.history.starthour"), "00", "23");
200          }
201          // Check and see if start minute is missing.
202          if (startMinStr == null || startMinStr.length() == 0)
203             addActionError("errors.required", getGuiMessageResources().getMessage("label.physicalenv.history.startmin"));
204          else{
205             // Check and see if start minute is an integer.
206             try {startMin = Integer.valueOf(startMinStr).intValue();}
207             catch (Exception e){
208                addActionError("errors.integer", getGuiMessageResources().getMessage("label.physicalenv.history.startmin"));
209             }
210             // Check that it is in correct range.
211             if (startMin < 0 || startHour > 59)
212                addActionError("errors.range", getGuiMessageResources().getMessage("label.physicalenv.history.startmin"), "00", "59");
213          }
214       }
215       else if (PhysicalEnvironmentActions.CHANGES_OP.equals(operation)){
216          // Check and see if key is missing.
217          if (key == null || key.length() == 0)
218             addActionError("errors.required", getGuiMessageResources().getMessage("label.physicalenv.key"));
219          // Check and see if starting date is missing.
220          if (startDateStr == null || startDateStr.length() == 0)
221             addActionError("errors.required", getGuiMessageResources().getMessage("label.physicalenv.history.startdate"));
222          else{
223             // Build a format using a locale pattern.
224             SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
225             // Check if it's parsable.
226             try {startDate = format.parse(startDateStr);}
227             catch (Exception e){
228                addActionError("errors.format", getGuiMessageResources().getMessage("label.physicalenv.history.startdate"));
229             }
230          }
231          // Check and see if ending date is missing.
232          if (endDateStr == null || endDateStr.length() == 0)
233             addActionError("errors.required", getGuiMessageResources().getMessage("label.physicalenv.history.enddate"));
234          else{
235             // Build a format using a locale pattern.
236             SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
237             // Check if it's parsable.
238             try {endDate = format.parse(endDateStr);}
239             catch (Exception e){
240                addActionError("errors.format", getGuiMessageResources().getMessage("label.physicalenv.history.enddate"));
241             }
242          }
243       }
244    }
245 
246 
247    // Override of ActionForm ---------------------------------------------------
248 
249    /**
250     * Reset form attributes.
251     * @param mapping Mapping between forwards name and path for this action
252     * @param request The servlet container request wrapper
253     */
254    public void reset(ActionMapping mapping, HttpServletRequest request){
255       super.reset(mapping, request);
256       key = null;
257       name = null;
258       startDate = null;
259       endDate = null;
260       fromLogical = false;
261       startHour = 0;
262       startMin = 0;
263       startDateStr = null;
264       startHourStr = null;
265       startMinStr = null;
266       endDateStr = null;
267    }
268 }