View Javadoc
1   /**
2    * Copyright 2005-2015 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.rule;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.kuali.rice.kew.api.rule.RoleName;
23  import org.kuali.rice.kew.engine.RouteContext;
24  import org.kuali.rice.kew.routeheader.DocumentContent;
25  
26  
27  /**
28   * A simple base RoleAttribute implementation for roles that do not need to be qualified
29   * prior to resolution.
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public abstract class UnqualifiedRoleAttribute extends AbstractRoleAttribute {
34  
35      private static final long serialVersionUID = -356582375961050905L;
36  	protected List<RoleName> roles;
37  
38      /**
39       * No-arg constructor for subclasses that will override {@link #getRoleNames()} to provide their own roles list
40       */
41      public UnqualifiedRoleAttribute() {
42          roles = Collections.emptyList();
43      }
44  
45      /**
46       * Constructor for subclasses that can provide a role list at construction time
47       */
48      public UnqualifiedRoleAttribute(List<RoleName> roles) {
49          this.roles = roles;
50      }
51  
52      public List<RoleName> getRoleNames() {
53          return roles;
54      }
55  
56      /**
57       * Returns a List<String> containing only the roleName parameter; i.e. no qualification occurs
58       */
59      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
60          List<String> qualifiedRoleName = new ArrayList<String>(1);
61          qualifiedRoleName.add(roleName);
62          return qualifiedRoleName;
63      }
64  
65      /**
66       * Helper method for parsing the actual role name out from the class/rolename combination
67       * as Role class combines the two and does expose the original role name
68       * @param classAndRole the class and role string (e.g. org.blah.MyRoleAttribute!SOME_ROLE_NAME)
69       * @return the role name portion of the class and role string (e.g. SOME_ROLE_NAME);
70       */
71      protected String parseRoleNameFromClassAndRole(String classAndRole) {
72          return classAndRole.substring(classAndRole.indexOf("!") + 1);
73      }
74  
75      /**
76       * @param roleName roleName to test
77       * @return whether the roleName specifies a role that this attribute can resolve
78       */
79      protected boolean isValidRoleName(String roleName) {
80          // this attribute should never be called to resolve any roles other than those it advertised as supporting!
81          boolean valid = false;
82          for (RoleName role: getRoleNames()) {
83              if (parseRoleNameFromClassAndRole(role.getName()).equals(roleName)) {
84                  valid = true;
85                  break;
86              }
87          }
88          return valid;
89      }
90  
91      public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
92          // some sanity checking
93          if (!roleName.equals(qualifiedRole)) {
94              throw new IllegalArgumentException("UnqualifiedRoleAttribute resolveQualifiedRole invoked with differing role and qualified role (they should be the same)");
95          }
96          if (!isValidRoleName(roleName)) {
97              throw new IllegalArgumentException("This attribute does not support the role: '" + roleName + "'");
98          }
99          return resolveRole(routeContext, roleName);
100     }
101 
102     /**
103      * Template method for subclasses to implement
104      * @param routeContext the RouteContext
105      * @param roleName the role name
106      * @return a ResolvedQualifiedRole
107      */
108     protected abstract ResolvedQualifiedRole resolveRole(RouteContext routeContext, String roleName);
109 }