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 ROptionOption
027: */
028: public abstract class BaseROptionOption extends BaseObject implements
029: org.apache.fulcrum.intake.Retrievable {
030: /** The Peer class */
031: private static final ROptionOptionPeer peer = new ROptionOptionPeer();
032:
033: /** The value for the option1Id field */
034: private Integer option1Id;
035:
036: /** The value for the option2Id field */
037: private Integer option2Id;
038:
039: /** The value for the relationshipId field */
040: private Integer relationshipId;
041:
042: /** The value for the weight field */
043: private int weight;
044:
045: /** The value for the preferredOrder field */
046: private int preferredOrder;
047:
048: /**
049: * Get the Option1Id
050: *
051: * @return Integer
052: */
053: public Integer getOption1Id() {
054: return option1Id;
055: }
056:
057: /**
058: * Set the value of Option1Id
059: *
060: * @param v new value
061: */
062: public void setOption1Id(Integer v) throws TorqueException {
063:
064: if (!ObjectUtils.equals(this .option1Id, v)) {
065: this .option1Id = v;
066: setModified(true);
067: }
068:
069: if (aAttributeOptionRelatedByOption1Id != null
070: && !ObjectUtils.equals(
071: aAttributeOptionRelatedByOption1Id
072: .getOptionId(), v)) {
073: aAttributeOptionRelatedByOption1Id = null;
074: }
075:
076: }
077:
078: /**
079: * Get the Option2Id
080: *
081: * @return Integer
082: */
083: public Integer getOption2Id() {
084: return option2Id;
085: }
086:
087: /**
088: * Set the value of Option2Id
089: *
090: * @param v new value
091: */
092: public void setOption2Id(Integer v) throws TorqueException {
093:
094: if (!ObjectUtils.equals(this .option2Id, v)) {
095: this .option2Id = v;
096: setModified(true);
097: }
098:
099: if (aAttributeOptionRelatedByOption2Id != null
100: && !ObjectUtils.equals(
101: aAttributeOptionRelatedByOption2Id
102: .getOptionId(), v)) {
103: aAttributeOptionRelatedByOption2Id = null;
104: }
105:
106: }
107:
108: /**
109: * Get the RelationshipId
110: *
111: * @return Integer
112: */
113: public Integer getRelationshipId() {
114: return relationshipId;
115: }
116:
117: /**
118: * Set the value of RelationshipId
119: *
120: * @param v new value
121: */
122: public void setRelationshipId(Integer v) throws TorqueException {
123:
124: if (!ObjectUtils.equals(this .relationshipId, v)) {
125: this .relationshipId = v;
126: setModified(true);
127: }
128:
129: if (aOptionRelationship != null
130: && !ObjectUtils.equals(aOptionRelationship
131: .getRelationshipId(), v)) {
132: aOptionRelationship = null;
133: }
134:
135: }
136:
137: /**
138: * Get the Weight
139: *
140: * @return int
141: */
142: public int getWeight() {
143: return weight;
144: }
145:
146: /**
147: * Set the value of Weight
148: *
149: * @param v new value
150: */
151: public void setWeight(int v) {
152:
153: if (this .weight != v) {
154: this .weight = v;
155: setModified(true);
156: }
157:
158: }
159:
160: /**
161: * Get the PreferredOrder
162: *
163: * @return int
164: */
165: public int getPreferredOrder() {
166: return preferredOrder;
167: }
168:
169: /**
170: * Set the value of PreferredOrder
171: *
172: * @param v new value
173: */
174: public void setPreferredOrder(int v) {
175:
176: if (this .preferredOrder != v) {
177: this .preferredOrder = v;
178: setModified(true);
179: }
180:
181: }
182:
183: private AttributeOption aAttributeOptionRelatedByOption1Id;
184:
185: /**
186: * Declares an association between this object and a AttributeOption object
187: *
188: * @param v AttributeOption
189: * @throws TorqueException
190: */
191: public void setAttributeOptionRelatedByOption1Id(AttributeOption v)
192: throws TorqueException {
193: if (v == null) {
194: setOption1Id((Integer) null);
195: } else {
196: setOption1Id(v.getOptionId());
197: }
198: aAttributeOptionRelatedByOption1Id = v;
199: }
200:
201: /**
202: * Returns the associated AttributeOption object.
203: * If it was not retrieved before, the object is retrieved from
204: * the database
205: *
206: * @return the associated AttributeOption object
207: * @throws TorqueException
208: */
209: public AttributeOption getAttributeOptionRelatedByOption1Id()
210: throws TorqueException {
211: if (aAttributeOptionRelatedByOption1Id == null
212: && (!ObjectUtils.equals(this .option1Id, null))) {
213: aAttributeOptionRelatedByOption1Id = AttributeOptionManager
214: .getInstance(SimpleKey.keyFor(this .option1Id));
215: }
216: return aAttributeOptionRelatedByOption1Id;
217: }
218:
219: /**
220: * Return the associated AttributeOption object
221: * If it was not retrieved before, the object is retrieved from
222: * the database using the passed connection
223: *
224: * @param connection the connection used to retrieve the associated object
225: * from the database, if it was not retrieved before
226: * @return the associated AttributeOption object
227: * @throws TorqueException
228: */
229: public AttributeOption getAttributeOptionRelatedByOption1Id(
230: Connection connection) throws TorqueException {
231: if (aAttributeOptionRelatedByOption1Id == null
232: && (!ObjectUtils.equals(this .option1Id, null))) {
233: aAttributeOptionRelatedByOption1Id = AttributeOptionManager
234: .getCachedInstance(SimpleKey.keyFor(this .option1Id));
235: if (aAttributeOptionRelatedByOption1Id == null) {
236: aAttributeOptionRelatedByOption1Id = AttributeOptionPeer
237: .retrieveByPK(SimpleKey.keyFor(this .option1Id),
238: connection);
239: AttributeOptionManager
240: .putInstance(aAttributeOptionRelatedByOption1Id);
241: }
242: }
243: return aAttributeOptionRelatedByOption1Id;
244: }
245:
246: /**
247: * Provides convenient way to set a relationship based on a
248: * ObjectKey, for example
249: * <code>bar.setFooKey(foo.getPrimaryKey())</code>
250: *
251: */
252: public void setAttributeOptionRelatedByOption1IdKey(ObjectKey key)
253: throws TorqueException {
254:
255: setOption1Id(new Integer(((NumberKey) key).intValue()));
256: }
257:
258: private AttributeOption aAttributeOptionRelatedByOption2Id;
259:
260: /**
261: * Declares an association between this object and a AttributeOption object
262: *
263: * @param v AttributeOption
264: * @throws TorqueException
265: */
266: public void setAttributeOptionRelatedByOption2Id(AttributeOption v)
267: throws TorqueException {
268: if (v == null) {
269: setOption2Id((Integer) null);
270: } else {
271: setOption2Id(v.getOptionId());
272: }
273: aAttributeOptionRelatedByOption2Id = v;
274: }
275:
276: /**
277: * Returns the associated AttributeOption object.
278: * If it was not retrieved before, the object is retrieved from
279: * the database
280: *
281: * @return the associated AttributeOption object
282: * @throws TorqueException
283: */
284: public AttributeOption getAttributeOptionRelatedByOption2Id()
285: throws TorqueException {
286: if (aAttributeOptionRelatedByOption2Id == null
287: && (!ObjectUtils.equals(this .option2Id, null))) {
288: aAttributeOptionRelatedByOption2Id = AttributeOptionManager
289: .getInstance(SimpleKey.keyFor(this .option2Id));
290: }
291: return aAttributeOptionRelatedByOption2Id;
292: }
293:
294: /**
295: * Return the associated AttributeOption object
296: * If it was not retrieved before, the object is retrieved from
297: * the database using the passed connection
298: *
299: * @param connection the connection used to retrieve the associated object
300: * from the database, if it was not retrieved before
301: * @return the associated AttributeOption object
302: * @throws TorqueException
303: */
304: public AttributeOption getAttributeOptionRelatedByOption2Id(
305: Connection connection) throws TorqueException {
306: if (aAttributeOptionRelatedByOption2Id == null
307: && (!ObjectUtils.equals(this .option2Id, null))) {
308: aAttributeOptionRelatedByOption2Id = AttributeOptionManager
309: .getCachedInstance(SimpleKey.keyFor(this .option2Id));
310: if (aAttributeOptionRelatedByOption2Id == null) {
311: aAttributeOptionRelatedByOption2Id = AttributeOptionPeer
312: .retrieveByPK(SimpleKey.keyFor(this .option2Id),
313: connection);
314: AttributeOptionManager
315: .putInstance(aAttributeOptionRelatedByOption2Id);
316: }
317: }
318: return aAttributeOptionRelatedByOption2Id;
319: }
320:
321: /**
322: * Provides convenient way to set a relationship based on a
323: * ObjectKey, for example
324: * <code>bar.setFooKey(foo.getPrimaryKey())</code>
325: *
326: */
327: public void setAttributeOptionRelatedByOption2IdKey(ObjectKey key)
328: throws TorqueException {
329:
330: setOption2Id(new Integer(((NumberKey) key).intValue()));
331: }
332:
333: private OptionRelationship aOptionRelationship;
334:
335: /**
336: * Declares an association between this object and a OptionRelationship object
337: *
338: * @param v OptionRelationship
339: * @throws TorqueException
340: */
341: public void setOptionRelationship(OptionRelationship v)
342: throws TorqueException {
343: if (v == null) {
344: setRelationshipId((Integer) null);
345: } else {
346: setRelationshipId(v.getRelationshipId());
347: }
348: aOptionRelationship = v;
349: }
350:
351: /**
352: * Returns the associated OptionRelationship object.
353: * If it was not retrieved before, the object is retrieved from
354: * the database
355: *
356: * @return the associated OptionRelationship object
357: * @throws TorqueException
358: */
359: public OptionRelationship getOptionRelationship()
360: throws TorqueException {
361: if (aOptionRelationship == null
362: && (!ObjectUtils.equals(this .relationshipId, null))) {
363: aOptionRelationship = OptionRelationshipManager
364: .getInstance(SimpleKey.keyFor(this .relationshipId));
365: }
366: return aOptionRelationship;
367: }
368:
369: /**
370: * Return the associated OptionRelationship object
371: * If it was not retrieved before, the object is retrieved from
372: * the database using the passed connection
373: *
374: * @param connection the connection used to retrieve the associated object
375: * from the database, if it was not retrieved before
376: * @return the associated OptionRelationship object
377: * @throws TorqueException
378: */
379: public OptionRelationship getOptionRelationship(
380: Connection connection) throws TorqueException {
381: if (aOptionRelationship == null
382: && (!ObjectUtils.equals(this .relationshipId, null))) {
383: aOptionRelationship = OptionRelationshipManager
384: .getCachedInstance(SimpleKey
385: .keyFor(this .relationshipId));
386: if (aOptionRelationship == null) {
387: aOptionRelationship = OptionRelationshipPeer
388: .retrieveByPK(SimpleKey
389: .keyFor(this .relationshipId),
390: connection);
391: OptionRelationshipManager
392: .putInstance(aOptionRelationship);
393: }
394: }
395: return aOptionRelationship;
396: }
397:
398: /**
399: * Provides convenient way to set a relationship based on a
400: * ObjectKey, for example
401: * <code>bar.setFooKey(foo.getPrimaryKey())</code>
402: *
403: */
404: public void setOptionRelationshipKey(ObjectKey key)
405: throws TorqueException {
406:
407: setRelationshipId(new Integer(((NumberKey) key).intValue()));
408: }
409:
410: private static List fieldNames = null;
411:
412: /**
413: * Generate a list of field names.
414: *
415: * @return a list of field names
416: */
417: public static synchronized List getFieldNames() {
418: if (fieldNames == null) {
419: fieldNames = new ArrayList();
420: fieldNames.add("Option1Id");
421: fieldNames.add("Option2Id");
422: fieldNames.add("RelationshipId");
423: fieldNames.add("Weight");
424: fieldNames.add("PreferredOrder");
425: fieldNames = Collections.unmodifiableList(fieldNames);
426: }
427: return fieldNames;
428: }
429:
430: /**
431: * Retrieves a field from the object by name passed in as a String.
432: *
433: * @param name field name
434: * @return value
435: */
436: public Object getByName(String name) {
437: if (name.equals("Option1Id")) {
438: return getOption1Id();
439: }
440: if (name.equals("Option2Id")) {
441: return getOption2Id();
442: }
443: if (name.equals("RelationshipId")) {
444: return getRelationshipId();
445: }
446: if (name.equals("Weight")) {
447: return new Integer(getWeight());
448: }
449: if (name.equals("PreferredOrder")) {
450: return new Integer(getPreferredOrder());
451: }
452: return null;
453: }
454:
455: /**
456: * Retrieves a field from the object by name passed in
457: * as a String. The String must be one of the static
458: * Strings defined in this Class' Peer.
459: *
460: * @param name peer name
461: * @return value
462: */
463: public Object getByPeerName(String name) {
464: if (name.equals(ROptionOptionPeer.OPTION1_ID)) {
465: return getOption1Id();
466: }
467: if (name.equals(ROptionOptionPeer.OPTION2_ID)) {
468: return getOption2Id();
469: }
470: if (name.equals(ROptionOptionPeer.RELATIONSHIP_ID)) {
471: return getRelationshipId();
472: }
473: if (name.equals(ROptionOptionPeer.WEIGHT)) {
474: return new Integer(getWeight());
475: }
476: if (name.equals(ROptionOptionPeer.PREFERRED_ORDER)) {
477: return new Integer(getPreferredOrder());
478: }
479: return null;
480: }
481:
482: /**
483: * Retrieves a field from the object by Position as specified
484: * in the xml schema. Zero-based.
485: *
486: * @param pos position in xml schema
487: * @return value
488: */
489: public Object getByPosition(int pos) {
490: if (pos == 0) {
491: return getOption1Id();
492: }
493: if (pos == 1) {
494: return getOption2Id();
495: }
496: if (pos == 2) {
497: return getRelationshipId();
498: }
499: if (pos == 3) {
500: return new Integer(getWeight());
501: }
502: if (pos == 4) {
503: return new Integer(getPreferredOrder());
504: }
505: return null;
506: }
507:
508: /**
509: * Stores the object in the database. If the object is new,
510: * it inserts it; otherwise an update is performed.
511: *
512: * @throws TorqueException
513: */
514: public void save() throws TorqueException {
515: save(ROptionOptionPeer.getMapBuilder().getDatabaseMap()
516: .getName());
517: }
518:
519: /**
520: * Stores the object in the database. If the object is new,
521: * it inserts it; otherwise an update is performed.
522: * Note: this code is here because the method body is
523: * auto-generated conditionally and therefore needs to be
524: * in this file instead of in the super class, BaseObject.
525: *
526: * @param dbName
527: * @throws TorqueException
528: */
529: public void save(String dbName) throws TorqueException {
530: Connection con = null;
531: try {
532: con = Transaction.begin(dbName);
533: save(con);
534: Transaction.commit(con);
535: } catch (TorqueException e) {
536: Transaction.safeRollback(con);
537: throw e;
538: }
539: }
540:
541: /** flag to prevent endless save loop, if this object is referenced
542: by another object which falls in this transaction. */
543: private boolean alreadyInSave = false;
544:
545: /**
546: * Stores the object in the database. If the object is new,
547: * it inserts it; otherwise an update is performed. This method
548: * is meant to be used as part of a transaction, otherwise use
549: * the save() method and the connection details will be handled
550: * internally
551: *
552: * @param con
553: * @throws TorqueException
554: */
555: public void save(Connection con) throws TorqueException {
556: if (!alreadyInSave) {
557: alreadyInSave = true;
558:
559: // If this object has been modified, then save it to the database.
560: if (isModified()) {
561: if (isNew()) {
562: ROptionOptionPeer.doInsert((ROptionOption) this ,
563: con);
564: setNew(false);
565: } else {
566: ROptionOptionPeer.doUpdate((ROptionOption) this ,
567: con);
568: }
569:
570: if (isCacheOnSave()) {
571: ROptionOptionManager.putInstance(this );
572: }
573: }
574:
575: alreadyInSave = false;
576: }
577: }
578:
579: /**
580: * Specify whether to cache the object after saving to the db.
581: * This method returns true
582: */
583: protected boolean isCacheOnSave() {
584: return true;
585: }
586:
587: private final SimpleKey[] pks = new SimpleKey[2];
588: private final ComboKey comboPK = new ComboKey(pks);
589:
590: /**
591: * Set the PrimaryKey with an ObjectKey
592: *
593: * @param key
594: */
595: public void setPrimaryKey(ObjectKey key) throws TorqueException {
596: SimpleKey[] keys = (SimpleKey[]) key.getValue();
597: SimpleKey tmpKey = null;
598: setOption1Id(new Integer(((NumberKey) keys[0]).intValue()));
599: setOption2Id(new Integer(((NumberKey) keys[1]).intValue()));
600: }
601:
602: /**
603: * Set the PrimaryKey using SimpleKeys.
604: *
605: * @param option1Id Integer
606: * @param option2Id Integer
607: */
608: public void setPrimaryKey(Integer option1Id, Integer option2Id)
609: throws TorqueException {
610: setOption1Id(option1Id);
611: setOption2Id(option2Id);
612: }
613:
614: /**
615: * Set the PrimaryKey using a String.
616: */
617: public void setPrimaryKey(String key) throws TorqueException {
618: setPrimaryKey(new ComboKey(key));
619: }
620:
621: /**
622: * returns an id that differentiates this object from others
623: * of its class.
624: */
625: public ObjectKey getPrimaryKey() {
626: pks[0] = SimpleKey.keyFor(getOption1Id());
627: pks[1] = SimpleKey.keyFor(getOption2Id());
628: return comboPK;
629: }
630:
631: /**
632: * get an id that differentiates this object from others
633: * of its class.
634: */
635: public String getQueryKey() {
636: if (getPrimaryKey() == null) {
637: return "";
638: } else {
639: return getPrimaryKey().toString();
640: }
641: }
642:
643: /**
644: * set an id that differentiates this object from others
645: * of its class.
646: */
647: public void setQueryKey(String key) throws TorqueException {
648: setPrimaryKey(key);
649: }
650:
651: /**
652: * Makes a copy of this object.
653: * It creates a new object filling in the simple attributes.
654: * It then fills all the association collections and sets the
655: * related objects to isNew=true.
656: */
657: public ROptionOption copy() throws TorqueException {
658: return copyInto(new ROptionOption());
659: }
660:
661: protected ROptionOption copyInto(ROptionOption copyObj)
662: throws TorqueException {
663: copyObj.setOption1Id(option1Id);
664: copyObj.setOption2Id(option2Id);
665: copyObj.setRelationshipId(relationshipId);
666: copyObj.setWeight(weight);
667: copyObj.setPreferredOrder(preferredOrder);
668:
669: copyObj.setOption1Id((Integer) null);
670: copyObj.setOption2Id((Integer) null);
671:
672: return copyObj;
673: }
674:
675: /**
676: * returns a peer instance associated with this om. Since Peer classes
677: * are not to have any instance attributes, this method returns the
678: * same instance for all member of this class. The method could therefore
679: * be static, but this would prevent one from overriding the behavior.
680: */
681: public ROptionOptionPeer getPeer() {
682: return peer;
683: }
684:
685: public String toString() {
686: StringBuffer str = new StringBuffer();
687: str.append("ROptionOption:\n");
688: str.append("Option1Id = ").append(getOption1Id()).append("\n");
689: str.append("Option2Id = ").append(getOption2Id()).append("\n");
690: str.append("RelationshipId = ").append(getRelationshipId())
691: .append("\n");
692: str.append("Weight = ").append(getWeight()).append("\n");
693: str.append("PreferredOrder = ").append(getPreferredOrder())
694: .append("\n");
695: return (str.toString());
696: }
697: }
|