View Javadoc
1   package org.kuali.asr.service;
2   
3   import org.apache.log4j.Logger;
4   import org.jfree.util.Log;
5   import org.kuali.asr.ASRConstants;
6   import org.kuali.asr.bo.*;
7   import org.kuali.asr.bo.ASRItem;
8   import org.kuali.asr.service.impl.OLEASRPlaceRequestHelperServiceImpl;
9   import org.kuali.ole.OLEConstants;
10  import org.kuali.ole.deliver.batch.OleNoticeBo;
11  import org.kuali.ole.deliver.bo.*;
12  import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
13  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
14  import org.kuali.ole.docstore.common.document.content.instance.Item;
15  import org.kuali.ole.docstore.common.document.content.instance.ItemStatus;
16  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
17  import org.kuali.ole.docstore.common.search.*;
18  import org.kuali.ole.docstore.model.bo.WorkItemDocument;
19  import org.kuali.ole.docstore.model.enums.DocType;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.rice.core.api.config.property.ConfigContext;
22  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
23  import org.kuali.rice.krad.service.BusinessObjectService;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  
26  import java.text.SimpleDateFormat;
27  import java.util.*;
28  
29  /**
30   * Created with IntelliJ IDEA.
31   * User: sheiksalahudeenm
32   * Date: 1/8/14
33   * Time: 1:13 PM
34   * To change this template use File | Settings | File Templates.
35   */
36  
37  public class ASRHelperServiceImpl {
38      private static final Logger LOG = Logger.getLogger(ASRHelperServiceImpl.class);
39      private BusinessObjectService businessObjectService=getBusinessObjectService();
40      private DocstoreClientLocator docstoreClientLocator=getDocstoreClientLocator();
41      private OLEASRPlaceRequestHelperServiceImpl oleasrPlaceRequestHelperService=new OLEASRPlaceRequestHelperServiceImpl();
42      private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = getOleDeliverRequestDocumentHelperService();
43  
44      public DocstoreClientLocator getDocstoreClientLocator() {
45          if (null == docstoreClientLocator) {
46              return SpringContext.getBean(DocstoreClientLocator.class);
47          }
48          return docstoreClientLocator;
49      }
50  
51      public BusinessObjectService getBusinessObjectService(){
52          if(businessObjectService == null){
53              businessObjectService = KRADServiceLocator.getBusinessObjectService();
54          }
55          return businessObjectService;
56      }
57  
58      public OleDeliverRequestDocumentHelperServiceImpl getOleDeliverRequestDocumentHelperService() {
59          if(oleDeliverRequestDocumentHelperService==null){
60              oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
61          }
62          return oleDeliverRequestDocumentHelperService;
63      }
64      //  private OLEASRPlaceRequestHelperServiceImpl oleasrPlaceReqestHelperService=new OLEASRPlaceRequestHelperServiceImpl();
65  
66  
67      public boolean isAuthorized(String principalId) {
68          /*PermissionService service = KimApiServiceLocator.getPermissionService();
69          return service.hasPermission(principalId, "OLE-PTRN", "Mapping Circulation Desk");*/
70          return oleasrPlaceRequestHelperService.processOperator(principalId);
71      }
72  
73      public boolean validatePatron(String patronId){
74          Map<String, String> patronMap = new HashMap<String, String>();
75          patronMap.put("olePatronId", patronId);
76          OleNoticeBo oleNoticeBo = new OleNoticeBo();
77          List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, patronMap);
78          return (olePatronDocumentList!=null && olePatronDocumentList.size()>0);
79      }
80  
81      public boolean validatePickupLocation(String pickupLocation){
82          Map<String,String> circulationDeskMap = new HashMap<String,String>();
83          circulationDeskMap.put("circulationDeskCode",pickupLocation);
84          circulationDeskMap.put("asrPickupLocation","true");
85          List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>)KRADServiceLocator.getBusinessObjectService().findMatching(OleCirculationDesk.class,circulationDeskMap);
86          return (oleCirculationDeskList!=null && oleCirculationDeskList.size()>0);
87      }
88  
89      public SearchParams buildSearchParams(Map<String, String> queryParams) {
90          SearchParams searchParams = new SearchParams();
91          List<SearchCondition> searchConditionList = new ArrayList<SearchCondition>();
92          List<SearchCondition>  searchConditions = new ArrayList<>();
93          if (null != queryParams) {
94              for (Iterator<String> keys = queryParams.keySet().iterator(); keys.hasNext(); ) {
95                  String key = keys.next();
96                  String value = queryParams.get(key);
97                  if(org.apache.commons.lang.StringUtils.isNotEmpty(value)) {
98                      //searchConditions.add(searchParams.buildSearchCondition("AND", searchParams.buildSearchField("item", key, value), "AND"));
99                      searchParams.getSearchConditions().add(searchParams.buildSearchCondition("AND", searchParams.buildSearchField("item", key, value), "AND"));
100                 }
101             }
102         }
103         return searchParams;
104     }
105 
106     public ASRResponseBo generateResponse(String code, String message, int statusCode){
107         ASRResponseBo asrResponseBo=new ASRResponseBo();
108         asrResponseBo.setCode(code);
109         asrResponseBo.setMessage(message);
110         asrResponseBo.setStatusCode(statusCode);
111         return asrResponseBo;
112     }
113 
114     public ASRItems generateASRItems(List<WorkItemDocument> workItemDocumentList){
115         ASRItems asrItems=new ASRItems();
116         List<ASRItem> asrItemList=new ArrayList<>();
117         ASRItem asrItem=null;
118         for(WorkItemDocument workItemDocument:workItemDocumentList){
119             asrItem=new ASRItem();
120             asrItem.setAuthor(workItemDocument.getItemStatus());
121             asrItem.setCallNumber(workItemDocument.getCallNumber());
122             asrItem.setItemBarcode(workItemDocument.getBarcode());
123             asrItem.setTitle(workItemDocument.getBibTitle());
124             asrItemList.add(asrItem);
125         }
126         asrItems.setAsrItems(asrItemList);
127         return asrItems;
128     }
129 
130 
131 
132 
133     public boolean updateItemStatus(Item oleItem, String itemStatus) throws Exception {
134         LOG.debug("Inside the updateItemStatus method");
135         if(oleItem==null){
136             return false;
137         }
138         try {
139             String itemUuid = oleItem.getItemIdentifier();
140             String itemXmlContent = buildItemContentWithItemStatus(oleItem, itemStatus);
141             LOG.info("itemXmlContent" + itemXmlContent);
142             String itemRecordUpdateResponse =  "";
143                 //    getDocstoreHelperService().updateInstanceRecord(itemUuid, OLEConstants.ITEM_DOC_TYPE, itemXmlContent);
144             LOG.info(itemRecordUpdateResponse);
145             return true;
146 
147         } catch (Exception e) {
148             LOG.error(OLEConstants.ITM_STS_TO_DOC_FAIL + e, e);
149             return false;
150 
151         }
152     }
153 
154     public String buildItemContentWithItemStatus(Item oleItem, String itemStatus) throws Exception {
155         LOG.debug("Inside the buildItemContentWithItemStatus method");
156         ItemStatus itemStatus1 = new ItemStatus();
157         itemStatus1.setCodeValue(itemStatus);
158         itemStatus1.setFullValue(itemStatus);
159         oleItem.setItemStatus(itemStatus1);
160         oleItem.setItemStatusEffectiveDate(String.valueOf(new SimpleDateFormat(OLEConstants.DAT_FORMAT_EFFECTIVE).format(new Date())));
161         String itemContent = new ItemOlemlRecordProcessor().toXML(oleItem);
162         return itemContent;
163     }
164 
165 
166 
167 
168     public Object cancelRequest(String operatorId,String requestId){
169         if(!isAuthorized(operatorId)){
170             return generateResponse(ASRConstants.OPERATOR_NOT_FOUND_CODE, ConfigContext.getCurrentContextConfig().getProperty(ASRConstants.OPERATOR_NOT_FOUND),ASRConstants.OPERATOR_NOT_FOUND_STATUS);
171         }
172         Map<String,String> requestMap = new HashMap<String,String>();
173         requestMap.put("requestId",requestId);
174         List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,requestMap);
175         if(oleDeliverRequestBoList!=null && oleDeliverRequestBoList.size()>0){
176             OleDeliverRequestBo oleDeliverRequestBo = oleDeliverRequestBoList.get(0);
177             oleDeliverRequestBo.setOperatorCreateId(operatorId);
178             if(!true){
179                 return generateResponse(ASRConstants.REQUEST_NOT_FOUND_CODE,ConfigContext.getCurrentContextConfig().getProperty(ASRConstants.REQUEST_NOT_FOUND),ASRConstants.REQUEST_NOT_FOUND_STATUS);
180             }
181           //  oleasrPlaceReqestHelperService.cancelDocument(oleDeliverRequestBo);
182             return generateResponse(ASRConstants.REQUEST_CANCELED_CODE,ConfigContext.getCurrentContextConfig().getProperty(ASRConstants.REQUEST_CANCELED),ASRConstants.REQUEST_CANCELED_STATUS);
183         }  else{
184             return generateResponse(ASRConstants.REQUEST_NOT_FOUND_CODE,ConfigContext.getCurrentContextConfig().getProperty(ASRConstants.REQUEST_NOT_FOUND),ASRConstants.REQUEST_NOT_FOUND_STATUS);
185         }
186     }
187 
188     public String getParameter(String name) {
189         String parameter = "";
190         try {
191             Map<String, String> criteriaMap = new HashMap<String, String>();
192             criteriaMap.put("namespaceCode", OLEConstants.DLVR_NMSPC);
193             criteriaMap.put("componentCode", OLEConstants.DLVR_CMPNT);
194             criteriaMap.put("name", name);
195             List<ParameterBo> parametersList = (List<ParameterBo>) businessObjectService.findMatching(ParameterBo.class, criteriaMap);
196             for (ParameterBo parameterBo : parametersList) {
197                 parameter = parameterBo.getValue();
198             }
199         } catch (Exception e) {
200             LOG.error(e, e);
201         }
202         return parameter;
203     }
204 
205 
206 
207 
208    public ASRItems getNewASRItems(){
209       ASRItems asrItems = new ASRItems();
210        List<ASRItem> asrItemList = new ArrayList<ASRItem>();
211        List<org.kuali.ole.deliver.bo.ASRItem> asrItemsList = (List<org.kuali.ole.deliver.bo.ASRItem>)businessObjectService.findAll(org.kuali.ole.deliver.bo.ASRItem.class);
212       ASRItem asrItem;
213        if(asrItemsList.size()>0){
214            for(org.kuali.ole.deliver.bo.ASRItem asrItemBo : asrItemsList){
215               asrItem = new ASRItem();
216                asrItem.setItemBarcode(asrItemBo.getItemBarcode());
217                asrItem.setTitle(asrItemBo.getTitle());
218                asrItem.setAuthor(asrItemBo.getAuthor());
219                asrItem.setCallNumber(asrItemBo.getCallNumber());
220                asrItemList.add(asrItem);
221            }
222        }
223        asrItems.setAsrItems(asrItemList);
224        if(asrItemList!=null && asrItemList.size()>0){
225            businessObjectService.delete(asrItemsList);
226        }
227        return asrItems;
228    }
229 
230 
231    public Map<String, String> getItemFieldValue(String searchField, String searchFieldValue, List<String> resultFields) {
232        Map<String, String> resultMap = new HashMap<>();
233        Map<String,String> searchParamsMap = new HashMap<String,String>();
234        searchParamsMap.put(searchField,searchFieldValue);
235        SearchParams searchParams = buildSearchParams(searchParamsMap);
236        for(String resultField :resultFields) {
237              searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", resultField));
238        }
239        try {
240 
241        SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
242        for(SearchResult searchResult : searchResponse.getSearchResults()){
243            for(SearchResultField searchResultField : searchResult.getSearchResultFields()) {
244                if(searchResultField.getDocType().equalsIgnoreCase(DocType.ITEM.getCode())) {
245                    for(String resultField : resultFields) {
246                        if(searchResultField.getFieldName().equalsIgnoreCase(resultField)) {
247                            resultMap.put(resultField, searchResultField.getFieldValue());
248                        }
249                    }
250                }
251            }
252        }
253        }catch(Exception e){
254            Log.error(e,e);
255        }
256        return resultMap;
257    }
258 
259     public Map<String, String> getHoldingFieldValue(String searchField, String searchFieldValue, List<String> resultFields) {
260         Map<String, String> resultMap = new HashMap<>();
261         Map<String,String> searchParamsMap = new HashMap<String,String>();
262         searchParamsMap.put(searchField,searchFieldValue);
263         SearchParams searchParams = buildSearchParamsForHoldings(searchParamsMap);
264         for(String resultField :resultFields) {
265             searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("holdings", resultField));
266         }
267         try {
268 
269             SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
270             for(SearchResult searchResult : searchResponse.getSearchResults()){
271                 for(SearchResultField searchResultField : searchResult.getSearchResultFields()) {
272                     if(searchResultField.getDocType().equalsIgnoreCase(DocType.HOLDINGS.getCode())) {
273                         for(String resultField : resultFields) {
274                             if(searchResultField.getFieldName().equalsIgnoreCase(resultField)) {
275                                 resultMap.put(resultField, searchResultField.getFieldValue());
276                             }
277                         }
278                     }
279                 }
280             }
281         }catch(Exception e){
282             Log.error(e,e);
283         }
284         return resultMap;
285     }
286 
287 
288     public org.kuali.ole.docstore.common.document.Item getItem(String itemUUID){
289         org.kuali.ole.docstore.common.document.Item item = null;
290         try {
291             item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(itemUUID);
292         } catch (Exception e) {
293             e.printStackTrace();
294         }
295         return item;
296     }
297 
298     public org.kuali.ole.docstore.common.document.Item getItemUsingBarcode(String barcode){
299         org.kuali.ole.docstore.common.document.Item item = null;
300         List<String> searchList = new ArrayList<String>();
301         searchList.add("id");
302         Map<String,String> resultMap =  getItemFieldValue("ItemBarcode_display", barcode, searchList);
303         String itemUUID = resultMap.get("id");
304         if(itemUUID!=null){
305             item = getItem(itemUUID);
306         }
307         return item;
308     }
309 
310     public boolean isAnASRItem(String itemLocation) {
311         boolean asrItem = false;
312         String[] libraryArray = getParameter("ASR_LOCATIONS").split("[';']");
313         for (String library : libraryArray) {
314             if (itemLocation != null) {
315                 for (String location : itemLocation.split("['/']")) {
316                     if (location.equals(library)) {
317                         asrItem = true;
318                         break;
319                     }
320                 }
321             }
322         }
323         return asrItem;
324 
325     }
326 
327 
328     public List<ASRTypeRequest> getASRRequest(String searchParameter,String value){
329         Map<String,String> requestMap = new HashMap<String,String>();
330         requestMap.put(searchParameter,value);
331         List<ASRTypeRequest> asrTypeRequests = (List<ASRTypeRequest>)businessObjectService.findMatching(ASRTypeRequest.class,requestMap);
332         if(asrTypeRequests.size()>0){
333             return asrTypeRequests;
334         }
335         return null;
336     }
337 
338     public boolean updateRequestStatus(ASRTypeRequest asrTypeRequest,String holdId,String requestStatus){
339         boolean updated = false;
340         Map<String,String> parameterMap = new HashMap<>();
341         parameterMap.put("requestId",holdId);
342         List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,parameterMap);
343         if(oleDeliverRequestBoList!=null && oleDeliverRequestBoList.size()>0){
344             OleDeliverRequestBo oleDeliverRequestBo = oleDeliverRequestBoList.get(0);
345             oleDeliverRequestBo.setRequestStatus(requestStatus);
346             businessObjectService.save(oleDeliverRequestBo);
347             List<ASRTypeRequest> asrTypeRequests;
348             if(asrTypeRequest == null){
349                 asrTypeRequests = getASRRequest("requestId", holdId);
350                 if(asrTypeRequests!=null && asrTypeRequests.size()>0){
351                     asrTypeRequest = asrTypeRequests.get(0);
352 
353                 } else{
354                     return false;
355                 }
356             }
357             asrTypeRequest.setRequestStatus(requestStatus);
358             ASRTypeRequest asrTypeRequest1=businessObjectService.save(asrTypeRequest);
359             if(asrTypeRequest1!=null){
360                 updated = true;
361             }
362         }
363        return updated;
364     }
365 
366     public boolean updateItemStatus(String itemBarcode,String status){
367         boolean updated = false;
368         Item items = null;
369         org.kuali.ole.docstore.common.document.Item item = getItemUsingBarcode(itemBarcode);
370         if(item!=null){
371         String itemContent = item.getContent();
372          if(itemContent!=null){
373         ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
374              items=itemOlemlRecordProcessor.fromXML(itemContent);
375         if(items!=null && items.getItemStatus()!=null){
376             ItemStatus itemStatus = new ItemStatus();
377             itemStatus.setCodeValue(status);
378             items.setItemStatus(itemStatus);
379             item.setContent(itemOlemlRecordProcessor.toXML(items));
380             try{
381            getDocstoreClientLocator().getDocstoreClient().updateItem(item);
382                 updated=true;
383             } catch(Exception e){
384                 Log.error(e,e);
385             }
386         }
387          }
388         }
389 
390       return updated;
391     }
392 
393     public boolean updateItemStatusForInTransit(String itemBarcode,String status){
394         boolean updated = false;
395         Item items = null;
396         org.kuali.ole.docstore.common.document.Item item = getItemUsingBarcode(itemBarcode);
397         if(item!=null){
398             String itemContent = item.getContent();
399             if(itemContent!=null){
400                 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
401                 items=itemOlemlRecordProcessor.fromXML(itemContent);
402                 if(items!=null && items.getItemStatus()!=null && items.getItemStatus().getCodeValue().equals(ASRConstants.IN_TRANSIT)){
403                     ItemStatus itemStatus = new ItemStatus();
404                     itemStatus.setCodeValue(status);
405                     items.setItemStatus(itemStatus);
406                     item.setContent(itemOlemlRecordProcessor.toXML(items));
407                     try{
408                         getDocstoreClientLocator().getDocstoreClient().updateItem(item);
409                         updated=true;
410                     } catch(Exception e){
411                         Log.error(e,e);
412                     }
413                 }
414             }
415         }
416 
417         return updated;
418     }
419 
420     public ASRRequests getASRRequest(List<ASRTypeRequest> asrRequestTypeList){
421         ASRRequests asrRequests=new ASRRequests();
422         List<ASRRequest> asrRequestList=new ArrayList<>();
423         ASRRequest asrRequest=null;
424         for(ASRTypeRequest asrTypeRequest:asrRequestTypeList){
425             asrRequest=new ASRRequest();
426             asrRequest.setHoldId(asrTypeRequest.getRequestId());
427             asrRequest.setItemBarcode(asrTypeRequest.getItemId());
428             asrRequestList.add(asrRequest);
429         }
430         asrRequests.setAsrRequests(asrRequestList);
431 
432         return asrRequests;
433     }
434 
435 
436     public ASRRequests getASRTypeRequest(String status){
437         ASRRequests asrRequests = new ASRRequests();
438         Map<String,String> requestMap = new HashMap<String,String>();
439             requestMap.put("requestStatus",status);
440          List<ASRTypeRequest> requestsList = (List<ASRTypeRequest>)businessObjectService.findMatching(ASRTypeRequest.class,requestMap);
441         if(requestsList.size()>0){
442                      asrRequests = getASRRequest(requestsList);
443         }
444         return asrRequests;
445 
446     }
447 
448 
449     public void deleteASRRequest(String itemBarcode){
450         OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
451         Map<String,String> requestMap = new HashMap<String,String>();
452         requestMap.put("itemId",itemBarcode);
453         List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,requestMap);
454         if(oleDeliverRequestBos.size()>0){
455             for(OleDeliverRequestBo oleDeliverRequestBo : oleDeliverRequestBos){
456                 oleDeliverRequestDocumentHelperService.cancelDocument(oleDeliverRequestBo);
457             }
458         }
459         List<ASRTypeRequest> asrTypeRequests = (List<ASRTypeRequest>)businessObjectService.findMatching(ASRTypeRequest.class,requestMap);
460         businessObjectService.delete(asrTypeRequests);
461 
462     }
463 
464 
465   public ASRRequestDetailBo generateASRDetailBo(OleDeliverRequestBo oleDeliverRequestBo){
466       ASRRequestDetailBo asrRequestDetailBo = new ASRRequestDetailBo();
467       String[] availableDates;
468       asrRequestDetailBo.setRequestId(oleDeliverRequestBo.getRequestId());
469       asrRequestDetailBo.setAvailable(false);
470       Map<String, String> loanMap = new HashMap<String, String>();
471       loanMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, oleDeliverRequestBo.getItemId());
472       List<OleLoanDocument> oleLoanDocumentList = (List<OleLoanDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class, loanMap);
473       if (oleLoanDocumentList.size() > 0) {
474           if (oleLoanDocumentList.get(0).getLoanDueDate() != null) {
475               availableDates = oleLoanDocumentList.get(0).getLoanDueDate().toString().split(" ");
476               if(availableDates!=null && availableDates.length>0){
477                   asrRequestDetailBo.setDateAvailable(availableDates[0]);
478               } else{
479                   asrRequestDetailBo.setDateAvailable(oleLoanDocumentList.get(0).getLoanDueDate().toString());
480               }
481               if(oleDeliverRequestBo.getPickUpLocationId()!=null){
482                   Map<String, String> deskMap = new HashMap<>();
483                   deskMap.put(OLEConstants.CIRCULATION_DESK_ID, oleDeliverRequestBo.getPickUpLocationId());
484                   List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>) KRADServiceLocator.getBusinessObjectService().findMatching(OleCirculationDesk.class, deskMap);
485                   if(oleCirculationDeskList.size()>0){
486 
487                       asrRequestDetailBo.setDateAvailableExpires(oleDeliverRequestDocumentHelperService.addDate(new java.sql.Date(oleLoanDocumentList.get(0).getLoanDueDate().getTime()), Integer.parseInt(oleCirculationDeskList.get(0).getOnHoldDays())).toString());
488                   }
489               }
490           }  else{
491               asrRequestDetailBo.setDateAvailable(OLEConstants.INDEFINITE);
492               asrRequestDetailBo.setDateAvailableExpires(OLEConstants.INDEFINITE);
493           }
494       }
495       asrRequestDetailBo.setDateExpires(oleDeliverRequestBo.getRequestExpiryDate().toString());
496       if(oleDeliverRequestBo.getRecallDueDate()!=null)
497           asrRequestDetailBo.setDateRecalled(oleDeliverRequestBo.getRecallDueDate().toString());
498       asrRequestDetailBo.setPatronId(oleDeliverRequestBo.getBorrowerBarcode());
499       asrRequestDetailBo.setPickupLocation(oleDeliverRequestBo.getPickUpLocationCode());
500       //asrRequestDetailBo.setRecallStatus();
501       if(oleDeliverRequestBo.getBorrowerQueuePosition()!=null)
502           asrRequestDetailBo.setPriority(oleDeliverRequestBo.getBorrowerQueuePosition().toString());
503       asrRequestDetailBo.setRequestType(oleDeliverRequestBo.getRequestTypeCode());
504       asrRequestDetailBo.setRequestStatus(oleDeliverRequestBo.getRequestStatus());
505       return asrRequestDetailBo;
506   }
507 
508 
509    public ASRRequestDetailsBo generateASRequestDetailsBo(List<OleDeliverRequestBo> oleDeliverRequestBos){
510        ASRRequestDetailsBo asrRequestDetailsBo = new ASRRequestDetailsBo();
511        List<ASRRequestDetailBo> asrRequestDetailBoList;
512        if(oleDeliverRequestBos.size()>0){
513              asrRequestDetailBoList = new ArrayList<ASRRequestDetailBo>();
514         for(OleDeliverRequestBo oleDeliverRequestBo : oleDeliverRequestBos){
515             asrRequestDetailBoList.add(generateASRDetailBo(oleDeliverRequestBo));
516         }
517         asrRequestDetailsBo.setAsrRequestDetailBos(asrRequestDetailBoList);
518            return asrRequestDetailsBo;
519        }
520        return null;
521    }
522 
523     public ASRRequestDetailsBo getRequests(String itemBarcode){
524         List<OleDeliverRequestBo> oleDeliverRequestBos =null;
525         Map<String,String> requestMap = new HashMap<String,String>();
526         requestMap.put("itemId",itemBarcode);
527         List<ASRTypeRequest> asrTypeRequests =  (List<ASRTypeRequest>)businessObjectService.findMatching(ASRTypeRequest.class,requestMap);
528         if(asrTypeRequests.size()>0){
529             oleDeliverRequestBos = new ArrayList<OleDeliverRequestBo>();
530                for(ASRTypeRequest asrTypeRequest : asrTypeRequests){
531                    requestMap.put("itemId",asrTypeRequest.getItemId());
532                       List<OleDeliverRequestBo> oleDeliverRequestBos1 = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,requestMap);
533                          if(oleDeliverRequestBos1.size()>0){
534                                    oleDeliverRequestBos.addAll(oleDeliverRequestBos1);
535                              break;
536                          }
537                }
538         }
539         if(oleDeliverRequestBos!=null && oleDeliverRequestBos.size()>0){
540             ASRRequestDetailsBo asrRequestDetailsBo = generateASRequestDetailsBo(oleDeliverRequestBos);
541             if(asrRequestDetailsBo !=null){
542                 return asrRequestDetailsBo;
543             }
544         }
545         return null;
546     }
547 
548     public OleDeliverRequestBo getPrioritizedRequest(String itemBarcode){
549         OleDeliverRequestBo oleDeliverRequestBo = null;
550         Map<String,String> requestMap = new HashMap<String,String>();
551         requestMap.put("itemId",itemBarcode);
552         requestMap.put("borrowerQueuePosition","1");
553         List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,requestMap);
554         if(oleDeliverRequestBos.size()>0){
555                oleDeliverRequestBo = oleDeliverRequestBos.get(0);
556         }
557         return oleDeliverRequestBo;
558     }
559 
560 
561     public boolean validateLocation(String operatorId,String itemLocation){
562         boolean valid=false;
563         Map<String,String> circulationDeskDetailMap = new HashMap<String,String>();
564         Map<String,String> circulationLocationMap = new HashMap<String,String>();
565         circulationDeskDetailMap.put("operatorId",operatorId);
566         circulationDeskDetailMap.put("defaultLocation","true");
567         List<OleCirculationDeskDetail> oleCirculationDeskDetails = (List<OleCirculationDeskDetail>)businessObjectService.findMatching(OleCirculationDeskDetail.class,circulationDeskDetailMap);
568         if(oleCirculationDeskDetails.size()>0){
569             circulationLocationMap.put("circulationDeskId",oleCirculationDeskDetails.get(0).getCirculationDeskId());
570         List<OleCirculationDeskLocation> oleCirculationDeskLocations = (List<OleCirculationDeskLocation>)businessObjectService.findMatching(OleCirculationDeskLocation.class,circulationLocationMap);
571         if(oleCirculationDeskLocations.size()>0){
572                    for(OleCirculationDeskLocation oleCirculationDeskLocation : oleCirculationDeskLocations){
573                        //for(String location:oleCirculationDeskLocation.getLocation().getFullLocationPath().split("['/']")){
574                        if(oleCirculationDeskLocation.getCirculationPickUpDeskLocation()==null || (oleCirculationDeskLocation.getCirculationPickUpDeskLocation()!=null && oleCirculationDeskLocation.getCirculationPickUpDeskLocation().trim().isEmpty())){
575                            if(oleCirculationDeskLocation.getLocation().getFullLocationPath().equals(itemLocation)){
576                                valid = true;
577                                break;
578                            }
579                        }
580                        //}
581                        /*if(oleCirculationDeskLocation.getLocation().getFullLocationPath().contains(itemLocation)){
582                             valid = true;
583                        }*/
584                    }
585         }
586       }
587        return valid;
588     }
589 
590 
591 
592     public boolean validPatron(String patronId){
593         boolean valid=false;
594         Map<String,String> patronMap = new HashMap<String,String>();
595         patronMap.put("olePatronId",patronId);
596         List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>)businessObjectService.findMatching(OlePatronDocument.class,patronMap);
597         if(olePatronDocuments!=null && olePatronDocuments.size()>0){
598             valid=true;
599         }
600         return valid;
601     }
602 
603 
604 
605    public boolean saveASRRequest(ASRRequestBo asrRequestBo){
606        boolean saved = false;
607        ASRTypeRequest asrTypeRequest = new ASRTypeRequest();
608        asrTypeRequest.setItemId(asrRequestBo.getItemBarcode());
609        asrTypeRequest.setPatronId(asrRequestBo.getPatronBarcode());
610        asrTypeRequest.setPickUpLocation(asrRequestBo.getPickupLocation());
611        asrTypeRequest.setPatronName(asrRequestBo.getPatronName());
612        ASRTypeRequest asrTypeRequest1 = businessObjectService.save(asrTypeRequest);
613        if(asrTypeRequest1!=null){
614            saved =true;
615        }
616        return saved;
617    }
618 
619 
620     public boolean saveASRItem(ASRItem asrItem){
621         boolean saved = false;
622         org.kuali.ole.deliver.bo.ASRItem asrItems = new org.kuali.ole.deliver.bo.ASRItem();
623         asrItems.setItemBarcode(asrItem.getItemBarcode());
624         asrItems.setCallNumber(asrItem.getCallNumber());
625         asrItems.setAuthor(asrItem.getAuthor());
626         asrItems.setTitle(asrItem.getTitle());
627 
628         org.kuali.ole.deliver.bo.ASRItem asrItem1 =   businessObjectService.save(asrItems);
629         if(asrItem1!=null){
630             saved = true;
631         }
632         return saved;
633     }
634 
635     public List<OleDeliverRequestBo> getDeliverRequest(Map<String,String> requestMap){
636         List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,requestMap);
637         if(oleDeliverRequestBos.size()>0){
638             return oleDeliverRequestBos;
639         }
640         return null;
641     }
642 
643 
644  public void deleteASRTypeRequest(String requestId){
645     Map<String,String> asrTypeMap = new HashMap<String,String>();
646     asrTypeMap.put("requestId",requestId);
647     businessObjectService.deleteMatching(ASRTypeRequest.class,asrTypeMap);
648  }
649 
650     public boolean isCirculationDesksLocation(String itemLocation,String circulationDeskId){
651         boolean valid=false;
652         Map<String,String> circulationLocationMap = new HashMap<String,String>();
653         circulationLocationMap.put("circulationDeskId",circulationDeskId);
654         List<OleCirculationDeskLocation> oleCirculationDeskLocations = (List<OleCirculationDeskLocation>)businessObjectService.findMatching(OleCirculationDeskLocation.class,circulationLocationMap);
655         if(oleCirculationDeskLocations.size()>0){
656             for(OleCirculationDeskLocation oleCirculationDeskLocation : oleCirculationDeskLocations){
657                 //for(String location:oleCirculationDeskLocation.getLocation().getFullLocationPath().split("['/']")){
658               if(oleCirculationDeskLocation.getCirculationPickUpDeskLocation()==null || (oleCirculationDeskLocation.getCirculationPickUpDeskLocation()!=null && oleCirculationDeskLocation.getCirculationPickUpDeskLocation().trim().isEmpty())){
659                 if(oleCirculationDeskLocation.getLocation().getFullLocationPath().equals(itemLocation)){
660                     valid = true;
661                     break;
662                 }
663               }
664                 //}
665                        /*if(oleCirculationDeskLocation.getLocation().getFullLocationPath().contains(itemLocation)){
666                             valid = true;
667                        }*/
668             }
669         }
670         return valid;
671     }
672 
673     public String getLocation(String itemBarcode){
674         List<String> resultList = new ArrayList<String>();
675         resultList.add(ASRConstants.LOCATION_LEVEL_DISPLAY);
676         resultList.add("holdingsIdentifier");
677         Map<String,String> resultMap = getItemFieldValue(ASRConstants.ITEM_BARCODE_DISPLAY,itemBarcode,resultList);
678         String location = resultMap.get(ASRConstants.LOCATION_LEVEL_DISPLAY);
679         if(location == null){
680             String holdingsId = resultMap.get("holdingsIdentifier");
681             resultList.clear();
682             resultList.add(ASRConstants.LOCATION_LEVEL_DISPLAY);
683             Map<String,String> resultMap1 = getHoldingFieldValue("id",holdingsId, resultList);
684             location = resultMap1.get(ASRConstants.LOCATION_LEVEL_DISPLAY);
685         }
686         return location;
687 
688     }
689 
690     public SearchParams buildSearchParamsForHoldings(Map<String, String> queryParams) {
691         SearchParams searchParams = new SearchParams();
692         List<SearchCondition> searchConditionList = new ArrayList<SearchCondition>();
693         List<SearchCondition>  searchConditions = new ArrayList<>();
694         if (null != queryParams) {
695             for (Iterator<String> keys = queryParams.keySet().iterator(); keys.hasNext(); ) {
696                 String key = keys.next();
697                 String value = queryParams.get(key);
698                 if(org.apache.commons.lang.StringUtils.isNotEmpty(value)) {
699                     //searchConditions.add(searchParams.buildSearchCondition("AND", searchParams.buildSearchField("item", key, value), "AND"));
700                     searchParams.getSearchConditions().add(searchParams.buildSearchCondition("AND", searchParams.buildSearchField("holdings", key, value), "AND"));
701                 }
702             }
703         }
704         return searchParams;
705     }
706 
707     public ASRRequests getASRRequestBasedOnLocation(ASRRequests asrRequests,String location){
708         List<ASRRequest> requestList = new ArrayList<ASRRequest>();
709         if(asrRequests.getAsrRequests()!=null){
710             for(ASRRequest asrRequest:asrRequests.getAsrRequests()){
711                 String itemBarcode = asrRequest.getItemBarcode();
712                 String itemLocation = getLocation(itemBarcode);
713                 if(isLocationMatch(itemLocation,location)){
714                     requestList.add(asrRequest);
715                 }
716             }
717         }
718         requestList = processASRRequestInformation(requestList);
719         asrRequests.setAsrRequests(requestList);
720         return asrRequests;
721 
722 
723     }
724 
725     public boolean isLocationMatch(String itemLocation,String asrLocation) {
726         boolean asrItem = false;
727         if(itemLocation!=null){
728             String[] libraryArray = itemLocation.split("['/']");
729             for (String library : libraryArray) {
730                 if (asrLocation.equals(library)) {
731                     asrItem = true;
732                 }
733             }
734         }
735 
736 
737         return asrItem;
738 
739     }
740 
741     public boolean deleteAllASRItems(ASRItems asrItems){
742         boolean isAllDeleted = false;
743         List<org.kuali.ole.deliver.bo.ASRItem> deleteList = new ArrayList<>();
744         List<org.kuali.ole.deliver.bo.ASRItem> asrItemList = new ArrayList<>();
745         asrItemList=(List<org.kuali.ole.deliver.bo.ASRItem>)getBusinessObjectService().findAll(org.kuali.ole.deliver.bo.ASRItem.class);
746         for(ASRItem asrItem:asrItems.getAsrItems()){
747             for(org.kuali.ole.deliver.bo.ASRItem dlvrASRItem:asrItemList){
748                 if(dlvrASRItem.getItemBarcode() !=null && dlvrASRItem.getItemBarcode().equals(asrItem.getItemBarcode())){
749                     deleteList.add(dlvrASRItem);
750                     break;
751                 }
752             }
753         }
754         getBusinessObjectService().delete(deleteList);
755         isAllDeleted=true;
756         return isAllDeleted;
757     }
758 
759 
760     public List<ASRRequest> processASRRequestInformation(List<ASRRequest> asrRequests){
761       if(asrRequests!=null && asrRequests.size()>0){
762              for(ASRRequest asrRequest : asrRequests){
763                  Map<String,String> requestMap = new HashMap<String,String>();
764                  requestMap.put("requestId",asrRequest.getHoldId());
765                  List<OleDeliverRequestBo> deliverRequestBos = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,requestMap);
766                  if(deliverRequestBos.size()>0){
767                      OleDeliverRequestBo oleDeliverRequestBo = deliverRequestBos.get(0);
768                      oleDeliverRequestBo.setRequestCreator(OLEConstants.OleDeliverRequest.REQUESTER_PATRON);
769                      asrRequest.setPickupLocation(oleDeliverRequestBo.getPickUpLocationCode());
770                      asrRequest.setRequestDate(oleDeliverRequestBo.getCreateDate().toString());
771                      asrRequest.setPatronBarcode(oleDeliverRequestBo.getBorrowerBarcode());
772                      oleDeliverRequestBo = oleDeliverRequestDocumentHelperService.processItem(oleDeliverRequestBo);
773                      oleDeliverRequestBo = oleDeliverRequestDocumentHelperService.processPatron(oleDeliverRequestBo);
774                      asrRequest.setAuthor(oleDeliverRequestBo.getAuthor());
775                      asrRequest.setTitle(oleDeliverRequestBo.getTitle());
776                      asrRequest.setCallNumber(oleDeliverRequestBo.getCallNumber());
777                      asrRequest.setPatronName(oleDeliverRequestBo.getBorrowerName());
778                      asrRequest.setRequestStatus(oleDeliverRequestBo.getRequestStatus());
779                  }
780              }
781       }
782         return asrRequests;
783     }
784 
785 
786 }