View Javadoc
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   * Created by hemalathas on 12/31/14.
22   */
23  public class OLEBatchProcessOrderImportProfileValueFinder extends KeyValuesBase {
24  
25      private static final Logger LOG = Logger.getLogger(OLEBatchProcessOrderImportProfileValueFinder.class);
26  
27      public static List<KeyValue> locationKeyValues = null;
28      public static List<KeyValue> krmsProfile = new ArrayList<KeyValue>();
29      public static List<KeyValue> mrcOnly = new ArrayList<KeyValue>();
30  
31      public static long timeLastRefreshed;
32  
33  
34      public static int refreshInterval = 300;     // in seconds
35  
36      /**
37       * This method returns the List of ConcreteKeyValue,
38       * ConcreteKeyValue has two arguments LevelCode and
39       * LocationName.
40       *
41       * @return List<KeyValue>
42       */
43  
44      private DocstoreUtil docstoreUtil;
45      public DocstoreUtil getDocstoreUtil() {
46  
47          if (docstoreUtil == null) {
48              docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
49  
50          }
51          return docstoreUtil;
52      }
53      @Override
54      public List<KeyValue> getKeyValues() {
55  
56          List<KeyValue> options = initProfileDetailsForOrderImport();
57          return options;
58      }
59  
60      public static List<KeyValue> initProfileDetailsForOrderImport() {
61          krmsProfile.clear();
62          List<KeyValue> keyValues = new ArrayList<KeyValue>();
63  
64          BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
65          Map parentCriteria1 = new HashMap();
66          parentCriteria1.put("batchProcessProfileType", "Order Record Import");
67          List<OLEBatchProcessProfileBo> oleBatchProcessProfileBo = (List<OLEBatchProcessProfileBo>) businessObjectService.findMatching(OLEBatchProcessProfileBo.class, parentCriteria1);
68  
69          keyValues.add(new ConcreteKeyValue("", ""));
70          for (OLEBatchProcessProfileBo profileBo : oleBatchProcessProfileBo) {
71              keyValues.add(new ConcreteKeyValue( profileBo.getBatchProcessProfileName(),profileBo.getBatchProcessProfileId()));
72              krmsProfile.add(new ConcreteKeyValue( profileBo.getBatchProcessProfileName(),profileBo.getKrmsProfileName()));
73              mrcOnly.add(new ConcreteKeyValue( profileBo.getBatchProcessProfileName(),profileBo.getMarcOnly().toString()));
74          }
75          return keyValues;
76      }
77  
78  
79  
80      public static List<String> retrieveBatchProfileNamesForOrderImport(String locationVal) {
81          locationKeyValues = retrieveProfileDetailsForOrderImport();
82          List<String> locationValues = new ArrayList<String>();
83          for (KeyValue keyValue : locationKeyValues) {
84              locationValues.add(keyValue.getKey());
85          }
86          Pattern pattern = Pattern.compile("[?$(){}\\[\\]\\^\\\\]");
87          Matcher matcher = pattern.matcher(locationVal);
88          if (matcher.matches()) {
89              return new ArrayList<String>();
90          }
91  
92          if (!locationVal.equalsIgnoreCase("*")) {
93              locationValues = Lists.newArrayList(Collections2.filter(locationValues, Predicates.contains(Pattern.compile(locationVal, Pattern.CASE_INSENSITIVE))));
94          }
95          Collections.sort(locationValues);
96          return locationValues;
97      }
98  
99  
100 
101     private static List<KeyValue> retrieveProfileDetailsForOrderImport() {
102         long currentTime = System.currentTimeMillis() / 1000;
103         if (locationKeyValues == null) {
104             locationKeyValues = initProfileDetailsForOrderImport();
105             timeLastRefreshed = currentTime;
106         } else {
107             if (currentTime - timeLastRefreshed > refreshInterval) {
108                 locationKeyValues = initProfileDetailsForOrderImport();
109                 timeLastRefreshed = currentTime;
110             }
111         }
112         return locationKeyValues;
113     }
114 
115 
116     public  static String getValue(String batchVal){
117         String value="";
118         locationKeyValues=initProfileDetailsForOrderImport();
119        for (KeyValue keyValue : locationKeyValues) {
120                 if(batchVal.equalsIgnoreCase(keyValue.getKey())){
121                     value =keyValue.getValue();
122                 }
123         }
124             return value;
125     }
126 
127     public static String getKrmsValue(String batchVal){
128         String value="";
129         locationKeyValues=initProfileDetailsForOrderImport();
130 
131         for (KeyValue keyValue : krmsProfile) {
132             if(batchVal.equalsIgnoreCase(keyValue.getKey())){
133                 value =keyValue.getValue();
134             }
135         }
136         return value;
137     }
138 
139 
140 
141     public static boolean getMarcValue(String batchVal){
142         String value="";
143         locationKeyValues=initProfileDetailsForOrderImport();
144 
145         for (KeyValue keyValue : mrcOnly) {
146             if(batchVal.equalsIgnoreCase(keyValue.getKey())){
147                 value =keyValue.getValue();
148             }
149         }
150         return Boolean.valueOf(value);
151     }
152 
153 }