Procházet zdrojové kódy

HandlingEvent now has a HandlingActivity field internally. HaAct is moved to shared package, as it's being used persistently by both Cargo and HandlingEvent.

Also improved a few toString() methods and added a CargoFactoryTest.
peter_backlund před 17 roky
rodič
revize
2ec76e5bb8

+ 2
- 2
dddsample/src/main/java/se/citerus/dddsample/domain/model/cargo/Cargo.java Zobrazit soubor

@@ -176,12 +176,12 @@ public class Cargo implements Entity<Cargo> {
176 176
    */
177 177
   @Override
178 178
   public int hashCode() {
179
-    return trackingId().hashCode();
179
+    return trackingId.hashCode();
180 180
   }
181 181
 
182 182
   @Override
183 183
   public String toString() {
184
-    return trackingId().toString();
184
+    return trackingId + " (" + routeSpecification + ")";
185 185
   }
186 186
 
187 187
   Cargo() {

+ 1
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/model/cargo/Delivery.java Zobrazit soubor

@@ -8,6 +8,7 @@ import static se.citerus.dddsample.domain.model.cargo.TransportStatus.*;
8 8
 import se.citerus.dddsample.domain.model.handling.HandlingEvent;
9 9
 import se.citerus.dddsample.domain.model.handling.HandlingHistory;
10 10
 import se.citerus.dddsample.domain.model.location.Location;
11
+import se.citerus.dddsample.domain.model.shared.HandlingActivity;
11 12
 import se.citerus.dddsample.domain.model.voyage.Voyage;
12 13
 import se.citerus.dddsample.domain.shared.DomainObjectUtils;
13 14
 import se.citerus.dddsample.domain.shared.ValueObject;

+ 5
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/model/cargo/RouteSpecification.java Zobrazit soubor

@@ -116,6 +116,11 @@ public class RouteSpecification extends AbstractSpecification<Itinerary> impleme
116 116
       toHashCode();
117 117
   }
118 118
 
119
+  @Override
120
+  public String toString() {
121
+    return origin + " to " + destination + " by " + arrivalDeadline;
122
+  }
123
+
119 124
   RouteSpecification() {
120 125
     // Needed by Hibernate
121 126
   }

+ 13
- 30
dddsample/src/main/java/se/citerus/dddsample/domain/model/handling/HandlingEvent.java Zobrazit soubor

@@ -5,6 +5,7 @@ import org.apache.commons.lang.builder.EqualsBuilder;
5 5
 import org.apache.commons.lang.builder.HashCodeBuilder;
6 6
 import se.citerus.dddsample.domain.model.cargo.Cargo;
7 7
 import se.citerus.dddsample.domain.model.location.Location;
8
+import se.citerus.dddsample.domain.model.shared.HandlingActivity;
8 9
 import se.citerus.dddsample.domain.model.voyage.Voyage;
9 10
 import se.citerus.dddsample.domain.shared.DomainEvent;
10 11
 import se.citerus.dddsample.domain.shared.DomainObjectUtils;
@@ -30,9 +31,7 @@ import java.util.Date;
30 31
  */
31 32
 public final class HandlingEvent implements DomainEvent<HandlingEvent> {
32 33
 
33
-  private Type type;
34
-  private Voyage voyage;
35
-  private Location location;
34
+  private HandlingActivity handlingActivity;
36 35
   private Date completionTime;
37 36
   private Date registrationTime;
38 37
   private Cargo cargo;
@@ -105,12 +104,10 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
105 104
       throw new IllegalArgumentException("Voyage is not allowed with event type " + type);
106 105
     }
107 106
 
108
-    this.voyage = voyage;
109 107
     this.completionTime = (Date) completionTime.clone();
110 108
     this.registrationTime = (Date) registrationTime.clone();
111
-    this.type = type;
112
-    this.location = location;
113 109
     this.cargo = cargo;
110
+    this.handlingActivity = new HandlingActivity(type, location, voyage);
114 111
   }
115 112
 
116 113
   /**
@@ -137,18 +134,16 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
137 134
 
138 135
     this.completionTime = (Date) completionTime.clone();
139 136
     this.registrationTime = (Date) registrationTime.clone();
140
-    this.type = type;
141
-    this.location = location;
142 137
     this.cargo = cargo;
143
-    this.voyage = null;
138
+    this.handlingActivity = new HandlingActivity(type, location);
144 139
   }
145 140
 
146 141
   public Type type() {
147
-    return this.type;
142
+    return handlingActivity.type();
148 143
   }
149 144
 
150 145
   public Voyage voyage() {
151
-    return DomainObjectUtils.nullSafe(this.voyage, Voyage.NONE);
146
+    return DomainObjectUtils.nullSafe(handlingActivity.voyage(), Voyage.NONE);
152 147
   }
153 148
 
154 149
   public Date completionTime() {
@@ -160,7 +155,7 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
160 155
   }
161 156
 
162 157
   public Location location() {
163
-    return this.location;
158
+    return handlingActivity.location();
164 159
   }
165 160
 
166 161
   public Cargo cargo() {
@@ -181,10 +176,8 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
181 176
   public boolean sameEventAs(final HandlingEvent other) {
182 177
     return other != null && new EqualsBuilder().
183 178
       append(this.cargo, other.cargo).
184
-      append(this.voyage, other.voyage).
185 179
       append(this.completionTime, other.completionTime).
186
-      append(this.location, other.location).
187
-      append(this.type, other.type).
180
+      append(this.handlingActivity, other.handlingActivity).
188 181
       isEquals();
189 182
   }
190 183
 
@@ -192,27 +185,17 @@ public final class HandlingEvent implements DomainEvent<HandlingEvent> {
192 185
   public int hashCode() {
193 186
     return new HashCodeBuilder().
194 187
       append(cargo).
195
-      append(voyage).
196 188
       append(completionTime).
197
-      append(location).
198
-      append(type).
189
+      append(handlingActivity).
199 190
       toHashCode();
200 191
   }
201 192
 
202 193
   @Override
203 194
   public String toString() {
204
-    final StringBuilder builder = new StringBuilder("\n--- Handling event ---\n").
205
-      append("Cargo: ").append(cargo.trackingId()).append("\n").
206
-      append("Type: ").append(type).append("\n").
207
-      append("Location: ").append(location.name()).append("\n").
208
-      append("Completed on: ").append(completionTime).append("\n").
209
-      append("Registered on: ").append(registrationTime).append("\n");
210
-
211
-    if (voyage != null) {
212
-      builder.append("Voyage: ").append(voyage.voyageNumber()).append("\n");
213
-    }
214
-
215
-    return builder.toString();
195
+    return "Cargo: " + cargo +
196
+      "\nActivity: " + handlingActivity +
197
+      "\nCompleted on: " + completionTime +
198
+      "\nRegistered on: " + registrationTime;
216 199
   }
217 200
 
218 201
   HandlingEvent() {

dddsample/src/main/java/se/citerus/dddsample/domain/model/cargo/HandlingActivity.java → dddsample/src/main/java/se/citerus/dddsample/domain/model/shared/HandlingActivity.java Zobrazit soubor

@@ -1,4 +1,4 @@
1
-package se.citerus.dddsample.domain.model.cargo;
1
+package se.citerus.dddsample.domain.model.shared;
2 2
 
3 3
 import org.apache.commons.lang.Validate;
4 4
 import org.apache.commons.lang.builder.EqualsBuilder;
@@ -12,12 +12,9 @@ import se.citerus.dddsample.domain.shared.ValueObject;
12 12
  * A handling activity represents how and where a cargo can be handled,
13 13
  * and can be used to express predictions about what is expected to
14 14
  * happen to a cargo in the future.
15
- *
16 15
  */
17 16
 public class HandlingActivity implements ValueObject<HandlingActivity> {
18 17
 
19
-  // TODO make HandlingActivity a part of HandlingEvent too? There is some overlap. 
20
-
21 18
   private HandlingEvent.Type type;
22 19
   private Location location;
23 20
   private Voyage voyage;
@@ -89,5 +86,5 @@ public class HandlingActivity implements ValueObject<HandlingActivity> {
89 86
   HandlingActivity() {
90 87
     // Needed by Hibernate
91 88
   }
92
-  
89
+
93 90
 }

+ 7
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/model/shared/package.html Zobrazit soubor

@@ -0,0 +1,7 @@
1
+<html>
2
+<body>
3
+<p>
4
+	Value objects that are shared between several aggregates.
5
+</p>
6
+</body>
7
+</html>

+ 1
- 1
dddsample/src/main/java/se/citerus/dddsample/interfaces/tracking/CargoTrackingViewAdapter.java Zobrazit soubor

@@ -3,9 +3,9 @@ package se.citerus.dddsample.interfaces.tracking;
3 3
 import org.springframework.context.MessageSource;
4 4
 import se.citerus.dddsample.domain.model.cargo.Cargo;
5 5
 import se.citerus.dddsample.domain.model.cargo.Delivery;
6
-import se.citerus.dddsample.domain.model.cargo.HandlingActivity;
7 6
 import se.citerus.dddsample.domain.model.handling.HandlingEvent;
8 7
 import se.citerus.dddsample.domain.model.location.Location;
8
+import se.citerus.dddsample.domain.model.shared.HandlingActivity;
9 9
 import se.citerus.dddsample.domain.model.voyage.Voyage;
10 10
 
11 11
 import java.text.SimpleDateFormat;

+ 13
- 10
dddsample/src/main/resources/se/citerus/dddsample/infrastructure/persistence/hibernate/HandlingEvent.hbm.xml Zobrazit soubor

@@ -1,24 +1,27 @@
1 1
 <?xml version="1.0"?>
2 2
 
3 3
 <!DOCTYPE hibernate-mapping PUBLIC
4
-  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5
-  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
+        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5
+        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
6 6
 
7 7
 <hibernate-mapping default-access="field">
8 8
   <class name="se.citerus.dddsample.domain.model.handling.HandlingEvent" table="HandlingEvent">
9 9
     <id name="id" column="id">
10 10
       <generator class="org.hibernate.id.IdentityGenerator"/>
11 11
     </id>
12
-    <many-to-one name="voyage" column="voyage_id" not-null="false" cascade="none" foreign-key="event_voyage_fk"/>
13
-    <many-to-one name="location" column="location_id" not-null="true" cascade="none" foreign-key="location_fk"/>
14 12
     <many-to-one name="cargo" column="cargo_id" not-null="true" cascade="none" foreign-key="cargo_fk"/>
15 13
     <property name="completionTime" column="completionTime" not-null="true"/>
16 14
     <property name="registrationTime" column="registrationTime" not-null="true"/>
17
-    <property name="type" column="type" not-null="true">
18
-      <type name="org.hibernate.type.EnumType">
19
-        <param name="enumClass">se.citerus.dddsample.domain.model.handling.HandlingEvent$Type</param>
20
-        <param name="type">12</param><!-- 12 is java.sql.Types.VARCHAR -->
21
-      </type>
22
-    </property>
15
+    <component name="handlingActivity">
16
+      <many-to-one name="voyage" column="voyage_id" not-null="false" cascade="none" foreign-key="event_voyage_fk"/>
17
+      <many-to-one name="location" column="location_id" not-null="true" cascade="none" foreign-key="location_fk"/>
18
+      <property name="type" column="type" not-null="true">
19
+        <type name="org.hibernate.type.EnumType">
20
+          <param name="enumClass">se.citerus.dddsample.domain.model.handling.HandlingEvent$Type</param>
21
+          <param name="type">12</param>
22
+          <!-- 12 is java.sql.Types.VARCHAR -->
23
+        </type>
24
+      </property>
25
+    </component>
23 26
   </class>
24 27
 </hibernate-mapping>

+ 31
- 0
dddsample/src/test/java/se/citerus/dddsample/domain/model/cargo/CargoFactoryTest.java Zobrazit soubor

@@ -0,0 +1,31 @@
1
+/**
2
+ * Purpose
3
+ * @author peter
4
+ * @created 2009-aug-08
5
+ * $Id$
6
+ */
7
+package se.citerus.dddsample.domain.model.cargo;
8
+
9
+import static org.junit.Assert.assertEquals;
10
+import static org.junit.Assert.assertNotNull;
11
+import org.junit.Test;
12
+import static se.citerus.dddsample.application.util.DateTestUtil.toDate;
13
+import static se.citerus.dddsample.domain.model.location.SampleLocations.HONGKONG;
14
+import static se.citerus.dddsample.domain.model.location.SampleLocations.ROTTERDAM;
15
+import se.citerus.dddsample.infrastructure.persistence.inmemory.CargoRepositoryInMem;
16
+import se.citerus.dddsample.infrastructure.persistence.inmemory.LocationRepositoryInMem;
17
+
18
+public class CargoFactoryTest {
19
+
20
+  @Test
21
+  public void createNewCargo() {
22
+    CargoFactory cargoFactory = new CargoFactory(new CargoRepositoryInMem(), new LocationRepositoryInMem());
23
+    Cargo cargo = cargoFactory.newCargo(
24
+      HONGKONG.unLocode(), ROTTERDAM.unLocode(), toDate("2009-07-01")
25
+    );
26
+    assertNotNull(cargo);
27
+    assertNotNull(cargo.trackingId());
28
+    assertEquals(cargo.routeSpecification(), new RouteSpecification(HONGKONG, ROTTERDAM, toDate("2009-07-01")));
29
+  }
30
+
31
+}

+ 1
- 0
dddsample/src/test/java/se/citerus/dddsample/scenario/CargoLifecycleScenarioTest.java Zobrazit soubor

@@ -13,6 +13,7 @@ import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.*;
13 13
 import se.citerus.dddsample.domain.model.location.Location;
14 14
 import se.citerus.dddsample.domain.model.location.LocationRepository;
15 15
 import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
16
+import se.citerus.dddsample.domain.model.shared.HandlingActivity;
16 17
 import static se.citerus.dddsample.domain.model.voyage.SampleVoyages.*;
17 18
 import se.citerus.dddsample.domain.model.voyage.Voyage;
18 19
 import static se.citerus.dddsample.domain.model.voyage.Voyage.NONE;