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.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}