KickJava   Java API By Example, From Geeks To Geeks.

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


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

31 public abstract class BaseAttributeType extends BaseObject
32     implements org.apache.fulcrum.intake.Retrievable
33 {
34     /** The Peer class */
35     private static final AttributeTypePeer peer =
36         new AttributeTypePeer();
37
38         
39     /** The value for the attributeTypeId field */
40     private Integer JavaDoc attributeTypeId;
41       
42     /** The value for the classId field */
43     private Integer JavaDoc classId;
44       
45     /** The value for the name field */
46     private String JavaDoc name;
47       
48     /** The value for the javaClassName field */
49     private String JavaDoc javaClassName;
50       
51     /** The value for the validationKey field */
52     private String JavaDoc validationKey;
53   
54     
55     /**
56      * Get the AttributeTypeId
57      *
58      * @return Integer
59      */

60     public Integer JavaDoc getAttributeTypeId()
61     {
62         return attributeTypeId;
63     }
64
65                                               
66     /**
67      * Set the value of AttributeTypeId
68      *
69      * @param v new value
70      */

71     public void setAttributeTypeId(Integer JavaDoc v) throws TorqueException
72     {
73     
74                   if (!ObjectUtils.equals(this.attributeTypeId, v))
75               {
76             this.attributeTypeId = v;
77             setModified(true);
78         }
79     
80           
81                                   
82         // update associated Attribute
83
if (collAttributes != null)
84         {
85             for (int i = 0; i < collAttributes.size(); i++)
86             {
87                 ((Attribute) collAttributes.get(i))
88                         .setTypeId(v);
89             }
90         }
91                       }
92   
93     /**
94      * Get the ClassId
95      *
96      * @return Integer
97      */

98     public Integer JavaDoc getClassId()
99     {
100         return classId;
101     }
102
103                               
104     /**
105      * Set the value of ClassId
106      *
107      * @param v new value
108      */

109     public void setClassId(Integer JavaDoc v) throws TorqueException
110     {
111     
112                   if (!ObjectUtils.equals(this.classId, v))
113               {
114             this.classId = v;
115             setModified(true);
116         }
117     
118                                   
119                 if (aAttributeClass != null && !ObjectUtils.equals(aAttributeClass.getAttributeClassId(), v))
120                 {
121             aAttributeClass = null;
122         }
123       
124               }
125   
126     /**
127      * Get the Name
128      *
129      * @return String
130      */

131     public String JavaDoc getName()
132     {
133         return name;
134     }
135
136                         
137     /**
138      * Set the value of Name
139      *
140      * @param v new value
141      */

142     public void setName(String JavaDoc v)
143     {
144     
145                   if (!ObjectUtils.equals(this.name, v))
146               {
147             this.name = v;
148             setModified(true);
149         }
150     
151           
152               }
153   
154     /**
155      * Get the JavaClassName
156      *
157      * @return String
158      */

159     public String JavaDoc getJavaClassName()
160     {
161         return javaClassName;
162     }
163
164                         
165     /**
166      * Set the value of JavaClassName
167      *
168      * @param v new value
169      */

170     public void setJavaClassName(String JavaDoc v)
171     {
172     
173                   if (!ObjectUtils.equals(this.javaClassName, v))
174               {
175             this.javaClassName = v;
176             setModified(true);
177         }
178     
179           
180               }
181   
182     /**
183      * Get the ValidationKey
184      *
185      * @return String
186      */

187     public String JavaDoc getValidationKey()
188     {
189         return validationKey;
190     }
191
192                         
193     /**
194      * Set the value of ValidationKey
195      *
196      * @param v new value
197      */

198     public void setValidationKey(String JavaDoc v)
199     {
200     
201                   if (!ObjectUtils.equals(this.validationKey, v))
202               {
203             this.validationKey = v;
204             setModified(true);
205         }
206     
207           
208               }
209   
210       
211         
212                   
213         private AttributeClass aAttributeClass;
214
215     /**
216      * Declares an association between this object and a AttributeClass object
217      *
218      * @param v AttributeClass
219      * @throws TorqueException
220      */

221     public void setAttributeClass(AttributeClass v) throws TorqueException
222     {
223             if (v == null)
224         {
225                   setClassId((Integer JavaDoc) null);
226               }
227         else
228         {
229             setClassId(v.getAttributeClassId());
230         }
231             aAttributeClass = v;
232     }
233
234                         
235     /**
236      * Get the associated AttributeClass object
237      *
238      * @return the associated AttributeClass object
239      * @throws TorqueException
240      */

241     public AttributeClass getAttributeClass() throws TorqueException
242     {
243         if ( !ObjectUtils.equals(getClassId(), null) )
244         {
245                 return AttributeClassManager.getInstance(SimpleKey.keyFor(getClassId()));
246             }
247         return aAttributeClass;
248     }
249
250     /**
251      * Provides convenient way to set a relationship based on a
252      * ObjectKey, for example
253      * <code>bar.setFooKey(foo.getPrimaryKey())</code>
254      *
255          */

256     public void setAttributeClassKey(ObjectKey key) throws TorqueException
257     {
258     
259                     setClassId(new Integer JavaDoc(((NumberKey) key).intValue()));
260               }
261      
262                                 
263             
264     /**
265      * Collection to store aggregation of collAttributes
266      */

267     protected List JavaDoc collAttributes;
268
269     /**
270      * Temporary storage of collAttributes to save a possible db hit in
271      * the event objects are add to the collection, but the
272      * complete collection is never requested.
273      */

274     protected void initAttributes()
275     {
276         if (collAttributes == null)
277         {
278             collAttributes = new ArrayList JavaDoc();
279         }
280     }
281
282             
283     /**
284      * Method called to associate a Attribute object to this object
285      * through the Attribute foreign key attribute
286      *
287      * @param l Attribute
288      * @throws TorqueException
289      */

290     public void addAttribute(Attribute l) throws TorqueException
291     {
292         getAttributes().add(l);
293         l.setAttributeType((AttributeType)this);
294     }
295
296     /**
297      * The criteria used to select the current contents of collAttributes
298      */

299     private Criteria lastAttributesCriteria = null;
300
301     /**
302      * If this collection has already been initialized, returns
303      * the collection. Otherwise returns the results of
304      * getAttributes(new Criteria())
305      *
306      * @throws TorqueException
307      */

308     public List JavaDoc getAttributes() throws TorqueException
309     {
310         if (collAttributes == null)
311         {
312             collAttributes = getAttributes(new Criteria(10));
313         }
314         return collAttributes;
315     }
316
317     /**
318      * If this collection has already been initialized with
319      * an identical criteria, it returns the collection.
320      * Otherwise if this AttributeType has previously
321      * been saved, it will retrieve related Attributes from storage.
322      * If this AttributeType is new, it will return
323      * an empty collection or the current collection, the criteria
324      * is ignored on a new object.
325      *
326      * @throws TorqueException
327      */

328     public List JavaDoc getAttributes(Criteria criteria) throws TorqueException
329     {
330         if (collAttributes == null)
331         {
332             if (isNew())
333             {
334                collAttributes = new ArrayList JavaDoc();
335             }
336             else
337             {
338                       criteria.add(AttributePeer.ATTRIBUTE_TYPE_ID, getAttributeTypeId() );
339                       collAttributes = AttributePeer.doSelect(criteria);
340             }
341         }
342         else
343         {
344             // criteria has no effect for a new object
345
if (!isNew())
346             {
347                 // the following code is to determine if a new query is
348
// called for. If the criteria is the same as the last
349
// one, just return the collection.
350
criteria.add(AttributePeer.ATTRIBUTE_TYPE_ID, getAttributeTypeId() );
351                       if (!lastAttributesCriteria.equals(criteria))
352                 {
353                     collAttributes = AttributePeer.doSelect(criteria);
354                 }
355             }
356         }
357         lastAttributesCriteria = criteria;
358
359         return collAttributes;
360     }
361
362     /**
363      * If this collection has already been initialized, returns
364      * the collection. Otherwise returns the results of
365      * getAttributes(new Criteria(),Connection)
366      * This method takes in the Connection also as input so that
367      * referenced objects can also be obtained using a Connection
368      * that is taken as input
369      */

370     public List JavaDoc getAttributes(Connection JavaDoc con) throws TorqueException
371     {
372         if (collAttributes == null)
373         {
374             collAttributes = getAttributes(new Criteria(10),con);
375         }
376         return collAttributes;
377     }
378
379     /**
380      * If this collection has already been initialized with
381      * an identical criteria, it returns the collection.
382      * Otherwise if this AttributeType has previously
383      * been saved, it will retrieve related Attributes from storage.
384      * If this AttributeType is new, it will return
385      * an empty collection or the current collection, the criteria
386      * is ignored on a new object.
387      * This method takes in the Connection also as input so that
388      * referenced objects can also be obtained using a Connection
389      * that is taken as input
390      */

391     public List JavaDoc getAttributes(Criteria criteria,Connection JavaDoc con) throws TorqueException
392     {
393         if (collAttributes == null)
394         {
395             if (isNew())
396             {
397                collAttributes = new ArrayList JavaDoc();
398             }
399             else
400             {
401                        criteria.add(AttributePeer.ATTRIBUTE_TYPE_ID, getAttributeTypeId() );
402                        collAttributes = AttributePeer.doSelect(criteria,con);
403              }
404          }
405          else
406          {
407              // criteria has no effect for a new object
408
if (!isNew())
409              {
410                  // the following code is to determine if a new query is
411
// called for. If the criteria is the same as the last
412
// one, just return the collection.
413
criteria.add(AttributePeer.ATTRIBUTE_TYPE_ID, getAttributeTypeId() );
414                      if (!lastAttributesCriteria.equals(criteria))
415                  {
416                      collAttributes = AttributePeer.doSelect(criteria,con);
417                  }
418              }
419          }
420          lastAttributesCriteria = criteria;
421
422          return collAttributes;
423      }
424
425                               
426               
427                     
428                               
429                                 
430                                                               
431                                         
432                     
433                     
434           
435     /**
436      * If this collection has already been initialized with
437      * an identical criteria, it returns the collection.
438      * Otherwise if this AttributeType is new, it will return
439      * an empty collection; or if this AttributeType has previously
440      * been saved, it will retrieve related Attributes from storage.
441      *
442      * This method is protected by default in order to keep the public
443      * api reasonable. You can provide public methods for those you
444      * actually need in AttributeType.
445      */

446     protected List JavaDoc getAttributesJoinAttributeType(Criteria criteria)
447         throws TorqueException
448     {
449         if (collAttributes == null)
450         {
451             if (isNew())
452             {
453                collAttributes = new ArrayList JavaDoc();
454             }
455             else
456             {
457                             criteria.add(AttributePeer.ATTRIBUTE_TYPE_ID, getAttributeTypeId() );
458                             collAttributes = AttributePeer.doSelectJoinAttributeType(criteria);
459             }
460         }
461         else
462         {
463             // the following code is to determine if a new query is
464
// called for. If the criteria is the same as the last
465
// one, just return the collection.
466

467                             criteria.add(AttributePeer.ATTRIBUTE_TYPE_ID, getAttributeTypeId() );
468                         if (!lastAttributesCriteria.equals(criteria))
469             {
470                 collAttributes = AttributePeer.doSelectJoinAttributeType(criteria);
471             }
472         }
473         lastAttributesCriteria = criteria;
474
475         return collAttributes;
476     }
477                   
478                     
479                     
480                                 
481                                                               
482                                         
483                     
484                     
485           
486     /**
487      * If this collection has already been initialized with
488      * an identical criteria, it returns the collection.
489      * Otherwise if this AttributeType is new, it will return
490      * an empty collection; or if this AttributeType has previously
491      * been saved, it will retrieve related Attributes from storage.
492      *
493      * This method is protected by default in order to keep the public
494      * api reasonable. You can provide public methods for those you
495      * actually need in AttributeType.
496      */

497     protected List JavaDoc getAttributesJoinAttributeOption(Criteria criteria)
498         throws TorqueException
499     {
500         if (collAttributes == null)
501         {
502             if (isNew())
503             {
504                collAttributes = new ArrayList JavaDoc();
505             }
506             else
507             {
508                             criteria.add(AttributePeer.ATTRIBUTE_TYPE_ID, getAttributeTypeId() );
509                             collAttributes = AttributePeer.doSelectJoinAttributeOption(criteria);
510             }
511         }
512         else
513         {
514             // the following code is to determine if a new query is
515
// called for. If the criteria is the same as the last
516
// one, just return the collection.
517

518                             criteria.add(AttributePeer.ATTRIBUTE_TYPE_ID, getAttributeTypeId() );
519                         if (!lastAttributesCriteria.equals(criteria))
520             {
521                 collAttributes = AttributePeer.doSelectJoinAttributeOption(criteria);
522             }
523         }
524         lastAttributesCriteria = criteria;
525
526         return collAttributes;
527     }
528                   
529                     
530                     
531                                 
532                                                               
533                                         
534                     
535                     
536           
537     /**
538      * If this collection has already been initialized with
539      * an identical criteria, it returns the collection.
540      * Otherwise if this AttributeType is new, it will return
541      * an empty collection; or if this AttributeType has previously
542      * been saved, it will retrieve related Attributes from storage.
543      *
544      * This method is protected by default in order to keep the public
545      * api reasonable. You can provide public methods for those you
546      * actually need in AttributeType.
547      */

548     protected List JavaDoc getAttributesJoinScarabUserImpl(Criteria criteria)
549         throws TorqueException
550     {
551         if (collAttributes == null)
552         {
553             if (isNew())
554             {
555                collAttributes = new ArrayList JavaDoc();
556             }
557             else
558             {
559                             criteria.add(AttributePeer.ATTRIBUTE_TYPE_ID, getAttributeTypeId() );
560                             collAttributes = AttributePeer.doSelectJoinScarabUserImpl(criteria);
561             }
562         }
563         else
564         {
565             // the following code is to determine if a new query is
566
// called for. If the criteria is the same as the last
567
// one, just return the collection.
568

569                             criteria.add(AttributePeer.ATTRIBUTE_TYPE_ID, getAttributeTypeId() );
570                         if (!lastAttributesCriteria.equals(criteria))
571             {
572                 collAttributes = AttributePeer.doSelectJoinScarabUserImpl(criteria);
573             }
574         }
575         lastAttributesCriteria = criteria;
576
577         return collAttributes;
578     }
579                             
580
581
582           
583     private static List JavaDoc fieldNames = null;
584
585     /**
586      * Generate a list of field names.
587      *
588      * @return a list of field names
589      */

590     public static synchronized List JavaDoc getFieldNames()
591     {
592         if (fieldNames == null)
593         {
594             fieldNames = new ArrayList JavaDoc();
595               fieldNames.add("AttributeTypeId");
596               fieldNames.add("ClassId");
597               fieldNames.add("Name");
598               fieldNames.add("JavaClassName");
599               fieldNames.add("ValidationKey");
600               fieldNames = Collections.unmodifiableList(fieldNames);
601         }
602         return fieldNames;
603     }
604
605     /**
606      * Retrieves a field from the object by name passed in as a String.
607      *
608      * @param name field name
609      * @return value
610      */

611     public Object JavaDoc getByName(String JavaDoc name)
612     {
613           if (name.equals("AttributeTypeId"))
614         {
615                 return getAttributeTypeId();
616             }
617           if (name.equals("ClassId"))
618         {
619                 return getClassId();
620             }
621           if (name.equals("Name"))
622         {
623                 return getName();
624             }
625           if (name.equals("JavaClassName"))
626         {
627                 return getJavaClassName();
628             }
629           if (name.equals("ValidationKey"))
630         {
631                 return getValidationKey();
632             }
633           return null;
634     }
635     
636     /**
637      * Retrieves a field from the object by name passed in
638      * as a String. The String must be one of the static
639      * Strings defined in this Class' Peer.
640      *
641      * @param name peer name
642      * @return value
643      */

644     public Object JavaDoc getByPeerName(String JavaDoc name)
645     {
646           if (name.equals(AttributeTypePeer.ATTRIBUTE_TYPE_ID))
647         {
648                 return getAttributeTypeId();
649             }
650           if (name.equals(AttributeTypePeer.ATTRIBUTE_CLASS_ID))
651         {
652                 return getClassId();
653             }
654           if (name.equals(AttributeTypePeer.ATTRIBUTE_TYPE_NAME))
655         {
656                 return getName();
657             }
658           if (name.equals(AttributeTypePeer.JAVA_CLASS_NAME))
659         {
660                 return getJavaClassName();
661             }
662           if (name.equals(AttributeTypePeer.VALIDATION_KEY))
663         {
664                 return getValidationKey();
665             }
666           return null;
667     }
668
669     /**
670      * Retrieves a field from the object by Position as specified
671      * in the xml schema. Zero-based.
672      *
673      * @param pos position in xml schema
674      * @return value
675      */

676     public Object JavaDoc getByPosition(int pos)
677     {
678             if (pos == 0)
679         {
680                 return getAttributeTypeId();
681             }
682               if (pos == 1)
683         {
684                 return getClassId();
685             }
686               if (pos == 2)
687         {
688                 return getName();
689             }
690               if (pos == 3)
691         {
692                 return getJavaClassName();
693             }
694               if (pos == 4)
695         {
696                 return getValidationKey();
697             }
698               return null;
699     }
700      
701     /**
702      * Stores the object in the database. If the object is new,
703      * it inserts it; otherwise an update is performed.
704      *
705      * @throws Exception
706      */

707     public void save() throws Exception JavaDoc
708     {
709           save(AttributeTypePeer.getMapBuilder()
710                 .getDatabaseMap().getName());
711       }
712
713     /**
714      * Stores the object in the database. If the object is new,
715      * it inserts it; otherwise an update is performed.
716        * Note: this code is here because the method body is
717      * auto-generated conditionally and therefore needs to be
718      * in this file instead of in the super class, BaseObject.
719        *
720      * @param dbName
721      * @throws TorqueException
722      */

723     public void save(String JavaDoc dbName) throws TorqueException
724     {
725         Connection JavaDoc con = null;
726           try
727         {
728             con = Transaction.begin(dbName);
729             save(con);
730             Transaction.commit(con);
731         }
732         catch(TorqueException e)
733         {
734             Transaction.safeRollback(con);
735             throw e;
736         }
737       }
738
739       /** flag to prevent endless save loop, if this object is referenced
740         by another object which falls in this transaction. */

741     private boolean alreadyInSave = false;
742       /**
743      * Stores the object in the database. If the object is new,
744      * it inserts it; otherwise an update is performed. This method
745      * is meant to be used as part of a transaction, otherwise use
746      * the save() method and the connection details will be handled
747      * internally
748      *
749      * @param con
750      * @throws TorqueException
751      */

752     public void save(Connection JavaDoc con) throws TorqueException
753     {
754           if (!alreadyInSave)
755         {
756             alreadyInSave = true;
757
758
759   
760             // If this object has been modified, then save it to the database.
761
if (isModified())
762             {
763                 if (isNew())
764                 {
765                     AttributeTypePeer.doInsert((AttributeType)this, con);
766                     setNew(false);
767                 }
768                 else
769                 {
770                     AttributeTypePeer.doUpdate((AttributeType)this, con);
771                 }
772
773                       if (isCacheOnSave())
774                 {
775                     AttributeTypeManager.putInstance(this);
776                 }
777               }
778
779                                       
780                             if (collAttributes != null)
781             {
782                 for (int i = 0; i < collAttributes.size(); i++)
783                 {
784                     ((Attribute)collAttributes.get(i)).save(con);
785                 }
786             }
787                           alreadyInSave = false;
788         }
789       }
790
791     /**
792      * Specify whether to cache the object after saving to the db.
793      * This method returns false
794      */

795     protected boolean isCacheOnSave()
796     {
797         return true;
798     }
799
800                         
801       /**
802      * Set the PrimaryKey using ObjectKey.
803      *
804      * @param attributeTypeId ObjectKey
805      */

806     public void setPrimaryKey(ObjectKey attributeTypeId)
807         throws TorqueException {
808             setAttributeTypeId(new Integer JavaDoc(((NumberKey)attributeTypeId).intValue()));
809         }
810
811     /**
812      * Set the PrimaryKey using a String.
813      *
814      * @param key
815      */

816     public void setPrimaryKey(String JavaDoc key) throws TorqueException
817     {
818             setAttributeTypeId(new Integer JavaDoc(key));
819         }
820
821   
822     /**
823      * returns an id that differentiates this object from others
824      * of its class.
825      */

826     public ObjectKey getPrimaryKey()
827     {
828           return SimpleKey.keyFor(getAttributeTypeId());
829       }
830  
831     /**
832      * get an id that differentiates this object from others
833      * of its class.
834      */

835     public String JavaDoc getQueryKey()
836     {
837         if (getPrimaryKey() == null)
838         {
839             return "";
840         }
841         else
842         {
843             return getPrimaryKey().toString();
844         }
845     }
846
847     /**
848      * set an id that differentiates this object from others
849      * of its class.
850      */

851     public void setQueryKey(String JavaDoc key)
852         throws TorqueException
853     {
854         setPrimaryKey(key);
855     }
856
857     /**
858      * Makes a copy of this object.
859      * It creates a new object filling in the simple attributes.
860        * It then fills all the association collections and sets the
861      * related objects to isNew=true.
862        */

863       public AttributeType copy() throws TorqueException
864     {
865         AttributeType copyObj = new AttributeType();
866             copyObj.setAttributeTypeId(attributeTypeId);
867           copyObj.setClassId(classId);
868           copyObj.setName(name);
869           copyObj.setJavaClassName(javaClassName);
870           copyObj.setValidationKey(validationKey);
871   
872                       copyObj.setAttributeTypeId((Integer JavaDoc)null);
873                                     
874                                       
875                 
876         List JavaDoc v = getAttributes();
877         for (int i = 0; i < v.size(); i++)
878         {
879             Attribute obj = (Attribute) v.get(i);
880             copyObj.addAttribute(obj.copy());
881         }
882                             return copyObj;
883     }
884
885     /**
886      * returns a peer instance associated with this om. Since Peer classes
887      * are not to have any instance attributes, this method returns the
888      * same instance for all member of this class. The method could therefore
889      * be static, but this would prevent one from overriding the behavior.
890      */

891     public AttributeTypePeer getPeer()
892     {
893         return peer;
894     }
895
896     public String JavaDoc toString()
897     {
898         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
899         str.append("AttributeType:\n");
900         str.append("AttributeTypeId = ")
901                .append(getAttributeTypeId())
902              .append("\n");
903         str.append("ClassId = ")
904                .append(getClassId())
905              .append("\n");
906         str.append("Name = ")
907                .append(getName())
908              .append("\n");
909         str.append("JavaClassName = ")
910                .append(getJavaClassName())
911              .append("\n");
912         str.append("ValidationKey = ")
913                .append(getValidationKey())
914              .append("\n");
915         return(str.toString());
916     }
917 }
918
Popular Tags