Ladies_Man

boxReservationThirdStepView

Feb 17th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.91 KB | None | 0 0
  1. package ru.pochta.abon.ui.view;
  2.  
  3. import com.vaadin.navigator.View;
  4. import com.vaadin.navigator.ViewChangeListener;
  5. import com.vaadin.shared.ui.MarginInfo;
  6. import com.vaadin.shared.ui.label.ContentMode;
  7. import com.vaadin.spring.annotation.SpringView;
  8. import com.vaadin.ui.*;
  9. import org.apache.commons.lang3.exception.ExceptionUtils;
  10. import org.apache.log4j.Logger;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.context.MessageSource;
  13. import ru.pochta.abon.container.Container;
  14. import ru.pochta.abon.container.ReserveOperation;
  15. import ru.pochta.abon.library.dto.abonClient.dto.*;
  16. import ru.pochta.abon.library.dto.abonClient.enumeration.ClaimStatusEnum;
  17. import ru.pochta.abon.library.dto.abonClient.enumeration.PaymentTypeEnum;
  18. import ru.pochta.abon.library.dto.abonClient.util.AbstractEntityUtil;
  19. import ru.pochta.abon.library.service.abonClient.ClaimService;
  20. import ru.pochta.abon.library.service.abonClient.ServiceManager;
  21. import ru.pochta.abon.library.service.abonClient.exception.RestException;
  22. import ru.pochta.abon.library.service.reporting.PrintableForm;
  23. import ru.pochta.abon.service.notification.MailService;
  24. import ru.pochta.abon.service.notification.NotificationService;
  25. import ru.pochta.abon.service.notification.SendEmailRunnable;
  26. import ru.pochta.abon.service.notification.SendSmsRunnable;
  27. import ru.pochta.abon.ui.component.AbonHeader;
  28. import ru.pochta.abon.ui.component.BoxReservationButtons;
  29. import ru.pochta.abon.ui.component.BoxReservationHeader;
  30. import ru.pochta.abon.ui.window.ConfirmationDialog;
  31. import ru.pochta.abon.util.ClaimHelper;
  32. import ru.pochta.abon.util.ExceptionNotificationUtil;
  33. import ru.pochta.abon.util.Helper;
  34.  
  35. import javax.annotation.PostConstruct;
  36. import java.time.ZonedDateTime;
  37. import java.time.format.DateTimeFormatter;
  38. import java.util.List;
  39. import java.util.StringJoiner;
  40.  
  41. import static ru.pochta.abon.payments.PaymentProcessor.createPayment;
  42. import static ru.pochta.abon.payments.PaymentProcessor.sendToEAS;
  43.  
  44. @SpringView(name = BoxReservationThirdStepView.VIEW_NAME)
  45. public class BoxReservationThirdStepView extends VerticalLayout implements View {
  46.  
  47.     public static final String VIEW_NAME = "boxReservationThirdStep";
  48.     public static final int STEP_NUMBER = 3;
  49.     private static final Logger LOG = Logger.getLogger(BoxReservationThirdStepView.class);
  50.  
  51.     @Autowired
  52.     private MessageSource messageSource;
  53.  
  54.     @Autowired
  55.     private Container container;
  56.  
  57.     @Autowired
  58.     private ServiceManager serviceManager;
  59.  
  60.     @Autowired
  61.     private NotificationService notificationService;
  62.  
  63.     @Autowired
  64.     private MailService mailService;
  65.  
  66.     @Autowired
  67.     private PrintableForm printableForm;
  68.  
  69.     private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
  70.     private Claim claim;
  71.     private Client client;
  72.     private ReserveOperation reserveOperation;
  73.  
  74.     private String claimId;
  75.     private String totalCost;
  76.  
  77.     @PostConstruct
  78.     void init() {
  79.         setSizeFull();
  80.         setSpacing(true);
  81.         addStyleName("light");
  82.     }
  83.  
  84.     @Override
  85.     public void enter(ViewChangeListener.ViewChangeEvent viewChangeEvent) {
  86.         claim = container.getReserveOperation().getClaim();
  87.         client = claim.getClient();
  88.         reserveOperation = container.getReserveOperation();
  89.         if (claim != null) {
  90.             claimId = claim.getEntityId().toString();
  91.             totalCost = claim.getTotalCost().toString();
  92.         }
  93.         addComponents();
  94.     }
  95.  
  96.     private void addComponents() {
  97.         addPostOfficeCardHeader();
  98.         addBoxReservationHeader();
  99.         addMainLayout();
  100.         addReservationButtons();
  101.     }
  102.  
  103.     private void addPostOfficeCardHeader() {
  104.         AbonHeader header = new AbonHeader(container, messageSource, serviceManager, null, false);
  105.         addComponent(header);
  106.         setExpandRatio(header, 0.1f);
  107.     }
  108.  
  109.  
  110.     private void addBoxReservationHeader() {
  111.         BoxReservationHeader boxReservationHeader =
  112.                 new BoxReservationHeader(STEP_NUMBER,
  113.                         messageSource.getMessage("reserveBoxView.headerString", null, getLocale()),
  114.                         claimId);
  115.         addComponent(boxReservationHeader);
  116.         setExpandRatio(boxReservationHeader, 0.05f);
  117.     }
  118.  
  119.     private void addMainLayout() {
  120.         VerticalLayout mainLayout = new VerticalLayout();
  121.         mainLayout.setMargin(new MarginInfo(false, true, false, true));
  122.         mainLayout.setSizeFull();
  123.         addComponent(mainLayout);
  124.         setExpandRatio(mainLayout, 0.75f);
  125.         setComponentAlignment(mainLayout, Alignment.MIDDLE_CENTER);
  126.         FormLayout payForm = new FormLayout();
  127.         TextField claimNumber = new TextField(messageSource.getMessage("abonThirdStep.claimNumber", null, getLocale()), claimId);
  128.         claimNumber.setEnabled(false);
  129.         payForm.addComponent(claimNumber);
  130.         TextField totalPrice = new TextField(messageSource.getMessage("abonThirdStep.totalPrice", null, getLocale()), totalCost);
  131.         payForm.addComponent(totalPrice);
  132.         totalPrice.setEnabled(false);
  133.         mainLayout.addComponent(payForm);
  134.     }
  135.  
  136.  
  137.     private void addReservationButtons() {
  138.         BoxReservationButtons reservationButtons = getNavigationButtons();
  139.  
  140.  
  141.         reservationButtons.setNextButtonEnabled(false);
  142.  
  143.         reservationButtons.setBackButtonEventListener(clickEvent -> {
  144.  
  145.             ConfirmationDialog dialog = Helper
  146.                     .checkIfClaimIsOutOfEditingModeOrCancelledAndRedirectToClaims(
  147.                             claim,
  148.                             container,
  149.                             serviceManager,
  150.                             messageSource,
  151.                             getLocale(),
  152.                             getUI()
  153.                     );
  154.             if (dialog != null) {
  155.                 getUI().addWindow(dialog);
  156.                 return;
  157.             }
  158.             // we didn't quit editing mode if we got there
  159.             Helper.updateClaimJustToHaveActualUpdateTime(claim, serviceManager, messageSource, getLocale(), getUI());
  160.  
  161.             getUI().getNavigator().navigateTo(BoxReservationSecondStepView.VIEW_NAME);
  162.         });
  163.  
  164.         reservationButtons.setSaveButtonEventListener(clickEvent -> {
  165.  
  166.             ConfirmationDialog dialog = Helper
  167.                     .checkIfClaimIsOutOfEditingModeOrCancelledAndRedirectToClaims(
  168.                             claim,
  169.                             container,
  170.                             serviceManager,
  171.                             messageSource,
  172.                             getLocale(),
  173.                             getUI()
  174.                     );
  175.             if (dialog != null) {
  176.                 getUI().addWindow(dialog);
  177.                 return;
  178.             }
  179.             // we didn't quit editing mode if we got there
  180.  
  181.             // reflect that the claim isn't in editing any longer
  182.             if (claim != null) {
  183.                 Claim tempClaim = AbstractEntityUtil.createTempEntity(claim, Claim.class);
  184.                 tempClaim.setInEditing(false);
  185.  
  186.                 try {
  187.                     serviceManager.getClaimService().update(tempClaim);
  188.                 } catch (RestException e) {
  189.                     e.printStackTrace();
  190.                 }
  191.             } else {
  192.                 LOG.warn("claim is null");
  193.             }
  194.  
  195.             getUI().getNavigator().navigateTo(PostOfficeCardView.VIEW_NAME);
  196.         });
  197.  
  198.         reservationButtons.setCancelButtonEventListener(clickEvent -> {
  199.  
  200.             ConfirmationDialog dialog = Helper
  201.                     .checkIfClaimIsOutOfEditingModeOrCancelledAndRedirectToClaims(
  202.                             claim,
  203.                             container,
  204.                             serviceManager,
  205.                             messageSource,
  206.                             getLocale(),
  207.                             getUI()
  208.                     );
  209.             if (dialog != null) {
  210.                 getUI().addWindow(dialog);
  211.                 return;
  212.             }
  213.             // we didn't quit editing mode if we got there
  214.  
  215.             ConfirmationDialog confirmationDialog = new ConfirmationDialog(
  216.                     messageSource.getMessage("abonFirstStep.CancelReservationCaption", null, getLocale()),
  217.                     messageSource.getMessage("abonFirstStep.CancelReservationQuestion", null, getLocale()),
  218.                     messageSource.getMessage("button.yes", null, getLocale()),
  219.                     messageSource.getMessage("button.no", null, getLocale())
  220.             );
  221.  
  222.             confirmationDialog.setOkButtonListener(event -> {
  223.  
  224.                 ConfirmationDialog dialogInner = Helper
  225.                         .checkIfClaimIsOutOfEditingModeOrCancelledAndRedirectToClaims(
  226.                                 claim,
  227.                                 container,
  228.                                 serviceManager,
  229.                                 messageSource,
  230.                                 getLocale(),
  231.                                 getUI()
  232.                         );
  233.                 if (dialogInner != null) {
  234.                     getUI().addWindow(dialogInner);
  235.                     return;
  236.                 }
  237.                 // we didn't quit editing mode if we got there
  238.  
  239.                 // Setting status Cancelled for the claim.
  240.                 // Involved Boxes are freed implicitly, no need in calls of boxService.update().
  241.                 Claim claim = container.getReserveOperation().getClaim();
  242.                 Claim tempClaim = AbstractEntityUtil.createTempEntity(claim, Claim.class);
  243.                 tempClaim
  244.                         .setClaimStatus(container
  245.                                 .getClaimStatus(ClaimStatusEnum
  246.                                         .CANCELLED));
  247.                 try {
  248.                     ClaimService claimService = serviceManager.getClaimService();
  249.                     claimService.update(tempClaim);
  250.                 } catch (RestException e) {
  251.                     Notification notification = ExceptionNotificationUtil.getNotification(e, messageSource, getLocale());
  252.                     notification.show(getUI().getPage());
  253.                 }
  254.  
  255.                 // Cleaning the container!
  256.                 // But still need reference to reserveOperation in order to set selected tab.
  257.                 container.cleanBoxReservationSpecificStuff();
  258.  
  259.                 confirmationDialog.close();
  260.                 getUI().getNavigator().navigateTo(PostOfficeCardView.VIEW_NAME);
  261.             });
  262.  
  263.             confirmationDialog.setCancelButtonListener(event -> {
  264.                 // Cancel*no = Ok, just stay on the page
  265.                 confirmationDialog.close();
  266.             });
  267.  
  268.             getUI().addWindow(confirmationDialog);
  269.         });
  270.  
  271.         if (container.getLoggedViaEASOPS()) {
  272.             reservationButtons.setTransferAndFinalizeButtonEventListener(clickEvent -> {
  273.  
  274.                 ConfirmationDialog dialog = Helper
  275.                         .checkIfClaimIsOutOfEditingModeOrCancelledAndRedirectToClaims(
  276.                                 claim,
  277.                                 container,
  278.                                 serviceManager,
  279.                                 messageSource,
  280.                                 getLocale(),
  281.                                 getUI()
  282.                         );
  283.                 if (dialog != null) {
  284.                     getUI().addWindow(dialog);
  285.                     return;
  286.                 }
  287.                 // we didn't quit editing mode if we got there
  288.                 String str = "";
  289.                 try {
  290.                     processClaim();
  291.                     PaymentType eas = container.getPaymentType(PaymentTypeEnum.EAS);
  292.                     Payment payment = null;
  293.                     try {
  294.                         payment = createPayment(claim, eas, false, container, serviceManager);
  295.                     } catch (Exception e) {
  296.                         e.printStackTrace();
  297.                         LOG.error("Couldn't calculate price via tarificator");
  298.                         Notification.show(
  299.                                 messageSource.getMessage("abonThirdStep.boxesPriceError", null, getLocale()),
  300.                                 Notification.Type.TRAY_NOTIFICATION);
  301.                     }
  302.                     if (payment != null && payment.getEntityId() != null) {
  303.                         reservationButtons.setTransferAndFinalizeButtonEnabled(false);
  304.                         Notification.show(
  305.                                 messageSource.getMessage("abonThirdStep.PaymentCreatedCaption", null, getLocale()),
  306.                                 messageSource.getMessage("abonThirdStep.PaymentCreatedText",
  307.                                         new Object[]{payment.getEntityId(), payment.getPaymentStatus().getStatus()}, getLocale()),
  308.                                 Notification.Type.TRAY_NOTIFICATION);
  309.                         sendToEAS(claim, payment);
  310.                     }
  311.                 } catch (RestException e) {
  312.                     LOG.error("status of claim " + claimId + "is not changed");
  313.                 }
  314.             });
  315.         } else {
  316.             reservationButtons.setTransferAndFinalizeButtonEventListener(clickEvent -> {
  317.  
  318.                 ConfirmationDialog dialog = Helper
  319.                         .checkIfClaimIsOutOfEditingModeOrCancelledAndRedirectToClaims(
  320.                                 claim,
  321.                                 container,
  322.                                 serviceManager,
  323.                                 messageSource,
  324.                                 getLocale(),
  325.                                 getUI()
  326.                         );
  327.                 if (dialog != null) {
  328.                     getUI().addWindow(dialog);
  329.                     return;
  330.                 }
  331.                 // we didn't quit editing mode if we got there
  332.  
  333.                 Window subWindow = new Window(messageSource.getMessage("abonThirdStep.windowTitle", null, getLocale()));
  334.                 subWindow.center();
  335.                 subWindow.setModal(true);
  336.                 subWindow.setResizable(false);
  337.                 VerticalLayout infoLayout = new VerticalLayout();
  338.                 infoLayout.setMargin(true);
  339.                 infoLayout.setSpacing(true);
  340.                 subWindow.setContent(infoLayout);
  341.                 infoLayout.addComponent(new Label(messageSource.getMessage("abonThirdStep.claimIssued", new Object[]{claimId}, getLocale()), ContentMode.HTML));
  342.                 HorizontalLayout buttonsLayout = new HorizontalLayout();
  343.                 buttonsLayout.setSpacing(true);
  344.                 Button okButton = new Button(messageSource.getMessage("button.Ok", null, getLocale()));
  345.                 okButton.addClickListener(click -> {
  346.  
  347.                     ConfirmationDialog dialogInner = Helper
  348.                             .checkIfClaimIsOutOfEditingModeOrCancelledAndRedirectToClaims(
  349.                                     claim,
  350.                                     container,
  351.                                     serviceManager,
  352.                                     messageSource,
  353.                                     getLocale(),
  354.                                     getUI()
  355.                             );
  356.                     if (dialogInner != null) {
  357.                         getUI().addWindow(dialogInner);
  358.                         return;
  359.                     }
  360.  
  361.                     try {
  362.  
  363.                         // we didn't quit editing mode if we got there
  364.                         processClaim(); // this one potentially throws an exception
  365.  
  366.                         // Assuming we want to create a payment only if the claim was processed without exceptions:
  367.                         // if we reached this line, then no exception has been thrown
  368.  
  369.  
  370.                         PaymentType notEas = container.getPaymentType(PaymentTypeEnum.NOT_EAS);
  371.                         try {
  372.                             createPayment(claim, notEas, false, container, serviceManager);
  373.                         } catch (Exception e) {
  374.                             e.printStackTrace();
  375.                             LOG.error("Couldn't calcualte price via tarificator");
  376.                             Notification.show(
  377.                                     messageSource.getMessage("abonThirdStep.boxesPriceError", null, getLocale()),
  378.                                     Notification.Type.TRAY_NOTIFICATION);
  379.                         }
  380.  
  381.                         sendEmailAndSMSNotification();
  382.                         subWindow.close();
  383.                         getUI().getNavigator().navigateTo(PostOfficeCardView.VIEW_NAME);
  384.                     } catch (RestException e) {
  385.                         Notification notification = ExceptionNotificationUtil.getNotification(e, messageSource, getLocale());
  386.                         notification.show(getUI().getPage());
  387.                     }
  388.                 });
  389.                 buttonsLayout.addComponent(okButton);
  390.                 Button printClaimButton = new Button(messageSource.getMessage("button.printClaim", null, getLocale()));
  391.                 printClaimButton.addClickListener(click -> {
  392.                     printableForm.print(claim);
  393.                 });
  394.                 buttonsLayout.addComponent(printClaimButton);
  395.  
  396.                 buttonsLayout.addComponent(new Button(messageSource.getMessage("button.printInvoice", null, getLocale())));
  397.                 infoLayout.addComponent(buttonsLayout);
  398.                 getUI().addWindow(subWindow);
  399.             });
  400.         }
  401.         addComponent(reservationButtons);
  402.         setExpandRatio(reservationButtons, 0.1f);
  403.     }
  404.  
  405.  
  406.     private BoxReservationButtons getNavigationButtons() {
  407.         BoxReservationButtons buttons;
  408.         if (!container.getLoggedViaEASOPS()) {
  409.             buttons = new BoxReservationButtons(
  410.                     messageSource.getMessage("button.back", null, getLocale()),
  411.                     messageSource.getMessage("button.cancelFin", null, getLocale()),
  412.                     messageSource.getMessage("button.saveAndExit", null, getLocale()),
  413.                     messageSource.getMessage("button.next", null, getLocale()),
  414.                     messageSource.getMessage("button.finalize", null, getLocale())
  415.             );
  416.         } else {
  417.             buttons = new BoxReservationButtons(
  418.                     messageSource.getMessage("button.back", null, getLocale()),
  419.                     messageSource.getMessage("button.cancelFin", null, getLocale()),
  420.                     messageSource.getMessage("button.saveAndExit", null, getLocale()),
  421.                     messageSource.getMessage("button.next", null, getLocale()),
  422.                     messageSource.getMessage("button.transfer", null, getLocale())
  423.             );
  424.         }
  425.         return buttons;
  426.     }
  427.  
  428.     private void processClaim() throws RestException {
  429.         //LOG.error("plugin string: " + container.getReserveOperation().getPluginString());
  430.         LOG.debug("processClaim() called for claim [" + claim.getEntityId() + "]");
  431.         claim.setClaimStatus(container.getClaimStatus(ClaimStatusEnum.AWAITING_PAYMENT));
  432.         // Editing is over, inEditing=false is set explicitly here.
  433.         // There is no else-if branch in ClaimController.update() for setting AWAITING_PAYMENT.
  434.         // So we fall through down to the branch related to inEditing
  435.         // and it will fire an error if current state is inEditing=true and new state is inEditing=true.
  436.  
  437.         claim.setInEditing(false);
  438.         ClaimService claimService = serviceManager.getClaimService();
  439.         claimService.update(claim);
  440.  
  441.  
  442.         //update parent status in case of prolongation case when parent is already finished
  443.         if (reserveOperation.isProlongateMode() || (claim != null && claim.getParent() != null)) {
  444.             if (ClaimHelper.isFinished(claim.getParent().getClaimStatus())) {
  445.                 Claim tempClaim = AbstractEntityUtil.createTempEntity(claim.getParent(), Claim.class);
  446.                 tempClaim.setClaimStatus(container.getClaimStatus(ClaimStatusEnum.PROLONGATED));
  447.                 claimService.update(tempClaim);
  448.             }
  449.  
  450.         }
  451.     }
  452.  
  453.     private void sendEmailAndSMSNotification() {
  454.         //send email confirmation
  455.         try {
  456.             String email = client.getEmail();
  457.             String index = claim.getPostOffice().getIndex();
  458.             String postOfficeAddress = claim.getPostOffice().getAddress();
  459.             ZonedDateTime paymentDeadline = (claim.getCreateDate() != null) ? claim.getCreateDate().plusDays(14) : null;
  460.             DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
  461.                     messageSource.getMessage("datePattern", null, getLocale()));
  462.             String paymentDeadlineStr = (paymentDeadline != null) ? paymentDeadline.format(formatter) : "?";
  463.             List<Box> boxes = claim.getBoxes();
  464.             StringJoiner boxNumbers = new StringJoiner(", ");
  465.             boxes.forEach(box -> {
  466.                 boxNumbers.add(box.getNumber());
  467.             });
  468.             String confirmationText = messageSource.getMessage("abonThirdStep.reservationConfirmation", new Object[]{index, postOfficeAddress, boxNumbers, paymentDeadlineStr}, getLocale());
  469.             String mailSubject = messageSource.getMessage("abonThirdStep.reservationConfirmationSubj", null, getLocale());
  470.             String mailSignature = messageSource.getMessage("abonThirdStep.reservationConfirmationEmailSignature", null, getLocale());
  471.             if (email != null && email.trim() != "") {
  472.                 (new Thread(new SendEmailRunnable(mailService, email, mailSubject, confirmationText, mailSignature)))
  473.                         .start();
  474.             }
  475.         } catch (Exception e) {
  476.             LOG.error("Couldn't send e-mail reservation confirmation" + ExceptionUtils.getStackTrace(e));
  477.         }
  478.         //send SMS
  479.         (new Thread(new SendSmsRunnable(notificationService, client.getPhoneNumber(), messageSource.getMessage("abonThirdStep.sms.text", null, getLocale()))))
  480.                 .start();
  481.     }
  482. }
Advertisement
Add Comment
Please, Sign In to add comment