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 org.apache.camel.bam.rules.ActivityRules; 020 import org.apache.commons.logging.Log; 021 import org.apache.commons.logging.LogFactory; 022 023 import javax.persistence.CascadeType; 024 import javax.persistence.Entity; 025 import javax.persistence.FetchType; 026 import javax.persistence.GeneratedValue; 027 import javax.persistence.Id; 028 import javax.persistence.ManyToOne; 029 import javax.persistence.OneToMany; 030 import java.util.Collection; 031 import java.util.Date; 032 import java.util.HashSet; 033 034 /** 035 * Represents a single business process 036 * 037 * @version $Revision: $ 038 */ 039 @Entity 040 public class ProcessInstance extends TemporalEntity { 041 private static final transient Log log = LogFactory.getLog(ProcessInstance.class); 042 private ProcessDefinition processDefinition; 043 private Collection<ActivityState> activityStates = new HashSet<ActivityState>(); 044 private String correlationKey; 045 046 public ProcessInstance() { 047 setTimeStarted(new Date()); 048 } 049 050 public String toString() { 051 return getClass().getName() + "[id: " + getId() + ", key: " + getCorrelationKey() + "]"; 052 } 053 054 // This crap is required to work around a bug in hibernate 055 @Override 056 @Id 057 @GeneratedValue 058 public Long getId() { 059 return super.getId(); 060 } 061 062 @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST}) 063 public ProcessDefinition getProcessDefinition() { 064 return processDefinition; 065 } 066 067 public void setProcessDefinition(ProcessDefinition processDefinition) { 068 this.processDefinition = processDefinition; 069 } 070 071 @OneToMany(mappedBy = "processInstance", fetch = FetchType.LAZY, cascade = {CascadeType.ALL}) 072 public Collection<ActivityState> getActivityStates() { 073 return activityStates; 074 } 075 076 public void setActivityStates(Collection<ActivityState> activityStates) { 077 this.activityStates = activityStates; 078 } 079 080 public String getCorrelationKey() { 081 return correlationKey; 082 } 083 084 public void setCorrelationKey(String correlationKey) { 085 this.correlationKey = correlationKey; 086 } 087 088 // Helper methods 089 //------------------------------------------------------------------------- 090 091 /** 092 * Returns the activity state for the given activity 093 * 094 * @param activityRules the activity to find the state for 095 * @return the activity state or null if no state could be found for the 096 * given activity 097 */ 098 public ActivityState getActivityState(ActivityRules activityRules) { 099 for (ActivityState activityState : getActivityStates()) { 100 if (activityState.isActivity(activityRules)) { 101 return activityState; 102 } 103 } 104 return null; 105 } 106 107 public ActivityState getOrCreateActivityState(ActivityRules activityRules) { 108 ActivityState state = getActivityState(activityRules); 109 110 if (state == null) { 111 state = createActivityState(); 112 state.setProcessInstance(this); 113 state.setActivityDefinition(activityRules.getActivityDefinition()); 114 // we don't need to do: getTemplate().persist(state); 115 } 116 117 return state; 118 } 119 120 protected ActivityState createActivityState() { 121 return new ActivityState(); 122 } 123 }