View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.om.security.turbine;
18  
19  import java.util.List;
20  import java.util.Hashtable;
21  import java.util.ArrayList;
22  import com.workingdogs.village.*;
23  
24  import org.apache.torque.util.Criteria;
25  import org.apache.torque.TorqueException;
26  import org.apache.torque.om.NumberKey;
27  import org.apache.turbine.util.ObjectUtils;
28  
29  import org.apache.jetspeed.om.security.JetspeedUser;
30  import org.apache.jetspeed.om.security.JetspeedUserFactory;
31  
32  
33  // Local classes
34  
35  /*** 
36   * The skeleton for this class was autogenerated by Torque on:
37   *
38   * [Wed May 29 23:45:25 PDT 2002]
39   *
40   *  You should add additional methods to this class to meet the
41   *  application requirements.  This class will only be generated as
42   *  long as it does not already exist in the output directory.
43   */
44  public class TurbineUserPeer 
45      extends org.apache.jetspeed.om.security.turbine.BaseTurbineUserPeer
46  {
47  
48      /*** The schema. */
49      private static Schema schema = initTableSchema(TABLE_NAME);
50  
51      /*** The columns. */
52      private static com.workingdogs.village.Column[] columns = 
53          initTableColumns(schema);
54  
55  
56      /*** The names of the columns. */
57      public static String[] columnNames = initColumnNames(columns);
58  
59  
60      /*** 
61        * Selects Jetspeed Users or a derived class there of, creating the
62        * JetspeedUsers,  and populating each user's hashtable of attributes
63        * from the result set of the query
64        *
65        * @param criteria the query criteria.
66        * @return a List of JetspeedUsers
67        */
68      public static List doSelectUsers( Criteria criteria ) throws TorqueException
69      {
70          return populateUserObjects( doSelectVillageRecords(criteria) );
71      }
72  
73  
74      /***
75       * The returned list will contain objects of the default type or
76       * objects that inherit from the default.
77       */
78      public static List populateUserObjects(List records)
79          throws TorqueException
80      {
81          List results = new ArrayList(records.size());
82  
83          // populate the object(s)
84          for ( int i=0; i<records.size(); i++ )
85          {
86              Record row = (Record)records.get(i);
87              results.add(TurbineUserPeer.row2UserObject(row, 1, TurbineUserPeer.getOMClass()));
88          }
89          return results;
90      }
91  
92  
93      /***
94       * Create a new object of type cls from a resultset row starting
95       * from a specified offset.  This is done so that you can select
96       * other rows than just those needed for this object.  You may
97       * for example want to create two objects from the same row.
98       */
99      public static JetspeedUser row2UserObject (Record row,
100                                               int offset,
101                                               Class cls )
102         throws TorqueException
103     {
104         try
105         {
106             //TurbineUser obj = (TurbineUser)cls.newInstance();
107             //populateObject(row, offset, obj);
108             //obj.setModified(false);
109             //obj.setNew(false);
110             JetspeedUser user = JetspeedUserFactory.getInstance(false);
111             populateUserObject(row, offset, user);
112             return user;
113         }
114         catch (Exception e)
115         {
116             throw new TorqueException(e);
117         }
118     }
119 
120     protected static final String TURBINE_OBJECTDATA = "OBJECTDATA";
121 
122     /*
123      * Populates a JetspeedUser from a single row in the result set.
124      * The hashtable of attributes for each user is populated with columns
125      * from the result set. 
126      *
127      * @param row a row containing a single user's attributes.
128      * @param user the JetspeedUser object to be populated.
129      *
130      */
131     protected static void populateUserObject(Record row, int offset, JetspeedUser user)
132         throws Exception
133     {
134         // Set values are where columns are expected.  They are not
135         // required to be in these positions, as we set the positions
136         // immediately following.
137         int idPosition = 1;
138         int objectDataPosition = columnNames.length;
139         for( int i=0; i<columnNames.length; i++ )
140         {
141             if (columnNames[i].equals(USER_ID))
142                 idPosition = i+1;
143 
144             if (columnNames[i].equals(TURBINE_OBJECTDATA))
145                 objectDataPosition = i+1;
146         }
147 
148         // Restore the Permanent Storage Hashtable.  First the
149         // Hashtable is restored, then any explicit table columns
150         // which should be included in the Hashtable are added.
151         byte[] objectData = (byte[])row.getValue(objectDataPosition).asBytes();
152         Hashtable tempHash = (Hashtable)ObjectUtils.deserialize(objectData);
153         if (tempHash == null)
154         {
155             tempHash = new Hashtable(10);
156         }
157 
158         for( int j=0; j<columnNames.length; j++ )
159         {
160             if (! columnNames[j].equalsIgnoreCase( TURBINE_OBJECTDATA ) )
161             {
162                 Object obj2 = null;
163                 Value value = row.getValue(j+1);
164                 if (value.isString()) 
165                     obj2 = value.asString();
166                 else if (value.isBigDecimal()) 
167                     obj2 = value.asBigDecimal();
168                 else if (value.isBytes()) 
169                     obj2 = value.asBytes();
170                 else if (value.isDate()) 
171                     obj2 = value.asDate();
172                 else if (value.isShort()) 
173                     obj2 = new Short(value.asShort());
174                 else if (value.isInt()) 
175                     obj2 = new Integer(value.asInt());
176                 else if (value.isLong()) 
177                     obj2 = new Long(value.asLong());
178                 else if (value.isDouble()) 
179                     obj2 = new Double(value.asDouble());
180                 else if (value.isFloat()) 
181                     obj2 = new Float(value.asFloat());
182                 else if (value.isBoolean()) 
183                     obj2 = new Boolean(value.asBoolean());
184                 else if (value.isTime()) 
185                     obj2 = value.asTime();
186                 else if (value.isTimestamp()) 
187                     obj2 = value.asTimestamp();
188                 else if (value.isUtilDate()) 
189                     obj2 = value.asUtilDate();
190                 else if (value.isByte()) 
191                     obj2 = new Byte(value.asByte());
192 
193                 if ( obj2 != null )
194                 {
195                     if (columnNames[j].equalsIgnoreCase( JetspeedUser.USER_ID ))
196                     {
197                         obj2 = value.toString();
198                     }
199 
200                     tempHash.put( columnNames[j], obj2 );
201                 }
202             }
203         }
204         user.setPermStorage( tempHash );
205     }
206 
207 
208     /*
209      * Populates a JetspeedUser from a single row in the result set.
210      * The hashtable of attributes for each user is populated with columns
211      * from the result set.
212      *
213      * @param row a row containing a single user's attributes.
214      * @param user the JetspeedUser object to be populated.
215      *
216      */
217     /*
218     DST: COMMITTED 8/19 FROM AMIT AMTE - IT HAS SOME BUGS, GOING TO REMOVE UNTIL RESOLVED
219     
220     protected static void populateUserObject(Record row, int offset, JetspeedUser user)
221         throws Exception
222     {
223         // Restore the Permanent Storage Hashtable.  First the
224         // Hashtable is restored, then any explicit table columns
225         // which should be included in the Hashtable are added.
226         byte[] objectData = (byte[])row.getValue(OBJECTDATA).asBytes();
227         Hashtable tempHash = (Hashtable)ObjectUtils.deserialize(objectData);
228         if (tempHash == null)
229         {
230             tempHash = new Hashtable(10);
231         }
232 
233         for( int j=0; j<columnNames.length; j++ )
234         {
235             if (! columnNames[j].equalsIgnoreCase( OBJECTDATA ) )
236             {
237                 Object obj2 = null;
238                 Value value = null;
239                 try {
240                     value = row.getValue(columnNames[j]);
241                 } catch (Exception ex) {
242                     // Column missing from the torque record ..
243                     // Probable/intentional mis-match between the
244                     // torque schema and db table
245                     continue;
246                 }
247 
248                 if (value.isString())
249                     obj2 = value.asString();
250                 else if (value.isBigDecimal())
251                     obj2 = value.asBigDecimal();
252                 else if (value.isBytes())
253                     obj2 = value.asBytes();
254                 else if (value.isDate())
255                     obj2 = value.asDate();
256                 else if (value.isShort())
257                     obj2 = new Short(value.asShort());
258                 else if (value.isInt())
259                     obj2 = new Integer(value.asInt());
260                 else if (value.isLong())
261                     obj2 = new Long(value.asLong());
262                 else if (value.isDouble())
263                     obj2 = new Double(value.asDouble());
264                 else if (value.isFloat())
265                     obj2 = new Float(value.asFloat());
266                 else if (value.isBoolean())
267                     obj2 = new Boolean(value.asBoolean());
268                 else if (value.isTime())
269                     obj2 = value.asTime();
270                 else if (value.isTimestamp())
271                     obj2 = value.asTimestamp();
272                 else if (value.isUtilDate())
273                     obj2 = value.asUtilDate();
274                 else if (value.isByte())
275                     obj2 = new Byte(value.asByte());
276 
277                 if ( obj2 != null )
278                 {
279                     if (columnNames[j].equalsIgnoreCase( JetspeedUser.USER_ID ))
280                     {
281                         obj2 = value.toString();
282                     }
283 
284                     System.out.println(columnNames[j] + ": " + obj2.toString());
285                     tempHash.put( columnNames[j], obj2 );
286                 }
287             }
288         }
289         user.setPermStorage( tempHash );
290     }
291     */
292 
293     /***
294      * Builds a criteria object based upon an User object
295      */
296     public static Criteria buildCriteria(JetspeedUser user)
297     {
298         Hashtable permData = (Hashtable) user.getPermStorage().clone();
299         Criteria criteria = new Criteria();
300         if ( !user.isNew() )
301         {
302             criteria.add(USER_ID, new NumberKey(user.getUserId()));
303         }
304 
305         for (int i=1; i < columnNames.length; i++ )
306         {
307             if ( permData.containsKey(columnNames[i]) )
308             {
309                 String key = new StringBuffer(TABLE_NAME)
310                     .append('.').append(columnNames[i]).toString();
311 
312                 criteria.add( key,
313                               permData.remove(TurbineUserPeer.columnNames[i]) );
314             }
315         }
316         String dataKey = new StringBuffer(TABLE_NAME).append('.').append(TURBINE_OBJECTDATA).toString();
317         criteria.add( dataKey, permData );
318         return criteria;
319     }
320 
321 }