KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: WfRequesterImpl.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.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Locale JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.entity.GenericEntityException;
36 import org.ofbiz.entity.GenericValue;
37 import org.ofbiz.service.GenericRequester;
38 import org.ofbiz.service.GenericServiceException;
39 import org.ofbiz.service.ModelService;
40 import org.ofbiz.workflow.InvalidPerformer;
41 import org.ofbiz.workflow.SourceNotAvailable;
42 import org.ofbiz.workflow.WfEventAudit;
43 import org.ofbiz.workflow.WfException;
44 import org.ofbiz.workflow.WfProcess;
45 import org.ofbiz.workflow.WfProcessMgr;
46 import org.ofbiz.workflow.WfRequester;
47
48 /**
49  * WfRequesterImpl - Workflow Requester implementation
50  *
51  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
52  * @version $Rev: 5462 $
53  * @since 2.0
54  */

55 public class WfRequesterImpl implements WfRequester {
56     
57     public static final String JavaDoc module = WfRequesterImpl.class.getName();
58
59     protected Map JavaDoc performers = null;
60       
61     /**
62      * Method WfRequesterImpl.
63      */

64     public WfRequesterImpl() {
65         this.performers = new HashMap JavaDoc();
66     }
67
68     /**
69      * @see org.ofbiz.workflow.WfRequester#registerProcess(org.ofbiz.workflow.WfProcess, java.util.Map, org.ofbiz.service.GenericRequester)
70      */

71     public void registerProcess(WfProcess process, Map JavaDoc context, GenericRequester requester) throws WfException {
72         if (process == null)
73             throw new WfException("Process cannot be null");
74         if (context == null)
75             throw new WfException("Context should not be null");
76
77         performers.put(process, requester);
78         WfProcessMgr mgr = process.manager();
79
80         // Validate the process context w/ what was passed.
81
try {
82             if (Debug.verboseOn()) Debug.logVerbose("Validating w/ signature: " + mgr.contextSignature(), module);
83             ModelService.validate(mgr.contextSignature(), context, true, null, ModelService.IN_PARAM, Locale.getDefault());
84         } catch (GenericServiceException e) {
85             throw new WfException("Context passed does not validate against defined signature: ", e);
86         }
87
88         // Set the context w/ the process
89
Map JavaDoc localContext = new HashMap JavaDoc(context);
90         localContext.putAll(mgr.getInitialContext());
91         process.setProcessContext(localContext);
92         
93         // Set the source reference id if one was passed
94
GenericValue processDefinition = process.getDefinitionObject();
95         String JavaDoc sourceReferenceField = processDefinition.getString("sourceReferenceField");
96         if (context.containsKey(sourceReferenceField)) {
97             GenericValue processObj = process.getRuntimeObject();
98             if (processObj != null) {
99                 try {
100                     processObj.set("sourceReferenceId", localContext.get(sourceReferenceField));
101                     processObj.store();
102                 } catch (GenericEntityException e) {
103                     throw new WfException("Cannot set sourceReferenceId on the process runtime object", e);
104                 }
105             }
106         }
107                   
108     }
109
110     /**
111      * @see org.ofbiz.workflow.WfRequester#howManyPerformer()
112      */

113     public int howManyPerformer() throws WfException {
114         return performers.size();
115     }
116   
117     /**
118      * @see org.ofbiz.workflow.WfRequester#getIteratorPerformer()
119      */

120     public Iterator JavaDoc getIteratorPerformer() throws WfException {
121         return performers.keySet().iterator();
122     }
123    
124     /**
125      * @see org.ofbiz.workflow.WfRequester#getSequencePerformer(int)
126      */

127     public List JavaDoc getSequencePerformer(int maxNumber) throws WfException {
128         if (maxNumber > 0)
129             return new ArrayList JavaDoc(performers.keySet()).subList(0, (maxNumber - 1));
130         return new ArrayList JavaDoc(performers.keySet());
131     }
132   
133     /**
134      * @see org.ofbiz.workflow.WfRequester#isMemberOfPerformer(org.ofbiz.workflow.WfProcess)
135      */

136     public boolean isMemberOfPerformer(WfProcess member) throws WfException {
137         return performers.containsKey(member);
138     }
139    
140     /**
141      * @see org.ofbiz.workflow.WfRequester#receiveEvent(org.ofbiz.workflow.WfEventAudit)
142      */

143     public synchronized void receiveEvent(WfEventAudit event) throws WfException, InvalidPerformer {
144         // Should the source of the audit come from the process? if so use this.
145
WfProcess process = null;
146
147         try {
148             process = (WfProcess) event.source();
149         } catch (SourceNotAvailable sna) {
150             throw new InvalidPerformer("Could not get the performer", sna);
151         } catch (ClassCastException JavaDoc cce) {
152             throw new InvalidPerformer("Not a valid process object", cce);
153         }
154         if (process == null)
155             throw new InvalidPerformer("No performer specified");
156         if (!performers.containsKey(process))
157             throw new InvalidPerformer("Performer not assigned to this requester");
158
159         GenericRequester req = null;
160
161         if (performers.containsKey(process))
162             req = (GenericRequester) performers.get(process);
163         if (req != null)
164             req.receiveResult(process.result());
165     }
166 }
167
168
Popular Tags