Coverage Report - org.kuali.rice.kim.document.rule.GenericPermissionMaintenanceDocumentRule
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericPermissionMaintenanceDocumentRule
0%
0/59
0%
0/32
8.333
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.kim.document.rule;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.api.uif.RemotableAttributeError;
 20  
 import org.kuali.rice.core.api.util.RiceKeyConstants;
 21  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 22  
 import org.kuali.rice.kim.api.type.KimType;
 23  
 import org.kuali.rice.kim.bo.impl.GenericPermission;
 24  
 import org.kuali.rice.kim.bo.role.dto.KimPermissionTemplateInfo;
 25  
 import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
 26  
 import org.kuali.rice.kim.service.support.KimPermissionTypeService;
 27  
 import org.kuali.rice.kns.document.MaintenanceDocument;
 28  
 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
 29  
 import org.kuali.rice.krad.util.GlobalVariables;
 30  
 
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 import java.util.regex.Matcher;
 34  
 import java.util.regex.Pattern;
 35  
 
 36  
 /**
 37  
  * This is a description of what this class does - kellerj don't forget to fill this in. 
 38  
  * 
 39  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 40  
  *
 41  
  */
 42  0
 public class GenericPermissionMaintenanceDocumentRule extends MaintenanceDocumentRuleBase {
 43  
         protected static final String DETAIL_VALUES_PROPERTY = "detailValues";
 44  
         protected static final String ERROR_MESSAGE_PREFIX = "error.document.kim.genericpermission.";
 45  
         protected static final String ERROR_MISSING_TEMPLATE = ERROR_MESSAGE_PREFIX + "missingtemplate";
 46  
         protected static final String ERROR_UNKNOWN_ATTRIBUTE = ERROR_MESSAGE_PREFIX + "unknownattribute";
 47  
         protected static final String ERROR_ATTRIBUTE_VALIDATION = ERROR_MESSAGE_PREFIX + "attributevalidation";
 48  
         
 49  
         @Override
 50  
         protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
 51  0
                 boolean rulesPassed = true;
 52  
                 try {
 53  0
                         GenericPermission perm = (GenericPermission)getNewBo();
 54  0
                         validateDetailValuesFormat(perm.getDetailValues());
 55  
                         // detailValues
 56  
                         // get the type from the template for validation
 57  0
                         KimPermissionTemplateInfo template = KimApiServiceLocator.getPermissionService().getPermissionTemplate( perm.getTemplateId() );
 58  0
                         if ( template == null ) {
 59  0
                                 GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH );
 60  0
                                 GlobalVariables.getMessageMap().putError( DETAIL_VALUES_PROPERTY, ERROR_MISSING_TEMPLATE, perm.getTemplateId() );
 61  0
                                 GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH );
 62  0
                                 rulesPassed = false;
 63  
                         } else {
 64  0
                                 KimType kimType = KimApiServiceLocator.getKimTypeInfoService().getKimType(template.getKimTypeId());
 65  0
                                 Map<String, String> details = perm.getDetails();
 66  
                                 // check that add passed attributes are defined
 67  0
                                 for ( String attributeName : details.keySet() ) {
 68  0
                                         if ( kimType.getAttributeDefinitionByName(attributeName) == null ) {
 69  0
                                                 GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH );
 70  0
                                                 GlobalVariables.getMessageMap().putError( DETAIL_VALUES_PROPERTY, ERROR_UNKNOWN_ATTRIBUTE, attributeName, template.getNamespaceCode(), template.getName() );
 71  0
                                                 GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH );
 72  0
                                                 rulesPassed = false;
 73  
                                         }
 74  
                                 }
 75  
                                 // if all attributes are known, pass to the service for validation
 76  0
                                 if ( !GlobalVariables.getMessageMap().hasErrors() ) {
 77  0
                                         KimPermissionTypeService service = getPermissionTypeService( kimType.getServiceName() );
 78  0
                                         if ( service != null ) {
 79  0
                                                 List<RemotableAttributeError> validationErrors = service.validateAttributes( kimType.getId(), details);
 80  0
                                                 if ( validationErrors != null && !validationErrors.isEmpty() ) {
 81  0
                                                         for ( RemotableAttributeError error : validationErrors ) {
 82  0
                                                                 GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH );
 83  0
                                                                 for (String errMsg : error.getErrors()) {
 84  0
                                     GlobalVariables.getMessageMap().putError( DETAIL_VALUES_PROPERTY, ERROR_ATTRIBUTE_VALIDATION, error.getAttributeName(), errMsg );
 85  
                                 }
 86  0
                                                                 GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH );
 87  
                                                         }
 88  0
                                                         rulesPassed = false;
 89  
                                                 }
 90  
                                         }
 91  
                                 }
 92  
                         }
 93  
                         // check each permission name against the type
 94  0
                 } catch ( RuntimeException ex ) {
 95  0
                         LOG.error( "Error in processCustomRouteDocumentBusinessRules()", ex );
 96  0
                         throw ex;
 97  0
                 }
 98  0
                 return rulesPassed;
 99  
         }
 100  
 
 101  
         protected boolean validateDetailValuesFormat(String permissionDetailValues){
 102  0
                 if(permissionDetailValues != null){
 103  0
                         String spacesPattern = "[\\s\\t]*";
 104  0
                         Pattern pattern = Pattern.compile(".+"+"="+".+");
 105  
                         Matcher matcher;
 106  
                         // ensure that all line delimiters are single linefeeds
 107  0
                         permissionDetailValues = permissionDetailValues.replace( "\r\n", "\n" );
 108  0
                         permissionDetailValues = permissionDetailValues.replace( '\r', '\n' );
 109  0
                         if(StringUtils.isNotBlank(permissionDetailValues)){
 110  0
                                 String[] values = permissionDetailValues.split( "\n" );
 111  0
                                 for(String attrib: values){
 112  0
                                       matcher = pattern.matcher(attrib);
 113  0
                                       if(!matcher.matches()){
 114  0
                                               GlobalVariables.getMessageMap().putError(MAINTAINABLE_ERROR_PATH+"."+DETAIL_VALUES_PROPERTY, RiceKeyConstants.ERROR_INVALID_FORMAT, new String[]{"Detail Values", permissionDetailValues});
 115  0
                                               return false;
 116  
                                       }
 117  
                                 }
 118  
                         }
 119  
                 }
 120  0
                 return true;
 121  
         }
 122  
         
 123  
         protected KimPermissionTypeService getPermissionTypeService( String serviceName ) {
 124  0
             if ( StringUtils.isBlank( serviceName ) ) {
 125  0
                     return null;
 126  
             }
 127  
             try {
 128  0
                     Object service = KIMServiceLocatorInternal.getService(serviceName);
 129  
                     // if we have a service name, it must exist
 130  0
                     if ( service == null ) {
 131  0
                                 LOG.warn("null returned for permission type service for service name: " + serviceName);
 132  
                     } else {
 133  
                             // whatever we retrieved must be of the correct type
 134  0
                             if ( !(service instanceof KimPermissionTypeService)  ) {
 135  0
                                     LOG.warn( "Service " + serviceName + " was not a KimPermissionTypeService.  Was: " + service.getClass().getName() );
 136  0
                                     service = null;
 137  
                             }
 138  
                     }
 139  0
                     return (KimPermissionTypeService)service;
 140  0
             } catch( Exception ex ) {
 141  0
                     LOG.error( "Error retrieving service: " + serviceName + " from the KIMServiceLocatorInternal.", ex );
 142  
             }
 143  0
             return null;
 144  
     }
 145  
 
 146  
 }