001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: 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
008: are 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 disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.spi.event;
046:
047: import org.obe.OBERuntimeException;
048: import org.obe.client.api.repository.RepositoryException;
049: import org.obe.spi.model.ActivityInstance;
050: import org.obe.spi.service.ProcessRepository;
051: import org.obe.spi.service.WorkflowEventBroker;
052: import org.obe.util.WorkflowUtilities;
053: import org.obe.xpdl.model.activity.Activity;
054: import org.obe.xpdl.model.workflow.WorkflowProcess;
055: import org.wfmc.audit.WMAEventCode;
056: import org.wfmc.wapi.WMActivityInstanceState;
057: import org.wfmc.wapi.WMInvalidActivityNameException;
058:
059: /**
060: * Delivers activity instance event notifications.
061: *
062: * @author Anthony Eden
063: * @author Adrian Price
064: */
065: public final class ActivityInstanceEvent extends WorkflowEvent {
066: private static final long serialVersionUID = -3860391828783448630L;
067: /**
068: * The activity instance was aborted.
069: */
070: public static final int ABORTED = WMActivityInstanceState.ABORT_ACTION;
071: /**
072: * The activity instance was completed.
073: */
074: public static final int COMPLETED = WMActivityInstanceState.COMPLETE_ACTION;
075: /**
076: * The activity instance was created.
077: */
078: public static final int CREATED = WMActivityInstanceState.CREATE_ACTION;
079: /**
080: * The activity instance was resumed.
081: */
082: public static final int RESUMED = WMActivityInstanceState.RESUME_ACTION;
083: /**
084: * The activity instance was started.
085: */
086: public static final int STARTED = WMActivityInstanceState.START_ACTION;
087: /**
088: * The activity instance was started.
089: */
090: public static final int STOPPED = WMActivityInstanceState.STOP_ACTION;
091: /**
092: * The activity instance was suspended.
093: */
094: public static final int SUSPENDED = WMActivityInstanceState.SUSPEND_ACTION;
095: /**
096: * The activity instance was terminated.
097: */
098: public static final int TERMINATED = WMActivityInstanceState.TERMINATE_ACTION;
099: private static final WMAEventCode[] IF5_EVENT_CODES = {
100: WMAEventCode.ABORTED_ACTIVITY_INSTANCE,
101: WMAEventCode.COMPLETED_ACTIVITY_INSTANCE,
102: WMAEventCode.UNKNOWN,
103: WMAEventCode.CHANGED_ACTIVITY_INSTANCE_STATE,
104: WMAEventCode.CHANGED_ACTIVITY_INSTANCE_STATE,
105: WMAEventCode.CHANGED_ACTIVITY_INSTANCE_STATE,
106: WMAEventCode.CHANGED_ACTIVITY_INSTANCE_STATE,
107: WMAEventCode.TERMINATED_ACTIVITY_INSTANCE };
108: private static final String[] EVENT_TYPES = {
109: "ActivityInstanceAborted", "ActivityInstanceCompleted",
110: "ActivityInstanceCreated", "ActivityInstanceResumed",
111: "ActivityInstanceStarted", "ActivityInstanceStopped",
112: "ActivityInstanceSuspended", "ActivityInstanceTerminated" };
113:
114: private int _previousState;
115: private transient Activity _definition;
116:
117: private static String valueOf(int state) {
118: return state == -1 ? null : WMActivityInstanceState.valueOf(
119: state).toString();
120: }
121:
122: /**
123: * Constructs a new activity instance event.
124: *
125: * @param source
126: * @param id
127: * @param broker
128: * @param definition
129: * @param previousState
130: */
131: public ActivityInstanceEvent(ActivityInstance source, int id,
132: WorkflowEventBroker broker, Activity definition,
133: int previousState) {
134:
135: super (source, id, ActivityInstance.class, EVENT_TYPES[id],
136: source.getActivityInstanceId(), broker);
137: _definition = definition;
138: _previousState = previousState;
139: }
140:
141: /**
142: * Returns the source activity instance.
143: *
144: * @return The activity instance that fired the event.
145: */
146: public ActivityInstance getActivityInstance() {
147: return (ActivityInstance) source;
148: }
149:
150: public int getPreviousState() {
151: return _previousState;
152: }
153:
154: /**
155: * Returns the definition of the source activity.
156: *
157: * @return The activity definition from the workflow process definition.
158: */
159: public Activity getActivityDefinition() {
160: if (_definition == null && _broker != null) {
161: ProcessRepository processRepository = _broker
162: .getServiceManager().getProcessRepository();
163: try {
164: WorkflowProcess workflow = processRepository
165: .findWorkflowProcess(getActivityInstance()
166: .getProcessDefinitionId());
167: _definition = WorkflowUtilities
168: .findActivity(workflow, getActivityInstance()
169: .getActivityDefinitionId());
170: } catch (RepositoryException e) {
171: throw new OBERuntimeException(e);
172: } catch (WMInvalidActivityNameException e) {
173: throw new OBERuntimeException(e);
174: }
175: }
176: return _definition;
177: }
178:
179: public WMAEventCode getWMAEventCode() {
180: return IF5_EVENT_CODES[_id];
181: }
182:
183: public String toString() {
184: return EVENT_TYPES[_id] + "[source=" + source
185: + ", previousState=" + valueOf(_previousState)
186: + ", definition=" + _definition + ']';
187: }
188: }
|