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.util.List;
050:
051: // Turbine classes
052: import org.apache.torque.TorqueException;
053: import org.apache.torque.om.Persistent;
054: import org.apache.torque.util.Criteria;
055: import java.sql.Connection;
056:
057: import org.tigris.scarab.attribute.DateAttribute;
058: import org.tigris.scarab.notification.ActivityType;
059: import org.tigris.scarab.om.Attachment;
060: import org.tigris.scarab.services.cache.ScarabCache;
061: import org.tigris.scarab.tools.ScarabLocalizationTool;
062: import org.tigris.scarab.tools.localization.L10NKeySet;
063: import org.tigris.scarab.tools.localization.L10NMessage;
064: import org.tigris.scarab.tools.localization.LocalizationKey;
065:
066: /**
067: * This class represents Activity records.
068: *
069: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
070: * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
071: * @version $Id: Activity.java 10331 2006-11-07 10:17:38Z ronvoe122 $
072: */
073: public class Activity extends BaseActivity implements Persistent {
074: private AttributeOption oldAttributeOption;
075: private AttributeOption newAttributeOption;
076: private static final Integer COPIED = new Integer(1);
077: private static final Integer MOVED = new Integer(2);
078:
079: protected static final String GET_ATTACHMENT = "getAttachment";
080:
081: /**
082: * This method properly handles the case where there may not be
083: * any rows selected and returns null in that case.
084: */
085: public Attachment getAttachment() {
086: Attachment attachment = null;
087: Object obj = ScarabCache.get(this , GET_ATTACHMENT);
088: if (obj == null) {
089: try {
090: Criteria crit = new Criteria();
091: crit
092: .add(ActivityPeer.ACTIVITY_ID, this
093: .getActivityId());
094: crit.addJoin(AttachmentPeer.ATTACHMENT_ID,
095: ActivityPeer.ATTACHMENT_ID);
096: List results = AttachmentPeer.doSelect(crit);
097: if (!results.isEmpty()) {
098: attachment = (Attachment) results.get(0);
099: ScarabCache.put(attachment, this , GET_ATTACHMENT);
100: }
101: } catch (Exception e) {
102: getLog().error("Activity.getAttachment(): ", e);
103: }
104: } else {
105: attachment = (Attachment) obj;
106: }
107: return attachment;
108: }
109:
110: /**
111: * Gets the AttributeOption object associated with the Old Value field
112: * (i.e., the old value for the attribute before the change.)
113: */
114: public AttributeOption getOldAttributeOption()
115: throws TorqueException {
116: if (oldAttributeOption == null && (getOldValue() != null)) {
117: oldAttributeOption = AttributeOptionManager
118: .getInstance(new Integer(getOldValue()));
119: }
120: return oldAttributeOption;
121: }
122:
123: /**
124: * Gets the AttributeOption object associated with the New Value field
125: * (i.e., the new value for the attribute after the change.)
126: */
127: public AttributeOption getNewAttributeOption()
128: throws TorqueException {
129: if (newAttributeOption == null && (getNewValue() != null)) {
130: newAttributeOption = AttributeOptionManager
131: .getInstance(new Integer(getNewValue()));
132: }
133: return newAttributeOption;
134: }
135:
136: public void save(Connection dbCon) throws TorqueException {
137: if (isNew()) {
138: Criteria crit = new Criteria();
139: // If there are previous activities on this attribute and value
140: // Set End Date
141: if (this .getOldValue() != null) {
142: crit.add(ActivityPeer.ISSUE_ID, getIssueId());
143: crit.add(ActivityPeer.ATTRIBUTE_ID, getAttributeId());
144: crit.add(ActivityPeer.END_DATE, null);
145: crit.add(ActivityPeer.NEW_VALUE, this .getOldValue());
146: List result = ActivityPeer.doSelect(crit);
147: int resultSize = result.size();
148: if (resultSize > 0) {
149: for (int i = 0; i < resultSize; i++) {
150: Activity a = (Activity) result.get(i);
151: a.setEndDate(getActivitySet().getCreatedDate());
152: a.save(dbCon);
153: }
154: }
155: }
156: }
157: // If they have just deleted a user assignment, set end date
158: if (getAttribute().isUserAttribute()
159: && this .getNewUserId() == null
160: && this .getOldUserId() != null) {
161: this .setEndDate(getActivitySet().getCreatedDate());
162: }
163:
164: super .save(dbCon);
165: }
166:
167: /**
168: * @deprecated Use getDescription(ScarabLocalizationTool l10nTool) instead
169: */
170: public String getDescription() {
171: return super .getDescription();
172: }
173:
174: public String getDescription(ScarabLocalizationTool l10nTool) {
175: String desc = null;
176: ActivityType type = ActivityType.getActivityType(this
177: .getActivityType());
178: // If the activity was stored before the field Type existed,
179: // we fallback to the good old unlocalized description stored in the activity.
180: if (type == null)
181: return super .getDescription();
182:
183: if (ActivityType.URL_CHANGED.equals(type)) {
184: desc = this .getUrlChangedDescription(this .getOldValue(),
185: this .getNewValue(), l10nTool);
186: } else if (ActivityType.URL_ADDED.equals(type)) {
187: desc = this .getUrlAddedDescription(this .getAttachment()
188: .getData(), this .getAttachment().getName(),
189: l10nTool);
190: } else if (ActivityType.URL_DESC_CHANGED.equals(type)) {
191: desc = this .getUrlDescChangedDescription(
192: this .getOldValue(), this .getNewValue(), l10nTool);
193: } else if (ActivityType.URL_DELETED.equals(type)) {
194: desc = this .getUrlDeletedDescription(this .getAttachment()
195: .getData(), this .getAttachment().getName(),
196: l10nTool);
197: } else if (ActivityType.COMMENT_ADDED.equals(type)) {
198: desc = this .getCommentAddedDescription(this .getAttachment()
199: .getData(), l10nTool);
200: } else if (ActivityType.COMMENT_CHANGED.equals(type)) {
201: desc = this .getCommentChangedDescription(l10nTool);
202: } else if (ActivityType.ATTACHMENT_CREATED.equals(type)) {
203: desc = this .getFileSavedDescription(this .getAttachment()
204: .getFileName(), l10nTool);
205: } else if (ActivityType.ISSUE_CREATED.equals(type)) {
206: desc = this .getIssueCreatedDescription(l10nTool);
207: } else if (ActivityType.ISSUE_MOVED.equals(type)) {
208: desc = this .getIssueCopiedOrMovedDescription(COPIED, this
209: .getOldValue(), this .getNewValue(), l10nTool);
210: } else if (ActivityType.ISSUE_COPIED.equals(type)) {
211: desc = this .getIssueCopiedOrMovedDescription(MOVED, this
212: .getOldValue(), this .getNewValue(), l10nTool);
213: } else if (ActivityType.ATTACHMENT_REMOVED.equals(type)) {
214: desc = new L10NMessage(L10NKeySet.AttachmentDeletedDesc,
215: this .getAttachment().getFileName())
216: .getMessage(l10nTool);
217: } else if (ActivityType.DEPENDENCY_CREATED.equals(type)) {
218: desc = getDependencyAddedDescription(l10nTool);
219: } else if (ActivityType.DEPENDENCY_CHANGED.equals(type)) {
220: desc = getDependencyChangedDescription(l10nTool);
221: } else if (ActivityType.DEPENDENCY_DELETED.equals(type)) {
222: desc = getDependencyDeletedDescription(l10nTool);
223: } else if (ActivityType.ATTRIBUTE_CHANGED.equals(type)) {
224: desc = getAttributeChangedDescription(l10nTool);
225: } else if (ActivityType.USER_ATTRIBUTE_CHANGED.equals(type)) {
226: desc = getUserAttributeChangedDescription(l10nTool);
227: } else if (ActivityType.ISSUE_DELETED.equals(type)) {
228: desc = getIssueDeletedDescription(l10nTool);
229: } else {
230: desc = "----";
231: }
232: return desc;
233: }
234:
235: private String getUrlChangedDescription(String oldUrl,
236: String newUrl, ScarabLocalizationTool l10nTool) {
237: Object[] args = { oldUrl, newUrl };
238: L10NMessage msg = new L10NMessage(L10NKeySet.UrlChangedDesc,
239: args);
240: return msg.getMessage(l10nTool);
241: }
242:
243: private String getUrlAddedDescription(String url, String desc,
244: ScarabLocalizationTool l10nTool) {
245: if (desc != null && desc.length() > 0)
246: url += " (" + desc + ")";
247: L10NMessage msg = new L10NMessage(L10NKeySet.UrlAddedDesc, url);
248: return msg.getMessage(l10nTool);
249: }
250:
251: private String getUrlDeletedDescription(String url, String desc,
252: ScarabLocalizationTool l10nTool) {
253: if (desc != null && desc.length() > 0)
254: url += " (" + desc + ")";
255: L10NMessage msg = new L10NMessage(L10NKeySet.UrlDeletedDesc,
256: url);
257: return msg.getMessage(l10nTool);
258: }
259:
260: private String getCommentAddedDescription(String comment,
261: ScarabLocalizationTool l10nTool) {
262: return L10NKeySet.AddedCommentToIssue.getMessage(l10nTool)
263: + ": '" + comment + "'";
264: }
265:
266: private String getCommentChangedDescription(
267: ScarabLocalizationTool l10nTool) {
268: // Generate description of modification
269: Object[] args = { this .getOldValue(), this .getNewValue() };
270: L10NMessage msg = new L10NMessage(L10NKeySet.ChangedComment,
271: args);
272: return msg.getMessage(l10nTool);
273: }
274:
275: private String getFileSavedDescription(String name,
276: ScarabLocalizationTool l10nTool) {
277: L10NMessage msg = new L10NMessage(L10NKeySet.FileAddedDesc,
278: name);
279: return msg.getMessage(l10nTool);
280: }
281:
282: private String getIssueCreatedDescription(
283: ScarabLocalizationTool l10nTool) {
284: return l10nTool.get(L10NKeySet.IssueCreated);
285: }
286:
287: private String getIssueDeletedDescription(
288: ScarabLocalizationTool l10nTool) {
289: String issueId = null;
290: try {
291: issueId = this .getIssue().getUniqueId();
292: } catch (TorqueException te) {
293: getLog().error("getIssueDeletedDescription(): " + te);
294: }
295: L10NMessage msg = new L10NMessage(L10NKeySet.IssueDeleted,
296: issueId);
297: return msg.getMessage(l10nTool);
298: }
299:
300: /**
301: *
302: * @param actionChoice Value of COPIED or MOVED
303: * @param oldIssue IssueID (with prefix) of the issue in the old location
304: * @param newIssue IssueID (with prefix) of the issue in the new location
305: * @param locale Locale to show the description in.
306: * @return
307: */
308: private String getIssueCopiedOrMovedDescription(
309: Integer actionChoice, String oldIssue, String newIssue,
310: ScarabLocalizationTool l10nTool) {
311: L10NMessage msg = null;
312: try {
313: Issue issue = this .getIssue();
314:
315: Object[] args = { actionChoice, oldIssue,
316: issue.getModule().getName(),
317: issue.getIssueType().getName() };
318: LocalizationKey key = null;
319: if (issue.getUniqueId().equals(oldIssue)) {
320: key = L10NKeySet.MovedToIssueDescription;
321: } else {
322: key = L10NKeySet.MovedFromIssueDescription;
323: }
324: msg = new L10NMessage(key, args);
325: } catch (TorqueException te) {
326: getLog().error("getIssueCopiedOrMovedDescription(): " + te);
327: }
328: return msg.getMessage(l10nTool);
329: }
330:
331: private String getUrlDescChangedDescription(String oldDescription,
332: String newDescription, ScarabLocalizationTool l10nTool) {
333: Object[] args = { oldDescription, newDescription, };
334: L10NMessage msg = new L10NMessage(
335: L10NKeySet.UrlDescChangedDesc, args);
336: return msg.getMessage(l10nTool);
337: }
338:
339: private String getDependencyAddedDescription(
340: ScarabLocalizationTool l10nTool) {
341: String desc = null;
342: try {
343: Object[] args = {
344: this .getDepend().getIssueRelatedByObserverId()
345: .getUniqueId(),
346: this .getDepend().getAction(l10nTool.getLocale()),
347: this .getDepend().getIssueRelatedByObservedId()
348: .getUniqueId() };
349: L10NMessage msg = new L10NMessage(L10NKeySet.AddDependency,
350: args);
351: desc = msg.getMessage(l10nTool);
352: } catch (TorqueException te) {
353: getLog().error("getDependencyAddedDescription(): " + te);
354: }
355: return desc;
356: }
357:
358: private String getDependencyChangedDescription(
359: ScarabLocalizationTool l10nTool) {
360: String oldName = this .getOldValue();
361: String newName = this .getNewValue();
362:
363: String desc = null;
364:
365: try {
366: Object[] args = {
367: this .getDepend().getIssueRelatedByObserverId()
368: .getUniqueId(),
369: this .getDepend().getIssueRelatedByObservedId()
370: .getUniqueId(), oldName, newName };
371: if (!newName.equals(oldName)) {
372: desc = (new L10NMessage(
373: L10NKeySet.DependencyTypeChangedDesc, args))
374: .getMessage(l10nTool);
375: } else {
376: desc = (new L10NMessage(
377: L10NKeySet.DependencyRolesSwitchedDesc, args))
378: .getMessage(l10nTool);
379: }
380: } catch (TorqueException te) {
381: getLog().error("getDependencyChangedDescription(): " + te);
382: }
383:
384: return desc;
385: }
386:
387: private String getDependencyDeletedDescription(
388: ScarabLocalizationTool l10nTool) {
389: String desc = null;
390: try {
391: Object[] args = {
392: this .getDepend().getDependType().getName(),
393: this .getIssue().getUniqueId(),
394: this .getDepend().getIssueRelatedByObservedId()
395: .getUniqueId() };
396: desc = (new L10NMessage(L10NKeySet.DependencyDeletedDesc,
397: args)).getMessage(l10nTool);
398: } catch (TorqueException te) {
399: getLog().error("getDependencyDeletedDescription(): " + te);
400: }
401: return desc;
402: }
403:
404: /**
405: * Gives the name of the attribute in the module,or falls back to the global
406: * name of the attribute if needed.
407: * @return
408: */
409: public String getDisplayName() {
410: String attrName = null;
411: try {
412: RModuleAttribute attr = this .getIssue().getModule()
413: .getRModuleAttribute(this .getAttribute(),
414: this .getIssue().getIssueType());
415: if (attr != null)
416: attrName = attr.getDisplayValue();
417: else
418: attrName = this .getAttribute().getName();
419: } catch (Exception e) {
420: getLog().error("getDisplayName(): " + e);
421: }
422: return attrName;
423: }
424:
425: private String getAttributeChangedDescription(
426: ScarabLocalizationTool l10nTool) {
427: String desc = null;
428: String attrName = this .getDisplayName();
429: String newValue = this .getNewValue();
430: String oldValue = this .getOldValue();
431: try {
432: if (this .getAttribute().isDateAttribute()) {
433: if (null != newValue)
434: newValue = DateAttribute.dateFormat(newValue,
435: L10NKeySet.ShortDatePattern
436: .getMessage(l10nTool));
437: if (null != oldValue)
438: oldValue = DateAttribute.dateFormat(oldValue,
439: L10NKeySet.ShortDatePattern
440: .getMessage(l10nTool));
441: }
442: } catch (Exception e) {
443: getLog().error("getAttributeChangedDescription(): " + e);
444: }
445: if (oldValue == null || oldValue.trim().length() == 0) {
446: if (newValue != null && newValue.length() > 0) {
447: Object[] args = { attrName, newValue };
448: desc = (new L10NMessage(
449: L10NKeySet.AttributeSetToNewValue, args))
450: .getMessage(l10nTool);
451: }
452: } else {
453: if (newValue != null && newValue.length() > 0) {
454: Object[] args = { attrName, oldValue, newValue };
455: desc = (new L10NMessage(
456: L10NKeySet.AttributeChangedFromToNewValue, args))
457: .getMessage(l10nTool);
458: } else {
459: Object[] args = { attrName };
460: desc = (new L10NMessage(
461: L10NKeySet.AttributeHasBeenUndefined, args))
462: .getMessage(l10nTool);
463: }
464: }
465: return desc;
466: }
467:
468: private String getUserAttributeChangedDescription(
469: ScarabLocalizationTool l10nTool) {
470: String desc = null;
471: try {
472: LocalizationKey key = L10NKeySet.UserAttributeSetToNewValue;
473: String value = this .getNewValue();
474: if (value == null) {
475: value = this .getOldValue();
476: key = L10NKeySet.UserAttributeRemovedFrom;
477: }
478: String attrName = this .getIssue().getModule()
479: .getRModuleAttribute(this .getAttribute(),
480: this .getIssue().getIssueType())
481: .getDisplayValue();
482: Object[] args = { attrName, value };
483: desc = (new L10NMessage(key, args)).getMessage(l10nTool);
484: return desc;
485: } catch (Exception e) {
486: getLog()
487: .error("getUserAttributeChangedDescription(): " + e);
488: }
489: return desc;
490: }
491:
492: public Activity copy(Issue issue, ActivitySet activitySet)
493: throws TorqueException {
494: Activity newA = new Activity();
495: newA.setIssueId(issue.getIssueId());
496: newA.setDescription(getDescription());
497: newA.setAttributeId(getAttributeId());
498: newA.setTransactionId(activitySet.getActivitySetId());
499: newA.setOldNumericValue(getOldNumericValue());
500: newA.setNewNumericValue(getNewNumericValue());
501: newA.setOldUserId(getOldUserId());
502: newA.setNewUserId(getNewUserId());
503: newA.setOldValue(getOldValue());
504: newA.setNewValue(getNewValue());
505: newA.setDependId(getDependId());
506: newA.setEndDate(getEndDate());
507: newA.setAttachmentId(getAttachmentId());
508: newA.setActivityType(getActivityType());
509: newA.save();
510: return newA;
511: }
512: }
|