1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.ole.sys.service.impl;
18
19 import java.sql.Timestamp;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.apache.commons.codec.binary.Base64;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.kuali.ole.sys.OLEPropertyConstants;
31 import org.kuali.ole.sys.service.SegmentedLookupResultsService;
32 import org.kuali.rice.core.api.datetime.DateTimeService;
33 import org.kuali.rice.kns.lookup.LookupResultsServiceImpl;
34 import org.kuali.rice.kns.lookup.LookupUtils;
35 import org.kuali.rice.kns.web.ui.ResultRow;
36 import org.kuali.rice.krad.bo.LookupResults;
37 import org.kuali.rice.krad.bo.PersistableBusinessObject;
38 import org.kuali.rice.krad.bo.SelectedObjectIds;
39 import org.kuali.rice.krad.exception.AuthorizationException;
40 import org.kuali.rice.krad.util.ObjectUtils;
41 import org.springframework.transaction.annotation.Transactional;
42
43
44
45
46 @Transactional
47 public class SegmentedLookupResultsServiceImpl extends LookupResultsServiceImpl implements SegmentedLookupResultsService {
48
49 private static final Log LOG = LogFactory.getLog(SegmentedLookupResultsServiceImpl.class);
50
51 private DateTimeService dateTimeService;
52
53
54
55
56
57
58 public DateTimeService getDateTimeService() {
59 return dateTimeService;
60 }
61
62
63
64
65
66
67 public void setDateTimeService(DateTimeService dateTimeService) {
68 this.dateTimeService = dateTimeService;
69 }
70
71
72
73
74 public void persistResultsTable(String lookupResultsSequenceNumber, List<ResultRow> resultTable, String personId) throws Exception {
75 String resultTableString = new String(Base64.encodeBase64(ObjectUtils.toByteArray(resultTable)));
76
77 Timestamp now = getDateTimeService().getCurrentTimestamp();
78
79 LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
80 if (lookupResults == null) {
81 lookupResults = new LookupResults();
82 lookupResults.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
83 }
84 lookupResults.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
85 lookupResults.setLookupPersonId(personId);
86 lookupResults.setSerializedLookupResults(resultTableString);
87 lookupResults.setLookupDate(now);
88 getBusinessObjectService().save(lookupResults);
89 if (LOG.isDebugEnabled()) {
90 LOG.debug("Wrote resultTable " + resultTable);
91 }
92 }
93
94
95
96
97 public void persistSelectedObjectIds(String lookupResultsSequenceNumber, Set<String> selectedObjectIds, String personId) throws Exception {
98 SelectedObjectIds selectedObjectIdsBO = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
99 if (selectedObjectIdsBO == null) {
100 selectedObjectIdsBO = new SelectedObjectIds();
101 selectedObjectIdsBO.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
102 }
103 selectedObjectIdsBO.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
104 selectedObjectIdsBO.setLookupPersonId(personId);
105 selectedObjectIdsBO.setSelectedObjectIds(LookupUtils.convertSetOfObjectIdsToString(selectedObjectIds));
106 selectedObjectIdsBO.setLookupDate(getDateTimeService().getCurrentTimestamp());
107 getBusinessObjectService().save(selectedObjectIdsBO);
108 }
109
110
111
112
113 public List<ResultRow> retrieveResultsTable(String lookupResultsSequenceNumber, String personId) throws Exception {
114 LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
115 if (!isAuthorizedToAccessLookupResults(lookupResults, personId)) {
116 throw new AuthorizationException(personId, "retrieve lookup results", "lookup sequence number " + lookupResultsSequenceNumber);
117 }
118 List<ResultRow> resultTable = (List<ResultRow>) ObjectUtils.fromByteArray(Base64.decodeBase64(lookupResults.getSerializedLookupResults().getBytes()));
119 return resultTable;
120 }
121
122
123
124
125
126
127
128
129
130 public Collection<PersistableBusinessObject> retrieveSelectedResultBOs(String lookupResultsSequenceNumber, Class boClass, String personId) throws Exception {
131 Set<String> setOfSelectedObjIds = retrieveSetOfSelectedObjectIds(lookupResultsSequenceNumber, personId);
132 return retrieveSelectedResultBOs(lookupResultsSequenceNumber, setOfSelectedObjIds, boClass, personId);
133 }
134
135
136
137
138
139
140 public Set<String> retrieveSetOfSelectedObjectIds(String lookupResultsSequenceNumber, String personId) throws Exception {
141 SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
142 if (!isAuthorizedToAccessSelectedObjectIds(selectedObjectIds, personId)) {
143 throw new AuthorizationException(personId, "retrieve lookup results", "lookup sequence number " + lookupResultsSequenceNumber);
144 }
145 Set<String> retval = LookupUtils.convertStringOfObjectIdsToSet(selectedObjectIds.getSelectedObjectIds());
146 return retval;
147 }
148
149
150
151
152
153
154
155
156 public Collection<PersistableBusinessObject> retrieveSelectedResultBOs(String lookupResultsSequenceNumber, Set<String> setOfSelectedObjIds, Class boClass, String personId) throws Exception {
157 if (LOG.isDebugEnabled()) {
158 LOG.debug("Retrieving results for class " + boClass + " with objectIds " + setOfSelectedObjIds);
159 }
160 if (setOfSelectedObjIds.isEmpty()) {
161
162 return new ArrayList<PersistableBusinessObject>();
163 }
164 Map<String, Collection<String>> queryCriteria = new HashMap<String, Collection<String>>();
165 queryCriteria.put(OLEPropertyConstants.OBJECT_ID, setOfSelectedObjIds);
166 return getBusinessObjectService().findMatching(boClass, queryCriteria);
167 }
168
169 }
170