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 RAttributeAttributeGroup
027: */
028: public abstract class BaseRAttributeAttributeGroup extends BaseObject
029: implements org.apache.fulcrum.intake.Retrievable {
030: /** The Peer class */
031: private static final RAttributeAttributeGroupPeer peer = new RAttributeAttributeGroupPeer();
032:
033: /** The value for the groupId field */
034: private Integer groupId;
035:
036: /** The value for the attributeId field */
037: private Integer attributeId;
038:
039: /** The value for the order field */
040: private int order = -1;
041:
042: /**
043: * Get the GroupId
044: *
045: * @return Integer
046: */
047: public Integer getGroupId() {
048: return groupId;
049: }
050:
051: /**
052: * Set the value of GroupId
053: *
054: * @param v new value
055: */
056: public void setGroupId(Integer v) throws TorqueException {
057:
058: if (!ObjectUtils.equals(this .groupId, v)) {
059: this .groupId = v;
060: setModified(true);
061: }
062:
063: if (aAttributeGroup != null
064: && !ObjectUtils.equals(aAttributeGroup
065: .getAttributeGroupId(), v)) {
066: aAttributeGroup = null;
067: }
068:
069: }
070:
071: /**
072: * Get the AttributeId
073: *
074: * @return Integer
075: */
076: public Integer getAttributeId() {
077: return attributeId;
078: }
079:
080: /**
081: * Set the value of AttributeId
082: *
083: * @param v new value
084: */
085: public void setAttributeId(Integer v) throws TorqueException {
086:
087: if (!ObjectUtils.equals(this .attributeId, v)) {
088: this .attributeId = v;
089: setModified(true);
090: }
091:
092: if (aAttribute != null
093: && !ObjectUtils.equals(aAttribute.getAttributeId(), v)) {
094: aAttribute = null;
095: }
096:
097: }
098:
099: /**
100: * Get the Order
101: *
102: * @return int
103: */
104: public int getOrder() {
105: return order;
106: }
107:
108: /**
109: * Set the value of Order
110: *
111: * @param v new value
112: */
113: public void setOrder(int v) {
114:
115: if (this .order != v) {
116: this .order = v;
117: setModified(true);
118: }
119:
120: }
121:
122: private Attribute aAttribute;
123:
124: /**
125: * Declares an association between this object and a Attribute object
126: *
127: * @param v Attribute
128: * @throws TorqueException
129: */
130: public void setAttribute(Attribute v) throws TorqueException {
131: if (v == null) {
132: setAttributeId((Integer) null);
133: } else {
134: setAttributeId(v.getAttributeId());
135: }
136: aAttribute = v;
137: }
138:
139: /**
140: * Returns the associated Attribute object.
141: * If it was not retrieved before, the object is retrieved from
142: * the database
143: *
144: * @return the associated Attribute object
145: * @throws TorqueException
146: */
147: public Attribute getAttribute() throws TorqueException {
148: if (aAttribute == null
149: && (!ObjectUtils.equals(this .attributeId, null))) {
150: aAttribute = AttributeManager.getInstance(SimpleKey
151: .keyFor(this .attributeId));
152: }
153: return aAttribute;
154: }
155:
156: /**
157: * Return the associated Attribute object
158: * If it was not retrieved before, the object is retrieved from
159: * the database using the passed connection
160: *
161: * @param connection the connection used to retrieve the associated object
162: * from the database, if it was not retrieved before
163: * @return the associated Attribute object
164: * @throws TorqueException
165: */
166: public Attribute getAttribute(Connection connection)
167: throws TorqueException {
168: if (aAttribute == null
169: && (!ObjectUtils.equals(this .attributeId, null))) {
170: aAttribute = AttributeManager.getCachedInstance(SimpleKey
171: .keyFor(this .attributeId));
172: if (aAttribute == null) {
173: aAttribute = AttributePeer.retrieveByPK(SimpleKey
174: .keyFor(this .attributeId), connection);
175: AttributeManager.putInstance(aAttribute);
176: }
177: }
178: return aAttribute;
179: }
180:
181: /**
182: * Provides convenient way to set a relationship based on a
183: * ObjectKey, for example
184: * <code>bar.setFooKey(foo.getPrimaryKey())</code>
185: *
186: */
187: public void setAttributeKey(ObjectKey key) throws TorqueException {
188:
189: setAttributeId(new Integer(((NumberKey) key).intValue()));
190: }
191:
192: private AttributeGroup aAttributeGroup;
193:
194: /**
195: * Declares an association between this object and a AttributeGroup object
196: *
197: * @param v AttributeGroup
198: * @throws TorqueException
199: */
200: public void setAttributeGroup(AttributeGroup v)
201: throws TorqueException {
202: if (v == null) {
203: setGroupId((Integer) null);
204: } else {
205: setGroupId(v.getAttributeGroupId());
206: }
207: aAttributeGroup = v;
208: }
209:
210: /**
211: * Returns the associated AttributeGroup object.
212: * If it was not retrieved before, the object is retrieved from
213: * the database
214: *
215: * @return the associated AttributeGroup object
216: * @throws TorqueException
217: */
218: public AttributeGroup getAttributeGroup() throws TorqueException {
219: if (aAttributeGroup == null
220: && (!ObjectUtils.equals(this .groupId, null))) {
221: aAttributeGroup = AttributeGroupManager
222: .getInstance(SimpleKey.keyFor(this .groupId));
223: }
224: return aAttributeGroup;
225: }
226:
227: /**
228: * Return the associated AttributeGroup object
229: * If it was not retrieved before, the object is retrieved from
230: * the database using the passed connection
231: *
232: * @param connection the connection used to retrieve the associated object
233: * from the database, if it was not retrieved before
234: * @return the associated AttributeGroup object
235: * @throws TorqueException
236: */
237: public AttributeGroup getAttributeGroup(Connection connection)
238: throws TorqueException {
239: if (aAttributeGroup == null
240: && (!ObjectUtils.equals(this .groupId, null))) {
241: aAttributeGroup = AttributeGroupManager
242: .getCachedInstance(SimpleKey.keyFor(this .groupId));
243: if (aAttributeGroup == null) {
244: aAttributeGroup = AttributeGroupPeer.retrieveByPK(
245: SimpleKey.keyFor(this .groupId), connection);
246: AttributeGroupManager.putInstance(aAttributeGroup);
247: }
248: }
249: return aAttributeGroup;
250: }
251:
252: /**
253: * Provides convenient way to set a relationship based on a
254: * ObjectKey, for example
255: * <code>bar.setFooKey(foo.getPrimaryKey())</code>
256: *
257: */
258: public void setAttributeGroupKey(ObjectKey key)
259: throws TorqueException {
260:
261: setGroupId(new Integer(((NumberKey) key).intValue()));
262: }
263:
264: private static List fieldNames = null;
265:
266: /**
267: * Generate a list of field names.
268: *
269: * @return a list of field names
270: */
271: public static synchronized List getFieldNames() {
272: if (fieldNames == null) {
273: fieldNames = new ArrayList();
274: fieldNames.add("GroupId");
275: fieldNames.add("AttributeId");
276: fieldNames.add("Order");
277: fieldNames = Collections.unmodifiableList(fieldNames);
278: }
279: return fieldNames;
280: }
281:
282: /**
283: * Retrieves a field from the object by name passed in as a String.
284: *
285: * @param name field name
286: * @return value
287: */
288: public Object getByName(String name) {
289: if (name.equals("GroupId")) {
290: return getGroupId();
291: }
292: if (name.equals("AttributeId")) {
293: return getAttributeId();
294: }
295: if (name.equals("Order")) {
296: return new Integer(getOrder());
297: }
298: return null;
299: }
300:
301: /**
302: * Retrieves a field from the object by name passed in
303: * as a String. The String must be one of the static
304: * Strings defined in this Class' Peer.
305: *
306: * @param name peer name
307: * @return value
308: */
309: public Object getByPeerName(String name) {
310: if (name.equals(RAttributeAttributeGroupPeer.GROUP_ID)) {
311: return getGroupId();
312: }
313: if (name.equals(RAttributeAttributeGroupPeer.ATTRIBUTE_ID)) {
314: return getAttributeId();
315: }
316: if (name.equals(RAttributeAttributeGroupPeer.PREFERRED_ORDER)) {
317: return new Integer(getOrder());
318: }
319: return null;
320: }
321:
322: /**
323: * Retrieves a field from the object by Position as specified
324: * in the xml schema. Zero-based.
325: *
326: * @param pos position in xml schema
327: * @return value
328: */
329: public Object getByPosition(int pos) {
330: if (pos == 0) {
331: return getGroupId();
332: }
333: if (pos == 1) {
334: return getAttributeId();
335: }
336: if (pos == 2) {
337: return new Integer(getOrder());
338: }
339: return null;
340: }
341:
342: /**
343: * Stores the object in the database. If the object is new,
344: * it inserts it; otherwise an update is performed.
345: *
346: * @throws TorqueException
347: */
348: public void save() throws TorqueException {
349: save(RAttributeAttributeGroupPeer.getMapBuilder()
350: .getDatabaseMap().getName());
351: }
352:
353: /**
354: * Stores the object in the database. If the object is new,
355: * it inserts it; otherwise an update is performed.
356: * Note: this code is here because the method body is
357: * auto-generated conditionally and therefore needs to be
358: * in this file instead of in the super class, BaseObject.
359: *
360: * @param dbName
361: * @throws TorqueException
362: */
363: public void save(String dbName) throws TorqueException {
364: Connection con = null;
365: try {
366: con = Transaction.begin(dbName);
367: save(con);
368: Transaction.commit(con);
369: } catch (TorqueException e) {
370: Transaction.safeRollback(con);
371: throw e;
372: }
373: }
374:
375: /** flag to prevent endless save loop, if this object is referenced
376: by another object which falls in this transaction. */
377: private boolean alreadyInSave = false;
378:
379: /**
380: * Stores the object in the database. If the object is new,
381: * it inserts it; otherwise an update is performed. This method
382: * is meant to be used as part of a transaction, otherwise use
383: * the save() method and the connection details will be handled
384: * internally
385: *
386: * @param con
387: * @throws TorqueException
388: */
389: public void save(Connection con) throws TorqueException {
390: if (!alreadyInSave) {
391: alreadyInSave = true;
392:
393: // If this object has been modified, then save it to the database.
394: if (isModified()) {
395: if (isNew()) {
396: RAttributeAttributeGroupPeer.doInsert(
397: (RAttributeAttributeGroup) this , con);
398: setNew(false);
399: } else {
400: RAttributeAttributeGroupPeer.doUpdate(
401: (RAttributeAttributeGroup) this , con);
402: }
403:
404: if (isCacheOnSave()) {
405: RAttributeAttributeGroupManager.putInstance(this );
406: }
407: }
408:
409: alreadyInSave = false;
410: }
411: }
412:
413: /**
414: * Specify whether to cache the object after saving to the db.
415: * This method returns true
416: */
417: protected boolean isCacheOnSave() {
418: return true;
419: }
420:
421: private final SimpleKey[] pks = new SimpleKey[2];
422: private final ComboKey comboPK = new ComboKey(pks);
423:
424: /**
425: * Set the PrimaryKey with an ObjectKey
426: *
427: * @param key
428: */
429: public void setPrimaryKey(ObjectKey key) throws TorqueException {
430: SimpleKey[] keys = (SimpleKey[]) key.getValue();
431: SimpleKey tmpKey = null;
432: setGroupId(new Integer(((NumberKey) keys[0]).intValue()));
433: setAttributeId(new Integer(((NumberKey) keys[1]).intValue()));
434: }
435:
436: /**
437: * Set the PrimaryKey using SimpleKeys.
438: *
439: * @param groupId Integer
440: * @param attributeId Integer
441: */
442: public void setPrimaryKey(Integer groupId, Integer attributeId)
443: throws TorqueException {
444: setGroupId(groupId);
445: setAttributeId(attributeId);
446: }
447:
448: /**
449: * Set the PrimaryKey using a String.
450: */
451: public void setPrimaryKey(String key) throws TorqueException {
452: setPrimaryKey(new ComboKey(key));
453: }
454:
455: /**
456: * returns an id that differentiates this object from others
457: * of its class.
458: */
459: public ObjectKey getPrimaryKey() {
460: pks[0] = SimpleKey.keyFor(getGroupId());
461: pks[1] = SimpleKey.keyFor(getAttributeId());
462: return comboPK;
463: }
464:
465: /**
466: * get an id that differentiates this object from others
467: * of its class.
468: */
469: public String getQueryKey() {
470: if (getPrimaryKey() == null) {
471: return "";
472: } else {
473: return getPrimaryKey().toString();
474: }
475: }
476:
477: /**
478: * set an id that differentiates this object from others
479: * of its class.
480: */
481: public void setQueryKey(String key) throws TorqueException {
482: setPrimaryKey(key);
483: }
484:
485: /**
486: * Makes a copy of this object.
487: * It creates a new object filling in the simple attributes.
488: * It then fills all the association collections and sets the
489: * related objects to isNew=true.
490: */
491: public RAttributeAttributeGroup copy() throws TorqueException {
492: return copyInto(new RAttributeAttributeGroup());
493: }
494:
495: protected RAttributeAttributeGroup copyInto(
496: RAttributeAttributeGroup copyObj) throws TorqueException {
497: copyObj.setGroupId(groupId);
498: copyObj.setAttributeId(attributeId);
499: copyObj.setOrder(order);
500:
501: copyObj.setGroupId((Integer) null);
502: copyObj.setAttributeId((Integer) null);
503:
504: return copyObj;
505: }
506:
507: /**
508: * returns a peer instance associated with this om. Since Peer classes
509: * are not to have any instance attributes, this method returns the
510: * same instance for all member of this class. The method could therefore
511: * be static, but this would prevent one from overriding the behavior.
512: */
513: public RAttributeAttributeGroupPeer getPeer() {
514: return peer;
515: }
516:
517: public String toString() {
518: StringBuffer str = new StringBuffer();
519: str.append("RAttributeAttributeGroup:\n");
520: str.append("GroupId = ").append(getGroupId()).append("\n");
521: str.append("AttributeId = ").append(getAttributeId()).append(
522: "\n");
523: str.append("Order = ").append(getOrder()).append("\n");
524: return (str.toString());
525: }
526: }
|