001package org.kuali.asr.service;
002
003import org.apache.log4j.Logger;
004import org.jfree.util.Log;
005import org.kuali.asr.ASRConstants;
006import org.kuali.asr.bo.*;
007import org.kuali.asr.bo.ASRItem;
008import org.kuali.asr.service.impl.OLEASRPlaceRequestHelperServiceImpl;
009import org.kuali.ole.OLEConstants;
010import org.kuali.ole.deliver.batch.OleNoticeBo;
011import org.kuali.ole.deliver.bo.*;
012import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
013import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
014import org.kuali.ole.docstore.common.document.content.instance.Item;
015import org.kuali.ole.docstore.common.document.content.instance.ItemStatus;
016import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
017import org.kuali.ole.docstore.common.search.*;
018import org.kuali.ole.docstore.model.bo.WorkItemDocument;
019import org.kuali.ole.docstore.model.enums.DocType;
020import org.kuali.ole.sys.context.SpringContext;
021import org.kuali.rice.core.api.config.property.ConfigContext;
022import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
023import org.kuali.rice.krad.service.BusinessObjectService;
024import org.kuali.rice.krad.service.KRADServiceLocator;
025
026import java.text.SimpleDateFormat;
027import java.util.*;
028
029/**
030 * Created with IntelliJ IDEA.
031 * User: sheiksalahudeenm
032 * Date: 1/8/14
033 * Time: 1:13 PM
034 * To change this template use File | Settings | File Templates.
035 */
036
037public class ASRHelperServiceImpl {
038    private static final Logger LOG = Logger.getLogger(ASRHelperServiceImpl.class);
039    private BusinessObjectService businessObjectService=getBusinessObjectService();
040    private DocstoreClientLocator docstoreClientLocator=getDocstoreClientLocator();
041    private OLEASRPlaceRequestHelperServiceImpl oleasrPlaceRequestHelperService=new OLEASRPlaceRequestHelperServiceImpl();
042    private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = getOleDeliverRequestDocumentHelperService();
043
044    public DocstoreClientLocator getDocstoreClientLocator() {
045        if (null == docstoreClientLocator) {
046            return SpringContext.getBean(DocstoreClientLocator.class);
047        }
048        return docstoreClientLocator;
049    }
050
051    public BusinessObjectService getBusinessObjectService(){
052        if(businessObjectService == null){
053            businessObjectService = KRADServiceLocator.getBusinessObjectService();
054        }
055        return businessObjectService;
056    }
057
058    public OleDeliverRequestDocumentHelperServiceImpl getOleDeliverRequestDocumentHelperService() {
059        if(oleDeliverRequestDocumentHelperService==null){
060            oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
061        }
062        return oleDeliverRequestDocumentHelperService;
063    }
064    //  private OLEASRPlaceRequestHelperServiceImpl oleasrPlaceReqestHelperService=new OLEASRPlaceRequestHelperServiceImpl();
065
066
067    public boolean isAuthorized(String principalId) {
068        /*PermissionService service = KimApiServiceLocator.getPermissionService();
069        return service.hasPermission(principalId, "OLE-PTRN", "Mapping Circulation Desk");*/
070        return oleasrPlaceRequestHelperService.processOperator(principalId);
071    }
072
073    public boolean validatePatron(String patronId){
074        Map<String, String> patronMap = new HashMap<String, String>();
075        patronMap.put("olePatronId", patronId);
076        OleNoticeBo oleNoticeBo = new OleNoticeBo();
077        List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, patronMap);
078        return (olePatronDocumentList!=null && olePatronDocumentList.size()>0);
079    }
080
081    public boolean validatePickupLocation(String pickupLocation){
082        Map<String,String> circulationDeskMap = new HashMap<String,String>();
083        circulationDeskMap.put("circulationDeskCode",pickupLocation);
084        circulationDeskMap.put("asrPickupLocation","true");
085        List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>)KRADServiceLocator.getBusinessObjectService().findMatching(OleCirculationDesk.class,circulationDeskMap);
086        return (oleCirculationDeskList!=null && oleCirculationDeskList.size()>0);
087    }
088
089    public SearchParams buildSearchParams(Map<String, String> queryParams) {
090        SearchParams searchParams = new SearchParams();
091        List<SearchCondition> searchConditionList = new ArrayList<SearchCondition>();
092        List<SearchCondition>  searchConditions = new ArrayList<>();
093        if (null != queryParams) {
094            for (Iterator<String> keys = queryParams.keySet().iterator(); keys.hasNext(); ) {
095                String key = keys.next();
096                String value = queryParams.get(key);
097                if(org.apache.commons.lang.StringUtils.isNotEmpty(value)) {
098                    //searchConditions.add(searchParams.buildSearchCondition("AND", searchParams.buildSearchField("item", key, value), "AND"));
099                    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}