1
1
package se .citerus .dddsample .tracking .bookingui .web ;
2
2
3
+ import org .codehaus .jettison .AbstractXMLStreamWriter ;
4
+ import org .codehaus .jettison .mapped .MappedNamespaceConvention ;
5
+ import org .codehaus .jettison .mapped .MappedXMLStreamWriter ;
3
6
import org .springframework .beans .propertyeditors .CustomDateEditor ;
4
7
import org .springframework .web .bind .ServletRequestDataBinder ;
5
8
import org .springframework .web .servlet .mvc .multiaction .MultiActionController ;
6
9
import se .citerus .dddsample .tracking .booking .api .*;
7
10
8
11
import javax .servlet .http .HttpServletRequest ;
9
12
import javax .servlet .http .HttpServletResponse ;
13
+ import javax .xml .stream .XMLStreamException ;
14
+ import java .io .StringWriter ;
10
15
import java .text .SimpleDateFormat ;
11
16
import java .util .*;
12
17
19
24
* this approach is generally preferred to the one taken in the tracking controller. However,
20
25
* there is never any one perfect solution for all situations, so we've chosen to demonstrate
21
26
* two polarized ways to build user interfaces.
22
- *
23
27
*/
24
28
public final class CargoAdminController extends MultiActionController {
25
29
26
30
private BookingServiceFacade bookingServiceFacade ;
31
+ private static final CustomDateEditor DATE_EDITOR = new CustomDateEditor (new SimpleDateFormat ("yyyy-MM-dd HH:mm" ), false );
32
+ private static final UnLocodeComparator UN_LOCODE_COMPARATOR = new UnLocodeComparator ();
33
+
34
+ public CargoAdminController (BookingServiceFacade bookingServiceFacade ) {
35
+ this .bookingServiceFacade = bookingServiceFacade ;
36
+ }
27
37
28
38
@ Override
29
39
protected void initBinder (HttpServletRequest request , ServletRequestDataBinder binder ) throws Exception {
30
40
super .initBinder (request , binder );
31
- binder .registerCustomEditor (Date .class , new CustomDateEditor ( new SimpleDateFormat ( "yyyy-MM-dd HH:mm" ), false ) );
41
+ binder .registerCustomEditor (Date .class , DATE_EDITOR );
32
42
}
33
43
34
- public Map registrationForm (HttpServletRequest request , HttpServletResponse response ) throws Exception {
44
+ public Map cargoBookingForm (HttpServletRequest request , HttpServletResponse response ) throws Exception {
35
45
Map <String , Object > map = new HashMap <String , Object >();
36
- List <LocationDTO > dtoList = bookingServiceFacade .listShippingLocations ();
37
46
38
- List <String > unLocodeStrings = new ArrayList <String >();
39
-
40
- for (LocationDTO dto : dtoList ) {
41
- unLocodeStrings .add (dto .getUnLocode ());
42
- }
43
-
44
- map .put ("unlocodes" , unLocodeStrings );
47
+ List <LocationDTO > dtoList = bookingServiceFacade .listShippingLocations ();
48
+ Collections .sort (dtoList , UN_LOCODE_COMPARATOR );
49
+
45
50
map .put ("locations" , dtoList );
46
51
return map ;
47
52
}
48
53
49
- public void register (HttpServletRequest request , HttpServletResponse response ,
50
- RegistrationCommand command ) throws Exception {
54
+ public void bookCargo (HttpServletRequest request , HttpServletResponse response ,
55
+ CargoBookingCommand command ) throws Exception {
51
56
Date arrivalDeadline = new SimpleDateFormat ("M/dd/yyyy" ).parse (command .getArrivalDeadline ());
52
57
String trackingId = bookingServiceFacade .bookNewCargo (
53
58
command .getOriginUnlocode (), command .getDestinationUnlocode (), arrivalDeadline
@@ -101,7 +106,6 @@ public void assignItinerary(HttpServletRequest request, HttpServletResponse resp
101
106
bookingServiceFacade .assignCargoToRoute (command .getTrackingId (), selectedRoute );
102
107
103
108
response .sendRedirect ("show.html?trackingId=" + command .getTrackingId ());
104
- //response.sendRedirect("list.html");
105
109
}
106
110
107
111
public Map pickNewDestination (HttpServletRequest request , HttpServletResponse response ) throws Exception {
@@ -124,7 +128,71 @@ public void changeDestination(HttpServletRequest request, HttpServletResponse re
124
128
response .sendRedirect ("show.html?trackingId=" + trackingId );
125
129
}
126
130
127
- public void setBookingServiceFacade (BookingServiceFacade bookingServiceFacade ) {
128
- this .bookingServiceFacade = bookingServiceFacade ;
131
+ public Map voyageDelayedForm (HttpServletRequest request , HttpServletResponse response ) throws Exception {
132
+ Map <String , List <String >> departures = new HashMap <String , List <String >>();
133
+ Map <String , List <String >> arrivals = new HashMap <String , List <String >>();
134
+
135
+ List <VoyageDTO > voyages = bookingServiceFacade .listAllVoyages ();
136
+
137
+ for (VoyageDTO voyage : voyages ) {
138
+ List <String > departureLocations = getLocationsList (departures , voyage );
139
+ List <String > arrivalLocations = getLocationsList (arrivals , voyage );
140
+ for (CarrierMovementDTO dto : voyage .getMovements ()) {
141
+ departureLocations .add (dto .getDepartureLocation ().getUnLocode ());
142
+ arrivalLocations .add (dto .getArrivalLocation ().getUnLocode ());
143
+ }
144
+ }
145
+
146
+ Map <String , Object > model = new HashMap <String , Object >();
147
+
148
+ model .put ("departures" , toJSON (departures ));
149
+ model .put ("arrivals" , toJSON (arrivals ));
150
+ model .put ("voyages" , voyages );
151
+
152
+ return model ;
153
+ }
154
+
155
+ public void voyageDelayed (HttpServletRequest request , HttpServletResponse response , VoyageDelayCommand command ) throws Exception {
156
+ if (command .getType () == VoyageDelayCommand .DelayType .DEPT ) {
157
+ bookingServiceFacade .departureDelayed (new VoyageDelayDTO (command .getVoyageNumber (), command .getHours () * 60 ));
158
+ } else if (command .getType () == VoyageDelayCommand .DelayType .ARR ) {
159
+ bookingServiceFacade .arrivalDelayed (new VoyageDelayDTO (command .getVoyageNumber (), command .getHours () * 60 ));
160
+ }
161
+
162
+ response .sendRedirect ("list.html" );
163
+ }
164
+
165
+ private String toJSON (Map <String , List <String >> locationMap ) throws XMLStreamException {
166
+ StringWriter stringWriter = new StringWriter ();
167
+ MappedNamespaceConvention con = new MappedNamespaceConvention ();
168
+ AbstractXMLStreamWriter w = new MappedXMLStreamWriter (con , stringWriter );
169
+
170
+ w .writeStartDocument ();
171
+ for (Map .Entry <String , List <String >> e : locationMap .entrySet ()) {
172
+ for (String location : e .getValue ()) {
173
+ w .writeStartElement (e .getKey ());
174
+ w .writeCharacters (location );
175
+ w .writeEndElement ();
176
+ }
177
+ }
178
+ w .writeEndDocument ();
179
+
180
+ return stringWriter .toString ();
181
+ }
182
+
183
+ private List <String > getLocationsList (Map <String , List <String >> map , VoyageDTO voyage ) {
184
+ List <String > locations = map .get (voyage .getVoyageNumber ());
185
+ if (locations == null ) {
186
+ locations = new ArrayList <String >();
187
+ map .put (voyage .getVoyageNumber (), locations );
188
+ }
189
+ return locations ;
190
+ }
191
+
192
+ private static class UnLocodeComparator implements Comparator <LocationDTO > {
193
+ @ Override
194
+ public int compare (LocationDTO o1 , LocationDTO o2 ) {
195
+ return o1 .getUnLocode ().compareTo (o2 .getUnLocode ());
196
+ }
129
197
}
130
198
}
0 commit comments