1 package org.kuali.ole.describe.keyvalue;
2
3 import org.kuali.ole.describe.bo.OleLocation;
4 import org.kuali.rice.core.api.util.ConcreteKeyValue;
5 import org.kuali.rice.core.api.util.KeyValue;
6 import org.kuali.rice.krad.keyvalues.KeyValuesBase;
7 import org.kuali.rice.krad.service.BusinessObjectService;
8 import org.kuali.rice.krad.service.KRADServiceLocator;
9
10 import java.util.*;
11
12
13
14
15 public class CallNumberBrowseLocationValuesBuilder extends KeyValuesBase {
16
17 @Override
18 public List<KeyValue> getKeyValues() {
19 List<KeyValue> options = retrieveLocationDetails();
20 List<KeyValue> locationList = new ArrayList<KeyValue>();
21 Map<String, String> map = new HashMap<String, String>();
22 for (KeyValue option : options) {
23 map.put(option.getKey(), option.getValue());
24 }
25 Map<String, Object> locationMap = sortByLocation(map);
26 for (Map.Entry<String, Object> entry : locationMap.entrySet()) {
27 locationList.add(new ConcreteKeyValue(entry.getKey(), (String) entry.getValue()));
28 }
29 return locationList;
30 }
31
32 public static List<KeyValue> retrieveLocationDetails() {
33 List<KeyValue> options = new ArrayList<KeyValue>();
34 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
35 Collection<OleLocation> oleLocationCollection = businessObjectService.findAll(OleLocation.class);
36 options.add(new ConcreteKeyValue("", ""));
37 for (OleLocation oleLocation : oleLocationCollection) {
38
39
40 String locationCode = oleLocation.getLocationCode();
41 String levelCode = oleLocation.getOleLocationLevel().getLevelCode();
42 if ("LIBRARY".equals(levelCode)) {
43 options.add(new ConcreteKeyValue(locationCode, locationCode));
44 }
45 }
46 return options;
47 }
48
49 private Map<String, Object> sortByLocation(Map<String, String> parentCriteria) {
50 Map<String, Object> map = new LinkedHashMap<String, Object>();
51 List<String> keyList = new ArrayList<String>(parentCriteria.keySet());
52 List<String> valueList = new ArrayList<String>(parentCriteria.values());
53 Set<String> sortedSet = new TreeSet<String>(valueList);
54 Object[] sortedArray = sortedSet.toArray();
55 int size = sortedArray.length;
56 for (int i = 0; i < size; i++) {
57 map.put(keyList.get(valueList.indexOf(sortedArray[i])), sortedArray[i]);
58 }
59 return map;
60 }
61
62 }
63