View Javadoc

1   package net.sourceforge.blogentis.om;
2   
3   
4   import java.sql.Connection;
5   import java.util.ArrayList;
6   import java.util.Collections;
7   import java.util.List;
8   
9   import org.apache.torque.TorqueException;
10  import org.apache.torque.om.BaseObject;
11  import org.apache.torque.om.ComboKey;
12  import org.apache.torque.om.NumberKey;
13  import org.apache.torque.om.ObjectKey;
14  import org.apache.torque.om.SimpleKey;
15  import org.apache.torque.util.Transaction;
16  
17    
18      
19    
20  /***
21   * You should not use this class directly.  It should not even be
22   * extended all references should be to PostSection
23   */
24  public abstract class BasePostSection extends BaseObject
25      implements org.apache.turbine.om.Retrievable
26  {
27      /*** The Peer class */
28      private static final PostSectionPeer peer =
29          new PostSectionPeer();
30  
31          
32      /*** The value for the postId field */
33      private int postId;
34        
35      /*** The value for the sectionId field */
36      private int sectionId;
37    
38      
39      /***
40       * Get the PostId
41       *
42       * @return int
43       */
44      public int getPostId()
45      {
46          return postId;
47      }
48  
49                                
50      /***
51       * Set the value of PostId
52       *
53       * @param v new value
54       */
55      public void setPostId(int v) throws TorqueException
56      {
57      
58                    if (this.postId != v)
59                {
60              this.postId = v;
61              setModified(true);
62          }
63      
64                            
65                  if (aPost != null && !(aPost.getPostId() == v))
66                  {
67              aPost = null;
68          }
69        
70                }
71    
72      /***
73       * Get the SectionId
74       *
75       * @return int
76       */
77      public int getSectionId()
78      {
79          return sectionId;
80      }
81  
82                                
83      /***
84       * Set the value of SectionId
85       *
86       * @param v new value
87       */
88      public void setSectionId(int v) throws TorqueException
89      {
90      
91                    if (this.sectionId != v)
92                {
93              this.sectionId = v;
94              setModified(true);
95          }
96      
97                            
98                  if (aSection != null && !(aSection.getSectionId() == v))
99                  {
100             aSection = null;
101         }
102       
103               }
104   
105       
106     
107                   
108     
109         private Post aPost;
110 
111     /***
112      * Declares an association between this object and a Post object
113      *
114      * @param v Post
115      * @throws TorqueException
116      */
117     public void setPost(Post v) throws TorqueException
118     {
119             if (v == null)
120         {
121                           setPostId( 0);
122               }
123         else
124         {
125             setPostId(v.getPostId());
126         }
127             aPost = v;
128     }
129 
130                                             
131     /***
132      * Get the associated Post object
133      *
134      * @return the associated Post object
135      * @throws TorqueException
136      */
137     public Post getPost() throws TorqueException
138     {
139         if (aPost == null && (this.postId > 0))
140         {
141                           aPost = PostPeer.retrieveByPK(SimpleKey.keyFor(this.postId));
142               
143             /* The following can be used instead of the line above to
144                guarantee the related object contains a reference
145                to this object, but this level of coupling
146                may be undesirable in many circumstances.
147                As it can lead to a db query with many results that may
148                never be used.
149                Post obj = PostPeer.retrieveByPK(this.postId);
150                obj.addPostSections(this);
151             */
152         }
153         return aPost;
154     }
155 
156     /***
157      * Provides convenient way to set a relationship based on a
158      * ObjectKey.  e.g.
159      * <code>bar.setFooKey(foo.getPrimaryKey())</code>
160      *
161          */
162     public void setPostKey(ObjectKey key) throws TorqueException
163     {
164       
165                         setPostId(((NumberKey) key).intValue());
166                   }
167     
168     
169                   
170     
171         private Section aSection;
172 
173     /***
174      * Declares an association between this object and a Section object
175      *
176      * @param v Section
177      * @throws TorqueException
178      */
179     public void setSection(Section v) throws TorqueException
180     {
181             if (v == null)
182         {
183                           setSectionId( 0);
184               }
185         else
186         {
187             setSectionId(v.getSectionId());
188         }
189             aSection = v;
190     }
191 
192                                             
193     /***
194      * Get the associated Section object
195      *
196      * @return the associated Section object
197      * @throws TorqueException
198      */
199     public Section getSection() throws TorqueException
200     {
201         if (aSection == null && (this.sectionId > 0))
202         {
203                           aSection = SectionPeer.retrieveByPK(SimpleKey.keyFor(this.sectionId));
204               
205             /* The following can be used instead of the line above to
206                guarantee the related object contains a reference
207                to this object, but this level of coupling
208                may be undesirable in many circumstances.
209                As it can lead to a db query with many results that may
210                never be used.
211                Section obj = SectionPeer.retrieveByPK(this.sectionId);
212                obj.addPostSections(this);
213             */
214         }
215         return aSection;
216     }
217 
218     /***
219      * Provides convenient way to set a relationship based on a
220      * ObjectKey.  e.g.
221      * <code>bar.setFooKey(foo.getPrimaryKey())</code>
222      *
223          */
224     public void setSectionKey(ObjectKey key) throws TorqueException
225     {
226       
227                         setSectionId(((NumberKey) key).intValue());
228                   }
229        
230                 
231     private static List fieldNames = null;
232 
233     /***
234      * Generate a list of field names.
235      *
236      * @return a list of field names
237      */
238     public static synchronized List getFieldNames()
239     {
240         if (fieldNames == null)
241         {
242             fieldNames = new ArrayList();
243               fieldNames.add("PostId");
244               fieldNames.add("SectionId");
245               fieldNames = Collections.unmodifiableList(fieldNames);
246         }
247         return fieldNames;
248     }
249 
250     /***
251      * Retrieves a field from the object by name passed in as a String.
252      *
253      * @param name field name
254      * @return value
255      */
256     public Object getByName(String name)
257     {
258           if (name.equals("PostId"))
259         {
260                 return new Integer(getPostId());
261             }
262           if (name.equals("SectionId"))
263         {
264                 return new Integer(getSectionId());
265             }
266           return null;
267     }
268     
269     /***
270      * Retrieves a field from the object by name passed in
271      * as a String.  The String must be one of the static
272      * Strings defined in this Class' Peer.
273      *
274      * @param name peer name
275      * @return value
276      */
277     public Object getByPeerName(String name)
278     {
279           if (name.equals(PostSectionPeer.POST_ID))
280         {
281                 return new Integer(getPostId());
282             }
283           if (name.equals(PostSectionPeer.SECTION_ID))
284         {
285                 return new Integer(getSectionId());
286             }
287           return null;
288     }
289 
290     /***
291      * Retrieves a field from the object by Position as specified
292      * in the xml schema.  Zero-based.
293      *
294      * @param pos position in xml schema
295      * @return value
296      */
297     public Object getByPosition(int pos)
298     {
299             if (pos == 0)
300         {
301                 return new Integer(getPostId());
302             }
303               if (pos == 1)
304         {
305                 return new Integer(getSectionId());
306             }
307               return null;
308     }
309      
310     /***
311      * Stores the object in the database.  If the object is new,
312      * it inserts it; otherwise an update is performed.
313      *
314      * @throws Exception
315      */
316     public void save() throws Exception
317     {
318           save(PostSectionPeer.getMapBuilder()
319                 .getDatabaseMap().getName());
320       }
321 
322     /***
323      * Stores the object in the database.  If the object is new,
324      * it inserts it; otherwise an update is performed.
325        * Note: this code is here because the method body is
326      * auto-generated conditionally and therefore needs to be
327      * in this file instead of in the super class, BaseObject.
328        *
329      * @param dbName
330      * @throws TorqueException
331      */
332     public void save(String dbName) throws TorqueException
333     {
334         Connection con = null;
335           try
336         {
337             con = Transaction.begin(dbName);
338             save(con);
339             Transaction.commit(con);
340         }
341         catch(TorqueException e)
342         {
343             Transaction.safeRollback(con);
344             throw e;
345         }
346       }
347 
348       /*** flag to prevent endless save loop, if this object is referenced
349         by another object which falls in this transaction. */
350     private boolean alreadyInSave = false;
351       /***
352      * Stores the object in the database.  If the object is new,
353      * it inserts it; otherwise an update is performed.  This method
354      * is meant to be used as part of a transaction, otherwise use
355      * the save() method and the connection details will be handled
356      * internally
357      *
358      * @param con
359      * @throws TorqueException
360      */
361     public void save(Connection con) throws TorqueException
362     {
363           if (!alreadyInSave)
364         {
365             alreadyInSave = true;
366 
367 
368   
369             // If this object has been modified, then save it to the database.
370             if (isModified())
371             {
372                 if (isNew())
373                 {
374                     PostSectionPeer.doInsert((PostSection) this, con);
375                     setNew(false);
376                 }
377                 else
378                 {
379                     PostSectionPeer.doUpdate((PostSection) this, con);
380                 }
381             }
382 
383                       alreadyInSave = false;
384         }
385       }
386 
387                                               
388   
389     private final SimpleKey[] pks = new SimpleKey[2];
390     private final ComboKey comboPK = new ComboKey(pks);
391     
392     /***
393      * Set the PrimaryKey with an ObjectKey
394      *
395      * @param key
396      */
397     public void setPrimaryKey(ObjectKey key) throws TorqueException
398     {
399         SimpleKey[] keys = (SimpleKey[]) key.getValue();
400         SimpleKey tmpKey = null;
401                       setPostId(((NumberKey)keys[0]).intValue());
402                         setSectionId(((NumberKey)keys[1]).intValue());
403               }
404 
405     /***
406      * Set the PrimaryKey using SimpleKeys.
407      *
408          * @param int postId
409          * @param int sectionId
410          */
411     public void setPrimaryKey( int postId, int sectionId)
412         throws TorqueException
413     {
414             setPostId(postId);
415             setSectionId(sectionId);
416         }
417 
418     /***
419      * Set the PrimaryKey using a String.
420      */
421     public void setPrimaryKey(String key) throws TorqueException
422     {
423         setPrimaryKey(new ComboKey(key));
424     }
425   
426     /***
427      * returns an id that differentiates this object from others
428      * of its class.
429      */
430     public ObjectKey getPrimaryKey()
431     {
432               pks[0] = SimpleKey.keyFor(getPostId());
433                   pks[1] = SimpleKey.keyFor(getSectionId());
434                   return comboPK;
435       }
436  
437     /***
438      * get an id that differentiates this object from others
439      * of its class.
440      */
441     public String getQueryKey()
442     {
443         if (getPrimaryKey() == null)
444         {
445             return "";
446         }
447         else
448         {
449             return getPrimaryKey().toString();
450         }
451     }
452 
453     /***
454      * set an id that differentiates this object from others
455      * of its class.
456      */
457     public void setQueryKey(String key)
458         throws TorqueException
459     {
460         setPrimaryKey(key);
461     }
462 
463     /***
464      * Makes a copy of this object.
465      * It creates a new object filling in the simple attributes.
466        * It then fills all the association collections and sets the
467      * related objects to isNew=true.
468        */
469       public PostSection copy() throws TorqueException
470     {
471         return copyInto(new PostSection());
472     }
473   
474     protected PostSection copyInto(PostSection copyObj) throws TorqueException
475     {
476           copyObj.setPostId(postId);
477           copyObj.setSectionId(sectionId);
478   
479                             copyObj.setPostId( 0);
480                                       copyObj.setSectionId( 0);
481             
482                 return copyObj;
483     }
484 
485     /***
486      * returns a peer instance associated with this om.  Since Peer classes
487      * are not to have any instance attributes, this method returns the
488      * same instance for all member of this class. The method could therefore
489      * be static, but this would prevent one from overriding the behavior.
490      */
491     public PostSectionPeer getPeer()
492     {
493         return peer;
494     }
495 
496     public String toString()
497     {
498         StringBuffer str = new StringBuffer();
499         str.append("PostSection:\n");
500         str.append("PostId = ")
501            .append(getPostId())
502            .append("\n");
503         str.append("SectionId = ")
504            .append(getSectionId())
505            .append("\n");
506         return(str.toString());
507     }
508 }