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 CollabNet <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 CollabNet.
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 CollabNet.
047: */
048:
049: import java.text.SimpleDateFormat;
050: import java.util.Date;
051:
052: import org.apache.torque.TorqueException;
053: import org.apache.torque.om.Persistent;
054: import org.apache.torque.util.Criteria;
055: import org.tigris.scarab.notification.ActivityType;
056: import org.tigris.scarab.tools.ScarabLocalizationTool;
057: import org.tigris.scarab.tools.localization.L10NKeySet;
058: import org.tigris.scarab.util.ScarabException;
059:
060: /**
061: * This class holds the information for every notification generated
062: * in the system (stored in NOTIFICATION_STATUS table)
063: *
064: */
065: public class NotificationStatus extends BaseNotificationStatus
066: implements Persistent, Comparable {
067:
068: static public final Integer WAIT = new Integer(1);
069: static public final Integer SCHEDULED = new Integer(2);
070: static public final Integer DEFERRED = new Integer(3);
071: static public final Integer FAIL = new Integer(4);
072: static public final Integer SENT = new Integer(5);
073: static public final Integer MARK_DELETED = new Integer(6);
074:
075: static private final Integer ARCHIVER_ID = new Integer(-1);
076:
077: private ActivityType activityType;
078: private Long issueId;
079:
080: public NotificationStatus() throws TorqueException {
081: this .setCreationDate(new Date());
082: this .setStatus(SCHEDULED);
083: }
084:
085: /**
086: * Create a new NotificationStatus entry.
087: * @param receiver
088: * @param activity
089: * @throws TorqueException
090: */
091: public NotificationStatus(ScarabUser receiver, Activity activity)
092: throws ScarabException {
093:
094: Attachment att = null;
095:
096: try {
097: this .setActivity(activity);
098: att = activity.getActivitySet().getAttachment();
099: if (att != null)
100: this .setComment(att.getData());
101: Integer receiverId = receiver.getUserId();
102: if (receiverId == null) {
103: receiverId = ARCHIVER_ID;
104: }
105: this .setReceiverId(receiverId);
106: } catch (TorqueException te) {
107: throw new ScarabException(
108: L10NKeySet.ExceptionTorqueGeneric, te);
109: }
110: this .setCreationDate(new Date());
111: this .setStatus(SCHEDULED);
112: }
113:
114: public Long getIssueId() {
115: return this .issueId;
116: }
117:
118: public ActivityType getActivityType() {
119: return this .activityType;
120: }
121:
122: /**
123: * NOT ONLY sets the activityId; it also updates the depending values.
124: */
125: public void setActivityId(Long id) {
126: try {
127: super .setActivityId(id);
128: this .issueId = this .getActivity().getIssue().getIssueId();
129: this .setCreationDate(this .getActivity().getActivitySet()
130: .getCreatedDate());
131: this .setCreatorId(this .getActivity().getActivitySet()
132: .getCreator().getUserId());
133: String activityTypeString = this .getActivity()
134: .getActivityType();
135: this .activityType = ActivityType
136: .getActivityType(activityTypeString);
137: } catch (TorqueException te) {
138: getLog().error(
139: "setActivity(): Cannot find activity's issue!: "
140: + te);
141: }
142: }
143:
144: /**
145: * Transform the database representation of the status (INTEGER)
146: * into a readable label (String)
147: *
148: * TODO: Should this return a localized string (receiving parameter l10nTool?)
149: *
150: * @return
151: */
152: public String getStatusLabel() {
153: Integer status = getStatus();
154: if (status.equals(WAIT))
155: return "wait";
156: if (status.equals(SCHEDULED))
157: return "scheduled";
158: if (status.equals(DEFERRED))
159: return "deferred";
160: if (status.equals(FAIL))
161: return "fail";
162: if (status.equals(SENT))
163: return "delivered";
164: if (status.equals(MARK_DELETED))
165: return "deleted";
166: throw new RuntimeException("Database inconsistency: status ["
167: + status + "] is not known.");
168: }
169:
170: /**
171: * Formats the creationDate of the notification's activity
172: * @param l10nTool
173: * @return
174: */
175: public String getActivityCreationDate(
176: ScarabLocalizationTool l10nTool) {
177: SimpleDateFormat sdf = new SimpleDateFormat(
178: L10NKeySet.ShortDateTimeDisplay.getMessage(l10nTool));
179: return sdf.format(this .getCreationDate());
180: }
181:
182: /**
183: * Get the creator of this entry
184: * @return
185: * @throws TorqueException
186: */
187: public ScarabUser getCreator() throws TorqueException {
188: Integer id = this .getCreatorId();
189: ScarabUser user = ScarabUserManager.getInstance(id);
190: return user;
191: }
192:
193: /**
194: * get the Receiver for this entry
195: * @return
196: * @throws TorqueException
197: */
198: public ScarabUser getReceiver() throws TorqueException {
199: Integer id = this .getReceiverId();
200: ScarabUser user = ScarabUserManager.getInstance(id);
201: return user;
202: }
203:
204: public void delete() throws TorqueException {
205: Criteria crit = new Criteria();
206: crit.add(NotificationStatusPeer.ACTIVITY_ID, this
207: .getActivityId());
208: crit
209: .add(NotificationStatusPeer.CREATOR_ID, this
210: .getCreatorId());
211: crit.add(NotificationStatusPeer.RECEIVER_ID, this
212: .getReceiverId());
213: NotificationStatusPeer.doDelete(crit);
214: }
215:
216: public void markDeleted() throws TorqueException {
217: this .setStatus(MARK_DELETED);
218: this .setChangeDate(new Date());
219: this .save();
220: }
221:
222: /**
223: * Compare two Notification objects, following this criteria:
224: * <ul>
225: * <li>IssueId</li>
226: * <li>User</li>
227: * <li>Activity type</li>
228: * <li>Timestamp</li>
229: * </ul>
230: * @author jorgeuriarte
231: *
232: */
233: public int compareTo(Object o2) {
234: int rdo = 0;
235: NotificationStatus not1 = this ;
236: NotificationStatus not2 = (NotificationStatus) o2;
237: if (null != not1 && null != not2) {
238: Long id1 = not1.getIssueId();
239: Long id2 = not2.getIssueId();
240: if (null == id1 && null != id2)
241: return -1;
242: if (null != id1 && null == id2)
243: return 1;
244:
245: Integer user1 = not1.getCreatorId();
246: Integer user2 = not2.getCreatorId();
247: rdo = user1.compareTo(user2);
248: if (0 == rdo) {
249: rdo = not1.getActivityType().getCode().compareTo(
250: not2.getActivityType().getCode());
251: if (0 == rdo) {
252: rdo = not1.getCreationDate().compareTo(
253: not2.getCreationDate());
254: }
255: }
256:
257: }
258: return rdo;
259: }
260:
261: }
|