Sfoglia il codice sorgente

Added Location to HandlingEvent c-tor (forced commit)

jorgen_falk 18 anni fa
parent
commit
b0e13bf4f9

+ 45
- 22
dddsample/src/main/java/se/citerus/dddsample/domain/HandlingEvent.java Vedi File

@@ -1,21 +1,28 @@
1 1
 package se.citerus.dddsample.domain;
2 2
 
3
+import org.apache.commons.lang.Validate;
3 4
 import org.apache.commons.lang.builder.HashCodeBuilder;
5
+import org.apache.commons.lang.builder.ToStringBuilder;
6
+import org.apache.commons.lang.builder.ToStringStyle;
4 7
 
5 8
 import javax.persistence.*;
6 9
 import java.util.Comparator;
7 10
 import java.util.Date;
8 11
 
9 12
 /**
10
- * HandlingEvent links the type of handling with a CarrierMovement.
11
- * 
12
- * Since HandlingEvents can be added in any order to a Cargo (or
13
- * DeliveryHistory), they need to implement Comparable to be able to be sorted
14
- * in correct order.
15
- *
13
+ * 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
+ * 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}.
16
+ * <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 
18
+ * {@link Type.UNLOAD}. All other events must be of {@link Type.RECEIVE}, {@link Type.CLAIM} or {@link Type.CUSTOMS}.
16 19
  */
17 20
 @Entity
18 21
 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
+
19 26
   /**
20 27
    * Comparator used to be able to sort HandlingEvents according to their completion time
21 28
    */
@@ -50,12 +57,6 @@ public class HandlingEvent {
50 57
     LOAD, UNLOAD, RECEIVE, CLAIM, CUSTOMS
51 58
   }
52 59
 
53
-  private HandlingEvent(Cargo cargo, Date completionTime, Date registrationTime, Type type) {
54
-    this.registrationTime = registrationTime;
55
-    this.completionTime = completionTime;
56
-    this.type = type;
57
-    this.cargo = cargo;
58
-  }
59 60
 
60 61
   /**
61 62
    * Constructor for events that do not have a carrier movement associated.
@@ -67,8 +68,13 @@ public class HandlingEvent {
67 68
    * @param location where the event took place
68 69
    */
69 70
   public HandlingEvent(Cargo cargo, Date completionTime, Date registrationTime, Type type, Location location) {
70
-    this(cargo, completionTime, registrationTime, type);
71
+    this.registrationTime = registrationTime;
72
+    this.completionTime = completionTime;
73
+    this.type = type;
74
+    this.cargo = cargo;
71 75
     this.location = location;
76
+    
77
+    validateType(type, VALID_TYPES_NO_CARRIERMOVEMENT);
72 78
   }
73 79
 
74 80
   /**
@@ -81,20 +87,22 @@ public class HandlingEvent {
81 87
    * @param completionTime completion time
82 88
    * @param registrationTime registration time
83 89
    * @param type type of event. Legal values are LOAD and UNLOAD
90
+   * @param location where the event took place
84 91
    * @param carrierMovement carrier movement.
85 92
    */
86
-  public HandlingEvent(Cargo cargo, Date completionTime, Date registrationTime, Type type, CarrierMovement carrierMovement) {
87
-    this(cargo, completionTime, registrationTime, type);
93
+  public HandlingEvent(Cargo cargo, Date completionTime, Date registrationTime, Type type, Location location, CarrierMovement carrierMovement) {
94
+    this.registrationTime = registrationTime;
95
+    this.completionTime = completionTime;
96
+    this.type = type;
97
+    this.cargo = cargo;
98
+    this.location = location;
88 99
     this.carrierMovement = carrierMovement;
89
-    if (Type.LOAD.equals(type)) {
90
-      this.location = carrierMovement.from();
91
-    } else if (Type.UNLOAD.equals(type)) {
92
-      this.location = carrierMovement.to();
93
-    } else {
94
-      throw new IllegalArgumentException("Can't derive location from carrier movement for event type " + type);
95
-    }
100
+    
101
+    validateType(type, VALID_TYPES_WITH_CARRIERMOVEMENT);
102
+    Validate.notNull(carrierMovement, "CarrierMovementId must not be null for this type of event");
96 103
   }
97 104
 
105
+
98 106
   public Long id() {
99 107
     return this.id;
100 108
   }
@@ -161,6 +169,21 @@ public class HandlingEvent {
161 169
     return other != null && id.equals(other.id);
162 170
   }
163 171
 
172
+  /**
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
177
+   */
178
+  private void validateType(Type type, Type[] validTypes) {
179
+    for (Type validType : validTypes) {
180
+      if (type.equals(validType)){
181
+        return;
182
+      }
183
+    }
184
+    throw new IllegalArgumentException("Illegal event type " + type + ". Valid types are: " + ToStringBuilder.reflectionToString(validTypes, ToStringStyle.NO_FIELD_NAMES_STYLE));
185
+  }
186
+  
164 187
   // Needed by Hibernate
165 188
   HandlingEvent() {}
166 189