View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Used for segemented lookup results
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       * Retrieve the Date Time Service
55       * 
56       * @return Date Time Service
57       */
58      public DateTimeService getDateTimeService() {
59          return dateTimeService;
60      }
61  
62      /**
63       * Assign the Date Time Service
64       * 
65       * @param dateTimeService
66       */
67      public void setDateTimeService(DateTimeService dateTimeService) {
68          this.dateTimeService = dateTimeService;
69      }
70  
71      /**
72       * @see org.kuali.rice.kns.lookup.LookupResultsService#persistResultsTable(java.lang.String, java.util.List, java.lang.String)
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       * @see org.kuali.rice.kns.lookup.LookupResultsService#persistSelectedObjectIds(java.lang.String, java.util.Set, java.lang.String)
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      * @see org.kuali.rice.kns.lookup.LookupResultsService#retrieveResultsTable(java.lang.String, java.lang.String)
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      * Returns a list of BOs that were selected. This implementation makes an attempt to retrieve all BOs with the given object IDs,
124      * unless they have been deleted or the object ID changed. Since data may have changed since the search, the returned BOs may
125      * not match the criteria used to search.
126      * 
127      * @see org.kuali.rice.kns.lookup.LookupResultsService#retrieveSelectedResultBOs(java.lang.String, java.lang.Class,
128      *      java.lang.String)
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      * @param lookupResultsSequenceNumber
137      * @param personId
138      * @return Set<String>
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      * @param lookupResultsSequenceNumber
151      * @param setOfSelectedObjIds
152      * @param boClass
153      * @param personId
154      * @return Collection<PersistableBusinessObject>
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             // OJB throws exception if querying on empty set
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