1 package org.kuali.ole.ncip.service.impl;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.kuali.asr.handler.CheckinItemResponseHandler;
6 import org.kuali.asr.handler.ResponseHandler;
7 import org.kuali.ole.OLEConstants;
8 import org.kuali.ole.deliver.bo.OleCirculationDesk;
9 import org.kuali.ole.deliver.drools.CheckedInItem;
10 import org.kuali.ole.deliver.drools.DroolsConstants;
11 import org.kuali.ole.deliver.drools.DroolsExchange;
12 import org.kuali.ole.deliver.form.OLEForm;
13 import org.kuali.ole.deliver.service.CircDeskLocationResolver;
14 import org.kuali.ole.deliver.util.DroolsResponse;
15 import org.kuali.ole.ncip.bo.OLECheckInItem;
16 import org.kuali.ole.deliver.controller.checkin.CheckInAPIController;
17 import org.kuali.rice.core.api.config.property.ConfigContext;
18
19 import java.util.Map;
20
21
22
23
24 public abstract class CheckinItemService {
25
26 private static final Logger LOG = Logger.getLogger(CheckinItemService.class);
27
28 protected String responseFormatType;
29 protected String response;
30
31 private ResponseHandler responseHandler;
32 private CircDeskLocationResolver circDeskLocationResolver;
33
34 public ResponseHandler getResponseHandler() {
35 if (null == responseHandler) {
36 responseHandler = new CheckinItemResponseHandler();
37 }
38 return responseHandler;
39 }
40
41 public void setResponseHandler(ResponseHandler responseHandler) {
42 this.responseHandler = responseHandler;
43 }
44
45 public CircDeskLocationResolver getCircDeskLocationResolver() {
46 if (circDeskLocationResolver == null) {
47 circDeskLocationResolver = new CircDeskLocationResolver();
48 }
49 return circDeskLocationResolver;
50 }
51
52 public void setCircDeskLocationResolver(CircDeskLocationResolver circDeskLocationResolver) {
53 this.circDeskLocationResolver = circDeskLocationResolver;
54 }
55
56 public String checkinItem(Map checkinParameters) {
57
58 CheckInAPIController checkInAPIController = new CheckInAPIController();
59
60 OLECheckInItem oleCheckInItem = new OLECheckInItem();
61
62 String operatorId = getOperatorId((String) checkinParameters.get("operatorId"));
63 String itemBarcode = (String) checkinParameters.get("itemBarcode");
64 responseFormatType = (String) checkinParameters.get("responseFormatType");
65 if (responseFormatType == null) {
66 responseFormatType = "xml";
67 }
68 responseFormatType = responseFormatType.toUpperCase();
69
70 OleCirculationDesk oleCirculationDesk = getCircDeskLocationResolver().getCircDeskForOpertorId(operatorId);
71 if (null == oleCirculationDesk) {
72 oleCheckInItem.setCode("026");
73 oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR));
74 return prepareResponse(oleCheckInItem);
75 }
76
77 if (StringUtils.isBlank(itemBarcode)) {
78 oleCheckInItem.setCode("900");
79 oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_BARCODE_REQUIRED));
80 return prepareResponse(oleCheckInItem);
81 }
82
83 DroolsExchange droolsExchange = new DroolsExchange();
84 droolsExchange.addToContext("itemBarcode", itemBarcode);
85 droolsExchange.addToContext("selectedCirculationDesk", oleCirculationDesk.getCirculationDeskId());
86 OLEForm oleAPIForm = new OLEForm();
87 oleAPIForm.setDroolsExchange(droolsExchange);
88
89 try {
90 DroolsResponse droolsResponse = checkInAPIController.checkin(oleAPIForm);
91 if (droolsResponse != null && StringUtils.isNotBlank(droolsResponse.getErrorMessage().getErrorMessage())) {
92 String checkinErrorMessage = droolsResponse.getErrorMessage().getErrorMessage();
93 String responseMessage = null;
94 if (droolsResponse.retriveErrorCode() != null) {
95 if (droolsResponse.retriveErrorCode().equalsIgnoreCase(DroolsConstants.ITEM_CLAIMS_RETURNED)) {
96 responseMessage = "Item is Claims Returned";
97 } else if (droolsResponse.retriveErrorCode().equalsIgnoreCase(DroolsConstants.ITEM_MISSING_PIECE)) {
98 responseMessage = "Item has missing pieces";
99 } else if (droolsResponse.retriveErrorCode().equalsIgnoreCase(DroolsConstants.ITEM_DAMAGED)) {
100 responseMessage = "Item is Damaged.";
101 } else if (droolsResponse.retriveErrorCode().equalsIgnoreCase(DroolsConstants.CHECKIN_REQUEST_EXITS_FOR_THIS_ITEM)) {
102 responseMessage = "Requests exists for this item";
103 }
104 }
105 if (StringUtils.isNotBlank(responseMessage)) {
106 oleCheckInItem.setCode("500");
107 oleCheckInItem.setMessage(responseMessage);
108 return prepareResponse(oleCheckInItem);
109 } else if (checkinErrorMessage.equalsIgnoreCase("Invalid item barcode!")) {
110 oleCheckInItem.setCode("014");
111 oleCheckInItem.setMessage(checkinErrorMessage);
112 return prepareResponse(oleCheckInItem);
113 }
114 oleCheckInItem.setCode("500");
115 oleCheckInItem.setMessage(checkinErrorMessage);
116 return prepareResponse(oleCheckInItem);
117 } else {
118 CheckedInItem checkedInItem = (CheckedInItem) droolsExchange.getFromContext("checkedInItem");
119 if (checkedInItem != null) {
120 oleCheckInItem.setAuthor(checkedInItem.getAuthor());
121 oleCheckInItem.setTitle(checkedInItem.getTitle());
122 oleCheckInItem.setCallNumber(checkedInItem.getCallNumber());
123 oleCheckInItem.setBarcode(checkedInItem.getPatronBarcode());
124 oleCheckInItem.setUserId(checkedInItem.getPatronId());
125 oleCheckInItem.setUserType(checkedInItem.getBorrowerType());
126 oleCheckInItem.setItemType(checkedInItem.getItemType());
127 oleCheckInItem.setCode("024");
128 oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_CHECKED_IN));
129 return prepareResponse(oleCheckInItem);
130 } else {
131 oleCheckInItem.setCode("025");
132 oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED));
133 return prepareResponse(oleCheckInItem);
134 }
135 }
136 } catch (Exception e) {
137 LOG.error("Exception " + e);
138 oleCheckInItem.setCode("025");
139 oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED));
140 return prepareResponse(oleCheckInItem);
141 }
142 }
143
144 public abstract String prepareResponse(OLECheckInItem oleCheckInItem);
145
146 public abstract String getOperatorId(String operatorId);
147 }