1 /*
2 * Copyright 2005-2007 The Kuali Foundation
3 *
4 *
5 * Licensed under the Educational Community License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.opensource.org/licenses/ecl2.php
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.kuali.rice.kew.rule;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22
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<Role> 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<Role> roles) {
49 this.roles = roles;
50 }
51
52 public List<Role> 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 (Role 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 }