Просмотр исходного кода

Made submission of handling reports synchronously registering handling events.

peter_backlund 17 лет назад
Родитель
Сommit
3562bc9264

+ 0
- 43
dddsample/src/main/java/se/citerus/dddsample/infrastructure/messaging/jms/HandlingEventRegistrationAttemptConsumer.java Просмотреть файл

@@ -1,43 +0,0 @@
1
-package se.citerus.dddsample.infrastructure.messaging.jms;
2
-
3
-import org.apache.commons.logging.Log;
4
-import org.apache.commons.logging.LogFactory;
5
-import se.citerus.dddsample.application.HandlingEventService;
6
-import se.citerus.dddsample.interfaces.handling.HandlingEventRegistrationAttempt;
7
-
8
-import javax.jms.Message;
9
-import javax.jms.MessageListener;
10
-import javax.jms.ObjectMessage;
11
-
12
-/**
13
- * Consumes handling event registration attempt messages and delegates to
14
- * proper registration.
15
- * 
16
- */
17
-public class HandlingEventRegistrationAttemptConsumer implements MessageListener {
18
-
19
-  private HandlingEventService handlingEventService;
20
-  private static final Log logger = LogFactory.getLog(HandlingEventRegistrationAttemptConsumer.class);
21
-
22
-  @Override
23
-  public void onMessage(final Message message) {
24
-    try {
25
-      final ObjectMessage om = (ObjectMessage) message;
26
-      HandlingEventRegistrationAttempt attempt = (HandlingEventRegistrationAttempt) om.getObject();
27
-      handlingEventService.registerHandlingEvent(
28
-        attempt.getCompletionTime(),
29
-        attempt.getTrackingId(),
30
-        attempt.getVoyageNumber(),
31
-        attempt.getUnLocode(),
32
-        attempt.getType()
33
-      );
34
-    } catch (Exception e) {
35
-      logger.error(e, e);
36
-    }
37
-  }
38
-
39
-  public void setHandlingEventService(HandlingEventService handlingEventService) {
40
-    this.handlingEventService = handlingEventService;
41
-  }
42
-
43
-}

+ 0
- 72
dddsample/src/main/java/se/citerus/dddsample/interfaces/handling/HandlingEventRegistrationAttempt.java Просмотреть файл

@@ -1,72 +0,0 @@
1
-package se.citerus.dddsample.interfaces.handling;
2
-
3
-import org.apache.commons.lang.builder.ToStringBuilder;
4
-import org.apache.commons.lang.builder.ToStringStyle;
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 java.io.Serializable;
11
-import java.util.Date;
12
-
13
-/**
14
- * This is a simple transfer object for passing incoming handling event
15
- * registration attempts to proper the registration procedure.
16
- *
17
- * It is used as a message queue element. 
18
- *
19
- */
20
-public final class HandlingEventRegistrationAttempt implements Serializable {
21
-
22
-  private final Date registrationTime;
23
-  private final Date completionTime;
24
-  private final TrackingId trackingId;
25
-  private final VoyageNumber voyageNumber;
26
-  private final HandlingEvent.Type type;
27
-  private final UnLocode unLocode;
28
-
29
-  public HandlingEventRegistrationAttempt(final Date registrationDate,
30
-                                          final Date completionDate,
31
-                                          final TrackingId trackingId,
32
-                                          final VoyageNumber voyageNumber,
33
-                                          final HandlingEvent.Type type,
34
-                                          final UnLocode unLocode) {
35
-    this.registrationTime = registrationDate;
36
-    this.completionTime = completionDate;
37
-    this.trackingId = trackingId;
38
-    this.voyageNumber = voyageNumber;
39
-    this.type = type;
40
-    this.unLocode = unLocode;
41
-  }
42
-
43
-  public Date getCompletionTime() {
44
-    return new Date(completionTime.getTime());
45
-  }
46
-
47
-  public TrackingId getTrackingId() {
48
-    return trackingId;
49
-  }
50
-
51
-  public VoyageNumber getVoyageNumber() {
52
-    return voyageNumber;
53
-  }
54
-
55
-  public HandlingEvent.Type getType() {
56
-    return type;
57
-  }
58
-
59
-  public UnLocode getUnLocode() {
60
-    return unLocode;
61
-  }
62
-
63
-  public Date getRegistrationTime() {
64
-    return registrationTime;
65
-  }
66
-
67
-  @Override
68
-  public String toString() {
69
-    return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
70
-  }
71
-  
72
-}

+ 5
- 7
dddsample/src/main/java/se/citerus/dddsample/interfaces/handling/file/UploadDirectoryScanner.java Просмотреть файл

@@ -4,12 +4,11 @@ import org.apache.commons.io.FileUtils;
4 4
 import org.apache.commons.logging.Log;
5 5
 import org.apache.commons.logging.LogFactory;
6 6
 import org.springframework.beans.factory.InitializingBean;
7
-import se.citerus.dddsample.application.ApplicationEvents;
7
+import se.citerus.dddsample.application.HandlingEventService;
8 8
 import se.citerus.dddsample.domain.model.cargo.TrackingId;
9 9
 import se.citerus.dddsample.domain.model.handling.HandlingEvent;
10 10
 import se.citerus.dddsample.domain.model.location.UnLocode;
11 11
 import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
12
-import se.citerus.dddsample.interfaces.handling.HandlingEventRegistrationAttempt;
13 12
 import static se.citerus.dddsample.interfaces.handling.HandlingReportParser.*;
14 13
 
15 14
 import java.io.File;
@@ -30,9 +29,9 @@ public class UploadDirectoryScanner extends TimerTask implements InitializingBea
30 29
 
31 30
   private File uploadDirectory;
32 31
   private File parseFailureDirectory;
32
+  private HandlingEventService handlingEventService;
33 33
 
34 34
   private final static Log logger = LogFactory.getLog(UploadDirectoryScanner.class);
35
-  private ApplicationEvents applicationEvents;
36 35
 
37 36
   @Override
38 37
   public void run() {
@@ -95,8 +94,7 @@ public class UploadDirectoryScanner extends TimerTask implements InitializingBea
95 94
     final UnLocode unLocode = parseUnLocode(unLocodeStr, errors);
96 95
 
97 96
     if (errors.isEmpty()) {
98
-      final HandlingEventRegistrationAttempt attempt = new HandlingEventRegistrationAttempt(new Date(), date, trackingId, voyageNumber, eventType, unLocode);
99
-      applicationEvents.receivedHandlingEventRegistrationAttempt(attempt);
97
+      handlingEventService.registerHandlingEvent(date, trackingId, voyageNumber, unLocode, eventType);
100 98
     } else {
101 99
       throw new Exception(errors.toString());
102 100
     }
@@ -137,7 +135,7 @@ public class UploadDirectoryScanner extends TimerTask implements InitializingBea
137 135
     this.parseFailureDirectory = parseFailureDirectory;
138 136
   }
139 137
 
140
-  public void setApplicationEvents(ApplicationEvents applicationEvents) {
141
-    this.applicationEvents = applicationEvents;
138
+  public void setHandlingEventService(HandlingEventService handlingEventService) {
139
+    this.handlingEventService = handlingEventService;
142 140
   }
143 141
 }

+ 37
- 27
dddsample/src/main/java/se/citerus/dddsample/interfaces/handling/ws/HandlingReportServiceImpl.java Просмотреть файл

@@ -6,62 +6,72 @@ import com.aggregator.HandlingReportErrors_Exception;
6 6
 import com.aggregator.HandlingReportService;
7 7
 import org.apache.commons.logging.Log;
8 8
 import org.apache.commons.logging.LogFactory;
9
-import se.citerus.dddsample.application.ApplicationEvents;
9
+import se.citerus.dddsample.application.HandlingEventService;
10 10
 import se.citerus.dddsample.domain.model.cargo.TrackingId;
11
+import se.citerus.dddsample.domain.model.handling.CannotCreateHandlingEventException;
11 12
 import se.citerus.dddsample.domain.model.handling.HandlingEvent;
12 13
 import se.citerus.dddsample.domain.model.location.UnLocode;
13 14
 import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
14
-import se.citerus.dddsample.interfaces.handling.HandlingEventRegistrationAttempt;
15 15
 import static se.citerus.dddsample.interfaces.handling.HandlingReportParser.*;
16 16
 
17 17
 import javax.jws.WebParam;
18 18
 import javax.jws.WebService;
19
-import java.util.ArrayList;
20
-import java.util.Date;
21
-import java.util.List;
19
+import java.util.*;
22 20
 
23 21
 /**
24 22
  * This web service endpoint implementation performs basic validation and parsing
25
- * of incoming data, and in case of a valid registration attempt, sends an asynchronous message
26
- * with the informtion to the handling event registration system for proper registration.
27
- *  
23
+ * of an incoming handling report, and attempts to register proper handling events
24
+ * by calling the application layer.
28 25
  */
29 26
 @WebService(endpointInterface = "com.aggregator.HandlingReportService")
30 27
 public class HandlingReportServiceImpl implements HandlingReportService {
31 28
 
32
-  private ApplicationEvents applicationEvents;
29
+  private HandlingEventService handlingEventService;
33 30
   private final static Log logger = LogFactory.getLog(HandlingReportServiceImpl.class);
34 31
 
35 32
   @Override
36 33
   public void submitReport(@WebParam(name = "arg0", targetNamespace = "") HandlingReport handlingReport) throws HandlingReportErrors_Exception {
37
-    final List<String> errors = new ArrayList<String>();
34
+    final List<String> validationErrors = new ArrayList<String>();
38 35
 
39
-    final Date completionTime = parseCompletionTime(handlingReport, errors);
40
-    final VoyageNumber voyageNumber = parseVoyageNumber(handlingReport.getVoyageNumber(), errors);
41
-    final HandlingEvent.Type type = parseEventType(handlingReport.getType(), errors);
42
-    final UnLocode unLocode = parseUnLocode(handlingReport.getUnLocode(), errors);
36
+    final Date completionTime = parseCompletionTime(handlingReport, validationErrors);
37
+    final VoyageNumber voyageNumber = parseVoyageNumber(handlingReport.getVoyageNumber(), validationErrors);
38
+    final HandlingEvent.Type type = parseEventType(handlingReport.getType(), validationErrors);
39
+    final UnLocode unLocode = parseUnLocode(handlingReport.getUnLocode(), validationErrors);
43 40
 
41
+    final Map<String,String> allErrors = new HashMap<String, String>();
44 42
     for (String trackingIdStr : handlingReport.getTrackingIds()) {
45
-      final TrackingId trackingId = parseTrackingId(trackingIdStr, errors);
43
+      final TrackingId trackingId = parseTrackingId(trackingIdStr, validationErrors);
46 44
 
47
-      if (errors.isEmpty()) {
48
-        final Date registrationTime = new Date();
49
-        final HandlingEventRegistrationAttempt attempt = new HandlingEventRegistrationAttempt(
50
-          registrationTime, completionTime, trackingId, voyageNumber, type, unLocode
51
-        );
52
-
53
-        applicationEvents.receivedHandlingEventRegistrationAttempt(attempt);
45
+      if (validationErrors.isEmpty()) {
46
+        try {
47
+          handlingEventService.registerHandlingEvent(completionTime, trackingId, voyageNumber, unLocode, type);
48
+        } catch (CannotCreateHandlingEventException e) {
49
+          logger.error(e, e);
50
+          allErrors.put(trackingIdStr, e.getMessage());
51
+        }
54 52
       } else {
55
-        logger.error("Parse error in handling report: " + errors);
56
-        final HandlingReportErrors faultInfo = new HandlingReportErrors();
57
-        throw new HandlingReportErrors_Exception(errors.toString(), faultInfo);
53
+        logger.error("Parse error in handling report: " + validationErrors);
54
+        allErrors.put(trackingIdStr, validationErrors.toString());
58 55
       }
59 56
     }
60 57
 
58
+    if (!allErrors.isEmpty()) {
59
+      final HandlingReportErrors faultInfo = new HandlingReportErrors();
60
+      throw new HandlingReportErrors_Exception(createErrorMessage(allErrors), faultInfo);
61
+    }
62
+  }
63
+
64
+  private String createErrorMessage(final Map<String, String> allErrors) {
65
+    final StringBuilder sb = new StringBuilder("--- BEGIN HANDLING REPORT ERRORS ---\n");
66
+    for (Map.Entry<String, String> e : allErrors.entrySet()) {
67
+      sb.append(e.getKey()).append(" : ").append(e.getValue()).append("\n");
68
+    }
69
+    sb.append("--- END HANDLING REPORT ERRORS ---");
70
+    return sb.toString();
61 71
   }
62 72
 
63
-  public void setApplicationEvents(ApplicationEvents applicationEvents) {
64
-    this.applicationEvents = applicationEvents;
73
+  public void setHandlingEventService(final HandlingEventService handlingEventService) {
74
+    this.handlingEventService = handlingEventService;
65 75
   }
66 76
 
67 77
 }

+ 2
- 2
dddsample/src/main/resources/context-interfaces.xml Просмотреть файл

@@ -18,7 +18,7 @@
18 18
   <jaxws:endpoint id="jax-ws.http" implementor="#handlingReportService" address="/RegisterEvent"/>
19 19
 
20 20
   <bean id="handlingReportService" class="se.citerus.dddsample.interfaces.handling.ws.HandlingReportServiceImpl">
21
-    <property name="applicationEvents" ref="applicationEvents"/>
21
+    <property name="handlingEventService" ref="handlingEventService"/>
22 22
   </bean>
23 23
 
24 24
 
@@ -34,7 +34,7 @@
34 34
   <bean id="uploadDirectoryScanner" class="se.citerus.dddsample.interfaces.handling.file.UploadDirectoryScanner">
35 35
     <property name="uploadDirectory" value="/tmp/upload"/>
36 36
     <property name="parseFailureDirectory" value="/tmp/failed"/>
37
-    <property name="applicationEvents" ref="applicationEvents"/>
37
+    <property name="handlingEventService" ref="handlingEventService"/>
38 38
   </bean>
39 39
 
40 40
   <bean id="handlingReportConsumptionSupport" class="se.citerus.dddsample.interfaces.handling.HandlingReportParser"/>