1 package org.kuali.ole.describe.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.OLEConstants;
8 import org.kuali.ole.deliver.processor.LoanProcessor;
9 import org.kuali.ole.deliver.bo.OleLoanDocument;
10 import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
11 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Item;
12 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Location;
13 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.LocationLevel;
14 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.OleHoldings;
15 import org.kuali.ole.describe.service.DocstoreHelperService;
16 import org.kuali.ole.describe.bo.OleLocation;
17 import org.kuali.ole.describe.bo.OleLocationLevel;
18 import org.kuali.rice.core.api.util.ConcreteKeyValue;
19 import org.kuali.rice.core.api.util.KeyValue;
20 import org.kuali.rice.krad.keyvalues.KeyValuesBase;
21 import org.kuali.rice.krad.service.BusinessObjectService;
22 import org.kuali.rice.krad.service.KRADServiceLocator;
23
24 import java.util.*;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28
29
30
31 public class LocationValuesBuilder extends KeyValuesBase {
32
33 private static final Logger LOG = Logger.getLogger(LocationValuesBuilder.class);
34
35 public static List<KeyValue> locationKeyValues = null;
36
37 public static long timeLastRefreshed;
38
39 public static int refreshInterval = 300;
40
41
42
43
44
45
46
47
48 @Override
49 public List<KeyValue> getKeyValues() {
50
51 List<KeyValue> options = initLocationDetails();
52
53 List<KeyValue> locationList = new ArrayList<KeyValue>();
54 Map<String, String> map = new HashMap<String, String>();
55 for (KeyValue option : options) {
56 map.put(option.getKey(), option.getValue());
57
58 }
59 Map<String, Object> locationMap = sortByLocation(map);
60 for (Map.Entry<String, Object> entry : locationMap.entrySet()) {
61 locationList.add(new ConcreteKeyValue(entry.getKey(), (String) entry.getValue()));
62 }
63 return locationList;
64 }
65
66 public static List<KeyValue> initLocationDetails() {
67
68 List<KeyValue> options = new ArrayList<KeyValue>();
69 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
70
71 Map parentCriteria1 = new HashMap();
72 parentCriteria1.put("levelCode", "SHELVING");
73 List<OleLocationLevel> oleLocationLevel = (List<OleLocationLevel>) businessObjectService.findMatching(OleLocationLevel.class, parentCriteria1);
74 String shelvingId = oleLocationLevel.get(0).getLevelId();
75 options.add(new ConcreteKeyValue("", ""));
76 Map parentCriteria = new HashMap();
77 parentCriteria.put("levelId", shelvingId);
78 Collection<OleLocation> oleLocationCollection = businessObjectService.findMatching(OleLocation.class, parentCriteria);
79 for (OleLocation oleLocation : oleLocationCollection) {
80 String locationName = oleLocation.getLocationName();
81 String levelId = oleLocation.getLevelId();
82 String levelCode = oleLocation.getLocationCode();
83 boolean parentId = oleLocation.getParentLocationId() != null ? true : false;
84 while (parentId) {
85 Map criteriaMap = new HashMap();
86 criteriaMap.put("locationId", oleLocation.getParentLocationId());
87 OleLocation location = businessObjectService.findByPrimaryKey(OleLocation.class,
88 criteriaMap);
89 if (locationName != null) {
90 locationName = location.getLocationName() + "/" + locationName;
91 }
92 if (levelCode != null) {
93 levelCode = location.getLocationCode() + "/" + levelCode;
94 }
95 parentId = location.getParentLocationId() != null ? true : false;
96 oleLocation = location;
97 }
98
99 options.add(new ConcreteKeyValue(levelCode, levelCode));
100 }
101 return options;
102 }
103
104
105 public static List<String> retrieveLocationDetailsForSuggest(String locationVal) {
106 List<KeyValue> locationKeyValues = retrieveLocationDetails();
107 List<String> locationValues = new ArrayList<String>();
108 for (KeyValue keyValue : locationKeyValues) {
109 locationValues.add(keyValue.getKey());
110 }
111
112 Pattern pattern = Pattern.compile("[?$(){}\\[\\]\\^\\\\]");
113 Matcher matcher = pattern.matcher(locationVal);
114 if (matcher.matches()) {
115 return new ArrayList<String>();
116 }
117
118 if (!locationVal.equalsIgnoreCase("*")) {
119 locationValues = Lists.newArrayList(Collections2.filter(locationValues, Predicates.contains(Pattern.compile(locationVal, Pattern.CASE_INSENSITIVE))));
120 }
121 Collections.sort(locationValues);
122 return locationValues;
123 }
124
125 private static List<KeyValue> retrieveLocationDetails() {
126 long currentTime = System.currentTimeMillis() / 1000;
127 if (locationKeyValues == null) {
128 locationKeyValues = initLocationDetails();
129 timeLastRefreshed = currentTime;
130 } else {
131 if (currentTime - timeLastRefreshed > refreshInterval) {
132 locationKeyValues = initLocationDetails();
133 timeLastRefreshed = currentTime;
134 }
135 }
136 return locationKeyValues;
137 }
138
139 private Map<String, Object> sortByLocation(Map<String, String> parentCriteria) {
140 Map<String, Object> map = new LinkedHashMap<String, Object>();
141
142 List<String> keyList = new ArrayList<String>(parentCriteria.keySet());
143 List<String> valueList = new ArrayList<String>(parentCriteria.values());
144 Set<String> sortedSet = new TreeSet<String>(valueList);
145 Object[] sortedArray = sortedSet.toArray();
146 int size = sortedArray.length;
147 for (int i = 0; i < size; i++) {
148 map.put(keyList.get(valueList.indexOf(sortedArray[i])), sortedArray[i]);
149 }
150 return map;
151 }
152
153
154 public String getLocation(LocationLevel oleLocationLevel) {
155 String location = null;
156 if (OLEConstants.LOCATION_LEVEL_SHELVING.equalsIgnoreCase(oleLocationLevel.getLevel()))
157 location = oleLocationLevel.getName();
158 else
159 location = getLocation(oleLocationLevel.getLocationLevel());
160 if ("".equals(location) || location == null)
161 return null;
162 return location;
163 }
164
165
166 public String getShelvingLocation(Location oleLocation) throws Exception {
167 if (oleLocation == null) {
168 return "";
169 }
170 LocationLevel locationLevel = oleLocation.getLocationLevel();
171 if (locationLevel != null) {
172 while (locationLevel != null && locationLevel.getLocationLevel() != null && !locationLevel.getLevel().equalsIgnoreCase(OLEConstants.OleDeliverRequest.SHELVING)) {
173 locationLevel = locationLevel.getLocationLevel();
174 }
175 return locationLevel.getName();
176 }
177 return null;
178 }
179
180
181
182
183
184
185
186
187
188 public void getLocation(Item oleItem, Object object, String instanceUUID) throws Exception {
189 LOG.debug("Inside the getLocation method");
190 try {
191 Location physicalLocation = oleItem.getLocation();
192 LocationLevel locationLevel = null;
193 locationLevel = physicalLocation.getLocationLevel();
194 getOleLocationLevel(object, locationLevel);
195 } catch (Exception itemException) {
196 LOG.error("--------------Invalid Item location data.---------------" + itemException);
197 try {
198 DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
199 OleHoldings oleHoldings = docstoreHelperService.getOleHoldings(instanceUUID);
200 if (oleHoldings != null) {
201 Location physicalLocation = oleHoldings.getLocation();
202 LocationLevel locationLevel = null;
203 locationLevel = physicalLocation.getLocationLevel();
204 getOleLocationLevel(object, locationLevel);
205 }
206 } catch (Exception holdingException) {
207 LOG.error("--------------Invalid Holding location data.---------------" + itemException);
208 throw new Exception(OLEConstants.INVAL_LOC);
209 }
210 }
211 }
212
213
214
215
216
217
218
219
220
221 public void getOleLocationLevel(Object object, LocationLevel locationLevel) throws Exception {
222 LOG.debug("Inside the getOleLocationLevel method");
223 StringBuffer location = new StringBuffer();
224 while (locationLevel.getLocationLevel() != null) {
225 LoanProcessor loanProcessor = new LoanProcessor();
226 OleLocationLevel oleLocationLevel = loanProcessor.getLocationLevelByName(locationLevel.getLevel());
227 OleLocation oleLocation = new OleLocation();
228 if (!"".equals(locationLevel.getName())) {
229 oleLocation = getLocationByLocationCode(locationLevel.getName());
230 }
231 setLocation(location, oleLocationLevel.getLevelName(), oleLocation.getLocationCode(), oleLocation.getLocationName(), object);
232 locationLevel = locationLevel.getLocationLevel();
233 }
234 if (object instanceof OleDeliverRequestBo) {
235 OleDeliverRequestBo oleDeliverRequestBo = (OleDeliverRequestBo) object;
236 oleDeliverRequestBo.setShelvingLocation(locationLevel.getName());
237 }
238 }
239
240
241
242
243
244
245
246
247 private OleLocation getLocationByLocationCode(String locationCode) throws Exception {
248 LOG.debug("Inside the getLocationByLocationCode method");
249 Map barMap = new HashMap();
250 barMap.put(OLEConstants.LOC_CD, locationCode);
251 List<OleLocation> matchingLocation = (List<OleLocation>) KRADServiceLocator.getBusinessObjectService().findMatching(OleLocation.class, barMap);
252 return matchingLocation.get(0);
253 }
254
255
256
257
258
259
260
261
262
263
264 private void setLocation(StringBuffer location, String locationLevelName, String locationCode, String locationName, Object object) throws Exception {
265 LOG.debug("Inside the setLocation method");
266 OleLoanDocument oleLoanDocument = null;
267 OleDeliverRequestBo oleDeliverRequestBo = null;
268 if (object instanceof OleLoanDocument) {
269 oleLoanDocument = (OleLoanDocument) object;
270 } else if (object instanceof OleDeliverRequestBo) {
271 oleDeliverRequestBo = (OleDeliverRequestBo) object;
272 }
273 if (locationCode != null) {
274 if (oleLoanDocument != null) {
275 if (locationLevelName.equalsIgnoreCase(OLEConstants.LOCATION_LEVEL_SHELVING)) {
276 location.append(locationName);
277 oleLoanDocument.setItemLocation(locationCode);
278 } else if (locationLevelName.equalsIgnoreCase(OLEConstants.LOCATION_LEVEL_COLLECTION)) {
279 location.append(locationName + "-");
280 oleLoanDocument.setItemCollection(locationCode);
281 } else if (locationLevelName.equalsIgnoreCase(OLEConstants.LOCATION_LEVEL_LIBRARY)) {
282 location.append(locationName + "-");
283 oleLoanDocument.setItemLibrary(locationCode);
284 } else if (locationLevelName.equalsIgnoreCase(OLEConstants.LOCATION_LEVEL_INSTITUTION)) {
285 location.append(locationName + "-");
286 oleLoanDocument.setItemInstitution(locationCode);
287 } else if (locationLevelName.equalsIgnoreCase(OLEConstants.LOCATION_LEVEL_CAMPUS)) {
288 location.append(locationName + "-");
289 oleLoanDocument.setItemCampus(locationCode);
290 }
291 } else if (oleDeliverRequestBo != null) {
292 if (locationLevelName.equalsIgnoreCase(OLEConstants.LOCATION_LEVEL_SHELVING)) {
293 oleDeliverRequestBo.setShelvingLocation(locationCode);
294 } else if (locationLevelName.equalsIgnoreCase(OLEConstants.LOCATION_LEVEL_COLLECTION)) {
295 oleDeliverRequestBo.setItemCollection(locationCode);
296 } else if (locationLevelName.equalsIgnoreCase(OLEConstants.LOCATION_LEVEL_LIBRARY)) {
297 oleDeliverRequestBo.setItemLibrary(locationCode);
298 } else if (locationLevelName.equalsIgnoreCase(OLEConstants.LOCATION_LEVEL_INSTITUTION)) {
299 oleDeliverRequestBo.setItemInstitution(locationCode);
300 } else if (locationLevelName.equalsIgnoreCase(OLEConstants.LOCATION_LEVEL_CAMPUS)) {
301 oleDeliverRequestBo.setItemCampus(locationCode);
302 }
303 }
304
305 }
306 }
307
308 }