001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.bam.model;
018    
019    import java.util.Collection;
020    import java.util.Date;
021    import java.util.HashSet;
022    import javax.persistence.*;
023    
024    import org.apache.camel.bam.rules.ActivityRules;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    
028    /**
029     * Represents a single business process
030     *
031     * @version $Revision: $
032     */
033    @Entity
034    public class ProcessInstance  {
035        private static final transient Log LOG = LogFactory.getLog(ProcessInstance.class);
036        private ProcessDefinition processDefinition;
037        private Collection<ActivityState> activityStates = new HashSet<ActivityState>();
038        private String correlationKey;
039        private Date timeStarted;
040        private Date timeCompleted;
041    
042        public ProcessInstance() {
043            setTimeStarted(new Date());
044        }
045    
046        public String toString() {
047            return "ProcessInstance[" + getCorrelationKey() + "]";
048        }
049    
050        @Id
051        public String getCorrelationKey() {
052            return correlationKey;
053        }
054    
055        public void setCorrelationKey(String correlationKey) {
056            this.correlationKey = correlationKey;
057        }
058    
059        @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST })
060        public ProcessDefinition getProcessDefinition() {
061            return processDefinition;
062        }
063    
064        public void setProcessDefinition(ProcessDefinition processDefinition) {
065            this.processDefinition = processDefinition;
066        }
067    
068        @OneToMany(mappedBy = "processInstance", fetch = FetchType.LAZY, cascade = {CascadeType.ALL })
069        public Collection<ActivityState> getActivityStates() {
070            return activityStates;
071        }
072    
073        public void setActivityStates(Collection<ActivityState> activityStates) {
074            this.activityStates = activityStates;
075        }
076    
077    
078        @Transient
079        public boolean isStarted() {
080            return timeStarted != null;
081        }
082    
083        @Transient
084        public boolean isCompleted() {
085            return timeCompleted != null;
086        }
087    
088        @Temporal(TemporalType.TIME)
089        public Date getTimeStarted() {
090            return timeStarted;
091        }
092    
093        public void setTimeStarted(Date timeStarted) {
094            this.timeStarted = timeStarted;
095        }
096    
097        @Temporal(TemporalType.TIME)
098        public Date getTimeCompleted() {
099            return timeCompleted;
100        }
101    
102        public void setTimeCompleted(Date timeCompleted) {
103            this.timeCompleted = timeCompleted;
104        }    // Helper methods
105        //-------------------------------------------------------------------------
106    
107        /**
108         * Returns the activity state for the given activity
109         *
110         * @param activityRules the activity to find the state for
111         * @return the activity state or null if no state could be found for the
112         *         given activity
113         */
114        public ActivityState getActivityState(ActivityRules activityRules) {
115            for (ActivityState activityState : getActivityStates()) {
116                if (activityState.isActivity(activityRules)) {
117                    return activityState;
118                }
119            }
120            return null;
121        }
122    
123        public ActivityState getOrCreateActivityState(ActivityRules activityRules) {
124            ActivityState state = getActivityState(activityRules);
125    
126            if (state == null) {
127                state = createActivityState();
128                state.setProcessInstance(this);
129                state.setActivityDefinition(activityRules.getActivityDefinition());
130                // we don't need to do: getTemplate().persist(state);
131            }
132    
133            return state;
134        }
135    
136        protected ActivityState createActivityState() {
137            return new ActivityState();
138        }
139    }