1 package org.kuali.ole.service;
2
3 import org.kuali.incubator.SolrRequestReponseHandler;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.deliver.request.bo.OleDeliverRequestBo;
6 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Item;
7 import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkItemOlemlRecordProcessor;
8 import org.kuali.ole.editor.service.DocstoreHelperService;
9 import org.kuali.ole.deliver.loan.bo.OleLoanDocument;
10 import org.kuali.ole.patron.bill.PatronBillPayment;
11 import org.kuali.ole.patron.bo.OleAddressBo;
12 import org.kuali.ole.patron.bo.OlePatronDocument;
13 import org.kuali.ole.patron.bo.OleProxyPatronDocument;
14 import org.kuali.rice.krad.service.KRADServiceLocator;
15
16 import java.sql.Timestamp;
17 import java.text.SimpleDateFormat;
18 import java.util.*;
19 import java.util.concurrent.TimeUnit;
20
21
22
23
24
25
26
27
28 public class OleCirculationPolicyServiceImpl implements OleCirculationPolicyService {
29 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleLocationServiceImpl.class);
30
31
32
33
34
35
36 @Override
37 public boolean isValidBarcode(String patronBarcode) {
38 return true;
39 }
40
41
42
43
44
45
46 @Override
47 public Date getPatronMembershipExpireDate(String patronBarcode) {
48 Date expirationDate=null;
49 try{
50 Map<String, String> criteria = new HashMap<String, String>();
51 criteria.put("barcode", patronBarcode);
52 List<OlePatronDocument> olePatronDocument = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,criteria);
53 if(olePatronDocument.size()==1) {
54 expirationDate = olePatronDocument.get(0).getExpirationDate();
55 }
56 }catch(Exception e){
57 LOG.error(e.getMessage());
58 }
59 return expirationDate;
60 }
61
62
63
64
65
66
67 @Override
68 public int getNoOfItemsLoaned(String olePatronId,boolean renewalFlag) {
69 Map<String, String> criteria = new HashMap<String, String>();
70 Collection<OleLoanDocument> oleLoanDocuments=null;
71 try{
72 criteria.put("patronId", olePatronId);
73 oleLoanDocuments = KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class,criteria);
74 }catch(Exception e){
75 LOG.error(e.getMessage());
76 }
77 int loanedItems = 0;
78 if(oleLoanDocuments!=null){
79 loanedItems = renewalFlag ?oleLoanDocuments.size() : oleLoanDocuments.size()+1;
80 }
81 return loanedItems;
82 }
83
84
85
86
87
88
89 @Override
90 public Timestamp calculateLoanDueDate(String loanPeriod) {
91 String loanPeriodType[]=null;
92 Timestamp dueDate = null;
93 Calendar calendar = Calendar.getInstance();
94 if(loanPeriod != null && loanPeriod.trim().length()>0){
95 loanPeriodType = loanPeriod.split("-");
96 int loanPeriodValue = Integer.parseInt(loanPeriodType[0].toString());
97 String loanPeriodTypeValue = loanPeriodType[1].toString();
98 if(loanPeriodTypeValue.equalsIgnoreCase("MINUTE")){
99 calendar.add(Calendar.MINUTE, loanPeriodValue);
100 } else if(loanPeriodTypeValue.equalsIgnoreCase("HOUR")) {
101 calendar.add(Calendar.HOUR, loanPeriodValue);
102 } else if(loanPeriodTypeValue.equalsIgnoreCase("WEEK")) {
103 calendar.add(Calendar.WEEK_OF_MONTH, loanPeriodValue);
104 } else {
105 calendar.add(Calendar.DATE, loanPeriodValue);
106 }
107 dueDate = new Timestamp(calendar.getTime().getTime());
108 }
109 return dueDate;
110 }
111 public PatronBillPayment getPatronBillPayment(String itemBarcode){
112 Map<String, String> criteria = new HashMap<String, String>();
113 criteria.put("itemBarcode",itemBarcode);
114 List<PatronBillPayment> patronBillPayments = (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,criteria);
115 return patronBillPayments!=null&&patronBillPayments.size()>0 ? patronBillPayments.get(0):null;
116 }
117
118 @Override
119 public HashMap getNumberOfOverdueItemsCheckedOut(String patronId) {
120 int count = 0;
121 HashMap overdueItems = new HashMap();
122
123 List<OleLoanDocument> overdueLoanedItem = new ArrayList<OleLoanDocument>();
124 Map<String, String> criteria = new HashMap<String, String>();
125 criteria.put("patronId", patronId);
126 List<OleLoanDocument> oleLoanDocuments= (List<OleLoanDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class,criteria);
127 oleLoanDocuments = oleLoanDocuments!=null?oleLoanDocuments:new ArrayList<OleLoanDocument>();
128 for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
129 Integer numberOfOverdueNoticesSent = new Integer(oleLoanDocument.getNumberOfOverdueNoticesSent()!=null?oleLoanDocument.getNumberOfOverdueNoticesSent():"0");
130 if(numberOfOverdueNoticesSent>0){
131 overdueLoanedItem.add(oleLoanDocument);
132 count++;
133 }
134 }
135 overdueItems.put("count",count);
136 overdueItems.put("oleLoanDocumentList",overdueLoanedItem);
137 return overdueItems;
138 }
139
140 @Override
141 public HashMap getRecalledOverdueItemsCheckedOut(List<OleLoanDocument> oleLoanDocuments) {
142 int count = 0;
143 List<Integer> listOfRecalledOverdueDays = new ArrayList<Integer>();
144 HashMap recalledOverdueItems = new HashMap();
145 List<OleLoanDocument> recallOverdueLoanedItem = new ArrayList<OleLoanDocument>();
146 oleLoanDocuments = oleLoanDocuments!=null?oleLoanDocuments:new ArrayList<OleLoanDocument>();
147 for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
148 Map<String, String> criteria = new HashMap<String, String>();
149 criteria.put("itemId", oleLoanDocument.getItemId());
150 List<OleDeliverRequestBo> oleDeliverRequestBoList =(List<OleDeliverRequestBo>)KRADServiceLocator.getBusinessObjectService().findMatching(OleDeliverRequestBo.class,criteria);
151 for(OleDeliverRequestBo oleDeliverRequestBo : oleDeliverRequestBoList){
152 if(oleDeliverRequestBo.getOleDeliverRequestType().getRequestTypeCode().contains("recall")){
153 count ++;
154 recallOverdueLoanedItem.add(oleLoanDocument);
155 if(oleLoanDocument!=null && oleLoanDocument.getLoanDueDate().compareTo(new Date())<=0){
156 listOfRecalledOverdueDays.add(new Integer(getTimeDiff(oleLoanDocument.getLoanDueDate(),new Date())));
157 }
158 break;
159 }
160 }
161 }
162 recalledOverdueItems.put("count",count);
163 recalledOverdueItems.put("oleLoanDocumentList",recallOverdueLoanedItem);
164 recalledOverdueItems.put("listOfRecalledOverdueDays",listOfRecalledOverdueDays);
165 return recalledOverdueItems;
166 }
167
168 @Override
169 public List<Integer> getNumberOfOverdueDays(String itemBarcode) {
170 List<Integer> listOfOverdueDays = new ArrayList<Integer>();
171 Map<String, String> criteria = new HashMap<String, String>();
172 criteria.put("itemId", itemBarcode);
173 OleLoanDocument oleLoanDocument=KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleLoanDocument.class,criteria);
174 if(oleLoanDocument!=null && oleLoanDocument.getLoanDueDate().compareTo(new Date())<=0){
175 listOfOverdueDays.add(new Integer(getTimeDiff(oleLoanDocument.getLoanDueDate(),new Date())));
176 }
177 return listOfOverdueDays;
178 }
179 public String getTimeDiff(Date dateOne, Date dateTwo) {
180 String diff = "";
181 long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
182 diff = String.format("%d",TimeUnit.MILLISECONDS.toDays(timeDiff),
183 -TimeUnit.HOURS.toDays(timeDiff));
184 return diff;
185 }
186 @Override
187 public int getNumberOfClaimsReturned(String patronId) {
188 int count = 0;
189 Map<String, String> criteria = new HashMap<String, String>();
190 criteria.put("patronId", patronId);
191 List<OleLoanDocument> oleLoanDocuments= (List<OleLoanDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class,criteria);
192 oleLoanDocuments = oleLoanDocuments!=null?oleLoanDocuments:new ArrayList<OleLoanDocument>();
193 for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
194 if(oleLoanDocument.isClaimsReturnedIndicator()){
195 count++;
196 }
197 }
198 return count;
199 }
200
201 @Override
202 public void checkInItem(String itemBarcode) throws Exception {
203 Map<String, String> criteria = new HashMap<String, String>();
204 criteria.put("itemId", itemBarcode);
205 OleLoanDocument oleLoanDocument=KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleLoanDocument.class,criteria);
206 KRADServiceLocator.getBusinessObjectService().delete(oleLoanDocument);
207 Item oleItem = getOleItem(itemBarcode);
208 updateItemStatus(oleItem);
209 }
210 private Item getOleItem(String itemBarcode) throws Exception{
211 SolrRequestReponseHandler solrRequestReponseHandler = new SolrRequestReponseHandler();
212 HashMap itemAndTitleDetails = new HashMap();
213 Item oleItem = null;
214 String itemXml = null;
215 try {
216
217 List<HashMap<String, Object>> documentList= solrRequestReponseHandler.retriveResults("ItemBarcode_display:"+itemBarcode);
218 HashMap<String, Object> itemvalues = documentList.get(0);
219 itemAndTitleDetails.put("instanceUuid",(String)((ArrayList)itemvalues.get("instanceIdentifier")).get(0));
220 itemAndTitleDetails.put("itemUuid",(String)((ArrayList)itemvalues.get("ItemIdentifier_display")).get(0));
221 if(itemvalues.get("bibIdentifier")!= null){
222 itemAndTitleDetails.put("bibUuid",(String)((ArrayList)itemvalues.get("bibIdentifier")).get(0));
223 }
224 Map itemUuid = itemAndTitleDetails;
225 DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
226 itemXml = docstoreHelperService.getDocstoreData((String)itemUuid.get("itemUuid"));
227 System.out.println(itemXml);
228 }
229 catch (Exception e) {
230 LOG.error(OLEConstants.ITM_BARCD_NT_AVAL_DOC);
231 throw new Exception(OLEConstants.ITM_BARCD_NT_AVAL_DOC);
232 }
233 try{
234 WorkItemOlemlRecordProcessor workItemOlemlRecordProcessor = new WorkItemOlemlRecordProcessor();
235 oleItem = workItemOlemlRecordProcessor.fromXML(itemXml);
236 }catch (Exception e){
237 LOG.error(OLEConstants.PAR_EXP);
238 throw new Exception(OLEConstants.PAR_EXP);
239 }
240 return oleItem;
241
242 }
243
244 private void updateItemStatus(Item oleItem) throws Exception{
245 try{
246 DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
247 String itemUuid = oleItem.getItemIdentifier();
248 String itemXmlContent = buildItemContent(oleItem);
249 String itemRecordUpdateResponse =
250 docstoreHelperService.updateInstanceRecord(itemUuid, OLEConstants.ITEM_DOC_TYPE, itemXmlContent);
251 LOG.info(itemRecordUpdateResponse);
252
253 } catch (Exception e) {
254 LOG.error(OLEConstants.ITM_STS_TO_DOC_FAIL);
255 throw new Exception(OLEConstants.ITM_STS_TO_DOC_FAIL);
256 }
257 }
258
259 private String buildItemContent (Item oleItem) throws Exception{
260 oleItem.setItemStatus(OLEConstants.ITEM_STATUS_AVAILABLE);
261 oleItem.setItemStatusEffectiveDate( String.valueOf(new SimpleDateFormat(OLEConstants.DAT_FORM).format(new Date())));
262 String itemContent = new WorkItemOlemlRecordProcessor().toXML(oleItem);
263 return itemContent;
264 }
265 public Integer getHoursDiff(Date dateOne, Date dateTwo) {
266 if(dateOne!=null && dateTwo!=null && dateOne.compareTo(dateTwo)<=0){
267 String diff = "";
268 long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
269 diff = String.format("%d",TimeUnit.MILLISECONDS.toHours(timeDiff),
270 -TimeUnit.HOURS.toMinutes(timeDiff));
271 return new Integer(diff);
272 }
273 return 0;
274 }
275
276 public List<OlePatronDocument> isProxyPatron(String partonId) throws Exception{
277 List realPatron = new ArrayList();
278 Map<String, String> criteria = new HashMap<String, String>();
279 Map<String, String> proxyCriteria = new HashMap<String, String>();
280 criteria.put("proxyPatronId", partonId);
281 List<OleProxyPatronDocument> oleProxyPatronDocuments = (List<OleProxyPatronDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleProxyPatronDocument.class,criteria);
282 if(oleProxyPatronDocuments != null && oleProxyPatronDocuments.size() >0){
283 for(int proxyPatron=0;proxyPatron<oleProxyPatronDocuments.size();proxyPatron++){
284 proxyCriteria.put("olePatronId", oleProxyPatronDocuments.get(proxyPatron).getOlePatronId());
285 List<OlePatronDocument> olePatronDocument = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,proxyCriteria);
286 if(olePatronDocument!=null)
287 realPatron.add(olePatronDocument.get(0));
288
289 }
290 return realPatron;
291 }
292 return realPatron;
293 }
294
295 public boolean isAddressVerified(String barcode) throws Exception{
296 Map<String, String> criteria = new HashMap<String, String>();
297 Map<String, String> addressCriteria = new HashMap<String, String>();
298 criteria.put("barcode", barcode);
299 List<OlePatronDocument> olePatronDocument = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,criteria);
300 if(olePatronDocument != null && olePatronDocument.size() >0){
301 if(olePatronDocument.get(0).getEntity().getEntityTypeContactInfos().get(0).getAddresses() != null && olePatronDocument.get(0).getEntity().getEntityTypeContactInfos().get(0).getAddresses().size() >0){
302 for(int address=0;address<olePatronDocument.get(0).getEntity().getEntityTypeContactInfos().get(0).getAddresses().size();address ++ ){
303 addressCriteria.put("id",olePatronDocument.get(0).getEntity().getEntityTypeContactInfos().get(0).getAddresses().get(address).getId());
304 List<OleAddressBo> oleAddressBos = (List<OleAddressBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleAddressBo.class,addressCriteria);
305 for(int preferredAddress=0;preferredAddress<oleAddressBos.size();preferredAddress++){
306 if(oleAddressBos.get(preferredAddress).isAddressVerified()){
307 return true;
308 }
309 }
310
311 }
312 }
313
314 }
315 return false;
316 }
317 }