1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package com.p4square.groupsindexer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.p4square.ccbapi.CCBAPI;
import com.p4square.ccbapi.model.*;
import com.p4square.groupsindexer.model.SearchField;
import com.p4square.groupsindexer.model.StringPair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SearchFieldsCache {
private static final long REFRESH_INTERVAL_MS = 15 * 60 * 1000;
private static final Logger LOG = LogManager.getLogger(SearchFieldsCache.class);
private final CCBAPI ccbClient;
private List<SearchField> cachedFields;
private long lastRefresh;
public SearchFieldsCache(CCBAPI ccbClient) {
this.ccbClient = ccbClient;
}
public synchronized List<SearchField> getSearchFields() {
if (System.currentTimeMillis() - lastRefresh < REFRESH_INTERVAL_MS) {
LOG.debug("Using cached CCB fields");
return cachedFields;
}
try {
LOG.info("Fetching fields from CCB");
cachedFields = new ArrayList<>();
final GetCustomFieldLabelsResponse labels = ccbClient.getCustomFieldLabels();
cachedFields.add(new SearchField("area", "Campus", getValues(LookupTableType.AREA)));
cachedFields.add(new SearchField("meetingDay", "Day", getValues(LookupTableType.MEET_DAY)));
for (final CustomField field : labels.getCustomFields()) {
final LookupTableType type = getTypeFromString(field.getName());
if (type != null) {
cachedFields.add(new SearchField(getSearchFieldIdForType(type), field.getLabel(), getValues(type)));
}
}
cachedFields.add(new SearchField("childcare", "Childcare",
Arrays.asList(StringPair.of("true", "Yes"), StringPair.of("false", "No"))));
lastRefresh = System.currentTimeMillis();
return cachedFields;
} catch (Exception e) {
LOG.error(e.getMessage());
return null;
}
}
private LookupTableType getTypeFromString(String name) {
switch (name) {
case "udf_grp_pulldown_1":
return LookupTableType.UDF_GRP_PULLDOWN_1;
case "udf_grp_pulldown_2":
return LookupTableType.UDF_GRP_PULLDOWN_2;
case "udf_grp_pulldown_3":
return LookupTableType.UDF_GRP_PULLDOWN_3;
default:
return null;
}
}
private String getSearchFieldIdForType(LookupTableType type) {
switch (type) {
case UDF_GRP_PULLDOWN_1:
return "udf_1";
case UDF_GRP_PULLDOWN_2:
return "udf_2";
case UDF_GRP_PULLDOWN_3:
return "udf_3";
default:
throw new IllegalArgumentException();
}
}
private List<StringPair> getValues(LookupTableType type) throws IOException {
final GetLookupTableRequest lookupTableRequest = new GetLookupTableRequest().withType(type);
final GetLookupTableResponse lookupTable = ccbClient.getLookupTable(lookupTableRequest);
return lookupTable.getItems()
.stream()
.map(entry -> StringPair.of(String.valueOf(entry.getId()), entry.getName()))
.collect(Collectors.toList());
}
}
|