1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.lookup;
17
18 import java.sql.Timestamp;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.apache.commons.codec.binary.Base64;
27 import org.kuali.rice.core.api.CoreApiServiceLocator;
28 import org.kuali.rice.kns.web.ui.ResultRow;
29 import org.kuali.rice.krad.bo.BusinessObject;
30 import org.kuali.rice.krad.dao.PersistedLookupMetadataDao;
31 import org.kuali.rice.krad.exception.AuthorizationException;
32 import org.kuali.rice.krad.service.BusinessObjectService;
33 import org.kuali.rice.krad.util.KRADConstants;
34 import org.kuali.rice.krad.util.ObjectUtils;
35
36
37
38
39 @Deprecated
40 public class LookupResultsServiceImpl implements LookupResultsService {
41 private BusinessObjectService businessObjectService;
42 private PersistedLookupMetadataDao persistedLookupMetadataDao;
43 private LookupResultsSupportStrategyService persistableBusinessObjectSupportStrategy;
44 private LookupResultsSupportStrategyService dataDictionarySupportStrategy;
45 private LookupResultsSupportStrategyService dataObjectBaseSupportStrategy;
46
47 public LookupResultsServiceImpl() {
48 dataObjectBaseSupportStrategy = new DataObjectBaseLookupResultsSupportStrategyImpl();
49 }
50
51
52
53
54 @Override
55 public void persistResultsTable(String lookupResultsSequenceNumber, List<ResultRow> resultTable, String personId) throws Exception {
56 String resultTableString = new String(Base64.encodeBase64(ObjectUtils.toByteArray(resultTable)));
57
58 Timestamp now = CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp();
59
60 LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
61 if (lookupResults == null) {
62 lookupResults = new LookupResults();
63 lookupResults.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
64 }
65 lookupResults.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
66 lookupResults.setLookupPersonId(personId);
67 lookupResults.setSerializedLookupResults(resultTableString);
68 lookupResults.setLookupDate(now);
69 businessObjectService.save(lookupResults);
70 }
71
72
73
74
75 @Override
76 public void persistSelectedObjectIds(String lookupResultsSequenceNumber, Set<String> selectedObjectIds, String personId) throws Exception {
77 SelectedObjectIds selectedObjectIdsBO = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
78 if (selectedObjectIdsBO == null) {
79 selectedObjectIdsBO = new SelectedObjectIds();
80 selectedObjectIdsBO.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
81 }
82 selectedObjectIdsBO.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
83 selectedObjectIdsBO.setLookupPersonId(personId);
84 selectedObjectIdsBO.setSelectedObjectIds(
85 LookupUtils.convertSetOfObjectIdsToString(selectedObjectIds));
86 selectedObjectIdsBO.setLookupDate(CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp());
87 businessObjectService.save(selectedObjectIdsBO);
88 }
89
90
91
92
93
94
95
96 protected LookupResults retrieveLookupResults(String lookupResultsSequenceNumber) throws Exception {
97 Map<String, String> queryCriteria = new HashMap<String, String>();
98 queryCriteria.put(KRADConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, lookupResultsSequenceNumber);
99 LookupResults lookupResults = businessObjectService.findByPrimaryKey(LookupResults.class, queryCriteria);
100
101 return lookupResults;
102 }
103
104
105
106
107
108
109
110 protected SelectedObjectIds retrieveSelectedObjectIds(String lookupResultsSequenceNumber) throws Exception {
111 Map<String, String> queryCriteria = new HashMap<String, String>();
112 queryCriteria.put(KRADConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, lookupResultsSequenceNumber);
113 SelectedObjectIds selectedObjectIds = businessObjectService.findByPrimaryKey(SelectedObjectIds.class, queryCriteria);
114
115 return selectedObjectIds;
116 }
117
118
119
120
121 @Override
122 public boolean isAuthorizedToAccessLookupResults(String lookupResultsSequenceNumber, String personId) {
123 try {
124 LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
125 return isAuthorizedToAccessLookupResults(lookupResults, personId);
126 }
127 catch (Exception e) {
128 return false;
129 }
130 }
131
132
133
134
135
136
137
138
139 protected boolean isAuthorizedToAccessLookupResults(LookupResults lookupResults, String personId) {
140 return isAuthorizedToAccessMultipleValueLookupMetadata(lookupResults, personId);
141 }
142
143
144
145
146 @Override
147 public boolean isAuthorizedToAccessSelectedObjectIds(String lookupResultsSequenceNumber, String personId) {
148 try {
149 SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
150 return isAuthorizedToAccessSelectedObjectIds(selectedObjectIds, personId);
151 }
152 catch (Exception e) {
153 return false;
154 }
155 }
156
157
158
159
160
161
162
163
164 protected boolean isAuthorizedToAccessSelectedObjectIds(SelectedObjectIds selectedObjectIds, String personId) {
165 return isAuthorizedToAccessMultipleValueLookupMetadata(selectedObjectIds, personId);
166 }
167
168
169
170
171
172 @Override
173 public List<ResultRow> retrieveResultsTable(String lookupResultsSequenceNumber, String personId) throws Exception {
174 LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
175 if (!isAuthorizedToAccessLookupResults(lookupResults, personId)) {
176
177 throw new AuthorizationException(personId, "retrieve lookup results", "lookup sequence number " + lookupResultsSequenceNumber);
178 }
179 List<ResultRow> resultTable = (List<ResultRow>) ObjectUtils.fromByteArray(Base64.decodeBase64(lookupResults.getSerializedLookupResults().getBytes()));
180 return resultTable;
181 }
182
183
184
185
186
187
188 @Override
189 public <T extends BusinessObject> Collection<T> retrieveSelectedResultBOs(String lookupResultsSequenceNumber, Class<T> boClass, String personId) throws Exception {
190 final LookupResultsSupportStrategyService supportService = getQualifingSupportStrategy(boClass);
191 if (supportService == null) {
192 throw new RuntimeException("BusinessObject class "+boClass.getName()+" cannot be used within a multiple value lookup; it either needs to be a PersistableBusinessObject or have both its primary keys and a lookupable defined in its data dictionary entry");
193 }
194
195 SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
196
197 if (!isAuthorizedToAccessSelectedObjectIds(selectedObjectIds, personId)) {
198
199 throw new AuthorizationException(personId, "retrieve lookup results", "lookup sequence number " + lookupResultsSequenceNumber);
200 }
201
202 Set<String> setOfSelectedObjIds = LookupUtils
203 .convertStringOfObjectIdsToSet(selectedObjectIds.getSelectedObjectIds());
204
205 if (setOfSelectedObjIds.isEmpty()) {
206
207 return new ArrayList<T>();
208 }
209
210 return supportService.retrieveSelectedResultBOs(boClass, setOfSelectedObjIds);
211 }
212
213
214
215
216
217
218
219 protected LookupResultsSupportStrategyService getQualifingSupportStrategy(Class boClass) {
220 if (getPersistableBusinessObjectSupportStrategy().qualifiesForStrategy(boClass)) {
221 return getPersistableBusinessObjectSupportStrategy();
222 } else if ( dataObjectBaseSupportStrategy.qualifiesForStrategy(boClass) ) {
223 return dataObjectBaseSupportStrategy;
224 } else if (getDataDictionarySupportStrategy().qualifiesForStrategy(boClass)) {
225 return getDataDictionarySupportStrategy();
226 }
227 return null;
228 }
229
230
231
232
233 @Override
234 public void clearPersistedLookupResults(String lookupResultsSequenceNumber) throws Exception {
235 LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
236 if (lookupResults != null) {
237 businessObjectService.delete(lookupResults);
238 }
239 }
240
241
242
243
244 @Override
245 public void clearPersistedSelectedObjectIds(String lookupResultsSequenceNumber) throws Exception {
246 SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
247 if (selectedObjectIds != null) {
248 businessObjectService.delete(selectedObjectIds);
249 }
250 }
251
252
253
254
255
256 @Override
257 public String getLookupId(BusinessObject businessObject) {
258 final LookupResultsSupportStrategyService supportService = getQualifingSupportStrategy(businessObject.getClass());
259 if (supportService == null) {
260 return null;
261 }
262 return supportService.getLookupIdForBusinessObject(businessObject);
263 }
264
265 public BusinessObjectService getBusinessObjectService() {
266 return businessObjectService;
267 }
268
269 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
270 this.businessObjectService = businessObjectService;
271 }
272
273
274
275
276
277
278
279 protected boolean isAuthorizedToAccessMultipleValueLookupMetadata(MultipleValueLookupMetadata mvlm, String personId) {
280 return personId.equals(mvlm.getLookupPersonId());
281 }
282
283
284 @Override
285 public void deleteOldLookupResults(Timestamp expirationDate) {
286 persistedLookupMetadataDao.deleteOldLookupResults(expirationDate);
287
288 }
289
290 @Override
291 public void deleteOldSelectedObjectIds(Timestamp expirationDate) {
292 persistedLookupMetadataDao.deleteOldSelectedObjectIds(expirationDate);
293 }
294
295 public PersistedLookupMetadataDao getPersistedLookupMetadataDao() {
296 return persistedLookupMetadataDao;
297 }
298
299 public void setPersistedLookupMetadataDao(PersistedLookupMetadataDao persistedLookupMetadataDao) {
300 this.persistedLookupMetadataDao = persistedLookupMetadataDao;
301 }
302
303
304
305
306 public LookupResultsSupportStrategyService getPersistableBusinessObjectSupportStrategy() {
307 return this.persistableBusinessObjectSupportStrategy;
308 }
309
310
311
312
313 public LookupResultsSupportStrategyService getDataDictionarySupportStrategy() {
314 return this.dataDictionarySupportStrategy;
315 }
316
317
318
319
320 public void setPersistableBusinessObjectSupportStrategy(
321 LookupResultsSupportStrategyService persistableBusinessObjectSupportStrategy) {
322 this.persistableBusinessObjectSupportStrategy = persistableBusinessObjectSupportStrategy;
323 }
324
325
326
327
328 public void setDataDictionarySupportStrategy(
329 LookupResultsSupportStrategyService dataDictionarySupportStrategy) {
330 this.dataDictionarySupportStrategy = dataDictionarySupportStrategy;
331 }
332
333 }
334