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