View Javadoc

1   /**
2    * Copyright 2005-2011 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.kew.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
21  import org.kuali.rice.kew.api.extension.ExtensionDefinition;
22  import org.kuali.rice.kew.api.extension.ExtensionRepositoryService;
23  import org.kuali.rice.kew.api.extension.ExtensionUtils;
24  import org.kuali.rice.kew.api.validation.RuleValidationContext;
25  import org.kuali.rice.kew.api.validation.ValidationResults;
26  import org.kuali.rice.kew.framework.validation.RuleValidationAttributeExporterService;
27  import org.kuali.rice.kew.rule.RuleValidationAttribute;
28  import org.springframework.beans.factory.annotation.Required;
29  
30  /**
31   * RuleValidationAttributeExporterService reference impl.  Delegates to the ExtensionRepositoryService
32   * to load the custom RuleValidationAttribute.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class RuleValidationAttributeExporterServiceImpl implements RuleValidationAttributeExporterService {
37      private static final Logger LOG = Logger.getLogger(RuleValidationAttributeExporterServiceImpl.class);
38  
39      private ExtensionRepositoryService extensionRepositoryService;
40  
41      @Required
42      public void setExtensionRepositoryService(ExtensionRepositoryService extensionRepositoryService) {
43          this.extensionRepositoryService = extensionRepositoryService;
44      }
45      private ExtensionRepositoryService getExtensionRepositoryService() {
46          return this.extensionRepositoryService;
47      }
48  
49      @Override
50      public ValidationResults validate(String attributeName, RuleValidationContext validationContext) {
51          if (StringUtils.isBlank(attributeName)) {
52              throw new RiceIllegalArgumentException("attribute name was null or blank");
53          }
54          RuleValidationAttribute attr = loadAttribute(attributeName);
55          return attr.validate(validationContext);
56      }
57  
58      /**
59       * Loads RuleValidationAttribute implementation class via {@link ExtensionRepositoryService}
60       * @param attributeName the RuleValidationAttribute name
61       * @return instance of the RuleValidationAttribute implementation class
62       * @throws RiceIllegalArgumentException if specified attribute name cannot be found or loaded
63       */
64      protected RuleValidationAttribute loadAttribute(String attributeName) {
65          ExtensionDefinition extensionDefinition = getExtensionRepositoryService().getExtensionByName(attributeName);
66          if (extensionDefinition == null) {
67              throw new RiceIllegalArgumentException("Failed to locate a RuleValidationAttribute with the given name: " + attributeName);
68          }
69          RuleValidationAttribute attribute = ExtensionUtils.loadExtension(extensionDefinition);
70          if (attribute == null) {
71              throw new RiceIllegalArgumentException("Failed to load RuleValidationAttribute for: " + extensionDefinition);
72          }
73          return attribute;
74      }
75  }