KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > om > BaseUserVote


1 package org.tigris.scarab.om;
2
3
4 import java.math.BigDecimal JavaDoc;
5 import java.sql.Connection JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.List JavaDoc;
10
11 import org.apache.commons.lang.ObjectUtils;
12 import org.apache.fulcrum.intake.Retrievable;
13 import org.apache.torque.TorqueException;
14 import org.apache.torque.om.BaseObject;
15 import org.apache.torque.om.ComboKey;
16 import org.apache.torque.om.DateKey;
17 import org.apache.torque.om.NumberKey;
18 import org.apache.torque.om.ObjectKey;
19 import org.apache.torque.om.SimpleKey;
20 import org.apache.torque.om.StringKey;
21 import org.apache.torque.om.Persistent;
22 import org.apache.torque.util.Criteria;
23 import org.apache.torque.util.Transaction;
24
25     
26       
27   
28 /**
29  * You should not use this class directly. It should not even be
30  * extended all references should be to UserVote
31  */

32 public abstract class BaseUserVote extends BaseObject
33     implements org.apache.fulcrum.intake.Retrievable
34 {
35     /** The Peer class */
36     private static final UserVotePeer peer =
37         new UserVotePeer();
38
39         
40     /** The value for the moduleId field */
41     private Integer JavaDoc moduleId;
42       
43     /** The value for the userId field */
44     private Integer JavaDoc userId;
45                                           
46     /** The value for the totalVotes field */
47     private int totalVotes = 0;
48                                           
49     /** The value for the usedVotes field */
50     private int usedVotes = 0;
51   
52     
53     /**
54      * Get the ModuleId
55      *
56      * @return Integer
57      */

58     public Integer JavaDoc getModuleId()
59     {
60         return moduleId;
61     }
62
63                               
64     /**
65      * Set the value of ModuleId
66      *
67      * @param v new value
68      */

69     public void setModuleId(Integer JavaDoc v) throws TorqueException
70     {
71     
72                   if (!ObjectUtils.equals(this.moduleId, v))
73               {
74             this.moduleId = v;
75             setModified(true);
76         }
77     
78                                           
79                 if (aModule != null && !ObjectUtils.equals(aModule.getModuleId(), v))
80                 {
81             aModule = null;
82         }
83       
84               }
85   
86     /**
87      * Get the UserId
88      *
89      * @return Integer
90      */

91     public Integer JavaDoc getUserId()
92     {
93         return userId;
94     }
95
96                               
97     /**
98      * Set the value of UserId
99      *
100      * @param v new value
101      */

102     public void setUserId(Integer JavaDoc v) throws TorqueException
103     {
104     
105                   if (!ObjectUtils.equals(this.userId, v))
106               {
107             this.userId = v;
108             setModified(true);
109         }
110     
111                                           
112                 if (aScarabUser != null && !ObjectUtils.equals(aScarabUser.getUserId(), v))
113                 {
114             aScarabUser = null;
115         }
116       
117               }
118   
119     /**
120      * Get the TotalVotes
121      *
122      * @return int
123      */

124     public int getTotalVotes()
125     {
126         return totalVotes;
127     }
128
129                         
130     /**
131      * Set the value of TotalVotes
132      *
133      * @param v new value
134      */

135     public void setTotalVotes(int v)
136     {
137     
138                   if (this.totalVotes != v)
139               {
140             this.totalVotes = v;
141             setModified(true);
142         }
143     
144           
145               }
146   
147     /**
148      * Get the UsedVotes
149      *
150      * @return int
151      */

152     public int getUsedVotes()
153     {
154         return usedVotes;
155     }
156
157                         
158     /**
159      * Set the value of UsedVotes
160      *
161      * @param v new value
162      */

163     public void setUsedVotes(int v)
164     {
165     
166                   if (this.usedVotes != v)
167               {
168             this.usedVotes = v;
169             setModified(true);
170         }
171     
172           
173               }
174   
175       
176             
177                   
178         private Module aModule;
179
180     /**
181      * Declares an association between this object and a Module object
182      *
183      * @param v Module
184      * @throws TorqueException
185      */

186     public void setModule(Module v) throws TorqueException
187     {
188             if (v == null)
189         {
190                   setModuleId((Integer JavaDoc) null);
191               }
192         else
193         {
194             setModuleId(v.getModuleId());
195         }
196             aModule = v;
197     }
198
199                         
200     /**
201      * Get the associated Module object
202      *
203      * @return the associated Module object
204      * @throws TorqueException
205      */

206     public Module getModule() throws TorqueException
207     {
208         if ( !ObjectUtils.equals(getModuleId(), null) )
209         {
210                 return ModuleManager.getInstance(SimpleKey.keyFor(getModuleId()));
211             }
212         return aModule;
213     }
214
215     /**
216      * Provides convenient way to set a relationship based on a
217      * ObjectKey, for example
218      * <code>bar.setFooKey(foo.getPrimaryKey())</code>
219      *
220          */

221     public void setModuleKey(ObjectKey key) throws TorqueException
222     {
223     
224                     setModuleId(new Integer JavaDoc(((NumberKey) key).intValue()));
225               }
226   
227             
228                   
229         private ScarabUser aScarabUser;
230
231     /**
232      * Declares an association between this object and a ScarabUser object
233      *
234      * @param v ScarabUser
235      * @throws TorqueException
236      */

237     public void setScarabUser(ScarabUser v) throws TorqueException
238     {
239             if (v == null)
240         {
241                   setUserId((Integer JavaDoc) null);
242               }
243         else
244         {
245             setUserId(v.getUserId());
246         }
247             aScarabUser = v;
248     }
249
250                         
251     /**
252      * Get the associated ScarabUser object
253      *
254      * @return the associated ScarabUser object
255      * @throws TorqueException
256      */

257     public ScarabUser getScarabUser() throws TorqueException
258     {
259         if ( !ObjectUtils.equals(getUserId(), null) )
260         {
261                 return ScarabUserManager.getInstance(SimpleKey.keyFor(getUserId()));
262             }
263         return aScarabUser;
264     }
265
266     /**
267      * Provides convenient way to set a relationship based on a
268      * ObjectKey, for example
269      * <code>bar.setFooKey(foo.getPrimaryKey())</code>
270      *
271          */

272     public void setScarabUserKey(ObjectKey key) throws TorqueException
273     {
274     
275                     setUserId(new Integer JavaDoc(((NumberKey) key).intValue()));
276               }
277      
278                 
279     private static List JavaDoc fieldNames = null;
280
281     /**
282      * Generate a list of field names.
283      *
284      * @return a list of field names
285      */

286     public static synchronized List JavaDoc getFieldNames()
287     {
288         if (fieldNames == null)
289         {
290             fieldNames = new ArrayList JavaDoc();
291               fieldNames.add("ModuleId");
292               fieldNames.add("UserId");
293               fieldNames.add("TotalVotes");
294               fieldNames.add("UsedVotes");
295               fieldNames = Collections.unmodifiableList(fieldNames);
296         }
297         return fieldNames;
298     }
299
300     /**
301      * Retrieves a field from the object by name passed in as a String.
302      *
303      * @param name field name
304      * @return value
305      */

306     public Object JavaDoc getByName(String JavaDoc name)
307     {
308           if (name.equals("ModuleId"))
309         {
310                 return getModuleId();
311             }
312           if (name.equals("UserId"))
313         {
314                 return getUserId();
315             }
316           if (name.equals("TotalVotes"))
317         {
318                 return new Integer JavaDoc(getTotalVotes());
319             }
320           if (name.equals("UsedVotes"))
321         {
322                 return new Integer JavaDoc(getUsedVotes());
323             }
324           return null;
325     }
326     
327     /**
328      * Retrieves a field from the object by name passed in
329      * as a String. The String must be one of the static
330      * Strings defined in this Class' Peer.
331      *
332      * @param name peer name
333      * @return value
334      */

335     public Object JavaDoc getByPeerName(String JavaDoc name)
336     {
337           if (name.equals(UserVotePeer.MODULE_ID))
338         {
339                 return getModuleId();
340             }
341           if (name.equals(UserVotePeer.USER_ID))
342         {
343                 return getUserId();
344             }
345           if (name.equals(UserVotePeer.TOTAL_VOTES))
346         {
347                 return new Integer JavaDoc(getTotalVotes());
348             }
349           if (name.equals(UserVotePeer.USED_VOTES))
350         {
351                 return new Integer JavaDoc(getUsedVotes());
352             }
353           return null;
354     }
355
356     /**
357      * Retrieves a field from the object by Position as specified
358      * in the xml schema. Zero-based.
359      *
360      * @param pos position in xml schema
361      * @return value
362      */

363     public Object JavaDoc getByPosition(int pos)
364     {
365             if (pos == 0)
366         {
367                 return getModuleId();
368             }
369               if (pos == 1)
370         {
371                 return getUserId();
372             }
373               if (pos == 2)
374         {
375                 return new Integer JavaDoc(getTotalVotes());
376             }
377               if (pos == 3)
378         {
379                 return new Integer JavaDoc(getUsedVotes());
380             }
381               return null;
382     }
383      
384     /**
385      * Stores the object in the database. If the object is new,
386      * it inserts it; otherwise an update is performed.
387      *
388      * @throws Exception
389      */

390     public void save() throws Exception JavaDoc
391     {
392           save(UserVotePeer.getMapBuilder()
393                 .getDatabaseMap().getName());
394       }
395
396     /**
397      * Stores the object in the database. If the object is new,
398      * it inserts it; otherwise an update is performed.
399        * Note: this code is here because the method body is
400      * auto-generated conditionally and therefore needs to be
401      * in this file instead of in the super class, BaseObject.
402        *
403      * @param dbName
404      * @throws TorqueException
405      */

406     public void save(String JavaDoc dbName) throws TorqueException
407     {
408         Connection JavaDoc con = null;
409           try
410         {
411             con = Transaction.begin(dbName);
412             save(con);
413             Transaction.commit(con);
414         }
415         catch(TorqueException e)
416         {
417             Transaction.safeRollback(con);
418             throw e;
419         }
420       }
421
422       /** flag to prevent endless save loop, if this object is referenced
423         by another object which falls in this transaction. */

424     private boolean alreadyInSave = false;
425       /**
426      * Stores the object in the database. If the object is new,
427      * it inserts it; otherwise an update is performed. This method
428      * is meant to be used as part of a transaction, otherwise use
429      * the save() method and the connection details will be handled
430      * internally
431      *
432      * @param con
433      * @throws TorqueException
434      */

435     public void save(Connection JavaDoc con) throws TorqueException
436     {
437           if (!alreadyInSave)
438         {
439             alreadyInSave = true;
440
441
442   
443             // If this object has been modified, then save it to the database.
444
if (isModified())
445             {
446                 if (isNew())
447                 {
448                     UserVotePeer.doInsert((UserVote)this, con);
449                     setNew(false);
450                 }
451                 else
452                 {
453                     UserVotePeer.doUpdate((UserVote)this, con);
454                 }
455
456                       if (isCacheOnSave())
457                 {
458                     UserVoteManager.putInstance(this);
459                 }
460               }
461
462                       alreadyInSave = false;
463         }
464       }
465
466     /**
467      * Specify whether to cache the object after saving to the db.
468      * This method returns false
469      */

470     protected boolean isCacheOnSave()
471     {
472         return true;
473     }
474
475                                               
476   
477     private final SimpleKey[] pks = new SimpleKey[2];
478     private final ComboKey comboPK = new ComboKey(pks);
479
480     /**
481      * Set the PrimaryKey with an ObjectKey
482      *
483      * @param key
484      */

485     public void setPrimaryKey(ObjectKey key) throws TorqueException
486     {
487         SimpleKey[] keys = (SimpleKey[]) key.getValue();
488         SimpleKey tmpKey = null;
489                       setModuleId(new Integer JavaDoc(((NumberKey)keys[0]).intValue()));
490                         setUserId(new Integer JavaDoc(((NumberKey)keys[1]).intValue()));
491               }
492
493     /**
494      * Set the PrimaryKey using SimpleKeys.
495      *
496          * @param moduleId Integer
497          * @param userId Integer
498          */

499     public void setPrimaryKey( Integer JavaDoc moduleId, Integer JavaDoc userId)
500         throws TorqueException
501     {
502             setModuleId(moduleId);
503             setUserId(userId);
504         }
505
506     /**
507      * Set the PrimaryKey using a String.
508      */

509     public void setPrimaryKey(String JavaDoc key) throws TorqueException
510     {
511         setPrimaryKey(new ComboKey(key));
512     }
513   
514     /**
515      * returns an id that differentiates this object from others
516      * of its class.
517      */

518     public ObjectKey getPrimaryKey()
519     {
520               pks[0] = SimpleKey.keyFor(getModuleId());
521                   pks[1] = SimpleKey.keyFor(getUserId());
522                   return comboPK;
523       }
524  
525     /**
526      * get an id that differentiates this object from others
527      * of its class.
528      */

529     public String JavaDoc getQueryKey()
530     {
531         if (getPrimaryKey() == null)
532         {
533             return "";
534         }
535         else
536         {
537             return getPrimaryKey().toString();
538         }
539     }
540
541     /**
542      * set an id that differentiates this object from others
543      * of its class.
544      */

545     public void setQueryKey(String JavaDoc key)
546         throws TorqueException
547     {
548         setPrimaryKey(key);
549     }
550
551     /**
552      * Makes a copy of this object.
553      * It creates a new object filling in the simple attributes.
554        * It then fills all the association collections and sets the
555      * related objects to isNew=true.
556        */

557       public UserVote copy() throws TorqueException
558     {
559         UserVote copyObj = new UserVote();
560             copyObj.setModuleId(moduleId);
561           copyObj.setUserId(userId);
562           copyObj.setTotalVotes(totalVotes);
563           copyObj.setUsedVotes(usedVotes);
564   
565                       copyObj.setModuleId((Integer JavaDoc)null);
566                                 copyObj.setUserId((Integer JavaDoc)null);
567                         
568                 return copyObj;
569     }
570
571     /**
572      * returns a peer instance associated with this om. Since Peer classes
573      * are not to have any instance attributes, this method returns the
574      * same instance for all member of this class. The method could therefore
575      * be static, but this would prevent one from overriding the behavior.
576      */

577     public UserVotePeer getPeer()
578     {
579         return peer;
580     }
581
582     public String JavaDoc toString()
583     {
584         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
585         str.append("UserVote:\n");
586         str.append("ModuleId = ")
587                .append(getModuleId())
588              .append("\n");
589         str.append("UserId = ")
590                .append(getUserId())
591              .append("\n");
592         str.append("TotalVotes = ")
593                .append(getTotalVotes())
594              .append("\n");
595         str.append("UsedVotes = ")
596                .append(getUsedVotes())
597              .append("\n");
598         return(str.toString());
599     }
600 }
601
Popular Tags