View Javadoc
1   package org.kuali.ole.deliver.controller.checkout;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.deliver.bo.*;
7   import org.kuali.ole.deliver.drools.DroolsExchange;
8   import org.kuali.ole.deliver.form.CircForm;
9   import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemRecord;
10  import org.kuali.rice.krad.util.GlobalVariables;
11  
12  import java.sql.Timestamp;
13  import java.text.SimpleDateFormat;
14  import java.util.*;
15  import java.util.regex.Matcher;
16  import java.util.regex.Pattern;
17  
18  /**
19   * Created by pvsubrah on 6/4/15.
20   */
21  public class CheckoutUIController extends CheckoutBaseController {
22      private static final Logger LOG = Logger.getLogger(CheckoutUIController.class);
23  
24      private CircForm getCircForm(DroolsExchange droolsExchange){
25          return (CircForm) droolsExchange.getContext().get("circForm");
26      }
27  
28      @Override
29      public ItemRecord getItemRecord(DroolsExchange droolsExchange) {
30          return getCircForm(droolsExchange).getItemRecord();
31      }
32  
33      @Override
34      public String getOperatorId(DroolsExchange droolsExchange) {
35          return getLoginUserId();
36      }
37  
38      @Override
39      public void setItemRecord(DroolsExchange droolsExchange, ItemRecord itemRecord) {
40          getCircForm(droolsExchange).setItemRecord(itemRecord);
41      }
42  
43      @Override
44      public String getItemBarcode(DroolsExchange droolsExchange) {
45          return getCircForm(droolsExchange).getItemBarcode();
46      }
47  
48      @Override
49      public void setItemBarcode(DroolsExchange droolsExchange, String itemBarcode) {
50          getCircForm(droolsExchange).setItemBarcode(itemBarcode);
51      }
52  
53      @Override
54      public OlePatronDocument getCurrentBorrower(DroolsExchange droolsExchange) {
55          return getCircForm(droolsExchange).getPatronDocument();
56      }
57  
58      @Override
59      public void setItemValidationDone(boolean result, DroolsExchange droolsExchange) {
60          getCircForm(droolsExchange).setItemValidationDone(true);
61      }
62  
63      @Override
64      public OleCirculationDesk getSelectedCirculationDesk(DroolsExchange droolsExchange) {
65          OleCirculationDesk oleCirculationDesk = getCircDeskLocationResolver().getCircDeskForOpertorId
66                  (GlobalVariables.getUserSession().getPrincipalId());
67          return oleCirculationDesk;
68      }
69  
70      @Override
71      public void addLoanDocumentToCurrentSession(OleLoanDocument oleLoanDocument, DroolsExchange droolsExchange) {
72          getCircForm(droolsExchange).getLoanDocumentListForCurrentSession().add(oleLoanDocument);
73      }
74  
75      @Override
76      public boolean processCustomDueDateIfSet(DroolsExchange droolsExchange, OleLoanDocument oleLoanDocument) {
77          if (null != getCircForm(droolsExchange).getCustomDueDateMap()) {
78              try {
79                  processCustomDueDate(droolsExchange, oleLoanDocument);
80              } catch (Exception e) {
81                  return true;
82              }
83          }
84          return false;
85      }
86  
87      private void processCustomDueDate(DroolsExchange droolsExchange, OleLoanDocument oleLoanDocument) throws Exception {
88          boolean timeFlag = false;
89          Timestamp timestamp;
90          Pattern pattern;
91          Matcher matcher;
92          SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OlePatron.PATRON_MAINTENANCE_DATE_FORMAT);
93  
94          String customDueDateTime = getCircForm(droolsExchange).getCustomDueDateTime();
95          Date customDueDateMap = getCircForm(droolsExchange).getCustomDueDateMap();
96  
97          if (StringUtils.isNotBlank(customDueDateTime)) {
98              String[] str = customDueDateTime.split(":");
99              pattern = Pattern.compile(OLEConstants.TIME_24_HR_PATTERN);
100             matcher = pattern.matcher(customDueDateTime);
101             timeFlag = matcher.matches();
102             if (timeFlag) {
103                 if (str != null && str.length <= 2) {
104                     getCircForm(droolsExchange).setCustomDueDateTime(customDueDateTime + OLEConstants.CHECK_IN_TIME_MS);
105                 }
106                 timestamp = Timestamp.valueOf(new SimpleDateFormat(OLEConstants.CHECK_IN_DATE_TIME_FORMAT).format(customDueDateMap).concat(" ").concat(customDueDateTime));
107             } else {
108                 getCircForm(droolsExchange).setCustomDueDateTimeMessage(OLEConstants.DUE_DATE_TIME_FORMAT_MESSAGE);
109                 throw new Exception();
110             }
111         } else if (fmt.format(customDueDateMap).compareTo(fmt.format(new Date())) == 0) {
112             timestamp = new Timestamp(new Date().getTime());
113         } else {
114             timestamp = Timestamp.valueOf(new SimpleDateFormat(OLEConstants.CHECK_IN_DATE_TIME_FORMAT).format(customDueDateMap).concat(" ").concat(new SimpleDateFormat("HH:mm:ss").format(new Date())));
115         }
116         oleLoanDocument.setLoanDueDate(timestamp);
117         oleLoanDocument.setCirculationPolicyId("No Circulation Policy Matched");
118     }
119 
120     @Override
121     public void setDueDateTimeForItemRecord(DroolsExchange droolsExchange, Timestamp loanDueDate) {
122         getItemRecord(droolsExchange).setDueDateTime(loanDueDate);
123     }
124 
125     @Override
126     public void addCurrentLoandDocumentToListofLoandedToPatron(DroolsExchange droolsExchange, OleLoanDocument oleLoanDocument) {
127         getCircForm(droolsExchange).getPatronDocument().getOleLoanDocuments().add(oleLoanDocument);
128     }
129 
130     @Override
131     public void removeCurrentLoanDocumentFromCurrentSession(DroolsExchange droolsExchange, OleLoanDocument oleLoanDocument) {
132         getCircForm(droolsExchange).getLoanDocumentListForCurrentSession().remove(oleLoanDocument);
133     }
134 
135     @Override
136     public void clearCurrentSessionList(DroolsExchange droolsExchange) {
137         getCircForm(droolsExchange).getLoanDocumentListForCurrentSession().clear();
138     }
139 
140     @Override
141     public String getCirculationLocationId(DroolsExchange droolsExchange) {
142         return getCircForm(droolsExchange).getSelectedCirculationDesk();
143     }
144 }