KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > PropertyItem


1 /*
2  */

3 package com.sslexplorer.properties;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Locale JavaDoc;
9 import java.util.StringTokenizer JavaDoc;
10
11 import javax.servlet.ServletContext JavaDoc;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.apache.struts.Globals;
17 import org.apache.struts.action.Action;
18 import org.apache.struts.config.ModuleConfig;
19 import org.apache.struts.util.MessageResources;
20 import org.apache.struts.util.ModuleUtils;
21
22 import com.sslexplorer.boot.PropertyDefinition;
23 import com.sslexplorer.boot.PropertyList;
24 import com.sslexplorer.boot.TypeMetaListItem;
25 import com.sslexplorer.core.CoreServlet;
26 import com.sslexplorer.input.MultiSelectDataSource;
27 import com.sslexplorer.input.MultiSelectSelectionModel;
28 import com.sslexplorer.security.LogonControllerFactory;
29 import com.sslexplorer.security.SessionInfo;
30
31 /**
32  * Wrapper bean used for displaying {@link com.sslexplorer.boot.PropertyDefinition}s
33  * and their values in the various configuration pages.
34  *
35  * @author Brett Smith <a HREF="mailto:brett@3sp.com">&lt;brett@3sp.com&gt;</a>
36  */

37 public class PropertyItem implements Comparable JavaDoc<PropertyItem> {
38     
39     final static Log log = LogFactory.getLog(PropertyItem.class);
40     
41     // Private instance variables
42

43     private PropertyDefinition definition;
44     private Object JavaDoc value;
45     private Pair[] listItems;
46     private int rows, columns;
47     private MultiSelectSelectionModel listDataSourceModel;
48     private HttpServletRequest JavaDoc request;
49     
50     /**
51      * Constructor
52      *
53      * @param action the struts action used to display this bean
54      * @param request the request object used to display the bean
55      * @param definition the property definition to display
56      * @param value the value to display
57      * @throws IllegalArgumentException if value is invalid
58      */

59     public PropertyItem(Action action, HttpServletRequest JavaDoc request,
60             PropertyDefinition definition, String JavaDoc value) {
61         if(value == null) {
62             throw new IllegalArgumentException JavaDoc("Value may not be null.");
63         }
64     
65         this.request = request;
66         this.definition = definition;
67
68         rows = 0;
69         columns = 0;
70
71         if (definition.getType() == PropertyDefinition.TYPE_LIST) {
72             List JavaDoc listItemsList = new ArrayList JavaDoc();
73             if (!definition.getTypeMeta().startsWith("!")) {
74                 for (Iterator JavaDoc i = ((List JavaDoc) definition.getTypeMetaObject()).iterator(); i.hasNext();) {
75                     TypeMetaListItem item = (TypeMetaListItem) i.next();
76                     ServletContext JavaDoc context = CoreServlet.getServlet().getServletContext();
77                     ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, context);
78                     String JavaDoc mrKey = (item.getMessageResourcesKey() == null ? "properties" : item.getMessageResourcesKey())
79                                     + moduleConfig.getPrefix();
80                     MessageResources res = (MessageResources) context.getAttribute(mrKey);
81                     String JavaDoc k = definition.getName() + ".value." + item.getValue();
82                     String JavaDoc v = "";
83                     if (res != null) {
84                         v = res.getMessage((Locale JavaDoc)request.getSession().getAttribute(Globals.LOCALE_KEY), k);
85                         if (v == null) {
86                             v = item.getValue();
87                         }
88                     }
89                     Pair pair = new Pair(item.getValue(), v);
90                     if (item.getValue().equals(value)) {
91                         this.value = pair.getValue();
92                     }
93                     listItemsList.add(pair);
94                 }
95             }
96             else {
97                 String JavaDoc className = definition.getTypeMeta().substring(1);
98                 try {
99                     Class JavaDoc clazz = Class.forName(className);
100                     Object JavaDoc obj = clazz.newInstance();
101                     if(obj instanceof PairListDataSource)
102                         listItemsList.addAll(((PairListDataSource)obj).getValues(request));
103                     else
104                         throw new Exception JavaDoc("Not a PairListDataSource.");
105                 }
106                 catch(Exception JavaDoc e) {
107                     log.error("Failed to create list data source.", e);
108                 }
109                 this.value = value;
110             }
111             listItems = new Pair[listItemsList.size()];
112             listItemsList.toArray(listItems);
113         } else if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) {
114             this.value = new PropertyList(value).getAsTextFieldText();
115             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(definition.getTypeMeta(),
116                     "x");
117             try {
118                 if(t.hasMoreTokens()) {
119                     columns = Integer.parseInt(t.nextToken());
120                     rows = Integer.parseInt(t.nextToken());
121                 }
122             } catch (NumberFormatException JavaDoc nfe) {
123
124             }
125         } else if (definition.getType() == PropertyDefinition.TYPE_TEXT_AREA) {
126             this.value = value;
127             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(definition.getTypeMeta(),
128                     "x");
129             try {
130                 columns = Integer.parseInt(t.nextToken());
131                 rows = Integer.parseInt(t.nextToken());
132             } catch (NumberFormatException JavaDoc nfe) {
133
134             }
135         } else if (definition.getType() == PropertyDefinition.TYPE_BOOLEAN) {
136             if(definition.getTypeMetaObject() != null) {
137                 String JavaDoc trueVal = (String JavaDoc)(((List JavaDoc)definition.getTypeMetaObject()).get(0));
138                 this.value = value.equals(trueVal) ? Boolean.TRUE : Boolean.FALSE;
139             }
140             else {
141                 this.value = Boolean.valueOf(value);
142             }
143         } else if (definition.getType() == PropertyDefinition.TYPE_STRING) {
144             try {
145                 columns = Integer.parseInt(definition.getTypeMeta());
146             } catch (NumberFormatException JavaDoc nfe) {
147             }
148             this.value = value;
149         } else if (definition.getType() == PropertyDefinition.TYPE_INTEGER) {
150             try {
151                 columns = Integer.parseInt(definition.getTypeMeta());
152             } catch (NumberFormatException JavaDoc nfe) {
153             }
154             this.value = value;
155         } else if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) {
156             try {
157                 columns = Integer.parseInt(definition.getTypeMeta());
158             } catch (NumberFormatException JavaDoc nfe) {
159             }
160             this.value = value;
161         } else if (definition.getType() == PropertyDefinition.TYPE_TIME_IN_MS) {
162             try {
163                 int val = Integer.parseInt(value);
164                 if (definition.getTypeMeta().equalsIgnoreCase("s")) {
165                     this.value = String.valueOf(val / 1000);
166                 } else if (definition.getTypeMeta().equalsIgnoreCase("m")) {
167                     this.value = String.valueOf(val / 1000 / 60);
168                 } else if (definition.getTypeMeta().equalsIgnoreCase("h")) {
169                     this.value = String.valueOf(val / 1000 / 60 / 60);
170                 } else if (definition.getTypeMeta().equalsIgnoreCase("d")) {
171                     this.value = String.valueOf(val / 1000 / 60 / 60 / 24);
172                 } else {
173                     this.value = String.valueOf(val);
174                 }
175             } catch (Exception JavaDoc e) {
176                 this.value = value;
177             }
178         } else if (definition.getType() == PropertyDefinition.TYPE_MULTI_SELECT_LIST) {
179             PropertyList pList = new PropertyList(value);
180             this.value = pList.getAsTextFieldText();
181             String JavaDoc clazz = definition.getTypeMeta();
182             SessionInfo session = LogonControllerFactory.getInstance().getSessionInfo(request);
183             try {
184                 listDataSourceModel = new MultiSelectSelectionModel(session, ((MultiSelectDataSource)Class.forName(clazz).newInstance()), pList);
185             }
186             catch(Throwable JavaDoc t) {
187                 log.error("Failed to list of available of values. " , t);
188             }
189             
190         } else {
191             this.value = value;
192         }
193
194         //
195
}
196     
197     /**
198      * Get the message resource key (i.e. bundle ID) of the bundle that
199      * contains the message resources for this property definition.
200      *
201      * @return message resources key (bundle)
202      */

203     public String JavaDoc getMessageResourcesKey() {
204         return definition.getMessageResourcesKey();
205     }
206
207     /**
208      * Get the number of columns appropriate for this property definition. Only
209      * applies to property definitions of type
210      * {@link PropertyDefinition#TYPE_PASSWORD},
211      * {@link PropertyDefinition#TYPE_STRING},
212      * {@link PropertyDefinition#TYPE_TEXT_AREA} and
213      * {@link PropertyDefinition#TYPE_TIME_IN_MS}
214      *
215      * @return number of columns
216      */

217     public int getColumns() {
218         return columns;
219     }
220
221     /**
222      * Get the number of row appropriate for this property definition. Only
223      * applies to property definitions of type
224      * {@link PropertyDefinition#TYPE_TEXT_AREA}.
225      *
226      * @return number of rows
227      */

228     public int getRows() {
229         return rows;
230     }
231
232     /**
233      * Get the property definition this object wraps
234      *
235      * @return property definition
236      */

237     public PropertyDefinition getDefinition() {
238         return definition;
239     }
240
241     /**
242      * Set the property definition this object wraps
243      *
244      * @param definition property definition
245      */

246     public void setDefinition(PropertyDefinition definition) {
247         this.definition = definition;
248     }
249
250     /**
251      * Convience method to get the name of the property definition this object wraps.
252      *
253      * @return the name.
254      */

255     public String JavaDoc getName() {
256         return definition.getName();
257     }
258
259     /**
260      * Convience method to get the category ID this property definition this
261      * object wraps is in.
262      *
263      * @return the category ID.
264      */

265     public int getCategory() {
266         return definition.getCategory();
267     }
268
269     /**
270      * Convience method to get the default value of the property defintiion
271      * this object wraps.
272      *
273      * @return Returns the value.
274      */

275     public String JavaDoc getDefaultValue() {
276         return definition.getDefaultValue();
277     }
278
279     /**
280      * Get the default of the property definition this object wraps as
281      * text.
282      *
283      * @return Returns the value.
284      */

285     public String JavaDoc getDefaultText() {
286         String JavaDoc val = getDefaultValue();
287         try {
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             for(int i = 0 ; i < listItems.length; i++) {
295                 if(definition.getDefaultValue().equals(listItems[i].getValue())) {
296                     val = listItems[i].getLabel();
297                     break;
298                 }
299             }
300         } else if (definition.getType() == PropertyDefinition.TYPE_TIME_IN_MS) {
301             try {
302                 int defaultItem = Integer
303                         .parseInt(definition.getDefaultValue());
304                 if (definition.getTypeMeta().equalsIgnoreCase("s")) {
305                     val = String.valueOf(defaultItem / 1000);
306                 } else if (definition.getTypeMeta().equalsIgnoreCase("m")) {
307                     val = String.valueOf(defaultItem / 1000 / 60);
308                 } else if (definition.getTypeMeta().equalsIgnoreCase("h")) {
309                     val = String.valueOf(defaultItem / 1000 / 60 / 60);
310                 } else if (definition.getTypeMeta().equalsIgnoreCase("d")) {
311                     val = String.valueOf(defaultItem / 1000 / 60 / 60 / 24);
312                 } else {
313                     val = String.valueOf(val);
314                 }
315             } catch (Exception JavaDoc e) {
316                 val = String.valueOf(val);
317             }
318         }
319         if (val.length() > 15) {
320             val = val.substring(0, 15);
321         }
322         }
323         catch(Throwable JavaDoc t) {
324         }
325         return val;
326     }
327
328     /**
329      * Convience method to get the property definition 'type meta', the
330      * string that describes and constraints for the type of property definition.
331      * For example 40x5 may be the 'type meta' for a property definition of
332      * type {@link PropertyDefinition#TYPE_TEXT_AREA} means supply a text area
333      * with 40 columns and 5 rows.
334      *
335      * @return get the type meta
336      */

337     public String JavaDoc getTypeMeta() {
338         return definition.getTypeMeta();
339     }
340
341     /**
342      * @return Returns the value.
343      */

344     public Pair[] getListItems() {
345         return listItems;
346     }
347
348     /**
349      * Get the value as an object
350      *
351      * @return the value.
352      */

353     public Object JavaDoc getValue() {
354         return value;
355     }
356
357     /**
358      * For boolean property definitions such as checkkbox
359      * that will return <code>true</code> if the value is true.
360      *
361      * @return selected.
362      */

363     public boolean getSelected() {
364         return value.equals(Boolean.TRUE);
365     }
366
367     /**
368      * Set the value as a boolean. For boolean property definitions such as
369      * checkbox.
370      *
371      * @param selected selected
372      */

373     public void setSelected(boolean selected) {
374         this.value = Boolean.valueOf(selected);
375     }
376
377     /**
378      * Set the value from a generic object. The actual value will
379      * be determined depending on the property definitions type.
380      *
381      * @param value the value to set.
382      */

383     public void setValue(Object JavaDoc value) {
384         this.value = value;
385         if(getType() == PropertyDefinition.TYPE_MULTI_SELECT_LIST) {
386             PropertyList l = new PropertyList();
387             l.setAsTextFieldText(getValue().toString());
388             String JavaDoc clazz = definition.getTypeMeta();
389             try {
390                 SessionInfo session = LogonControllerFactory.getInstance().getSessionInfo(request);
391                 listDataSourceModel = new MultiSelectSelectionModel(session, ((MultiSelectDataSource)Class.forName(clazz).newInstance()), l);
392             }
393             catch(Throwable JavaDoc t) {
394                 log.error("Failed to list of available of values. " , t);
395             }
396         }
397     }
398
399     /**
400      * Convience method to get the type of the property definition. See
401      * {@link PropertyDefinition#getType()} for a more details description.
402      *
403      * @return type
404      */

405     public int getType() {
406         return definition.getType();
407     }
408
409     /**
410      * Get the value as an object suitable for displaying with the appropriate
411      * component. For most types a {@link String} is returned, but other
412      * default struts supported type are also used e.g. {@link Boolean} or
413      * {@link Integer}.
414      *
415      * @return value
416      */

417     public Object JavaDoc getPropertyValue() {
418         if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST ||
419                         definition.getType() == PropertyDefinition.TYPE_MULTI_SELECT_LIST) {
420             PropertyList l = new PropertyList();
421             l.setAsTextFieldText(getValue().toString());
422             return l.getAsPropertyText();
423         }
424         else if (getDefinition().getType() == PropertyDefinition.TYPE_TIME_IN_MS) {
425             try {
426                 int v = Integer.parseInt(getValue().toString());
427                 if (getDefinition().getTypeMeta().equalsIgnoreCase("s")) {
428                     v = v * 1000;
429                 } else if (getDefinition().getTypeMeta().equalsIgnoreCase("m")) {
430                     v = v * 1000 * 60;
431                 } else if (getDefinition().getTypeMeta().equalsIgnoreCase("h")) {
432                     v = v * 1000 * 60 * 60;
433                 } else if (getDefinition().getTypeMeta().equalsIgnoreCase("d")) {
434                     v = v * 1000 * 60 * 60 * 24;
435                 }
436                 return String.valueOf(v);
437             } catch (Exception JavaDoc e) {
438
439             }
440         }
441         return getValue();
442     }
443     
444     /**
445      * Get the list data source model that may be used as the source list
446      * for property definitions of type {@link PropertyDefinition#TYPE_MULTI_SELECT_LIST}
447      *
448      * @return list data source model
449      */

450     
451     public MultiSelectSelectionModel getListDataSourceModel() {
452         return listDataSourceModel;
453     }
454
455     public int compareTo(PropertyItem o) {
456         return getDefinition().compareTo(o.getDefinition());
457     }
458 }
Popular Tags