1 package org.kuali.ole.ncip.util;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.apache.commons.lang.StringUtils;
5 import org.apache.log4j.Logger;
6 import org.kuali.ole.OLEConstants;
7 import org.kuali.ole.OLEParameterConstants;
8 import org.kuali.ole.deliver.batch.OleDeliverBatchServiceImpl;
9 import org.kuali.ole.deliver.batch.OleMailer;
10 import org.kuali.ole.deliver.batch.OleNoticeBo;
11 import org.kuali.ole.deliver.bo.*;
12 import org.kuali.ole.deliver.service.CircDeskLocationResolver;
13 import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
14 import org.kuali.ole.describe.bo.OleInstanceItemType;
15 import org.kuali.ole.service.OlePatronHelperService;
16 import org.kuali.ole.service.OlePatronHelperServiceImpl;
17 import org.kuali.ole.utility.OleStopWatch;
18 import org.kuali.rice.core.api.mail.EmailBody;
19 import org.kuali.rice.core.api.mail.EmailFrom;
20 import org.kuali.rice.core.api.mail.EmailSubject;
21 import org.kuali.rice.core.api.mail.EmailTo;
22 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23 import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
24 import org.kuali.rice.coreservice.api.parameter.Parameter;
25 import org.kuali.rice.coreservice.api.parameter.ParameterKey;
26 import org.kuali.rice.kew.api.exception.WorkflowException;
27 import org.kuali.rice.kim.api.identity.Person;
28 import org.kuali.rice.kim.api.identity.PersonService;
29 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30 import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
31 import org.kuali.rice.krad.UserSession;
32 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
33 import org.kuali.rice.krad.service.BusinessObjectService;
34 import org.kuali.rice.krad.service.DocumentService;
35 import org.kuali.rice.krad.service.KRADServiceLocator;
36 import org.kuali.rice.krad.util.GlobalVariables;
37
38 import java.sql.Timestamp;
39 import java.util.Date;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44
45
46
47 public class OLENCIPAcceptItemUtil {
48
49 private static final Logger LOG = Logger.getLogger(OLENCIPAcceptItemUtil.class);
50
51 private BusinessObjectService businessObjectService;
52 private CircDeskLocationResolver circDeskLocationResolver;
53 private PersonService personService;
54 private DocumentService documentService;
55 private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService;
56 private OlePatronHelperServiceImpl olePatronHelperService;
57 private OLENCIPAcceptItemUtil olencipAcceptItemUtil;
58
59 public BusinessObjectService getBusinessObjectService() {
60 if (null == businessObjectService) {
61 businessObjectService = KRADServiceLocator.getBusinessObjectService();
62 }
63 return businessObjectService;
64 }
65
66 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
67 this.businessObjectService = businessObjectService;
68 }
69
70 private CircDeskLocationResolver getCircDeskLocationResolver() {
71 if (circDeskLocationResolver == null) {
72 circDeskLocationResolver = new CircDeskLocationResolver();
73 }
74 return circDeskLocationResolver;
75 }
76
77 public void setCircDeskLocationResolver(CircDeskLocationResolver circDeskLocationResolver) {
78 this.circDeskLocationResolver = circDeskLocationResolver;
79 }
80
81 public PersonService getPersonService() {
82 if (null == personService) {
83 personService = KimApiServiceLocator.getPersonService();
84 }
85 return personService;
86 }
87
88 public void setPersonService(PersonService personService) {
89 this.personService = personService;
90 }
91
92 private DocumentService getDocumentService() {
93 if (null == documentService) {
94 documentService = GlobalResourceLoader.getService(OLEConstants.DOCUMENT_HEADER_SERVICE);
95 }
96 return documentService;
97 }
98
99 public void setDocumentService(DocumentService documentService) {
100 this.documentService = documentService;
101 }
102
103 public OleDeliverRequestDocumentHelperServiceImpl getOleDeliverRequestDocumentHelperService() {
104 if (oleDeliverRequestDocumentHelperService == null) {
105 oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
106 }
107 return oleDeliverRequestDocumentHelperService;
108 }
109
110 public void setOleDeliverRequestDocumentHelperService(OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService) {
111 this.oleDeliverRequestDocumentHelperService = oleDeliverRequestDocumentHelperService;
112 }
113
114 public OlePatronHelperService getOlePatronHelperService() {
115 if (olePatronHelperService == null)
116 olePatronHelperService = new OlePatronHelperServiceImpl();
117 return olePatronHelperService;
118 }
119
120 public void setOlePatronHelperService(OlePatronHelperServiceImpl olePatronHelperService) {
121 this.olePatronHelperService = olePatronHelperService;
122 }
123
124 public OLENCIPAcceptItemUtil getOlencipAcceptItemUtil() {
125 if (null == olencipAcceptItemUtil) {
126 olencipAcceptItemUtil = new OLENCIPAcceptItemUtil();
127 }
128 return olencipAcceptItemUtil;
129 }
130
131 public void setOlencipAcceptItemUtil(OLENCIPAcceptItemUtil olencipAcceptItemUtil) {
132 this.olencipAcceptItemUtil = olencipAcceptItemUtil;
133 }
134
135 public String validateRequestType(String requestType) {
136 if (StringUtils.isNotBlank(requestType)) {
137 Map<String, String> requestTypeMap = new HashMap<String, String>();
138 requestTypeMap.put(OLEConstants.OleDeliverRequest.REQUEST_TYPE_CD, requestType);
139 List<OleDeliverRequestType> oleDeliverRequestTypeList = (List<OleDeliverRequestType>) getBusinessObjectService().findMatching(OleDeliverRequestType.class, requestTypeMap);
140 if (CollectionUtils.isNotEmpty(oleDeliverRequestTypeList)) {
141 return oleDeliverRequestTypeList.get(0).getRequestTypeId();
142 }
143 }
144 return null;
145 }
146
147 public boolean isValidItemType(String itemType) {
148 Map<String, String> itemTypeMap = new HashMap<>();
149 itemTypeMap.put(OLEConstants.OleDeliverRequest.ITEM_TYPE_CODE, itemType);
150 List<OleInstanceItemType> itemTypeList = (List) getBusinessObjectService().findMatching(OleInstanceItemType.class, itemTypeMap);
151 if (CollectionUtils.isNotEmpty(itemTypeList)) {
152 return true;
153 }
154 return false;
155 }
156
157 public OleCirculationDesk validatePickUpLocation(String pickUpLocation, String location) {
158 if (StringUtils.isNotBlank(pickUpLocation)) {
159 Map circulationDeskMap = new HashMap();
160 circulationDeskMap.put("oleCirculationDesk.circulationDeskCode", pickUpLocation);
161 circulationDeskMap.put("circulationPickUpDeskLocation", location);
162 List<OleCirculationDeskLocation> oleCirculationDeskLocations = (List<OleCirculationDeskLocation>) getBusinessObjectService().findMatching(OleCirculationDeskLocation.class, circulationDeskMap);
163 if (CollectionUtils.isNotEmpty(oleCirculationDeskLocations)) {
164 return oleCirculationDeskLocations.get(0).getOleCirculationDesk();
165 }
166 }
167 return null;
168 }
169
170 public OleDeliverRequestBo getOleDeliverRequestBo(String itemType, String location, String requestTypeId, String requestType, OlePatronDocument olePatronDocument) {
171 OleDeliverRequestBo oleDeliverRequestBo = new OleDeliverRequestBo();
172
173 oleDeliverRequestBo.setItemType(itemType);
174
175 Map<String, String> locationMap = getCircDeskLocationResolver().getLocationMap(location);
176 oleDeliverRequestBo.setItemLibrary(locationMap.get(OLEConstants.ITEM_LIBRARY));
177 oleDeliverRequestBo.setItemInstitution(locationMap.get(OLEConstants.ITEM_INSTITUTION));
178 oleDeliverRequestBo.setItemCampus(locationMap.get(OLEConstants.ITEM_CAMPUS));
179 oleDeliverRequestBo.setItemCollection(locationMap.get(OLEConstants.ITEM_COLLECTION));
180 oleDeliverRequestBo.setShelvingLocation(locationMap.get(OLEConstants.ITEM_SHELVING));
181
182 oleDeliverRequestBo.setRequestTypeCode(requestType);
183 if (isHoldDelivery(requestTypeId)) {
184 oleDeliverRequestBo.setRequestTypeId("4");
185 oleDeliverRequestBo.setRequestTypeCode("Hold/Hold Request");
186 } else if (isPageDelivery(requestTypeId)) {
187 oleDeliverRequestBo.setRequestTypeId("6");
188 oleDeliverRequestBo.setRequestTypeCode("Page/Hold Request");
189 } else if (isRecallDelivery(requestTypeId)) {
190 oleDeliverRequestBo.setRequestTypeId("2");
191 oleDeliverRequestBo.setRequestTypeCode("Recall/Hold Request");
192 } else {
193 oleDeliverRequestBo.setRequestTypeId(requestTypeId);
194 }
195
196 oleDeliverRequestBo.setOlePatron(olePatronDocument);
197 return oleDeliverRequestBo;
198 }
199
200 private boolean isRecallDelivery(String requestTypeId) {
201 return requestTypeId.equals("1");
202 }
203
204 private boolean isPageDelivery(String requestTypeId) {
205 return requestTypeId.equals("5");
206 }
207
208 private boolean isHoldDelivery(String requestTypeId) {
209 return requestTypeId.equals("3");
210 }
211
212 public void savePickupNoticeHistory(String content, String olePatronId) {
213 OLEDeliverNoticeHistory oleDeliverNoticeHistory = new OLEDeliverNoticeHistory();
214 oleDeliverNoticeHistory.setNoticeType(OLEConstants.PICKUP_NOTICE);
215 oleDeliverNoticeHistory.setNoticeSentDate(new Timestamp(new Date().getTime()));
216 oleDeliverNoticeHistory.setPatronId(olePatronId);
217 oleDeliverNoticeHistory.setNoticeSendType(OLEConstants.EMAIL);
218 oleDeliverNoticeHistory.setNoticeContent(content.getBytes());
219 getBusinessObjectService().save(oleDeliverNoticeHistory);
220 }
221
222 public Map placeRequest(String operatorId, OlePatronDocument olePatronDocument, String itemBarcode, String itemUUID, OleCirculationDesk olePickUpLocation, String requestTypeId, String bibId, String title, String author, String callNumber, String requestExpirationDay, String location) {
223 OleStopWatch oleStopWatch = new OleStopWatch();
224 oleStopWatch.start();
225 Map responseMap = new HashMap();
226 MaintenanceDocument newDocument = null;
227 try {
228 if (null == GlobalVariables.getUserSession()) {
229 Person person = getPersonService().getPerson(operatorId);
230 String principalName = person.getPrincipalName();
231 UserSession userSession = new UserSession(principalName);
232 GlobalVariables.setUserSession(userSession);
233 }
234 newDocument = (MaintenanceDocument) getDocumentService().getNewDocument(OLEConstants.REQUEST_DOC_TYPE);
235 } catch (WorkflowException e) {
236 e.printStackTrace();
237 responseMap.put(OLEConstants.STATUS, OLEConstants.RQST_CREATION_FAIL);
238 return responseMap;
239 }
240 OleDeliverRequestBo oleDeliverRequestBo = null;
241 oleDeliverRequestBo = (OleDeliverRequestBo) newDocument.getNewMaintainableObject().getDataObject();
242 try {
243 oleDeliverRequestBo.setCreateDate(new java.sql.Date(System.currentTimeMillis()));
244 oleDeliverRequestBo.setRequestLevel("Item Level");
245
246 oleDeliverRequestBo.setBorrowerId(olePatronDocument.getOlePatronId());
247 oleDeliverRequestBo.setBorrowerBarcode(olePatronDocument.getBarcode());
248 oleDeliverRequestBo.setOlePatron(olePatronDocument);
249
250 oleDeliverRequestBo.setRequestTypeId(requestTypeId);
251
252 oleDeliverRequestBo.setBorrowerQueuePosition(1);
253
254 oleDeliverRequestBo.setPickUpLocationId(olePickUpLocation.getCirculationDeskId());
255 oleDeliverRequestBo.setOlePickUpLocation(olePickUpLocation);
256
257 oleDeliverRequestBo.setItemId(itemBarcode);
258 oleDeliverRequestBo.setItemUuid(itemUUID);
259
260 oleDeliverRequestBo.setBibId(bibId);
261 oleDeliverRequestBo.setTitle(title);
262 oleDeliverRequestBo.setAuthor(author);
263 oleDeliverRequestBo.setCallNumber(callNumber);
264
265 oleDeliverRequestBo.setRequestCreator(OLEConstants.OleDeliverRequest.REQUESTER_OPERATOR);
266 oleDeliverRequestBo.setOperatorCreateId(operatorId);
267
268 oleDeliverRequestBo.setItemLocation(location);
269
270 if (StringUtils.isNotBlank(requestExpirationDay)) {
271 Timestamp requestExpirationDate = getOleDeliverRequestDocumentHelperService().calculateXDatesBasedOnCalendar(getOleDeliverRequestDocumentHelperService().getCalendarGroup(oleDeliverRequestBo.getItemLocation()), requestExpirationDay, null, true);
272 oleDeliverRequestBo.setRequestExpiryDate(new java.sql.Date(requestExpirationDate.getTime()));
273 }
274
275 newDocument.getDocumentHeader().setDocumentDescription(OLEConstants.NEW_REQUEST_DOC);
276 newDocument.getNewMaintainableObject().setDataObject(oleDeliverRequestBo);
277
278 try {
279 newDocument = (MaintenanceDocument) getDocumentService().routeDocument(newDocument, null, null);
280 } catch (WorkflowException e) {
281 LOG.error("Exception " + e);
282 }
283 oleDeliverRequestBo = (OleDeliverRequestBo) newDocument.getNewMaintainableObject().getDataObject();
284
285 if (oleDeliverRequestBo.getRequestId() != null) {
286 responseMap.put(OLEConstants.STATUS, OLEConstants.RQST_SUCCESS);
287 responseMap.put(OLEConstants.OleDeliverRequest.REQUEST_ID, oleDeliverRequestBo.getRequestId());
288 } else {
289 responseMap.put(OLEConstants.STATUS, OLEConstants.RQST_FAIL);
290 }
291 } catch (Exception e) {
292 LOG.error("Exception " + e);
293 responseMap.put(OLEConstants.STATUS, OLEConstants.RQST_FAIL);
294 return responseMap;
295 }
296 oleStopWatch.end();
297 LOG.info("Time taken to place request: " + oleStopWatch.getTotalTime());
298 sendNotice(oleDeliverRequestBo, olePatronDocument);
299 return responseMap;
300 }
301
302 private void sendNotice(OleDeliverRequestBo oleDeliverRequestBo, OlePatronDocument olePatronDocument) {
303 OleStopWatch oleStopWatch = new OleStopWatch();
304 oleStopWatch.start();
305 String noticeSendParameter = getParameter(OLEParameterConstants.NCIP_ACCEPT_ITEM_NOTICE_INDICATOR);
306 if (noticeSendParameter != null && (noticeSendParameter.trim().isEmpty() || noticeSendParameter.equalsIgnoreCase("Y"))) {
307
308 OleNoticeBo oleNoticeBo = new OleNoticeBo();
309 EntityTypeContactInfoBo entityTypeContactInfoBo = olePatronDocument.getEntity().getEntityTypeContactInfos().get(0);
310 try {
311 oleNoticeBo.setPatronName(olePatronDocument.getEntity().getNames().get(0).getFirstName() + " " + oleDeliverRequestBo.getOlePatron().getEntity().getNames().get(0).getLastName());
312 oleNoticeBo.setPatronAddress(getOlePatronHelperService().getPatronPreferredAddress(entityTypeContactInfoBo) != null ? getOlePatronHelperService().getPatronPreferredAddress(entityTypeContactInfoBo) : "");
313 oleNoticeBo.setPatronEmailAddress(getOlePatronHelperService().getPatronHomeEmailId(entityTypeContactInfoBo) != null ? getOlePatronHelperService().getPatronHomeEmailId(entityTypeContactInfoBo) : "");
314 oleNoticeBo.setPatronPhoneNumber(getOlePatronHelperService().getPatronHomePhoneNumber(entityTypeContactInfoBo) != null ? getOlePatronHelperService().getPatronHomePhoneNumber(entityTypeContactInfoBo) : "");
315 } catch (Exception e) {
316 LOG.error("Exception", e);
317 if (LOG.isDebugEnabled()) {
318 LOG.debug("Exception Occured while setting the patron information for the patron . Patron Barcode : " + oleDeliverRequestBo.getBorrowerBarcode());
319 }
320 }
321 oleNoticeBo.setNoticeName(OLEConstants.PICKUP_NOTICE);
322 Date pickupDate = new java.sql.Date(System.currentTimeMillis());
323 if (oleDeliverRequestBo.getOlePickUpLocation() != null && oleDeliverRequestBo.getOlePickUpLocation().getOnHoldDays() != null) {
324 pickupDate = getOleDeliverRequestDocumentHelperService().addDate(new java.sql.Date(System.currentTimeMillis()), new Integer(oleDeliverRequestBo.getOlePickUpLocation().getOnHoldDays()));
325 }
326 oleNoticeBo.setNoticeSpecificContent(OLEConstants.PICKUP_NOTICE_START_CONTENT + oleDeliverRequestBo.getOlePickUpLocation().getCirculationDeskPublicName() + OLEConstants.PICKUP_NOTICE_MIDDLE_CONTENT + pickupDate + OLEConstants.PICKUP_NOTICE_FINAL_CONTENT);
327 oleNoticeBo.setAuthor(oleDeliverRequestBo.getAuthor());
328 oleNoticeBo.setItemCallNumber(oleDeliverRequestBo.getCallNumber());
329 oleNoticeBo.setItemId(oleDeliverRequestBo.getItemId());
330 oleNoticeBo.setTitle(oleDeliverRequestBo.getTitle());
331 oleNoticeBo.setCirculationDeskName(oleDeliverRequestBo.getOlePickUpLocation().getCirculationDeskPublicName());
332 oleNoticeBo.setCirculationDeskReplyToEmail(oleDeliverRequestBo.getOlePickUpLocation().getReplyToEmail());
333 OleDeliverBatchServiceImpl oleDeliverBatchService = new OleDeliverBatchServiceImpl();
334 try {
335 String content = oleDeliverBatchService.getEmailPickUpNotice(oleNoticeBo);
336 if (!content.trim().equals("")) {
337 OleMailer oleMailer = GlobalResourceLoader.getService("oleMailer");
338 if (oleDeliverRequestBo.getOlePickUpLocation() != null && StringUtils.isNotBlank(oleDeliverRequestBo.getOlePickUpLocation().getReplyToEmail())) {
339 oleMailer.sendEmail(new EmailFrom(oleDeliverRequestBo.getOlePickUpLocation().getReplyToEmail()), new EmailTo(oleNoticeBo.getPatronEmailAddress()), new EmailSubject(OLEConstants.NOTICE_MAIL), new EmailBody(content), true);
340 } else {
341 String fromAddress = getParameter(OLEParameterConstants.NOTICE_FROM_MAIL);
342 if (fromAddress != null && (fromAddress.equals("") || fromAddress.trim().isEmpty())) {
343 fromAddress = OLEConstants.KUALI_MAIL;
344 }
345 oleMailer.sendEmail(new EmailFrom(fromAddress), new EmailTo(oleNoticeBo.getPatronEmailAddress()), new EmailSubject(OLEConstants.NOTICE_MAIL), new EmailBody(content), true);
346 }
347 if (LOG.isDebugEnabled()) {
348 LOG.debug("Mail send Successfully to " + oleNoticeBo.getPatronEmailAddress());
349 }
350
351 getOlencipAcceptItemUtil().savePickupNoticeHistory(content, olePatronDocument.getOlePatronId());
352 } else {
353 if (LOG.isDebugEnabled()) {
354 LOG.debug("Notice Type :" + oleNoticeBo.getNoticeName() + " " + "Item Barcode : " + oleNoticeBo.getItemId() + " " + "Patron Name :" + oleNoticeBo.getPatronName());
355 }
356 }
357 } catch (Exception e) {
358 LOG.error("Exception", e);
359 }
360 }
361 oleStopWatch.end();
362 LOG.info("Time taken to send notice : " + oleStopWatch.getTotalTime());
363 }
364
365 public String getParameter(String name) {
366 ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,name);
367 Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
368 if(parameter==null){
369 parameterKey = ParameterKey.create(OLEConstants.APPL_ID_OLE, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,name);
370 parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
371 }
372 return parameter!=null?parameter.getValue():null;
373 }
374 }