1 package org.kuali.ole.deliver.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.kuali.ole.OLEConstants;
7 import org.kuali.ole.deliver.bo.OleCirculationDesk;
8 import org.kuali.ole.deliver.bo.OleCirculationDeskDetail;
9 import org.kuali.rice.core.api.util.ConcreteKeyValue;
10 import org.kuali.rice.core.api.util.KeyValue;
11 import org.kuali.rice.kim.api.permission.PermissionService;
12 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
13 import org.kuali.rice.krad.keyvalues.KeyValuesBase;
14 import org.kuali.rice.krad.service.KRADServiceLocator;
15 import org.kuali.rice.krad.util.GlobalVariables;
16
17 import java.util.*;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21
22
23
24
25
26
27
28 public class CirculationDeskChangeKeyValue extends KeyValuesBase {
29
30 public static List<KeyValue> circulationDeskKeyValues = null;
31
32 public static long timeLastRefreshed;
33
34 public static int refreshInterval = 300;
35
36 private PermissionService getPermissionService() {
37 PermissionService service = KimApiServiceLocator.getPermissionService();
38 return service;
39 }
40
41 @Override
42 public List<KeyValue> getKeyValues() {
43 List<KeyValue> options = new ArrayList<KeyValue>();
44 Map<String, String> userMap = new HashMap<String, String>();
45 userMap.put("operatorId", GlobalVariables.getUserSession().getPrincipalId());
46
47 Collection<OleCirculationDeskDetail> oleCirculationDeskDetails = KRADServiceLocator.getBusinessObjectService().findMatching(OleCirculationDeskDetail.class, userMap);
48
49 for (OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetails) {
50 if (oleCirculationDeskDetail.isDefaultLocation() && oleCirculationDeskDetail.getOleCirculationDesk().isActive()) {
51 options.add(new ConcreteKeyValue(oleCirculationDeskDetail.getOleCirculationDesk().getCirculationDeskId(), oleCirculationDeskDetail.getOleCirculationDesk().getCirculationDeskCode()));
52 }
53 }
54 for (OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetails) {
55 if (!oleCirculationDeskDetail.isDefaultLocation() && oleCirculationDeskDetail.getOleCirculationDesk().isActive()) {
56 options.add(new ConcreteKeyValue(oleCirculationDeskDetail.getOleCirculationDesk().getCirculationDeskId(), oleCirculationDeskDetail.getOleCirculationDesk().getCirculationDeskCode()));
57 }
58 }
59
60
61
62
63
64
65
66 return options;
67 }
68
69 public static List<KeyValue> initCirculationDeskDetails() {
70
71 List<KeyValue> options = new ArrayList<KeyValue>();
72 Collection<OleCirculationDesk> oleCirculationDesks = KRADServiceLocator.getBusinessObjectService().findAll(OleCirculationDesk.class);
73 for (OleCirculationDesk oleCirculationDesk : oleCirculationDesks) {
74 if (oleCirculationDesk.isActive()) {
75 options.add(new ConcreteKeyValue(oleCirculationDesk.getCirculationDeskId(), oleCirculationDesk.getCirculationDeskCode()));
76 }
77 }
78 return options;
79 }
80
81
82 public static List<String> retrieveCirculationDeskDetailsForSuggest(String locationVal) {
83 List<KeyValue> locationKeyValues = retrieveLocationDetails();
84 List<String> locationValues = new ArrayList<String>();
85 for (KeyValue keyValue : locationKeyValues) {
86 locationValues.add(keyValue.getValue());
87 }
88
89 Pattern pattern = Pattern.compile("[?$(){}\\[\\]\\^\\\\]");
90 Matcher matcher = pattern.matcher(locationVal);
91 if (matcher.matches()) {
92 return new ArrayList<String>();
93 }
94
95 if (!locationVal.equalsIgnoreCase("*")) {
96 locationValues = Lists.newArrayList(Collections2.filter(locationValues, Predicates.contains(Pattern.compile(locationVal, Pattern.CASE_INSENSITIVE))));
97 }
98 Collections.sort(locationValues);
99 return locationValues;
100 }
101
102 private static List<KeyValue> retrieveLocationDetails() {
103 long currentTime = System.currentTimeMillis() / 1000;
104 if (circulationDeskKeyValues == null) {
105 circulationDeskKeyValues = initCirculationDeskDetails();
106 timeLastRefreshed = currentTime;
107 } else {
108 if (currentTime - timeLastRefreshed > refreshInterval) {
109 circulationDeskKeyValues = initCirculationDeskDetails();
110 timeLastRefreshed = currentTime;
111 }
112 }
113 return circulationDeskKeyValues;
114 }
115
116 }