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