View Javadoc
1   package org.kuali.ole.deliver.controller.checkin;
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.DocumentUniqueIDPrefix;
7   import org.kuali.ole.OLEConstants;
8   import org.kuali.ole.deliver.OleLoanDocumentsFromSolrBuilder;
9   import org.kuali.ole.deliver.PatronBillGenerator;
10  import org.kuali.ole.deliver.bo.*;
11  import org.kuali.ole.deliver.calendar.service.impl.OleCalendarServiceImpl;
12  import org.kuali.ole.deliver.controller.checkout.CircUtilController;
13  import org.kuali.ole.deliver.drools.CheckedInItem;
14  import org.kuali.ole.deliver.drools.DroolsConstants;
15  import org.kuali.ole.deliver.drools.DroolsExchange;
16  import org.kuali.ole.deliver.form.CheckinForm;
17  import org.kuali.ole.deliver.form.OLEForm;
18  import org.kuali.ole.deliver.util.*;
19  import org.kuali.ole.deliver.util.printSlip.*;
20  import org.kuali.ole.docstore.common.document.content.instance.ItemClaimsReturnedRecord;
21  import org.kuali.ole.docstore.common.document.content.instance.MissingPieceItemRecord;
22  import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.HoldingsRecord;
23  import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemRecord;
24  import org.kuali.ole.util.DocstoreUtil;
25  import org.kuali.rice.core.api.util.RiceConstants;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  
28  import java.math.BigDecimal;
29  import java.sql.Timestamp;
30  import java.text.SimpleDateFormat;
31  import java.util.*;
32  
33  /**
34   * Created by pvsubrah on 7/22/15.
35   */
36  public abstract class CheckinBaseController extends CircUtilController {
37      private static final Logger LOG = Logger.getLogger(CheckinBaseController.class);
38  
39      protected abstract void setDataElements(OLEForm oleForm, ItemRecord itemRecord, OleLoanDocument loanDocument);
40  
41      public abstract CheckinForm getCheckinForm(OLEForm oleForm);
42  
43      public abstract String getItemBarcode(OLEForm oleForm);
44  
45      public abstract String getSelectedCirculationDesk(OLEForm oleForm);
46  
47      public abstract Date getCustomDueDateMap(OLEForm oleForm);
48  
49      public abstract String getCustomDueDateTime(OLEForm oleForm);
50  
51      public abstract void setCheckedInItem(CheckedInItem checkedInItem, OLEForm oleForm);
52  
53      public abstract CheckedInItem getCheckedInItem(OLEForm oleForm);
54  
55      public abstract void addCheckedInItemToCheckedInItemList(CheckedInItem checkedInItem, OLEForm oleForm);
56  
57      public abstract boolean isRecordNoteForDamagedItem(OLEForm oleForm);
58  
59      public abstract boolean isRecordNoteForClaimsReturn(OLEForm oleForm);
60  
61      public abstract boolean isRecordNoteForMissingPiece(OLEForm oleForm);
62  
63      public abstract String getMissingPieceMatchCheck(OLEForm oleForm);
64  
65      public abstract void setNoOfPieces(OLEForm oleForm, String numberOfPieces);
66  
67      public abstract OleLoanDocument getOleLoanDocument(OLEForm oleForm);
68  
69      public abstract DroolsExchange getDroolsExchange(OLEForm oleForm);
70  
71      public abstract String getOperatorId(OLEForm oleForm);
72  
73      private OnHoldCourtesyNoticeUtil onHoldCourtesyNoticeUtil;
74      private DamagedItemNoteHandler damagedItemNoteHandler;
75      private ClaimsReturnedNoteHandler claimsReturnedNoteHandler;
76  
77      private MissingPieceNoteHandler missingPieceNoteHandler;
78      private OleLoanDocumentsFromSolrBuilder oleLoanDocumentsFromSolrBuilder;
79  
80      public OnHoldCourtesyNoticeUtil getOnHoldCourtesyNoticeUtil() {
81          if (null == onHoldCourtesyNoticeUtil) {
82              onHoldCourtesyNoticeUtil = new OnHoldCourtesyNoticeUtil();
83          }
84          return onHoldCourtesyNoticeUtil;
85      }
86  
87      public void setOnHoldCourtesyNoticeUtil(OnHoldCourtesyNoticeUtil onHoldCourtesyNoticeUtil) {
88          this.onHoldCourtesyNoticeUtil = onHoldCourtesyNoticeUtil;
89      }
90  
91      public DamagedItemNoteHandler getDamagedItemNoteHandler() {
92          if (damagedItemNoteHandler == null) {
93              damagedItemNoteHandler = new DamagedItemNoteHandler();
94          }
95          return damagedItemNoteHandler;
96      }
97  
98      public void setDamagedItemNoteHandler(DamagedItemNoteHandler damagedItemNoteHandler) {
99          this.damagedItemNoteHandler = damagedItemNoteHandler;
100     }
101 
102     public ClaimsReturnedNoteHandler getClaimsReturnedNoteHandler() {
103         if (claimsReturnedNoteHandler == null) {
104             claimsReturnedNoteHandler = new ClaimsReturnedNoteHandler();
105         }
106         return claimsReturnedNoteHandler;
107     }
108 
109     public void setClaimsReturnedNoteHandler(ClaimsReturnedNoteHandler claimsReturnedNoteHandler) {
110         this.claimsReturnedNoteHandler = claimsReturnedNoteHandler;
111     }
112 
113     public MissingPieceNoteHandler getMissingPieceNoteHandler() {
114         if (missingPieceNoteHandler == null) {
115             missingPieceNoteHandler = new MissingPieceNoteHandler();
116         }
117         return missingPieceNoteHandler;
118     }
119 
120     public void setMissingPieceNoteHandler(MissingPieceNoteHandler missingPieceNoteHandler) {
121         this.missingPieceNoteHandler = missingPieceNoteHandler;
122     }
123 
124     public DroolsResponse checkin(OLEForm oleForm) {
125         String itemBarcode = getItemBarcode(oleForm);
126 
127         ItemRecord itemRecord = getItemRecordByBarcode(itemBarcode);
128 
129         DroolsResponse droolsResponse = null;
130         if (null != itemRecord) {
131             OleLoanDocument loanDocument = getLoanDocument(itemBarcode);
132             setDataElements(oleForm, itemRecord, loanDocument);
133             droolsResponse = preValidations(itemRecord, oleForm);
134             if (null != droolsResponse) {
135                 return droolsResponse;
136             }
137             droolsResponse = processCheckinAfterPreValidation(itemRecord, oleForm, loanDocument);
138 
139         } else {
140             droolsResponse = new DroolsResponse();
141             droolsResponse.getErrorMessage().setErrorMessage("Invalid item barcode : " + itemBarcode);
142             droolsResponse.getErrorMessage().setErrorCode(DroolsConstants.GENERAL_MESSAGE_FLAG);
143         }
144 
145         transferExchangeInfoFromDroolsResponseToCheckinForm(oleForm, droolsResponse);
146 
147         return droolsResponse;
148     }
149 
150     private void transferExchangeInfoFromDroolsResponseToCheckinForm(OLEForm oleForm, DroolsResponse droolsResponse) {
151         for (Iterator<String> iterator = droolsResponse.getDroolsExchange().getContext().keySet().iterator(); iterator.hasNext(); ) {
152             String key = iterator.next();
153             getDroolsExchange(oleForm).addToContext(key, droolsResponse.getDroolsExchange().getContext().get(key));
154         }
155     }
156 
157     public DroolsResponse preValidations(ItemRecord itemRecord, OLEForm oleForm) {
158         return preValidationForDamaged(itemRecord, oleForm);
159     }
160 
161     public DroolsResponse preValidationForDamaged(ItemRecord itemRecord, OLEForm oleForm) {
162         DroolsResponse droolsResponse;
163         droolsResponse = checkForDamagedItem(itemRecord);
164         if (droolsResponse != null) return droolsResponse;
165         return preValidationForClaimsReturned(itemRecord, oleForm);
166     }
167 
168     public DroolsResponse preValidationForClaimsReturned(ItemRecord itemRecord, OLEForm oleForm) {
169         DroolsResponse droolsResponse;
170         droolsResponse = checkForClaimsReturnedNote(itemRecord);
171         if (droolsResponse != null) return droolsResponse;
172         return preValidationForMissingPiece(itemRecord, oleForm);
173     }
174 
175     public DroolsResponse preValidationForMissingPiece(ItemRecord itemRecord, OLEForm oleForm) {
176         DroolsResponse droolsResponse;
177         droolsResponse = checkForMissingPieceNote(itemRecord);
178         setNoOfPieces(oleForm, itemRecord.getNumberOfPieces());
179         if (droolsResponse != null) return droolsResponse;
180         return preValidationForCheckinRequestExists(itemRecord, oleForm);
181     }
182 
183     public DroolsResponse preValidationForCheckinRequestExists(ItemRecord itemRecord, OLEForm oleForm) {
184         DroolsResponse droolsResponse;
185         droolsResponse = processIfCheckinRequestExist(itemRecord, oleForm);
186         if (droolsResponse != null) return droolsResponse;
187         OleLoanDocument oleLoanDocument = getOleLoanDocument(oleForm);
188         return processCheckinAfterPreValidation(itemRecord, oleForm, oleLoanDocument);
189     }
190 
191 
192     public DroolsResponse processCheckinAfterPreValidation(ItemRecord itemRecord, OLEForm oleForm, OleLoanDocument loanDocument) {
193         DroolsResponse droolsResponse;
194 
195         OleCirculationDesk oleCirculationDesk = getCircDeskLocationResolver().getOleCirculationDesk(getSelectedCirculationDesk(oleForm));
196         OleItemRecordForCirc oleItemRecordForCirc = ItemInfoUtil.getInstance().getOleItemRecordForCirc(itemRecord, oleCirculationDesk);
197         oleItemRecordForCirc.setOperatorCircLocation(oleCirculationDesk);
198         oleItemRecordForCirc.setCheckinLocation(oleCirculationDesk);
199 
200 
201         oleItemRecordForCirc.setItemRecord(itemRecord);
202         /*
203             oleItemSearch record is fetched to get all the bib/holdings/item information
204          */
205         OleItemSearch oleItemSearch = new DocstoreUtil().getOleItemSearchList(oleItemRecordForCirc.getItemUUID());
206 
207         boolean updateRecentlyReturnedTable = false;
208         droolsResponse = new DroolsResponse();
209         droolsResponse.setDroolsExchange(oleForm.getDroolsExchange());
210 
211         OlePatronDocument olePatronDocument = null;
212         if (null != loanDocument) {
213             olePatronDocument = loanDocument.getOlePatron();
214             ArrayList<Object> facts = new ArrayList<>();
215             facts.add(droolsResponse);
216             facts.add(oleItemRecordForCirc);
217             facts.add(oleCirculationDesk);
218 
219             fireRules(facts, null, "checkin-validation-for-loan");
220 
221             if (!ruleMatched(droolsResponse)) {
222                 droolsResponse.getErrorMessage().setErrorMessage("No checkin rule found!");
223                 droolsResponse.getErrorMessage().setErrorCode(DroolsConstants.GENERAL_MESSAGE_FLAG);
224                 return droolsResponse;
225             }
226 
227             facts.clear();
228 
229             facts.add(oleItemRecordForCirc);
230             ItemFineRate itemFineRate = new ItemFineRate();
231             facts.add(itemFineRate);
232             facts.add(olePatronDocument);
233             facts.add(loanDocument);
234 
235             fireRules(facts, null, "fine validation");
236 
237             try {
238                 updateLoanDocument(loanDocument, oleItemSearch, itemRecord);
239                 saveMissingPieceNote(oleForm);
240                 saveClaimsReturnedNote(oleForm);
241                 saveDamagedItemNote(oleForm);
242                 updateItemStatusAndCircCount(oleItemRecordForCirc);
243                 emailToPatronForOnHoldStatus();
244                 generateBillPayment(getSelectedCirculationDesk(oleForm), loanDocument, getCustomDueDateMap(oleForm), itemFineRate);
245             } catch (Exception e) {
246                 LOG.error(e.getStackTrace());
247             }
248 
249             if (null != oleItemRecordForCirc.getItemStatusToBeUpdatedTo() && oleItemRecordForCirc.getItemStatusToBeUpdatedTo().equals(OLEConstants.RECENTLY_RETURNED)) {
250                 updateRecentlyReturnedTable = true;
251             }
252 
253         } else {
254             ArrayList<Object> facts = new ArrayList<>();
255             facts.add(oleCirculationDesk);
256             facts.add(oleItemRecordForCirc);
257             facts.add(droolsResponse);
258 
259             fireRules(facts, null, "checkin-validation-for-no-loan");
260 
261             if (null != oleItemRecordForCirc.getItemStatusToBeUpdatedTo()) {
262                 updateItemStatusAndCircCount(oleItemRecordForCirc);
263                 if (oleItemRecordForCirc.getItemStatusToBeUpdatedTo().equals(OLEConstants.RECENTLY_RETURNED)) {
264                     updateRecentlyReturnedTable = true;
265                 }
266             } else {
267                 if (!ruleMatched(droolsResponse)) {
268                     droolsResponse.getErrorMessage().setErrorMessage("No checkin rule found!");
269                     droolsResponse.getErrorMessage().setErrorCode(DroolsConstants.GENERAL_MESSAGE_FLAG);
270                     return droolsResponse;
271                 }
272             }
273         }
274 
275         transferExchangeInfoFromDroolsResponseToCheckinForm(oleForm, droolsResponse);
276 
277         handleRecentlyReturnedRecord(oleItemRecordForCirc, updateRecentlyReturnedTable);
278 
279         updateCheckedInItemList(oleForm, itemRecord.getCheckInNote(), oleItemRecordForCirc, oleItemSearch, olePatronDocument);
280 
281 //        handleOnHoldRequestIfExists(oleItemRecordForCirc);
282 
283         handleIntransitStatus(oleItemRecordForCirc, oleForm);
284 
285         handleCheckinNote(oleForm, oleItemRecordForCirc);
286 
287         handleAutoCheckout(oleForm, oleItemRecordForCirc);
288 
289         return droolsResponse;
290     }
291 
292 
293     private DroolsResponse processIfCheckinRequestExist(ItemRecord itemRecord, OLEForm oleForm) {
294         OleDeliverRequestBo prioritizedRequest = ItemInfoUtil.getInstance().getPrioritizedRequest(itemRecord.getBarCode());
295         if (null != prioritizedRequest) {
296             DroolsResponse droolsResponse = new DroolsResponse();
297             droolsResponse.addErrorMessageCode(DroolsConstants.CHECKIN_REQUEST_EXITS_FOR_THIS_ITEM);
298             droolsResponse.addErrorMessage("Request already exist for this item. <br/><br/> Do you want to checkin this item?.");
299             return droolsResponse;
300         }
301         return null;
302     }
303 
304     private void saveMissingPieceNote(OLEForm oleForm) {
305         DroolsExchange droolsExchange = oleForm.getDroolsExchange();
306         ItemRecord itemRecord = (ItemRecord) droolsExchange.getContext().get("itemRecord");
307         OlePatronDocument olePatronDocument = (OlePatronDocument) droolsExchange.getContext().get("olePatronDocument");
308         if (StringUtils.isNotBlank(getMissingPieceMatchCheck(oleForm)) && getMissingPieceMatchCheck(oleForm).equalsIgnoreCase("mismatched")) {
309             Map<String, Object> missingPieceRecordInfo = new HashMap<>();
310             CheckinForm checkinForm = getCheckinForm(oleForm);
311             missingPieceRecordInfo.put("itemBarcode", checkinForm.getItemBarcode());
312             missingPieceRecordInfo.put("customDate", checkinForm.getCustomDueDateMap());
313             missingPieceRecordInfo.put("customTime", checkinForm.getCustomDueDateTime());
314             missingPieceRecordInfo.put("selectedCirculationDesk", checkinForm.getSelectedCirculationDesk());
315             missingPieceRecordInfo.put("missingPieceCount", checkinForm.getMissingPieceCount());
316             missingPieceRecordInfo.put("missingPieceNote", checkinForm.getMissingPieceNote());
317             getMissingPieceNoteHandler().updateMissingPieceRecord(itemRecord, missingPieceRecordInfo);
318             if (isRecordNoteForMissingPiece(oleForm)) {
319                 if (olePatronDocument != null) {
320                     getMissingPieceNoteHandler().savePatronNoteForMissingPiece(missingPieceRecordInfo, olePatronDocument, itemRecord);
321                 }
322             }
323         }
324     }
325 
326     private DroolsResponse checkForMissingPieceNote(ItemRecord itemRecord) {
327         int noOfPieces = StringUtils.isNotBlank(itemRecord.getNumberOfPieces()) ? Integer.parseInt(itemRecord.getNumberOfPieces()) : 0;
328         if (noOfPieces > 1) {
329             DroolsResponse droolsResponse = new DroolsResponse();
330             droolsResponse.addErrorMessageCode(DroolsConstants.ITEM_MISSING_PIECE);
331             droolsResponse.addErrorMessage(OLEConstants.VERIFY_PIECES + itemRecord.getNumberOfPieces() + OLEConstants.PIECES_RETURNED + OLEConstants.BREAK + "Total No of Pieces :      "
332                     + itemRecord.getNumberOfPieces() + OLEConstants.BREAK + "No of missing Pieces : " + (itemRecord.getMissingPiecesCount() != null ? itemRecord.getMissingPiecesCount() : "0"));
333             return droolsResponse;
334         }
335         return null;
336     }
337 
338     private void saveClaimsReturnedNote(OLEForm oleForm) {
339         DroolsExchange droolsExchange = oleForm.getDroolsExchange();
340         ItemRecord itemRecord = (ItemRecord) droolsExchange.getContext().get("itemRecord");
341         OlePatronDocument olePatronDocument = (OlePatronDocument) droolsExchange.getContext().get("olePatronDocument");
342         if (isRecordNoteForClaimsReturn(oleForm) && olePatronDocument != null) {
343             Map<String, Object> claimsRecordInfo = new HashMap<>();
344             CheckinForm checkinForm = getCheckinForm(oleForm);
345             claimsRecordInfo.put("itemBarcode", checkinForm.getItemBarcode());
346             claimsRecordInfo.put("customDate", checkinForm.getCustomDueDateMap());
347             claimsRecordInfo.put("customTime", checkinForm.getCustomDueDateTime());
348             claimsRecordInfo.put("selectedCirculationDesk", checkinForm.getSelectedCirculationDesk());
349             getClaimsReturnedNoteHandler().savePatronNoteForClaims(claimsRecordInfo, olePatronDocument);
350         }
351         itemRecord.setClaimsReturnedFlag(false);
352         itemRecord.setClaimsReturnedNote(null);
353         itemRecord.setClaimsReturnedFlagCreateDate(null);
354     }
355 
356     private DroolsResponse checkForClaimsReturnedNote(ItemRecord itemRecord) {
357         if (itemRecord.getClaimsReturnedFlag()) {
358             DroolsResponse droolsResponse = new DroolsResponse();
359             droolsResponse.addErrorMessageCode(DroolsConstants.ITEM_CLAIMS_RETURNED);
360             droolsResponse.addErrorMessage("Claims Returned item has been found!" + OLEConstants.BREAK + "Claims Returned Note: " + itemRecord.getClaimsReturnedNote());
361             return droolsResponse;
362         }
363         return null;
364     }
365 
366     private void saveDamagedItemNote(OLEForm oleForm) {
367         DroolsExchange droolsExchange = oleForm.getDroolsExchange();
368         OlePatronDocument olePatronDocument = (OlePatronDocument) droolsExchange.getContext().get("olePatronDocument");
369         if (isRecordNoteForDamagedItem(oleForm) && olePatronDocument != null) {
370             Map<String, Object> damagedRecordInfo = new HashMap<>();
371             CheckinForm checkinForm = getCheckinForm(oleForm);
372             damagedRecordInfo.put("itemBarcode", checkinForm.getItemBarcode());
373             damagedRecordInfo.put("customDate", checkinForm.getCustomDueDateMap());
374             damagedRecordInfo.put("customTime", checkinForm.getCustomDueDateTime());
375             damagedRecordInfo.put("selectedCirculationDesk", checkinForm.getSelectedCirculationDesk());
376             getDamagedItemNoteHandler().savePatronNoteForDamaged(damagedRecordInfo, olePatronDocument);
377         }
378     }
379 
380     private DroolsResponse checkForDamagedItem(ItemRecord itemRecord) {
381         if (itemRecord.isItemDamagedStatus()) {
382             DroolsResponse droolsResponse = new DroolsResponse();
383             droolsResponse.addErrorMessageCode(DroolsConstants.ITEM_DAMAGED);
384             droolsResponse.addErrorMessage("Item is Damaged. Do you want to continue?" + OLEConstants.BREAK + "Damaged Note: " + itemRecord.getDamagedItemNote());
385             return droolsResponse;
386         }
387         return null;
388     }
389 
390     private void handleIntransitStatus(OleItemRecordForCirc oleItemRecordForCirc, OLEForm oleForm) {
391         getDroolsExchange(oleForm).addToContext("oleItemRecordForCirc", oleItemRecordForCirc);
392         if(oleItemRecordForCirc.getItemStatusToBeUpdatedTo().equals(OLEConstants.ITEM_STATUS_IN_TRANSIT )|| oleItemRecordForCirc.getItemStatusToBeUpdatedTo().equals(OLEConstants.ITEM_STATUS_IN_TRANSIT_HOLD)) {
393             updateLoanInTransitRecordHistory(oleForm);
394         }
395     }
396 
397     public void updateLoanInTransitRecordHistory(OLEForm oleForm) {
398         OleItemRecordForCirc oleItemRecordForCirc = (OleItemRecordForCirc)getDroolsExchange(oleForm).getContext().get("oleItemRecordForCirc");
399         if (StringUtils.isNotBlank(oleItemRecordForCirc.getRouteToLocation())) {
400             ItemRecord itemRecord = null != oleItemRecordForCirc ? oleItemRecordForCirc.getItemRecord() : null;
401             OLELoanIntransitRecordHistory oleLoanIntransitRecordHistory = new OLELoanIntransitRecordHistory();
402             if(null != itemRecord) {
403                 oleLoanIntransitRecordHistory.setItemBarcode(itemRecord.getBarCode());
404                 oleLoanIntransitRecordHistory.setItemUUID(DocumentUniqueIDPrefix.getPrefixedId(itemRecord.getUniqueIdPrefix(),itemRecord.getItemId()));
405             }
406             oleLoanIntransitRecordHistory.setHomeCirculationDesk(null != oleItemRecordForCirc.getCheckinLocation() ? oleItemRecordForCirc.getCheckinLocation().getCirculationDeskCode() : getSelectedCirculationDesk(oleForm));
407             oleLoanIntransitRecordHistory.setRouteCirculationDesk(oleItemRecordForCirc.getRouteToLocation());
408             oleLoanIntransitRecordHistory.setOperator(getOperatorId(oleForm));
409             try {
410                 oleLoanIntransitRecordHistory.setReturnedDateTime(processDateAndTimeForAlterDueDate(getCustomDueDateMap(oleForm), getCustomDueDateTime(oleForm)));
411             } catch (Exception e) {
412                 e.printStackTrace();
413             }
414             getBusinessObjectService().save(oleLoanIntransitRecordHistory);
415         }
416     }
417 
418     private void handleAutoCheckout(OLEForm oleForm, OleItemRecordForCirc oleItemRecordForCirc) {
419         OleDeliverRequestBo oleDeliverRequestBo = oleItemRecordForCirc.getOleDeliverRequestBo();
420         if (null != oleDeliverRequestBo && (oleDeliverRequestBo.getOleDeliverRequestType().getRequestTypeCode().contains(OLEConstants.OleDeliverRequest.HOLD_DELIVERY) ||
421                 oleDeliverRequestBo.getOleDeliverRequestType().getRequestTypeCode().contains(OLEConstants.OleDeliverRequest.RECALL_DELIVERY))) {
422             getDroolsExchange(oleForm).addToContext("autoCheckout", true);
423             getDroolsExchange(oleForm).addToContext("requestBo", oleDeliverRequestBo);
424         }
425     }
426 
427     private void handleCheckinNote(OLEForm oleForm, OleItemRecordForCirc oleItemRecordForCirc) {
428         if (StringUtils.isNotBlank(oleItemRecordForCirc.getItemRecord().getCheckInNote())) {
429             getDroolsExchange(oleForm).addToContext("checkinNote", true);
430         }
431     }
432 
433     private void handleOnHoldRequestIfExists(OleItemRecordForCirc oleItemRecordForCirc) {
434         OleDeliverRequestBo oleDeliverRequestBo = oleItemRecordForCirc.getOleDeliverRequestBo();
435         if (null != oleItemRecordForCirc.getItemStatusToBeUpdatedTo() && oleItemRecordForCirc.getItemStatusToBeUpdatedTo().equalsIgnoreCase(OLEConstants.ITEM_STATUS_ON_HOLD) &&
436                 null != oleDeliverRequestBo && oleDeliverRequestBo.getOleDeliverRequestType().getRequestTypeCode().equals("Hold/Hold Request")) {
437             getOnHoldCourtesyNoticeUtil().sendOnHoldNotice(oleItemRecordForCirc.getOleDeliverRequestBo());
438         }
439     }
440 
441     public ItemInfoUtil getItemInfoUtil() {
442         return ItemInfoUtil.getInstance();
443     }
444 
445     public OlePrintSlipUtil getOlePrintSlipUtil(OleItemRecordForCirc oleItemRecordForCirc) {
446         String itemStatusToBeUpdatedTo = oleItemRecordForCirc.getItemStatusToBeUpdatedTo();
447         OleCirculationDesk oleCirculationDesk = null != oleItemRecordForCirc ? oleItemRecordForCirc.getCheckinLocation() : null;
448 
449         if (itemStatusToBeUpdatedTo.equalsIgnoreCase(OLEConstants.ITEM_STATUS_IN_TRANSIT)) {
450             return new InTransitRegularPrintSlipUtil();
451         } else if (itemStatusToBeUpdatedTo.equalsIgnoreCase(OLEConstants.ITEM_STATUS_IN_TRANSIT_HOLD)) {
452             return new InTransitForHoldRegularPrintSlipUtil();
453         } else if (itemStatusToBeUpdatedTo.equalsIgnoreCase(OLEConstants.ITEM_STATUS_IN_TRANSIT_STAFF)) {
454             return new InTransitForStaffRegularPrintSlipUtil();
455         } else if (itemStatusToBeUpdatedTo.equalsIgnoreCase(OLEConstants.ITEM_STATUS_ON_HOLD)) {
456             if (oleCirculationDesk != null && oleCirculationDesk.getHoldFormat() != null && oleCirculationDesk.getHoldFormat().equals(OLEConstants.RECEIPT_PRINTER)) {
457                 return new OnHoldRecieptPrintSlipUtil();
458             } else {
459                 return new OnHoldRegularPrintSlipUtil();
460             }
461         } else if (itemStatusToBeUpdatedTo.equalsIgnoreCase(OLEConstants.ITEM_STATUS_IN_TRANSIT_LOAN)) {
462             return new InTransitForLoanRegularPrintSlipUtil();
463         }
464         return null;
465     }
466 
467     private void handleRecentlyReturnedRecord(OleItemRecordForCirc oleItemRecordForCirc, boolean updateRecentlyReturnedTable) {
468         if (updateRecentlyReturnedTable) {
469             createOrUpdateRecentlyReturnedRecord(oleItemRecordForCirc.getItemUUID(), oleItemRecordForCirc.getOperatorCircLocation().getCirculationDeskId());
470         }
471     }
472 
473     private void emailToPatronForOnHoldStatus() {
474 
475     }
476 
477     public boolean ruleMatched(DroolsResponse droolsResponse) {
478         if (droolsResponse.isRuleMatched()) {
479             return true;
480         }
481         return false;
482     }
483 
484     private void updateCheckedInItemList(OLEForm oleForm, String checkinNote, OleItemRecordForCirc oleItemRecordForCirc, OleItemSearch oleItemSearch, OlePatronDocument olePatronDocument) {
485         CheckedInItem checkedInItem = new CheckedInItem();
486         SimpleDateFormat dateFormat = new SimpleDateFormat(RiceConstants.SIMPLE_DATE_FORMAT_FOR_DATE + " " + RiceConstants.SIMPLE_DATE_FORMAT_FOR_TIME);
487         checkedInItem.setCheckinNote(checkinNote);
488         checkedInItem.setTitle(oleItemSearch.getTitle());
489         checkedInItem.setItemId(oleItemSearch.getItemBarCode());
490         checkedInItem.setAuthor(oleItemSearch.getAuthor());
491         checkedInItem.setBibUuid(oleItemSearch.getBibUUID());
492         checkedInItem.setInstanceUuid(oleItemSearch.getInstanceUUID());
493         checkedInItem.setItemUuid(oleItemSearch.getItemUUID());
494         String location = null != oleItemSearch.getShelvingLocation() ? oleItemSearch.getShelvingLocation().split("/")[oleItemSearch.getShelvingLocation().split("/").length - 1] : oleItemSearch.getShelvingLocation();
495         try {
496             checkedInItem.setLocation(getOleLoanDocumentsFromSolrBuilder().getLocationFullName(location));
497         } catch (Exception e) {
498             e.printStackTrace();
499         }
500         checkedInItem.setLocationCode(oleItemSearch.getShelvingLocation());
501         checkedInItem.setCallNumber(oleItemSearch.getCallNumber());
502         checkedInItem.setCopyNumber(oleItemSearch.getCopyNumber());
503         checkedInItem.setEnumeration(oleItemSearch.getEnumeration());
504         checkedInItem.setChronology(oleItemSearch.getChronology());
505         try {
506             checkedInItem.setCheckInDate(dateFormat.format(processDateAndTimeForAlterDueDate(getCustomDueDateMap(oleForm),getCustomDueDateTime(oleForm))));
507         } catch (Exception e) {
508             e.printStackTrace();
509         }
510         checkedInItem.setItemStatus(oleItemRecordForCirc.getItemStatusToBeUpdatedTo());
511         checkedInItem.setPatronId((olePatronDocument != null) ? olePatronDocument.getOlePatronId() : "");
512         checkedInItem.setPatronBarcode((olePatronDocument != null) ? olePatronDocument.getBarcode() : "");
513         checkedInItem.setBorrowerType((olePatronDocument != null) ? olePatronDocument.getBorrowerTypeName() : "");
514         checkedInItem.setBillName(getBillName(olePatronDocument, oleItemSearch.getItemBarCode()));
515         checkedInItem.setItemType(oleItemSearch.getItemType());
516         if(oleItemRecordForCirc.getItemStatusToBeUpdatedTo().equals(OLEConstants.ITEM_STATUS_IN_TRANSIT )|| oleItemRecordForCirc.getItemStatusToBeUpdatedTo().equals(OLEConstants.ITEM_STATUS_IN_TRANSIT_HOLD)) {
517             checkedInItem.setRouteToLocation(oleItemRecordForCirc.getRouteToLocation());
518         }
519         addCheckedInItemToCheckedInItemList(checkedInItem, oleForm);
520         setCheckedInItem(checkedInItem, oleForm);
521     }
522 
523     private String getBillName(OlePatronDocument olePatronDocument, String itemBarcode) {
524         if (null != olePatronDocument) {
525             List<PatronBillPayment> patronBillPayments = olePatronDocument.getPatronBillPayments();
526             for (Iterator<PatronBillPayment> iterator = patronBillPayments.iterator(); iterator.hasNext(); ) {
527                 PatronBillPayment patronBillPayment = iterator.next();
528                 for (Iterator<FeeType> patronBillPaymentIterator = patronBillPayment.getFeeType().iterator(); patronBillPaymentIterator.hasNext(); ) {
529                     FeeType feeType = patronBillPaymentIterator.next();
530                     if (null != feeType.getItemBarcode() && feeType.getItemBarcode().equalsIgnoreCase(itemBarcode)) {
531                         return feeType.getOleFeeType().getFeeTypeCode();
532                     }
533                 }
534             }
535         }
536         return null;
537     }
538 
539     private String generateBillPayment(String selectedCirculationDesk, OleLoanDocument loanDocument, Date customDueDateMap, ItemFineRate itemFineRate) throws Exception {
540         String billPayment = null;
541         if (null == itemFineRate.getFineRate() || null == itemFineRate.getMaxFine() || null == itemFineRate.getInterval()) {
542             LOG.error("No fine rule found");
543         } else {
544             Double overdueFine = new OleCalendarServiceImpl().calculateOverdueFine(selectedCirculationDesk, loanDocument.getLoanDueDate(), new Timestamp(customDueDateMap.getTime()), itemFineRate);
545             overdueFine = overdueFine >= itemFineRate.getMaxFine() ? itemFineRate.getMaxFine() : overdueFine;
546 
547             if (null != loanDocument.getReplacementBill() && loanDocument.getReplacementBill().compareTo(BigDecimal.ZERO) > 0) {
548                 billPayment = getPatronBillGenerator().generatePatronBillPayment(loanDocument, OLEConstants.REPLACEMENT_FEE, loanDocument.getReplacementBill().doubleValue());
549             } else if (null != overdueFine && overdueFine > 0) {
550                 billPayment = getPatronBillGenerator().generatePatronBillPayment(loanDocument, OLEConstants.OVERDUE_FINE, overdueFine);
551             }
552         }
553 
554         return billPayment;
555     }
556 
557     private PatronBillGenerator getPatronBillGenerator() {
558         return new PatronBillGenerator();
559     }
560 
561     private void updateLoanDocument(OleLoanDocument loanDocument, OleItemSearch oleItemSearch, ItemRecord itemRecord) throws Exception {
562         createCirculationHistoryAndTemporaryHistoryRecords(loanDocument, oleItemSearch, itemRecord);
563         getBusinessObjectService().delete(loanDocument);
564     }
565 
566     private void updateItemStatusAndCircCount(OleItemRecordForCirc oleItemRecordForCirc) {
567         HashMap parameterValues = new HashMap();
568         parameterValues.put("patronId", null);
569         parameterValues.put("proxyPatronId", null);
570         parameterValues.put("itemCheckoutDateTime", null);
571         parameterValues.put("loanDueDate", null);
572         parameterValues.put("numRenewals", null);
573         parameterValues.put("itemStatus", oleItemRecordForCirc.getItemStatusToBeUpdatedTo());
574         parameterValues.put("locationsCheckinCountRecordToBeUpdated", oleItemRecordForCirc.getLocationsCheckinCountRecordToBeUpdated());
575         parameterValues.put("claimsReturnNote", oleItemRecordForCirc.getItemRecord().getClaimsReturnedNote());
576         Timestamp claimsReturnedFlagCreateDate = oleItemRecordForCirc.getItemRecord().getClaimsReturnedFlagCreateDate();
577         parameterValues.put("ClaimsReturnedDate", claimsReturnedFlagCreateDate == null ? claimsReturnedFlagCreateDate : convertToString(claimsReturnedFlagCreateDate));
578         parameterValues.put("itemClaimsReturnedRecords", prepareClaimsReturnHistoryRecords(oleItemRecordForCirc.getItemRecord()));
579         parameterValues.put("itemClaimsReturnedFlag", oleItemRecordForCirc.getItemRecord().getClaimsReturnedFlag());
580         parameterValues.put("missingPieceItemFlag", oleItemRecordForCirc.getItemRecord().isMissingPieceFlag());
581         parameterValues.put("missingPieceItemNote", oleItemRecordForCirc.getItemRecord().getMissingPieceFlagNote());
582         parameterValues.put("missingPieceItemCount", oleItemRecordForCirc.getItemRecord().getMissingPiecesCount());
583         parameterValues.put("noOfmissingPiece", oleItemRecordForCirc.getItemRecord().getNumberOfPieces());
584         Timestamp missingPieceItemDate = oleItemRecordForCirc.getItemRecord().getMissingPieceEffectiveDate();
585         parameterValues.put("missingPieceItemDate", missingPieceItemDate == null ? missingPieceItemDate : convertToString(missingPieceItemDate));
586         parameterValues.put("itemMissingPieceItemRecords", prepareMissingPieceHistoryRecords(oleItemRecordForCirc.getItemRecord()));
587         updateItemInfoInSolr(parameterValues, oleItemRecordForCirc.getItemUUID(), true);
588     }
589 
590     private List<ItemClaimsReturnedRecord> prepareClaimsReturnHistoryRecords(ItemRecord itemRecord) {
591         List<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemClaimsReturnedRecord> claimsReturnedRecordList = itemRecord.getItemClaimsReturnedRecords();
592         List<ItemClaimsReturnedRecord> itemClaimsReturnedRecordList = new ArrayList<>();
593         for (Iterator<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemClaimsReturnedRecord> iterator = claimsReturnedRecordList.iterator(); iterator.hasNext(); ) {
594             org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemClaimsReturnedRecord itemClaimsReturnedRecord = iterator.next();
595             ItemClaimsReturnedRecord claimsReturnedRecord = new ItemClaimsReturnedRecord();
596             if (itemClaimsReturnedRecord.getClaimsReturnedFlagCreateDate() != null && !itemClaimsReturnedRecord.getClaimsReturnedFlagCreateDate().toString().isEmpty()) {
597                 String formatedDateStringForClaimsReturn = convertToString(itemClaimsReturnedRecord.getClaimsReturnedFlagCreateDate());
598                 claimsReturnedRecord.setClaimsReturnedFlagCreateDate(formatedDateStringForClaimsReturn);
599             }
600             claimsReturnedRecord.setClaimsReturnedNote(itemClaimsReturnedRecord.getClaimsReturnedNote());
601             claimsReturnedRecord.setClaimsReturnedPatronBarcode(itemClaimsReturnedRecord.getClaimsReturnedPatronBarcode());
602             claimsReturnedRecord.setClaimsReturnedOperatorId(itemClaimsReturnedRecord.getClaimsReturnedOperatorId());
603             claimsReturnedRecord.setItemId(itemClaimsReturnedRecord.getItemId());
604             itemClaimsReturnedRecordList.add(claimsReturnedRecord);
605         }
606         return itemClaimsReturnedRecordList;
607     }
608 
609     private List<MissingPieceItemRecord> prepareMissingPieceHistoryRecords(ItemRecord itemRecord) {
610         SimpleDateFormat dateFormatForMissingItem = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
611         List<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.MissingPieceItemRecord> missingPieceItemRecordList = itemRecord.getMissingPieceItemRecordList();
612         List<MissingPieceItemRecord> itemMissingPieceRecordList = new ArrayList<>();
613         for (Iterator<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.MissingPieceItemRecord> iterator = missingPieceItemRecordList.iterator(); iterator.hasNext(); ) {
614             org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.MissingPieceItemRecord itemMissingPieceItemRecord = iterator.next();
615             MissingPieceItemRecord missingPieceItemRecord = new MissingPieceItemRecord();
616             if (itemMissingPieceItemRecord.getMissingPieceDate() != null && !itemMissingPieceItemRecord.getMissingPieceDate().toString().isEmpty()) {
617                 Timestamp formatedDateStringForDamagedItem = itemMissingPieceItemRecord.getMissingPieceDate();
618                 missingPieceItemRecord.setMissingPieceDate(dateFormatForMissingItem.format(formatedDateStringForDamagedItem));
619             }
620             missingPieceItemRecord.setMissingPieceFlagNote(itemMissingPieceItemRecord.getMissingPieceFlagNote());
621             missingPieceItemRecord.setMissingPieceCount(itemMissingPieceItemRecord.getMissingPieceCount());
622             missingPieceItemRecord.setPatronBarcode(itemMissingPieceItemRecord.getPatronBarcode());
623             missingPieceItemRecord.setOperatorId(itemMissingPieceItemRecord.getOperatorId());
624             missingPieceItemRecord.setItemId(itemMissingPieceItemRecord.getItemId());
625             itemMissingPieceRecordList.add(missingPieceItemRecord);
626         }
627         return itemMissingPieceRecordList;
628     }
629 
630 
631     private OleLoanDocument getLoanDocument(String itemBarcode) {
632         HashMap<String, Object> criteriaMap = new HashMap<>();
633         criteriaMap.put("itemId", itemBarcode);
634         List<OleLoanDocument> oleLoanDocuments = (List<OleLoanDocument>) getBusinessObjectService().findMatching(OleLoanDocument.class, criteriaMap);
635         if (!CollectionUtils.isEmpty(oleLoanDocuments)) {
636             return oleLoanDocuments.get(0);
637         }
638         return null;
639     }
640 
641     private void createCirculationHistoryAndTemporaryHistoryRecords(OleLoanDocument oleLoanDocument, OleItemSearch oleItemSearch, ItemRecord itemRecord) throws Exception {
642         try {
643             OlePatronDocument olePatronDocument = oleLoanDocument.getOlePatron();
644             OleCirculationHistory oleCirculationHistory = new OleCirculationHistory();
645             oleCirculationHistory.setLoanId(oleLoanDocument.getLoanId());
646             oleCirculationHistory.setCirculationPolicyId(oleLoanDocument.getCirculationPolicyId());
647             oleCirculationHistory.setBibAuthor(oleItemSearch.getAuthor());
648             oleCirculationHistory.setBibTitle(oleItemSearch.getTitle());
649             oleCirculationHistory.setCheckInDate(oleLoanDocument.getCheckInDate() != null ? oleLoanDocument.getCheckInDate() : new Timestamp(System.currentTimeMillis()));
650             oleCirculationHistory.setCreateDate(oleLoanDocument.getCreateDate());
651             oleCirculationHistory.setCirculationLocationId(oleLoanDocument.getCirculationLocationId());
652             oleCirculationHistory.setDueDate(oleLoanDocument.getLoanDueDate());
653             oleCirculationHistory.setItemId(oleLoanDocument.getItemId());
654             oleCirculationHistory.setNumberOfOverdueNoticesSent(oleLoanDocument.getNumberOfOverdueNoticesSent());
655             oleCirculationHistory.setNumberOfRenewals(oleLoanDocument.getNumberOfRenewals());
656             oleCirculationHistory.setStatisticalCategory(olePatronDocument.getStatisticalCategory());
657             oleCirculationHistory.setRepaymentFeePatronBillId(oleLoanDocument.getRepaymentFeePatronBillId());
658             oleCirculationHistory.setProxyPatronId(olePatronDocument.getProxyPatronId());
659             if (olePatronDocument.getOleBorrowerType() != null) {
660                 oleCirculationHistory.setPatronTypeId(olePatronDocument.getOleBorrowerType().getBorrowerTypeId());
661             }
662             oleCirculationHistory.setPatronId(oleLoanDocument.getPatronId());
663             oleCirculationHistory.setPastDueDate(oleLoanDocument.getPastDueDate());
664             oleCirculationHistory.setOverdueNoticeDate(oleLoanDocument.getOverDueNoticeDate());
665             oleCirculationHistory.setOleRequestId(oleLoanDocument.getOleRequestId());
666             oleCirculationHistory.setItemUuid(oleLoanDocument.getItemUuid());
667             oleCirculationHistory.setItemLocation(itemRecord.getLocation());
668             oleCirculationHistory.setHoldingsLocation(getHoldingsLocation(itemRecord.getHoldingsId()));
669 
670             OleCirculationHistory savedCircHistoryRecord = getBusinessObjectService().save(oleCirculationHistory);
671 
672             OleTemporaryCirculationHistory oleTemporaryCirculationHistory = new OleTemporaryCirculationHistory();
673             oleTemporaryCirculationHistory.setCirculationLocationId(oleLoanDocument.getCirculationLocationId());
674             oleTemporaryCirculationHistory.setOlePatronId(oleLoanDocument.getPatronId());
675             oleTemporaryCirculationHistory.setItemId(oleLoanDocument.getItemId());
676             oleTemporaryCirculationHistory.setCheckInDate(oleLoanDocument.getCheckInDate() != null ? oleLoanDocument.getCheckInDate() : new Timestamp(System.currentTimeMillis()));
677             oleTemporaryCirculationHistory.setItemUuid(oleLoanDocument.getItemUuid());
678             oleTemporaryCirculationHistory.setDueDate(oleLoanDocument.getLoanDueDate());
679             oleTemporaryCirculationHistory.setCheckOutDate(oleLoanDocument.getCreateDate());
680             OleTemporaryCirculationHistory savedTempCircHistoryRecord = getBusinessObjectService().save(oleTemporaryCirculationHistory);
681 
682         } catch (Exception tempHistoryException) {
683             throw new Exception("Unable to record the transaction in temporary circulation history.");
684         }
685 
686     }
687 
688     private String getHoldingsLocation(String holdignsId) {
689         HashMap<String, Object> map = new HashMap<>();
690         map.put("holdingsId", holdignsId);
691         HoldingsRecord byPrimaryKey = getBusinessObjectService().findByPrimaryKey(HoldingsRecord.class, map);
692         if (null != byPrimaryKey) {
693             return byPrimaryKey.getLocation();
694         }
695         return null;
696     }
697 
698     private void createOrUpdateRecentlyReturnedRecord(String itemUUID, String circulationDeskId) {
699         HashMap<String, String> map = new HashMap<String, String>();
700         map.put("itemUuid", itemUUID);
701         OleRecentlyReturned oleRecentlyReturned = getBusinessObjectService().findByPrimaryKey(OleRecentlyReturned.class, map);
702         if (oleRecentlyReturned != null) {
703             oleRecentlyReturned.setCirculationDeskId(circulationDeskId);
704         } else {
705             oleRecentlyReturned = new OleRecentlyReturned();
706             oleRecentlyReturned.setCirculationDeskId(circulationDeskId);
707             oleRecentlyReturned.setItemUuid(itemUUID);
708         }
709         getBusinessObjectService().save(oleRecentlyReturned);
710     }
711 
712     private OleLoanDocumentsFromSolrBuilder getOleLoanDocumentsFromSolrBuilder() {
713         if (null == oleLoanDocumentsFromSolrBuilder) {
714             oleLoanDocumentsFromSolrBuilder = new OleLoanDocumentsFromSolrBuilder();
715         }
716         return oleLoanDocumentsFromSolrBuilder;
717     }
718 
719 }