|
|
@@ -2,8 +2,6 @@ package se.citerus.dddsample.domain;
|
|
2
|
2
|
|
|
3
|
3
|
import org.apache.commons.lang.Validate;
|
|
4
|
4
|
import org.apache.commons.lang.builder.HashCodeBuilder;
|
|
5
|
|
-import org.apache.commons.lang.builder.ToStringBuilder;
|
|
6
|
|
-import org.apache.commons.lang.builder.ToStringStyle;
|
|
7
|
5
|
|
|
8
|
6
|
import javax.persistence.*;
|
|
9
|
7
|
import java.util.Comparator;
|
|
|
@@ -12,16 +10,13 @@ import java.util.Date;
|
|
12
|
10
|
/**
|
|
13
|
11
|
* A HandlingEvent is used to register the event when, for instance, a cargo is unloaded from a carrier at a some loacation at a given time. The
|
|
14
|
12
|
* HandlingEvent's are sent from different Incident Logging Applications some time after the event occured and contain information about the
|
|
15
|
|
- * {@link TrackingID}, {@link Location}, timestamp of the completion of the event, and possibly, if applicable a {@link CarrierMovement}.
|
|
|
13
|
+ * {@link TrackingId}, {@link Location}, timestamp of the completion of the event, and possibly, if applicable a {@link CarrierMovement}.
|
|
16
|
14
|
* <br><br>
|
|
17
|
|
- * HandlingEvent's could contain information about a {@link CarrierMovement} and if so, the event type must be either {@link Type.LOAD} or
|
|
|
15
|
+ * HandlingEvent's could contain information about a {@link CarrierMovement} and if so, the event type must be either {@link Type.LOAD} or
|
|
18
|
16
|
* {@link Type.UNLOAD}. All other events must be of {@link Type.RECEIVE}, {@link Type.CLAIM} or {@link Type.CUSTOMS}.
|
|
19
|
17
|
*/
|
|
20
|
18
|
@Entity
|
|
21
|
19
|
public class HandlingEvent {
|
|
22
|
|
- private static final Type[] VALID_TYPES_WITH_CARRIERMOVEMENT = new Type[]{Type.LOAD, Type.UNLOAD};
|
|
23
|
|
-
|
|
24
|
|
- private static final Type[] VALID_TYPES_NO_CARRIERMOVEMENT = new Type[]{Type.CLAIM, Type.RECEIVE, Type.CUSTOMS};
|
|
25
|
20
|
|
|
26
|
21
|
/**
|
|
27
|
22
|
* Comparator used to be able to sort HandlingEvents according to their completion time
|
|
|
@@ -54,18 +49,42 @@ public class HandlingEvent {
|
|
54
|
49
|
private Cargo cargo;
|
|
55
|
50
|
|
|
56
|
51
|
public enum Type {
|
|
57
|
|
- LOAD, UNLOAD, RECEIVE, CLAIM, CUSTOMS
|
|
|
52
|
+ LOAD(true),
|
|
|
53
|
+ UNLOAD(true),
|
|
|
54
|
+ RECEIVE(false),
|
|
|
55
|
+ CLAIM(false),
|
|
|
56
|
+ CUSTOMS(false);
|
|
|
57
|
+
|
|
|
58
|
+ private boolean carrierMovementRequired;
|
|
|
59
|
+
|
|
|
60
|
+ private Type(boolean carrierMovementRequired) {
|
|
|
61
|
+ this.carrierMovementRequired = carrierMovementRequired;
|
|
|
62
|
+ }
|
|
|
63
|
+
|
|
|
64
|
+ /**
|
|
|
65
|
+ * @return True if a carrier movement association is required for this event type.
|
|
|
66
|
+ */
|
|
|
67
|
+ public boolean requiresCarrierMovement() {
|
|
|
68
|
+ return carrierMovementRequired;
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ /**
|
|
|
72
|
+ * @return True if a carrier movement association is prohibited for this event type.
|
|
|
73
|
+ */
|
|
|
74
|
+ public boolean prohibitsCarrierMovement() {
|
|
|
75
|
+ return !requiresCarrierMovement();
|
|
|
76
|
+ }
|
|
58
|
77
|
}
|
|
59
|
78
|
|
|
60
|
79
|
|
|
61
|
80
|
/**
|
|
62
|
81
|
* Constructor for events that do not have a carrier movement associated.
|
|
63
|
82
|
*
|
|
64
|
|
- * @param cargo cargo
|
|
65
|
|
- * @param completionTime completion time
|
|
|
83
|
+ * @param cargo cargo
|
|
|
84
|
+ * @param completionTime completion time
|
|
66
|
85
|
* @param registrationTime registration time
|
|
67
|
|
- * @param type type of event. Legal values are CLAIM, RECIEVE and CUSTOMS
|
|
68
|
|
- * @param location where the event took place
|
|
|
86
|
+ * @param type type of event. Legal values are CLAIM, RECIEVE and CUSTOMS
|
|
|
87
|
+ * @param location where the event took place
|
|
69
|
88
|
*/
|
|
70
|
89
|
public HandlingEvent(Cargo cargo, Date completionTime, Date registrationTime, Type type, Location location) {
|
|
71
|
90
|
this.registrationTime = registrationTime;
|
|
|
@@ -73,8 +92,8 @@ public class HandlingEvent {
|
|
73
|
92
|
this.type = type;
|
|
74
|
93
|
this.cargo = cargo;
|
|
75
|
94
|
this.location = location;
|
|
76
|
|
-
|
|
77
|
|
- validateType(type, VALID_TYPES_NO_CARRIERMOVEMENT);
|
|
|
95
|
+
|
|
|
96
|
+ validateType();
|
|
78
|
97
|
}
|
|
79
|
98
|
|
|
80
|
99
|
/**
|
|
|
@@ -83,12 +102,12 @@ public class HandlingEvent {
|
|
83
|
102
|
* the location is the starting point of the movement, if the type is UNLOAD the location
|
|
84
|
103
|
* is the end point.
|
|
85
|
104
|
*
|
|
86
|
|
- * @param cargo cargo
|
|
87
|
|
- * @param completionTime completion time
|
|
|
105
|
+ * @param cargo cargo
|
|
|
106
|
+ * @param completionTime completion time
|
|
88
|
107
|
* @param registrationTime registration time
|
|
89
|
|
- * @param type type of event. Legal values are LOAD and UNLOAD
|
|
90
|
|
- * @param location where the event took place
|
|
91
|
|
- * @param carrierMovement carrier movement.
|
|
|
108
|
+ * @param type type of event. Legal values are LOAD and UNLOAD
|
|
|
109
|
+ * @param location where the event took place
|
|
|
110
|
+ * @param carrierMovement carrier movement.
|
|
92
|
111
|
*/
|
|
93
|
112
|
public HandlingEvent(Cargo cargo, Date completionTime, Date registrationTime, Type type, Location location, CarrierMovement carrierMovement) {
|
|
94
|
113
|
this.registrationTime = registrationTime;
|
|
|
@@ -97,9 +116,9 @@ public class HandlingEvent {
|
|
97
|
116
|
this.cargo = cargo;
|
|
98
|
117
|
this.location = location;
|
|
99
|
118
|
this.carrierMovement = carrierMovement;
|
|
100
|
|
-
|
|
101
|
|
- validateType(type, VALID_TYPES_WITH_CARRIERMOVEMENT);
|
|
|
119
|
+
|
|
102
|
120
|
Validate.notNull(carrierMovement, "CarrierMovementId must not be null for this type of event");
|
|
|
121
|
+ validateType();
|
|
103
|
122
|
}
|
|
104
|
123
|
|
|
105
|
124
|
|
|
|
@@ -145,8 +164,8 @@ public class HandlingEvent {
|
|
145
|
164
|
}
|
|
146
|
165
|
HandlingEvent other = (HandlingEvent) object;
|
|
147
|
166
|
return this.location.equals(other.location) &&
|
|
148
|
|
- this.completionTime.equals(other.completionTime) &&
|
|
149
|
|
- this.type.equals(other.type);
|
|
|
167
|
+ this.completionTime.equals(other.completionTime) &&
|
|
|
168
|
+ this.type.equals(other.type);
|
|
150
|
169
|
}
|
|
151
|
170
|
|
|
152
|
171
|
/**
|
|
|
@@ -170,21 +189,21 @@ public class HandlingEvent {
|
|
170
|
189
|
}
|
|
171
|
190
|
|
|
172
|
191
|
/**
|
|
173
|
|
- * Private helper that validate a HandlingEvent type agains an array of valid types
|
|
174
|
|
- *
|
|
175
|
|
- * @param type The type that should be validated
|
|
176
|
|
- * @param validTypes The list of valid types
|
|
|
192
|
+ * Validate that the event type is compatible with the carrier movement value.
|
|
|
193
|
+ * <p/>
|
|
|
194
|
+ * Only certain types of events may be associated with a carrier movement.
|
|
177
|
195
|
*/
|
|
178
|
|
- private void validateType(Type type, Type[] validTypes) {
|
|
179
|
|
- for (Type validType : validTypes) {
|
|
180
|
|
- if (type.equals(validType)){
|
|
181
|
|
- return;
|
|
182
|
|
- }
|
|
|
196
|
+ private void validateType() {
|
|
|
197
|
+ if (type.requiresCarrierMovement() && carrierMovement == null) {
|
|
|
198
|
+ throw new IllegalArgumentException("Carrier movement is required for event type " + type);
|
|
|
199
|
+ }
|
|
|
200
|
+ if (type.prohibitsCarrierMovement() && carrierMovement != null) {
|
|
|
201
|
+ throw new IllegalArgumentException("Carrier movement is not allowed with event type " + type);
|
|
183
|
202
|
}
|
|
184
|
|
- throw new IllegalArgumentException("Illegal event type " + type + ". Valid types are: " + ToStringBuilder.reflectionToString(validTypes, ToStringStyle.NO_FIELD_NAMES_STYLE));
|
|
185
|
203
|
}
|
|
186
|
|
-
|
|
|
204
|
+
|
|
187
|
205
|
// Needed by Hibernate
|
|
188
|
|
- HandlingEvent() {}
|
|
|
206
|
+ HandlingEvent() {
|
|
|
207
|
+ }
|
|
189
|
208
|
|
|
190
|
209
|
}
|