Coverage Report - org.kuali.rice.kns.lookup.LookupResultsServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
LookupResultsServiceImpl
0%
0/90
0%
0/22
1.88
 
 1  
 /*
 2  
  * Copyright 2007-2008 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  
 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.kns.bo.BusinessObject;
 28  
 import org.kuali.rice.kns.bo.LookupResults;
 29  
 import org.kuali.rice.kns.bo.MultipleValueLookupMetadata;
 30  
 import org.kuali.rice.kns.bo.SelectedObjectIds;
 31  
 import org.kuali.rice.kns.dao.PersistedLookupMetadataDao;
 32  
 import org.kuali.rice.kns.exception.AuthorizationException;
 33  
 import org.kuali.rice.kns.service.BusinessObjectService;
 34  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 35  
 import org.kuali.rice.kns.util.KNSConstants;
 36  
 import org.kuali.rice.kns.util.ObjectUtils;
 37  
 import org.kuali.rice.kns.web.ui.ResultRow;
 38  
 
 39  0
 public class LookupResultsServiceImpl implements LookupResultsService {
 40  
     private BusinessObjectService businessObjectService;
 41  
     private PersistedLookupMetadataDao persistedLookupMetadataDao;
 42  
     private LookupResultsSupportStrategyService persistableBusinessObjectSupportStrategy;
 43  
     private LookupResultsSupportStrategyService dataDictionarySupportStrategy;
 44  
     
 45  
     /**
 46  
      * @see org.kuali.rice.kns.lookup.LookupResultsService#persistResultsTable(java.lang.String, java.util.List, java.lang.String)
 47  
      */
 48  
     public void persistResultsTable(String lookupResultsSequenceNumber, List<ResultRow> resultTable, String personId) throws Exception {
 49  0
         String resultTableString = new String(Base64.encodeBase64(ObjectUtils.toByteArray(resultTable)));
 50  
         
 51  0
         Timestamp now = KNSServiceLocator.getDateTimeService().getCurrentTimestamp();
 52  
         
 53  0
         LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
 54  0
         if (lookupResults == null) {
 55  0
             lookupResults = new LookupResults();
 56  0
             lookupResults.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
 57  
         }
 58  0
         lookupResults.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
 59  0
         lookupResults.setLookupPersonId(personId);
 60  0
         lookupResults.setSerializedLookupResults(resultTableString);
 61  0
         lookupResults.setLookupDate(now);
 62  0
         businessObjectService.save(lookupResults);
 63  0
     }
 64  
 
 65  
     /**
 66  
      * @see org.kuali.rice.kns.lookup.LookupResultsService#persistSelectedObjectIds(java.lang.String, java.util.Set, java.lang.String)
 67  
      */
 68  
     public void persistSelectedObjectIds(String lookupResultsSequenceNumber, Set<String> selectedObjectIds, String personId) throws Exception {
 69  0
         SelectedObjectIds selectedObjectIdsBO = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
 70  0
         if (selectedObjectIdsBO == null) {
 71  0
             selectedObjectIdsBO = new SelectedObjectIds();
 72  0
             selectedObjectIdsBO.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
 73  
         }
 74  0
         selectedObjectIdsBO.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
 75  0
         selectedObjectIdsBO.setLookupPersonId(personId);
 76  0
         selectedObjectIdsBO.setSelectedObjectIds(LookupUtils.convertSetOfObjectIdsToString(selectedObjectIds));
 77  0
         selectedObjectIdsBO.setLookupDate(KNSServiceLocator.getDateTimeService().getCurrentTimestamp());
 78  0
         businessObjectService.save(selectedObjectIdsBO);
 79  0
     }
 80  
 
 81  
     /**
 82  
      * Retrieves the LookupResults BO with the given sequence number.  Does not check authentication.
 83  
      * @param lookupResultsSequenceNumber
 84  
      * @return
 85  
      * @throws Exception
 86  
      */
 87  
     protected LookupResults retrieveLookupResults(String lookupResultsSequenceNumber) throws Exception {
 88  0
         Map<String, String> queryCriteria = new HashMap<String, String>();
 89  0
         queryCriteria.put(KNSConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, lookupResultsSequenceNumber);
 90  0
         LookupResults lookupResults = (LookupResults) businessObjectService.findByPrimaryKey(LookupResults.class, queryCriteria);
 91  
         
 92  0
         return lookupResults;
 93  
     }
 94  
 
 95  
     /**
 96  
      * Retrieves the SelectedObjectIds BO with the given sequence number.  Does not check authentication.
 97  
      * @param lookupResultsSequenceNumber
 98  
      * @return
 99  
      * @throws Exception
 100  
      */
 101  
     protected SelectedObjectIds retrieveSelectedObjectIds(String lookupResultsSequenceNumber) throws Exception {
 102  0
         Map<String, String> queryCriteria = new HashMap<String, String>();
 103  0
         queryCriteria.put(KNSConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, lookupResultsSequenceNumber);
 104  0
         SelectedObjectIds selectedObjectIds = (SelectedObjectIds) businessObjectService.findByPrimaryKey(SelectedObjectIds.class, queryCriteria);
 105  
         
 106  0
         return selectedObjectIds;
 107  
     }
 108  
 
 109  
     /**
 110  
      * @see org.kuali.rice.kns.lookup.LookupResultsService#isAuthorizedToAccessLookupResults(java.lang.String, java.lang.String)
 111  
      */
 112  
     public boolean isAuthorizedToAccessLookupResults(String lookupResultsSequenceNumber, String personId) {
 113  
         try {
 114  0
             LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
 115  0
             return isAuthorizedToAccessLookupResults(lookupResults, personId);
 116  
         }
 117  0
         catch (Exception e) {
 118  0
             return false;
 119  
         }
 120  
     }
 121  
 
 122  
     /**
 123  
      * Returns whether the user ID parameter is allowed to view the results.
 124  
      * 
 125  
      * @param lookupResults
 126  
      * @param personId
 127  
      * @return
 128  
      */
 129  
     protected boolean isAuthorizedToAccessLookupResults(LookupResults lookupResults, String personId) {
 130  0
         return isAuthorizedToAccessMultipleValueLookupMetadata(lookupResults, personId);
 131  
     }
 132  
 
 133  
     /**
 134  
      * @see org.kuali.rice.kns.lookup.LookupResultsService#isAuthorizedToAccessSelectedObjectIds(java.lang.String, java.lang.String)
 135  
      */
 136  
     public boolean isAuthorizedToAccessSelectedObjectIds(String lookupResultsSequenceNumber, String personId) {
 137  
         try {
 138  0
             SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
 139  0
             return isAuthorizedToAccessSelectedObjectIds(selectedObjectIds, personId);
 140  
         }
 141  0
         catch (Exception e) {
 142  0
             return false;
 143  
         }
 144  
     }
 145  
 
 146  
     /**
 147  
      * Returns whether the user ID parameter is allowed to view the selected object IDs
 148  
      * 
 149  
      * @param selectedObjectIds
 150  
      * @param personId
 151  
      * @return
 152  
      */
 153  
     protected boolean isAuthorizedToAccessSelectedObjectIds(SelectedObjectIds selectedObjectIds, String personId) {
 154  0
         return isAuthorizedToAccessMultipleValueLookupMetadata(selectedObjectIds, personId);
 155  
     }
 156  
     
 157  
 
 158  
     /**
 159  
      * @see org.kuali.rice.kns.lookup.LookupResultsService#retrieveResultsTable(java.lang.String, java.lang.String)
 160  
      */
 161  
     public List<ResultRow> retrieveResultsTable(String lookupResultsSequenceNumber, String personId) throws Exception {
 162  0
         LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
 163  0
         if (!isAuthorizedToAccessLookupResults(lookupResults, personId)) {
 164  
             // TODO: use the other identifier
 165  0
             throw new AuthorizationException(personId, "retrieve lookup results", "lookup sequence number " + lookupResultsSequenceNumber);
 166  
         }
 167  0
         List<ResultRow> resultTable = (List<ResultRow>) ObjectUtils.fromByteArray(Base64.decodeBase64(lookupResults.getSerializedLookupResults().getBytes()));
 168  0
         return resultTable;
 169  
     }
 170  
 
 171  
     /**
 172  
      * Figures out which support strategy to defer to and uses that service to retrieve the results; if the bo class doesn't qualify with any support strategy, an exception is thrown.  A nasty one, too.
 173  
      * 
 174  
      * @see org.kuali.rice.kns.lookup.LookupResultsService#retrieveSelectedResultBOs(java.lang.String, java.lang.Class, java.lang.String)
 175  
      */
 176  
     public <T extends BusinessObject> Collection<T> retrieveSelectedResultBOs(String lookupResultsSequenceNumber, Class<T> boClass, String personId) throws Exception {
 177  0
             final LookupResultsSupportStrategyService supportService = getQualifingSupportStrategy(boClass);
 178  0
             if (supportService == null) {
 179  0
                     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");
 180  
             }
 181  
             
 182  0
             SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
 183  
         
 184  0
         if (!isAuthorizedToAccessSelectedObjectIds(selectedObjectIds, personId)) {
 185  
             // TODO: use the other identifier
 186  0
             throw new AuthorizationException(personId, "retrieve lookup results", "lookup sequence number " + lookupResultsSequenceNumber);
 187  
         }
 188  
         
 189  0
         Set<String> setOfSelectedObjIds = LookupUtils.convertStringOfObjectIdsToSet(selectedObjectIds.getSelectedObjectIds());
 190  
         
 191  0
         if (setOfSelectedObjIds.isEmpty()) {
 192  
             // OJB throws exception if querying on empty set
 193  0
             return new ArrayList<T>();
 194  
         }
 195  
             
 196  0
             return supportService.retrieveSelectedResultBOs(boClass, setOfSelectedObjIds);
 197  
     }
 198  
     
 199  
     /**
 200  
      * Given the business object class, determines the best qualifying LookupResultsSupportStrategyService to use
 201  
      * 
 202  
      * @param boClass a business object class
 203  
      * @return an LookupResultsSupportStrategyService implementation, or null if no qualifying strategies could be found
 204  
      */
 205  
     protected LookupResultsSupportStrategyService getQualifingSupportStrategy(Class boClass) {
 206  0
             if (getPersistableBusinessObjectSupportStrategy().qualifiesForStrategy(boClass)) {
 207  0
                     return getPersistableBusinessObjectSupportStrategy();
 208  0
             } else if (getDataDictionarySupportStrategy().qualifiesForStrategy(boClass)) {
 209  0
                     return getDataDictionarySupportStrategy();
 210  
             }
 211  0
             return null;
 212  
     }
 213  
     
 214  
     /**
 215  
      * @see org.kuali.rice.kns.lookup.LookupResultsService#clearPersistedLookupResults(java.lang.String)
 216  
      */
 217  
     public void clearPersistedLookupResults(String lookupResultsSequenceNumber) throws Exception {
 218  0
         LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber);
 219  0
         if (lookupResults != null) {
 220  0
             businessObjectService.delete(lookupResults);
 221  
         }
 222  0
     }
 223  
     
 224  
     /**
 225  
      * @see org.kuali.rice.kns.lookup.LookupResultsService#clearPersistedSelectedObjectIds(java.lang.String)
 226  
      */
 227  
     public void clearPersistedSelectedObjectIds(String lookupResultsSequenceNumber) throws Exception {
 228  0
         SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber);
 229  0
         if (selectedObjectIds != null) {
 230  0
             businessObjectService.delete(selectedObjectIds);
 231  
         }
 232  0
     }
 233  
     
 234  
     /**
 235  
          * Figures out which LookupResultsServiceSupportStrategy to defer to, and uses that to get the lookup id
 236  
          * @see org.kuali.rice.kns.lookup.LookupResultsService#getLookupId(org.kuali.rice.kns.bo.BusinessObject)
 237  
          */
 238  
         public String getLookupId(BusinessObject businessObject) {
 239  0
                 final LookupResultsSupportStrategyService supportService = getQualifingSupportStrategy(businessObject.getClass());
 240  0
                 if (supportService == null) {
 241  0
                         return null; // this may happen quite often, so let's just return null - no exception here
 242  
                 }
 243  0
                 return supportService.getLookupIdForBusinessObject(businessObject);
 244  
         }
 245  
 
 246  
         public BusinessObjectService getBusinessObjectService() {
 247  0
         return businessObjectService;
 248  
     }
 249  
 
 250  
     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
 251  0
         this.businessObjectService = businessObjectService;
 252  0
     }
 253  
     
 254  
     /**
 255  
      * Determines whether the passed in user ID is allowed to view the lookup metadata (object IDs or results table)
 256  
      * @param mvlm
 257  
      * @param personId
 258  
      * @return
 259  
      */
 260  
     protected boolean isAuthorizedToAccessMultipleValueLookupMetadata(MultipleValueLookupMetadata mvlm, String personId) {
 261  0
         return personId.equals(mvlm.getLookupPersonId());
 262  
     }
 263  
 
 264  
     
 265  
     public void deleteOldLookupResults(Timestamp expirationDate) {
 266  0
         persistedLookupMetadataDao.deleteOldLookupResults(expirationDate);
 267  
         
 268  0
     }
 269  
 
 270  
     public void deleteOldSelectedObjectIds(Timestamp expirationDate) {
 271  0
         persistedLookupMetadataDao.deleteOldSelectedObjectIds(expirationDate);
 272  0
     }
 273  
 
 274  
     public PersistedLookupMetadataDao getPersistedLookupMetadataDao() {
 275  0
         return persistedLookupMetadataDao;
 276  
     }
 277  
 
 278  
     public void setPersistedLookupMetadataDao(PersistedLookupMetadataDao persistedLookupMetadataDao) {
 279  0
         this.persistedLookupMetadataDao = persistedLookupMetadataDao;
 280  0
     }
 281  
 
 282  
         /**
 283  
          * @return the persistableBusinessObjectSupportStrategy
 284  
          */
 285  
         public LookupResultsSupportStrategyService getPersistableBusinessObjectSupportStrategy() {
 286  0
                 return this.persistableBusinessObjectSupportStrategy;
 287  
         }
 288  
 
 289  
         /**
 290  
          * @return the dataDictionarySupportStrategy
 291  
          */
 292  
         public LookupResultsSupportStrategyService getDataDictionarySupportStrategy() {
 293  0
                 return this.dataDictionarySupportStrategy;
 294  
         }
 295  
 
 296  
         /**
 297  
          * @param persistableBusinessObjectSupportStrategy the persistableBusinessObjectSupportStrategy to set
 298  
          */
 299  
         public void setPersistableBusinessObjectSupportStrategy(
 300  
                         LookupResultsSupportStrategyService persistableBusinessObjectSupportStrategy) {
 301  0
                 this.persistableBusinessObjectSupportStrategy = persistableBusinessObjectSupportStrategy;
 302  0
         }
 303  
 
 304  
         /**
 305  
          * @param dataDictionarySupportStrategy the dataDictionarySupportStrategy to set
 306  
          */
 307  
         public void setDataDictionarySupportStrategy(
 308  
                         LookupResultsSupportStrategyService dataDictionarySupportStrategy) {
 309  0
                 this.dataDictionarySupportStrategy = dataDictionarySupportStrategy;
 310  0
         }
 311  
     
 312  
 }
 313