CargoTrackingViewAdapter.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package se.citerus.dddsample.interfaces.tracking;
  2. import org.springframework.context.MessageSource;
  3. import se.citerus.dddsample.domain.model.cargo.Cargo;
  4. import se.citerus.dddsample.domain.model.cargo.Delivery;
  5. import se.citerus.dddsample.domain.model.cargo.HandlingActivity;
  6. import se.citerus.dddsample.domain.model.handling.HandlingEvent;
  7. import se.citerus.dddsample.domain.model.location.Location;
  8. import se.citerus.dddsample.domain.model.voyage.Voyage;
  9. import java.text.SimpleDateFormat;
  10. import java.util.*;
  11. /**
  12. * View adapter for displaying a cargo in a tracking context.
  13. */
  14. public final class CargoTrackingViewAdapter {
  15. private final Cargo cargo;
  16. private final MessageSource messageSource;
  17. private final Locale locale;
  18. private final List<HandlingEventViewAdapter> events;
  19. private final String FORMAT = "yyyy-MM-dd hh:mm";
  20. private final TimeZone timeZone;
  21. /**
  22. * Constructor.
  23. *
  24. * @param cargo
  25. * @param messageSource
  26. * @param locale
  27. * @param handlingEvents
  28. */
  29. public CargoTrackingViewAdapter(Cargo cargo, MessageSource messageSource, Locale locale, List<HandlingEvent> handlingEvents) {
  30. this(cargo, messageSource, locale, handlingEvents, TimeZone.getDefault());
  31. }
  32. /**
  33. * Constructor.
  34. *
  35. * @param cargo
  36. * @param messageSource
  37. * @param locale
  38. * @param handlingEvents
  39. */
  40. public CargoTrackingViewAdapter(Cargo cargo, MessageSource messageSource, Locale locale, List<HandlingEvent> handlingEvents, TimeZone tz) {
  41. this.messageSource = messageSource;
  42. this.locale = locale;
  43. this.cargo = cargo;
  44. this.timeZone = tz;
  45. this.events = new ArrayList<HandlingEventViewAdapter>(handlingEvents.size());
  46. for (HandlingEvent handlingEvent : handlingEvents) {
  47. events.add(new HandlingEventViewAdapter(handlingEvent));
  48. }
  49. }
  50. /**
  51. * @param location a location
  52. * @return A formatted string for displaying the location.
  53. */
  54. private String getDisplayText(Location location) {
  55. return location.name();
  56. }
  57. /**
  58. * @return An unmodifiable list of handling event view adapters.
  59. */
  60. public List<HandlingEventViewAdapter> getEvents() {
  61. return Collections.unmodifiableList(events);
  62. }
  63. /**
  64. * @return A translated string describing the cargo status.
  65. */
  66. public String getStatusText() {
  67. final Delivery delivery = cargo.delivery();
  68. final String code = "cargo.status." + delivery.transportStatus().name();
  69. final Object[] args;
  70. switch (delivery.transportStatus()) {
  71. case IN_PORT:
  72. args = new Object[] {getDisplayText(delivery.lastKnownLocation())};
  73. break;
  74. case ONBOARD_CARRIER:
  75. args = new Object[] {delivery.currentVoyage().voyageNumber().idString()};
  76. break;
  77. case CLAIMED:
  78. case NOT_RECEIVED:
  79. case UNKNOWN:
  80. default:
  81. args = null;
  82. break;
  83. }
  84. return messageSource.getMessage(code, args, "[Unknown status]", locale);
  85. }
  86. /**
  87. * @return Cargo destination location.
  88. */
  89. public String getDestination() {
  90. return getDisplayText(cargo.routeSpecification().destination());
  91. }
  92. /**
  93. * @return Cargo osigin location.
  94. */
  95. public String getOrigin() {
  96. return getDisplayText(cargo.origin());
  97. }
  98. /**
  99. * @return Cargo tracking id.
  100. */
  101. public String getTrackingId() {
  102. return cargo.trackingId().idString();
  103. }
  104. public String getEta() {
  105. Date eta = cargo.delivery().estimatedTimeOfArrival();
  106. if (eta == null) return "?";
  107. else return new SimpleDateFormat(FORMAT).format(eta);
  108. }
  109. public String getNextExpectedActivity() {
  110. HandlingActivity activity = cargo.delivery().nextExpectedActivity();
  111. if (activity == null) {
  112. return "";
  113. }
  114. String text = "Next expected activity is to ";
  115. HandlingEvent.Type type = activity.type();
  116. if (type.sameValueAs(HandlingEvent.Type.LOAD)) {
  117. return
  118. text + type.name().toLowerCase() + " cargo onto voyage " + activity.voyage().voyageNumber() +
  119. " in " + activity.location().name();
  120. } else if (type.sameValueAs(HandlingEvent.Type.UNLOAD)) {
  121. return
  122. text + type.name().toLowerCase() + " cargo off of " + activity.voyage().voyageNumber() +
  123. " in " + activity.location().name();
  124. } else {
  125. return text + type.name().toLowerCase() + " cargo in " + activity.location().name();
  126. }
  127. }
  128. /**
  129. * @return True if cargo is misdirected.
  130. */
  131. public boolean isMisdirected() {
  132. return cargo.delivery().isMisdirected();
  133. }
  134. /**
  135. * Handling event view adapter component.
  136. */
  137. public final class HandlingEventViewAdapter {
  138. private final HandlingEvent handlingEvent;
  139. /**
  140. * Constructor.
  141. *
  142. * @param handlingEvent handling event
  143. */
  144. public HandlingEventViewAdapter(HandlingEvent handlingEvent) {
  145. this.handlingEvent = handlingEvent;
  146. }
  147. /**
  148. * @return Location where the event occurred.
  149. */
  150. public String getLocation() {
  151. return handlingEvent.location().name();
  152. }
  153. /**
  154. * @return Time when the event was completed.
  155. */
  156. public String getTime() {
  157. final SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
  158. sdf.setTimeZone(timeZone);
  159. return sdf.format(handlingEvent.completionTime());
  160. }
  161. /**
  162. * @return Type of event.
  163. */
  164. public String getType() {
  165. return handlingEvent.type().toString();
  166. }
  167. /**
  168. * @return Voyage number, or empty string if not applicable.
  169. */
  170. public String getVoyageNumber() {
  171. final Voyage voyage = handlingEvent.voyage();
  172. return voyage.voyageNumber().idString();
  173. }
  174. /**
  175. * @return True if the event was expected, according to the cargo's itinerary.
  176. */
  177. public boolean isExpected() {
  178. return cargo.itinerary().isExpected(handlingEvent);
  179. }
  180. public String getDescription() {
  181. Object[] args;
  182. switch (handlingEvent.type()) {
  183. case LOAD:
  184. case UNLOAD:
  185. args = new Object[] {
  186. handlingEvent.voyage().voyageNumber().idString(),
  187. handlingEvent.location().name(),
  188. handlingEvent.completionTime()
  189. };
  190. break;
  191. case RECEIVE:
  192. case CLAIM:
  193. args = new Object[] {
  194. handlingEvent.location().name(),
  195. handlingEvent.completionTime()
  196. };
  197. break;
  198. default:
  199. args = new Object[] {};
  200. }
  201. String key = "deliveryHistory.eventDescription." + handlingEvent.type().name();
  202. return messageSource.getMessage(key,args,locale);
  203. }
  204. }
  205. }