View Javadoc
1   package org.kuali.ole.deliver.controller.checkout;
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.bo.OleLoanDocument;
9   import org.kuali.ole.deliver.bo.OlePatronDocument;
10  import org.kuali.ole.deliver.calendar.service.DateUtil;
11  import org.kuali.ole.deliver.drools.DroolsConstants;
12  import org.kuali.ole.deliver.form.CircForm;
13  import org.kuali.ole.deliver.form.OleLoanForm;
14  import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
15  import org.kuali.ole.deliver.util.DroolsResponse;
16  import org.kuali.ole.deliver.util.ErrorMessage;
17  import org.kuali.ole.deliver.util.OleItemRecordForCirc;
18  import org.kuali.ole.docstore.common.document.content.instance.ItemClaimsReturnedRecord;
19  import org.kuali.ole.docstore.common.document.content.instance.ItemDamagedRecord;
20  import org.kuali.ole.docstore.common.document.content.instance.MissingPieceItemRecord;
21  import org.kuali.rice.core.api.config.property.ConfigContext;
22  import org.kuali.rice.krad.util.GlobalVariables;
23  import org.kuali.rice.krad.web.form.UifFormBase;
24  import org.springframework.validation.BindingResult;
25  import org.springframework.web.bind.annotation.ModelAttribute;
26  import org.springframework.web.bind.annotation.RequestMapping;
27  import org.springframework.web.servlet.ModelAndView;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.sql.Timestamp;
32  import java.text.SimpleDateFormat;
33  import java.util.*;
34  
35  /**
36   * Created by hemalathas on 6/21/15.
37   */
38  public class CheckoutItemController extends CircFastAddItemController {
39  
40      private static final Logger LOG = Logger.getLogger(CheckoutItemController.class);
41  
42  
43      @RequestMapping(params = "methodToCall=lookupItemAndSaveLoan")
44      public ModelAndView lookupItemAndSaveLoan(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
45                                                HttpServletRequest request, HttpServletResponse response) {
46          CircForm circForm = (CircForm) form;
47          DroolsResponse droolsResponse = getCheckoutUIController(circForm.getFormKey()).lookupItemAndSaveLoan(circForm);
48          if (null != droolsResponse &&
49                  StringUtils.isNotBlank(droolsResponse.retrieveErrorMessage())) {
50              if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.ERROR_CODES.CUSTOM_LOAN_DUE_DATE_REQUIRED.getName())) {
51                  circForm.setErrorMessage(droolsResponse.getErrorMessage());
52                  showDialog("customDueDateDialog", circForm, request, response);
53              } else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.GENERAL_MESSAGE_FLAG)) {
54                  circForm.setErrorMessage(droolsResponse.getErrorMessage());
55                  showDialog("generalInfoDialog", circForm, request, response);
56              } else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.REQUEST_EXITS_FOR_AVAIL_ITEM)) {
57                  circForm.setErrorMessage(droolsResponse.getErrorMessage());
58                  circForm.setRequestExistOrLoanedCheck(true);
59                  showDialog("overrideMessageDialog", circForm, request, response);
60              }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.REQUEST_EXITS_FOR_LOANED_ITEM)) {
61                  circForm.setErrorMessage(droolsResponse.getErrorMessage());
62                  showDialog("checkoutRenewOptionDialog", circForm, request, response);
63              }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.CHECKED_OUT_BY_SAME_PATRON)) {
64                  circForm.setErrorMessage(droolsResponse.getErrorMessage());
65                  showDialog("checkoutRenewOptionDialog", circForm, request, response);
66              }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.LOANED_BY_DIFFERENT_PATRON)) {
67                  circForm.setErrorMessage(droolsResponse.getErrorMessage());
68                  resetItemInfoForNextTrans(circForm);
69                  showDialog("generalInfoDialog", circForm, request, response);
70              } else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.GENERAL_INFO)){
71                  circForm.setErrorMessage(droolsResponse.getErrorMessage());
72                  showDialog("generalInfoDialog",circForm,request,response);
73              }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.ITEM_CLAIMS_RETURNED)){
74                  handleClaimsReturnedProcess(request, response, circForm, droolsResponse);
75              }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.ITEM_DAMAGED)){
76                  handleDamagedItemProcess(request, response, circForm, droolsResponse);
77              }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.ITEM_MISSING_PIECE)){
78                  handleMissingPieceProcess(request, response, circForm, droolsResponse);
79              }else {
80                  circForm.setErrorMessage(droolsResponse.getErrorMessage());
81                  circForm.setItemOverride(true);
82                  showErrorMessageDialog(circForm, request, response);
83              }
84          } else {
85              resetItemInfoForNextTrans(circForm);
86          }
87  
88          return getUIFModelAndView(circForm);
89      }
90  
91      @RequestMapping(params = "methodToCall=processDamagedItem")
92      public ModelAndView processDamagedItem(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
93                                                HttpServletRequest request, HttpServletResponse response) {
94          CircForm circForm = (CircForm) form;
95          processItemInformationIfAvailable(request, circForm);
96          OleItemRecordForCirc oleItemRecordForCirc = (OleItemRecordForCirc) circForm.getDroolsExchange().getContext().get("oleItemRecordForCirc");
97          DroolsResponse droolsResponse = getCheckoutUIController(circForm.getFormKey()).
98                  preValidationForDamagedItem(oleItemRecordForCirc.getItemRecord(), circForm);
99          if (null != droolsResponse && StringUtils.isNotBlank(droolsResponse.retrieveErrorMessage())) {
100             if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.ERROR_CODES.CUSTOM_LOAN_DUE_DATE_REQUIRED.getName())) {
101                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
102                 showDialog("customDueDateDialog", circForm, request, response);
103             } else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.REQUEST_EXITS_FOR_AVAIL_ITEM)) {
104                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
105                 circForm.setRequestExistOrLoanedCheck(true);
106                 showDialog("overrideMessageDialog", circForm, request, response);
107             }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.REQUEST_EXITS_FOR_LOANED_ITEM)) {
108                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
109                 showDialog("checkoutRenewOptionDialog", circForm, request, response);
110             }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.CHECKED_OUT_BY_SAME_PATRON)) {
111                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
112                 showDialog("checkoutRenewOptionDialog", circForm, request, response);
113             }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.LOANED_BY_DIFFERENT_PATRON)) {
114                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
115                 resetItemInfoForNextTrans(circForm);
116                 showDialog("generalInfoDialog", circForm, request, response);
117             } else if(null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equalsIgnoreCase(DroolsConstants.ITEM_MISSING_PIECE)) {
118                 handleMissingPieceProcess(request, response, circForm, droolsResponse);
119             } else if (null!= droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equalsIgnoreCase(DroolsConstants.ITEM_DAMAGED)) {
120                 handleDamagedItemProcess(request, response, circForm, droolsResponse);
121             } else {
122                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
123                 circForm.setItemOverride(true);
124                 showErrorMessageDialog(circForm, request, response);
125             }
126         } else {
127             resetItemInfoForNextTrans(circForm);
128         }
129         return getUIFModelAndView(circForm);
130     }
131 
132     @RequestMapping(params = "methodToCall=processMissingPieceItem")
133     public ModelAndView processMissingPieceItem(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
134                                                 HttpServletRequest request, HttpServletResponse response) {
135         CircForm circForm = (CircForm) form;
136         processItemInformationIfAvailable(request, circForm);
137         OleItemRecordForCirc oleItemRecordForCirc = (OleItemRecordForCirc) circForm.getDroolsExchange().getContext().get("oleItemRecordForCirc");
138         DroolsResponse droolsResponse = getCheckoutUIController(circForm.getFormKey()).preValidationForMissingPiece(oleItemRecordForCirc.getItemRecord(), circForm);
139         if (null != droolsResponse && StringUtils.isNotBlank(droolsResponse.retrieveErrorMessage())) {
140             if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.ERROR_CODES.CUSTOM_LOAN_DUE_DATE_REQUIRED.getName())) {
141                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
142                 showDialog("customDueDateDialog", circForm, request, response);
143             } else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.REQUEST_EXITS_FOR_AVAIL_ITEM)) {
144                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
145                 circForm.setRequestExistOrLoanedCheck(true);
146                 showDialog("overrideMessageDialog", circForm, request, response);
147             }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.REQUEST_EXITS_FOR_LOANED_ITEM)) {
148                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
149                 showDialog("checkoutRenewOptionDialog", circForm, request, response);
150             }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.CHECKED_OUT_BY_SAME_PATRON)) {
151                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
152                 showDialog("checkoutRenewOptionDialog", circForm, request, response);
153             }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.LOANED_BY_DIFFERENT_PATRON)) {
154                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
155                 resetItemInfoForNextTrans(circForm);
156                 showDialog("generalInfoDialog", circForm, request, response);
157             } else if(null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equalsIgnoreCase(DroolsConstants.ITEM_MISSING_PIECE)) {
158                 handleMissingPieceProcess(request, response, circForm, droolsResponse);
159             } else {
160                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
161                 circForm.setItemOverride(true);
162                 showErrorMessageDialog(circForm, request, response);
163             }
164         } else {
165             resetItemInfoForNextTrans(circForm);
166         }
167         return getUIFModelAndView(circForm);
168     }
169 
170     @RequestMapping(params = "methodToCall=processCheckoutAfterPreValidations")
171     public ModelAndView processCheckoutAfterPreValidations(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
172                                                     HttpServletRequest request, HttpServletResponse response) {
173         CircForm circForm = (CircForm) form;
174         processItemInformationIfAvailable(request, circForm);
175         OleItemRecordForCirc oleItemRecordForCirc = (OleItemRecordForCirc) circForm.getDroolsExchange().getContext().get("oleItemRecordForCirc");
176         DroolsResponse droolsResponse = getCheckoutUIController(circForm.getFormKey()).processCheckoutAfterPreValidations(circForm, oleItemRecordForCirc);
177         if (null != droolsResponse && StringUtils.isNotBlank(droolsResponse.retrieveErrorMessage())) {
178             if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.ERROR_CODES.CUSTOM_LOAN_DUE_DATE_REQUIRED.getName())) {
179                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
180                 showDialog("customDueDateDialog", circForm, request, response);
181             } else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.REQUEST_EXITS_FOR_AVAIL_ITEM)) {
182                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
183                 circForm.setRequestExistOrLoanedCheck(true);
184                 showDialog("overrideMessageDialog", circForm, request, response);
185             }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.REQUEST_EXITS_FOR_LOANED_ITEM)) {
186                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
187                 showDialog("checkoutRenewOptionDialog", circForm, request, response);
188             }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.CHECKED_OUT_BY_SAME_PATRON)) {
189                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
190                 showDialog("checkoutRenewOptionDialog", circForm, request, response);
191             }else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.LOANED_BY_DIFFERENT_PATRON)) {
192                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
193                 resetItemInfoForNextTrans(circForm);
194                 showDialog("generalInfoDialog", circForm, request, response);
195             } else {
196                 circForm.setErrorMessage(droolsResponse.getErrorMessage());
197                 circForm.setItemOverride(true);
198                 showErrorMessageDialog(circForm, request, response);
199             }
200         } else {
201             resetItemInfoForNextTrans(circForm);
202         }
203         return getUIFModelAndView(circForm);
204     }
205 
206     private void handleMissingPieceProcess(HttpServletRequest request, HttpServletResponse response, CircForm circForm, DroolsResponse droolsResponse) {
207         circForm.setRecordNoteForMissingPiece(true);
208         circForm.setErrorMessage(droolsResponse.getErrorMessage());
209         showDialog("checkoutMissingPieceDialog",circForm,request,response);
210     }
211 
212     private void handleDamagedItemProcess(HttpServletRequest request, HttpServletResponse response, CircForm circForm, DroolsResponse droolsResponse) {
213         circForm.setRecordNoteForDamagedItem(true);
214         circForm.setErrorMessage(droolsResponse.getErrorMessage());
215         showDialog("checkoutDamagedItemDialog",circForm,request,response);
216     }
217 
218     private void handleClaimsReturnedProcess(HttpServletRequest request, HttpServletResponse response, CircForm circForm, DroolsResponse droolsResponse) {
219         circForm.setRecordNoteForClaimsReturn(true);
220         circForm.setErrorMessage(droolsResponse.getErrorMessage());
221         showDialog("checkoutClaimsReturnDialog",circForm,request,response);
222     }
223 
224     private void processItemInformationIfAvailable(HttpServletRequest request, CircForm circForm) {
225 
226         claimsReturnedCheck(request, circForm);
227 
228         damagedItemCheck(request, circForm);
229 
230         missingPieceCheck(request, circForm);
231 
232     }
233 
234     private void missingPieceCheck(HttpServletRequest request, CircForm circForm) {
235         String recordNoteForMissingPieceChecked = request.getParameter("recordNoteForMissingPieceChecked");
236         if(StringUtils.isNotBlank(recordNoteForMissingPieceChecked)) {
237             circForm.setRecordNoteForMissingPiece(Boolean.valueOf(recordNoteForMissingPieceChecked));
238         }
239     }
240 
241     private void damagedItemCheck(HttpServletRequest request, CircForm circForm) {
242         String recordNoteForDamageChecked = request.getParameter("recordNoteForDamageChecked");
243         if(StringUtils.isNotBlank(recordNoteForDamageChecked)) {
244             circForm.setRecordNoteForDamagedItem(Boolean.valueOf(recordNoteForDamageChecked));
245         }
246     }
247 
248     private void claimsReturnedCheck(HttpServletRequest request, CircForm circForm) {
249         String recordNoteForClaimChecked = request.getParameter("recordNoteForClaimChecked");
250         if(StringUtils.isNotBlank(recordNoteForClaimChecked)) {
251             circForm.setRecordNoteForClaimsReturn(Boolean.valueOf(recordNoteForClaimChecked));
252         }
253     }
254 
255     @RequestMapping(params = "methodToCall=proceedToValidateItemAndSaveLoan")
256     public ModelAndView proceedToValidateItemAndSaveLoan(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
257                                                          HttpServletRequest request, HttpServletResponse response) {
258         CircForm circForm = (CircForm) form;
259         circForm.setErrorMessage(new ErrorMessage());
260         DroolsResponse droolsResponse = getCheckoutUIController(circForm.getFormKey()).proceedWithItemValidation(circForm);
261         circForm.setDroolsExchange(droolsResponse.getDroolsExchange());
262         circForm.setErrorMessage(droolsResponse.getErrorMessage());
263         if (null != droolsResponse &&
264                 StringUtils.isNotBlank(droolsResponse.retrieveErrorMessage())) {
265             if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.ERROR_CODES.CUSTOM_LOAN_DUE_DATE_REQUIRED.getName())) {
266                 showDialog("customDueDateDialog", circForm, request, response);
267             } else if (null != droolsResponse.retriveErrorCode() && droolsResponse.retriveErrorCode().equals(DroolsConstants.GENERAL_MESSAGE_FLAG)) {
268                 showDialog("generalMessageAndResetUIDialog", circForm, request, response);
269             } else {
270                 circForm.setItemOverride(true);
271                 showErrorMessageDialog(circForm, request, response);
272             }
273         } else {
274             resetItemInfoForNextTrans(circForm);
275         }
276         return getUIFModelAndView(circForm);
277     }
278 
279     @RequestMapping(params = "methodToCall=proceedToSaveLoan")
280     public ModelAndView proceedToSaveLoan(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
281                                           HttpServletRequest request, HttpServletResponse response) {
282         CircForm circForm = (CircForm) form;
283         String customDate = request.getParameter("customDueDateMap");
284         if (StringUtils.isNotBlank(customDate)) {
285             circForm.setCustomDueDateMap(new DateUtil().getDate(customDate));
286         }
287         getCheckoutUIController(circForm.getFormKey()).proceedToSaveLoan(circForm);
288         resetItemInfoForNextTrans(circForm);
289         return getUIFModelAndView(circForm, "circViewPage");
290     }
291 
292 
293     @RequestMapping(params = "methodToCall=setItemBarcode")
294     public ModelAndView setItemBarcode(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
295                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
296         OleLoanForm oleLoanForm = (OleLoanForm) form;
297         oleLoanForm.setItem(fastAddBarcode);
298         return getUIFModelAndView(oleLoanForm, "circViewPage");
299     }
300 
301     @RequestMapping(params = "methodToCall=openFastAdd")
302     public ModelAndView openFastAdd(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
303                                     HttpServletRequest request, HttpServletResponse response) {
304         LOG.debug("Inside the openFastAdd method");
305         OleLoanForm oleLoanForm = (OleLoanForm) form;
306         oleLoanForm.setFastAddItemIndicator(true);
307         String url = ConfigContext.getCurrentContextConfig().getProperty("ole.fs.url.base") + OLEConstants.FAST_ADD_LINK;
308         oleLoanForm.setFastAddUrl(url);
309         return getUIFModelAndView(oleLoanForm, "circViewPage");
310     }
311 
312     public void createClaimsReturnForItem(CircForm circForm, List<OleLoanDocument> loanDocumentList, OlePatronDocument olePatronDocument) throws Exception {
313         if (CollectionUtils.isNotEmpty(loanDocumentList)) {
314             for (Iterator<OleLoanDocument> iterator = loanDocumentList.iterator(); iterator.hasNext(); ) {
315                 OleLoanDocument oleLoanDocument = iterator.next();
316                 List<ItemClaimsReturnedRecord> itemClaimsReturnedRecords = updateClaimsReturnedHistory(oleLoanDocument, olePatronDocument.getBarcode());
317                 Map parameterMap = new HashMap();
318                 parameterMap.put("claimsReturnNote", oleLoanDocument.getClaimsReturnNote());
319                 parameterMap.put("ClaimsReturnedDate", convertToString(oleLoanDocument.getClaimsReturnedDate()));
320                 parameterMap.put("itemClaimsReturnedRecords", itemClaimsReturnedRecords);
321                 parameterMap.put("itemClaimsReturnedFlag", oleLoanDocument.isClaimsReturnedIndicator());
322                 parameterMap.put("patronId", olePatronDocument.getOlePatronId());
323                 parameterMap.put("proxyPatronId", oleLoanDocument.getProxyPatronId());
324                 getCheckoutUIController(circForm.getFormKey()).updateItemInfoInSolr(parameterMap, oleLoanDocument.getItemUuid(), false);
325                 new OleDeliverRequestDocumentHelperServiceImpl().cancelPendingRequestForClaimsReturnedItem(oleLoanDocument.getItemUuid());
326             }
327         }
328     }
329 
330     public void deleteClaimsReturnForItem(CircForm circForm, List<OleLoanDocument> loanDocumentList) throws Exception {
331         if (CollectionUtils.isNotEmpty(loanDocumentList)) {
332             for (Iterator<OleLoanDocument> iterator = loanDocumentList.iterator(); iterator.hasNext(); ) {
333                 OleLoanDocument oleLoanDocument = iterator.next();
334                 Map parameterMap = new HashMap();
335                 parameterMap.put("deleteClaimsReturn", null);
336                 Boolean itemUpdate = getCheckoutUIController(circForm.getFormKey()).deleteItemInfoInSolr(parameterMap, oleLoanDocument.getItemUuid());
337                 if(itemUpdate!=null && itemUpdate.booleanValue()) {
338                     oleLoanDocument.setClaimsReturnNote(null);
339                     oleLoanDocument.setClaimsReturnedDate(null);
340                 }
341             }
342         }
343     }
344 
345     private List<ItemClaimsReturnedRecord> updateClaimsReturnedHistory(OleLoanDocument existingLoanDocument, String patronBarcode) {
346         SimpleDateFormat dateFormatForClaimsReturn = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
347         Map<String, String> map = new HashMap<>();
348         map.put("itemId", DocumentUniqueIDPrefix.getDocumentId(existingLoanDocument.getItemUuid()));
349         List<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemClaimsReturnedRecord> claimsReturnedRecordList = (List<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemClaimsReturnedRecord>) getBusinessObjectService().findMatchingOrderBy(org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemClaimsReturnedRecord.class, map, "claimsReturnedId", true);
350         List<ItemClaimsReturnedRecord> itemClaimsReturnedRecordList = new ArrayList<>();
351         for (Iterator<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemClaimsReturnedRecord> iterator = claimsReturnedRecordList.iterator(); iterator.hasNext(); ) {
352             org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemClaimsReturnedRecord itemClaimsReturnedRecord = iterator.next();
353             ItemClaimsReturnedRecord claimsReturnedRecord = new ItemClaimsReturnedRecord();
354             if (itemClaimsReturnedRecord.getClaimsReturnedFlagCreateDate() != null && !itemClaimsReturnedRecord.getClaimsReturnedFlagCreateDate().toString().isEmpty()) {
355                 String formatedDateStringForClaimsReturn = convertToString(itemClaimsReturnedRecord.getClaimsReturnedFlagCreateDate());
356                 claimsReturnedRecord.setClaimsReturnedFlagCreateDate(formatedDateStringForClaimsReturn);
357             }
358             claimsReturnedRecord.setClaimsReturnedNote(itemClaimsReturnedRecord.getClaimsReturnedNote());
359             claimsReturnedRecord.setClaimsReturnedPatronBarcode(itemClaimsReturnedRecord.getClaimsReturnedPatronBarcode());
360             claimsReturnedRecord.setClaimsReturnedOperatorId(itemClaimsReturnedRecord.getClaimsReturnedOperatorId());
361             claimsReturnedRecord.setItemId(itemClaimsReturnedRecord.getItemId());
362             itemClaimsReturnedRecordList.add(claimsReturnedRecord);
363         }
364         ItemClaimsReturnedRecord claimsReturnedRecord = new ItemClaimsReturnedRecord();
365         claimsReturnedRecord.setClaimsReturnedNote(existingLoanDocument.getClaimsReturnNote());
366         claimsReturnedRecord.setClaimsReturnedPatronBarcode(patronBarcode);
367         claimsReturnedRecord.setClaimsReturnedOperatorId(GlobalVariables.getUserSession().getPrincipalId());
368         claimsReturnedRecord.setItemId(DocumentUniqueIDPrefix.getDocumentId(existingLoanDocument.getItemUuid()));
369         claimsReturnedRecord.setClaimsReturnedFlagCreateDate(dateFormatForClaimsReturn.format(existingLoanDocument.getClaimsReturnedDate()));
370         itemClaimsReturnedRecordList.add(claimsReturnedRecord);
371         return itemClaimsReturnedRecordList;
372     }
373 
374 
375     public void createMissingPieceItem(CircForm circForm, List<OleLoanDocument> loanDocumentList, OlePatronDocument olePatronDocument) throws Exception {
376         if (CollectionUtils.isNotEmpty(loanDocumentList)) {
377             for (Iterator<OleLoanDocument> iterator = loanDocumentList.iterator(); iterator.hasNext(); ) {
378                 OleLoanDocument oleLoanDocument = iterator.next();
379                 List<MissingPieceItemRecord> itemMissingPieceItemRecords = updateMissingPieceItemHistory(oleLoanDocument, olePatronDocument.getBarcode());
380                 Map parameterMap = new HashMap();
381                 parameterMap.put("missingPieceItemFlag", oleLoanDocument.isMissingPieceFlag());
382                 parameterMap.put("missingPieceItemNote", oleLoanDocument.getMissingPieceNote());
383                 parameterMap.put("noOfmissingPiece", oleLoanDocument.getNoOfMissingPiece());
384                 parameterMap.put("missingPieceItemCount", oleLoanDocument.getMissingPiecesCount());
385                 parameterMap.put("missingPieceItemDate", convertToString(oleLoanDocument.getMissingPieceItemDate()));
386                 parameterMap.put("itemMissingPieceItemRecords", itemMissingPieceItemRecords);
387                 parameterMap.put("patronId", olePatronDocument.getOlePatronId());
388                 parameterMap.put("proxyPatronId", oleLoanDocument.getProxyPatronId());
389                 getCheckoutUIController(circForm.getFormKey()).updateItemInfoInSolr(parameterMap, oleLoanDocument.getItemUuid(), false);
390                 new OleDeliverRequestDocumentHelperServiceImpl().cancelPendingRequestForMissingPieceItem(oleLoanDocument.getItemUuid());
391             }
392         }
393     }
394 
395 
396     private List<MissingPieceItemRecord> updateMissingPieceItemHistory(OleLoanDocument existingLoanDocument, String patronBarcode) {
397         SimpleDateFormat dateFormatForMissingItem = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
398         Map<String, String> map = new HashMap<>();
399         map.put("itemId", DocumentUniqueIDPrefix.getDocumentId(existingLoanDocument.getItemUuid()));
400         List<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.MissingPieceItemRecord> missingPieceItemRecordList = (List<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.MissingPieceItemRecord>) getBusinessObjectService().findMatchingOrderBy(org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.MissingPieceItemRecord.class, map, "missingPieceItemId", true);
401         List<MissingPieceItemRecord> itemMissingPieceRecordList = new ArrayList<>();
402         for (Iterator<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.MissingPieceItemRecord> iterator = missingPieceItemRecordList.iterator(); iterator.hasNext(); ) {
403             org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.MissingPieceItemRecord itemMissingPieceItemRecord = iterator.next();
404             MissingPieceItemRecord missingPieceItemRecord = new MissingPieceItemRecord();
405             if (itemMissingPieceItemRecord.getMissingPieceDate() != null && !itemMissingPieceItemRecord.getMissingPieceDate().toString().isEmpty()) {
406                 Timestamp formatedDateStringForDamagedItem = itemMissingPieceItemRecord.getMissingPieceDate();
407                 missingPieceItemRecord.setMissingPieceDate(dateFormatForMissingItem.format(formatedDateStringForDamagedItem));
408             }
409             missingPieceItemRecord.setMissingPieceFlagNote(itemMissingPieceItemRecord.getMissingPieceFlagNote());
410             missingPieceItemRecord.setMissingPieceCount(itemMissingPieceItemRecord.getMissingPieceCount());
411             missingPieceItemRecord.setPatronBarcode(itemMissingPieceItemRecord.getPatronBarcode());
412             missingPieceItemRecord.setOperatorId(itemMissingPieceItemRecord.getOperatorId());
413             missingPieceItemRecord.setItemId(itemMissingPieceItemRecord.getItemId());
414             itemMissingPieceRecordList.add(missingPieceItemRecord);
415         }
416         MissingPieceItemRecord missingPieceItemRecord = new MissingPieceItemRecord();
417         missingPieceItemRecord.setMissingPieceFlagNote(existingLoanDocument.getMissingPieceNote());
418         missingPieceItemRecord.setMissingPieceCount(existingLoanDocument.getMissingPiecesCount());
419         missingPieceItemRecord.setPatronBarcode(patronBarcode);
420         missingPieceItemRecord.setOperatorId(GlobalVariables.getUserSession().getPrincipalId());
421         missingPieceItemRecord.setItemId(DocumentUniqueIDPrefix.getDocumentId(existingLoanDocument.getItemUuid()));
422         missingPieceItemRecord.setMissingPieceDate(dateFormatForMissingItem.format(existingLoanDocument.getMissingPieceItemDate()));
423         itemMissingPieceRecordList.add(missingPieceItemRecord);
424         return itemMissingPieceRecordList;
425     }
426 
427     public void deleteMissingPieceItem(CircForm circForm, List<OleLoanDocument> loanDocumentList) throws Exception {
428         if (CollectionUtils.isNotEmpty(loanDocumentList)) {
429             for (Iterator<OleLoanDocument> iterator = loanDocumentList.iterator(); iterator.hasNext(); ) {
430                 OleLoanDocument oleLoanDocument = iterator.next();
431                 Map parameterMap = new HashMap();
432                 parameterMap.put("deleteMissingPieceItem", null);
433                 Boolean itemUpdate = getCheckoutUIController(circForm.getFormKey()).deleteItemInfoInSolr(parameterMap, oleLoanDocument.getItemUuid());
434                 if(itemUpdate!=null && itemUpdate.booleanValue()) {
435                     oleLoanDocument.setMissingPieceNote(null);
436                 }
437             }
438         }
439     }
440 
441 
442     public void createDamagedItem(CircForm circForm, List<OleLoanDocument> loanDocumentList, OlePatronDocument olePatronDocument) throws Exception {
443         if (CollectionUtils.isNotEmpty(loanDocumentList)) {
444             for (Iterator<OleLoanDocument> iterator = loanDocumentList.iterator(); iterator.hasNext(); ) {
445                 OleLoanDocument oleLoanDocument = iterator.next();
446                 List<ItemDamagedRecord> itemDamagedItemRecords = updateDamagedItemHistory(oleLoanDocument, olePatronDocument.getBarcode());
447                 Map parameterMap = new HashMap();
448                 parameterMap.put("damagedItemNote", oleLoanDocument.getItemDamagedNote());
449                 parameterMap.put("damagedItemDate", convertToString(oleLoanDocument.getDamagedItemDate()));
450                 parameterMap.put("itemDamagedRecords", itemDamagedItemRecords);
451                 parameterMap.put("patronId", olePatronDocument.getOlePatronId());
452                 parameterMap.put("proxyPatronId", oleLoanDocument.getProxyPatronId());
453                 getCheckoutUIController(circForm.getFormKey()).updateItemInfoInSolr(parameterMap, oleLoanDocument.getItemUuid(), false);
454                 new OleDeliverRequestDocumentHelperServiceImpl().cancelPendingRequestForDamagedItem(oleLoanDocument.getItemUuid());
455             }
456         }
457     }
458 
459     private List<ItemDamagedRecord> updateDamagedItemHistory(OleLoanDocument existingLoanDocument, String patronBarcode) {
460         SimpleDateFormat dateFormatForDamagedItem = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
461         Map<String, String> map = new HashMap<>();
462         map.put("itemId", DocumentUniqueIDPrefix.getDocumentId(existingLoanDocument.getItemUuid()));
463         List<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemDamagedRecord> damagedItemRecordList = (List<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemDamagedRecord>) getBusinessObjectService().findMatchingOrderBy(org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemDamagedRecord.class, map, "itemDamagedId", true);
464         List<ItemDamagedRecord> itemDamagedRecordList = new ArrayList<>();
465         for (Iterator<org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemDamagedRecord> iterator = damagedItemRecordList.iterator(); iterator.hasNext(); ) {
466             org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemDamagedRecord itemDamagedItemRecord = iterator.next();
467             ItemDamagedRecord damagedItemRecord = new ItemDamagedRecord();
468             if (itemDamagedItemRecord.getDamagedItemDate() != null && !itemDamagedItemRecord.getDamagedItemDate().toString().isEmpty()) {
469                 Timestamp formatedDateStringForDamagedItem = itemDamagedItemRecord.getDamagedItemDate();
470                 damagedItemRecord.setDamagedItemDate(dateFormatForDamagedItem.format(formatedDateStringForDamagedItem));
471             }
472             damagedItemRecord.setDamagedItemNote(itemDamagedItemRecord.getDamagedItemNote());
473             damagedItemRecord.setPatronBarcode(itemDamagedItemRecord.getPatronBarcode());
474             damagedItemRecord.setOperatorId(itemDamagedItemRecord.getOperatorId());
475             damagedItemRecord.setItemId(itemDamagedItemRecord.getItemId());
476             itemDamagedRecordList.add(damagedItemRecord);
477         }
478         ItemDamagedRecord damagedItemRecord = new ItemDamagedRecord();
479         damagedItemRecord.setDamagedItemNote(existingLoanDocument.getItemDamagedNote());
480         damagedItemRecord.setPatronBarcode(patronBarcode);
481         damagedItemRecord.setOperatorId(GlobalVariables.getUserSession().getPrincipalId());
482         damagedItemRecord.setItemId(DocumentUniqueIDPrefix.getDocumentId(existingLoanDocument.getItemUuid()));
483         damagedItemRecord.setDamagedItemDate(dateFormatForDamagedItem.format(existingLoanDocument.getDamagedItemDate()));
484         itemDamagedRecordList.add(damagedItemRecord);
485         return itemDamagedRecordList;
486     }
487 
488     public void deleteDamagedItem(CircForm circForm, List<OleLoanDocument> loanDocumentList) throws Exception {
489         if (CollectionUtils.isNotEmpty(loanDocumentList)) {
490             for (Iterator<OleLoanDocument> iterator = loanDocumentList.iterator(); iterator.hasNext(); ) {
491                 OleLoanDocument oleLoanDocument = iterator.next();
492                 Map parameterMap = new HashMap();
493                 parameterMap.put("deleteDamagedItem", null);
494                 Boolean itemUpdate = getCheckoutUIController(circForm.getFormKey()).deleteItemInfoInSolr(parameterMap, oleLoanDocument.getItemUuid());
495                 if(itemUpdate!=null && itemUpdate.booleanValue()) {
496                     oleLoanDocument.setItemDamagedNote(null);
497                 }
498             }
499         }
500     }
501 
502 
503     public String convertToString(Timestamp date) {
504         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
505         String stringFormatOfDate = "";
506         stringFormatOfDate = simpleDateFormat.format(date).toString();
507         return stringFormatOfDate;
508     }
509 
510 
511 }