KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > communicator > applications > webtalk > controller > FeatureManagementForm


1 /*
2  * FeatureManagementForm.java
3  *
4  * Form Bean for generic, or 'Other' features
5  */

6
7 package com.quikj.application.communicator.applications.webtalk.controller;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import org.apache.struts.action.*;
11 import java.util.*;
12
13 /**
14  *
15  * @author bhm
16  */

17 public class FeatureManagementForm extends ActionForm
18 {
19     
20     /** Holds value of property name. */
21     private String JavaDoc name;
22     
23     /** Holds value of property submit. */
24     private String JavaDoc submit = "Find";
25     
26     /** Holds value of property className. */
27     private String JavaDoc className;
28     
29     /** Holds value of property active. */
30     private boolean active = false;
31     
32     /** Holds value of property params. */
33     private String JavaDoc params;
34     
35     /** Holds value of property paramsList. */
36     private HashMap paramsList;
37     
38     /** Creates a new instance of GroupManagementForm */
39     public FeatureManagementForm()
40     {
41     }
42     
43     /** Getter for property name.
44      * @return Value of property name.
45      *
46      */

47     public String JavaDoc getName()
48     {
49         return this.name;
50     }
51     
52     /** Setter for property name.
53      * @param name New value of property name.
54      *
55      */

56     public void setName(String JavaDoc name)
57     {
58         this.name = name.trim();
59     }
60     
61     /** Getter for property submit.
62      * @return Value of property submit.
63      *
64      */

65     public String JavaDoc getSubmit()
66     {
67         return this.submit;
68     }
69     
70     /** Setter for property submit.
71      * @param submit New value of property submit.
72      *
73      */

74     public void setSubmit(String JavaDoc submit)
75     {
76         this.submit = submit;
77     }
78     
79     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request)
80     {
81         // Check for mandatory data
82
ActionErrors errors = new ActionErrors();
83         
84         if ((name == null) || (name.length() == 0))
85         {
86             errors.add("name", new ActionError("error.feature.no.name"));
87         }
88         
89         if (DataCheckUtility.followsTableIdRules(name) == false)
90         {
91             errors.add("name", new ActionError("error.feature.invalid.id"));
92         }
93         
94         // general checks for create/modify
95
if ((submit.equals("Create") == true) || (submit.equals("Modify") == true))
96         {
97             if ((className == null) || (className.length() == 0))
98             {
99                 errors.add("className", new ActionError("error.feature.no.className"));
100             }
101             
102             // validate params input, convert to featureParams
103
processFeatureParams(getParams(), errors);
104             
105         }
106         
107         return errors;
108     }
109     
110     public void reset()
111     {
112         name = null;
113         submit = "Find";
114         className = null;
115         active = false;
116         params = null;
117     }
118     
119     /** Getter for property className.
120      * @return Value of property className.
121      *
122      */

123     public String JavaDoc getClassName()
124     {
125         return this.className;
126     }
127     
128     /** Setter for property className.
129      * @param className New value of property className.
130      *
131      */

132     public void setClassName(String JavaDoc className)
133     {
134         this.className = className;
135     }
136     
137     /** Getter for property active.
138      * @return Value of property active.
139      *
140      */

141     public boolean isActive()
142     {
143         return this.active;
144     }
145     
146     /** Setter for property active.
147      * @param active New value of property active.
148      *
149      */

150     public void setActive(boolean active)
151     {
152         this.active = active;
153     }
154     
155     /** Getter for property params.
156      * @return Value of property params.
157      *
158      */

159     public String JavaDoc getParams()
160     {
161         return this.params;
162     }
163     
164     /** Setter for property params.
165      * @param params New value of property params.
166      *
167      */

168     public void setParams(String JavaDoc params)
169     {
170         this.params = params;
171     }
172     
173     /** Getter for property paramsList.
174      * @return Value of property paramsList.
175      *
176      */

177     public HashMap getParamsList()
178     {
179         return this.paramsList;
180     }
181     
182     /** Setter for property paramsList.
183      * @param paramsList New value of property paramsList.
184      *
185      */

186     public void setParamsList(HashMap paramsList)
187     {
188         this.paramsList = paramsList;
189         params = null;
190         
191         if (paramsList != null)
192         {
193             Set key_set = paramsList.keySet();
194             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
195             
196             for (Iterator i = key_set.iterator(); i.hasNext();)
197             {
198                 String JavaDoc key = (String JavaDoc) i.next();
199                 String JavaDoc value = (String JavaDoc) paramsList.get(key);
200                 
201                 buf.append(Utilities.escapeEqual(key) + "=" + Utilities.escapeEqual(value));
202                 if (i.hasNext() == true)
203                 {
204                     buf.append('\n');
205                 }
206             }
207             
208             params = buf.toString();
209         }
210     }
211     
212     private void processFeatureParams(String JavaDoc input, ActionErrors errors)
213     {
214         /*
215          * USER INPUT RULES:
216          *
217          * zero or more pairs of: key = value (spaces around '=' doesn't matter)
218          * if > one pair, pairs must be separated by <CR> or newline
219          * <CR> or newline not allowed in any key or any value
220          * no duplicate keys allowed
221          * if key or value contains: =, it must be escaped as: &eq;
222          * if key or value contains: &, it must be escaped as: &amp;
223          *
224          */

225         
226         if (input != null)
227         {
228             input = input.trim();
229             if (input.length() > 0)
230             {
231                 StringTokenizer strtok = new StringTokenizer(input, "\n");
232                 int num_pairs = strtok.countTokens();
233                 
234                 paramsList = new HashMap();
235                 
236                 for (int i = 0; i < num_pairs; i++)
237                 {
238                     String JavaDoc pair = strtok.nextToken();
239                     
240                     StringTokenizer pairtok = new StringTokenizer(pair, "=", true);
241                     int num_subparms = pairtok.countTokens();
242                     
243                     if (num_subparms != 3)
244                     {
245                         errors.add("params", new ActionError("error.feature.params.pairtokens",
246                         new Integer JavaDoc(i + 1)));
247                         
248                         continue;
249                     }
250                     
251                     String JavaDoc input_key = pairtok.nextToken().trim();
252                     pairtok.nextToken();
253                     String JavaDoc input_value = pairtok.nextToken().trim();
254                     
255                     String JavaDoc key = Utilities.deEscapeEqual(input_key);
256                     String JavaDoc value = Utilities.deEscapeEqual(input_value);
257                     
258                     if (paramsList.containsKey(key) == true)
259                     {
260                         errors.add("params", new ActionError("error.feature.params.duplicatekey",
261                         new Integer JavaDoc(i + 1)));
262                         
263                         continue;
264                     }
265                     
266                     paramsList.put(key, value);
267                 }
268             }
269         }
270     }
271     
272     
273 }
274
Popular Tags