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