View Javadoc

1   package se.citerus.dddsample.interfaces.handling;
2   
3   import com.aggregator.HandlingReport;
4   import org.apache.commons.lang.StringUtils;
5   import se.citerus.dddsample.domain.model.cargo.TrackingId;
6   import se.citerus.dddsample.domain.model.handling.HandlingEvent;
7   import se.citerus.dddsample.domain.model.location.UnLocode;
8   import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
9   
10  import javax.xml.datatype.XMLGregorianCalendar;
11  import java.text.ParseException;
12  import java.text.SimpleDateFormat;
13  import java.util.Arrays;
14  import java.util.Date;
15  import java.util.List;
16  
17  /**
18   * Utility methods for parsing various forms of handling report formats.
19   *
20   * Supports the notification pattern for incremental error reporting.
21   *
22   */
23  public class HandlingReportParser {
24  
25    public static final String ISO_8601_FORMAT = "yyyy-MM-dd HH:mm";
26  
27    public static UnLocode parseUnLocode(final String unlocode, final List<String> errors) {
28      try {
29        return new UnLocode(unlocode);
30      } catch (IllegalArgumentException e) {
31        errors.add(e.getMessage());
32        return null;
33      }
34    }
35  
36    public static TrackingId parseTrackingId(final String trackingId, final List<String> errors) {
37      try {
38        return new TrackingId(trackingId);
39      } catch (IllegalArgumentException e) {
40        errors.add(e.getMessage());
41        return null;
42      }
43    }
44  
45    public static VoyageNumber parseVoyageNumber(final String voyageNumber, final List<String> errors) {
46      if (StringUtils.isNotEmpty(voyageNumber)) {
47        try {
48          return new VoyageNumber(voyageNumber);
49        } catch (IllegalArgumentException e) {
50          errors.add(e.getMessage());
51          return null;
52        }
53      } else {
54        return null;
55      }
56    }
57  
58    public static Date parseDate(final String completionTime, final List<String> errors) {
59      Date date;
60      try {
61        date = new SimpleDateFormat(ISO_8601_FORMAT).parse(completionTime);
62      } catch (ParseException e) {
63        errors.add("Invalid date format: " + completionTime + ", must be on ISO 8601 format: " + ISO_8601_FORMAT);
64        date = null;
65      }
66      return date;
67    }
68  
69    public static HandlingEvent.Type parseEventType(final String eventType, final List<String> errors) {
70      try {
71        return HandlingEvent.Type.valueOf(eventType);
72      } catch (IllegalArgumentException e) {
73        errors.add(eventType + " is not a valid handling event type. Valid types are: " + Arrays.toString(HandlingEvent.Type.values()));
74        return null;
75      }
76    }
77  
78    public static Date parseCompletionTime(HandlingReport handlingReport, List<String> errors) {
79      final XMLGregorianCalendar completionTime = handlingReport.getCompletionTime();
80      if (completionTime == null) {
81        errors.add("Completion time is required");
82        return null;
83      }
84  
85      return completionTime.toGregorianCalendar().getTime();
86    }
87  }