KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > extensions > ShortcutParameterItem


1 /*
2  */

3 package com.sslexplorer.extensions;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Locale JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import org.apache.struts.Globals;
11 import org.apache.struts.action.ActionErrors;
12 import org.apache.struts.action.ActionMessage;
13
14 import com.sslexplorer.boot.CodedException;
15 import com.sslexplorer.boot.PropertyDefinition;
16 import com.sslexplorer.boot.PropertyList;
17 import com.sslexplorer.core.BundleActionMessage;
18 import com.sslexplorer.core.CoreException;
19 import com.sslexplorer.core.CoreUtil;
20
21 /**
22  * Wraps an {@link ApplicationParameterDefinition} for use when editing an
23  * application shortcut.
24  *
25  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
26  */

27 public class ShortcutParameterItem implements Comparable JavaDoc<ShortcutParameterItem> {
28
29     /**
30      * Dummy password used to avoid rendering real password in HTML form
31      */

32     public final static String JavaDoc DUMMY_PASSWORD = "******";
33
34     // Private instance variables
35
private ApplicationParameterDefinition definition;
36     private Object JavaDoc value;
37     private Pair[] listItems;
38     private int rows, columns;
39     private ExtensionDescriptor app;
40     private Locale JavaDoc locale;
41
42     /**
43      * Constructor.
44      *
45      * @param app extension descriptor
46      * @param definition definition
47      * @param value value
48      * @param locale locale to use for name, description etc
49      */

50     public ShortcutParameterItem(ExtensionDescriptor app, ApplicationParameterDefinition definition, String JavaDoc value, Locale JavaDoc locale) {
51         this.definition = definition;
52         this.app = app;
53         this.locale = locale;
54
55         rows = 0;
56         columns = 0;
57
58         if (definition.getType() == PropertyDefinition.TYPE_LIST) {
59             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(definition.getTypeMeta(), ",");
60             List JavaDoc<Pair> listItemsList = new ArrayList JavaDoc<Pair>();
61             while (t.hasMoreTokens()) {
62                 String JavaDoc n = t.nextToken();
63                 String JavaDoc k = "application." + app.getId() + "." + definition.getName() + ".value." + n;
64                 String JavaDoc v = app.getMessageResources().getMessage(k);
65                 Pair pair = new Pair(n, v);
66                 if (n.equals(value)) {
67                     this.value = pair.getValue();
68                 }
69                 listItemsList.add(pair);
70             }
71             listItems = new Pair[listItemsList.size()];
72             listItemsList.toArray(listItems);
73         } else if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) {
74             this.value = new PropertyList(value).getAsTextFieldText();
75             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(definition.getTypeMeta(), "x");
76             try {
77                 columns = Integer.parseInt(t.nextToken());
78                 rows = Integer.parseInt(t.nextToken());
79             } catch (NumberFormatException JavaDoc nfe) {
80
81             }
82         } else if (definition.getType() == PropertyDefinition.TYPE_TEXT_AREA) {
83             this.value = value;
84             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(definition.getTypeMeta(), "x");
85             try {
86                 columns = Integer.parseInt(t.nextToken());
87                 rows = Integer.parseInt(t.nextToken());
88             } catch (NumberFormatException JavaDoc nfe) {
89
90             }
91         } else if (definition.getType() == PropertyDefinition.TYPE_BOOLEAN) {
92             String JavaDoc trueVal = (String JavaDoc) (((List JavaDoc) definition.getTypeMetaObject()).get(0));
93             this.value = value.equals(trueVal) ? Boolean.TRUE : Boolean.FALSE;
94         } else if (definition.getType() == PropertyDefinition.TYPE_STRING) {
95             try {
96                 columns = Integer.parseInt(definition.getTypeMeta());
97             } catch (NumberFormatException JavaDoc nfe) {
98             }
99             this.value = value;
100         } else if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) {
101             try {
102                 columns = Integer.parseInt(definition.getTypeMeta());
103             } catch (NumberFormatException JavaDoc nfe) {
104             }
105             this.value = value;
106         } else if (definition.getType() == PropertyDefinition.TYPE_TIME_IN_MS) {
107             try {
108                 int val = Integer.parseInt(definition.getDefaultValue());
109                 if (definition.getTypeMeta().equalsIgnoreCase("s")) {
110                     this.value = String.valueOf(val / 1000);
111                 } else if (definition.getTypeMeta().equalsIgnoreCase("m")) {
112                     this.value = String.valueOf(val / 1000 / 60);
113                 } else if (definition.getTypeMeta().equalsIgnoreCase("h")) {
114                     this.value = String.valueOf(val / 1000 / 60 / 60);
115                 } else if (definition.getTypeMeta().equalsIgnoreCase("d")) {
116                     this.value = String.valueOf(val / 1000 / 60 / 60 / 24);
117                 }
118             } catch (Exception JavaDoc e) {
119             }
120         } else {
121             this.value = value;
122         }
123
124         //
125
}
126
127     /**
128      * If this is to be used as a password field, get the initial value. This
129      * will be a dummy password if a password has been set. If the dummy
130      * password is then received upon form submission, the password is
131      * considered unchanged.
132      *
133      * @return initial password field value
134      */

135     public String JavaDoc getPasswordValue() {
136         return getValue() == null || getValue().equals("") ? "" : DUMMY_PASSWORD;
137     }
138
139     /**
140      * Validate the value of this item against the rules in the property
141      * definition.
142      *
143      * @return error message if invalid or <code>null</code> if ok
144      * @throws Exception
145      */

146     public ActionMessage validateItem() throws Exception JavaDoc {
147         if (getType() == PropertyDefinition.TYPE_INTEGER) {
148             if (getValue().toString().trim().length() == 0) {
149                 if (!definition.isOptional()) {
150                     return new BundleActionMessage("properties", "error.integerRequired", app.getMessageResources()
151                                     .getMessage("application." + app.getId() + "." + getName() + ".name"));
152                 }
153                 return null;
154             }
155         } else if (getType() == PropertyDefinition.TYPE_STRING || getType() == PropertyDefinition.TYPE_TEXT_AREA
156             || getType() == PropertyDefinition.TYPE_PASSWORD) {
157             if (getValue().toString().trim().length() == 0) {
158                 if (!definition.isOptional()) {
159                     return new BundleActionMessage("properties", "error.requiredParameterEmpty", app.getMessageResources()
160                                     .getMessage("application." + app.getId() + "." + getName() + ".name"));
161
162                 }
163                 return null;
164             }
165         }
166
167         PropertyDefinition def = getDefinition();
168         try {
169             def.validate(String.valueOf(getPropertyValue()), getClass().getClassLoader());
170         } catch (CoreException ce) {
171             ce.getBundleActionMessage().setArg3(def.getName());
172             return ce.getBundleActionMessage();
173         }
174         return null;
175     }
176
177     /**
178      * Get if this field is optional.
179      *
180      * @return optional
181      */

182     public boolean getOptional() {
183         return definition.isOptional();
184     }
185
186     /**
187      * Get the localised name of this field.
188      *
189      * @return localised name
190      */

191     public String JavaDoc getLocalisedName() {
192         String JavaDoc key = "application." + app.getId() + "." + getName() + ".name";
193         return app.getMessageResources().getMessage(key);
194     }
195
196     /**
197      * Get the localised description of this field.
198      *
199      * @return localised description
200      */

201     public String JavaDoc getLocalisedDescription() {
202         String JavaDoc key = "application." + app.getId() + "." + getName() + ".description";
203         return app.getMessageResources().isPresent(key) ? app.getMessageResources().getMessage(locale, key) : getLocalisedName();
204     }
205
206     /**
207      * Get the localised category
208      *
209      * @return localised category
210      */

211     public String JavaDoc getLocalisedCategory() {
212         String JavaDoc key = "application." + app.getId() + ".category." + getCategory() + ".name";
213         return app.getMessageResources().isPresent(key) ? app.getMessageResources().getMessage(locale, key) : "";
214     }
215
216     /**
217      * If the field supports it, get the number of columns to use for the
218      * rendered input component.
219      *
220      * @return columns
221      */

222     public int getColumns() {
223         return columns;
224     }
225
226     /**
227      * If the field supports it, get the number of rows to use for the rendered
228      * input component.
229      *
230      * @return rows
231      */

232     public int getRows() {
233         return rows;
234     }
235
236     /**
237      * Get the application parameter definition that defines this field.
238      *
239      * @return application parameter definition
240      */

241     public PropertyDefinition getDefinition() {
242         return definition;
243     }
244
245     /**
246      * Set the application parameter definition that defines this field.
247      *
248      * @param definition application parameter definition
249      */

250     public void setDefinition(ApplicationParameterDefinition definition) {
251         this.definition = definition;
252     }
253
254     /**
255      * Convience method to get the name of the definition.
256      *
257      * @return definition name
258      */

259     public String JavaDoc getName() {
260         return definition.getName();
261     }
262
263     /**
264      * Convience method to get the category of the definition.
265      *
266      * @return category
267      */

268     public int getCategory() {
269         return definition.getCategory();
270     }
271
272     /**
273      * Convience method to get the default value of the definition.
274      *
275      * @return default value
276      */

277     public String JavaDoc getDefaultValue() {
278         return definition.getDefaultValue();
279     }
280
281     /**
282      * Get the default value to use for the rendered input component.
283      *
284      * @return default type
285      */

286     public String JavaDoc getDefaultText() {
287         String JavaDoc val = getDefaultValue();
288         if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) {
289             val = "";
290         } else if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) {
291             PropertyList list = new PropertyList(definition.getDefaultValue());
292             val = list.size() > 0 ? list.getPropertyItem(0) : "";
293         } else if (definition.getType() == PropertyDefinition.TYPE_LIST) {
294             try {
295                 int defaultItem = Integer.parseInt(definition.getDefaultValue());
296                 String JavaDoc k = "application." + app.getId() + "." + definition.getName() + ".value." + defaultItem;
297                 val = app.getMessageResources().getMessage(k);
298             } catch (Exception JavaDoc e) {
299             }
300         } else if (definition.getType() == PropertyDefinition.TYPE_TIME_IN_MS) {
301             try {
302                 int defaultItem = Integer.parseInt(getDefaultValue());
303                 if (definition.getTypeMeta().equalsIgnoreCase("s")) {
304                     val = String.valueOf(defaultItem / 1000);
305                 } else if (definition.getTypeMeta().equalsIgnoreCase("m")) {
306                     val = String.valueOf(defaultItem / 1000 / 60);
307                 } else if (definition.getTypeMeta().equalsIgnoreCase("h")) {
308                     val = String.valueOf(defaultItem / 1000 / 60 / 60);
309                 } else if (definition.getTypeMeta().equalsIgnoreCase("d")) {
310                     val = String.valueOf(defaultItem / 1000 / 60 / 60 / 24);
311                 } else {
312                     val = String.valueOf(val);
313                 }
314             } catch (Exception JavaDoc e) {
315                 val = String.valueOf(getDefaultValue());
316             }
317         }
318         if (val.length() > 15) {
319             val = val.substring(0, 15);
320         }
321         return val;
322     }
323
324     /**
325      * Convenience method to get the type meta information from the definition
326      *
327      * @return type meta
328      */

329     public String JavaDoc getTypeMeta() {
330         return definition.getTypeMeta();
331     }
332
333     /**
334      * Get an array of all name value pairs if the component supports it (i.e.
335      * multi-entry).
336      *
337      * @return name value pairs
338      */

339     public Pair[] getListItems() {
340         return listItems;
341     }
342
343     /**
344      * Get the value as an object.
345      *
346      * @return value
347      */

348     public Object JavaDoc getValue() {
349         return value;
350     }
351
352     /**
353      * For use by checkbox components, get if the value is <code>true</code>.
354      *
355      * @return checkbox selected
356      */

357     public boolean getSelected() {
358         return value.equals(Boolean.TRUE);
359     }
360
361     /**
362      * For use by checkbox components, set if the value is <code>true</code>.
363      *
364      * @param selected checkbox selected
365      */

366     public void setSelected(boolean selected) {
367         this.value = Boolean.valueOf(selected);
368     }
369
370     /**
371      * Set the value as an object.
372      *
373      * @param value value
374      */

375     public void setValue(Object JavaDoc value) {
376         if (getType() != PropertyDefinition.TYPE_PASSWORD || !value.equals(DUMMY_PASSWORD)) {
377             this.value = value;
378         }
379     }
380
381     /**
382      * Convenience method to get the field type from the definition.
383      *
384      * @return field type
385      */

386     public int getType() {
387         return definition.getType();
388     }
389
390     /**
391      * Get the value to actually store. The format of this will differ depending
392      * on the type.
393      *
394      * @return property value
395      */

396     public Object JavaDoc getPropertyValue() {
397         if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) {
398             PropertyList l = new PropertyList();
399             l.setAsTextFieldText(getValue().toString());
400             return l.getAsPropertyText();
401         } else if (definition.getType() == PropertyDefinition.TYPE_BOOLEAN) {
402             String JavaDoc trueVal = (String JavaDoc) (((List JavaDoc) definition.getTypeMetaObject()).get(0));
403             String JavaDoc falseVal = (String JavaDoc) (((List JavaDoc) definition.getTypeMetaObject()).get(1));
404             return Boolean.TRUE.equals(getValue()) ? trueVal : falseVal;
405         }
406         return getValue();
407     }
408
409     /*
410      * (non-Javadoc)
411      *
412      * @see java.lang.Comparable#compareTo(java.lang.Object)
413      */

414     public int compareTo(ShortcutParameterItem obj) {
415         return definition.compareTo(obj.getDefinition());
416     }
417
418     /**
419      * Encapsulate a value / label pair
420      *
421      * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
422      */

423     public class Pair {
424         Object JavaDoc value;
425         String JavaDoc label;
426
427         /**
428          * Constructor.
429          *
430          * @param value
431          * @param label
432          */

433         public Pair(Object JavaDoc value, String JavaDoc label) {
434             this.value = value;
435             this.label = label;
436         }
437
438         /**
439          * Get the value
440          *
441          * @return value
442          */

443         public Object JavaDoc getValue() {
444             return value;
445         }
446
447         /**
448          * Get the label
449          *
450          * @return label
451          */

452         public String JavaDoc getLabel() {
453             return label;
454         }
455
456         /**
457          * Set the value
458          *
459          * @param value value
460          */

461         public void setValue(Object JavaDoc value) {
462             this.value = value;
463         }
464
465         /*
466          * (non-Javadoc)
467          *
468          * @see java.lang.Object#toString()
469          */

470         public String JavaDoc toString() {
471             return "pair[label=" + label + ",value=" + value + "]";
472         }
473     }
474
475 }
Popular Tags