KickJava   Java API By Example, From Geeks To Geeks.

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


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  * You should not use this class directly. It should not even be
28  * extended all references should be to AttributeClass
29  */

30 public abstract class BaseAttributeClass extends BaseObject
31     implements org.apache.fulcrum.intake.Retrievable
32 {
33     /** The Peer class */
34     private static final AttributeClassPeer peer =
35         new AttributeClassPeer();
36
37         
38     /** The value for the attributeClassId field */
39     private Integer JavaDoc attributeClassId;
40       
41     /** The value for the name field */
42     private String JavaDoc name;
43       
44     /** The value for the desc field */
45     private String JavaDoc desc;
46       
47     /** The value for the javaClassName field */
48     private String JavaDoc javaClassName;
49   
50     
51     /**
52      * Get the AttributeClassId
53      *
54      * @return Integer
55      */

56     public Integer JavaDoc getAttributeClassId()
57     {
58         return attributeClassId;
59     }
60
61                                               
62     /**
63      * Set the value of AttributeClassId
64      *
65      * @param v new value
66      */

67     public void setAttributeClassId(Integer JavaDoc v) throws TorqueException
68     {
69     
70                   if (!ObjectUtils.equals(this.attributeClassId, v))
71               {
72             this.attributeClassId = v;
73             setModified(true);
74         }
75     
76           
77                                   
78         // update associated AttributeType
79
if (collAttributeTypes != null)
80         {
81             for (int i = 0; i < collAttributeTypes.size(); i++)
82             {
83                 ((AttributeType) collAttributeTypes.get(i))
84                         .setClassId(v);
85             }
86         }
87                       }
88   
89     /**
90      * Get the Name
91      *
92      * @return String
93      */

94     public String JavaDoc getName()
95     {
96         return name;
97     }
98
99                         
100     /**
101      * Set the value of Name
102      *
103      * @param v new value
104      */

105     public void setName(String JavaDoc v)
106     {
107     
108                   if (!ObjectUtils.equals(this.name, v))
109               {
110             this.name = v;
111             setModified(true);
112         }
113     
114           
115               }
116   
117     /**
118      * Get the Desc
119      *
120      * @return String
121      */

122     public String JavaDoc getDesc()
123     {
124         return desc;
125     }
126
127                         
128     /**
129      * Set the value of Desc
130      *
131      * @param v new value
132      */

133     public void setDesc(String JavaDoc v)
134     {
135     
136                   if (!ObjectUtils.equals(this.desc, v))
137               {
138             this.desc = v;
139             setModified(true);
140         }
141     
142           
143               }
144   
145     /**
146      * Get the JavaClassName
147      *
148      * @return String
149      */

150     public String JavaDoc getJavaClassName()
151     {
152         return javaClassName;
153     }
154
155                         
156     /**
157      * Set the value of JavaClassName
158      *
159      * @param v new value
160      */

161     public void setJavaClassName(String JavaDoc v)
162     {
163     
164                   if (!ObjectUtils.equals(this.javaClassName, v))
165               {
166             this.javaClassName = v;
167             setModified(true);
168         }
169     
170           
171               }
172   
173          
174                                 
175             
176     /**
177      * Collection to store aggregation of collAttributeTypes
178      */

179     protected List JavaDoc collAttributeTypes;
180
181     /**
182      * Temporary storage of collAttributeTypes to save a possible db hit in
183      * the event objects are add to the collection, but the
184      * complete collection is never requested.
185      */

186     protected void initAttributeTypes()
187     {
188         if (collAttributeTypes == null)
189         {
190             collAttributeTypes = new ArrayList JavaDoc();
191         }
192     }
193
194             
195     /**
196      * Method called to associate a AttributeType object to this object
197      * through the AttributeType foreign key attribute
198      *
199      * @param l AttributeType
200      * @throws TorqueException
201      */

202     public void addAttributeType(AttributeType l) throws TorqueException
203     {
204         getAttributeTypes().add(l);
205         l.setAttributeClass((AttributeClass)this);
206     }
207
208     /**
209      * The criteria used to select the current contents of collAttributeTypes
210      */

211     private Criteria lastAttributeTypesCriteria = null;
212
213     /**
214      * If this collection has already been initialized, returns
215      * the collection. Otherwise returns the results of
216      * getAttributeTypes(new Criteria())
217      *
218      * @throws TorqueException
219      */

220     public List JavaDoc getAttributeTypes() throws TorqueException
221     {
222         if (collAttributeTypes == null)
223         {
224             collAttributeTypes = getAttributeTypes(new Criteria(10));
225         }
226         return collAttributeTypes;
227     }
228
229     /**
230      * If this collection has already been initialized with
231      * an identical criteria, it returns the collection.
232      * Otherwise if this AttributeClass has previously
233      * been saved, it will retrieve related AttributeTypes from storage.
234      * If this AttributeClass is new, it will return
235      * an empty collection or the current collection, the criteria
236      * is ignored on a new object.
237      *
238      * @throws TorqueException
239      */

240     public List JavaDoc getAttributeTypes(Criteria criteria) throws TorqueException
241     {
242         if (collAttributeTypes == null)
243         {
244             if (isNew())
245             {
246                collAttributeTypes = new ArrayList JavaDoc();
247             }
248             else
249             {
250                       criteria.add(AttributeTypePeer.ATTRIBUTE_CLASS_ID, getAttributeClassId() );
251                       collAttributeTypes = AttributeTypePeer.doSelect(criteria);
252             }
253         }
254         else
255         {
256             // criteria has no effect for a new object
257
if (!isNew())
258             {
259                 // the following code is to determine if a new query is
260
// called for. If the criteria is the same as the last
261
// one, just return the collection.
262
criteria.add(AttributeTypePeer.ATTRIBUTE_CLASS_ID, getAttributeClassId() );
263                       if (!lastAttributeTypesCriteria.equals(criteria))
264                 {
265                     collAttributeTypes = AttributeTypePeer.doSelect(criteria);
266                 }
267             }
268         }
269         lastAttributeTypesCriteria = criteria;
270
271         return collAttributeTypes;
272     }
273
274     /**
275      * If this collection has already been initialized, returns
276      * the collection. Otherwise returns the results of
277      * getAttributeTypes(new Criteria(),Connection)
278      * This method takes in the Connection also as input so that
279      * referenced objects can also be obtained using a Connection
280      * that is taken as input
281      */

282     public List JavaDoc getAttributeTypes(Connection JavaDoc con) throws TorqueException
283     {
284         if (collAttributeTypes == null)
285         {
286             collAttributeTypes = getAttributeTypes(new Criteria(10),con);
287         }
288         return collAttributeTypes;
289     }
290
291     /**
292      * If this collection has already been initialized with
293      * an identical criteria, it returns the collection.
294      * Otherwise if this AttributeClass has previously
295      * been saved, it will retrieve related AttributeTypes from storage.
296      * If this AttributeClass is new, it will return
297      * an empty collection or the current collection, the criteria
298      * is ignored on a new object.
299      * This method takes in the Connection also as input so that
300      * referenced objects can also be obtained using a Connection
301      * that is taken as input
302      */

303     public List JavaDoc getAttributeTypes(Criteria criteria,Connection JavaDoc con) throws TorqueException
304     {
305         if (collAttributeTypes == null)
306         {
307             if (isNew())
308             {
309                collAttributeTypes = new ArrayList JavaDoc();
310             }
311             else
312             {
313                        criteria.add(AttributeTypePeer.ATTRIBUTE_CLASS_ID, getAttributeClassId() );
314                        collAttributeTypes = AttributeTypePeer.doSelect(criteria,con);
315              }
316          }
317          else
318          {
319              // criteria has no effect for a new object
320
if (!isNew())
321              {
322                  // the following code is to determine if a new query is
323
// called for. If the criteria is the same as the last
324
// one, just return the collection.
325
criteria.add(AttributeTypePeer.ATTRIBUTE_CLASS_ID, getAttributeClassId() );
326                      if (!lastAttributeTypesCriteria.equals(criteria))
327                  {
328                      collAttributeTypes = AttributeTypePeer.doSelect(criteria,con);
329                  }
330              }
331          }
332          lastAttributeTypesCriteria = criteria;
333
334          return collAttributeTypes;
335      }
336
337                   
338               
339                     
340                               
341                                 
342                                                               
343                                         
344                     
345                     
346           
347     /**
348      * If this collection has already been initialized with
349      * an identical criteria, it returns the collection.
350      * Otherwise if this AttributeClass is new, it will return
351      * an empty collection; or if this AttributeClass has previously
352      * been saved, it will retrieve related AttributeTypes from storage.
353      *
354      * This method is protected by default in order to keep the public
355      * api reasonable. You can provide public methods for those you
356      * actually need in AttributeClass.
357      */

358     protected List JavaDoc getAttributeTypesJoinAttributeClass(Criteria criteria)
359         throws TorqueException
360     {
361         if (collAttributeTypes == null)
362         {
363             if (isNew())
364             {
365                collAttributeTypes = new ArrayList JavaDoc();
366             }
367             else
368             {
369                             criteria.add(AttributeTypePeer.ATTRIBUTE_CLASS_ID, getAttributeClassId() );
370                             collAttributeTypes = AttributeTypePeer.doSelectJoinAttributeClass(criteria);
371             }
372         }
373         else
374         {
375             // the following code is to determine if a new query is
376
// called for. If the criteria is the same as the last
377
// one, just return the collection.
378

379                             criteria.add(AttributeTypePeer.ATTRIBUTE_CLASS_ID, getAttributeClassId() );
380                         if (!lastAttributeTypesCriteria.equals(criteria))
381             {
382                 collAttributeTypes = AttributeTypePeer.doSelectJoinAttributeClass(criteria);
383             }
384         }
385         lastAttributeTypesCriteria = criteria;
386
387         return collAttributeTypes;
388     }
389                             
390
391
392           
393     private static List JavaDoc fieldNames = null;
394
395     /**
396      * Generate a list of field names.
397      *
398      * @return a list of field names
399      */

400     public static synchronized List JavaDoc getFieldNames()
401     {
402         if (fieldNames == null)
403         {
404             fieldNames = new ArrayList JavaDoc();
405               fieldNames.add("AttributeClassId");
406               fieldNames.add("Name");
407               fieldNames.add("Desc");
408               fieldNames.add("JavaClassName");
409               fieldNames = Collections.unmodifiableList(fieldNames);
410         }
411         return fieldNames;
412     }
413
414     /**
415      * Retrieves a field from the object by name passed in as a String.
416      *
417      * @param name field name
418      * @return value
419      */

420     public Object JavaDoc getByName(String JavaDoc name)
421     {
422           if (name.equals("AttributeClassId"))
423         {
424                 return getAttributeClassId();
425             }
426           if (name.equals("Name"))
427         {
428                 return getName();
429             }
430           if (name.equals("Desc"))
431         {
432                 return getDesc();
433             }
434           if (name.equals("JavaClassName"))
435         {
436                 return getJavaClassName();
437             }
438           return null;
439     }
440     
441     /**
442      * Retrieves a field from the object by name passed in
443      * as a String. The String must be one of the static
444      * Strings defined in this Class' Peer.
445      *
446      * @param name peer name
447      * @return value
448      */

449     public Object JavaDoc getByPeerName(String JavaDoc name)
450     {
451           if (name.equals(AttributeClassPeer.ATTRIBUTE_CLASS_ID))
452         {
453                 return getAttributeClassId();
454             }
455           if (name.equals(AttributeClassPeer.ATTRIBUTE_CLASS_NAME))
456         {
457                 return getName();
458             }
459           if (name.equals(AttributeClassPeer.ATTRIBUTE_CLASS_DESC))
460         {
461                 return getDesc();
462             }
463           if (name.equals(AttributeClassPeer.JAVA_CLASS_NAME))
464         {
465                 return getJavaClassName();
466             }
467           return null;
468     }
469
470     /**
471      * Retrieves a field from the object by Position as specified
472      * in the xml schema. Zero-based.
473      *
474      * @param pos position in xml schema
475      * @return value
476      */

477     public Object JavaDoc getByPosition(int pos)
478     {
479             if (pos == 0)
480         {
481                 return getAttributeClassId();
482             }
483               if (pos == 1)
484         {
485                 return getName();
486             }
487               if (pos == 2)
488         {
489                 return getDesc();
490             }
491               if (pos == 3)
492         {
493                 return getJavaClassName();
494             }
495               return null;
496     }
497      
498     /**
499      * Stores the object in the database. If the object is new,
500      * it inserts it; otherwise an update is performed.
501      *
502      * @throws Exception
503      */

504     public void save() throws Exception JavaDoc
505     {
506           save(AttributeClassPeer.getMapBuilder()
507                 .getDatabaseMap().getName());
508       }
509
510     /**
511      * Stores the object in the database. If the object is new,
512      * it inserts it; otherwise an update is performed.
513        * Note: this code is here because the method body is
514      * auto-generated conditionally and therefore needs to be
515      * in this file instead of in the super class, BaseObject.
516        *
517      * @param dbName
518      * @throws TorqueException
519      */

520     public void save(String JavaDoc dbName) throws TorqueException
521     {
522         Connection JavaDoc con = null;
523           try
524         {
525             con = Transaction.begin(dbName);
526             save(con);
527             Transaction.commit(con);
528         }
529         catch(TorqueException e)
530         {
531             Transaction.safeRollback(con);
532             throw e;
533         }
534       }
535
536       /** flag to prevent endless save loop, if this object is referenced
537         by another object which falls in this transaction. */

538     private boolean alreadyInSave = false;
539       /**
540      * Stores the object in the database. If the object is new,
541      * it inserts it; otherwise an update is performed. This method
542      * is meant to be used as part of a transaction, otherwise use
543      * the save() method and the connection details will be handled
544      * internally
545      *
546      * @param con
547      * @throws TorqueException
548      */

549     public void save(Connection JavaDoc con) throws TorqueException
550     {
551           if (!alreadyInSave)
552         {
553             alreadyInSave = true;
554
555
556   
557             // If this object has been modified, then save it to the database.
558
if (isModified())
559             {
560                 if (isNew())
561                 {
562                     AttributeClassPeer.doInsert((AttributeClass)this, con);
563                     setNew(false);
564                 }
565                 else
566                 {
567                     AttributeClassPeer.doUpdate((AttributeClass)this, con);
568                 }
569
570                       if (isCacheOnSave())
571                 {
572                     AttributeClassManager.putInstance(this);
573                 }
574               }
575
576                                       
577                             if (collAttributeTypes != null)
578             {
579                 for (int i = 0; i < collAttributeTypes.size(); i++)
580                 {
581                     ((AttributeType)collAttributeTypes.get(i)).save(con);
582                 }
583             }
584                           alreadyInSave = false;
585         }
586       }
587
588     /**
589      * Specify whether to cache the object after saving to the db.
590      * This method returns false
591      */

592     protected boolean isCacheOnSave()
593     {
594         return true;
595     }
596
597                         
598       /**
599      * Set the PrimaryKey using ObjectKey.
600      *
601      * @param attributeClassId ObjectKey
602      */

603     public void setPrimaryKey(ObjectKey attributeClassId)
604         throws TorqueException {
605             setAttributeClassId(new Integer JavaDoc(((NumberKey)attributeClassId).intValue()));
606         }
607
608     /**
609      * Set the PrimaryKey using a String.
610      *
611      * @param key
612      */

613     public void setPrimaryKey(String JavaDoc key) throws TorqueException
614     {
615             setAttributeClassId(new Integer JavaDoc(key));
616         }
617
618   
619     /**
620      * returns an id that differentiates this object from others
621      * of its class.
622      */

623     public ObjectKey getPrimaryKey()
624     {
625           return SimpleKey.keyFor(getAttributeClassId());
626       }
627  
628     /**
629      * get an id that differentiates this object from others
630      * of its class.
631      */

632     public String JavaDoc getQueryKey()
633     {
634         if (getPrimaryKey() == null)
635         {
636             return "";
637         }
638         else
639         {
640             return getPrimaryKey().toString();
641         }
642     }
643
644     /**
645      * set an id that differentiates this object from others
646      * of its class.
647      */

648     public void setQueryKey(String JavaDoc key)
649         throws TorqueException
650     {
651         setPrimaryKey(key);
652     }
653
654     /**
655      * Makes a copy of this object.
656      * It creates a new object filling in the simple attributes.
657        * It then fills all the association collections and sets the
658      * related objects to isNew=true.
659        */

660       public AttributeClass copy() throws TorqueException
661     {
662         AttributeClass copyObj = new AttributeClass();
663             copyObj.setAttributeClassId(attributeClassId);
664           copyObj.setName(name);
665           copyObj.setDesc(desc);
666           copyObj.setJavaClassName(javaClassName);
667   
668                       copyObj.setAttributeClassId((Integer JavaDoc)null);
669                               
670                                       
671                 
672         List JavaDoc v = getAttributeTypes();
673         for (int i = 0; i < v.size(); i++)
674         {
675             AttributeType obj = (AttributeType) v.get(i);
676             copyObj.addAttributeType(obj.copy());
677         }
678                             return copyObj;
679     }
680
681     /**
682      * returns a peer instance associated with this om. Since Peer classes
683      * are not to have any instance attributes, this method returns the
684      * same instance for all member of this class. The method could therefore
685      * be static, but this would prevent one from overriding the behavior.
686      */

687     public AttributeClassPeer getPeer()
688     {
689         return peer;
690     }
691
692     public String JavaDoc toString()
693     {
694         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
695         str.append("AttributeClass:\n");
696         str.append("AttributeClassId = ")
697                .append(getAttributeClassId())
698              .append("\n");
699         str.append("Name = ")
700                .append(getName())
701              .append("\n");
702         str.append("Desc = ")
703                .append(getDesc())
704              .append("\n");
705         str.append("JavaClassName = ")
706                .append(getJavaClassName())
707              .append("\n");
708         return(str.toString());
709     }
710 }
711
Popular Tags