HandlingEventServiceEndpointImpl.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package se.citerus.dddsample.application.ws;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5. import org.springframework.transaction.PlatformTransactionManager;
  6. import org.springframework.transaction.TransactionStatus;
  7. import org.springframework.transaction.support.TransactionCallbackWithoutResult;
  8. import org.springframework.transaction.support.TransactionTemplate;
  9. import se.citerus.dddsample.domain.model.cargo.TrackingId;
  10. import se.citerus.dddsample.domain.model.carrier.CarrierMovementId;
  11. import se.citerus.dddsample.domain.model.handling.HandlingEvent;
  12. import se.citerus.dddsample.domain.model.location.UnLocode;
  13. import se.citerus.dddsample.domain.service.HandlingEventService;
  14. import se.citerus.dddsample.domain.service.UnknownCarrierMovementIdException;
  15. import se.citerus.dddsample.domain.service.UnknownLocationException;
  16. import se.citerus.dddsample.domain.service.UnknownTrackingIdException;
  17. import javax.jws.WebService;
  18. import java.text.ParseException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. @WebService(endpointInterface = "se.citerus.dddsample.application.ws.HandlingEventServiceEndpoint")
  22. public class HandlingEventServiceEndpointImpl implements HandlingEventServiceEndpoint {
  23. private HandlingEventService handlingEventService;
  24. private TransactionTemplate transactionTemplate;
  25. private final Log logger = LogFactory.getLog(getClass());
  26. protected static final String ISO_8601_FORMAT = "yyyy-mm-dd HH:MM:SS.SSS";
  27. public void register(final String completionTime, final String trackingId, final String carrierMovementId,
  28. final String unlocode, final String eventType) {
  29. try {
  30. final Date date = parseIso8601Date(completionTime);
  31. final TrackingId tid = new TrackingId(trackingId);
  32. final CarrierMovementId cid;
  33. if (StringUtils.isNotEmpty(carrierMovementId)) {
  34. cid = new CarrierMovementId(carrierMovementId);
  35. } else {
  36. cid = null;
  37. }
  38. final HandlingEvent.Type type = parseEventType(eventType);
  39. final UnLocode ul = new UnLocode(unlocode);
  40. // Using programmatic demarcation here due to weaving conflicts
  41. // between jax-ws and Spring transaction annotations
  42. transactionTemplate.execute(new TransactionCallbackWithoutResult() {
  43. protected void doInTransactionWithoutResult(TransactionStatus status) {
  44. try {
  45. handlingEventService.register(date, tid, cid, ul, type);
  46. } catch (UnknownCarrierMovementIdException e) {
  47. handleUnknownCarrierMovementId(e);
  48. } catch (UnknownTrackingIdException e) {
  49. handleUnknownTrackingId(e);
  50. } catch (UnknownLocationException e) {
  51. handleOtherError(e);
  52. }
  53. }
  54. });
  55. } catch (IllegalArgumentException iae) {
  56. handleIllegalArgument(iae);
  57. } catch (ParseException pe) {
  58. handleInvalidDateFormat(completionTime);
  59. } catch (InvalidEventTypeException iete) {
  60. handleInvalidEventType(iete);
  61. } catch (Exception e) {
  62. handleOtherError(e);
  63. }
  64. }
  65. private void handleIllegalArgument(IllegalArgumentException iae) {
  66. logger.error(iae, iae);
  67. }
  68. private void handleOtherError(Exception e) {
  69. logger.error(e, e);
  70. }
  71. private void handleInvalidEventType(InvalidEventTypeException iete) {
  72. logger.error(iete, iete);
  73. }
  74. private void handleInvalidDateFormat(String completionTime) {
  75. logger.error("Invalid date format: " + completionTime + ", must be on ISO 8601 format: " + ISO_8601_FORMAT);
  76. }
  77. private HandlingEvent.Type parseEventType(final String eventType) throws InvalidEventTypeException {
  78. try {
  79. return HandlingEvent.Type.valueOf(eventType);
  80. } catch (IllegalArgumentException e) {
  81. throw new InvalidEventTypeException(eventType);
  82. }
  83. }
  84. private void handleUnknownCarrierMovementId(UnknownCarrierMovementIdException e) {
  85. logger.info("Placing event in retry queue due to: " + e.getMessage());
  86. }
  87. private void handleUnknownTrackingId(Exception e) {
  88. logger.info("Placing event in retry queue due to: " + e.getMessage());
  89. }
  90. private Date parseIso8601Date(final String completionTime) throws ParseException {
  91. return new SimpleDateFormat(ISO_8601_FORMAT).parse(completionTime);
  92. }
  93. public void setHandlingEventService(final HandlingEventService handlingEventService) {
  94. this.handlingEventService = handlingEventService;
  95. }
  96. public void setTransactionManager(PlatformTransactionManager transactionManager) {
  97. transactionTemplate = new TransactionTemplate(transactionManager);
  98. }
  99. }