View Javadoc

1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //   this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //   this list of conditions and the following disclaimer in the documentation  
12  //   and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //   nor the names of its contributors may be used to endorse or promote products  
15  //   derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  
29  package org.objectledge.parameters;
30  
31  import java.beans.IndexedPropertyDescriptor;
32  import java.beans.Introspector;
33  import java.beans.PropertyDescriptor;
34  import java.lang.reflect.Method;
35  import java.math.BigDecimal;
36  import java.util.Date;
37  
38  /***
39   * Uses bean introspection to set writable properties of bean from
40   * the parameters.
41   * 
42   * <p>This class includes code from Jakarta Turbine project and parts of it are subject to
43   * Apache Software Foundation License included in ObjectLedge distribution
44   * (file /ASFLicense.txt).</p>
45   *
46   * @author <a href="maito:mgolebsk@elka.pw.edu.pl">Marcin Golebski</a>
47   * @created 2005-07-26
48   * @version $Id: RequestParametersUtils.java,v 1.1 2005/08/26 07:14:15 zwierzem Exp $
49   */
50  public class RequestParametersUtils
51  {
52      /***
53       * Uses bean introspection to set writable properties of bean from
54       * the parameters, where a (case-insensitive) name match between
55       * the bean property and the parameter is looked for.
56       *
57       * @param bean An Object.
58       * @throws Exception 
59       * @exception Exception a generic exception.
60       */
61      public static void setProperties(RequestParameters params, Object bean) 
62      throws Exception 
63      {
64          
65          Class beanClass = bean.getClass();
66          PropertyDescriptor[] props
67                  = Introspector.getBeanInfo(beanClass).getPropertyDescriptors();
68  
69          for (int i = 0; i < props.length; i++)
70          {
71              String propname = props[i].getName();
72              Method setter = props[i].getWriteMethod();
73              if (setter != null &&
74                      (containsKey(params, propname)     ))
75  //TODO                    
76  //                    ||
77  //                    containsDateSelectorKeys(propname) ||
78  //                    containsTimeSelectorKeys(propname)))
79              {
80                  setProperty(params, bean, props[i]);
81              }
82          }
83      }   
84  
85  
86      /***
87       * Uses bean introspection to set writable properties of bean from
88       * the parameters, where a (case-insensitive) name match between
89       * the bean property and the parameter is looked for.
90       *
91       * @param bean An Object.
92       * @throws Exception 
93       * @exception Exception a generic exception.
94       */
95      //TODO
96      private static void setPropertiesForSearch(RequestParameters params, Object bean) 
97      throws Exception 
98      {
99          
100         Class beanClass = bean.getClass();
101         PropertyDescriptor[] props
102                 = Introspector.getBeanInfo(beanClass).getPropertyDescriptors();
103 
104         for (int i = 0; i < props.length; i++)
105         {
106             String propname = props[i].getName();
107             Method setter = props[i].getWriteMethod();
108             if (setter != null &&
109                     containsKey(params, propname)     )
110             {
111                 setProperty(params, bean, props[i]);
112             }
113         }
114     }    
115 
116     
117     /***
118      * Determine whether a given key has been inserted.  All keys are
119      * stored in lowercase strings, so override method to account for
120      * this.
121      *
122      * @param key An Object with the key to search for.
123      * @return True if the object is found.
124      */
125     private static boolean containsKey(RequestParameters params, Object key)
126     {
127         String tmp = convert((String)key);
128         return params.isDefined(tmp) && !params.get(tmp).equals("");
129     }    
130     
131     /***
132      * Trims the string data and applies the conversion specified in
133      * the property given by URL_CASE_FOLDING.  It returns a new
134      * string so that it does not destroy the value data.
135      *
136      * @param value A String to be processed.
137      * @return A new String converted to lowercase and trimmed.
138      */
139     private static String convert(String value)
140     {
141         return value.trim();
142         //.toLowerCase();
143     }    
144     
145 
146     /***
147      * Set the property 'prop' in the bean to the value of the
148      * corresponding parameters.  Supports all types supported by
149      * getXXX methods plus a few more that come for free because
150      * primitives have to be wrapped before being passed to invoke
151      * anyway.
152      *
153      * @param bean An Object.
154      * @param prop A PropertyDescriptor.
155      * @exception Exception a generic exception.
156      */
157     private static void setProperty(RequestParameters params, Object bean,
158                                PropertyDescriptor prop)
159             throws Exception
160     {
161         if (prop instanceof IndexedPropertyDescriptor)
162         {
163             throw new Exception(prop.getName() +
164                     " is an indexed property (not supported)");
165         }
166 
167         Method setter = prop.getWriteMethod();
168         if (setter == null)
169         {
170             throw new Exception(prop.getName() +
171                     " is a read only property");
172         }
173 
174         Class propclass = prop.getPropertyType();
175         Object[] args = {null};
176 
177         if (propclass == String.class)
178         {
179             args[0] = params.get(prop.getName());
180         }
181         else if (propclass == Integer.class || propclass == Integer.TYPE)
182         {
183             args[0] = params.getInt(prop.getName());
184         }
185         else if (propclass == Long.class || propclass == Long.TYPE)
186         {
187             args[0] = new Long(params.getLong(prop.getName()));
188         }
189         else if (propclass == Short.class || propclass == Short.TYPE)
190         {
191             args[0] = new Short((short)params.getLong(prop.getName()));
192         }
193         else if (propclass == Byte.class || propclass == Byte.TYPE)
194         {
195             args[0] = new Byte((byte)params.getLong(prop.getName()));
196         }           
197         else if (propclass == Character.class || propclass == Character.TYPE)
198         {
199             args[0] = new Character(params.get(prop.getName()).charAt(0));
200         }           
201         else if (propclass == Boolean.class || propclass == Boolean.TYPE)
202         {
203             args[0] = params.getBoolean(prop.getName());
204         }
205         else if (propclass == Double.class || propclass == Double.TYPE)
206         {
207             //FIXME
208 //            args[0] = new Double(getDouble(prop.getName()));
209             args[0] = new Double(params.getFloat(prop.getName()));
210         }
211         else if (propclass == BigDecimal.class)
212         {
213             //FIXME
214 //            args[0] = getBigDecimal(prop.getName());
215             args[0] = new BigDecimal(params.getFloat(prop.getName()));
216         }
217         else if (propclass == String[].class)
218         {
219             args[0] = params.getStrings(prop.getName());
220         }
221 //        else if (propclass == Object.class)
222 //        {
223 //            args[0] = params.getObject(prop.getName());
224 //        }
225         else if (propclass == int[].class)
226         {
227             args[0] = params.getInts(prop.getName());
228         }
229         else if (propclass == Integer[].class)
230         {
231 //            args[0] = getIntObjects(prop.getName());
232             args[0] = new Integer(params.getInt(prop.getName()));
233         }
234         else if (propclass == Date.class)
235         {
236             args[0] = params.getDate(prop.getName());
237         }
238 //        else if (propclass == NumberKey.class)
239 //        {
240 //            args[0] = getNumberKey(prop.getName());
241 //        }
242 //        else if (propclass == StringKey.class)
243 //        {
244 //            args[0] = getStringKey(prop.getName());
245 //        }
246         else
247         {
248             throw new Exception("property "
249                     + prop.getName()
250                     + " is of unsupported type "
251                     + propclass.toString());
252         }
253 
254         setter.invoke(bean, args);
255     }
256     
257   
258 }
259