| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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.PersistableBusinessObject; |
| 31 | |
import org.kuali.rice.kns.bo.SelectedObjectIds; |
| 32 | |
import org.kuali.rice.kns.dao.PersistedLookupMetadataDao; |
| 33 | |
import org.kuali.rice.kns.exception.AuthorizationException; |
| 34 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
| 35 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
| 36 | |
import org.kuali.rice.kns.util.KNSConstants; |
| 37 | |
import org.kuali.rice.kns.util.KNSPropertyConstants; |
| 38 | |
import org.kuali.rice.kns.util.ObjectUtils; |
| 39 | |
import org.kuali.rice.kns.web.ui.ResultRow; |
| 40 | |
|
| 41 | 0 | public class LookupResultsServiceImpl implements LookupResultsService { |
| 42 | |
private BusinessObjectService businessObjectService; |
| 43 | |
private PersistedLookupMetadataDao persistedLookupMetadataDao; |
| 44 | |
private LookupResultsSupportStrategyService persistableBusinessObjectSupportStrategy; |
| 45 | |
private LookupResultsSupportStrategyService dataDictionarySupportStrategy; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public void persistResultsTable(String lookupResultsSequenceNumber, List<ResultRow> resultTable, String personId) throws Exception { |
| 51 | 0 | String resultTableString = new String(Base64.encodeBase64(ObjectUtils.toByteArray(resultTable))); |
| 52 | |
|
| 53 | 0 | Timestamp now = KNSServiceLocator.getDateTimeService().getCurrentTimestamp(); |
| 54 | |
|
| 55 | 0 | LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber); |
| 56 | 0 | if (lookupResults == null) { |
| 57 | 0 | lookupResults = new LookupResults(); |
| 58 | 0 | lookupResults.setLookupResultsSequenceNumber(lookupResultsSequenceNumber); |
| 59 | |
} |
| 60 | 0 | lookupResults.setLookupResultsSequenceNumber(lookupResultsSequenceNumber); |
| 61 | 0 | lookupResults.setLookupPersonId(personId); |
| 62 | 0 | lookupResults.setSerializedLookupResults(resultTableString); |
| 63 | 0 | lookupResults.setLookupDate(now); |
| 64 | 0 | businessObjectService.save(lookupResults); |
| 65 | 0 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public void persistSelectedObjectIds(String lookupResultsSequenceNumber, Set<String> selectedObjectIds, String personId) throws Exception { |
| 71 | 0 | SelectedObjectIds selectedObjectIdsBO = retrieveSelectedObjectIds(lookupResultsSequenceNumber); |
| 72 | 0 | if (selectedObjectIdsBO == null) { |
| 73 | 0 | selectedObjectIdsBO = new SelectedObjectIds(); |
| 74 | 0 | selectedObjectIdsBO.setLookupResultsSequenceNumber(lookupResultsSequenceNumber); |
| 75 | |
} |
| 76 | 0 | selectedObjectIdsBO.setLookupResultsSequenceNumber(lookupResultsSequenceNumber); |
| 77 | 0 | selectedObjectIdsBO.setLookupPersonId(personId); |
| 78 | 0 | selectedObjectIdsBO.setSelectedObjectIds(LookupUtils.convertSetOfObjectIdsToString(selectedObjectIds)); |
| 79 | 0 | selectedObjectIdsBO.setLookupDate(KNSServiceLocator.getDateTimeService().getCurrentTimestamp()); |
| 80 | 0 | businessObjectService.save(selectedObjectIdsBO); |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
protected LookupResults retrieveLookupResults(String lookupResultsSequenceNumber) throws Exception { |
| 90 | 0 | Map<String, String> queryCriteria = new HashMap<String, String>(); |
| 91 | 0 | queryCriteria.put(KNSConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, lookupResultsSequenceNumber); |
| 92 | 0 | LookupResults lookupResults = (LookupResults) businessObjectService.findByPrimaryKey(LookupResults.class, queryCriteria); |
| 93 | |
|
| 94 | 0 | return lookupResults; |
| 95 | |
} |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
protected SelectedObjectIds retrieveSelectedObjectIds(String lookupResultsSequenceNumber) throws Exception { |
| 104 | 0 | Map<String, String> queryCriteria = new HashMap<String, String>(); |
| 105 | 0 | queryCriteria.put(KNSConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, lookupResultsSequenceNumber); |
| 106 | 0 | SelectedObjectIds selectedObjectIds = (SelectedObjectIds) businessObjectService.findByPrimaryKey(SelectedObjectIds.class, queryCriteria); |
| 107 | |
|
| 108 | 0 | return selectedObjectIds; |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public boolean isAuthorizedToAccessLookupResults(String lookupResultsSequenceNumber, String personId) { |
| 115 | |
try { |
| 116 | 0 | LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber); |
| 117 | 0 | return isAuthorizedToAccessLookupResults(lookupResults, personId); |
| 118 | |
} |
| 119 | 0 | catch (Exception e) { |
| 120 | 0 | return false; |
| 121 | |
} |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
protected boolean isAuthorizedToAccessLookupResults(LookupResults lookupResults, String personId) { |
| 132 | 0 | return isAuthorizedToAccessMultipleValueLookupMetadata(lookupResults, personId); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public boolean isAuthorizedToAccessSelectedObjectIds(String lookupResultsSequenceNumber, String personId) { |
| 139 | |
try { |
| 140 | 0 | SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber); |
| 141 | 0 | return isAuthorizedToAccessSelectedObjectIds(selectedObjectIds, personId); |
| 142 | |
} |
| 143 | 0 | catch (Exception e) { |
| 144 | 0 | return false; |
| 145 | |
} |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
protected boolean isAuthorizedToAccessSelectedObjectIds(SelectedObjectIds selectedObjectIds, String personId) { |
| 156 | 0 | return isAuthorizedToAccessMultipleValueLookupMetadata(selectedObjectIds, personId); |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public List<ResultRow> retrieveResultsTable(String lookupResultsSequenceNumber, String personId) throws Exception { |
| 164 | 0 | LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber); |
| 165 | 0 | if (!isAuthorizedToAccessLookupResults(lookupResults, personId)) { |
| 166 | |
|
| 167 | 0 | throw new AuthorizationException(personId, "retrieve lookup results", "lookup sequence number " + lookupResultsSequenceNumber); |
| 168 | |
} |
| 169 | 0 | List<ResultRow> resultTable = (List<ResultRow>) ObjectUtils.fromByteArray(Base64.decodeBase64(lookupResults.getSerializedLookupResults().getBytes())); |
| 170 | 0 | return resultTable; |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
public <T extends BusinessObject> Collection<T> retrieveSelectedResultBOs(String lookupResultsSequenceNumber, Class<T> boClass, String personId) throws Exception { |
| 179 | 0 | final LookupResultsSupportStrategyService supportService = getQualifingSupportStrategy(boClass); |
| 180 | 0 | if (supportService == null) { |
| 181 | 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"); |
| 182 | |
} |
| 183 | |
|
| 184 | 0 | SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber); |
| 185 | |
|
| 186 | 0 | if (!isAuthorizedToAccessSelectedObjectIds(selectedObjectIds, personId)) { |
| 187 | |
|
| 188 | 0 | throw new AuthorizationException(personId, "retrieve lookup results", "lookup sequence number " + lookupResultsSequenceNumber); |
| 189 | |
} |
| 190 | |
|
| 191 | 0 | Set<String> setOfSelectedObjIds = LookupUtils.convertStringOfObjectIdsToSet(selectedObjectIds.getSelectedObjectIds()); |
| 192 | |
|
| 193 | 0 | if (setOfSelectedObjIds.isEmpty()) { |
| 194 | |
|
| 195 | 0 | return new ArrayList<T>(); |
| 196 | |
} |
| 197 | |
|
| 198 | 0 | return supportService.retrieveSelectedResultBOs(boClass, setOfSelectedObjIds); |
| 199 | |
} |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
protected LookupResultsSupportStrategyService getQualifingSupportStrategy(Class boClass) { |
| 208 | 0 | if (getPersistableBusinessObjectSupportStrategy().qualifiesForStrategy(boClass)) { |
| 209 | 0 | return getPersistableBusinessObjectSupportStrategy(); |
| 210 | 0 | } else if (getDataDictionarySupportStrategy().qualifiesForStrategy(boClass)) { |
| 211 | 0 | return getDataDictionarySupportStrategy(); |
| 212 | |
} |
| 213 | 0 | return null; |
| 214 | |
} |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
public void clearPersistedLookupResults(String lookupResultsSequenceNumber) throws Exception { |
| 220 | 0 | LookupResults lookupResults = retrieveLookupResults(lookupResultsSequenceNumber); |
| 221 | 0 | if (lookupResults != null) { |
| 222 | 0 | businessObjectService.delete(lookupResults); |
| 223 | |
} |
| 224 | 0 | } |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
public void clearPersistedSelectedObjectIds(String lookupResultsSequenceNumber) throws Exception { |
| 230 | 0 | SelectedObjectIds selectedObjectIds = retrieveSelectedObjectIds(lookupResultsSequenceNumber); |
| 231 | 0 | if (selectedObjectIds != null) { |
| 232 | 0 | businessObjectService.delete(selectedObjectIds); |
| 233 | |
} |
| 234 | 0 | } |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
public String getLookupId(BusinessObject businessObject) { |
| 241 | 0 | final LookupResultsSupportStrategyService supportService = getQualifingSupportStrategy(businessObject.getClass()); |
| 242 | 0 | if (supportService == null) { |
| 243 | 0 | return null; |
| 244 | |
} |
| 245 | 0 | return supportService.getLookupIdForBusinessObject(businessObject); |
| 246 | |
} |
| 247 | |
|
| 248 | |
public BusinessObjectService getBusinessObjectService() { |
| 249 | 0 | return businessObjectService; |
| 250 | |
} |
| 251 | |
|
| 252 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
| 253 | 0 | this.businessObjectService = businessObjectService; |
| 254 | 0 | } |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
protected boolean isAuthorizedToAccessMultipleValueLookupMetadata(MultipleValueLookupMetadata mvlm, String personId) { |
| 263 | 0 | return personId.equals(mvlm.getLookupPersonId()); |
| 264 | |
} |
| 265 | |
|
| 266 | |
|
| 267 | |
public void deleteOldLookupResults(Timestamp expirationDate) { |
| 268 | 0 | persistedLookupMetadataDao.deleteOldLookupResults(expirationDate); |
| 269 | |
|
| 270 | 0 | } |
| 271 | |
|
| 272 | |
public void deleteOldSelectedObjectIds(Timestamp expirationDate) { |
| 273 | 0 | persistedLookupMetadataDao.deleteOldSelectedObjectIds(expirationDate); |
| 274 | 0 | } |
| 275 | |
|
| 276 | |
public PersistedLookupMetadataDao getPersistedLookupMetadataDao() { |
| 277 | 0 | return persistedLookupMetadataDao; |
| 278 | |
} |
| 279 | |
|
| 280 | |
public void setPersistedLookupMetadataDao(PersistedLookupMetadataDao persistedLookupMetadataDao) { |
| 281 | 0 | this.persistedLookupMetadataDao = persistedLookupMetadataDao; |
| 282 | 0 | } |
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
public LookupResultsSupportStrategyService getPersistableBusinessObjectSupportStrategy() { |
| 288 | 0 | return this.persistableBusinessObjectSupportStrategy; |
| 289 | |
} |
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
public LookupResultsSupportStrategyService getDataDictionarySupportStrategy() { |
| 295 | 0 | return this.dataDictionarySupportStrategy; |
| 296 | |
} |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
public void setPersistableBusinessObjectSupportStrategy( |
| 302 | |
LookupResultsSupportStrategyService persistableBusinessObjectSupportStrategy) { |
| 303 | 0 | this.persistableBusinessObjectSupportStrategy = persistableBusinessObjectSupportStrategy; |
| 304 | 0 | } |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
public void setDataDictionarySupportStrategy( |
| 310 | |
LookupResultsSupportStrategyService dataDictionarySupportStrategy) { |
| 311 | 0 | this.dataDictionarySupportStrategy = dataDictionarySupportStrategy; |
| 312 | 0 | } |
| 313 | |
|
| 314 | |
} |
| 315 | |
|