Coverage Report - org.apache.camel.bam.model.ActivityState
 
Classes in this File Line Coverage Branch Coverage Complexity
ActivityState
95% 
100% 
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 org.apache.camel.bam.processor.ProcessContext;
 20  
 import org.apache.camel.bam.rules.ActivityRules;
 21  
 import org.apache.camel.util.ObjectHelper;
 22  
 
 23  
 import javax.persistence.CascadeType;
 24  
 import javax.persistence.Entity;
 25  
 import javax.persistence.FetchType;
 26  
 import javax.persistence.GeneratedValue;
 27  
 import javax.persistence.Id;
 28  
 import javax.persistence.ManyToOne;
 29  
 import javax.persistence.Temporal;
 30  
 import javax.persistence.TemporalType;
 31  
 import javax.persistence.Transient;
 32  
 import java.util.Date;
 33  
 
 34  
 /**
 35  
  * The default state for a specific activity within a process
 36  
  * 
 37  
  * @version $Revision: $
 38  
  */
 39  
 @Entity
 40  16
 public class ActivityState extends TemporalEntity {
 41  
     private ProcessInstance processInstance;
 42  16
     private Integer receivedMessageCount = 0;
 43  
     private ActivityDefinition activityDefinition;
 44  
     private Date timeExpected;
 45  
     @Temporal(TemporalType.TIME)
 46  
     private Date timeOverdue;
 47  16
     private Integer escalationLevel = 0;
 48  
 
 49  
     // This crap is required to work around a bug in hibernate
 50  
     @Override
 51  
     @Id
 52  
     @GeneratedValue
 53  
     public Long getId() {
 54  60
         return super.getId();
 55  
     }
 56  
 
 57  
     @Override
 58  
     public String toString() {
 59  4
         return "ActivityState[" + getId() + " on " + getProcessInstance() + " " + getActivityDefinition() + "]";
 60  
     }
 61  
 
 62  
     public synchronized void processExchange(ActivityRules activityRules, ProcessContext context) throws Exception {
 63  6
         int messageCount = 0;
 64  6
         Integer count = getReceivedMessageCount();
 65  6
         if (count != null) {
 66  6
             messageCount = count.intValue();
 67  
         }
 68  6
         setReceivedMessageCount(++messageCount);
 69  
 
 70  6
         if (messageCount == 1) {
 71  6
             onFirstMessage(context);
 72  
         }
 73  6
         int expectedMessages = activityRules.getExpectedMessages();
 74  6
         if (messageCount == expectedMessages) {
 75  6
             onExpectedMessage(context);
 76  6
         } else if (messageCount > expectedMessages) {
 77  0
             onExcessMessage(context);
 78  
         }
 79  6
     }
 80  
 
 81  
     /**
 82  
      * Returns true if this state is for the given activity
 83  
      */
 84  
     public boolean isActivity(ActivityRules activityRules) {
 85  14
         return ObjectHelper.equals(getActivityDefinition(), activityRules.getActivityDefinition());
 86  
     }
 87  
 
 88  
     // Properties
 89  
     // -----------------------------------------------------------------------
 90  
     @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST })
 91  
     public ProcessInstance getProcessInstance() {
 92  195
         return processInstance;
 93  
     }
 94  
 
 95  
     public void setProcessInstance(ProcessInstance processInstance) {
 96  14
         this.processInstance = processInstance;
 97  14
         processInstance.getActivityStates().add(this);
 98  14
     }
 99  
 
 100  
     @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST })
 101  
     public ActivityDefinition getActivityDefinition() {
 102  202
         return activityDefinition;
 103  
     }
 104  
 
 105  
     public void setActivityDefinition(ActivityDefinition activityDefinition) {
 106  14
         this.activityDefinition = activityDefinition;
 107  14
     }
 108  
 
 109  
     public Integer getEscalationLevel() {
 110  184
         return escalationLevel;
 111  
     }
 112  
 
 113  
     public void setEscalationLevel(Integer escalationLevel) {
 114  14
         this.escalationLevel = escalationLevel;
 115  14
     }
 116  
 
 117  
     public Integer getReceivedMessageCount() {
 118  190
         return receivedMessageCount;
 119  
     }
 120  
 
 121  
     public void setReceivedMessageCount(Integer receivedMessageCount) {
 122  12
         this.receivedMessageCount = receivedMessageCount;
 123  12
     }
 124  
 
 125  
     @Temporal(TemporalType.TIME)
 126  
     public Date getTimeExpected() {
 127  188
         return timeExpected;
 128  
     }
 129  
 
 130  
     public void setTimeExpected(Date timeExpected) {
 131  10
         this.timeExpected = timeExpected;
 132  10
     }
 133  
 
 134  
     @Temporal(TemporalType.TIME)
 135  
     public Date getTimeOverdue() {
 136  190
         return timeOverdue;
 137  
     }
 138  
 
 139  
     public void setTimeOverdue(Date timeOverdue) {
 140  10
         this.timeOverdue = timeOverdue;
 141  10
     }
 142  
 
 143  
     public void setTimeCompleted(Date timeCompleted) {
 144  6
         super.setTimeCompleted(timeCompleted);
 145  6
         if (timeCompleted != null) {
 146  6
             setEscalationLevel(-1);
 147  
         }
 148  6
     }
 149  
 
 150  
     @Transient
 151  
     public String getCorrelationKey() {
 152  1
         ProcessInstance pi = getProcessInstance();
 153  1
         if (pi == null) {
 154  0
             return null;
 155  
         }
 156  1
         return pi.getCorrelationKey();
 157  
     }
 158  
 
 159  
     // Implementation methods
 160  
     // -----------------------------------------------------------------------
 161  
 
 162  
     /**
 163  
      * Called when the first message is reached
 164  
      */
 165  
     protected void onFirstMessage(ProcessContext context) {
 166  6
         if (!isStarted()) {
 167  6
             setTimeStarted(currentTime());
 168  6
             context.onStarted(this);
 169  
         }
 170  6
     }
 171  
 
 172  
     /**
 173  
      * Called when the expected number of messages are is reached
 174  
      */
 175  
     protected void onExpectedMessage(ProcessContext context) {
 176  6
         if (!isCompleted()) {
 177  6
             setTimeCompleted(currentTime());
 178  6
             context.onCompleted(this);
 179  
         }
 180  6
     }
 181  
 
 182  
     /**
 183  
      * Called when an excess message (after the expected number of messages) are
 184  
      * received
 185  
      */
 186  
     protected void onExcessMessage(ProcessContext context) {
 187  
         // TODO
 188  0
     }
 189  
 
 190  
     protected Date currentTime() {
 191  12
         return new Date();
 192  
     }
 193  
 }