View Javadoc

1   /**
2    * Copyright 2005-2007 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.ResourceActions;
19  
20  import org.apache.struts.action.ActionMapping;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import java.util.Map;
25  import java.util.Date;
26  import java.util.Arrays;
27  import java.util.HashMap;
28  import java.text.SimpleDateFormat;
29  /**
30   * Form object used for manipulating Resources into Join application
31   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
32   * @version $Revision: 1.3 $
33   *
34   * @struts.form name="resourceForm"
35   */
36  public class ResourceForm extends JoinForm{
37     
38     // Attributes ---------------------------------------------------------------
39     
40     /** This resource name (must be unique) */
41     private String name;
42     /** The path to the log directory of this resource */
43     private String logDirPath;
44     /** The path to the current log file of this resource */
45     private String logFilePath;
46     /** The key of this resource type */
47     private String resourceTypeKey;
48     /** The identifier of machine this resource is hosted on */
49     private long machineId = -1;
50  
51     /** The category of this resource (this determine its persistent implementation) */
52     private String category;
53     /** The starting date of changes tracking period. */
54     private Date startDate;
55     /** The ending date of changes tracking period. */
56     private Date endDate;
57     /** The hour of day for configuration time */
58     private int startHour;
59     /** The minute of hour for configuration time */
60     private int startMin;
61  
62     /** The string representation of identifier of machine this resource is hosted on */
63     private String machineIdStr;
64     /** The String representation of changes tracking period start date. */
65     private String startDateStr = null;
66     /** The String representation of changes tracking period start hour. */
67     private String startHourStr = null;
68     /** The String representation of changes tracking period start minutes. */
69     private String startMinStr = null;
70     /** The String representation of changes tracking period end date. */
71     private String endDateStr = null;
72  
73     /** The list of actually supported resource categories */
74     public static final String[] SUPPORTED_CATEGORIES = {"EIS", "Service", "VersionedResource", "Gateway"};
75     /** The map of actually supported resource implementations (key is the category) */
76     public static Map SUPPORTED_IMPLEMENTATIONS;
77  
78     static{
79        SUPPORTED_IMPLEMENTATIONS = new HashMap();
80        SUPPORTED_IMPLEMENTATIONS.put("EIS", "org.figure8.join.businessobjects.environment.EIS");
81        SUPPORTED_IMPLEMENTATIONS.put("Service", "org.figure8.join.businessobjects.environment.Service");
82        SUPPORTED_IMPLEMENTATIONS.put("Gateway", "org.figure8.join.businessobjects.environment.Gateway");
83        SUPPORTED_IMPLEMENTATIONS.put("VersionedResource", "org.figure8.join.businessobjects.environment.VersionedResource");
84     }
85  
86  
87     // Constructors -------------------------------------------------------------
88     
89     /** Creates a new instance of ResourceForm */
90     public ResourceForm(){
91     }
92   
93     
94     // Public -------------------------------------------------------------------
95  
96     /** @return This resource name (must be unique) */
97     public String getName(){
98        return name;
99     }
100    /** @param name This resource name (must be unique) */
101    public void setName(String name){
102       this.name = name;
103    }
104    /** @return The path to the log directory of this resource */
105    public String getLogDirPath(){
106       return logDirPath;
107    }
108    /** @param logDirPath The path to the log directory of this resource */
109    public void setLogDirPath(String logDirPath){
110       this.logDirPath = logDirPath;
111    }
112    /** @return The path to the current log file of this resource */
113    public String getLogFilePath(){
114       return logFilePath;
115    }
116    /** @param logFilePath The path to the current log file of this resource */
117    public void setLogFilePath(String logFilePath){
118       this.logFilePath = logFilePath;
119    }
120    /** @return he key of this resource type */
121    public String getResourceTypeKey(){
122       return resourceTypeKey;
123    }
124    /** @param typeKey he key of this resource type */
125    public void setResourceTypeKey(String typeKey){
126       this.resourceTypeKey = typeKey;
127    }
128    /** @return The identifier of machine this resource is hosted on */
129    public long getMachineId(){
130       return machineId;
131    }
132    /** @param machineId The identifier of machine this resource is hosted on */
133    public void setMachineId(long machineId){
134       this.machineId = machineId;
135       this.machineIdStr = String.valueOf(machineId);
136    }
137 
138    /** @return The category of this resource (this determine its persistent implementation) */
139    public String getCategory(){
140       return category;
141    }
142    /** @param category The category of this resource (this determine its persistent implementation) */
143    public void setCategory(String category){
144       this.category = category;
145    }
146 
147    /** @return The string representation of identifier of machine this resource is hosted on */
148    public String getMachineIdStr(){
149       return machineIdStr;
150    }
151    /** @param machineIdStr The string representation of identifier of machine this resource is hosted on */
152    public void setMachineIdStr(String machineIdStr){
153       this.machineIdStr = machineIdStr;
154    }
155    /** @return The starting date of changes tracking period */
156    public Date getStartDate(){
157       return startDate;
158    }
159    /** @param startDateStr The String representation of changes tracking period start date (must be a date) */
160    public void setStartDateStr(String startDateStr){
161       this.startDateStr = startDateStr;
162    }
163    /** @param startHourStr The String representation of changes tracking period start hour (must be an integer) */
164    public void setStartHourStr(String startHourStr){
165       this.startHourStr = startHourStr;
166    }
167    /** @param startMinStr The String representation of changes tracking period start minute (must be an integer) */
168    public void setStartMinStr(String startMinStr){
169       this.startMinStr = startMinStr;
170    }
171    /** @return The ending date of changes tracking period */
172    public Date getEndDate(){
173       return endDate;
174    }
175    /** @param endDateStr The String representation of changes tracking period start date (must be a date) */
176    public void setEndDateStr(String endDateStr){
177       this.endDateStr = endDateStr;
178    }
179 
180    /**
181     * Retrieve the supported resource categories for form validation purpose. This
182     * is a hook that will allow subclasses to override and specifiy their supported
183     * resource categories implementations.
184     * @return The current default SUPPORTED_CATEGORIES static variable
185     */
186    public static String[] getSupportedCategories(){
187       return SUPPORTED_CATEGORIES;
188    }
189 
190    /**
191     * Retrieve the FQN of the Java class representing a valid implementation for a
192     * resource category. This is a hook that will allow subclasses to override and
193     * specifiy their own categories implementations.
194     * @param category The resource category to get implementation for
195     * @return The full name of Java resource implementation or null if none match
196     */
197    public static String getSupportedImplementation(String category){
198       return (String)SUPPORTED_IMPLEMENTATIONS.get(category);
199    }
200 
201 
202    // Implementation of JoinForm -----------------------------------------------
203    
204    /**
205     * Validation of the form attributes.
206     * @param operation String representing the operation to invoke on Action
207     * @param mapping Mapping between forwards name and path for this action
208     * @param request The servlet container request wrapper
209     */
210    public void doValidate(String operation, ActionMapping mapping, HttpServletRequest request){
211 
212       // Check if category is missing.
213       if (category == null || category.length() == 0)
214          addActionError("errors.required", getGuiMessageResources().getMessage("label.resource.category"));
215       else{
216          // Check if it is a valid category.
217          if (!Arrays.asList(getSupportedCategories()).contains(category))
218             addActionError("errors.invalid", getGuiMessageResources().getMessage("label.resource.category"));
219       }
220 
221       // Check attributes depending on operation.
222       if (ResourceActions.LOAD_OP.equals(operation)){
223          // Check if identifier is missing.
224          validateEntityObjectId();
225       }
226       else if (ResourceActions.SAVE_OP.equals(operation)){
227          // Check if name is missing.
228          if (name == null || name.length() == 0)
229             addActionError("errors.required", getGuiMessageResources().getMessage("label.resource.name"));
230          // Check if resource type key is missing.
231          if (resourceTypeKey == null || resourceTypeKey.length() == 0)
232             addActionError("errors.required", getGuiMessageResources().getMessage("label.resource.resourcetype"));
233          // Check if machine identifier is missing if not an EIS.
234          if (!"EIS".equals(category)){
235             if (machineIdStr == null || machineIdStr.length() == 0)
236                addActionError("errors.required", getGuiMessageResources().getMessage("label.resource.machine"));
237             else{
238                try {machineId = Long.valueOf(machineIdStr).longValue();}
239                catch (Exception e){
240                   addActionError("errors.integer", getGuiMessageResources().getMessage("label.resource.machine"));
241                }
242             }
243          }
244       }
245       else if (ResourceActions.CONFIG_OP.equals(operation)){
246          // Clear errors made so far.
247          errors.clear();
248          // Check if name is missing.
249          if (name == null || name.length() == 0)
250             addActionError("errors.required", getGuiMessageResources().getMessage("label.resource.name"));
251          // Check and see if starting date is missing.
252          if (startDateStr == null || startDateStr.length() == 0)
253             addActionError("errors.required", getGuiMessageResources().getMessage("label.gateway.history.startdate"));
254          else{
255             // Build a format using a locale pattern.
256             SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
257             // Check if it's parsable.
258             try {startDate = format.parse(startDateStr);}
259             catch (Exception e){
260                addActionError("errors.format", getGuiMessageResources().getMessage("label.gateway.history.startdate"));
261             }
262          }
263          // Check and see if start hour is missing.
264          if (startHourStr == null || startHourStr.length() == 0)
265             addActionError("errors.required", getGuiMessageResources().getMessage("label.gateway.history.starthour"));
266          else{
267             // Check and see if start hour is an integer.
268             try {startHour = Integer.valueOf(startHourStr).intValue();}
269             catch (Exception e){
270                addActionError("errors.integer", getGuiMessageResources().getMessage("label.gateway.history.starthour"));
271             }
272             // Check that it is in correct range.
273             if (startHour < 0 || startHour > 23)
274                addActionError("errors.range", getGuiMessageResources().getMessage("label.gateway.history.starthour"), "00", "23");
275          }
276          // Check and see if start minute is missing.
277          if (startMinStr == null || startMinStr.length() == 0)
278             addActionError("errors.required", getGuiMessageResources().getMessage("label.gateway.history.startmin"));
279          else{
280             // Check and see if start minute is an integer.
281             try {startMin = Integer.valueOf(startMinStr).intValue();}
282             catch (Exception e){
283                addActionError("errors.integer", getGuiMessageResources().getMessage("label.gateway.history.startmin"));
284             }
285             // Check that it is in correct range.
286             if (startMin < 0 || startHour > 59)
287                addActionError("errors.range", getGuiMessageResources().getMessage("label.gateway.history.startmin"), "00", "59");
288          }
289       }
290       else if (ResourceActions.CHANGES_OP.equals(operation)){
291          // Clear errors made so far.
292          errors.clear();
293          // Check if name is missing.
294          if (name == null || name.length() == 0)
295             addActionError("errors.required", getGuiMessageResources().getMessage("label.resource.name"));
296          // Check and see if starting date is missing.
297          if (startDateStr == null || startDateStr.length() == 0)
298             addActionError("errors.required", getGuiMessageResources().getMessage("label.gateway.history.startdate"));
299          else{
300             // Build a format using a locale pattern.
301             SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
302             // Check if it's parsable.
303             try {startDate = format.parse(startDateStr);}
304             catch (Exception e){
305                addActionError("errors.format", getGuiMessageResources().getMessage("label.gateway.history.startdate"));
306             }
307          }
308          // Check and see if ending date is missing.
309          if (endDateStr == null || endDateStr.length() == 0)
310             addActionError("errors.required", getGuiMessageResources().getMessage("label.gateway.history.enddate"));
311          else{
312             // Build a format using a locale pattern.
313             SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
314             // Check if it's parsable.
315             try {endDate = format.parse(endDateStr);}
316             catch (Exception e){
317                addActionError("errors.format", getGuiMessageResources().getMessage("label.gateway.history.enddate"));
318             }
319          }
320       }
321    }
322    
323    
324    // Override of ActionForm ---------------------------------------------------
325 
326    /**
327     * Reset form attributes.
328     * @param mapping Mapping between forwards name and path for this action
329     * @param request The servlet container request wrapper
330     */
331    public void reset(ActionMapping mapping, HttpServletRequest request){
332       super.reset(mapping, request);
333       // Reset form attributes.
334       name = null;
335       machineId = -1;
336       logDirPath = null;
337       logFilePath = null;
338       resourceTypeKey = null;
339       category = null;
340       machineIdStr = null;
341    }
342 }