001package org.kuali.ole.deliver.keyvalue;
002
003import com.google.common.base.Predicates;
004import com.google.common.collect.Collections2;
005import com.google.common.collect.Lists;
006import org.kuali.ole.OLEConstants;
007import org.kuali.ole.deliver.bo.OleCirculationDesk;
008import org.kuali.ole.deliver.bo.OleCirculationDeskDetail;
009import org.kuali.rice.core.api.criteria.QueryByCriteria;
010import org.kuali.rice.core.api.util.ConcreteKeyValue;
011import org.kuali.rice.core.api.util.KeyValue;
012import org.kuali.rice.kim.api.permission.PermissionService;
013import org.kuali.rice.kim.api.services.KimApiServiceLocator;
014import org.kuali.rice.kns.service.KNSServiceLocator;
015import org.kuali.rice.krad.keyvalues.KeyValuesBase;
016import org.kuali.rice.krad.service.KRADServiceLocator;
017import org.kuali.rice.krad.util.GlobalVariables;
018
019import java.util.*;
020import java.util.regex.Matcher;
021import java.util.regex.Pattern;
022
023/**
024 * Created with IntelliJ IDEA.
025 * User: ?
026 * Date: 10/19/12
027 * Time: 12:13 PM
028 * To change this template use File | Settings | File Templates.
029 */
030public class CirculationDeskChangeKeyValue extends KeyValuesBase {
031
032    public static List<KeyValue> circulationDeskKeyValues = null;
033
034    public static long timeLastRefreshed;
035
036    public static int refreshInterval = 300;     // in seconds
037
038    private PermissionService getPermissionService() {
039        PermissionService service = KimApiServiceLocator.getPermissionService();
040        return service;
041    }
042
043    @Override
044    public List<KeyValue> getKeyValues() {
045        List<KeyValue> options = new ArrayList<KeyValue>();
046        Map<String, String> userMap = new HashMap<String, String>();
047        userMap.put("operatorId", GlobalVariables.getUserSession().getPrincipalId());
048        /*  List<String> myList = new CopyOnWriteArrayList<String>()<String>();*/
049        /*Collection<OleCirculationDeskDetail> oleCirculationDeskDetails = KRADServiceLocator.KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(OleCirculationDeskDetail.class, userMap);*/
050        Collection<OleCirculationDeskDetail> oleCirculationDeskDetails = KRADServiceLocator.getDataObjectService().findMatching(OleCirculationDeskDetail.class, QueryByCriteria.Builder.andAttributes(userMap).build()).getResults();
051
052        for (OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetails) {
053            if (oleCirculationDeskDetail.isDefaultLocation() && oleCirculationDeskDetail.getOleCirculationDesk().isActive()) {
054                options.add(new ConcreteKeyValue(oleCirculationDeskDetail.getOleCirculationDesk().getCirculationDeskId(), oleCirculationDeskDetail.getOleCirculationDesk().getCirculationDeskCode()));
055            }
056        }
057        for (OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetails) {
058            if (!oleCirculationDeskDetail.isDefaultLocation() && oleCirculationDeskDetail.getOleCirculationDesk().isActive()) {
059                options.add(new ConcreteKeyValue(oleCirculationDeskDetail.getOleCirculationDesk().getCirculationDeskId(), oleCirculationDeskDetail.getOleCirculationDesk().getCirculationDeskCode()));
060            }
061        }
062        /*if (options.size() < 1) {
063            if (getPermissionService().hasPermission(GlobalVariables.getUserSession().getPrincipalId(), OLEConstants.OlePatron.PATRON_NAMESPACE, OLEConstants.CAN_OVERRIDE_LOAN) || getPermissionService().hasPermission(GlobalVariables.getUserSession().getPrincipalId(), OLEConstants.OlePatron.PATRON_NAMESPACE, OLEConstants.CAN_LOAN)) {
064                //GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_VALIDATIONS);
065            } else {
066                //GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.UNAUTHORIZED_LOAN_USER);
067            }
068        }*/
069        return options;
070    }
071
072    public static List<KeyValue> initCirculationDeskDetails() {
073
074        List<KeyValue> options = new ArrayList<KeyValue>();
075        /*Collection<OleCirculationDesk> oleCirculationDesks = KRADServiceLocator.KRADServiceLocatorWeb.getLegacyDataAdapter().findAll(OleCirculationDesk.class);*/
076        Collection<OleCirculationDesk> oleCirculationDesks = (Collection<OleCirculationDesk>) KRADServiceLocator.getDataObjectService().findMatching(OleCirculationDesk.class, QueryByCriteria.Builder.create().build()).getResults();
077        for (OleCirculationDesk oleCirculationDesk : oleCirculationDesks) {
078            if (oleCirculationDesk.isActive()) {
079                options.add(new ConcreteKeyValue(oleCirculationDesk.getCirculationDeskId(), oleCirculationDesk.getCirculationDeskCode()));
080            }
081        }
082        return options;
083    }
084
085
086    public static List<String> retrieveCirculationDeskDetailsForSuggest(String locationVal) {
087        List<KeyValue> locationKeyValues = retrieveLocationDetails();
088        List<String> locationValues = new ArrayList<String>();
089        for (KeyValue keyValue : locationKeyValues) {
090            locationValues.add(keyValue.getValue());
091        }
092
093        Pattern pattern = Pattern.compile("[?$(){}\\[\\]\\^\\\\]");
094        Matcher matcher = pattern.matcher(locationVal);
095        if (matcher.matches()) {
096            return new ArrayList<String>();
097        }
098
099        if (!locationVal.equalsIgnoreCase("*")) {
100            locationValues = Lists.newArrayList(Collections2.filter(locationValues, Predicates.contains(Pattern.compile(locationVal, Pattern.CASE_INSENSITIVE))));
101        }
102        Collections.sort(locationValues);
103        return locationValues;
104    }
105
106    private static List<KeyValue> retrieveLocationDetails() {
107        long currentTime = System.currentTimeMillis() / 1000;
108        if (circulationDeskKeyValues == null) {
109            circulationDeskKeyValues = initCirculationDeskDetails();
110            timeLastRefreshed = currentTime;
111        } else {
112            if (currentTime - timeLastRefreshed > refreshInterval) {
113                circulationDeskKeyValues = initCirculationDeskDetails();
114                timeLastRefreshed = currentTime;
115            }
116        }
117        return circulationDeskKeyValues;
118    }
119
120}