|
|
@@ -1,109 +1,33 @@
|
|
1
|
1
|
package se.citerus.dddsample.infrastructure.persistence.inmemory;
|
|
2
|
2
|
|
|
3
|
|
-import org.apache.commons.logging.Log;
|
|
4
|
|
-import org.apache.commons.logging.LogFactory;
|
|
5
|
|
-import se.citerus.dddsample.domain.model.cargo.Cargo;
|
|
6
|
3
|
import se.citerus.dddsample.domain.model.cargo.TrackingId;
|
|
7
|
4
|
import se.citerus.dddsample.domain.model.handling.HandlingEvent;
|
|
8
|
5
|
import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
|
|
9
|
6
|
import se.citerus.dddsample.domain.model.handling.HandlingHistory;
|
|
10
|
|
-import se.citerus.dddsample.domain.model.voyage.CarrierMovement;
|
|
11
|
|
-import se.citerus.dddsample.domain.model.voyage.VoyageRepository;
|
|
12
|
7
|
|
|
13
|
|
-import java.text.DateFormat;
|
|
14
|
|
-import java.text.ParseException;
|
|
15
|
|
-import java.text.SimpleDateFormat;
|
|
16
|
|
-import java.util.Date;
|
|
17
|
|
-import java.util.HashMap;
|
|
|
8
|
+import java.util.*;
|
|
18
|
9
|
|
|
19
|
10
|
public class HandlingEventRepositoryInMem implements HandlingEventRepository {
|
|
20
|
|
- private final Log logger = LogFactory.getLog(getClass());
|
|
21
|
|
- private final HashMap<String, HandlingEvent> eventDB = new HashMap<String, HandlingEvent>();
|
|
22
|
|
- private VoyageRepository voyageRepository;
|
|
23
|
|
-
|
|
24
|
|
- /**
|
|
25
|
|
- * Initilaze the in mem repository.
|
|
26
|
|
- * <p/>
|
|
27
|
|
- * SpringIoC will call this init-method after the bean has bean created and properties has been set.
|
|
28
|
|
- *
|
|
29
|
|
- * @throws ParseException
|
|
30
|
|
- */
|
|
31
|
|
- public void init() throws ParseException {
|
|
32
|
|
- /*
|
|
33
|
|
- // CargoXYZ
|
|
34
|
|
- final Cargo cargoXYZ = new Cargo(new TrackingId("XYZ"), STOCKHOLM, MELBOURNE);
|
|
35
|
|
- registerEvent(cargoXYZ, "2007-11-30", HandlingEvent.Type.RECEIVE, null);
|
|
36
|
|
-
|
|
37
|
|
- final CarrierMovement stockholmToHamburg = voyageRepository.find(new CarrierMovementId("SESTO_DEHAM"));
|
|
38
|
|
- registerEvent(cargoXYZ, "2007-12-01", HandlingEvent.Type.LOAD, stockholmToHamburg);
|
|
39
|
|
- registerEvent(cargoXYZ, "2007-12-02", HandlingEvent.Type.UNLOAD, stockholmToHamburg);
|
|
40
|
|
-
|
|
41
|
|
- final CarrierMovement hamburgToHongKong = voyageRepository.find(new CarrierMovementId("DEHAM_CNHKG"));
|
|
42
|
|
- registerEvent(cargoXYZ, "2007-12-03", HandlingEvent.Type.LOAD, hamburgToHongKong);
|
|
43
|
|
- registerEvent(cargoXYZ, "2007-12-05", HandlingEvent.Type.UNLOAD, hamburgToHongKong);
|
|
44
|
|
-
|
|
45
|
|
- //CargoZYX
|
|
46
|
|
- final Cargo cargoZYX = new Cargo(new TrackingId("ZYX"), MELBOURNE, STOCKHOLM);
|
|
47
|
|
- registerEvent(cargoZYX, "2007-12-09", HandlingEvent.Type.RECEIVE, null);
|
|
48
|
|
-
|
|
49
|
|
- final CarrierMovement melbourneToTokyo = voyageRepository.find(new CarrierMovementId("AUMEL_JPTOK"));
|
|
50
|
|
- registerEvent(cargoZYX, "2007-12-10", HandlingEvent.Type.LOAD, melbourneToTokyo);
|
|
51
|
|
- registerEvent(cargoZYX, "2007-12-12", HandlingEvent.Type.UNLOAD, melbourneToTokyo);
|
|
52
|
|
-
|
|
53
|
|
- final CarrierMovement tokyoToLosAngeles = voyageRepository.find(new CarrierMovementId("JPTOK_USLA"));
|
|
54
|
|
- registerEvent(cargoZYX, "2007-12-13", HandlingEvent.Type.LOAD, tokyoToLosAngeles);
|
|
55
|
|
-
|
|
56
|
|
- //CargoABC
|
|
57
|
|
- final Cargo cargoABC = new Cargo(new TrackingId("ABC"), STOCKHOLM, HELSINKI);
|
|
58
|
|
- registerEvent(cargoABC, "2008-01-01", HandlingEvent.Type.RECEIVE, null);
|
|
59
|
|
-
|
|
60
|
|
- final CarrierMovement stockholmToHelsinki = new CarrierMovement(
|
|
61
|
|
- new CarrierMovementId("CAR_001"), STOCKHOLM, HELSINKI, new Date(), new Date());
|
|
62
|
|
-
|
|
63
|
|
- registerEvent(cargoABC, "2008-01-02", HandlingEvent.Type.LOAD, stockholmToHelsinki);
|
|
64
|
|
- registerEvent(cargoABC, "2008-01-03", HandlingEvent.Type.UNLOAD, stockholmToHelsinki);
|
|
65
|
|
- registerEvent(cargoABC, "2008-01-05", HandlingEvent.Type.CLAIM, null);
|
|
66
|
|
-
|
|
67
|
|
- //CargoCBA
|
|
68
|
|
- final Cargo cargoCBA = new Cargo(new TrackingId("CBA"), HELSINKI, STOCKHOLM);
|
|
69
|
|
- registerEvent(cargoCBA, "2008-01-10", HandlingEvent.Type.RECEIVE, null);
|
|
70
|
|
- */
|
|
71
|
|
- }
|
|
72
|
|
-
|
|
73
|
|
- private void registerEvent(Cargo cargo, String date, HandlingEvent.Type type, CarrierMovement carrierMovement) throws ParseException {
|
|
74
|
|
- /*
|
|
75
|
|
- HandlingEvent event = new HandlingEvent(cargo, getCompletionTime(date), new Date(), type, null, carrierMovement);
|
|
76
|
|
- String id = cargo.trackingId() + "_" + type + "_" + date;
|
|
77
|
|
-
|
|
78
|
|
- logger.debug("Adding event " + id + "(" + event + ")");
|
|
79
|
|
- eventDB.put(id, event);
|
|
80
|
|
- */
|
|
81
|
|
- }
|
|
82
|
11
|
|
|
|
12
|
+ private Map<TrackingId, List<HandlingEvent>> eventMap = new HashMap<TrackingId, List<HandlingEvent>>();
|
|
83
|
13
|
|
|
|
14
|
+ @Override
|
|
84
|
15
|
public void store(HandlingEvent event) {
|
|
85
|
|
- eventDB.put(event.cargo().trackingId().idString(), event);
|
|
|
16
|
+ final TrackingId trackingId = event.cargo().trackingId();
|
|
|
17
|
+ List<HandlingEvent> list = eventMap.get(trackingId);
|
|
|
18
|
+ if (list == null) {
|
|
|
19
|
+ list = new ArrayList<HandlingEvent>();
|
|
|
20
|
+ eventMap.put(trackingId, list);
|
|
|
21
|
+ }
|
|
|
22
|
+ list.add(event);
|
|
86
|
23
|
}
|
|
87
|
24
|
|
|
88
|
25
|
@Override
|
|
89
|
26
|
public HandlingHistory lookupHandlingHistoryOfCargo(TrackingId trackingId) {
|
|
90
|
|
- return HandlingHistory.EMPTY;
|
|
91
|
|
- }
|
|
92
|
|
-
|
|
93
|
|
- /**
|
|
94
|
|
- * Parse an ISO 8601 (YYYY-MM-DD) String to Date
|
|
95
|
|
- *
|
|
96
|
|
- * @param isoFormat String to parse.
|
|
97
|
|
- * @return Created date instance.
|
|
98
|
|
- * @throws ParseException Thrown if parsing fails.
|
|
99
|
|
- */
|
|
100
|
|
- private Date getDate(final String isoFormat) throws ParseException {
|
|
101
|
|
- final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
102
|
|
- return dateFormat.parse(isoFormat);
|
|
103
|
|
- }
|
|
104
|
|
-
|
|
105
|
|
- public void setCarrierRepository(final VoyageRepository voyageRepository) {
|
|
106
|
|
- this.voyageRepository = voyageRepository;
|
|
|
27
|
+ List<HandlingEvent> events = eventMap.get(trackingId);
|
|
|
28
|
+ if (events == null) events = Collections.emptyList();
|
|
|
29
|
+
|
|
|
30
|
+ return new HandlingHistory(events);
|
|
107
|
31
|
}
|
|
108
|
32
|
|
|
109
|
33
|
}
|