001package org.kuali.ole.batch.keyvalue;
002
003import com.google.common.base.Predicates;
004import com.google.common.collect.Collections2;
005import com.google.common.collect.Lists;
006import org.apache.log4j.Logger;
007import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
008import org.kuali.ole.sys.context.SpringContext;
009import org.kuali.ole.util.DocstoreUtil;
010import org.kuali.rice.core.api.util.ConcreteKeyValue;
011import org.kuali.rice.core.api.util.KeyValue;
012import org.kuali.rice.krad.keyvalues.KeyValuesBase;
013import org.kuali.rice.krad.service.BusinessObjectService;
014import org.kuali.rice.krad.service.KRADServiceLocator;
015
016import java.util.*;
017import java.util.regex.Matcher;
018import java.util.regex.Pattern;
019
020/**
021 * Created by hemalathas on 12/31/14.
022 */
023public class OLEBatchProcessBatchDeleteProfileValueFinder extends KeyValuesBase {
024
025    private static final Logger LOG = Logger.getLogger(OLEBatchProcessBatchDeleteProfileValueFinder.class);
026
027    public static List<KeyValue> locationKeyValues = null;
028
029    public static long timeLastRefreshed;
030
031
032    public static int refreshInterval = 300;     // in seconds
033
034    /**
035     * This method returns the List of ConcreteKeyValue,
036     * ConcreteKeyValue has two arguments LevelCode and
037     * LocationName.
038     *
039     * @return List<KeyValue>
040     */
041
042    private DocstoreUtil docstoreUtil;
043    public DocstoreUtil getDocstoreUtil() {
044
045        if (docstoreUtil == null) {
046            docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
047
048        }
049        return docstoreUtil;
050    }
051    @Override
052    public List<KeyValue> getKeyValues() {
053
054        List<KeyValue> options = initProfileDetailsForBatchDelete();
055        return options;
056    }
057
058    public static List<KeyValue> initProfileDetailsForBatchDelete() {
059
060        List<KeyValue> keyValues = new ArrayList<KeyValue>();
061        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
062        Map parentCriteria1 = new HashMap();
063        parentCriteria1.put("batchProcessProfileType", "Batch Delete");
064        List<OLEBatchProcessProfileBo> oleBatchProcessProfileBo = (List<OLEBatchProcessProfileBo>) businessObjectService.findMatching(OLEBatchProcessProfileBo.class, parentCriteria1);
065        keyValues.add(new ConcreteKeyValue("", ""));
066        for (OLEBatchProcessProfileBo profileBo : oleBatchProcessProfileBo) {
067            keyValues.add(new ConcreteKeyValue(profileBo.getBatchProcessProfileName(), profileBo.getBatchProcessProfileId()));
068        }
069        return keyValues;
070    }
071
072    public String getProfileName(String profileName){
073        return profileName;
074    }
075
076
077    public static List<String> retrieveBatchProfileNamesForBatchDelete(String locationVal) {
078        List<KeyValue> locationKeyValues = retrieveProfileDetailsForBatchDelete();
079        List<String> locationValues = new ArrayList<String>();
080        for (KeyValue keyValue : locationKeyValues) {
081            locationValues.add(keyValue.getKey());
082        }
083        Pattern pattern = Pattern.compile("[?$(){}\\[\\]\\^\\\\]");
084        Matcher matcher = pattern.matcher(locationVal);
085        if (matcher.matches()) {
086            return new ArrayList<String>();
087        }
088
089        if (!locationVal.equalsIgnoreCase("*")) {
090            locationValues = Lists.newArrayList(Collections2.filter(locationValues, Predicates.contains(Pattern.compile(locationVal, Pattern.CASE_INSENSITIVE))));
091        }
092        Collections.sort(locationValues);
093        return locationValues;
094    }
095
096
097
098    private static List<KeyValue> retrieveProfileDetailsForBatchDelete() {
099        long currentTime = System.currentTimeMillis() / 1000;
100        if (locationKeyValues == null) {
101            locationKeyValues = initProfileDetailsForBatchDelete();
102            timeLastRefreshed = currentTime;
103        } else {
104            if (currentTime - timeLastRefreshed > refreshInterval) {
105                locationKeyValues = initProfileDetailsForBatchDelete();
106                timeLastRefreshed = currentTime;
107            }
108        }
109        return locationKeyValues;
110    }
111
112    public  static String getValue(String batchVal){
113        String value="";
114        locationKeyValues=retrieveProfileDetailsForBatchDelete();
115        for (KeyValue keyValue : locationKeyValues) {
116            if(batchVal.equalsIgnoreCase(keyValue.getKey())){
117                value =keyValue.getValue();
118            }
119        }
120        return value;
121    }
122
123
124}