View Javadoc
1   package org.kuali.ole.deliver.controller.renew;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang3.StringUtils;
5   import org.apache.log4j.Logger;
6   import org.kuali.ole.deliver.bo.OLEDeliverNotice;
7   import org.kuali.ole.deliver.bo.OleLoanDocument;
8   import org.kuali.ole.deliver.bo.OlePatronDocument;
9   import org.kuali.ole.deliver.controller.checkout.CircUtilController;
10  import org.kuali.ole.deliver.form.CircForm;
11  import org.kuali.ole.deliver.util.*;
12  import org.kuali.ole.docstore.common.document.DocstoreDocument;
13  import org.kuali.ole.docstore.common.document.Item;
14  import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemRecord;
15  import org.kuali.ole.ncip.service.impl.NonSip2RenewItemService;
16  import org.kuali.ole.ncip.service.impl.RenewItemsService;
17  import org.kuali.ole.utility.OleStopWatch;
18  
19  import java.util.*;
20  
21  /**
22   * Created by sheiksalahudeenm on 8/13/15.
23   */
24  public class RenewController extends CircUtilController {
25  
26      private static final Logger LOG = Logger.getLogger(RenewController.class);
27      private RenewItemControllerUtil renewItemControllerUtil;
28      private RenewItemsService renewItemsService;
29  
30      public DroolsResponse renewItems(List<OleLoanDocument> selectedLoanDocumentList, OlePatronDocument olePatronDocument) {
31          List<Item> itemList = new ArrayList<>();
32          DroolsResponse finalDroolResponse = new DroolsResponse();
33  
34          for (Iterator<OleLoanDocument> iterator = selectedLoanDocumentList.iterator(); iterator.hasNext(); ) {
35              DroolsResponse droolsResponse = new DroolsResponse();
36              OleLoanDocument oleLoanDocument = iterator.next();
37  
38              boolean isIndefiniteDueDate = oleLoanDocument.IsIndefiniteDueDate();
39              if (!isIndefiniteDueDate) {
40  
41                  List<Object> facts = new ArrayList<>();
42  
43                  oleLoanDocument.setPastDueDate(oleLoanDocument.getLoanDueDate());
44                  ItemRecord itemRecord = getItemRecordByBarcode(oleLoanDocument.getItemId());
45                  OleItemRecordForCirc oleItemRecordForCirc = ItemInfoUtil.getInstance().getOleItemRecordForCirc(itemRecord);
46  
47                  NoticeInfo noticeInfo = new NoticeInfo();
48                  facts.add(oleLoanDocument);
49                  facts.add(olePatronDocument);
50                  facts.add(oleItemRecordForCirc);
51                  facts.add(droolsResponse);
52                  facts.add(noticeInfo);
53                  fireRules(facts, null, "renewal validation");
54  
55                  if (droolsResponse.isRuleMatched()) {
56                      if (StringUtils.isBlank(droolsResponse.retrieveErrorMessage())) {
57                          if (!oleLoanDocument.isPastAndRenewDueDateSame()) {
58                              Integer numRenewals = getRenewItemControllerUtil().IncrementRenewalCount(oleLoanDocument);
59                              oleLoanDocument.setNumberOfRenewals(numRenewals.toString());
60                              List<OLEDeliverNotice> oleDeliverNotices = processNotices(noticeInfo, oleLoanDocument, oleItemRecordForCirc.getItemRecord());
61                              oleLoanDocument.setDeliverNotices(oleDeliverNotices);
62                              if (null != oleLoanDocument.getLoanId()) {
63                                  Item itemForUpdate = getItemForUpdate(oleLoanDocument);
64                                  if (null != itemForUpdate) {
65                                      itemList.add(itemForUpdate);
66                                      droolsResponse.setSucessMessage("Successfully Renewed");
67                                      droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
68                                      finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
69                                  }
70                              }
71                          } else {
72                              droolsResponse.addErrorMessageCode("PastAndNewDueDateSame");
73                              droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
74                              finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
75                          }
76                      } else {
77                          droolsResponse.addErrorMessageCode("ErrorMessageFromRule");
78                          droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
79                          finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
80                      }
81                  } else {
82                      droolsResponse.addErrorMessageCode("No renewal policy found");
83                      droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
84                      finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
85                  }
86              } else {
87                  droolsResponse.addErrorMessageCode("InDefiniteDueDate");
88                  droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
89                  finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
90              }
91          }
92  
93  
94          getRenewItemsService().processBulkUpdate(itemList, finalDroolResponse);
95  
96          return finalDroolResponse;
97  
98      }
99  
100     public DroolsResponse proceedToSaveLoanDocuments(List<OleLoanDocument> loanDocumentsForRenew) {
101         DroolsResponse finalDroolResponse = new DroolsResponse();
102         List<Item> updateItemList = new ArrayList<>();
103         for (Iterator<OleLoanDocument> iterator = loanDocumentsForRenew.iterator(); iterator.hasNext(); ) {
104             DroolsResponse droolsResponse = new DroolsResponse();
105             OleLoanDocument oleLoanDocument = iterator.next();
106             Item itemForUpdate = getItemForUpdate(oleLoanDocument);
107             if (null != itemForUpdate) {
108                 updateItemList.add(itemForUpdate);
109                 droolsResponse.setSucessMessage("Successfully Renewed");
110                 droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
111                 finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
112             }
113         }
114         getRenewItemsService().processBulkUpdate(updateItemList, finalDroolResponse);
115         return finalDroolResponse;
116 
117     }
118 
119 
120 
121     public Item getItemForUpdate(OleLoanDocument oleLoanDocument) {
122         Map updateParameters = getRenewItemControllerUtil().getUpdateParameters(oleLoanDocument);
123         return getItemForUpdate(updateParameters);
124     }
125 
126     public String analyzeRenewedLoanDocuments(CircForm circForm, DroolsResponse droolsResponse) {
127         StringBuilder messageContentForRenew = new StringBuilder();
128         Map<String, Object> context = droolsResponse.getDroolsExchange().getContext();
129         if(context.size() > 0){
130             for (Iterator<String> iterator = context.keySet().iterator(); iterator.hasNext(); ) {
131                 String key = iterator.next();
132                 DroolsResponse individualDroolResponse = (DroolsResponse) context.get(key);
133                 if(StringUtils.isNotBlank(individualDroolResponse.getSucessMessage()) && individualDroolResponse.getSucessMessage().equalsIgnoreCase("Successfully Renewed")){
134                     OleLoanDocument oleLoanDocument = (OleLoanDocument) individualDroolResponse.getDroolsExchange().getContext().get(key);
135                     String content = "Successfully renewed for item ("+oleLoanDocument.getItemId()+")";
136                     appendContentToStrinBuilder(messageContentForRenew, content);
137                 }else if(individualDroolResponse.retriveErrorCode().equalsIgnoreCase("No renewal policy found")){
138                     Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
139                     OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
140                     if (null != individualLoanDocument) {
141                         individualLoanDocument.setNonCirculatingItem(true);
142                         if(null == circForm.getLoanDocumentsForRenew()){
143                             circForm.setLoanDocumentsForRenew(new ArrayList<OleLoanDocument>());
144                         }
145                         individualLoanDocument.setErrorMessage(null);
146                         individualLoanDocument.setErrorMessage("No renewal policy found for this item.");
147                         circForm.getLoanDocumentsForRenew().add(individualLoanDocument);
148                     }
149                 }else if(individualDroolResponse.retriveErrorCode().equalsIgnoreCase("ErrorMessageFromRule")){
150                     Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
151                     OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
152                     if (null != individualLoanDocument) {
153                         individualLoanDocument.setNonCirculatingItem(false);
154                         if(null == circForm.getLoanDocumentsForRenew()){
155                             circForm.setLoanDocumentsForRenew(new ArrayList<OleLoanDocument>());
156                         }
157                         individualLoanDocument.setErrorMessage(null);
158                         individualLoanDocument.setErrorMessage(individualDroolResponse.retrieveErrorMessage());
159                         circForm.getLoanDocumentsForRenew().add(individualLoanDocument);
160                     }
161                 }else if(individualDroolResponse.retriveErrorCode().equalsIgnoreCase("PastAndNewDueDateSame")){
162                     Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
163                     OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
164                     String content = "Item ("+individualLoanDocument.getItemId() + ") was not renewed because the new due date/time would not change.";
165                     appendContentToStrinBuilder(messageContentForRenew, content);
166                 }else if(individualDroolResponse.retriveErrorCode().equalsIgnoreCase("InDefiniteDueDate")){
167                     Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
168                     OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
169                     String content = "Item ("+individualLoanDocument.getItemId() + ") wasn't renewed. Items on indefinite loan do not need to be renewed.";
170                     appendContentToStrinBuilder(messageContentForRenew, content);
171                 }
172             }
173         }
174         return messageContentForRenew.toString();
175     }
176 
177     private void appendContentToStrinBuilder(StringBuilder stringBuilder, String content) {
178         if(stringBuilder.length() > 0){
179             stringBuilder.append("<br/>").append(content);
180         }else{
181             stringBuilder.append(content);
182         }
183     }
184 
185     public void prepareResponeMessage(CircForm circForm, String responseForNonCirculatingItem) {
186         ErrorMessage errorMessage = circForm.getErrorMessage();
187 
188         if(null == errorMessage){
189             errorMessage = new ErrorMessage();
190         }
191         String errorMessageContent = errorMessage.getErrorMessage();
192         if(StringUtils.isBlank(errorMessageContent)){
193             errorMessage.setErrorMessage(responseForNonCirculatingItem);
194         }else{
195             errorMessage.setErrorMessage("<br/>" + responseForNonCirculatingItem);
196         }
197         circForm.setErrorMessage(errorMessage);
198     }
199 
200     public String prepareResponseForRenewedLoanDocuments(DroolsResponse droolsResponse) {
201         StringBuilder stringBuilder = new StringBuilder();
202         Map<String, Object> context = droolsResponse.getDroolsExchange().getContext();
203         if(context.size() > 0){
204             for (Iterator<String> iterator = context.keySet().iterator(); iterator.hasNext(); ) {
205                 String key = iterator.next();
206                 DroolsResponse individualDroolResponse = (DroolsResponse) context.get(key);
207                 if(StringUtils.isNotBlank(individualDroolResponse.getSucessMessage()) && individualDroolResponse.getSucessMessage().equalsIgnoreCase("Successfully Renewed")){
208                     OleLoanDocument oleLoanDocument = (OleLoanDocument) individualDroolResponse.getDroolsExchange().getContext().get(key);
209                     String content = "Successfully renewed for item ("+oleLoanDocument.getItemId()+")";
210                     appendContentToStrinBuilder(stringBuilder, content);
211                 }else {
212                     OleLoanDocument oleLoanDocument = (OleLoanDocument) individualDroolResponse.getDroolsExchange().getContext().get(key);
213                     String content = "Renewed failed for item ("+oleLoanDocument.getItemId()+")";
214                     appendContentToStrinBuilder(stringBuilder, content);
215                 }
216             }
217         }
218         return stringBuilder.toString();
219     }
220 
221     public RenewItemControllerUtil getRenewItemControllerUtil() {
222         if (null == renewItemControllerUtil) {
223             renewItemControllerUtil = new RenewItemControllerUtil();
224         }
225         return renewItemControllerUtil;
226     }
227 
228     public void setRenewItemControllerUtil(RenewItemControllerUtil renewItemControllerUtil) {
229         this.renewItemControllerUtil = renewItemControllerUtil;
230     }
231 
232     public RenewItemsService getRenewItemsService() {
233         if (null == renewItemsService) {
234             renewItemsService = new NonSip2RenewItemService();
235         }
236         return renewItemsService;
237     }
238 
239     public void setRenewItemsService(RenewItemsService renewItemsService) {
240         this.renewItemsService = renewItemsService;
241     }
242 }