1 package org.kuali.ole.batch.keyvalue;
2
3 import com.google.common.base.Predicates;
4 import com.google.common.collect.Collections2;
5 import com.google.common.collect.Lists;
6 import org.apache.log4j.Logger;
7 import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
8 import org.kuali.ole.sys.context.SpringContext;
9 import org.kuali.ole.util.DocstoreUtil;
10 import org.kuali.rice.core.api.util.ConcreteKeyValue;
11 import org.kuali.rice.core.api.util.KeyValue;
12 import org.kuali.rice.krad.keyvalues.KeyValuesBase;
13 import org.kuali.rice.krad.service.BusinessObjectService;
14 import org.kuali.rice.krad.service.KRADServiceLocator;
15
16 import java.util.*;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20
21
22
23 public class OLEBatchProcessInvoiceImportProfileValueFinder extends KeyValuesBase {
24
25 private static final Logger LOG = Logger.getLogger(OLEBatchProcessInvoiceImportProfileValueFinder.class);
26
27 public static List<KeyValue> locationKeyValues = null;
28
29 public static long timeLastRefreshed;
30
31
32 public static int refreshInterval = 300;
33
34
35
36
37
38
39
40
41
42 private DocstoreUtil docstoreUtil;
43 public DocstoreUtil getDocstoreUtil() {
44
45 if (docstoreUtil == null) {
46 docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
47
48 }
49 return docstoreUtil;
50 }
51 @Override
52 public List<KeyValue> getKeyValues() {
53
54 List<KeyValue> options = initProfileDetailsForInvoiceImport();
55 return options;
56 }
57
58 public static List<KeyValue> initProfileDetailsForInvoiceImport() {
59
60 List<KeyValue> keyValues = new ArrayList<KeyValue>();
61 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
62 Map parentCriteria1 = new HashMap();
63 parentCriteria1.put("batchProcessProfileType", "Invoice Import");
64 List<OLEBatchProcessProfileBo> oleBatchProcessProfileBo = (List<OLEBatchProcessProfileBo>) businessObjectService.findMatching(OLEBatchProcessProfileBo.class, parentCriteria1);
65 keyValues.add(new ConcreteKeyValue("", ""));
66 for (OLEBatchProcessProfileBo profileBo : oleBatchProcessProfileBo) {
67 keyValues.add(new ConcreteKeyValue(profileBo.getBatchProcessProfileName(),profileBo.getBatchProcessProfileId()));
68 }
69 return keyValues;
70 }
71
72 public String getProfileName(String profileName){
73 return profileName;
74 }
75
76
77 public static List<String> retrieveBatchProfileNamesForInvoiceImport(String locationVal) {
78 List<KeyValue> locationKeyValues = retrieveProfileDetailsForInvoiceImport();
79 List<String> locationValues = new ArrayList<String>();
80 for (KeyValue keyValue : locationKeyValues) {
81 locationValues.add(keyValue.getKey());
82 }
83 Pattern pattern = Pattern.compile("[?$(){}\\[\\]\\^\\\\]");
84 Matcher matcher = pattern.matcher(locationVal);
85 if (matcher.matches()) {
86 return new ArrayList<String>();
87 }
88
89 if (!locationVal.equalsIgnoreCase("*")) {
90 locationValues = Lists.newArrayList(Collections2.filter(locationValues, Predicates.contains(Pattern.compile(locationVal, Pattern.CASE_INSENSITIVE))));
91 }
92 Collections.sort(locationValues);
93 return locationValues;
94 }
95
96
97
98 private static List<KeyValue> retrieveProfileDetailsForInvoiceImport() {
99 long currentTime = System.currentTimeMillis() / 1000;
100 if (locationKeyValues == null) {
101 locationKeyValues = initProfileDetailsForInvoiceImport();
102 timeLastRefreshed = currentTime;
103 } else {
104 if (currentTime - timeLastRefreshed > refreshInterval) {
105 locationKeyValues = initProfileDetailsForInvoiceImport();
106 timeLastRefreshed = currentTime;
107 }
108 }
109 return locationKeyValues;
110 }
111
112 public static String getValue(String batchVal){
113 String value="";
114 locationKeyValues=retrieveProfileDetailsForInvoiceImport();
115
116 for (KeyValue keyValue : locationKeyValues) {
117 if(batchVal.equalsIgnoreCase(keyValue.getKey())){
118 value =keyValue.getValue();
119 }
120 }
121 return value;
122 }
123
124
125 }