Coverage Report - org.kuali.student.common.ui.server.gwt.AbstractDataService
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDataService
0%
0/136
0%
0/44
3.045
 
 1  
 package org.kuali.student.common.ui.server.gwt;
 2  
 
 3  
 import java.util.HashMap;
 4  
 import java.util.List;
 5  
 import java.util.Map;
 6  
 import java.util.UUID;
 7  
 
 8  
 import org.apache.commons.lang.StringUtils;
 9  
 import org.apache.log4j.Logger;
 10  
 import org.kuali.rice.kew.dto.DocumentDetailDTO;
 11  
 import org.kuali.rice.kew.service.WorkflowUtility;
 12  
 import org.kuali.rice.kim.bo.types.dto.AttributeSet;
 13  
 import org.kuali.rice.kim.service.IdentityManagementService;
 14  
 import org.kuali.student.common.assembly.data.Data;
 15  
 import org.kuali.student.common.assembly.data.Metadata;
 16  
 import org.kuali.student.common.assembly.transform.AuthorizationFilter;
 17  
 import org.kuali.student.common.assembly.transform.MetadataFilter;
 18  
 import org.kuali.student.common.assembly.transform.TransformFilter;
 19  
 import org.kuali.student.common.assembly.transform.TransformationManager;
 20  
 import org.kuali.student.common.assembly.transform.TransformFilter.TransformFilterAction;
 21  
 import org.kuali.student.common.dto.DtoConstants;
 22  
 import org.kuali.student.common.exceptions.DataValidationErrorException;
 23  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 24  
 import org.kuali.student.common.exceptions.OperationFailedException;
 25  
 import org.kuali.student.common.exceptions.VersionMismatchException;
 26  
 import org.kuali.student.common.rice.StudentIdentityConstants;
 27  
 import org.kuali.student.common.rice.authorization.PermissionType;
 28  
 import org.kuali.student.common.ui.client.service.DataSaveResult;
 29  
 import org.kuali.student.common.ui.shared.IdAttributes;
 30  
 import org.kuali.student.common.util.security.SecurityUtils;
 31  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 32  
 import org.kuali.student.core.assembly.transform.ProposalWorkflowFilter;
 33  
 import org.kuali.student.core.proposal.dto.ProposalInfo;
 34  
 import org.kuali.student.core.proposal.service.ProposalService;
 35  
 import org.springframework.transaction.annotation.Transactional;
 36  
 
 37  
 @Transactional(readOnly=true,noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class})
 38  0
 public abstract class AbstractDataService implements DataService{
 39  
 
 40  
         private static final long serialVersionUID = 1L;
 41  
 
 42  0
         final Logger LOG = Logger.getLogger(AbstractDataService.class);
 43  
 
 44  
         private TransformationManager transformationManager;
 45  
         
 46  
         private IdentityManagementService permissionService;
 47  
         
 48  
         private WorkflowUtility workflowUtilityService;
 49  
 
 50  
     //TODO: why do we have this reference in the base class????
 51  
         private ProposalService proposalService;
 52  
 
 53  
         @Override
 54  
         public Data getData(String id) throws OperationFailedException {
 55  0
                 Map<String, Object> filterProperties = getDefaultFilterProperties();
 56  0
                 filterProperties.put(TransformFilter.FILTER_ACTION, TransformFilterAction.GET);
 57  0
                 filterProperties.put(MetadataFilter.METADATA_ID_VALUE, id);
 58  
                 
 59  0
                 String dtoId = id;
 60  
                 //First check if this is a proposal id
 61  
         //TODO: Igor : Why do we check for this when getting the data for programs?
 62  
                 try{
 63  0
                         if (proposalService != null){
 64  0
                                 ProposalInfo proposalInfo = proposalService.getProposal(dtoId);
 65  0
                                 filterProperties.put(ProposalWorkflowFilter.PROPOSAL_INFO, proposalInfo);
 66  0
                                 dtoId = proposalInfo.getProposalReference().get(0);
 67  
                         }                        
 68  
 
 69  0
                         Object dto = get(dtoId);
 70  0
                         if (dto != null){
 71  0
                                 return transformationManager.transform(dto, getDtoClass().getName(), filterProperties);
 72  
                         }
 73  0
                 } catch(DoesNotExistException e){
 74  0
                         return null;
 75  0
                 } catch (Exception e) {
 76  0
                         LOG.error("Error getting data",e);
 77  0
                         throw new OperationFailedException("Error getting data",e);
 78  0
                 }
 79  0
                 return null;
 80  
         }
 81  
 
 82  
         @Override
 83  
         public Metadata getMetadata(String id, Map<String, String> attributes) {
 84  0
                 Map<String, Object> filterProperties = getDefaultFilterProperties();
 85  0
                 filterProperties.put(MetadataFilter.METADATA_ID_VALUE, id);
 86  
                 
 87  
                 //Place id attributes into filter properties
 88  0
                 String idType = (attributes != null? attributes.get(IdAttributes.ID_TYPE):null);
 89  0
                 String docType = (attributes != null ? attributes.get(StudentIdentityConstants.DOCUMENT_TYPE_NAME):null);
 90  0
                 String dtoState = (attributes != null ? attributes.get(DtoConstants.DTO_STATE):null);
 91  0
                 String dtoNextState = (attributes != null ? attributes.get(DtoConstants.DTO_NEXT_STATE):null);
 92  0
                 String workflowNode = (attributes != null ? attributes.get(DtoConstants.DTO_WORKFLOW_NODE):null);
 93  
                                 
 94  0
                 if (idType == null){
 95  0
                         filterProperties.remove(MetadataFilter.METADATA_ID_TYPE);
 96  
                 } else {
 97  0
                         filterProperties.put(MetadataFilter.METADATA_ID_TYPE, idType);
 98  
                 }
 99  
         
 100  0
                 if (docType == null){
 101  0
                         filterProperties.put(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE, getDefaultWorkflowDocumentType());
 102  
                 } else {
 103  0
                         filterProperties.put(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE, docType);
 104  
                 }
 105  
 
 106  0
                 if (dtoState != null){
 107  0
                         filterProperties.put(DtoConstants.DTO_STATE, dtoState);                        
 108  
                 }
 109  
                 
 110  0
                 if (dtoNextState != null){
 111  0
                         filterProperties.put(DtoConstants.DTO_NEXT_STATE, dtoNextState);                        
 112  
                 }
 113  
 
 114  0
                 if (workflowNode != null){
 115  0
                         filterProperties.put(DtoConstants.DTO_WORKFLOW_NODE, workflowNode);                        
 116  
                 }
 117  
 
 118  0
                 if (checkDocumentLevelPermissions()){
 119  0
                         filterProperties.put(AuthorizationFilter.DOC_LEVEL_PERM_CHECK, Boolean.TRUE.toString());
 120  
                 }
 121  
                 
 122  0
                 Metadata metadata = transformationManager.getMetadata(getDtoClass().getName(), filterProperties); 
 123  0
                 return metadata;
 124  
         }
 125  
 
 126  
         @Override
 127  
         @Transactional(readOnly=false,noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class})
 128  
         public DataSaveResult saveData(Data data) throws OperationFailedException, DataValidationErrorException, VersionMismatchException{
 129  0
                 Map<String, Object> filterProperties = getDefaultFilterProperties();
 130  0
                 filterProperties.put(TransformFilter.FILTER_ACTION, TransformFilterAction.SAVE);
 131  
                 
 132  0
                 DataSaveResult saveResult = new DataSaveResult();
 133  
                 try {
 134  
                         //Convert data object to dto object
 135  0
                         Object dto = transformationManager.transform(data, getDtoClass(), filterProperties);
 136  
                         
 137  
                         //This calls save method for DataService impl, which makes the needed service calls to persist dto
 138  
                         //The service call should do it's own validation, any errors will cause DataValidationErrorException
 139  
                         //and is handled in the catch below.
 140  0
                         dto = save(dto, filterProperties);
 141  
                         
 142  
                         //Validate saved data again to get validation warnings that may exist on the data
 143  0
                         List<ValidationResultInfo> validationResults = validate(dto);
 144  
                         
 145  
                         //Convert saved data object back to data object to send to UI
 146  0
                         Data persistedData = transformationManager.transform(dto, getDtoClass().getName(), filterProperties);                        
 147  
                         
 148  0
                         saveResult.setValue(persistedData);
 149  0
                         saveResult.setValidationResults(validationResults);                        
 150  0
                 }catch (DataValidationErrorException dvee){
 151  
                         //Throw the error, we need the the transaction to be rolled back when service throws an error.
 152  0
                         throw dvee;
 153  0
                 }catch (OperationFailedException ofe){
 154  0
                     throw ofe;
 155  0
                 }catch (VersionMismatchException vme){
 156  0
                     throw vme;
 157  0
                 }catch (Exception e) {
 158  0
                         LOG.error("Failed to save data",e);
 159  0
                         throw new OperationFailedException("Failed to save data",e);
 160  0
                 }
 161  
                 
 162  0
                 return saveResult;
 163  
         }
 164  
         
 165  
         
 166  
 
 167  
         @Override
 168  
         public List<ValidationResultInfo> validateData(Data data) throws OperationFailedException {
 169  
                 List<ValidationResultInfo> validationResults;
 170  
                 
 171  
                 try {
 172  0
                         Metadata metadata = transformationManager.getUnfilteredMetadata(getDtoClass().getName());
 173  0
                         Object dto = transformationManager.getMapper().convertFromData(data, getDtoClass(), metadata);
 174  0
                         validationResults = validate(dto);
 175  0
                 } catch (Exception e) {
 176  0
                         throw new OperationFailedException("Unable to validate data", e);
 177  0
                 }
 178  
 
 179  0
                 return validationResults;
 180  
         }
 181  
 
 182  
         @Override
 183  
         public Boolean isAuthorized(PermissionType type, Map<String,String> attributes) {
 184  0
                 String user = SecurityUtils.getCurrentPrincipalId();
 185  0
                 boolean result = false;
 186  0
                 if (checkDocumentLevelPermissions()) {
 187  0
                         if (type == null) {
 188  0
                                 return null;
 189  
                         }
 190  0
                         String namespaceCode = type.getPermissionNamespace();
 191  0
                         String permissionTemplateName = type.getPermissionTemplateName();
 192  
                         
 193  0
                         AttributeSet roleQuals = new AttributeSet();
 194  0
                         AttributeSet permissionDetails = new AttributeSet();
 195  
                         
 196  0
                         if (attributes != null) {
 197  
                                 //Determine permission details and role qualifiers to pass into permission service.
 198  
                                 //We will use same attributes for permission details and role qualifiers (never hurts to use more than needed)
 199  
                                 
 200  0
                                 if (proposalService != null){
 201  0
                                         ProposalInfo proposalInfo = null;
 202  
                                         try {
 203  
                                                 //Retrieve the proposal info provided the proposal id (passed in as KS_JEW_OBJECT_ID) or the workflow id
 204  0
                                                 if (attributes.containsKey(IdAttributes.IdType.KS_KEW_OBJECT_ID.toString())){
 205  0
                                                         proposalInfo = proposalService.getProposal(attributes.get(IdAttributes.IdType.KS_KEW_OBJECT_ID.toString()));
 206  0
                                                 } else if (attributes.containsKey(IdAttributes.IdType.DOCUMENT_ID.toString())){
 207  0
                                                         proposalInfo = proposalService.getProposalByWorkflowId(attributes.get(IdAttributes.IdType.DOCUMENT_ID.toString()));
 208  
                                                 }
 209  
                                                 
 210  
                                                 //Check if the route status is in the list of allowed statuses
 211  0
                                                 DocumentDetailDTO docDetail = getWorkflowUtilityService().getDocumentDetail(Long.parseLong(proposalInfo.getWorkflowId()));
 212  0
                                                 String routeStatusCode = docDetail.getDocRouteStatus(); 
 213  
 
 214  
                                                 //Populate attributes with additional attributes required for permission check
 215  0
                                                 if (proposalInfo != null){
 216  0
                                                         attributes.put(IdAttributes.IdType.KS_KEW_OBJECT_ID.toString(), proposalInfo.getId());
 217  0
                                                         attributes.put(IdAttributes.IdType.DOCUMENT_ID.toString(), proposalInfo.getWorkflowId());
 218  0
                                                         attributes.put(StudentIdentityConstants.DOCUMENT_TYPE_NAME, proposalInfo.getType());
 219  0
                                                         attributes.put(StudentIdentityConstants.ROUTE_STATUS_CODE, routeStatusCode);
 220  
                                                 }
 221  0
                                         } catch (Exception e){
 222  0
                                                 LOG.error("Could not retrieve proposal to determine permission qualifiers:" + e.toString());
 223  0
                                         }
 224  
                                 }
 225  
                                 
 226  0
                                 permissionDetails.putAll(attributes);
 227  
                                 
 228  
                                 //Put in additional random number for role qualifiers. This is to avoid this request from being cached. 
 229  
                                 //Might want to do this only for specific templates to take advantage of caching
 230  0
                                 attributes.put("RAND_NO_CACHE", UUID.randomUUID().toString());
 231  0
                                 roleQuals.putAll(attributes);
 232  
                         }
 233  0
                         if (StringUtils.isNotBlank(namespaceCode) && StringUtils.isNotBlank(permissionTemplateName)) {
 234  0
                                 LOG.info("Checking Permission '" + namespaceCode + "/" + permissionTemplateName + "' for user '" + user + "'");
 235  0
                                 result = getPermissionService().isAuthorizedByTemplateName(user, namespaceCode, permissionTemplateName, permissionDetails, roleQuals);
 236  
                         }
 237  
                         else {
 238  0
                                 LOG.info("Can not check Permission with namespace '" + namespaceCode + "' and template name '" + permissionTemplateName + "' for user '" + user + "'");
 239  0
                                 return Boolean.TRUE;
 240  
                         }
 241  0
                 }
 242  
                 else {
 243  0
                         LOG.info("Will not check for document level permissions. Defaulting authorization to true.");
 244  0
                         result = true;
 245  
                 }
 246  0
                 LOG.info("Result of authorization check for user '" + user + "': " + result);
 247  0
                 return Boolean.valueOf(result);
 248  
         }
 249  
         
 250  
         public Map<String, Object> getDefaultFilterProperties(){
 251  0
                 Map<String, Object> filterProperties = new HashMap<String,Object>();
 252  0
                 filterProperties.put(MetadataFilter.METADATA_ID_TYPE, StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID);
 253  0
                 filterProperties.put(ProposalWorkflowFilter.WORKFLOW_USER, SecurityUtils.getCurrentPrincipalId());
 254  
                 
 255  0
                 return filterProperties;
 256  
         }
 257  
         
 258  
         protected DataSaveResult _saveData(Data data, Map<String, Object> filterProperties) throws OperationFailedException{
 259  
                 try {
 260  0
                         filterProperties.put(MetadataFilter.METADATA_ID_VALUE, (String)data.query("id"));        
 261  
 
 262  0
                         Object dto = transformationManager.transform(data, getDtoClass(),filterProperties);
 263  0
                         dto = save(dto, filterProperties);
 264  
                                 
 265  0
                         Data persistedData = transformationManager.transform(dto,getDtoClass().getName(), filterProperties);
 266  0
                         return new DataSaveResult(null, persistedData);
 267  0
                 } catch (DataValidationErrorException dvee){
 268  0
                         return new DataSaveResult(dvee.getValidationResults(), null);
 269  0
                 } catch (Exception e) {
 270  0
                         LOG.error("Unable to save", e);
 271  0
                         throw new OperationFailedException("Unable to save");
 272  
                 }                
 273  
         }
 274  
         
 275  
         protected boolean checkDocumentLevelPermissions() {
 276  0
                 return false;
 277  
         }
 278  
 
 279  
 
 280  
 
 281  
         public TransformationManager getTransformationManager() {
 282  0
                 return transformationManager;
 283  
         }
 284  
 
 285  
         public void setTransformationManager(TransformationManager transformationManager) {
 286  0
                 this.transformationManager = transformationManager;
 287  0
         }
 288  
 
 289  
         public IdentityManagementService getPermissionService() {
 290  0
                 return permissionService;
 291  
         }
 292  
 
 293  
         public void setPermissionService(IdentityManagementService permissionService) {
 294  0
                 this.permissionService = permissionService;
 295  0
         }
 296  
         
 297  
         public ProposalService getProposalService() {
 298  0
                 return proposalService;
 299  
         }
 300  
 
 301  
         public void setProposalService(ProposalService proposalService) {
 302  0
                 this.proposalService = proposalService;
 303  0
         }
 304  
         
 305  
         public WorkflowUtility getWorkflowUtilityService() {
 306  0
                 return workflowUtilityService;
 307  
         }
 308  
 
 309  
         public void setWorkflowUtilityService(WorkflowUtility workflowUtilityService) {
 310  0
                 this.workflowUtilityService = workflowUtilityService;
 311  0
         }
 312  
 
 313  
         protected abstract String getDefaultWorkflowDocumentType();
 314  
         
 315  
         protected abstract String getDefaultMetaDataState();
 316  
         
 317  
         /**
 318  
          * Implement this method to make to make service call to get DTO object. The method is called
 319  
          * by the get(Data) method before it invokes transformationManager to convert DTO to a Data map 
 320  
          * 
 321  
          * @param id DTO id
 322  
          * @return the dto retrieved by calling the appropriate service method
 323  
          * @throws Exception
 324  
          */
 325  
         protected abstract Object get(String id) throws Exception;
 326  
         
 327  
         /**
 328  
          * Implement this method to make a service call to get DTO object. The method is called         
 329  
          * by the save(Data) method after it invokes transformationManager to convert Data map to DTO 
 330  
          * 
 331  
          * @param dto
 332  
          * @param properties
 333  
          * @return the persisted dto object
 334  
          * @throws Exception
 335  
          */
 336  
         protected abstract Object save(Object dto, Map<String, Object> properties) throws Exception;
 337  
         
 338  
         /**
 339  
          * Implement this method to make a service call to get DTO object. The method is called         
 340  
          * in the save(data) method before calling the save(dto,properties) method to validate the data
 341  
  
 342  
          * @param dto
 343  
          * @return
 344  
          * @throws Exception
 345  
          */
 346  
         protected abstract List<ValidationResultInfo> validate(Object dto) throws Exception;
 347  
 
 348  
         /**
 349  
          * Implement this method to return the type of the dto object.
 350  
          * 
 351  
          * @return The object type returned and expected by the get & save dto methods
 352  
          */
 353  
         protected abstract Class<?> getDtoClass();
 354  
 }