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