1 | |
package org.codeforamerica.open311.internals.caching; |
2 | |
|
3 | |
import java.io.Serializable; |
4 | |
import java.util.HashMap; |
5 | |
import java.util.List; |
6 | |
import java.util.Map; |
7 | |
|
8 | |
import org.codeforamerica.open311.facade.City; |
9 | |
import org.codeforamerica.open311.facade.data.Service; |
10 | |
import org.codeforamerica.open311.facade.data.ServiceDefinition; |
11 | |
import org.codeforamerica.open311.facade.data.ServiceDiscoveryInfo; |
12 | |
import org.codeforamerica.open311.facade.data.ServiceRequest; |
13 | |
import org.codeforamerica.open311.facade.data.operations.GETServiceRequestsFilter; |
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
public abstract class AbstractCache implements Cache { |
23 | |
protected final static String FILE = "cache.prop"; |
24 | |
|
25 | |
|
26 | |
|
27 | |
private final Map<CacheableOperation, Integer> timeToLive; |
28 | |
|
29 | 1 | public AbstractCache() { |
30 | 1 | timeToLive = new HashMap<CacheableOperation, Integer>(); |
31 | 1 | timeToLive.put(CacheableOperation.GET_SERVICE_DISCOVERY, 720); |
32 | 1 | timeToLive.put(CacheableOperation.GET_SERVICE_LIST, 24); |
33 | 1 | timeToLive.put(CacheableOperation.GET_SERVICE_DEFINITION, 24); |
34 | 1 | timeToLive.put(CacheableOperation.GET_SERVICE_REQUEST_LIST, 24); |
35 | 1 | timeToLive.put(CacheableOperation.GET_SINGLE_SERVICE_REQUEST, 24); |
36 | 1 | timeToLive.put(CacheableOperation.GET_CITIES_SERVICE_DISCOVERY_URLS, |
37 | |
1440); |
38 | 1 | } |
39 | |
|
40 | |
@Override |
41 | |
public void saveCitiesInfo(String data) { |
42 | 3 | CacheableObject cacheableObject = new CacheableObject( |
43 | |
data, |
44 | |
timeToLive |
45 | |
.get(CacheableOperation.GET_CITIES_SERVICE_DISCOVERY_URLS)); |
46 | 3 | saveProperty( |
47 | |
CacheableOperation.GET_CITIES_SERVICE_DISCOVERY_URLS.toString(), |
48 | |
cacheableObject.serialize()); |
49 | 3 | } |
50 | |
|
51 | |
@Override |
52 | |
public String retrieveCitiesInfo() { |
53 | 6 | return (String) new CacheableObject( |
54 | |
getProperty(CacheableOperation.GET_CITIES_SERVICE_DISCOVERY_URLS |
55 | |
.toString())).getObject(); |
56 | |
} |
57 | |
|
58 | |
@Override |
59 | |
public void saveServiceDiscovery(City city, |
60 | |
ServiceDiscoveryInfo serviceDiscovery) { |
61 | 6 | if (city != null && serviceDiscovery != null) { |
62 | 5 | CacheableObject cacheableObject = new CacheableObject( |
63 | |
serviceDiscovery, |
64 | |
timeToLive.get(CacheableOperation.GET_SERVICE_DISCOVERY)); |
65 | 5 | saveProperty(CacheableOperation.GET_SERVICE_DISCOVERY.toString() |
66 | |
+ city.toString(), cacheableObject.serialize()); |
67 | |
} |
68 | 6 | } |
69 | |
|
70 | |
@Override |
71 | |
public ServiceDiscoveryInfo retrieveCachedServiceDiscoveryInfo(City city) { |
72 | 9 | if (city != null) { |
73 | 8 | String rawData = getProperty(CacheableOperation.GET_SERVICE_DISCOVERY |
74 | |
+ city.toString()); |
75 | 8 | CacheableObject deserializedObject = new CacheableObject(rawData); |
76 | 8 | return (ServiceDiscoveryInfo) deserializedObject.getObject(); |
77 | |
} |
78 | 1 | return null; |
79 | |
} |
80 | |
|
81 | |
@Override |
82 | |
public void saveListOfServices(String endpointUrl, List<Service> services) { |
83 | 4 | if (endpointUrl != null && endpointUrl.length() > 0 && services != null) { |
84 | 1 | Serializable list = (Serializable) services; |
85 | 1 | CacheableObject cacheableObject = new CacheableObject(list, |
86 | |
timeToLive.get(CacheableOperation.GET_SERVICE_LIST)); |
87 | 1 | saveProperty(CacheableOperation.GET_SERVICE_LIST.toString() |
88 | |
+ endpointUrl, cacheableObject.serialize()); |
89 | |
} |
90 | 4 | } |
91 | |
|
92 | |
@SuppressWarnings("unchecked") |
93 | |
@Override |
94 | |
public List<Service> retrieveCachedServiceList(String endpointUrl) { |
95 | 7 | String rawData = getProperty(CacheableOperation.GET_SERVICE_LIST |
96 | |
+ endpointUrl); |
97 | 7 | CacheableObject deserializedObject = new CacheableObject(rawData); |
98 | 7 | return (List<Service>) deserializedObject.getObject(); |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
public void saveServiceDefinition(String endpointUrl, String serviceCode, |
103 | |
ServiceDefinition serviceDefinition) { |
104 | 2 | if (endpointUrl != null && endpointUrl.length() > 0 |
105 | |
&& serviceCode != null && serviceCode.length() > 0 |
106 | |
&& serviceDefinition != null) { |
107 | 1 | Serializable definition = (Serializable) serviceDefinition; |
108 | 1 | CacheableObject cacheableObject = new CacheableObject(definition, |
109 | |
timeToLive.get(CacheableOperation.GET_SERVICE_DEFINITION)); |
110 | 1 | saveProperty(CacheableOperation.GET_SERVICE_DEFINITION.toString() |
111 | |
+ endpointUrl + serviceCode, cacheableObject.serialize()); |
112 | |
} |
113 | 2 | } |
114 | |
|
115 | |
@Override |
116 | |
public ServiceDefinition retrieveCachedServiceDefinition( |
117 | |
String endpointUrl, String serviceCode) { |
118 | 4 | String rawData = getProperty(CacheableOperation.GET_SERVICE_DEFINITION |
119 | |
+ endpointUrl + serviceCode); |
120 | 4 | CacheableObject deserializedObject = new CacheableObject(rawData); |
121 | 4 | return (ServiceDefinition) deserializedObject.getObject(); |
122 | |
} |
123 | |
|
124 | |
@Override |
125 | |
public void saveServiceRequestList(String endpointUrl, |
126 | |
GETServiceRequestsFilter filter, List<ServiceRequest> requests) { |
127 | 2 | if (endpointUrl != null && endpointUrl.length() > 0 && filter != null |
128 | |
&& requests != null) { |
129 | 1 | Serializable list = (Serializable) requests; |
130 | 1 | CacheableObject cacheableObject = new CacheableObject(list, |
131 | |
timeToLive.get(CacheableOperation.GET_SERVICE_REQUEST_LIST)); |
132 | 1 | saveProperty(CacheableOperation.GET_SERVICE_REQUEST_LIST |
133 | |
+ endpointUrl + CacheableObject.serialize(filter), |
134 | |
cacheableObject.serialize()); |
135 | |
} |
136 | 2 | } |
137 | |
|
138 | |
@SuppressWarnings("unchecked") |
139 | |
@Override |
140 | |
public List<ServiceRequest> retrieveCachedServiceRequests( |
141 | |
String endpointUrl, GETServiceRequestsFilter filter) { |
142 | 3 | String rawData = getProperty(CacheableOperation.GET_SERVICE_REQUEST_LIST |
143 | |
+ endpointUrl + CacheableObject.serialize(filter)); |
144 | 3 | CacheableObject deserializedObject = new CacheableObject(rawData); |
145 | 3 | return (List<ServiceRequest>) deserializedObject.getObject(); |
146 | |
} |
147 | |
|
148 | |
@Override |
149 | |
public void saveSingleServiceRequest(String endpointUrl, |
150 | |
String serviceRequestId, ServiceRequest request) { |
151 | 2 | if (endpointUrl != null && endpointUrl.length() > 0 |
152 | |
&& serviceRequestId != null && serviceRequestId.length() > 0 |
153 | |
&& request != null) { |
154 | 1 | CacheableObject cacheableServiceRequest = new CacheableObject( |
155 | |
request, |
156 | |
timeToLive |
157 | |
.get(CacheableOperation.GET_SINGLE_SERVICE_REQUEST)); |
158 | 1 | saveProperty(CacheableOperation.GET_SINGLE_SERVICE_REQUEST |
159 | |
+ endpointUrl + serviceRequestId, |
160 | |
cacheableServiceRequest.serialize()); |
161 | |
} |
162 | 2 | } |
163 | |
|
164 | |
@Override |
165 | |
public ServiceRequest retrieveCachedServiceRequest(String endpointUrl, |
166 | |
String serviceRequestId) { |
167 | 5 | String rawData = getProperty(CacheableOperation.GET_SINGLE_SERVICE_REQUEST |
168 | |
+ endpointUrl + serviceRequestId); |
169 | 5 | CacheableObject deserializedObject = new CacheableObject(rawData); |
170 | 5 | return (ServiceRequest) deserializedObject.getObject(); |
171 | |
} |
172 | |
|
173 | |
public void setCustomTimeToLive(CacheableOperation operation, |
174 | |
int timeToLiveInHours) { |
175 | 0 | if (operation != null && timeToLiveInHours >= 1) { |
176 | 0 | timeToLive.put(operation, timeToLiveInHours); |
177 | |
} |
178 | 0 | } |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
protected abstract void saveProperty(String key, String value); |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
protected abstract String getProperty(String key); |
200 | |
} |