001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.lookup;
017:
018: import java.sql.Timestamp;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.apache.commons.codec.binary.Base64;
027: import org.kuali.RiceConstants;
028: import org.kuali.RicePropertyConstants;
029: import org.kuali.core.bo.LookupResults;
030: import org.kuali.core.bo.MultipleValueLookupMetadata;
031: import org.kuali.core.bo.PersistableBusinessObject;
032: import org.kuali.core.bo.SelectedObjectIds;
033: import org.kuali.core.dao.PersistedLookupMetadataDao;
034: import org.kuali.core.exceptions.AuthorizationException;
035: import org.kuali.core.service.BusinessObjectService;
036: import org.kuali.core.util.ObjectUtils;
037: import org.kuali.core.web.ui.ResultRow;
038: import org.kuali.rice.KNSServiceLocator;
039: import org.springframework.transaction.annotation.Transactional;
040:
041: @Transactional
042: public class LookupResultsServiceImpl implements LookupResultsService {
043: private BusinessObjectService businessObjectService;
044: private PersistedLookupMetadataDao persistedLookupMetadataDao;
045:
046: /**
047: * @see org.kuali.core.lookup.LookupResultsService#persistResultsTable(java.lang.String, java.util.List, java.lang.String)
048: */
049: public void persistResultsTable(String lookupResultsSequenceNumber,
050: List<ResultRow> resultTable, String universalUserId)
051: throws Exception {
052: String resultTableString = new String(Base64
053: .encodeBase64(ObjectUtils.toByteArray(resultTable)));
054:
055: Timestamp now = KNSServiceLocator.getDateTimeService()
056: .getCurrentTimestamp();
057:
058: LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
059: if (lookupResults == null) {
060: lookupResults = new LookupResults();
061: lookupResults
062: .setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
063: }
064: lookupResults
065: .setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
066: lookupResults.setLookupUniversalUserId(universalUserId);
067: lookupResults.setSerializedLookupResults(resultTableString);
068: lookupResults.setLookupDate(now);
069: businessObjectService.save(lookupResults);
070: }
071:
072: /**
073: * @see org.kuali.core.lookup.LookupResultsService#persistSelectedObjectIds(java.lang.String, java.util.Set, java.lang.String)
074: */
075: public void persistSelectedObjectIds(
076: String lookupResultsSequenceNumber,
077: Set<String> selectedObjectIds, String universalUserId)
078: throws Exception {
079: SelectedObjectIds selectedObjectIdsBO = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
080: if (selectedObjectIdsBO == null) {
081: selectedObjectIdsBO = new SelectedObjectIds();
082: selectedObjectIdsBO
083: .setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
084: }
085: selectedObjectIdsBO
086: .setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
087: selectedObjectIdsBO.setLookupUniversalUserId(universalUserId);
088: selectedObjectIdsBO.setSelectedObjectIds(LookupUtils
089: .convertSetOfObjectIdsToString(selectedObjectIds));
090: selectedObjectIdsBO.setLookupDate(KNSServiceLocator
091: .getDateTimeService().getCurrentTimestamp());
092: businessObjectService.save(selectedObjectIdsBO);
093: }
094:
095: /**
096: * Retrieves the LookupResults BO with the given sequence number. Does not check authentication.
097: * @param lookupResultsSequenceNumber
098: * @return
099: * @throws Exception
100: */
101: protected LookupResults retrieveLookupResults(
102: String lookupResultsSequenceNumber) throws Exception {
103: Map<String, String> queryCriteria = new HashMap<String, String>();
104: queryCriteria.put(RiceConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER,
105: lookupResultsSequenceNumber);
106: LookupResults lookupResults = (LookupResults) businessObjectService
107: .findByPrimaryKey(LookupResults.class, queryCriteria);
108:
109: return lookupResults;
110: }
111:
112: /**
113: * Retrieves the SelectedObjectIds BO with the given sequence number. Does not check authentication.
114: * @param lookupResultsSequenceNumber
115: * @return
116: * @throws Exception
117: */
118: protected SelectedObjectIds retrieveSelectedObjectIds(
119: String lookupResultsSequenceNumber) throws Exception {
120: Map<String, String> queryCriteria = new HashMap<String, String>();
121: queryCriteria.put(RiceConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER,
122: lookupResultsSequenceNumber);
123: SelectedObjectIds selectedObjectIds = (SelectedObjectIds) businessObjectService
124: .findByPrimaryKey(SelectedObjectIds.class,
125: queryCriteria);
126:
127: return selectedObjectIds;
128: }
129:
130: /**
131: * @see org.kuali.core.lookup.LookupResultsService#isAuthorizedToAccessLookupResults(java.lang.String, java.lang.String)
132: */
133: public boolean isAuthorizedToAccessLookupResults(
134: String lookupResultsSequenceNumber, String universalUserId) {
135: try {
136: LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
137: return isAuthorizedToAccessLookupResults(lookupResults,
138: universalUserId);
139: } catch (Exception e) {
140: return false;
141: }
142: }
143:
144: /**
145: * Returns whether the user ID parameter is allowed to view the results.
146: *
147: * @param lookupResults
148: * @param universalUserId
149: * @return
150: */
151: protected boolean isAuthorizedToAccessLookupResults(
152: LookupResults lookupResults, String universalUserId) {
153: return isAuthorizedToAccessMultipleValueLookupMetadata(
154: lookupResults, universalUserId);
155: }
156:
157: /**
158: * @see org.kuali.core.lookup.LookupResultsService#isAuthorizedToAccessSelectedObjectIds(java.lang.String, java.lang.String)
159: */
160: public boolean isAuthorizedToAccessSelectedObjectIds(
161: String lookupResultsSequenceNumber, String universalUserId) {
162: try {
163: SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
164: return isAuthorizedToAccessSelectedObjectIds(
165: selectedObjectIds, universalUserId);
166: } catch (Exception e) {
167: return false;
168: }
169: }
170:
171: /**
172: * Returns whether the user ID parameter is allowed to view the selected object IDs
173: *
174: * @param selectedObjectIds
175: * @param universalUserId
176: * @return
177: */
178: protected boolean isAuthorizedToAccessSelectedObjectIds(
179: SelectedObjectIds selectedObjectIds, String universalUserId) {
180: return isAuthorizedToAccessMultipleValueLookupMetadata(
181: selectedObjectIds, universalUserId);
182: }
183:
184: /**
185: * @see org.kuali.core.lookup.LookupResultsService#retrieveResultsTable(java.lang.String, java.lang.String)
186: */
187: public List<ResultRow> retrieveResultsTable(
188: String lookupResultsSequenceNumber, String universalUserId)
189: throws Exception {
190: LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
191: if (!isAuthorizedToAccessLookupResults(lookupResults,
192: universalUserId)) {
193: // TODO: use the other identifier
194: throw new AuthorizationException(universalUserId,
195: "retrieve lookup results",
196: "lookup sequence number "
197: + lookupResultsSequenceNumber);
198: }
199: List<ResultRow> resultTable = (List<ResultRow>) ObjectUtils
200: .fromByteArray(Base64.decodeBase64(lookupResults
201: .getSerializedLookupResults().getBytes()));
202: return resultTable;
203: }
204:
205: /**
206: * Returns a list of BOs that were selected.
207: *
208: * This implementation makes an attempt to retrieve all BOs with the given object IDs, unless they have been deleted or the object ID changed.
209: * Since data may have changed since the search, the returned BOs may not match the criteria used to search.
210: *
211: * @see org.kuali.core.lookup.LookupResultsService#retrieveSelectedResultBOs(java.lang.String, java.lang.Class, java.lang.String)
212: */
213: public Collection<PersistableBusinessObject> retrieveSelectedResultBOs(
214: String lookupResultsSequenceNumber, Class boClass,
215: String universalUserId) throws Exception {
216: SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
217:
218: if (!isAuthorizedToAccessSelectedObjectIds(selectedObjectIds,
219: universalUserId)) {
220: // TODO: use the other identifier
221: throw new AuthorizationException(universalUserId,
222: "retrieve lookup results",
223: "lookup sequence number "
224: + lookupResultsSequenceNumber);
225: }
226:
227: Set<String> setOfSelectedObjIds = LookupUtils
228: .convertStringOfObjectIdsToSet(selectedObjectIds
229: .getSelectedObjectIds());
230:
231: if (setOfSelectedObjIds.isEmpty()) {
232: // OJB throws exception if querying on empty set
233: return new ArrayList<PersistableBusinessObject>();
234: }
235: Map<String, Collection<String>> queryCriteria = new HashMap<String, Collection<String>>();
236: queryCriteria.put(RicePropertyConstants.OBJECT_ID,
237: setOfSelectedObjIds);
238: return businessObjectService.findMatching(boClass,
239: queryCriteria);
240: }
241:
242: /**
243: * @see org.kuali.core.lookup.LookupResultsService#clearPersistedLookupResults(java.lang.String)
244: */
245: public void clearPersistedLookupResults(
246: String lookupResultsSequenceNumber) throws Exception {
247: LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
248: if (lookupResults != null) {
249: businessObjectService.delete(lookupResults);
250: }
251: }
252:
253: /**
254: * @see org.kuali.core.lookup.LookupResultsService#clearPersistedSelectedObjectIds(java.lang.String)
255: */
256: public void clearPersistedSelectedObjectIds(
257: String lookupResultsSequenceNumber) throws Exception {
258: SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
259: if (selectedObjectIds != null) {
260: businessObjectService.delete(selectedObjectIds);
261: }
262: }
263:
264: public BusinessObjectService getBusinessObjectService() {
265: return businessObjectService;
266: }
267:
268: public void setBusinessObjectService(
269: BusinessObjectService businessObjectService) {
270: this .businessObjectService = businessObjectService;
271: }
272:
273: /**
274: * Determines whether the passed in user ID is allowed to view the lookup metadata (object IDs or results table)
275: * @param mvlm
276: * @param universalUserId
277: * @return
278: */
279: protected boolean isAuthorizedToAccessMultipleValueLookupMetadata(
280: MultipleValueLookupMetadata mvlm, String universalUserId) {
281: return universalUserId.equals(mvlm.getLookupUniversalUserId());
282: }
283:
284: public void deleteOldLookupResults(Timestamp expirationDate) {
285: persistedLookupMetadataDao
286: .deleteOldLookupResults(expirationDate);
287:
288: }
289:
290: public void deleteOldSelectedObjectIds(Timestamp expirationDate) {
291: persistedLookupMetadataDao
292: .deleteOldSelectedObjectIds(expirationDate);
293: }
294:
295: public PersistedLookupMetadataDao getPersistedLookupMetadataDao() {
296: return persistedLookupMetadataDao;
297: }
298:
299: public void setPersistedLookupMetadataDao(
300: PersistedLookupMetadataDao persistedLookupMetadataDao) {
301: this.persistedLookupMetadataDao = persistedLookupMetadataDao;
302: }
303: }
|