001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sec.document.validation.impl;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.sec.SecConstants;
023    import org.kuali.kfs.sec.SecKeyConstants;
024    import org.kuali.kfs.sec.SecPropertyConstants;
025    import org.kuali.kfs.sec.businessobject.SecurityAttributeMetadata;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.rice.kns.service.BusinessObjectService;
028    import org.kuali.rice.kns.util.GlobalVariables;
029    
030    
031    /**
032     * Contains some common validation logic
033     */
034    public class SecurityValidationUtil {
035    
036        /**
037         * Validates the given value exist for the attribute. SECURITY_ATTRIBUTE_METADATA_MAP maps the attribute to the business object class and primitive key field need to do the
038         * existence search.
039         * 
040         * @param attributeName name of attribute for value
041         * @param attributeValue the value to validate
042         * @param errorKeyPrefix prefix for error key if the value does not exist
043         * @return boolean true if the value exist, false if it does not
044         */
045        public static boolean validateAttributeValue(String attributeName, String attributeValue, String errorKeyPrefix) {
046            boolean isValid = true;
047    
048            if (!SecConstants.SECURITY_ATTRIBUTE_METADATA_MAP.containsKey(attributeName)) {
049                return isValid;
050            }
051            SecurityAttributeMetadata attributeMetadata = (SecurityAttributeMetadata) SecConstants.SECURITY_ATTRIBUTE_METADATA_MAP.get(attributeName);
052    
053            String[] attributeValues;
054            if (StringUtils.contains(attributeValue, SecConstants.SecurityValueSpecialCharacters.MULTI_VALUE_SEPERATION_CHARACTER)) {
055                attributeValues = StringUtils.split(attributeValue, SecConstants.SecurityValueSpecialCharacters.MULTI_VALUE_SEPERATION_CHARACTER);
056            }
057            else {
058                attributeValues = new String[1];
059                attributeValues[0] = attributeValue;
060            }
061    
062            for (int i = 0; i < attributeValues.length; i++) {
063                if (!StringUtils.contains(attributeValues[i], SecConstants.SecurityValueSpecialCharacters.WILDCARD_CHARACTER)) {
064                    Map<String, String> searchValues = new HashMap<String, String>();
065                    searchValues.put(attributeMetadata.getAttributeField(), attributeValues[i]);
066    
067                    int matches = SpringContext.getBean(BusinessObjectService.class).countMatching(attributeMetadata.getAttributeClass(), searchValues);
068                    if (matches <= 0) {
069                        GlobalVariables.getMessageMap().putError(errorKeyPrefix + SecPropertyConstants.ATTRIBUTE_VALUE, SecKeyConstants.ERROR_ATTRIBUTE_VALUE_EXISTENCE, attributeValues[i], attributeName);
070                        isValid = false;
071                    }
072                }
073            }
074    
075            return isValid;
076        }
077    
078    }