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.getLocation().getFullLocationPath().equals(itemLocation)){
575                                valid = true;
576                                break;
577                            }
578                        //}
579                        /*if(oleCirculationDeskLocation.getLocation().getFullLocationPath().contains(itemLocation)){
580                             valid = true;
581                        }*/
582                    }
583         }
584       }
585        return valid;
586     }
587 
588 
589 
590     public boolean validPatron(String patronId){
591         boolean valid=false;
592         Map<String,String> patronMap = new HashMap<String,String>();
593         patronMap.put("olePatronId",patronId);
594         List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>)businessObjectService.findMatching(OlePatronDocument.class,patronMap);
595         if(olePatronDocuments!=null && olePatronDocuments.size()>0){
596             valid=true;
597         }
598         return valid;
599     }
600 
601 
602 
603    public boolean saveASRRequest(ASRRequestBo asrRequestBo){
604        boolean saved = false;
605        ASRTypeRequest asrTypeRequest = new ASRTypeRequest();
606        asrTypeRequest.setItemId(asrRequestBo.getItemBarcode());
607        asrTypeRequest.setPatronId(asrRequestBo.getPatronBarcode());
608        asrTypeRequest.setPickUpLocation(asrRequestBo.getPickupLocation());
609        asrTypeRequest.setPatronName(asrRequestBo.getPatronName());
610        ASRTypeRequest asrTypeRequest1 = businessObjectService.save(asrTypeRequest);
611        if(asrTypeRequest1!=null){
612            saved =true;
613        }
614        return saved;
615    }
616 
617 
618     public boolean saveASRItem(ASRItem asrItem){
619         boolean saved = false;
620         org.kuali.ole.deliver.bo.ASRItem asrItems = new org.kuali.ole.deliver.bo.ASRItem();
621         asrItems.setItemBarcode(asrItem.getItemBarcode());
622         asrItems.setCallNumber(asrItem.getCallNumber());
623         asrItems.setAuthor(asrItem.getAuthor());
624         asrItems.setTitle(asrItem.getTitle());
625 
626         org.kuali.ole.deliver.bo.ASRItem asrItem1 =   businessObjectService.save(asrItems);
627         if(asrItem1!=null){
628             saved = true;
629         }
630         return saved;
631     }
632 
633     public List<OleDeliverRequestBo> getDeliverRequest(Map<String,String> requestMap){
634         List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,requestMap);
635         if(oleDeliverRequestBos.size()>0){
636             return oleDeliverRequestBos;
637         }
638         return null;
639     }
640 
641 
642  public void deleteASRTypeRequest(String requestId){
643     Map<String,String> asrTypeMap = new HashMap<String,String>();
644     asrTypeMap.put("requestId",requestId);
645     businessObjectService.deleteMatching(ASRTypeRequest.class,asrTypeMap);
646  }
647 
648     public boolean isCirculationDesksLocation(String itemLocation,String circulationDeskId){
649         boolean valid=false;
650         Map<String,String> circulationLocationMap = new HashMap<String,String>();
651         circulationLocationMap.put("circulationDeskId",circulationDeskId);
652         List<OleCirculationDeskLocation> oleCirculationDeskLocations = (List<OleCirculationDeskLocation>)businessObjectService.findMatching(OleCirculationDeskLocation.class,circulationLocationMap);
653         if(oleCirculationDeskLocations.size()>0){
654             for(OleCirculationDeskLocation oleCirculationDeskLocation : oleCirculationDeskLocations){
655                 //for(String location:oleCirculationDeskLocation.getLocation().getFullLocationPath().split("['/']")){
656                 if(oleCirculationDeskLocation.getLocation().getFullLocationPath().equals(itemLocation)){
657                     valid = true;
658                     break;
659                 }
660                 //}
661                        /*if(oleCirculationDeskLocation.getLocation().getFullLocationPath().contains(itemLocation)){
662                             valid = true;
663                        }*/
664             }
665         }
666         return valid;
667     }
668 
669     public String getLocation(String itemBarcode){
670         List<String> resultList = new ArrayList<String>();
671         resultList.add(ASRConstants.LOCATION_LEVEL_DISPLAY);
672         resultList.add("holdingsIdentifier");
673         Map<String,String> resultMap = getItemFieldValue(ASRConstants.ITEM_BARCODE_DISPLAY,itemBarcode,resultList);
674         String location = resultMap.get(ASRConstants.LOCATION_LEVEL_DISPLAY);
675         if(location == null){
676             String holdingsId = resultMap.get("holdingsIdentifier");
677             resultList.clear();
678             resultList.add(ASRConstants.LOCATION_LEVEL_DISPLAY);
679             Map<String,String> resultMap1 = getHoldingFieldValue("id",holdingsId, resultList);
680             location = resultMap1.get(ASRConstants.LOCATION_LEVEL_DISPLAY);
681         }
682         return location;
683 
684     }
685 
686     public SearchParams buildSearchParamsForHoldings(Map<String, String> queryParams) {
687         SearchParams searchParams = new SearchParams();
688         List<SearchCondition> searchConditionList = new ArrayList<SearchCondition>();
689         List<SearchCondition>  searchConditions = new ArrayList<>();
690         if (null != queryParams) {
691             for (Iterator<String> keys = queryParams.keySet().iterator(); keys.hasNext(); ) {
692                 String key = keys.next();
693                 String value = queryParams.get(key);
694                 if(org.apache.commons.lang.StringUtils.isNotEmpty(value)) {
695                     //searchConditions.add(searchParams.buildSearchCondition("AND", searchParams.buildSearchField("item", key, value), "AND"));
696                     searchParams.getSearchConditions().add(searchParams.buildSearchCondition("AND", searchParams.buildSearchField("holdings", key, value), "AND"));
697                 }
698             }
699         }
700         return searchParams;
701     }
702 
703     public ASRRequests getASRRequestBasedOnLocation(ASRRequests asrRequests,String location){
704         List<ASRRequest> requestList = new ArrayList<ASRRequest>();
705         if(asrRequests.getAsrRequests()!=null){
706             for(ASRRequest asrRequest:asrRequests.getAsrRequests()){
707                 String itemBarcode = asrRequest.getItemBarcode();
708                 String itemLocation = getLocation(itemBarcode);
709                 if(isLocationMatch(itemLocation,location)){
710                     requestList.add(asrRequest);
711                 }
712             }
713         }
714         requestList = processASRRequestInformation(requestList);
715         asrRequests.setAsrRequests(requestList);
716         return asrRequests;
717 
718 
719     }
720 
721     public boolean isLocationMatch(String itemLocation,String asrLocation) {
722         boolean asrItem = false;
723         if(itemLocation!=null){
724             String[] libraryArray = itemLocation.split("['/']");
725             for (String library : libraryArray) {
726                 if (asrLocation.equals(library)) {
727                     asrItem = true;
728                 }
729             }
730         }
731 
732 
733         return asrItem;
734 
735     }
736 
737     public boolean deleteAllASRItems(ASRItems asrItems){
738         boolean isAllDeleted = false;
739         List<org.kuali.ole.deliver.bo.ASRItem> deleteList = new ArrayList<>();
740         List<org.kuali.ole.deliver.bo.ASRItem> asrItemList = new ArrayList<>();
741         asrItemList=(List<org.kuali.ole.deliver.bo.ASRItem>)getBusinessObjectService().findAll(org.kuali.ole.deliver.bo.ASRItem.class);
742         for(ASRItem asrItem:asrItems.getAsrItems()){
743             for(org.kuali.ole.deliver.bo.ASRItem dlvrASRItem:asrItemList){
744                 if(dlvrASRItem.getItemBarcode() !=null && dlvrASRItem.getItemBarcode().equals(asrItem.getItemBarcode())){
745                     deleteList.add(dlvrASRItem);
746                     break;
747                 }
748             }
749         }
750         getBusinessObjectService().delete(deleteList);
751         isAllDeleted=true;
752         return isAllDeleted;
753     }
754 
755 
756     public List<ASRRequest> processASRRequestInformation(List<ASRRequest> asrRequests){
757       if(asrRequests!=null && asrRequests.size()>0){
758              for(ASRRequest asrRequest : asrRequests){
759                  Map<String,String> requestMap = new HashMap<String,String>();
760                  requestMap.put("requestId",asrRequest.getHoldId());
761                  List<OleDeliverRequestBo> deliverRequestBos = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,requestMap);
762                  if(deliverRequestBos.size()>0){
763                      OleDeliverRequestBo oleDeliverRequestBo = deliverRequestBos.get(0);
764                      oleDeliverRequestBo.setRequestCreator(OLEConstants.OleDeliverRequest.REQUESTER_PATRON);
765                      asrRequest.setPickupLocation(oleDeliverRequestBo.getPickUpLocationCode());
766                      asrRequest.setRequestDate(oleDeliverRequestBo.getCreateDate().toString());
767                      asrRequest.setPatronBarcode(oleDeliverRequestBo.getBorrowerBarcode());
768                      oleDeliverRequestBo = oleDeliverRequestDocumentHelperService.processItem(oleDeliverRequestBo);
769                      oleDeliverRequestBo = oleDeliverRequestDocumentHelperService.processPatron(oleDeliverRequestBo);
770                      asrRequest.setAuthor(oleDeliverRequestBo.getAuthor());
771                      asrRequest.setTitle(oleDeliverRequestBo.getTitle());
772                      asrRequest.setCallNumber(oleDeliverRequestBo.getCallNumber());
773                      asrRequest.setPatronName(oleDeliverRequestBo.getBorrowerName());
774                      asrRequest.setRequestStatus(oleDeliverRequestBo.getRequestStatus());
775                  }
776              }
777       }
778         return asrRequests;
779     }
780 
781 
782 }