001package org.kuali.ole.deliver.controller.renew;
002
003import org.apache.commons.collections.CollectionUtils;
004import org.apache.commons.lang3.StringUtils;
005import org.apache.log4j.Logger;
006import org.kuali.ole.deliver.bo.OLEDeliverNotice;
007import org.kuali.ole.deliver.bo.OleLoanDocument;
008import org.kuali.ole.deliver.bo.OlePatronDocument;
009import org.kuali.ole.deliver.controller.checkout.CircUtilController;
010import org.kuali.ole.deliver.form.CircForm;
011import org.kuali.ole.deliver.util.*;
012import org.kuali.ole.docstore.common.document.Item;
013import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemRecord;
014import org.kuali.ole.ncip.service.impl.NonSip2RenewItemService;
015import org.kuali.ole.ncip.service.impl.RenewItemsService;
016
017import java.sql.Timestamp;
018import java.util.ArrayList;
019import java.util.Iterator;
020import java.util.List;
021import java.util.Map;
022
023/**
024 * Created by sheiksalahudeenm on 8/13/15.
025 */
026public class RenewController extends CircUtilController {
027
028    private static final Logger LOG = Logger.getLogger(RenewController.class);
029    private RenewItemControllerUtil renewItemControllerUtil;
030    private RenewItemsService renewItemsService;
031
032    public DroolsResponse renewItems(List<OleLoanDocument> selectedLoanDocumentList, OlePatronDocument olePatronDocument) {
033        List<Item> itemList = new ArrayList<>();
034        DroolsResponse finalDroolResponse = new DroolsResponse();
035
036        for (Iterator<OleLoanDocument> iterator = selectedLoanDocumentList.iterator(); iterator.hasNext(); ) {
037            DroolsResponse droolsResponse = new DroolsResponse();
038            OleLoanDocument oleLoanDocument = iterator.next();
039
040            boolean isIndefiniteDueDate = oleLoanDocument.IsIndefiniteDueDate();
041            if (!isIndefiniteDueDate) {
042
043                List<Object> facts = new ArrayList<>();
044
045                oleLoanDocument.setPastDueDate(oleLoanDocument.getLoanDueDate());
046                ItemRecord itemRecord = getItemRecordByBarcode(oleLoanDocument.getItemId());
047                OleItemRecordForCirc oleItemRecordForCirc = ItemInfoUtil.getInstance().getOleItemRecordForCirc(itemRecord, null);
048
049                NoticeInfo noticeInfo = new NoticeInfo();
050                facts.add(oleLoanDocument);
051                facts.add(olePatronDocument);
052                facts.add(oleItemRecordForCirc);
053                facts.add(droolsResponse);
054                facts.add(noticeInfo);
055                fireRules(facts, null, "renewal validation");
056
057                if (droolsResponse.isRuleMatched()) {
058                    if (StringUtils.isBlank(droolsResponse.retrieveErrorMessage())) {
059                        boolean pastAndRenewDueDateSame = false;
060                        try {
061                            pastAndRenewDueDateSame = oleLoanDocument.isPastAndRenewDueDateSame();
062                            if (!pastAndRenewDueDateSame) {
063                                Integer numRenewals = getRenewItemControllerUtil().incrementRenewalCount(oleLoanDocument);
064                                oleLoanDocument.setNumberOfRenewals(numRenewals.toString());
065                                List<OLEDeliverNotice> oleDeliverNotices = processNotices(oleLoanDocument, oleItemRecordForCirc.getItemRecord());
066                                oleLoanDocument.setDeliverNotices(oleDeliverNotices);
067                                if (null != oleLoanDocument.getLoanId()) {
068                                    Item itemForUpdate = getItemForUpdate(oleLoanDocument);
069                                    if (null != itemForUpdate) {
070                                        itemList.add(itemForUpdate);
071                                        droolsResponse.setSucessMessage("Successfully Renewed");
072                                        droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
073                                        finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
074                                    }
075                                }
076                            } else {
077                                droolsResponse.addErrorMessageCode("PastAndNewDueDateSame");
078                                droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
079                                finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
080                            }
081                        } catch (Exception e) {
082                            if (e.getMessage().equalsIgnoreCase("No Fixed Due Date found for the renewal policy")) {
083                                oleLoanDocument.setLoanDueDate(new Timestamp(oleLoanDocument.getPastDueDate().getTime()));
084                                droolsResponse.addErrorMessageCode("InvalidFixedDueDateMapping");
085                                droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
086                                finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
087                            } else {
088                                oleLoanDocument.setLoanDueDate(new Timestamp(oleLoanDocument.getPastDueDate().getTime()));
089                                droolsResponse.addErrorMessageCode("InternalException");
090                                droolsResponse.addErrorMessage(e.getMessage());
091                                droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
092                                finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
093                            }
094                        }
095                    } else {
096                        droolsResponse.addErrorMessageCode("ErrorMessageFromRule");
097                        droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
098                        finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
099                    }
100                } else {
101                    droolsResponse.addErrorMessageCode("No renewal policy found");
102                    droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
103                    finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
104                }
105            } else {
106                droolsResponse.addErrorMessageCode("InDefiniteDueDate");
107                droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
108                finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
109            }
110        }
111
112        if (CollectionUtils.isNotEmpty(itemList)) {
113            getRenewItemsService().processBulkUpdate(itemList, finalDroolResponse);
114        }
115
116        return finalDroolResponse;
117
118    }
119
120    public DroolsResponse proceedToSaveLoanDocuments(List<OleLoanDocument> loanDocumentsForRenew) {
121        DroolsResponse finalDroolResponse = new DroolsResponse();
122        List<Item> updateItemList = new ArrayList<>();
123        for (Iterator<OleLoanDocument> iterator = loanDocumentsForRenew.iterator(); iterator.hasNext(); ) {
124            DroolsResponse droolsResponse = new DroolsResponse();
125            OleLoanDocument oleLoanDocument = iterator.next();
126            Integer numRenewals = getRenewItemControllerUtil().incrementRenewalCount(oleLoanDocument);
127            oleLoanDocument.setNumberOfRenewals(numRenewals.toString());
128            updateNoticesForLoanDocument(oleLoanDocument);
129            Item itemForUpdate = getItemForUpdate(oleLoanDocument);
130            if (null != itemForUpdate) {
131                updateItemList.add(itemForUpdate);
132                droolsResponse.setSucessMessage("Successfully Renewed");
133                droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
134                finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
135            }
136        }
137        getRenewItemsService().processBulkUpdate(updateItemList, finalDroolResponse);
138        return finalDroolResponse;
139
140    }
141
142
143    public Item getItemForUpdate(OleLoanDocument oleLoanDocument) {
144        Map updateParameters = getRenewItemControllerUtil().getUpdateParameters(oleLoanDocument);
145        return getItemForUpdate(updateParameters);
146    }
147
148    public String analyzeRenewedLoanDocuments(CircForm circForm, DroolsResponse droolsResponse) {
149        StringBuilder messageContentForRenew = new StringBuilder();
150        Map<String, Object> context = droolsResponse.getDroolsExchange().getContext();
151        if (context.size() > 0) {
152            for (Iterator<String> iterator = context.keySet().iterator(); iterator.hasNext(); ) {
153                String key = iterator.next();
154                DroolsResponse individualDroolResponse = (DroolsResponse) context.get(key);
155                if (StringUtils.isNotBlank(individualDroolResponse.getSucessMessage()) && individualDroolResponse.getSucessMessage().equalsIgnoreCase("Successfully Renewed")) {
156                    OleLoanDocument oleLoanDocument = (OleLoanDocument) individualDroolResponse.getDroolsExchange().getContext().get(key);
157                    String content = "Successfully renewed for item (" + oleLoanDocument.getItemId() + ")";
158                    appendContentToStrinBuilder(messageContentForRenew, content);
159                } else if (individualDroolResponse.retriveErrorCode().equalsIgnoreCase("No renewal policy found")) {
160                    Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
161                    OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
162                    if (null != individualLoanDocument) {
163                        individualLoanDocument.setNonCirculatingItem(true);
164                        if (null == circForm.getLoanDocumentsForRenew()) {
165                            circForm.setLoanDocumentsForRenew(new ArrayList<OleLoanDocument>());
166                        }
167                        individualLoanDocument.setErrorMessage(null);
168                        individualLoanDocument.setErrorMessage("No renewal policy found for this item.");
169                        circForm.getLoanDocumentsForRenew().add(individualLoanDocument);
170                    }
171                } else if (individualDroolResponse.retriveErrorCode().equalsIgnoreCase("ErrorMessageFromRule")) {
172                    Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
173                    OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
174                    if (null != individualLoanDocument) {
175                        individualLoanDocument.setNonCirculatingItem(false);
176                        if (null == circForm.getLoanDocumentsForRenew()) {
177                            circForm.setLoanDocumentsForRenew(new ArrayList<OleLoanDocument>());
178                        }
179                        individualLoanDocument.setErrorMessage(null);
180                        individualLoanDocument.setErrorMessage(individualDroolResponse.retrieveErrorMessage());
181                        circForm.getLoanDocumentsForRenew().add(individualLoanDocument);
182                    }
183                } else if (individualDroolResponse.retriveErrorCode().equalsIgnoreCase("PastAndNewDueDateSame")) {
184                    Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
185                    OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
186                    String content = "Item (" + individualLoanDocument.getItemId() + ") was not renewed because the new due date/time would not change.";
187                    appendContentToStrinBuilder(messageContentForRenew, content);
188                } else if (individualDroolResponse.retriveErrorCode().equalsIgnoreCase("InDefiniteDueDate")) {
189                    Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
190                    OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
191                    String content = "Item (" + individualLoanDocument.getItemId() + ") wasn't renewed. Items on indefinite loan do not need to be renewed.";
192                    appendContentToStrinBuilder(messageContentForRenew, content);
193                } else if (individualDroolResponse.retriveErrorCode().equalsIgnoreCase("InvalidFixedDueDateMapping")) {
194                    Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
195                    OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
196                    String content = "Item (" + individualLoanDocument.getItemId() + ") wasn't renewed. Invalid Fixed Due Date Mapping.";
197                    appendContentToStrinBuilder(messageContentForRenew, content);
198                } else if (individualDroolResponse.retriveErrorCode().equalsIgnoreCase("InternalException")) {
199                    Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
200                    OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
201                    String content = "Item (" + individualLoanDocument.getItemId() + ") wasn't renewed." + droolsResponse.retrieveErrorMessage();
202                    appendContentToStrinBuilder(messageContentForRenew, content);
203                }
204            }
205        }
206        return messageContentForRenew.toString();
207    }
208
209    private void appendContentToStrinBuilder(StringBuilder stringBuilder, String content) {
210        if (stringBuilder.length() > 0) {
211            stringBuilder.append("<br/>").append(content);
212        } else {
213            stringBuilder.append(content);
214        }
215    }
216
217    public void prepareResponeMessage(CircForm circForm, String responseForNonCirculatingItem) {
218        ErrorMessage errorMessage = circForm.getErrorMessage();
219
220        if (null == errorMessage) {
221            errorMessage = new ErrorMessage();
222        }
223        String errorMessageContent = errorMessage.getErrorMessage();
224        if (StringUtils.isBlank(errorMessageContent)) {
225            errorMessage.setErrorMessage(responseForNonCirculatingItem);
226        } else {
227            errorMessage.setErrorMessage("<br/>" + responseForNonCirculatingItem);
228        }
229        circForm.setErrorMessage(errorMessage);
230    }
231
232    public String prepareResponseForRenewedLoanDocuments(DroolsResponse droolsResponse) {
233        StringBuilder stringBuilder = new StringBuilder();
234        Map<String, Object> context = droolsResponse.getDroolsExchange().getContext();
235        if (context.size() > 0) {
236            for (Iterator<String> iterator = context.keySet().iterator(); iterator.hasNext(); ) {
237                String key = iterator.next();
238                DroolsResponse individualDroolResponse = (DroolsResponse) context.get(key);
239                if (StringUtils.isNotBlank(individualDroolResponse.getSucessMessage()) && individualDroolResponse.getSucessMessage().equalsIgnoreCase("Successfully Renewed")) {
240                    OleLoanDocument oleLoanDocument = (OleLoanDocument) individualDroolResponse.getDroolsExchange().getContext().get(key);
241                    String content = "Successfully renewed for item (" + oleLoanDocument.getItemId() + ")";
242                    appendContentToStrinBuilder(stringBuilder, content);
243                } else {
244                    OleLoanDocument oleLoanDocument = (OleLoanDocument) individualDroolResponse.getDroolsExchange().getContext().get(key);
245                    String content = "Renewed failed for item (" + oleLoanDocument.getItemId() + ")";
246                    appendContentToStrinBuilder(stringBuilder, content);
247                }
248            }
249        }
250        return stringBuilder.toString();
251    }
252
253    public RenewItemControllerUtil getRenewItemControllerUtil() {
254        if (null == renewItemControllerUtil) {
255            renewItemControllerUtil = new RenewItemControllerUtil();
256        }
257        return renewItemControllerUtil;
258    }
259
260    public void setRenewItemControllerUtil(RenewItemControllerUtil renewItemControllerUtil) {
261        this.renewItemControllerUtil = renewItemControllerUtil;
262    }
263
264    public RenewItemsService getRenewItemsService() {
265        if (null == renewItemsService) {
266            renewItemsService = new NonSip2RenewItemService();
267        }
268        return renewItemsService;
269    }
270
271    public void setRenewItemsService(RenewItemsService renewItemsService) {
272        this.renewItemsService = renewItemsService;
273    }
274}