|
|
@@ -1,333 +1,340 @@
|
|
1
|
1
|
package se.citerus.dddsample.application.util;
|
|
2
|
2
|
|
|
3
|
|
-import org.hibernate.SessionFactory;
|
|
4
|
3
|
import org.hibernate.Session;
|
|
5
|
|
-import org.springframework.beans.factory.BeanFactoryUtils;
|
|
|
4
|
+import org.hibernate.SessionFactory;
|
|
6
|
5
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
7
|
6
|
import org.springframework.transaction.PlatformTransactionManager;
|
|
8
|
7
|
import org.springframework.transaction.TransactionStatus;
|
|
9
|
8
|
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
|
10
|
9
|
import org.springframework.transaction.support.TransactionTemplate;
|
|
11
|
|
-import org.springframework.web.context.WebApplicationContext;
|
|
12
|
|
-import org.springframework.web.context.support.WebApplicationContextUtils;
|
|
13
|
|
-import static se.citerus.dddsample.application.util.DateTestUtil.toDate;
|
|
14
|
10
|
import se.citerus.dddsample.domain.model.cargo.*;
|
|
15
|
11
|
import se.citerus.dddsample.domain.model.handling.*;
|
|
16
|
12
|
import se.citerus.dddsample.domain.model.location.Location;
|
|
17
|
13
|
import se.citerus.dddsample.domain.model.location.LocationRepository;
|
|
18
|
14
|
import se.citerus.dddsample.domain.model.location.SampleLocations;
|
|
19
|
|
-import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
|
|
20
|
|
-import static se.citerus.dddsample.domain.model.voyage.SampleVoyages.*;
|
|
21
|
15
|
import se.citerus.dddsample.domain.model.voyage.VoyageRepository;
|
|
22
|
16
|
|
|
23
|
|
-import javax.servlet.ServletContextEvent;
|
|
24
|
|
-import javax.servlet.ServletContextListener;
|
|
25
|
|
-import javax.sql.DataSource;
|
|
26
|
17
|
import java.sql.Timestamp;
|
|
27
|
18
|
import java.text.ParseException;
|
|
28
|
19
|
import java.text.SimpleDateFormat;
|
|
29
|
|
-import static java.util.Arrays.asList;
|
|
30
|
20
|
import java.util.Date;
|
|
31
|
21
|
|
|
|
22
|
+import static java.util.Arrays.asList;
|
|
|
23
|
+import static java.util.Objects.requireNonNull;
|
|
|
24
|
+import static se.citerus.dddsample.application.util.DateTestUtil.toDate;
|
|
|
25
|
+import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
|
|
|
26
|
+import static se.citerus.dddsample.domain.model.voyage.SampleVoyages.*;
|
|
|
27
|
+
|
|
32
|
28
|
/**
|
|
33
|
29
|
* Provides sample data.
|
|
34
|
30
|
*/
|
|
35
|
|
-public class SampleDataGenerator implements ServletContextListener {
|
|
36
|
|
-
|
|
37
|
|
- private static final Timestamp base;
|
|
38
|
|
- static {
|
|
39
|
|
- try {
|
|
40
|
|
- Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2008-01-01");
|
|
41
|
|
- base = new Timestamp(date.getTime() - 1000L * 60 * 60 * 24 * 100);
|
|
42
|
|
- } catch (ParseException e) {
|
|
43
|
|
- throw new RuntimeException(e);
|
|
44
|
|
- }
|
|
45
|
|
- }
|
|
46
|
|
-
|
|
47
|
|
- private static void loadHandlingEventData(JdbcTemplate jdbcTemplate) {
|
|
48
|
|
- String handlingEventSql =
|
|
49
|
|
- "insert into HandlingEvent (completionTime, registrationTime, type, location_id, voyage_id, cargo_id) " +
|
|
50
|
|
- "values (?, ?, ?, ?, ?, ?)";
|
|
51
|
|
-
|
|
52
|
|
- Object[][] handlingEventArgs = {
|
|
53
|
|
- //XYZ (SESTO-FIHEL-DEHAM-CNHKG-JPTOK-AUMEL)
|
|
54
|
|
- {ts(0), ts((0)), "RECEIVE", 1, null, 1},
|
|
55
|
|
- {ts((4)), ts((5)), "LOAD", 1, 1, 1},
|
|
56
|
|
- {ts((14)), ts((14)), "UNLOAD", 5, 1, 1},
|
|
57
|
|
- {ts((15)), ts((15)), "LOAD", 5, 1, 1},
|
|
58
|
|
- {ts((30)), ts((30)), "UNLOAD", 6, 1, 1},
|
|
59
|
|
- {ts((33)), ts((33)), "LOAD", 6, 1, 1},
|
|
60
|
|
- {ts((34)), ts((34)), "UNLOAD", 3, 1, 1},
|
|
61
|
|
- {ts((60)), ts((60)), "LOAD", 3, 1, 1},
|
|
62
|
|
- {ts((70)), ts((71)), "UNLOAD", 4, 1, 1},
|
|
63
|
|
- {ts((75)), ts((75)), "LOAD", 4, 1, 1},
|
|
64
|
|
- {ts((88)), ts((88)), "UNLOAD", 2, 1, 1},
|
|
65
|
|
- {ts((100)), ts((102)), "CLAIM", 2, null, 1},
|
|
66
|
|
-
|
|
67
|
|
- //ZYX (AUMEL - USCHI - DEHAM -)
|
|
68
|
|
- {ts((200)), ts((201)), "RECEIVE", 2, null, 3},
|
|
69
|
|
- {ts((202)), ts((202)), "LOAD", 2, 2, 3},
|
|
70
|
|
- {ts((208)), ts((208)), "UNLOAD", 7, 2, 3},
|
|
71
|
|
- {ts((212)), ts((212)), "LOAD", 7, 2, 3},
|
|
72
|
|
- {ts((230)), ts((230)), "UNLOAD", 6, 2, 3},
|
|
73
|
|
- {ts((235)), ts((235)), "LOAD", 6, 2, 3},
|
|
74
|
|
-
|
|
75
|
|
- //ABC
|
|
76
|
|
- {ts((20)), ts((21)), "CLAIM", 2, null, 2},
|
|
77
|
|
-
|
|
78
|
|
- //CBA
|
|
79
|
|
- {ts((0)), ts((1)), "RECEIVE", 2, null, 4},
|
|
80
|
|
- {ts((10)), ts((11)), "LOAD", 2, 2, 4},
|
|
81
|
|
- {ts((20)), ts((21)), "UNLOAD", 7, 2, 4},
|
|
82
|
|
-
|
|
83
|
|
- //FGH
|
|
84
|
|
- {ts(100), ts(160), "RECEIVE", 3, null, 5},
|
|
85
|
|
- {ts(150), ts(110), "LOAD", 3, 3, 5},
|
|
86
|
|
-
|
|
87
|
|
- // JKL
|
|
88
|
|
- {ts(200), ts(220), "RECEIVE", 6, null, 6},
|
|
89
|
|
- {ts(300), ts(330), "LOAD", 6, 3, 6},
|
|
90
|
|
- {ts(400), ts(440), "UNLOAD", 5, 3, 6} // Unexpected event
|
|
91
|
|
- };
|
|
92
|
|
- executeUpdate(jdbcTemplate, handlingEventSql, handlingEventArgs);
|
|
93
|
|
- }
|
|
94
|
|
-
|
|
95
|
|
- private static void loadCarrierMovementData(JdbcTemplate jdbcTemplate) {
|
|
96
|
|
- String voyageSql =
|
|
97
|
|
- "insert into Voyage (id, voyage_number) values (?, ?)";
|
|
98
|
|
- Object[][] voyageArgs = {
|
|
99
|
|
- {1,"0101"},
|
|
100
|
|
- {2,"0202"},
|
|
101
|
|
- {3,"0303"}
|
|
102
|
|
- };
|
|
103
|
|
- executeUpdate(jdbcTemplate, voyageSql, voyageArgs);
|
|
104
|
|
-
|
|
105
|
|
- String carrierMovementSql =
|
|
106
|
|
- "insert into CarrierMovement (id, voyage_id, departure_location_id, arrival_location_id, departure_time, arrival_time, cm_index) " +
|
|
107
|
|
- "values (?,?,?,?,?,?,?)";
|
|
108
|
|
-
|
|
109
|
|
- Object[][] carrierMovementArgs = {
|
|
110
|
|
- // SESTO - FIHEL - DEHAM - CNHKG - JPTOK - AUMEL (voyage 0101)
|
|
111
|
|
- {1,1,1,5,ts(1),ts(2),0},
|
|
112
|
|
- {2,1,5,6,ts(1),ts(2),1},
|
|
113
|
|
- {3,1,6,3,ts(1),ts(2),2},
|
|
114
|
|
- {4,1,3,4,ts(1),ts(2),3},
|
|
115
|
|
- {5,1,4,2,ts(1),ts(2),4},
|
|
116
|
|
-
|
|
117
|
|
- // AUMEL - USCHI - DEHAM - SESTO - FIHEL (voyage 0202)
|
|
118
|
|
- {7,2,2,7,ts(1),ts(2),0},
|
|
119
|
|
- {8,2,7,6,ts(1),ts(2),1},
|
|
120
|
|
- {9,2,6,1,ts(1),ts(2),2},
|
|
121
|
|
- {6,2,1,5,ts(1),ts(2),3},
|
|
122
|
|
-
|
|
123
|
|
- // CNHKG - AUMEL - FIHEL - DEHAM - SESTO - USCHI - JPTKO (voyage 0303)
|
|
124
|
|
- {10,3,3,2,ts(1),ts(2),0},
|
|
125
|
|
- {11,3,2,5,ts(1),ts(2),1},
|
|
126
|
|
- {12,3,6,1,ts(1),ts(2),2},
|
|
127
|
|
- {13,3,1,7,ts(1),ts(2),3},
|
|
128
|
|
- {14,3,7,4,ts(1),ts(2),4}
|
|
129
|
|
- };
|
|
130
|
|
- executeUpdate(jdbcTemplate, carrierMovementSql, carrierMovementArgs);
|
|
131
|
|
- }
|
|
132
|
|
-
|
|
133
|
|
- private static void loadCargoData(JdbcTemplate jdbcTemplate) {
|
|
134
|
|
- String cargoSql =
|
|
135
|
|
- "insert into Cargo (id, tracking_id, origin_id, spec_origin_id, spec_destination_id, spec_arrival_deadline, transport_status, current_voyage_id, last_known_location_id, is_misdirected, routing_status, calculated_at, unloaded_at_dest) " +
|
|
136
|
|
- "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
137
|
|
-
|
|
138
|
|
- Object[][] cargoArgs = {
|
|
139
|
|
- {1, "XYZ", 1, 1, 2, ts(10), "IN_PORT", null, 1, false, "ROUTED", ts(100), false},
|
|
140
|
|
- {2, "ABC", 1, 1, 5, ts(20), "IN_PORT", null, 1, false, "ROUTED", ts(100), false},
|
|
141
|
|
- {3, "ZYX", 2, 2, 1, ts(30), "IN_PORT", null, 1, false, "NOT_ROUTED", ts(100), false},
|
|
142
|
|
- {4, "CBA", 5, 5, 1, ts(40), "IN_PORT", null, 1, false, "MISROUTED", ts(100), false},
|
|
143
|
|
- {5, "FGH", 1, 3, 5, ts(50), "IN_PORT", null, 1, false, "ROUTED", ts(100), false}, // Cargo origin differs from spec origin
|
|
144
|
|
- {6, "JKL", 6, 6, 4, ts(60), "IN_PORT", null, 1, true, "ROUTED", ts(100), false}
|
|
145
|
|
- };
|
|
146
|
|
- executeUpdate(jdbcTemplate, cargoSql, cargoArgs);
|
|
147
|
|
- }
|
|
148
|
|
-
|
|
149
|
|
- private static void loadLocationData(JdbcTemplate jdbcTemplate) {
|
|
150
|
|
- String locationSql =
|
|
151
|
|
- "insert into Location (id, unlocode, name) " +
|
|
152
|
|
- "values (?, ?, ?)";
|
|
153
|
|
-
|
|
154
|
|
- Object[][] locationArgs = {
|
|
155
|
|
- {1, "SESTO", "Stockholm"},
|
|
156
|
|
- {2, "AUMEL", "Melbourne"},
|
|
157
|
|
- {3, "CNHKG", "Hongkong"},
|
|
158
|
|
- {4, "JPTOK", "Tokyo"},
|
|
159
|
|
- {5, "FIHEL", "Helsinki"},
|
|
160
|
|
- {6, "DEHAM", "Hamburg"},
|
|
161
|
|
- {7, "USCHI", "Chicago"}
|
|
162
|
|
- };
|
|
163
|
|
- executeUpdate(jdbcTemplate, locationSql, locationArgs);
|
|
164
|
|
- }
|
|
165
|
|
-
|
|
166
|
|
- private static void loadItineraryData(JdbcTemplate jdbcTemplate) {
|
|
167
|
|
- String legSql =
|
|
168
|
|
- "insert into Leg (id, cargo_id, voyage_id, load_location_id, unload_location_id, load_time, unload_time, leg_index) " +
|
|
169
|
|
- "values (?,?,?,?,?,?,?,?)";
|
|
170
|
|
-
|
|
171
|
|
- Object [][] legArgs = {
|
|
172
|
|
- // Cargo 5: Hongkong - Melbourne - Stockholm - Helsinki
|
|
173
|
|
- {1,5,1,3,2,ts(1),ts(2),0},
|
|
174
|
|
- {2,5,1,2,1,ts(3),ts(4),1},
|
|
175
|
|
- {3,5,1,1,5,ts(4),ts(5),2},
|
|
176
|
|
- // Cargo 6: Hamburg - Stockholm - Chicago - Tokyo
|
|
177
|
|
- {4,6,2,6,1,ts(1),ts(2),0},
|
|
178
|
|
- {5,6,2,1,7,ts(3),ts(4),1},
|
|
179
|
|
- {6,6,2,7,4,ts(5),ts(6),2}
|
|
180
|
|
- };
|
|
181
|
|
- executeUpdate(jdbcTemplate, legSql, legArgs);
|
|
182
|
|
- }
|
|
183
|
|
-
|
|
184
|
|
- public void contextInitialized(ServletContextEvent event) {
|
|
185
|
|
- WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
|
|
186
|
|
- DataSource dataSource = (DataSource) BeanFactoryUtils.beanOfType(context, DataSource.class);
|
|
187
|
|
- PlatformTransactionManager transactionManager = (PlatformTransactionManager) BeanFactoryUtils.beanOfType(context, PlatformTransactionManager.class);
|
|
188
|
|
- TransactionTemplate tt = new TransactionTemplate(transactionManager);
|
|
189
|
|
- //loadSampleData(new JdbcTemplate(dataSource), tt);
|
|
190
|
|
-
|
|
191
|
|
-
|
|
192
|
|
- SessionFactory sf = (SessionFactory) BeanFactoryUtils.beanOfType(context, SessionFactory.class);
|
|
193
|
|
- HandlingEventFactory handlingEventFactory = new HandlingEventFactory(
|
|
194
|
|
- getBean(context, CargoRepository.class),
|
|
195
|
|
- getBean(context, VoyageRepository.class),
|
|
196
|
|
- getBean(context, LocationRepository.class));
|
|
197
|
|
- loadHibernateData(tt, sf, handlingEventFactory, getBean(context, HandlingEventRepository.class));
|
|
198
|
|
- }
|
|
199
|
|
-
|
|
200
|
|
- private <T> T getBean(WebApplicationContext context, Class<T> cls) {
|
|
201
|
|
- return (T) BeanFactoryUtils.beanOfType(context, cls);
|
|
202
|
|
- }
|
|
203
|
|
-
|
|
204
|
|
- public static void loadHibernateData(TransactionTemplate tt, final SessionFactory sf, final HandlingEventFactory handlingEventFactory, final HandlingEventRepository handlingEventRepository) {
|
|
205
|
|
- System.out.println("*** Loading Hibernate data ***");
|
|
206
|
|
- tt.execute(new TransactionCallbackWithoutResult() {
|
|
207
|
|
- @Override
|
|
208
|
|
- protected void doInTransactionWithoutResult(TransactionStatus status) {
|
|
209
|
|
- Session session = sf.getCurrentSession();
|
|
210
|
|
-
|
|
211
|
|
- for (Location location : SampleLocations.getAll()) {
|
|
212
|
|
- session.save(location);
|
|
213
|
|
- }
|
|
|
31
|
+public class SampleDataGenerator {
|
|
|
32
|
+
|
|
|
33
|
+ private static final Timestamp base;
|
|
214
|
34
|
|
|
215
|
|
- session.save(HONGKONG_TO_NEW_YORK);
|
|
216
|
|
- session.save(NEW_YORK_TO_DALLAS);
|
|
217
|
|
- session.save(DALLAS_TO_HELSINKI);
|
|
218
|
|
- session.save(HELSINKI_TO_HONGKONG);
|
|
219
|
|
- session.save(DALLAS_TO_HELSINKI_ALT);
|
|
220
|
|
-
|
|
221
|
|
- RouteSpecification routeSpecification = new RouteSpecification(HONGKONG, HELSINKI, toDate("2009-03-15"));
|
|
222
|
|
- TrackingId trackingId = new TrackingId("ABC123");
|
|
223
|
|
- Cargo abc123 = new Cargo(trackingId, routeSpecification);
|
|
224
|
|
-
|
|
225
|
|
- Itinerary itinerary = new Itinerary(asList(
|
|
226
|
|
- new Leg(HONGKONG_TO_NEW_YORK, HONGKONG, NEWYORK, toDate("2009-03-02"), toDate("2009-03-05")),
|
|
227
|
|
- new Leg(NEW_YORK_TO_DALLAS, NEWYORK, DALLAS, toDate("2009-03-06"), toDate("2009-03-08")),
|
|
228
|
|
- new Leg(DALLAS_TO_HELSINKI, DALLAS, HELSINKI, toDate("2009-03-09"), toDate("2009-03-12"))
|
|
229
|
|
- ));
|
|
230
|
|
- abc123.assignToRoute(itinerary);
|
|
231
|
|
-
|
|
232
|
|
- session.save(abc123);
|
|
233
|
|
-
|
|
|
35
|
+ static {
|
|
234
|
36
|
try {
|
|
235
|
|
- HandlingEvent event1 = handlingEventFactory.createHandlingEvent(
|
|
236
|
|
- new Date(), toDate("2009-03-01"), trackingId, null, HONGKONG.unLocode(), HandlingEvent.Type.RECEIVE
|
|
237
|
|
- );
|
|
238
|
|
- session.save(event1);
|
|
239
|
|
-
|
|
240
|
|
- HandlingEvent event2 = handlingEventFactory.createHandlingEvent(
|
|
241
|
|
- new Date(), toDate("2009-03-02"), trackingId, HONGKONG_TO_NEW_YORK.voyageNumber(), HONGKONG.unLocode(), HandlingEvent.Type.LOAD
|
|
242
|
|
- );
|
|
243
|
|
- session.save(event2);
|
|
244
|
|
-
|
|
245
|
|
- HandlingEvent event3 = handlingEventFactory.createHandlingEvent(
|
|
246
|
|
- new Date(), toDate("2009-03-05"), trackingId, HONGKONG_TO_NEW_YORK.voyageNumber(), NEWYORK.unLocode(), HandlingEvent.Type.UNLOAD
|
|
247
|
|
- );
|
|
248
|
|
- session.save(event3);
|
|
249
|
|
- } catch (CannotCreateHandlingEventException e) {
|
|
250
|
|
- throw new RuntimeException(e);
|
|
|
37
|
+ Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2008-01-01");
|
|
|
38
|
+ base = new Timestamp(date.getTime() - 1000L * 60 * 60 * 24 * 100);
|
|
|
39
|
+ } catch (ParseException e) {
|
|
|
40
|
+ throw new RuntimeException(e);
|
|
251
|
41
|
}
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ private final PlatformTransactionManager transactionManager;
|
|
|
45
|
+ private final SessionFactory sf;
|
|
|
46
|
+ private final CargoRepository cargoRepository;
|
|
|
47
|
+ private final VoyageRepository voyageRepository;
|
|
|
48
|
+ private final LocationRepository locationRepository;
|
|
|
49
|
+ private final HandlingEventRepository handlingEventRepository;
|
|
|
50
|
+
|
|
|
51
|
+ public SampleDataGenerator(PlatformTransactionManager transactionManager,
|
|
|
52
|
+ SessionFactory sf,
|
|
|
53
|
+ CargoRepository cargoRepository,
|
|
|
54
|
+ VoyageRepository voyageRepository,
|
|
|
55
|
+ LocationRepository locationRepository,
|
|
|
56
|
+ HandlingEventRepository handlingEventRepository) {
|
|
|
57
|
+ this.transactionManager = requireNonNull(transactionManager);
|
|
|
58
|
+ this.sf = requireNonNull(sf);
|
|
|
59
|
+ this.cargoRepository = requireNonNull(cargoRepository);
|
|
|
60
|
+ this.voyageRepository = requireNonNull(voyageRepository);
|
|
|
61
|
+ this.locationRepository = requireNonNull(locationRepository);
|
|
|
62
|
+ this.handlingEventRepository = requireNonNull(handlingEventRepository);
|
|
|
63
|
+ }
|
|
252
|
64
|
|
|
253
|
|
- HandlingHistory handlingHistory = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId);
|
|
254
|
|
- abc123.deriveDeliveryProgress(handlingHistory);
|
|
|
65
|
+ private static void loadHandlingEventData(JdbcTemplate jdbcTemplate) {
|
|
|
66
|
+ String handlingEventSql =
|
|
|
67
|
+ "insert into HandlingEvent (completionTime, registrationTime, type, location_id, voyage_id, cargo_id) " +
|
|
|
68
|
+ "values (?, ?, ?, ?, ?, ?)";
|
|
|
69
|
+
|
|
|
70
|
+ Object[][] handlingEventArgs = {
|
|
|
71
|
+ //XYZ (SESTO-FIHEL-DEHAM-CNHKG-JPTOK-AUMEL)
|
|
|
72
|
+ {ts(0), ts((0)), "RECEIVE", 1, null, 1},
|
|
|
73
|
+ {ts((4)), ts((5)), "LOAD", 1, 1, 1},
|
|
|
74
|
+ {ts((14)), ts((14)), "UNLOAD", 5, 1, 1},
|
|
|
75
|
+ {ts((15)), ts((15)), "LOAD", 5, 1, 1},
|
|
|
76
|
+ {ts((30)), ts((30)), "UNLOAD", 6, 1, 1},
|
|
|
77
|
+ {ts((33)), ts((33)), "LOAD", 6, 1, 1},
|
|
|
78
|
+ {ts((34)), ts((34)), "UNLOAD", 3, 1, 1},
|
|
|
79
|
+ {ts((60)), ts((60)), "LOAD", 3, 1, 1},
|
|
|
80
|
+ {ts((70)), ts((71)), "UNLOAD", 4, 1, 1},
|
|
|
81
|
+ {ts((75)), ts((75)), "LOAD", 4, 1, 1},
|
|
|
82
|
+ {ts((88)), ts((88)), "UNLOAD", 2, 1, 1},
|
|
|
83
|
+ {ts((100)), ts((102)), "CLAIM", 2, null, 1},
|
|
|
84
|
+
|
|
|
85
|
+ //ZYX (AUMEL - USCHI - DEHAM -)
|
|
|
86
|
+ {ts((200)), ts((201)), "RECEIVE", 2, null, 3},
|
|
|
87
|
+ {ts((202)), ts((202)), "LOAD", 2, 2, 3},
|
|
|
88
|
+ {ts((208)), ts((208)), "UNLOAD", 7, 2, 3},
|
|
|
89
|
+ {ts((212)), ts((212)), "LOAD", 7, 2, 3},
|
|
|
90
|
+ {ts((230)), ts((230)), "UNLOAD", 6, 2, 3},
|
|
|
91
|
+ {ts((235)), ts((235)), "LOAD", 6, 2, 3},
|
|
|
92
|
+
|
|
|
93
|
+ //ABC
|
|
|
94
|
+ {ts((20)), ts((21)), "CLAIM", 2, null, 2},
|
|
|
95
|
+
|
|
|
96
|
+ //CBA
|
|
|
97
|
+ {ts((0)), ts((1)), "RECEIVE", 2, null, 4},
|
|
|
98
|
+ {ts((10)), ts((11)), "LOAD", 2, 2, 4},
|
|
|
99
|
+ {ts((20)), ts((21)), "UNLOAD", 7, 2, 4},
|
|
|
100
|
+
|
|
|
101
|
+ //FGH
|
|
|
102
|
+ {ts(100), ts(160), "RECEIVE", 3, null, 5},
|
|
|
103
|
+ {ts(150), ts(110), "LOAD", 3, 3, 5},
|
|
|
104
|
+
|
|
|
105
|
+ // JKL
|
|
|
106
|
+ {ts(200), ts(220), "RECEIVE", 6, null, 6},
|
|
|
107
|
+ {ts(300), ts(330), "LOAD", 6, 3, 6},
|
|
|
108
|
+ {ts(400), ts(440), "UNLOAD", 5, 3, 6} // Unexpected event
|
|
|
109
|
+ };
|
|
|
110
|
+ executeUpdate(jdbcTemplate, handlingEventSql, handlingEventArgs);
|
|
|
111
|
+ }
|
|
255
|
112
|
|
|
256
|
|
- session.update(abc123);
|
|
|
113
|
+ private static void loadCarrierMovementData(JdbcTemplate jdbcTemplate) {
|
|
|
114
|
+ String voyageSql =
|
|
|
115
|
+ "insert into Voyage (id, voyage_number) values (?, ?)";
|
|
|
116
|
+ Object[][] voyageArgs = {
|
|
|
117
|
+ {1, "0101"},
|
|
|
118
|
+ {2, "0202"},
|
|
|
119
|
+ {3, "0303"}
|
|
|
120
|
+ };
|
|
|
121
|
+ executeUpdate(jdbcTemplate, voyageSql, voyageArgs);
|
|
|
122
|
+
|
|
|
123
|
+ String carrierMovementSql =
|
|
|
124
|
+ "insert into CarrierMovement (id, voyage_id, departure_location_id, arrival_location_id, departure_time, arrival_time, cm_index) " +
|
|
|
125
|
+ "values (?,?,?,?,?,?,?)";
|
|
|
126
|
+
|
|
|
127
|
+ Object[][] carrierMovementArgs = {
|
|
|
128
|
+ // SESTO - FIHEL - DEHAM - CNHKG - JPTOK - AUMEL (voyage 0101)
|
|
|
129
|
+ {1, 1, 1, 5, ts(1), ts(2), 0},
|
|
|
130
|
+ {2, 1, 5, 6, ts(1), ts(2), 1},
|
|
|
131
|
+ {3, 1, 6, 3, ts(1), ts(2), 2},
|
|
|
132
|
+ {4, 1, 3, 4, ts(1), ts(2), 3},
|
|
|
133
|
+ {5, 1, 4, 2, ts(1), ts(2), 4},
|
|
|
134
|
+
|
|
|
135
|
+ // AUMEL - USCHI - DEHAM - SESTO - FIHEL (voyage 0202)
|
|
|
136
|
+ {7, 2, 2, 7, ts(1), ts(2), 0},
|
|
|
137
|
+ {8, 2, 7, 6, ts(1), ts(2), 1},
|
|
|
138
|
+ {9, 2, 6, 1, ts(1), ts(2), 2},
|
|
|
139
|
+ {6, 2, 1, 5, ts(1), ts(2), 3},
|
|
|
140
|
+
|
|
|
141
|
+ // CNHKG - AUMEL - FIHEL - DEHAM - SESTO - USCHI - JPTKO (voyage 0303)
|
|
|
142
|
+ {10, 3, 3, 2, ts(1), ts(2), 0},
|
|
|
143
|
+ {11, 3, 2, 5, ts(1), ts(2), 1},
|
|
|
144
|
+ {12, 3, 6, 1, ts(1), ts(2), 2},
|
|
|
145
|
+ {13, 3, 1, 7, ts(1), ts(2), 3},
|
|
|
146
|
+ {14, 3, 7, 4, ts(1), ts(2), 4}
|
|
|
147
|
+ };
|
|
|
148
|
+ executeUpdate(jdbcTemplate, carrierMovementSql, carrierMovementArgs);
|
|
|
149
|
+ }
|
|
257
|
150
|
|
|
258
|
|
- // Cargo JKL567
|
|
|
151
|
+ private static void loadCargoData(JdbcTemplate jdbcTemplate) {
|
|
|
152
|
+ String cargoSql =
|
|
|
153
|
+ "insert into Cargo (id, tracking_id, origin_id, spec_origin_id, spec_destination_id, spec_arrival_deadline, transport_status, current_voyage_id, last_known_location_id, is_misdirected, routing_status, calculated_at, unloaded_at_dest) " +
|
|
|
154
|
+ "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
|
155
|
+
|
|
|
156
|
+ Object[][] cargoArgs = {
|
|
|
157
|
+ {1, "XYZ", 1, 1, 2, ts(10), "IN_PORT", null, 1, false, "ROUTED", ts(100), false},
|
|
|
158
|
+ {2, "ABC", 1, 1, 5, ts(20), "IN_PORT", null, 1, false, "ROUTED", ts(100), false},
|
|
|
159
|
+ {3, "ZYX", 2, 2, 1, ts(30), "IN_PORT", null, 1, false, "NOT_ROUTED", ts(100), false},
|
|
|
160
|
+ {4, "CBA", 5, 5, 1, ts(40), "IN_PORT", null, 1, false, "MISROUTED", ts(100), false},
|
|
|
161
|
+ {5, "FGH", 1, 3, 5, ts(50), "IN_PORT", null, 1, false, "ROUTED", ts(100), false}, // Cargo origin differs from spec origin
|
|
|
162
|
+ {6, "JKL", 6, 6, 4, ts(60), "IN_PORT", null, 1, true, "ROUTED", ts(100), false}
|
|
|
163
|
+ };
|
|
|
164
|
+ executeUpdate(jdbcTemplate, cargoSql, cargoArgs);
|
|
|
165
|
+ }
|
|
259
|
166
|
|
|
260
|
|
- RouteSpecification routeSpecification1 = new RouteSpecification(HANGZOU, STOCKHOLM, toDate("2009-03-18"));
|
|
261
|
|
- TrackingId trackingId1 = new TrackingId("JKL567");
|
|
262
|
|
- Cargo jkl567 = new Cargo(trackingId1, routeSpecification1);
|
|
|
167
|
+ private static void loadLocationData(JdbcTemplate jdbcTemplate) {
|
|
|
168
|
+ String locationSql =
|
|
|
169
|
+ "insert into Location (id, unlocode, name) " +
|
|
|
170
|
+ "values (?, ?, ?)";
|
|
|
171
|
+
|
|
|
172
|
+ Object[][] locationArgs = {
|
|
|
173
|
+ {1, "SESTO", "Stockholm"},
|
|
|
174
|
+ {2, "AUMEL", "Melbourne"},
|
|
|
175
|
+ {3, "CNHKG", "Hongkong"},
|
|
|
176
|
+ {4, "JPTOK", "Tokyo"},
|
|
|
177
|
+ {5, "FIHEL", "Helsinki"},
|
|
|
178
|
+ {6, "DEHAM", "Hamburg"},
|
|
|
179
|
+ {7, "USCHI", "Chicago"}
|
|
|
180
|
+ };
|
|
|
181
|
+ executeUpdate(jdbcTemplate, locationSql, locationArgs);
|
|
|
182
|
+ }
|
|
263
|
183
|
|
|
264
|
|
- Itinerary itinerary1 = new Itinerary(asList(
|
|
265
|
|
- new Leg(HONGKONG_TO_NEW_YORK, HANGZOU, NEWYORK, toDate("2009-03-03"), toDate("2009-03-05")),
|
|
266
|
|
- new Leg(NEW_YORK_TO_DALLAS, NEWYORK, DALLAS, toDate("2009-03-06"), toDate("2009-03-08")),
|
|
267
|
|
- new Leg(DALLAS_TO_HELSINKI, DALLAS, STOCKHOLM, toDate("2009-03-09"), toDate("2009-03-11"))
|
|
268
|
|
- ));
|
|
269
|
|
- jkl567.assignToRoute(itinerary1);
|
|
|
184
|
+ private static void loadItineraryData(JdbcTemplate jdbcTemplate) {
|
|
|
185
|
+ String legSql =
|
|
|
186
|
+ "insert into Leg (id, cargo_id, voyage_id, load_location_id, unload_location_id, load_time, unload_time, leg_index) " +
|
|
|
187
|
+ "values (?,?,?,?,?,?,?,?)";
|
|
|
188
|
+
|
|
|
189
|
+ Object[][] legArgs = {
|
|
|
190
|
+ // Cargo 5: Hongkong - Melbourne - Stockholm - Helsinki
|
|
|
191
|
+ {1, 5, 1, 3, 2, ts(1), ts(2), 0},
|
|
|
192
|
+ {2, 5, 1, 2, 1, ts(3), ts(4), 1},
|
|
|
193
|
+ {3, 5, 1, 1, 5, ts(4), ts(5), 2},
|
|
|
194
|
+ // Cargo 6: Hamburg - Stockholm - Chicago - Tokyo
|
|
|
195
|
+ {4, 6, 2, 6, 1, ts(1), ts(2), 0},
|
|
|
196
|
+ {5, 6, 2, 1, 7, ts(3), ts(4), 1},
|
|
|
197
|
+ {6, 6, 2, 7, 4, ts(5), ts(6), 2}
|
|
|
198
|
+ };
|
|
|
199
|
+ executeUpdate(jdbcTemplate, legSql, legArgs);
|
|
|
200
|
+ }
|
|
270
|
201
|
|
|
271
|
|
- session.save(jkl567);
|
|
|
202
|
+ public void generate() {
|
|
|
203
|
+ TransactionTemplate tt = new TransactionTemplate(transactionManager);
|
|
|
204
|
+ //loadSampleData(new JdbcTemplate(dataSource), tt);
|
|
272
|
205
|
|
|
273
|
|
- try {
|
|
274
|
|
- HandlingEvent event1 = handlingEventFactory.createHandlingEvent(
|
|
275
|
|
- new Date(), toDate("2009-03-01"), trackingId1, null, HANGZOU.unLocode(), HandlingEvent.Type.RECEIVE
|
|
276
|
|
- );
|
|
277
|
|
- session.save(event1);
|
|
278
|
|
-
|
|
279
|
|
- HandlingEvent event2 = handlingEventFactory.createHandlingEvent(
|
|
280
|
|
- new Date(), toDate("2009-03-03"), trackingId1, HONGKONG_TO_NEW_YORK.voyageNumber(), HANGZOU.unLocode(), HandlingEvent.Type.LOAD
|
|
281
|
|
- );
|
|
282
|
|
- session.save(event2);
|
|
283
|
|
-
|
|
284
|
|
- HandlingEvent event3 = handlingEventFactory.createHandlingEvent(
|
|
285
|
|
- new Date(), toDate("2009-03-05"), trackingId1, HONGKONG_TO_NEW_YORK.voyageNumber(), NEWYORK.unLocode(), HandlingEvent.Type.UNLOAD
|
|
286
|
|
- );
|
|
287
|
|
- session.save(event3);
|
|
288
|
|
-
|
|
289
|
|
- HandlingEvent event4 = handlingEventFactory.createHandlingEvent(
|
|
290
|
|
- new Date(), toDate("2009-03-06"), trackingId1, HONGKONG_TO_NEW_YORK.voyageNumber(), NEWYORK.unLocode(), HandlingEvent.Type.LOAD
|
|
291
|
|
- );
|
|
292
|
|
- session.save(event4);
|
|
293
|
|
-
|
|
294
|
|
- } catch (CannotCreateHandlingEventException e) {
|
|
295
|
|
- throw new RuntimeException(e);
|
|
296
|
|
- }
|
|
|
206
|
+ HandlingEventFactory handlingEventFactory = new HandlingEventFactory(
|
|
|
207
|
+ cargoRepository,
|
|
|
208
|
+ voyageRepository,
|
|
|
209
|
+ locationRepository);
|
|
|
210
|
+ loadHibernateData(tt, sf, handlingEventFactory, handlingEventRepository);
|
|
|
211
|
+ }
|
|
|
212
|
+
|
|
|
213
|
+ public static void loadHibernateData(TransactionTemplate tt, final SessionFactory sf, final HandlingEventFactory handlingEventFactory, final HandlingEventRepository handlingEventRepository) {
|
|
|
214
|
+ System.out.println("*** Loading Hibernate data ***");
|
|
|
215
|
+ tt.execute(new TransactionCallbackWithoutResult() {
|
|
|
216
|
+ @Override
|
|
|
217
|
+ protected void doInTransactionWithoutResult(TransactionStatus status) {
|
|
|
218
|
+ Session session = sf.getCurrentSession();
|
|
|
219
|
+
|
|
|
220
|
+ for (Location location : SampleLocations.getAll()) {
|
|
|
221
|
+ session.save(location);
|
|
|
222
|
+ }
|
|
|
223
|
+
|
|
|
224
|
+ session.save(HONGKONG_TO_NEW_YORK);
|
|
|
225
|
+ session.save(NEW_YORK_TO_DALLAS);
|
|
|
226
|
+ session.save(DALLAS_TO_HELSINKI);
|
|
|
227
|
+ session.save(HELSINKI_TO_HONGKONG);
|
|
|
228
|
+ session.save(DALLAS_TO_HELSINKI_ALT);
|
|
|
229
|
+
|
|
|
230
|
+ RouteSpecification routeSpecification = new RouteSpecification(HONGKONG, HELSINKI, toDate("2009-03-15"));
|
|
|
231
|
+ TrackingId trackingId = new TrackingId("ABC123");
|
|
|
232
|
+ Cargo abc123 = new Cargo(trackingId, routeSpecification);
|
|
|
233
|
+
|
|
|
234
|
+ Itinerary itinerary = new Itinerary(asList(
|
|
|
235
|
+ new Leg(HONGKONG_TO_NEW_YORK, HONGKONG, NEWYORK, toDate("2009-03-02"), toDate("2009-03-05")),
|
|
|
236
|
+ new Leg(NEW_YORK_TO_DALLAS, NEWYORK, DALLAS, toDate("2009-03-06"), toDate("2009-03-08")),
|
|
|
237
|
+ new Leg(DALLAS_TO_HELSINKI, DALLAS, HELSINKI, toDate("2009-03-09"), toDate("2009-03-12"))
|
|
|
238
|
+ ));
|
|
|
239
|
+ abc123.assignToRoute(itinerary);
|
|
|
240
|
+
|
|
|
241
|
+ session.save(abc123);
|
|
|
242
|
+
|
|
|
243
|
+ try {
|
|
|
244
|
+ HandlingEvent event1 = handlingEventFactory.createHandlingEvent(
|
|
|
245
|
+ new Date(), toDate("2009-03-01"), trackingId, null, HONGKONG.unLocode(), HandlingEvent.Type.RECEIVE
|
|
|
246
|
+ );
|
|
|
247
|
+ session.save(event1);
|
|
|
248
|
+
|
|
|
249
|
+ HandlingEvent event2 = handlingEventFactory.createHandlingEvent(
|
|
|
250
|
+ new Date(), toDate("2009-03-02"), trackingId, HONGKONG_TO_NEW_YORK.voyageNumber(), HONGKONG.unLocode(), HandlingEvent.Type.LOAD
|
|
|
251
|
+ );
|
|
|
252
|
+ session.save(event2);
|
|
|
253
|
+
|
|
|
254
|
+ HandlingEvent event3 = handlingEventFactory.createHandlingEvent(
|
|
|
255
|
+ new Date(), toDate("2009-03-05"), trackingId, HONGKONG_TO_NEW_YORK.voyageNumber(), NEWYORK.unLocode(), HandlingEvent.Type.UNLOAD
|
|
|
256
|
+ );
|
|
|
257
|
+ session.save(event3);
|
|
|
258
|
+ } catch (CannotCreateHandlingEventException e) {
|
|
|
259
|
+ throw new RuntimeException(e);
|
|
|
260
|
+ }
|
|
|
261
|
+
|
|
|
262
|
+ HandlingHistory handlingHistory = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId);
|
|
|
263
|
+ abc123.deriveDeliveryProgress(handlingHistory);
|
|
|
264
|
+
|
|
|
265
|
+ session.update(abc123);
|
|
|
266
|
+
|
|
|
267
|
+ // Cargo JKL567
|
|
|
268
|
+
|
|
|
269
|
+ RouteSpecification routeSpecification1 = new RouteSpecification(HANGZOU, STOCKHOLM, toDate("2009-03-18"));
|
|
|
270
|
+ TrackingId trackingId1 = new TrackingId("JKL567");
|
|
|
271
|
+ Cargo jkl567 = new Cargo(trackingId1, routeSpecification1);
|
|
|
272
|
+
|
|
|
273
|
+ Itinerary itinerary1 = new Itinerary(asList(
|
|
|
274
|
+ new Leg(HONGKONG_TO_NEW_YORK, HANGZOU, NEWYORK, toDate("2009-03-03"), toDate("2009-03-05")),
|
|
|
275
|
+ new Leg(NEW_YORK_TO_DALLAS, NEWYORK, DALLAS, toDate("2009-03-06"), toDate("2009-03-08")),
|
|
|
276
|
+ new Leg(DALLAS_TO_HELSINKI, DALLAS, STOCKHOLM, toDate("2009-03-09"), toDate("2009-03-11"))
|
|
|
277
|
+ ));
|
|
|
278
|
+ jkl567.assignToRoute(itinerary1);
|
|
|
279
|
+
|
|
|
280
|
+ session.save(jkl567);
|
|
|
281
|
+
|
|
|
282
|
+ try {
|
|
|
283
|
+ HandlingEvent event1 = handlingEventFactory.createHandlingEvent(
|
|
|
284
|
+ new Date(), toDate("2009-03-01"), trackingId1, null, HANGZOU.unLocode(), HandlingEvent.Type.RECEIVE
|
|
|
285
|
+ );
|
|
|
286
|
+ session.save(event1);
|
|
|
287
|
+
|
|
|
288
|
+ HandlingEvent event2 = handlingEventFactory.createHandlingEvent(
|
|
|
289
|
+ new Date(), toDate("2009-03-03"), trackingId1, HONGKONG_TO_NEW_YORK.voyageNumber(), HANGZOU.unLocode(), HandlingEvent.Type.LOAD
|
|
|
290
|
+ );
|
|
|
291
|
+ session.save(event2);
|
|
|
292
|
+
|
|
|
293
|
+ HandlingEvent event3 = handlingEventFactory.createHandlingEvent(
|
|
|
294
|
+ new Date(), toDate("2009-03-05"), trackingId1, HONGKONG_TO_NEW_YORK.voyageNumber(), NEWYORK.unLocode(), HandlingEvent.Type.UNLOAD
|
|
|
295
|
+ );
|
|
|
296
|
+ session.save(event3);
|
|
|
297
|
+
|
|
|
298
|
+ HandlingEvent event4 = handlingEventFactory.createHandlingEvent(
|
|
|
299
|
+ new Date(), toDate("2009-03-06"), trackingId1, HONGKONG_TO_NEW_YORK.voyageNumber(), NEWYORK.unLocode(), HandlingEvent.Type.LOAD
|
|
|
300
|
+ );
|
|
|
301
|
+ session.save(event4);
|
|
|
302
|
+
|
|
|
303
|
+ } catch (CannotCreateHandlingEventException e) {
|
|
|
304
|
+ throw new RuntimeException(e);
|
|
|
305
|
+ }
|
|
|
306
|
+
|
|
|
307
|
+ HandlingHistory handlingHistory1 = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId1);
|
|
|
308
|
+ jkl567.deriveDeliveryProgress(handlingHistory1);
|
|
|
309
|
+
|
|
|
310
|
+ session.update(jkl567);
|
|
|
311
|
+ }
|
|
|
312
|
+ });
|
|
|
313
|
+ }
|
|
|
314
|
+
|
|
|
315
|
+ public static void loadSampleData(final JdbcTemplate jdbcTemplate, TransactionTemplate transactionTemplate) {
|
|
|
316
|
+ transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
|
|
317
|
+ protected void doInTransactionWithoutResult(TransactionStatus status) {
|
|
|
318
|
+ loadLocationData(jdbcTemplate);
|
|
|
319
|
+ loadCarrierMovementData(jdbcTemplate);
|
|
|
320
|
+ loadCargoData(jdbcTemplate);
|
|
|
321
|
+ loadItineraryData(jdbcTemplate);
|
|
|
322
|
+ loadHandlingEventData(jdbcTemplate);
|
|
|
323
|
+ }
|
|
|
324
|
+ });
|
|
|
325
|
+ }
|
|
297
|
326
|
|
|
298
|
|
- HandlingHistory handlingHistory1 = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId1);
|
|
299
|
|
- jkl567.deriveDeliveryProgress(handlingHistory1);
|
|
300
|
|
-
|
|
301
|
|
- session.update(jkl567);
|
|
302
|
|
- }
|
|
303
|
|
- });
|
|
304
|
|
- }
|
|
305
|
|
-
|
|
306
|
|
- public void contextDestroyed(ServletContextEvent event) {}
|
|
307
|
|
-
|
|
308
|
|
- public static void loadSampleData(final JdbcTemplate jdbcTemplate, TransactionTemplate transactionTemplate) {
|
|
309
|
|
- transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
|
310
|
|
- protected void doInTransactionWithoutResult(TransactionStatus status) {
|
|
311
|
|
- loadLocationData(jdbcTemplate);
|
|
312
|
|
- loadCarrierMovementData(jdbcTemplate);
|
|
313
|
|
- loadCargoData(jdbcTemplate);
|
|
314
|
|
- loadItineraryData(jdbcTemplate);
|
|
315
|
|
- loadHandlingEventData(jdbcTemplate);
|
|
316
|
|
- }
|
|
317
|
|
- });
|
|
318
|
|
- }
|
|
319
|
|
-
|
|
320
|
|
- private static void executeUpdate(JdbcTemplate jdbcTemplate, String sql, Object[][] args) {
|
|
321
|
|
- for (Object[] arg : args) {
|
|
322
|
|
- jdbcTemplate.update(sql, arg);
|
|
|
327
|
+ private static void executeUpdate(JdbcTemplate jdbcTemplate, String sql, Object[][] args) {
|
|
|
328
|
+ for (Object[] arg : args) {
|
|
|
329
|
+ jdbcTemplate.update(sql, arg);
|
|
|
330
|
+ }
|
|
323
|
331
|
}
|
|
324
|
|
- }
|
|
325
|
332
|
|
|
326
|
|
- private static Timestamp ts(int hours) {
|
|
327
|
|
- return new Timestamp(base.getTime() + 1000L * 60 * 60 * hours);
|
|
328
|
|
- }
|
|
|
333
|
+ private static Timestamp ts(int hours) {
|
|
|
334
|
+ return new Timestamp(base.getTime() + 1000L * 60 * 60 * hours);
|
|
|
335
|
+ }
|
|
329
|
336
|
|
|
330
|
|
- public static Date offset(int hours) {
|
|
331
|
|
- return new Date(ts(hours).getTime());
|
|
332
|
|
- }
|
|
|
337
|
+ public static Date offset(int hours) {
|
|
|
338
|
+ return new Date(ts(hours).getTime());
|
|
|
339
|
+ }
|
|
333
|
340
|
}
|