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:
017: package org.kuali.module.labor.service.impl;
018:
019: import java.sql.Timestamp;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import org.apache.commons.codec.binary.Base64;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.kuali.core.bo.LookupResults;
031: import org.kuali.core.bo.PersistableBusinessObject;
032: import org.kuali.core.bo.SelectedObjectIds;
033: import org.kuali.core.exceptions.AuthorizationException;
034: import org.kuali.core.lookup.LookupResultsServiceImpl;
035: import org.kuali.core.lookup.LookupUtils;
036: import org.kuali.core.service.DateTimeService;
037: import org.kuali.core.util.ObjectUtils;
038: import org.kuali.core.web.ui.ResultRow;
039: import org.kuali.kfs.KFSPropertyConstants;
040: import org.kuali.module.labor.service.SegmentedLookupResultsService;
041: import org.springframework.transaction.annotation.Transactional;
042:
043: /**
044: * Used for segemented lookup results
045: */
046: @Transactional
047: public class SegmentedLookupResultsServiceImpl extends
048: LookupResultsServiceImpl implements
049: SegmentedLookupResultsService {
050:
051: private static final Log LOG = LogFactory
052: .getLog(SegmentedLookupResultsServiceImpl.class);
053:
054: private DateTimeService dateTimeService;
055:
056: /**
057: * Retrieve the Date Time Service
058: *
059: * @return Date Time Service
060: */
061: public DateTimeService getDateTimeService() {
062: return dateTimeService;
063: }
064:
065: /**
066: * Assign the Date Time Service
067: *
068: * @param dateTimeService
069: */
070: public void setDateTimeService(DateTimeService dateTimeService) {
071: this .dateTimeService = dateTimeService;
072: }
073:
074: /**
075: * @see org.kuali.core.lookup.LookupResultsService#persistResultsTable(java.lang.String, java.util.List, java.lang.String)
076: */
077: public void persistResultsTable(String lookupResultsSequenceNumber,
078: List<ResultRow> resultTable, String universalUserId)
079: throws Exception {
080: String resultTableString = new String(Base64
081: .encodeBase64(ObjectUtils.toByteArray(resultTable)));
082:
083: Timestamp now = getDateTimeService().getCurrentTimestamp();
084:
085: LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
086: if (lookupResults == null) {
087: lookupResults = new LookupResults();
088: lookupResults
089: .setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
090: }
091: lookupResults
092: .setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
093: lookupResults.setLookupUniversalUserId(universalUserId);
094: lookupResults.setSerializedLookupResults(resultTableString);
095: lookupResults.setLookupDate(now);
096: getBusinessObjectService().save(lookupResults);
097: LOG.debug("Wrote resultTable " + resultTable);
098: }
099:
100: /**
101: * @see org.kuali.core.lookup.LookupResultsService#persistSelectedObjectIds(java.lang.String, java.util.Set, java.lang.String)
102: */
103: public void persistSelectedObjectIds(
104: String lookupResultsSequenceNumber,
105: Set<String> selectedObjectIds, String universalUserId)
106: throws Exception {
107: SelectedObjectIds selectedObjectIdsBO = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
108: if (selectedObjectIdsBO == null) {
109: selectedObjectIdsBO = new SelectedObjectIds();
110: selectedObjectIdsBO
111: .setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
112: }
113: selectedObjectIdsBO
114: .setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
115: selectedObjectIdsBO.setLookupUniversalUserId(universalUserId);
116: selectedObjectIdsBO.setSelectedObjectIds(LookupUtils
117: .convertSetOfObjectIdsToString(selectedObjectIds));
118: selectedObjectIdsBO.setLookupDate(getDateTimeService()
119: .getCurrentTimestamp());
120: getBusinessObjectService().save(selectedObjectIdsBO);
121: }
122:
123: /**
124: * @see org.kuali.core.lookup.LookupResultsService#retrieveResultsTable(java.lang.String, java.lang.String)
125: */
126: public List<ResultRow> retrieveResultsTable(
127: String lookupResultsSequenceNumber, String universalUserId)
128: throws Exception {
129: LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
130: if (!isAuthorizedToAccessLookupResults(lookupResults,
131: universalUserId)) {
132: throw new AuthorizationException(universalUserId,
133: "retrieve lookup results",
134: "lookup sequence number "
135: + lookupResultsSequenceNumber);
136: }
137: List<ResultRow> resultTable = (List<ResultRow>) ObjectUtils
138: .fromByteArray(Base64.decodeBase64(lookupResults
139: .getSerializedLookupResults().getBytes()));
140: return resultTable;
141: }
142:
143: /**
144: * Returns a list of BOs that were selected. This implementation makes an attempt to retrieve all BOs with the given object IDs,
145: * unless they have been deleted or the object ID changed. Since data may have changed since the search, the returned BOs may
146: * not match the criteria used to search.
147: *
148: * @see org.kuali.core.lookup.LookupResultsService#retrieveSelectedResultBOs(java.lang.String, java.lang.Class,
149: * java.lang.String)
150: */
151: public Collection<PersistableBusinessObject> retrieveSelectedResultBOs(
152: String lookupResultsSequenceNumber, Class boClass,
153: String universalUserId) throws Exception {
154: Set<String> setOfSelectedObjIds = retrieveSetOfSelectedObjectIds(
155: lookupResultsSequenceNumber, universalUserId);
156: return retrieveSelectedResultBOs(lookupResultsSequenceNumber,
157: setOfSelectedObjIds, boClass, universalUserId);
158: }
159:
160: /**
161: * @param lookupResultsSequenceNumber
162: * @param universalUserId
163: * @return Set<String>
164: */
165: public Set<String> retrieveSetOfSelectedObjectIds(
166: String lookupResultsSequenceNumber, String universalUserId)
167: throws Exception {
168: SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
169: if (!isAuthorizedToAccessSelectedObjectIds(selectedObjectIds,
170: universalUserId)) {
171: throw new AuthorizationException(universalUserId,
172: "retrieve lookup results",
173: "lookup sequence number "
174: + lookupResultsSequenceNumber);
175: }
176: Set<String> retval = LookupUtils
177: .convertStringOfObjectIdsToSet(selectedObjectIds
178: .getSelectedObjectIds());
179: return retval;
180: }
181:
182: /**
183: * @param lookupResultsSequenceNumber
184: * @param setOfSelectedObjIds
185: * @param boClass
186: * @param universalUserId
187: * @return Collection<PersistableBusinessObject>
188: */
189: public Collection<PersistableBusinessObject> retrieveSelectedResultBOs(
190: String lookupResultsSequenceNumber,
191: Set<String> setOfSelectedObjIds, Class boClass,
192: String universalUserId) throws Exception {
193: LOG.debug("Retrieving results for class " + boClass
194: + " with objectIds " + setOfSelectedObjIds);
195: if (setOfSelectedObjIds.isEmpty()) {
196: // OJB throws exception if querying on empty set
197: return new ArrayList<PersistableBusinessObject>();
198: }
199: Map<String, Collection<String>> queryCriteria = new HashMap<String, Collection<String>>();
200: queryCriteria.put(KFSPropertyConstants.OBJECT_ID,
201: setOfSelectedObjIds);
202: return getBusinessObjectService().findMatching(boClass,
203: queryCriteria);
204: }
205:
206: }
|