001: /*
002: * $Id: WfRequesterImpl.java,v 1.4 2004/03/12 23:45:02 ajzeneski 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.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.entity.GenericEntityException;
035: import org.ofbiz.entity.GenericValue;
036: import org.ofbiz.service.GenericRequester;
037: import org.ofbiz.service.GenericServiceException;
038: import org.ofbiz.service.ModelService;
039: import org.ofbiz.workflow.InvalidPerformer;
040: import org.ofbiz.workflow.SourceNotAvailable;
041: import org.ofbiz.workflow.WfEventAudit;
042: import org.ofbiz.workflow.WfException;
043: import org.ofbiz.workflow.WfProcess;
044: import org.ofbiz.workflow.WfProcessMgr;
045: import org.ofbiz.workflow.WfRequester;
046:
047: /**
048: * WfRequesterImpl - Workflow Requester implementation
049: *
050: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
051: * @version $Revision: 1.4 $
052: * @since 2.0
053: */
054: public class WfRequesterImpl implements WfRequester {
055:
056: public static final String module = WfRequesterImpl.class.getName();
057:
058: protected Map performers = null;
059:
060: /**
061: * Method WfRequesterImpl.
062: */
063: public WfRequesterImpl() {
064: this .performers = new HashMap();
065: }
066:
067: /**
068: * @see org.ofbiz.workflow.WfRequester#registerProcess(org.ofbiz.workflow.WfProcess, java.util.Map, org.ofbiz.service.GenericRequester)
069: */
070: public void registerProcess(WfProcess process, Map context,
071: GenericRequester requester) throws WfException {
072: if (process == null)
073: throw new WfException("Process cannot be null");
074: if (context == null)
075: throw new WfException("Context should not be null");
076:
077: performers.put(process, requester);
078: WfProcessMgr mgr = process.manager();
079:
080: // Validate the process context w/ what was passed.
081: try {
082: if (Debug.verboseOn())
083: Debug.logVerbose("Validating w/ signature: "
084: + mgr.contextSignature(), module);
085: ModelService.validate(mgr.contextSignature(), context,
086: true, null, ModelService.IN_PARAM);
087: } catch (GenericServiceException e) {
088: throw new WfException(
089: "Context passed does not validate against defined signature: ",
090: e);
091: }
092:
093: // Set the context w/ the process
094: Map localContext = new HashMap(context);
095: localContext.putAll(mgr.getInitialContext());
096: process.setProcessContext(localContext);
097:
098: // Set the source reference id if one was passed
099: GenericValue processDefinition = process.getDefinitionObject();
100: String sourceReferenceField = processDefinition
101: .getString("sourceReferenceField");
102: if (context.containsKey(sourceReferenceField)) {
103: GenericValue processObj = process.getRuntimeObject();
104: if (processObj != null) {
105: try {
106: processObj.set("sourceReferenceId", localContext
107: .get(sourceReferenceField));
108: processObj.store();
109: } catch (GenericEntityException e) {
110: throw new WfException(
111: "Cannot set sourceReferenceId on the process runtime object",
112: e);
113: }
114: }
115: }
116:
117: }
118:
119: /**
120: * @see org.ofbiz.workflow.WfRequester#howManyPerformer()
121: */
122: public int howManyPerformer() throws WfException {
123: return performers.size();
124: }
125:
126: /**
127: * @see org.ofbiz.workflow.WfRequester#getIteratorPerformer()
128: */
129: public Iterator getIteratorPerformer() throws WfException {
130: return performers.keySet().iterator();
131: }
132:
133: /**
134: * @see org.ofbiz.workflow.WfRequester#getSequencePerformer(int)
135: */
136: public List getSequencePerformer(int maxNumber) throws WfException {
137: if (maxNumber > 0)
138: return new ArrayList(performers.keySet()).subList(0,
139: (maxNumber - 1));
140: return new ArrayList(performers.keySet());
141: }
142:
143: /**
144: * @see org.ofbiz.workflow.WfRequester#isMemberOfPerformer(org.ofbiz.workflow.WfProcess)
145: */
146: public boolean isMemberOfPerformer(WfProcess member)
147: throws WfException {
148: return performers.containsKey(member);
149: }
150:
151: /**
152: * @see org.ofbiz.workflow.WfRequester#receiveEvent(org.ofbiz.workflow.WfEventAudit)
153: */
154: public synchronized void receiveEvent(WfEventAudit event)
155: throws WfException, InvalidPerformer {
156: // Should the source of the audit come from the process? if so use this.
157: WfProcess process = null;
158:
159: try {
160: process = (WfProcess) event.source();
161: } catch (SourceNotAvailable sna) {
162: throw new InvalidPerformer("Could not get the performer",
163: sna);
164: } catch (ClassCastException cce) {
165: throw new InvalidPerformer("Not a valid process object",
166: cce);
167: }
168: if (process == null)
169: throw new InvalidPerformer("No performer specified");
170: if (!performers.containsKey(process))
171: throw new InvalidPerformer(
172: "Performer not assigned to this requester");
173:
174: GenericRequester req = null;
175:
176: if (performers.containsKey(process))
177: req = (GenericRequester) performers.get(process);
178: if (req != null)
179: req.receiveResult(process.result());
180: }
181: }
|