Coverage Report - org.apache.camel.bam.model.ProcessInstance
 
Classes in this File Line Coverage Branch Coverage Complexity
ProcessInstance
94% 
60% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.bam.model;
 18  
 
 19  
 import java.util.Collection;
 20  
 import java.util.Date;
 21  
 import java.util.HashSet;
 22  
 import javax.persistence.*;
 23  
 
 24  
 import org.apache.camel.bam.rules.ActivityRules;
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 
 28  
 /**
 29  
  * Represents a single business process
 30  
  *
 31  
  * @version $Revision: $
 32  
  */
 33  
 @Entity
 34  
 public class ProcessInstance  {
 35  2
     private static final transient Log LOG = LogFactory.getLog(ProcessInstance.class);
 36  
     private ProcessDefinition processDefinition;
 37  10
     private Collection<ActivityState> activityStates = new HashSet<ActivityState>();
 38  
     private String correlationKey;
 39  
     private Date timeStarted;
 40  
     private Date timeCompleted;
 41  
 
 42  10
     public ProcessInstance() {
 43  10
         setTimeStarted(new Date());
 44  10
     }
 45  
 
 46  
     public String toString() {
 47  8
         return "ProcessInstance[" + getCorrelationKey() + "]";
 48  
     }
 49  
 
 50  
     @Id
 51  
     public String getCorrelationKey() {
 52  51
         return correlationKey;
 53  
     }
 54  
 
 55  
     public void setCorrelationKey(String correlationKey) {
 56  12
         this.correlationKey = correlationKey;
 57  12
     }
 58  
 
 59  
     @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST })
 60  
     public ProcessDefinition getProcessDefinition() {
 61  140
         return processDefinition;
 62  
     }
 63  
 
 64  
     public void setProcessDefinition(ProcessDefinition processDefinition) {
 65  8
         this.processDefinition = processDefinition;
 66  8
     }
 67  
 
 68  
     @OneToMany(mappedBy = "processInstance", fetch = FetchType.LAZY, cascade = {CascadeType.ALL })
 69  
     public Collection<ActivityState> getActivityStates() {
 70  170
         return activityStates;
 71  
     }
 72  
 
 73  
     public void setActivityStates(Collection<ActivityState> activityStates) {
 74  8
         this.activityStates = activityStates;
 75  8
     }
 76  
 
 77  
 
 78  
     @Transient
 79  
     public boolean isStarted() {
 80  0
         return timeStarted != null;
 81  
     }
 82  
 
 83  
     @Transient
 84  
     public boolean isCompleted() {
 85  0
         return timeCompleted != null;
 86  
     }
 87  
 
 88  
     @Temporal(TemporalType.TIME)
 89  
     public Date getTimeStarted() {
 90  132
         return timeStarted;
 91  
     }
 92  
 
 93  
     public void setTimeStarted(Date timeStarted) {
 94  18
         this.timeStarted = timeStarted;
 95  18
     }
 96  
 
 97  
     @Temporal(TemporalType.TIME)
 98  
     public Date getTimeCompleted() {
 99  132
         return timeCompleted;
 100  
     }
 101  
 
 102  
     public void setTimeCompleted(Date timeCompleted) {
 103  8
         this.timeCompleted = timeCompleted;
 104  8
     }    // 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  16
         for (ActivityState activityState : getActivityStates()) {
 116  14
             if (activityState.isActivity(activityRules)) {
 117  8
                 return activityState;
 118  
             }
 119  6
         }
 120  8
         return null;
 121  
     }
 122  
 
 123  
     public ActivityState getOrCreateActivityState(ActivityRules activityRules) {
 124  10
         ActivityState state = getActivityState(activityRules);
 125  
 
 126  10
         if (state == null) {
 127  8
             state = createActivityState();
 128  8
             state.setProcessInstance(this);
 129  8
             state.setActivityDefinition(activityRules.getActivityDefinition());
 130  
             // we don't need to do: getTemplate().persist(state);
 131  
         }
 132  
 
 133  10
         return state;
 134  
     }
 135  
 
 136  
     protected ActivityState createActivityState() {
 137  8
         return new ActivityState();
 138  
     }
 139  
 }