| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 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 |
|
|
| 33 |
|
|
| 34 |
|
|
| 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 |
|
|
| 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 |
|
|
| 80 |
|
|
| 81 |
|
public boolean isActivity(ActivityRules activityRules) { |
| 82 |
2 |
return ObjectHelper.equals(getActivityDefinition(), activityRules.getActivityDefinition()); |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
|
| 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 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 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 |
|
|
| 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 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
protected void onExcessMessage(ProcessContext context) { |
| 173 |
|
|
| 174 |
0 |
} |
| 175 |
|
|
| 176 |
|
protected Date currentTime() { |
| 177 |
2 |
return new Date(); |
| 178 |
|
} |
| 179 |
|
} |