001 /**
002 * Copyright 2005-2012 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.rice.kew.rule;
017
018 import java.util.ArrayList;
019 import java.util.Collections;
020 import java.util.List;
021
022 import org.kuali.rice.kew.api.rule.RoleName;
023 import org.kuali.rice.kew.engine.RouteContext;
024 import org.kuali.rice.kew.routeheader.DocumentContent;
025
026
027 /**
028 * A simple base RoleAttribute implementation for roles that do not need to be qualified
029 * prior to resolution.
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033 public abstract class UnqualifiedRoleAttribute extends AbstractRoleAttribute {
034
035 private static final long serialVersionUID = -356582375961050905L;
036 protected List<RoleName> roles;
037
038 /**
039 * No-arg constructor for subclasses that will override {@link #getRoleNames()} to provide their own roles list
040 */
041 public UnqualifiedRoleAttribute() {
042 roles = Collections.emptyList();
043 }
044
045 /**
046 * Constructor for subclasses that can provide a role list at construction time
047 */
048 public UnqualifiedRoleAttribute(List<RoleName> roles) {
049 this.roles = roles;
050 }
051
052 public List<RoleName> getRoleNames() {
053 return roles;
054 }
055
056 /**
057 * Returns a List<String> containing only the roleName parameter; i.e. no qualification occurs
058 */
059 public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
060 List<String> qualifiedRoleName = new ArrayList<String>(1);
061 qualifiedRoleName.add(roleName);
062 return qualifiedRoleName;
063 }
064
065 /**
066 * Helper method for parsing the actual role name out from the class/rolename combination
067 * as Role class combines the two and does expose the original role name
068 * @param classAndRole the class and role string (e.g. org.blah.MyRoleAttribute!SOME_ROLE_NAME)
069 * @return the role name portion of the class and role string (e.g. SOME_ROLE_NAME);
070 */
071 protected String parseRoleNameFromClassAndRole(String classAndRole) {
072 return classAndRole.substring(classAndRole.indexOf("!") + 1);
073 }
074
075 /**
076 * @param roleName roleName to test
077 * @return whether the roleName specifies a role that this attribute can resolve
078 */
079 protected boolean isValidRoleName(String roleName) {
080 // this attribute should never be called to resolve any roles other than those it advertised as supporting!
081 boolean valid = false;
082 for (RoleName role: getRoleNames()) {
083 if (parseRoleNameFromClassAndRole(role.getName()).equals(roleName)) {
084 valid = true;
085 break;
086 }
087 }
088 return valid;
089 }
090
091 public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
092 // some sanity checking
093 if (!roleName.equals(qualifiedRole)) {
094 throw new IllegalArgumentException("UnqualifiedRoleAttribute resolveQualifiedRole invoked with differing role and qualified role (they should be the same)");
095 }
096 if (!isValidRoleName(roleName)) {
097 throw new IllegalArgumentException("This attribute does not support the role: '" + roleName + "'");
098 }
099 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 }