001: /*
002: * $Id: WfAssignmentImpl.java,v 1.2 2003/08/19 17:45:18 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.workflow.impl;
026:
027: import java.sql.Timestamp;
028: import java.util.Date;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.base.util.UtilDateTime;
035: import org.ofbiz.entity.GenericEntityException;
036: import org.ofbiz.entity.GenericValue;
037: import org.ofbiz.workflow.CannotComplete;
038: import org.ofbiz.workflow.InvalidResource;
039: import org.ofbiz.workflow.WfActivity;
040: import org.ofbiz.workflow.WfAssignment;
041: import org.ofbiz.workflow.WfException;
042: import org.ofbiz.workflow.WfResource;
043:
044: /**
045: * WfAssignmentImpl - Workflow Assignment Object implementation
046: *
047: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
048: * @version $Revision: 1.2 $
049: * @since 2.0
050: */
051: public class WfAssignmentImpl implements WfAssignment {
052:
053: public static final String module = WfAssignmentImpl.class
054: .getName();
055:
056: protected WfActivity activity = null;
057: protected WfResource resource = null;
058: protected Timestamp fromDate = null;
059: protected boolean create = false;
060:
061: /**
062: * Creates new WfAssignment.
063: * @param activity Sets the activity object for this assignment.
064: * @param resource The WfResource object this is assigned to.
065: * @throws WfException
066: */
067: public WfAssignmentImpl(WfActivity activity, WfResource resource,
068: Timestamp fromDate, boolean create) throws WfException {
069: this .activity = activity;
070: this .resource = resource;
071: this .fromDate = fromDate;
072: this .create = create;
073: checkAssignment();
074: }
075:
076: // makes the assignment entity
077: private void checkAssignment() throws WfException {
078: String workEffortId = activity.runtimeKey();
079: String partyId = resource.resourcePartyId();
080: String roleTypeId = resource.resourceRoleId();
081:
082: if (workEffortId == null)
083: throw new WfException(
084: "WorkEffort could not be found for assignment");
085: if (partyId == null && roleTypeId == null)
086: throw new WfException(
087: "Both party and role type IDs cannot be null");
088: if (fromDate == null)
089: throw new WfException("From date cannot be null");
090:
091: GenericValue value = null;
092: Map fields = new HashMap();
093:
094: fields.put("workEffortId", workEffortId);
095: fields.put("partyId", partyId);
096: fields.put("roleTypeId", roleTypeId);
097: fields.put("fromDate", fromDate);
098: fields.put("statusId", "CAL_SENT");
099:
100: // check if one exists
101: try {
102: if (valueObject() != null) {
103: Debug
104: .logVerbose(
105: "[WfAssignment.checkAssignment] : found existing assignment.",
106: module);
107: return;
108: }
109: } catch (WfException e) {
110: Debug
111: .logVerbose(
112: "[WfAssignment.checkAssignment] : no existing assignment.",
113: module);
114: }
115:
116: if (create) {
117: // none exist; create a new one
118: try {
119: GenericValue v = activity.getDelegator().makeValue(
120: "WorkEffortPartyAssignment", fields);
121:
122: value = activity.getDelegator().create(v);
123: Debug.logVerbose(
124: "[WfAssignment.checkAssignment] : created new party assignment : "
125: + v, module);
126: } catch (GenericEntityException e) {
127: throw new WfException(e.getMessage(), e);
128: }
129: if (value == null)
130: throw new WfException(
131: "Could not create the assignement");
132: }
133: if (value == null)
134: throw new WfException(
135: "No existing assignment found or create failed");
136: }
137:
138: /**
139: * @see org.ofbiz.workflow.WfAssignment#accept()
140: */
141: public void accept() throws WfException {
142: boolean allDelegated = true;
143: boolean acceptAll = activity.getDefinitionObject().get(
144: "acceptAllAssignments") != null ? activity
145: .getDefinitionObject().getBoolean(
146: "acceptAllAssignments").booleanValue() : false;
147:
148: if (!acceptAll) {
149: // check for existing accepted assignment
150: if (!activity.state()
151: .equals("open.not_running.not_started")) {
152: // activity already running all assignments must be delegated in order to accept
153: Iterator ai = activity.getIteratorAssignment();
154:
155: while (ai.hasNext() && allDelegated) {
156: WfAssignment a = (WfAssignment) ai.next();
157: if (!a.equals(this )
158: && !a.status().equals("CAL_DELEGATED")) {
159: allDelegated = false;
160: }
161: }
162: // we cannot accept if the activity is running, with active assignments
163: if (!allDelegated) {
164: throw new WfException(
165: "Cannot accept; Activity already running with active assignments");
166: }
167: } else {
168: // activity not running, auto change all assignments to delegated status
169: Debug
170: .logVerbose(
171: "[WfAssignment.accept] : setting other assignments to delegated status.",
172: module);
173: Iterator ai = activity.getIteratorAssignment();
174:
175: while (ai.hasNext()) {
176: WfAssignment a = (WfAssignment) ai.next();
177: if (!this .isEqual(a))
178: a.delegate();
179: }
180: }
181: }
182: // set this assignment as accepted
183: changeStatus("CAL_ACCEPTED");
184: }
185:
186: /**
187: * @see org.ofbiz.workflow.WfAssignment#setResult(java.util.Map)
188: */
189: public void setResult(Map results) throws WfException {
190: activity.setResult(results);
191: }
192:
193: /**
194: * @see org.ofbiz.workflow.WfAssignment#complete()
195: */
196: public void complete() throws WfException {
197: changeStatus("CAL_COMPLETED");
198: try {
199: activity.complete();
200: } catch (CannotComplete e) {
201: Debug.logWarning("Activity not complete : "
202: + e.getMessage(), module);
203: }
204: }
205:
206: /**
207: * @see org.ofbiz.workflow.WfAssignment#delegate()
208: */
209: public void delegate() throws WfException {
210: // check and make sure we are not already delegated
211: if (status().equals("CAL_DELEGATED"))
212: throw new WfException(
213: "Assignment has already been delegated");
214:
215: // set the thru-date
216: GenericValue valueObject = valueObject();
217: try {
218: valueObject.set("thruDate", UtilDateTime.nowTimestamp());
219: valueObject.store();
220: if (Debug.verboseOn())
221: Debug
222: .logVerbose(
223: "[WfAssignment.delegated()] : set the thru-date.",
224: module);
225: } catch (GenericEntityException e) {
226: e.printStackTrace();
227: throw new WfException(e.getMessage(), e);
228: }
229:
230: // change the status
231: changeStatus("CAL_DELEGATED");
232: }
233:
234: /**
235: * @see org.ofbiz.workflow.WfAssignment#changeStatus(java.lang.String)
236: */
237: public void changeStatus(String status) throws WfException {
238: // change the status
239: GenericValue valueObject = valueObject();
240: try {
241: valueObject.set("statusId", status);
242: valueObject.store();
243: if (Debug.verboseOn())
244: Debug.logVerbose(
245: "[WfAssignment.changeStatus] : changed status to "
246: + status, module);
247: } catch (GenericEntityException e) {
248: e.printStackTrace();
249: throw new WfException(e.getMessage(), e);
250: }
251: }
252:
253: /**
254: * @see org.ofbiz.workflow.WfAssignment#activity()
255: */
256: public WfActivity activity() throws WfException {
257: return activity;
258: }
259:
260: /**
261: * @see org.ofbiz.workflow.WfAssignment#assignee()
262: */
263: public WfResource assignee() throws WfException {
264: return resource;
265: }
266:
267: /**
268: * @see org.ofbiz.workflow.WfAssignment#setAssignee(org.ofbiz.workflow.WfResource)
269: */
270: public void setAssignee(WfResource newValue) throws WfException,
271: InvalidResource {
272: remove();
273: this .resource = newValue;
274: this .fromDate = new Timestamp(new Date().getTime());
275: checkAssignment();
276: }
277:
278: /**
279: * @see org.ofbiz.workflow.WfAssignment#remove()
280: */
281: public void remove() throws WfException {
282: try {
283: valueObject().remove();
284: } catch (GenericEntityException e) {
285: throw new WfException(e.getMessage(), e);
286: }
287: }
288:
289: /**
290: * @see org.ofbiz.workflow.WfAssignment#status()
291: */
292: public String status() throws WfException {
293: return valueObject().getString("statusId");
294: }
295:
296: /**
297: * @see org.ofbiz.workflow.WfAssignment#fromDate()
298: */
299: public Timestamp fromDate() throws WfException {
300: return fromDate;
301: }
302:
303: private GenericValue valueObject() throws WfException {
304: GenericValue value = null;
305: Map fields = new HashMap();
306:
307: fields.put("workEffortId", activity.runtimeKey());
308: fields.put("partyId", resource.resourcePartyId());
309: fields.put("roleTypeId", resource.resourceRoleId());
310: fields.put("fromDate", fromDate);
311: try {
312: value = activity.getDelegator().findByPrimaryKey(
313: "WorkEffortPartyAssignment", fields);
314: } catch (GenericEntityException e) {
315: throw new WfException(e.getMessage(), e);
316: }
317: if (value == null)
318: throw new WfException(
319: "Invalid assignment; no runtime entity");
320: return value;
321: }
322:
323: private boolean isEqual(WfAssignment asgn) throws WfException {
324: // compare this to null = different assignment
325: if (asgn == null) {
326: return false;
327: }
328:
329: // if status is different; we must be different
330: if (!this .status().equals(asgn.status())) {
331: return false;
332: }
333:
334: // different activity = different assignment
335: WfActivity this Activity = this .activity();
336: WfActivity compActivity = asgn.activity();
337: if ((this Activity == null && compActivity != null)
338: || (this Activity != null && compActivity == null)) {
339: return false;
340: } else {
341: String this Key = this Activity.runtimeKey();
342: String compKey = compActivity.runtimeKey();
343: if ((this Key != null && compKey == null)
344: || (this Key == null && compKey != null)) {
345: return false;
346: } else if (this Key != null && compKey != null
347: && !this Key.equals(compKey)) {
348: return false;
349: }
350: }
351:
352: // different participantId = different assignment - the rest doesn't matter
353: WfResource this Resource = this .assignee();
354: WfResource compResource = asgn.assignee();
355: if ((this Resource == null && compResource != null)
356: || (this Resource != null && compResource == null)) {
357: return false;
358: } else {
359: String this Key = this Resource.resourceKey();
360: String compKey = compResource.resourceKey();
361: if ((this Key != null && compKey == null)
362: || (this Key == null && compKey != null)) {
363: return false;
364: } else if (this Key != null && compKey != null
365: && !this Key.equals(compKey)) {
366: return false;
367: }
368: }
369:
370: // same status, same activity, same participantId = same assignement
371: return true;
372: }
373: }
|