001: package org.tigris.scarab.om;
002:
003: import java.math.BigDecimal;
004: import java.sql.Connection;
005: import java.util.ArrayList;
006: import java.util.Collections;
007: import java.util.Date;
008: import java.util.List;
009:
010: import org.apache.commons.lang.ObjectUtils;
011: import org.apache.fulcrum.intake.Retrievable;
012: import org.apache.torque.TorqueException;
013: import org.apache.torque.om.BaseObject;
014: import org.apache.torque.om.ComboKey;
015: import org.apache.torque.om.DateKey;
016: import org.apache.torque.om.NumberKey;
017: import org.apache.torque.om.ObjectKey;
018: import org.apache.torque.om.SimpleKey;
019: import org.apache.torque.om.StringKey;
020: import org.apache.torque.om.Persistent;
021: import org.apache.torque.util.Criteria;
022: import org.apache.torque.util.Transaction;
023:
024: /**
025: * You should not use this class directly. It should not even be
026: * extended all references should be to AttachmentType
027: */
028: public abstract class BaseAttachmentType extends BaseObject implements
029: org.apache.fulcrum.intake.Retrievable {
030: /** The Peer class */
031: private static final AttachmentTypePeer peer = new AttachmentTypePeer();
032:
033: /** The value for the attachmentTypeId field */
034: private Integer attachmentTypeId;
035:
036: /** The value for the name field */
037: private String name;
038:
039: /** The value for the searchable field */
040: private boolean searchable = false;
041:
042: /**
043: * Get the AttachmentTypeId
044: *
045: * @return Integer
046: */
047: public Integer getAttachmentTypeId() {
048: return attachmentTypeId;
049: }
050:
051: /**
052: * Set the value of AttachmentTypeId
053: *
054: * @param v new value
055: */
056: public void setAttachmentTypeId(Integer v) throws TorqueException {
057:
058: if (!ObjectUtils.equals(this .attachmentTypeId, v)) {
059: this .attachmentTypeId = v;
060: setModified(true);
061: }
062:
063: // update associated Attachment
064: if (collAttachments != null) {
065: for (int i = 0; i < collAttachments.size(); i++) {
066: ((Attachment) collAttachments.get(i)).setTypeId(v);
067: }
068: }
069: }
070:
071: /**
072: * Get the Name
073: *
074: * @return String
075: */
076: public String getName() {
077: return name;
078: }
079:
080: /**
081: * Set the value of Name
082: *
083: * @param v new value
084: */
085: public void setName(String v) {
086:
087: if (!ObjectUtils.equals(this .name, v)) {
088: this .name = v;
089: setModified(true);
090: }
091:
092: }
093:
094: /**
095: * Get the Searchable
096: *
097: * @return boolean
098: */
099: public boolean getSearchable() {
100: return searchable;
101: }
102:
103: /**
104: * Set the value of Searchable
105: *
106: * @param v new value
107: */
108: public void setSearchable(boolean v) {
109:
110: if (this .searchable != v) {
111: this .searchable = v;
112: setModified(true);
113: }
114:
115: }
116:
117: /**
118: * Collection to store aggregation of collAttachments
119: */
120: protected List collAttachments;
121:
122: /**
123: * Temporary storage of collAttachments to save a possible db hit in
124: * the event objects are add to the collection, but the
125: * complete collection is never requested.
126: */
127: protected void initAttachments() {
128: if (collAttachments == null) {
129: collAttachments = new ArrayList();
130: }
131: }
132:
133: /**
134: * Method called to associate a Attachment object to this object
135: * through the Attachment foreign key attribute
136: *
137: * @param l Attachment
138: * @throws TorqueException
139: */
140: public void addAttachment(Attachment l) throws TorqueException {
141: getAttachments().add(l);
142: l.setAttachmentType((AttachmentType) this );
143: }
144:
145: /**
146: * The criteria used to select the current contents of collAttachments
147: */
148: private Criteria lastAttachmentsCriteria = null;
149:
150: /**
151: * If this collection has already been initialized, returns
152: * the collection. Otherwise returns the results of
153: * getAttachments(new Criteria())
154: *
155: * @return the collection of associated objects
156: * @throws TorqueException
157: */
158: public List getAttachments() throws TorqueException {
159: if (collAttachments == null) {
160: collAttachments = getAttachments(new Criteria(10));
161: }
162: return collAttachments;
163: }
164:
165: /**
166: * If this collection has already been initialized with
167: * an identical criteria, it returns the collection.
168: * Otherwise if this AttachmentType has previously
169: * been saved, it will retrieve related Attachments from storage.
170: * If this AttachmentType is new, it will return
171: * an empty collection or the current collection, the criteria
172: * is ignored on a new object.
173: *
174: * @throws TorqueException
175: */
176: public List getAttachments(Criteria criteria)
177: throws TorqueException {
178: if (collAttachments == null) {
179: if (isNew()) {
180: collAttachments = new ArrayList();
181: } else {
182: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
183: getAttachmentTypeId());
184: collAttachments = AttachmentPeer.doSelect(criteria);
185: }
186: } else {
187: // criteria has no effect for a new object
188: if (!isNew()) {
189: // the following code is to determine if a new query is
190: // called for. If the criteria is the same as the last
191: // one, just return the collection.
192: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
193: getAttachmentTypeId());
194: if (!lastAttachmentsCriteria.equals(criteria)) {
195: collAttachments = AttachmentPeer.doSelect(criteria);
196: }
197: }
198: }
199: lastAttachmentsCriteria = criteria;
200:
201: return collAttachments;
202: }
203:
204: /**
205: * If this collection has already been initialized, returns
206: * the collection. Otherwise returns the results of
207: * getAttachments(new Criteria(),Connection)
208: * This method takes in the Connection also as input so that
209: * referenced objects can also be obtained using a Connection
210: * that is taken as input
211: */
212: public List getAttachments(Connection con) throws TorqueException {
213: if (collAttachments == null) {
214: collAttachments = getAttachments(new Criteria(10), con);
215: }
216: return collAttachments;
217: }
218:
219: /**
220: * If this collection has already been initialized with
221: * an identical criteria, it returns the collection.
222: * Otherwise if this AttachmentType has previously
223: * been saved, it will retrieve related Attachments from storage.
224: * If this AttachmentType is new, it will return
225: * an empty collection or the current collection, the criteria
226: * is ignored on a new object.
227: * This method takes in the Connection also as input so that
228: * referenced objects can also be obtained using a Connection
229: * that is taken as input
230: */
231: public List getAttachments(Criteria criteria, Connection con)
232: throws TorqueException {
233: if (collAttachments == null) {
234: if (isNew()) {
235: collAttachments = new ArrayList();
236: } else {
237: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
238: getAttachmentTypeId());
239: collAttachments = AttachmentPeer
240: .doSelect(criteria, con);
241: }
242: } else {
243: // criteria has no effect for a new object
244: if (!isNew()) {
245: // the following code is to determine if a new query is
246: // called for. If the criteria is the same as the last
247: // one, just return the collection.
248: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
249: getAttachmentTypeId());
250: if (!lastAttachmentsCriteria.equals(criteria)) {
251: collAttachments = AttachmentPeer.doSelect(criteria,
252: con);
253: }
254: }
255: }
256: lastAttachmentsCriteria = criteria;
257:
258: return collAttachments;
259: }
260:
261: /**
262: * If this collection has already been initialized with
263: * an identical criteria, it returns the collection.
264: * Otherwise if this AttachmentType is new, it will return
265: * an empty collection; or if this AttachmentType has previously
266: * been saved, it will retrieve related Attachments from storage.
267: *
268: * This method is protected by default in order to keep the public
269: * api reasonable. You can provide public methods for those you
270: * actually need in AttachmentType.
271: */
272: protected List getAttachmentsJoinIssue(Criteria criteria)
273: throws TorqueException {
274: if (collAttachments == null) {
275: if (isNew()) {
276: collAttachments = new ArrayList();
277: } else {
278: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
279: getAttachmentTypeId());
280: collAttachments = AttachmentPeer
281: .doSelectJoinIssue(criteria);
282: }
283: } else {
284: // the following code is to determine if a new query is
285: // called for. If the criteria is the same as the last
286: // one, just return the collection.
287: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
288: getAttachmentTypeId());
289: if (!lastAttachmentsCriteria.equals(criteria)) {
290: collAttachments = AttachmentPeer
291: .doSelectJoinIssue(criteria);
292: }
293: }
294: lastAttachmentsCriteria = criteria;
295:
296: return collAttachments;
297: }
298:
299: /**
300: * If this collection has already been initialized with
301: * an identical criteria, it returns the collection.
302: * Otherwise if this AttachmentType is new, it will return
303: * an empty collection; or if this AttachmentType has previously
304: * been saved, it will retrieve related Attachments from storage.
305: *
306: * This method is protected by default in order to keep the public
307: * api reasonable. You can provide public methods for those you
308: * actually need in AttachmentType.
309: */
310: protected List getAttachmentsJoinAttachmentType(Criteria criteria)
311: throws TorqueException {
312: if (collAttachments == null) {
313: if (isNew()) {
314: collAttachments = new ArrayList();
315: } else {
316: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
317: getAttachmentTypeId());
318: collAttachments = AttachmentPeer
319: .doSelectJoinAttachmentType(criteria);
320: }
321: } else {
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(AttachmentPeer.ATTACHMENT_TYPE_ID,
326: getAttachmentTypeId());
327: if (!lastAttachmentsCriteria.equals(criteria)) {
328: collAttachments = AttachmentPeer
329: .doSelectJoinAttachmentType(criteria);
330: }
331: }
332: lastAttachmentsCriteria = criteria;
333:
334: return collAttachments;
335: }
336:
337: /**
338: * If this collection has already been initialized with
339: * an identical criteria, it returns the collection.
340: * Otherwise if this AttachmentType is new, it will return
341: * an empty collection; or if this AttachmentType has previously
342: * been saved, it will retrieve related Attachments from storage.
343: *
344: * This method is protected by default in order to keep the public
345: * api reasonable. You can provide public methods for those you
346: * actually need in AttachmentType.
347: */
348: protected List getAttachmentsJoinScarabUserImplRelatedByCreatedBy(
349: Criteria criteria) throws TorqueException {
350: if (collAttachments == null) {
351: if (isNew()) {
352: collAttachments = new ArrayList();
353: } else {
354: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
355: getAttachmentTypeId());
356: collAttachments = AttachmentPeer
357: .doSelectJoinScarabUserImplRelatedByCreatedBy(criteria);
358: }
359: } else {
360: // the following code is to determine if a new query is
361: // called for. If the criteria is the same as the last
362: // one, just return the collection.
363: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
364: getAttachmentTypeId());
365: if (!lastAttachmentsCriteria.equals(criteria)) {
366: collAttachments = AttachmentPeer
367: .doSelectJoinScarabUserImplRelatedByCreatedBy(criteria);
368: }
369: }
370: lastAttachmentsCriteria = criteria;
371:
372: return collAttachments;
373: }
374:
375: /**
376: * If this collection has already been initialized with
377: * an identical criteria, it returns the collection.
378: * Otherwise if this AttachmentType is new, it will return
379: * an empty collection; or if this AttachmentType has previously
380: * been saved, it will retrieve related Attachments from storage.
381: *
382: * This method is protected by default in order to keep the public
383: * api reasonable. You can provide public methods for those you
384: * actually need in AttachmentType.
385: */
386: protected List getAttachmentsJoinScarabUserImplRelatedByModifiedBy(
387: Criteria criteria) throws TorqueException {
388: if (collAttachments == null) {
389: if (isNew()) {
390: collAttachments = new ArrayList();
391: } else {
392: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
393: getAttachmentTypeId());
394: collAttachments = AttachmentPeer
395: .doSelectJoinScarabUserImplRelatedByModifiedBy(criteria);
396: }
397: } else {
398: // the following code is to determine if a new query is
399: // called for. If the criteria is the same as the last
400: // one, just return the collection.
401: criteria.add(AttachmentPeer.ATTACHMENT_TYPE_ID,
402: getAttachmentTypeId());
403: if (!lastAttachmentsCriteria.equals(criteria)) {
404: collAttachments = AttachmentPeer
405: .doSelectJoinScarabUserImplRelatedByModifiedBy(criteria);
406: }
407: }
408: lastAttachmentsCriteria = criteria;
409:
410: return collAttachments;
411: }
412:
413: private static List fieldNames = null;
414:
415: /**
416: * Generate a list of field names.
417: *
418: * @return a list of field names
419: */
420: public static synchronized List getFieldNames() {
421: if (fieldNames == null) {
422: fieldNames = new ArrayList();
423: fieldNames.add("AttachmentTypeId");
424: fieldNames.add("Name");
425: fieldNames.add("Searchable");
426: fieldNames = Collections.unmodifiableList(fieldNames);
427: }
428: return fieldNames;
429: }
430:
431: /**
432: * Retrieves a field from the object by name passed in as a String.
433: *
434: * @param name field name
435: * @return value
436: */
437: public Object getByName(String name) {
438: if (name.equals("AttachmentTypeId")) {
439: return getAttachmentTypeId();
440: }
441: if (name.equals("Name")) {
442: return getName();
443: }
444: if (name.equals("Searchable")) {
445: return Boolean.valueOf(getSearchable());
446: }
447: return null;
448: }
449:
450: /**
451: * Retrieves a field from the object by name passed in
452: * as a String. The String must be one of the static
453: * Strings defined in this Class' Peer.
454: *
455: * @param name peer name
456: * @return value
457: */
458: public Object getByPeerName(String name) {
459: if (name.equals(AttachmentTypePeer.ATTACHMENT_TYPE_ID)) {
460: return getAttachmentTypeId();
461: }
462: if (name.equals(AttachmentTypePeer.ATTACHMENT_TYPE_NAME)) {
463: return getName();
464: }
465: if (name.equals(AttachmentTypePeer.SEARCHABLE)) {
466: return Boolean.valueOf(getSearchable());
467: }
468: return null;
469: }
470:
471: /**
472: * Retrieves a field from the object by Position as specified
473: * in the xml schema. Zero-based.
474: *
475: * @param pos position in xml schema
476: * @return value
477: */
478: public Object getByPosition(int pos) {
479: if (pos == 0) {
480: return getAttachmentTypeId();
481: }
482: if (pos == 1) {
483: return getName();
484: }
485: if (pos == 2) {
486: return Boolean.valueOf(getSearchable());
487: }
488: return null;
489: }
490:
491: /**
492: * Stores the object in the database. If the object is new,
493: * it inserts it; otherwise an update is performed.
494: *
495: * @throws TorqueException
496: */
497: public void save() throws TorqueException {
498: save(AttachmentTypePeer.getMapBuilder().getDatabaseMap()
499: .getName());
500: }
501:
502: /**
503: * Stores the object in the database. If the object is new,
504: * it inserts it; otherwise an update is performed.
505: * Note: this code is here because the method body is
506: * auto-generated conditionally and therefore needs to be
507: * in this file instead of in the super class, BaseObject.
508: *
509: * @param dbName
510: * @throws TorqueException
511: */
512: public void save(String dbName) throws TorqueException {
513: Connection con = null;
514: try {
515: con = Transaction.begin(dbName);
516: save(con);
517: Transaction.commit(con);
518: } catch (TorqueException e) {
519: Transaction.safeRollback(con);
520: throw e;
521: }
522: }
523:
524: /** flag to prevent endless save loop, if this object is referenced
525: by another object which falls in this transaction. */
526: private boolean alreadyInSave = false;
527:
528: /**
529: * Stores the object in the database. If the object is new,
530: * it inserts it; otherwise an update is performed. This method
531: * is meant to be used as part of a transaction, otherwise use
532: * the save() method and the connection details will be handled
533: * internally
534: *
535: * @param con
536: * @throws TorqueException
537: */
538: public void save(Connection con) throws TorqueException {
539: if (!alreadyInSave) {
540: alreadyInSave = true;
541:
542: // If this object has been modified, then save it to the database.
543: if (isModified()) {
544: if (isNew()) {
545: AttachmentTypePeer.doInsert((AttachmentType) this ,
546: con);
547: setNew(false);
548: } else {
549: AttachmentTypePeer.doUpdate((AttachmentType) this ,
550: con);
551: }
552:
553: if (isCacheOnSave()) {
554: AttachmentTypeManager.putInstance(this );
555: }
556: }
557:
558: if (collAttachments != null) {
559: for (int i = 0; i < collAttachments.size(); i++) {
560: ((Attachment) collAttachments.get(i)).save(con);
561: }
562: }
563: alreadyInSave = false;
564: }
565: }
566:
567: /**
568: * Specify whether to cache the object after saving to the db.
569: * This method returns true
570: */
571: protected boolean isCacheOnSave() {
572: return true;
573: }
574:
575: /**
576: * Set the PrimaryKey using ObjectKey.
577: *
578: * @param key attachmentTypeId ObjectKey
579: */
580: public void setPrimaryKey(ObjectKey key) throws TorqueException {
581: setAttachmentTypeId(new Integer(((NumberKey) key).intValue()));
582: }
583:
584: /**
585: * Set the PrimaryKey using a String.
586: *
587: * @param key
588: */
589: public void setPrimaryKey(String key) throws TorqueException {
590: setAttachmentTypeId(new Integer(key));
591: }
592:
593: /**
594: * returns an id that differentiates this object from others
595: * of its class.
596: */
597: public ObjectKey getPrimaryKey() {
598: return SimpleKey.keyFor(getAttachmentTypeId());
599: }
600:
601: /**
602: * get an id that differentiates this object from others
603: * of its class.
604: */
605: public String getQueryKey() {
606: if (getPrimaryKey() == null) {
607: return "";
608: } else {
609: return getPrimaryKey().toString();
610: }
611: }
612:
613: /**
614: * set an id that differentiates this object from others
615: * of its class.
616: */
617: public void setQueryKey(String key) throws TorqueException {
618: setPrimaryKey(key);
619: }
620:
621: /**
622: * Makes a copy of this object.
623: * It creates a new object filling in the simple attributes.
624: * It then fills all the association collections and sets the
625: * related objects to isNew=true.
626: */
627: public AttachmentType copy() throws TorqueException {
628: return copyInto(new AttachmentType());
629: }
630:
631: protected AttachmentType copyInto(AttachmentType copyObj)
632: throws TorqueException {
633: copyObj.setAttachmentTypeId(attachmentTypeId);
634: copyObj.setName(name);
635: copyObj.setSearchable(searchable);
636:
637: copyObj.setAttachmentTypeId((Integer) null);
638:
639: List v = getAttachments();
640: if (v != null) {
641: for (int i = 0; i < v.size(); i++) {
642: Attachment obj = (Attachment) v.get(i);
643: copyObj.addAttachment(obj.copy());
644: }
645: } else {
646: copyObj.collAttachments = null;
647: }
648: return copyObj;
649: }
650:
651: /**
652: * returns a peer instance associated with this om. Since Peer classes
653: * are not to have any instance attributes, this method returns the
654: * same instance for all member of this class. The method could therefore
655: * be static, but this would prevent one from overriding the behavior.
656: */
657: public AttachmentTypePeer getPeer() {
658: return peer;
659: }
660:
661: public String toString() {
662: StringBuffer str = new StringBuffer();
663: str.append("AttachmentType:\n");
664: str.append("AttachmentTypeId = ").append(getAttachmentTypeId())
665: .append("\n");
666: str.append("Name = ").append(getName()).append("\n");
667: str.append("Searchable = ").append(getSearchable())
668: .append("\n");
669: return (str.toString());
670: }
671: }
|