peter_backlund 17 jaren geleden
bovenliggende
commit
d032c94a7b

+ 5
- 3
dddsample/src/main/java/se/citerus/dddsample/domain/model/handling/CannotCreateHandlingEventException.java Bestand weergeven

@@ -1,10 +1,12 @@
1 1
 package se.citerus.dddsample.domain.model.handling;
2 2
 
3 3
 /**
4
+ * If a {@link se.citerus.dddsample.domain.model.handling.HandlingEvent} can't be
5
+ * created from an incoming {@link #se.citerus.dddsample.application.HandlingEventRegistrationAttempt},
4 6
  *
7
+ * It is a checked exception because it's not a programming error, but rather a
8
+ * special case that the application is built to handle. It can occur during normal
9
+ * program execution.
5 10
  */
6 11
 public class CannotCreateHandlingEventException extends Exception {
7
-
8
-
9
-
10 12
 }

+ 35
- 25
dddsample/src/main/java/se/citerus/dddsample/domain/model/handling/HandlingEvent.java Bestand weergeven

@@ -3,10 +3,11 @@ package se.citerus.dddsample.domain.model.handling;
3 3
 import org.apache.commons.lang.Validate;
4 4
 import org.apache.commons.lang.builder.EqualsBuilder;
5 5
 import org.apache.commons.lang.builder.HashCodeBuilder;
6
+import org.apache.commons.lang.builder.ToStringBuilder;
7
+import org.apache.commons.lang.builder.ToStringStyle;
6 8
 import se.citerus.dddsample.domain.model.DomainEvent;
7 9
 import se.citerus.dddsample.domain.model.ValueObject;
8 10
 import se.citerus.dddsample.domain.model.cargo.Cargo;
9
-import se.citerus.dddsample.domain.model.carrier.CarrierMovement;
10 11
 import se.citerus.dddsample.domain.model.carrier.Voyage;
11 12
 import se.citerus.dddsample.domain.model.location.Location;
12 13
 import se.citerus.dddsample.domain.shared.DomainObjectUtils;
@@ -21,11 +22,11 @@ import java.util.Date;
21 22
  * The HandlingEvent's are sent from different Incident Logging Applications
22 23
  * some time after the event occured and contain information about the
23 24
  * {@link se.citerus.dddsample.domain.model.cargo.TrackingId}, {@link se.citerus.dddsample.domain.model.location.Location}, timestamp of the completion of the event,
24
- * and possibly, if applicable a {@link se.citerus.dddsample.domain.model.carrier.CarrierMovement}.
25
+ * and possibly, if applicable a {@link se.citerus.dddsample.domain.model.carrier.Voyage}.
25 26
  * <p/>
26 27
  * This class is the only member, and consequently the root, of the HandlingEvent aggregate. 
27 28
  * <p/>
28
- * HandlingEvent's could contain information about a {@link CarrierMovement} and if so,
29
+ * HandlingEvent's could contain information about a {@link Voyage} and if so,
29 30
  * the event type must be either {@link Type#LOAD} or {@link Type#UNLOAD}.
30 31
  * <p/>
31 32
  * All other events must be of {@link Type#RECEIVE}, {@link Type#CLAIM} or {@link Type#CUSTOMS}.
@@ -35,11 +36,12 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
35 36
   /**
36 37
    * Comparator used to be able to sort HandlingEvents according to their completion time
37 38
    */
38
-  public static final Comparator<HandlingEvent> BY_COMPLETION_TIME_COMPARATOR = new Comparator<HandlingEvent>() {
39
-    public int compare(final HandlingEvent o1, final HandlingEvent o2) {
40
-      return o1.completionTime().compareTo(o2.completionTime());
41
-    }
42
-  };
39
+  public static final Comparator<HandlingEvent> BY_COMPLETION_TIME_COMPARATOR =
40
+    new Comparator<HandlingEvent>() {
41
+      public int compare(final HandlingEvent he1, final HandlingEvent he2) {
42
+        return he1.completionTime().compareTo(he2.completionTime());
43
+      }
44
+    };
43 45
 
44 46
   private Type type;
45 47
   private Voyage voyage;
@@ -59,37 +61,39 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
59 61
     CLAIM(false),
60 62
     CUSTOMS(false);
61 63
 
62
-    private boolean carrierMovementRequired;
64
+    private final boolean voyageRequired;
63 65
 
64 66
     /**
65 67
      * Private enum constructor.
66 68
      *
67
-     * @param carrierMovementRequired whether or not a carrier movement is associated with this event type
69
+     * @param voyageRequired whether or not a voyage is associated with this event type
68 70
      */
69
-    private Type(final boolean carrierMovementRequired) {
70
-      this.carrierMovementRequired = carrierMovementRequired;
71
+    private Type(final boolean voyageRequired) {
72
+      this.voyageRequired = voyageRequired;
71 73
     }
72 74
 
73 75
     /**
74
-     * @return True if a carrier movement association is required for this event type.
76
+     * @return True if a voyage association is required for this event type.
75 77
      */
76
-    public boolean requiresCarrierMovement() {
77
-      return carrierMovementRequired;
78
+    public boolean requiresVoyage() {
79
+      return voyageRequired;
78 80
     }
79 81
 
80 82
     /**
81
-     * @return True if a carrier movement association is prohibited for this event type.
83
+     * @return True if a voyage association is prohibited for this event type.
82 84
      */
83
-    public boolean prohibitsCarrierMovement() {
84
-      return !requiresCarrierMovement();
85
+    public boolean prohibitsVoyage() {
86
+      return !requiresVoyage();
85 87
     }
86 88
 
89
+    @Override
87 90
     public boolean sameValueAs(Type other) {
88
-      return false;  //To change body of implemented methods use File | Settings | File Templates.
91
+      return other != null && this.equals(other);
89 92
     }
90 93
 
94
+    @Override
91 95
     public Type copy() {
92
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
96
+      return this;
93 97
     }
94 98
   }
95 99
 
@@ -104,8 +108,8 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
104 108
   public HandlingEvent(Cargo cargo, Date completionTime, Date registrationTime, Type type,
105 109
                        Location location, Voyage voyage) {
106 110
     Validate.noNullElements(new Object[] {cargo, completionTime, registrationTime, type, location, voyage});
107
-    if (type.prohibitsCarrierMovement()) {
108
-      throw new IllegalArgumentException("Carrier movement is not allowed with event type " + type);
111
+    if (type.prohibitsVoyage()) {
112
+      throw new IllegalArgumentException("Voyage is not allowed with event type " + type);
109 113
     }
110 114
 
111 115
     this.voyage = voyage;
@@ -125,8 +129,8 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
125 129
    */
126 130
   public HandlingEvent(Cargo cargo, Date completionTime, Date registrationTime, Type type, Location location) {
127 131
     Validate.noNullElements(new Object[] {cargo, completionTime, registrationTime, type, location});
128
-    if (type.requiresCarrierMovement()) {
129
-      throw new IllegalArgumentException("Carrier movement is required for event type " + type);
132
+    if (type.requiresVoyage()) {
133
+      throw new IllegalArgumentException("Voyage is required for event type " + type);
130 134
     }
131 135
 
132 136
     this.completionTime = completionTime;
@@ -171,6 +175,7 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
171 175
     return sameEventAs(event);
172 176
   }
173 177
 
178
+  @Override
174 179
   public boolean sameEventAs(final HandlingEvent other) {
175 180
     return other != null && new EqualsBuilder().
176 181
       append(this.cargo, other.cargo).
@@ -192,8 +197,13 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
192 197
       toHashCode();
193 198
   }
194 199
 
195
-  // Needed by Hibernate
200
+  @Override
201
+  public String toString() {
202
+    return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
203
+  }
204
+
196 205
   HandlingEvent() {
206
+    // Needed by Hibernate
197 207
   }
198 208
 
199 209
 

+ 1
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/model/handling/HandlingEventFactory.java Bestand weergeven

@@ -52,6 +52,7 @@ public class HandlingEventFactory {
52 52
     
53 53
     if (location == null) throw new UnknownLocationException(unlocode);
54 54
 
55
+    // TODO parameterize
55 56
     final Date registrationTime = new Date();
56 57
 
57 58
     if (voyage == null) {

+ 1
- 1
dddsample/src/test/java/se/citerus/dddsample/domain/model/handling/HandlingEventFactoryTest.java Bestand weergeven

@@ -39,7 +39,7 @@ public class HandlingEventFactoryTest extends TestCase {
39 39
 
40 40
     trackingId = new TrackingId("ABC");
41 41
     RouteSpecification routeSpecification = new RouteSpecification(TOKYO, HELSINKI, new Date());
42
-    cargo = new Cargo(trackingId, routeSpecification);
42
+    cargo = new Cargo(trackingId, TOKYO, routeSpecification);
43 43
   }
44 44
 
45 45
   public void testCreateHandlingEventWithCarrierMovement() throws Exception {

+ 1
- 1
dddsample/src/test/java/se/citerus/dddsample/domain/model/handling/HandlingEventTest.java Bestand weergeven

@@ -19,7 +19,7 @@ public class HandlingEventTest extends TestCase {
19 19
   protected void setUp() throws Exception {
20 20
     TrackingId trackingId = new TrackingId("XYZ");
21 21
     RouteSpecification routeSpecification = new RouteSpecification(HONGKONG, NEWYORK, new Date());
22
-    cargo = new Cargo(trackingId, routeSpecification);
22
+    cargo = new Cargo(trackingId, HONGKONG, routeSpecification);
23 23
   }
24 24
 
25 25
   public void testNewWithCarrierMovement() throws Exception {