001: package org.tigris.scarab.om;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2005 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: import java.sql.Connection;
050: import java.util.List;
051: import java.util.HashMap;
052:
053: import org.apache.torque.TorqueException;
054: import org.apache.torque.om.Persistent;
055: import org.tigris.scarab.notification.ActivityType;
056: import org.tigris.scarab.util.ScarabConstants;
057:
058: /**
059: * This class manages Activity objects.
060: *
061: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
062: * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
063: * @version $Id: ActivityManager.java 10020 2006-03-16 16:19:13Z jorgeuriarte $
064: */
065: public class ActivityManager extends BaseActivityManager {
066: /**
067: * Creates a new <code>ActivityManager</code> instance.
068: *
069: * @exception TorqueException if an error occurs
070: */
071: public ActivityManager() throws TorqueException {
072: super ();
073: validFields = new HashMap();
074: validFields.put(ActivityPeer.ISSUE_ID, null);
075: }
076:
077: protected Persistent putInstanceImpl(Persistent om)
078: throws TorqueException {
079: Persistent oldOm = super .putInstanceImpl(om);
080: List listeners = (List) listenersMap.get(ActivityPeer.ISSUE_ID);
081: notifyListeners(listeners, oldOm, om);
082: return oldOm;
083: }
084:
085: /**
086: * Convenience method for getting an Activity instance
087: * with a String primary key (which gets converted to a Integer).
088: */
089: public static Activity getInstance(String id)
090: throws TorqueException {
091: return getInstance(new Long(id));
092: }
093:
094: public static Activity createNumericActivity(Issue issue,
095: Attribute attribute, ActivitySet activitySet,
096: Attachment attachment, Integer oldNumericValue,
097: Integer newNumericValue) throws TorqueException {
098: return create(issue, attribute, activitySet,
099: ActivityType.ATTRIBUTE_CHANGED, null, attachment,
100: oldNumericValue, newNumericValue, null, null, null,
101: null, null, null);
102: }
103:
104: public static Activity createUserActivity(Issue issue,
105: Attribute attribute, ActivitySet activitySet,
106: Attachment attachment, Integer oldUserId, Integer newUserId)
107: throws TorqueException {
108: String oldUsername = null;
109: String newUsername = null;
110: if (oldUserId != null) {
111: oldUsername = ScarabUserManager.getInstance(oldUserId)
112: .getUserName();
113: }
114: if (newUserId != null) {
115: newUsername = ScarabUserManager.getInstance(newUserId)
116: .getUserName();
117: }
118: return create(issue, attribute, activitySet,
119: ActivityType.USER_ATTRIBUTE_CHANGED, null, attachment,
120: null, null, oldUserId, newUserId, null, null,
121: oldUsername, newUsername);
122: }
123:
124: public static Activity createAddDependencyActivity(Issue issue,
125: ActivitySet activitySet, Depend depend)
126: throws TorqueException {
127: return create(issue, null, activitySet,
128: ActivityType.DEPENDENCY_CREATED, null, null, depend,
129: null, null, null, null, null, null, null, depend
130: .getDependType().getName(), null);
131: }
132:
133: public static Activity createChangeDependencyActivity(Issue issue,
134: ActivitySet activitySet, Depend depend,
135: String oldTextValue, String newTextValue)
136: throws TorqueException {
137: return create(issue, null, activitySet,
138: ActivityType.DEPENDENCY_CHANGED, null, null, depend,
139: null, null, null, null, null, null, oldTextValue,
140: newTextValue, null);
141: }
142:
143: public static Activity createDeleteDependencyActivity(Issue issue,
144: ActivitySet activitySet, Depend depend)
145: throws TorqueException {
146: return create(issue, null, activitySet,
147: ActivityType.DEPENDENCY_DELETED, null, null, depend,
148: null, null, null, null, null, null, depend
149: .getDependType().getName(), null, null);
150: }
151:
152: public static Activity createTextActivity(Issue issue,
153: ActivitySet activitySet, ActivityType type,
154: String newTextValue) throws TorqueException {
155: return create(issue, null, activitySet, type, null, null, null,
156: null, null, null, null, null, null, newTextValue);
157: }
158:
159: public static Activity createTextActivity(Issue issue,
160: ActivitySet activitySet, ActivityType type,
161: Attachment attachment) throws TorqueException {
162: return create(issue, null, activitySet, type, null, attachment,
163: null, null, null, null, null, null, null, null);
164: }
165:
166: public static Activity createTextActivity(Issue issue,
167: Attribute attribute, ActivitySet activitySet,
168: ActivityType type, String oldTextValue, String newTextValue)
169: throws TorqueException {
170: return create(issue, attribute, activitySet, type, null, null,
171: null, null, null, null, null, null, oldTextValue,
172: newTextValue);
173: }
174:
175: public static Activity createTextActivity(Issue issue,
176: ActivitySet activitySet, ActivityType type,
177: Attachment attachment, String oldTextValue,
178: String newTextValue) throws TorqueException {
179: return create(issue, null, activitySet, type, null, attachment,
180: null, null, null, null, null, null, oldTextValue,
181: newTextValue);
182: }
183:
184: public static Activity createTextActivity(Issue issue,
185: Attribute attribute, ActivitySet activitySet,
186: ActivityType type, String description,
187: Attachment attachment, String oldTextValue,
188: String newTextValue) throws TorqueException {
189: return create(issue, attribute, activitySet, type, description,
190: attachment, null, null, null, null, null, null,
191: oldTextValue, newTextValue);
192: }
193:
194: /**
195: * Populates a new Activity object for initial issue creation.
196: */
197: public static Activity createReportIssueActivity(Issue issue,
198: ActivitySet activitySet, String message)
199: throws TorqueException {
200: return create(issue, AttributeManager
201: .getInstance(ScarabConstants.INTEGER_0), activitySet,
202: ActivityType.ISSUE_CREATED, null, null, null, null,
203: null, null, null, null, null, null);
204: }
205:
206: /**
207: * Populates a new Activity for issue deletion
208: *
209: * @param issue
210: * @param activitySet
211: * @return
212: * @throws TorqueException
213: */
214: public static Activity createDeleteIssueActivity(Issue issue,
215: ActivitySet activitySet) throws TorqueException {
216: return create(issue, AttributeManager
217: .getInstance(ScarabConstants.INTEGER_0), activitySet,
218: ActivityType.ISSUE_DELETED, null, null, null, null,
219: null, null, null, null, null, null);
220: }
221:
222: /**
223: * Populates a new Activity object.
224: */
225: public static Activity create(Issue issue, Attribute attribute,
226: ActivitySet activitySet, ActivityType type,
227: String description, Attachment attachment,
228: Integer oldNumericValue, Integer newNumericValue,
229: Integer oldUserId, Integer newUserId, Integer oldOptionId,
230: Integer newOptionId, String oldTextValue,
231: String newTextValue) throws TorqueException {
232: return create(issue, attribute, activitySet, type, description,
233: attachment, null, oldNumericValue, newNumericValue,
234: oldUserId, newUserId, oldOptionId, newOptionId,
235: oldTextValue, newTextValue, null);
236: }
237:
238: /**
239: * Populates a new Activity object.
240: */
241: public static Activity create(Issue issue, Attribute attribute,
242: ActivitySet activitySet, ActivityType type,
243: String description, Attachment attachment,
244: Integer oldNumericValue, Integer newNumericValue,
245: Integer oldUserId, Integer newUserId, Integer oldOptionId,
246: Integer newOptionId, String oldTextValue,
247: String newTextValue, Connection dbCon)
248: throws TorqueException {
249: return create(issue, attribute, activitySet, type, description,
250: attachment, null, oldNumericValue, newNumericValue,
251: oldUserId, newUserId, oldOptionId, newOptionId,
252: oldTextValue, newTextValue, dbCon);
253: }
254:
255: /**
256: * Populates a new Activity object.
257: */
258: public static Activity create(Issue issue, Attribute attribute,
259: ActivitySet activitySet, ActivityType type,
260: String description, Attachment attachment, Depend depend,
261: Integer oldNumericValue, Integer newNumericValue,
262: Integer oldUserId, Integer newUserId, Integer oldOptionId,
263: Integer newOptionId, String oldTextValue,
264: String newTextValue, Connection dbCon)
265: throws TorqueException {
266: Activity activity = ActivityManager.getInstance();
267: activity.setIssue(issue);
268: if (attribute == null) {
269: attribute = Attribute.getInstance(0);
270: }
271: activity.setAttribute(attribute);
272: activity.setActivitySet(activitySet);
273: activity.setOldNumericValue(oldNumericValue);
274: activity.setNewNumericValue(newNumericValue);
275: activity.setOldUserId(oldUserId);
276: activity.setNewUserId(newUserId);
277: activity.setOldOptionId(oldOptionId);
278: activity.setNewOptionId(newOptionId);
279: activity.setOldValue(oldTextValue);
280: activity.setNewValue(newTextValue);
281: activity.setDepend(depend);
282: activity.setDescription(description);
283: activity.setActivityType(type != null ? type.getCode() : null);
284: if (attachment != null) {
285: activity.setAttachment(attachment);
286: }
287: if (dbCon == null) {
288: try {
289: activity.save();
290: } catch (Exception e) {
291: if (e instanceof TorqueException) {
292: throw (TorqueException) e; //EXCEPTION
293: } else {
294: throw new TorqueException(e); //EXCEPTION
295: }
296: }
297: } else {
298: activity.save(dbCon);
299: }
300: // Make sure new activity is added to activity cache
301: try {
302: issue.addActivity(activity);
303: } catch (Exception e) {
304: throw new TorqueException(e); //EXCEPTION
305: }
306: return activity;
307: }
308: }
|