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