1 package org.kuali.ole.deliver.controller.renew;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.apache.commons.lang3.StringUtils;
5 import org.apache.log4j.Logger;
6 import org.kuali.ole.deliver.bo.OLEDeliverNotice;
7 import org.kuali.ole.deliver.bo.OleLoanDocument;
8 import org.kuali.ole.deliver.bo.OlePatronDocument;
9 import org.kuali.ole.deliver.controller.checkout.CircUtilController;
10 import org.kuali.ole.deliver.form.CircForm;
11 import org.kuali.ole.deliver.util.*;
12 import org.kuali.ole.docstore.common.document.Item;
13 import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemRecord;
14 import org.kuali.ole.ncip.service.impl.NonSip2RenewItemService;
15 import org.kuali.ole.ncip.service.impl.RenewItemsService;
16
17 import java.sql.Timestamp;
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22
23
24
25
26 public class RenewController extends CircUtilController {
27
28 private static final Logger LOG = Logger.getLogger(RenewController.class);
29 private RenewItemControllerUtil renewItemControllerUtil;
30 private RenewItemsService renewItemsService;
31
32 public DroolsResponse renewItems(List<OleLoanDocument> selectedLoanDocumentList, OlePatronDocument olePatronDocument) {
33 List<Item> itemList = new ArrayList<>();
34 DroolsResponse finalDroolResponse = new DroolsResponse();
35
36 for (Iterator<OleLoanDocument> iterator = selectedLoanDocumentList.iterator(); iterator.hasNext(); ) {
37 DroolsResponse droolsResponse = new DroolsResponse();
38 OleLoanDocument oleLoanDocument = iterator.next();
39
40 boolean isIndefiniteDueDate = oleLoanDocument.IsIndefiniteDueDate();
41 if (!isIndefiniteDueDate) {
42
43 List<Object> facts = new ArrayList<>();
44
45 oleLoanDocument.setPastDueDate(oleLoanDocument.getLoanDueDate());
46 ItemRecord itemRecord = getItemRecordByBarcode(oleLoanDocument.getItemId());
47 OleItemRecordForCirc oleItemRecordForCirc = ItemInfoUtil.getInstance().getOleItemRecordForCirc(itemRecord, null);
48
49 NoticeInfo noticeInfo = new NoticeInfo();
50 facts.add(oleLoanDocument);
51 facts.add(olePatronDocument);
52 facts.add(oleItemRecordForCirc);
53 facts.add(droolsResponse);
54 facts.add(noticeInfo);
55 fireRules(facts, null, "renewal validation");
56
57 if (droolsResponse.isRuleMatched()) {
58 if (StringUtils.isBlank(droolsResponse.retrieveErrorMessage())) {
59 boolean pastAndRenewDueDateSame = false;
60 try {
61 pastAndRenewDueDateSame = oleLoanDocument.isPastAndRenewDueDateSame();
62 if (!pastAndRenewDueDateSame) {
63 Integer numRenewals = getRenewItemControllerUtil().incrementRenewalCount(oleLoanDocument);
64 oleLoanDocument.setNumberOfRenewals(numRenewals.toString());
65 List<OLEDeliverNotice> oleDeliverNotices = processNotices(oleLoanDocument, oleItemRecordForCirc.getItemRecord());
66 oleLoanDocument.setDeliverNotices(oleDeliverNotices);
67 if (null != oleLoanDocument.getLoanId()) {
68 Item itemForUpdate = getItemForUpdate(oleLoanDocument);
69 if (null != itemForUpdate) {
70 itemList.add(itemForUpdate);
71 droolsResponse.setSucessMessage("Successfully Renewed");
72 droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
73 finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
74 }
75 }
76 } else {
77 droolsResponse.addErrorMessageCode("PastAndNewDueDateSame");
78 droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
79 finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
80 }
81 } catch (Exception e) {
82 if (e.getMessage().equalsIgnoreCase("No Fixed Due Date found for the renewal policy")) {
83 oleLoanDocument.setLoanDueDate(new Timestamp(oleLoanDocument.getPastDueDate().getTime()));
84 droolsResponse.addErrorMessageCode("InvalidFixedDueDateMapping");
85 droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
86 finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
87 } else {
88 oleLoanDocument.setLoanDueDate(new Timestamp(oleLoanDocument.getPastDueDate().getTime()));
89 droolsResponse.addErrorMessageCode("InternalException");
90 droolsResponse.addErrorMessage(e.getMessage());
91 droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
92 finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
93 }
94 }
95 } else {
96
97 droolsResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
98 finalDroolResponse.getDroolsExchange().getContext().put(oleLoanDocument.getItemUuid(), droolsResponse);
99 }
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(CollectionUtils.isNotEmpty(individualDroolResponse.getErrorMessage().getPermissions())){
175 individualLoanDocument.setErrorMessage(null);
176 individualLoanDocument.setErrorMessage(individualDroolResponse.retrieveErrorMessage());
177 circForm.getLoanDocumentsForRenew().add(individualLoanDocument);
178 } else {
179 String content = "Item (" + individualLoanDocument.getItemId() + ") : " + individualDroolResponse.retrieveErrorMessage();
180 appendContentToStrinBuilder(messageContentForRenew, content);
181 }
182 } else if (individualDroolResponse.retriveErrorCode().equalsIgnoreCase("PastAndNewDueDateSame")) {
183 Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
184 OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
185 String content = "Item (" + individualLoanDocument.getItemId() + ") was not renewed because the new due date/time would not change.";
186 appendContentToStrinBuilder(messageContentForRenew, content);
187 } else if (individualDroolResponse.retriveErrorCode().equalsIgnoreCase("InDefiniteDueDate")) {
188 Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
189 OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
190 String content = "Item (" + individualLoanDocument.getItemId() + ") wasn't renewed. Items on indefinite loan do not need to be renewed.";
191 appendContentToStrinBuilder(messageContentForRenew, content);
192 } else if (individualDroolResponse.retriveErrorCode().equalsIgnoreCase("InvalidFixedDueDateMapping")) {
193 Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
194 OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
195 String content = "Item (" + individualLoanDocument.getItemId() + ") wasn't renewed. Invalid Fixed Due Date Mapping.";
196 appendContentToStrinBuilder(messageContentForRenew, content);
197 } else if (individualDroolResponse.retriveErrorCode().equalsIgnoreCase("InternalException")) {
198 Map<String, Object> individualResponseMap = individualDroolResponse.getDroolsExchange().getContext();
199 OleLoanDocument individualLoanDocument = (OleLoanDocument) individualResponseMap.get(key);
200 String content = "Item (" + individualLoanDocument.getItemId() + ") wasn't renewed." + droolsResponse.retrieveErrorMessage();
201 appendContentToStrinBuilder(messageContentForRenew, content);
202 }
203 }
204 }
205 return messageContentForRenew.toString();
206 }
207
208 private void appendContentToStrinBuilder(StringBuilder stringBuilder, String content) {
209 if (stringBuilder.length() > 0) {
210 stringBuilder.append("<br/>").append(content);
211 } else {
212 stringBuilder.append(content);
213 }
214 }
215
216 public void prepareResponeMessage(CircForm circForm, String responseForNonCirculatingItem) {
217 ErrorMessage errorMessage = circForm.getErrorMessage();
218
219 if (null == errorMessage) {
220 errorMessage = new ErrorMessage();
221 }
222 String errorMessageContent = errorMessage.getErrorMessage();
223 if (StringUtils.isBlank(errorMessageContent)) {
224 errorMessage.setErrorMessage(responseForNonCirculatingItem);
225 } else {
226 errorMessage.setErrorMessage("<br/>" + responseForNonCirculatingItem);
227 }
228 circForm.setErrorMessage(errorMessage);
229 }
230
231 public String prepareResponseForRenewedLoanDocuments(DroolsResponse droolsResponse) {
232 StringBuilder stringBuilder = new StringBuilder();
233 Map<String, Object> context = droolsResponse.getDroolsExchange().getContext();
234 if (context.size() > 0) {
235 for (Iterator<String> iterator = context.keySet().iterator(); iterator.hasNext(); ) {
236 String key = iterator.next();
237 DroolsResponse individualDroolResponse = (DroolsResponse) context.get(key);
238 if (StringUtils.isNotBlank(individualDroolResponse.getSucessMessage()) && individualDroolResponse.getSucessMessage().equalsIgnoreCase("Successfully Renewed")) {
239 OleLoanDocument oleLoanDocument = (OleLoanDocument) individualDroolResponse.getDroolsExchange().getContext().get(key);
240 String content = "Successfully renewed for item (" + oleLoanDocument.getItemId() + ")";
241 appendContentToStrinBuilder(stringBuilder, content);
242 } else {
243 OleLoanDocument oleLoanDocument = (OleLoanDocument) individualDroolResponse.getDroolsExchange().getContext().get(key);
244 String content = "Renewed failed for item (" + oleLoanDocument.getItemId() + ")";
245 appendContentToStrinBuilder(stringBuilder, content);
246 }
247 }
248 }
249 return stringBuilder.toString();
250 }
251
252 public RenewItemControllerUtil getRenewItemControllerUtil() {
253 if (null == renewItemControllerUtil) {
254 renewItemControllerUtil = new RenewItemControllerUtil();
255 }
256 return renewItemControllerUtil;
257 }
258
259 public void setRenewItemControllerUtil(RenewItemControllerUtil renewItemControllerUtil) {
260 this.renewItemControllerUtil = renewItemControllerUtil;
261 }
262
263 public RenewItemsService getRenewItemsService() {
264 if (null == renewItemsService) {
265 renewItemsService = new NonSip2RenewItemService();
266 }
267 return renewItemsService;
268 }
269
270 public void setRenewItemsService(RenewItemsService renewItemsService) {
271 this.renewItemsService = renewItemsService;
272 }
273 }