KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > workflow > impl > WfAssignmentImpl


1 /*
2  * $Id: WfAssignmentImpl.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.workflow.impl;
26
27 import java.sql.Timestamp JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.UtilDateTime;
35 import org.ofbiz.entity.GenericEntityException;
36 import org.ofbiz.entity.GenericValue;
37 import org.ofbiz.workflow.CannotComplete;
38 import org.ofbiz.workflow.InvalidResource;
39 import org.ofbiz.workflow.WfActivity;
40 import org.ofbiz.workflow.WfAssignment;
41 import org.ofbiz.workflow.WfException;
42 import org.ofbiz.workflow.WfResource;
43
44 /**
45  * WfAssignmentImpl - Workflow Assignment Object implementation
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @version $Rev: 5462 $
49  * @since 2.0
50  */

51 public class WfAssignmentImpl implements WfAssignment {
52
53     public static final String JavaDoc module = WfAssignmentImpl.class.getName();
54
55     protected WfActivity activity = null;
56     protected WfResource resource = null;
57     protected Timestamp JavaDoc fromDate = null;
58     protected boolean create = false;
59
60     /**
61      * Creates new WfAssignment.
62      * @param activity Sets the activity object for this assignment.
63      * @param resource The WfResource object this is assigned to.
64      * @throws WfException
65      */

66     public WfAssignmentImpl(WfActivity activity, WfResource resource, Timestamp JavaDoc fromDate, boolean create) throws WfException {
67         this.activity = activity;
68         this.resource = resource;
69         this.fromDate = fromDate;
70         this.create = create;
71         checkAssignment();
72     }
73
74     // makes the assignment entity
75
private void checkAssignment() throws WfException {
76         String JavaDoc workEffortId = activity.runtimeKey();
77         String JavaDoc partyId = resource.resourcePartyId();
78         String JavaDoc roleTypeId = resource.resourceRoleId();
79
80         if (workEffortId == null)
81             throw new WfException("WorkEffort could not be found for assignment");
82         if (partyId == null && roleTypeId == null)
83             throw new WfException("Both party and role type IDs cannot be null");
84         if (fromDate == null)
85             throw new WfException("From date cannot be null");
86
87         GenericValue value = null;
88         Map JavaDoc fields = new HashMap JavaDoc();
89
90         fields.put("workEffortId", workEffortId);
91         fields.put("partyId", partyId);
92         fields.put("roleTypeId", roleTypeId);
93         fields.put("fromDate", fromDate);
94         fields.put("statusId", "CAL_SENT");
95
96         // check if one exists
97
try {
98             if (valueObject() != null) {
99                 Debug.logVerbose("[WfAssignment.checkAssignment] : found existing assignment.", module);
100                 return;
101             }
102         } catch (WfException e) {
103             Debug.logVerbose("[WfAssignment.checkAssignment] : no existing assignment.", module);
104         }
105
106         if (create) {
107             // none exist; create a new one
108
try {
109                 GenericValue v = activity.getDelegator().makeValue("WorkEffortPartyAssignment", fields);
110
111                 value = activity.getDelegator().create(v);
112                 Debug.logVerbose("[WfAssignment.checkAssignment] : created new party assignment : " + v, module);
113             } catch (GenericEntityException e) {
114                 throw new WfException(e.getMessage(), e);
115             }
116             if (value == null)
117                 throw new WfException("Could not create the assignement");
118         }
119         if (value == null)
120             throw new WfException("No existing assignment found or create failed");
121     }
122    
123     /**
124      * @see org.ofbiz.workflow.WfAssignment#accept()
125      */

126     public void accept() throws WfException {
127         boolean allDelegated = true;
128         boolean acceptAll = activity.getDefinitionObject().get("acceptAllAssignments") != null ?
129             activity.getDefinitionObject().getBoolean("acceptAllAssignments").booleanValue() : false;
130
131         if (!acceptAll) {
132             // check for existing accepted assignment
133
if (!activity.state().equals("open.not_running.not_started")) {
134                 // activity already running all assignments must be delegated in order to accept
135
Iterator JavaDoc ai = activity.getIteratorAssignment();
136
137                 while (ai.hasNext() && allDelegated) {
138                     WfAssignment a = (WfAssignment) ai.next();
139                     if (!a.equals(this) && !a.status().equals("CAL_DELEGATED")) {
140                         allDelegated = false;
141                     }
142                 }
143                 // we cannot accept if the activity is running, with active assignments
144
if (!allDelegated) {
145                     throw new WfException("Cannot accept; Activity already running with active assignments");
146                 }
147             } else {
148                 // activity not running, auto change all assignments to delegated status
149
Debug.logVerbose("[WfAssignment.accept] : setting other assignments to delegated status.", module);
150                 Iterator JavaDoc ai = activity.getIteratorAssignment();
151
152                 while (ai.hasNext()) {
153                     WfAssignment a = (WfAssignment) ai.next();
154                     if (!this.isEqual(a)) a.delegate();
155                 }
156             }
157         }
158         // set this assignment as accepted
159
changeStatus("CAL_ACCEPTED");
160     }
161  
162     /**
163      * @see org.ofbiz.workflow.WfAssignment#setResult(java.util.Map)
164      */

165     public void setResult(Map JavaDoc results) throws WfException {
166         activity.setResult(results);
167     }
168
169     /**
170      * @see org.ofbiz.workflow.WfAssignment#complete()
171      */

172     public void complete() throws WfException {
173         changeStatus("CAL_COMPLETED");
174         try {
175             activity.complete();
176         } catch (CannotComplete e) {
177             Debug.logWarning("Activity not complete : " + e.getMessage(), module);
178         }
179     }
180
181     /**
182      * @see org.ofbiz.workflow.WfAssignment#delegate()
183      */

184     public void delegate() throws WfException {
185         // check and make sure we are not already delegated
186
if (status().equals("CAL_DELEGATED"))
187             throw new WfException("Assignment has already been delegated");
188         
189         // set the thru-date
190
GenericValue valueObject = valueObject();
191         try {
192             valueObject.set("thruDate", UtilDateTime.nowTimestamp());
193             valueObject.store();
194             if (Debug.verboseOn()) Debug.logVerbose("[WfAssignment.delegated()] : set the thru-date.", module);
195         } catch (GenericEntityException e) {
196             e.printStackTrace();
197             throw new WfException(e.getMessage(), e);
198         }
199         
200         // change the status
201
changeStatus("CAL_DELEGATED");
202     }
203
204     /**
205      * @see org.ofbiz.workflow.WfAssignment#changeStatus(java.lang.String)
206      */

207     public void changeStatus(String JavaDoc status) throws WfException {
208         // change the status
209
GenericValue valueObject = valueObject();
210         try {
211             valueObject.set("statusId", status);
212             valueObject.store();
213             if (Debug.verboseOn()) Debug.logVerbose("[WfAssignment.changeStatus] : changed status to " + status, module);
214         } catch (GenericEntityException e) {
215             e.printStackTrace();
216             throw new WfException(e.getMessage(), e);
217         }
218     }
219
220     /**
221      * @see org.ofbiz.workflow.WfAssignment#activity()
222      */

223     public WfActivity activity() throws WfException {
224         return activity;
225     }
226
227     /**
228      * @see org.ofbiz.workflow.WfAssignment#assignee()
229      */

230     public WfResource assignee() throws WfException {
231         return resource;
232     }
233
234     /**
235      * @see org.ofbiz.workflow.WfAssignment#setAssignee(org.ofbiz.workflow.WfResource)
236      */

237     public void setAssignee(WfResource newValue) throws WfException, InvalidResource {
238         remove();
239         this.resource = newValue;
240         this.fromDate = new Timestamp JavaDoc(new Date JavaDoc().getTime());
241         checkAssignment();
242     }
243
244     /**
245      * @see org.ofbiz.workflow.WfAssignment#remove()
246      */

247     public void remove() throws WfException {
248         try {
249             valueObject().remove();
250         } catch (GenericEntityException e) {
251             throw new WfException(e.getMessage(), e);
252         }
253     }
254
255     /**
256      * @see org.ofbiz.workflow.WfAssignment#status()
257      */

258     public String JavaDoc status() throws WfException {
259         return valueObject().getString("statusId");
260     }
261
262     /**
263      * @see org.ofbiz.workflow.WfAssignment#fromDate()
264      */

265     public Timestamp JavaDoc fromDate() throws WfException {
266         return fromDate;
267     }
268
269     private GenericValue valueObject() throws WfException {
270         GenericValue value = null;
271         Map JavaDoc fields = new HashMap JavaDoc();
272
273         fields.put("workEffortId", activity.runtimeKey());
274         fields.put("partyId", resource.resourcePartyId());
275         fields.put("roleTypeId", resource.resourceRoleId());
276         fields.put("fromDate", fromDate);
277         try {
278             value = activity.getDelegator().findByPrimaryKey("WorkEffortPartyAssignment", fields);
279         } catch (GenericEntityException e) {
280             throw new WfException(e.getMessage(), e);
281         }
282         if (value == null)
283             throw new WfException("Invalid assignment; no runtime entity");
284         return value;
285     }
286     
287     private boolean isEqual(WfAssignment asgn) throws WfException {
288         // compare this to null = different assignment
289
if (asgn == null) {
290             return false;
291         }
292         
293         // if status is different; we must be different
294
if (!this.status().equals(asgn.status())) {
295             return false;
296         }
297         
298         // different activity = different assignment
299
WfActivity thisActivity = this.activity();
300         WfActivity compActivity = asgn.activity();
301         if ((thisActivity == null && compActivity != null) || (thisActivity != null && compActivity == null)) {
302             return false;
303         } else {
304             String JavaDoc thisKey = thisActivity.runtimeKey();
305             String JavaDoc compKey = compActivity.runtimeKey();
306             if ((thisKey != null && compKey == null) || (thisKey == null && compKey != null)) {
307                 return false;
308             } else if (thisKey != null && compKey != null && !thisKey.equals(compKey)) {
309                 return false;
310             }
311         }
312         
313         // different participantId = different assignment - the rest doesn't matter
314
WfResource thisResource = this.assignee();
315         WfResource compResource = asgn.assignee();
316         if ((thisResource == null && compResource != null) || (thisResource != null && compResource == null)) {
317             return false;
318         } else {
319             String JavaDoc thisKey = thisResource.resourceKey();
320             String JavaDoc compKey = compResource.resourceKey();
321             if ((thisKey != null && compKey == null) || (thisKey == null && compKey != null)) {
322                 return false;
323             } else if (thisKey != null && compKey != null && !thisKey.equals(compKey)) {
324                 return false;
325             }
326         }
327         
328         // same status, same activity, same participantId = same assignement
329
return true;
330     }
331 }
332
333
Popular Tags