001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.validation;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
021import org.kuali.rice.kew.api.extension.ExtensionDefinition;
022import org.kuali.rice.kew.api.extension.ExtensionRepositoryService;
023import org.kuali.rice.kew.api.extension.ExtensionUtils;
024import org.kuali.rice.kew.api.validation.RuleValidationContext;
025import org.kuali.rice.kew.api.validation.ValidationResults;
026import org.kuali.rice.kew.framework.validation.RuleValidationAttributeExporterService;
027import org.kuali.rice.kew.rule.RuleValidationAttribute;
028import org.springframework.beans.factory.annotation.Required;
029
030/**
031 * RuleValidationAttributeExporterService reference impl.  Delegates to the ExtensionRepositoryService
032 * to load the custom RuleValidationAttribute.
033 *
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036public class RuleValidationAttributeExporterServiceImpl implements RuleValidationAttributeExporterService {
037    private static final Logger LOG = Logger.getLogger(RuleValidationAttributeExporterServiceImpl.class);
038
039    private ExtensionRepositoryService extensionRepositoryService;
040
041    @Required
042    public void setExtensionRepositoryService(ExtensionRepositoryService extensionRepositoryService) {
043        this.extensionRepositoryService = extensionRepositoryService;
044    }
045    private ExtensionRepositoryService getExtensionRepositoryService() {
046        return this.extensionRepositoryService;
047    }
048
049    @Override
050    public ValidationResults validate(String attributeName, RuleValidationContext validationContext) {
051        if (StringUtils.isBlank(attributeName)) {
052            throw new RiceIllegalArgumentException("attribute name was null or blank");
053        }
054        RuleValidationAttribute attr = loadAttribute(attributeName);
055        return attr.validate(validationContext);
056    }
057
058    /**
059     * Loads RuleValidationAttribute implementation class via {@link ExtensionRepositoryService}
060     * @param attributeName the RuleValidationAttribute name
061     * @return instance of the RuleValidationAttribute implementation class
062     * @throws RiceIllegalArgumentException if specified attribute name cannot be found or loaded
063     */
064    protected RuleValidationAttribute loadAttribute(String attributeName) {
065        ExtensionDefinition extensionDefinition = getExtensionRepositoryService().getExtensionByName(attributeName);
066        if (extensionDefinition == null) {
067            throw new RiceIllegalArgumentException("Failed to locate a RuleValidationAttribute with the given name: " + attributeName);
068        }
069        RuleValidationAttribute attribute = ExtensionUtils.loadExtension(extensionDefinition);
070        if (attribute == null) {
071            throw new RiceIllegalArgumentException("Failed to load RuleValidationAttribute for: " + extensionDefinition);
072        }
073        return attribute;
074    }
075}