001package org.kuali.ole.deliver.util;
002
003import org.apache.commons.collections.CollectionUtils;
004import org.apache.commons.lang3.StringUtils;
005import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
006import org.kuali.ole.describe.bo.OleLocation;
007import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.HoldingsRecord;
008import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemRecord;
009import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemTypeRecord;
010import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.LocationsCheckinCountRecord;
011import org.kuali.rice.krad.service.BusinessObjectService;
012import org.kuali.rice.krad.service.KRADServiceLocator;
013
014import java.util.*;
015
016/**
017 * Created by pvsubrah on 6/5/15.
018 */
019public class ItemInfoUtil extends OLEUtil {
020    private static ItemInfoUtil itemInfoUtil;
021
022
023
024    public enum LEVEL_CODES {
025        INSTITUTION(1), CAMPUS(2), LIBRARY(3), COLLECTION(4), SHELVING(5);
026        private int id;
027
028        LEVEL_CODES(int id) {
029            this.id = id;
030        }
031
032        public int getId() {
033            return id;
034        }
035    };
036
037
038    private ItemInfoUtil() {
039    }
040
041    public static ItemInfoUtil getInstance() {
042        if (null == itemInfoUtil) {
043            itemInfoUtil = new ItemInfoUtil();
044        }
045        return itemInfoUtil;
046    }
047
048    public OleItemRecordForCirc getOleItemRecordForCirc(ItemRecord itemRecord) {
049        OleItemRecordForCirc oleItemRecordForCirc = new OleItemRecordForCirc();        
050        oleItemRecordForCirc.setItemRecord(itemRecord);
051
052        String location = null;
053        location = itemRecord.getLocation();
054
055        if (StringUtils.isBlank(location)) {
056            BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
057            Map<String, String> criteriaMap = new HashMap();
058            criteriaMap.put("holdingsId", itemRecord.getHoldingsId());
059            List<HoldingsRecord> holdingsRecords = (List<HoldingsRecord>) businessObjectService.findMatching(HoldingsRecord
060                            .class,
061                    criteriaMap);
062
063            HoldingsRecord holdingsRecord = holdingsRecords.get(0);
064            location = holdingsRecord.getLocation();
065        }
066        populateItemLocation(oleItemRecordForCirc,location);
067
068        OleDeliverRequestBo requestBO = getRequestBO(itemRecord.getBarCode());
069        oleItemRecordForCirc.setOleDeliverRequestBo(requestBO);
070
071        return oleItemRecordForCirc;
072    }
073
074    public void populateItemLocation(OleItemRecordForCirc oleItemRecordForCirc,String location) {
075        StringTokenizer stringTokenizer = new StringTokenizer(location, "/");
076
077        while (stringTokenizer.hasMoreTokens()) {
078            String locationCode = stringTokenizer.nextToken();
079            int levelIdForLocationCode = getLevelIdForLocationCode(locationCode);
080            if (levelIdForLocationCode == LEVEL_CODES.INSTITUTION.getId()) {
081               oleItemRecordForCirc.setInstitutionLocation(locationCode);
082            } else if (levelIdForLocationCode == LEVEL_CODES.CAMPUS.getId()) {
083                oleItemRecordForCirc.setCampusLocation(locationCode);
084            }
085            if (levelIdForLocationCode == LEVEL_CODES.LIBRARY.getId()) {
086               oleItemRecordForCirc.setItemLibraryLocation(locationCode);
087            }
088            if (levelIdForLocationCode == LEVEL_CODES.COLLECTION.getId()) {
089               oleItemRecordForCirc.setCollectionLocation(locationCode);
090            }
091            if (levelIdForLocationCode == LEVEL_CODES.SHELVING.getId()) {
092               oleItemRecordForCirc.setItemLocation(locationCode);
093            }
094        }
095    }
096
097    private int getLevelIdForLocationCode(String locationCode) {
098        HashMap<String, Object> map = new HashMap<String, Object>();
099        map.put("locationCode", locationCode);
100        List<OleLocation> locations = (List<OleLocation>) getBusinessObjectService().findMatching(OleLocation.class, map);
101        if (locations.size() > 0) {
102            String locationId = locations.get(0).getOleLocationLevel().getLevelId();
103            return Integer.valueOf(locationId);
104        }
105        return 0;
106
107    }
108
109    public OleDeliverRequestBo getRequestBO(String itemBarcode) {
110        OleDeliverRequestBo oleDeliverRequestBo = getPrioritizedRequest(itemBarcode);
111        if(null != oleDeliverRequestBo){
112            return oleDeliverRequestBo;
113        }
114        return null;
115    }
116
117    public OleDeliverRequestBo getPrioritizedRequest(String itemId) {
118        Map requestMap = new HashMap();
119        requestMap.put("itemId", itemId);
120        requestMap.put("borrowerQueuePosition", 1);
121        List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, requestMap);
122        return oleDeliverRequestBos != null && oleDeliverRequestBos.size() > 0 ? oleDeliverRequestBos.get(0) : null;
123    }
124
125    public OleDeliverRequestBo getRequestByPatronBarcode(String patronBarcode, String itemBarcode) {
126        Map requestMap = new HashMap();
127        requestMap.put("borrowerBarcode", patronBarcode);
128        requestMap.put("itemId", itemBarcode);
129        List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, requestMap);
130        return oleDeliverRequestBos != null && oleDeliverRequestBos.size() > 0 ? oleDeliverRequestBos.get(0) : null;
131    }
132}