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.rule;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.rice.kew.api.identity.Id;
24  import org.kuali.rice.kew.api.identity.PrincipalId;
25  import org.kuali.rice.kew.api.rule.RoleName;
26  import org.kuali.rice.kew.engine.RouteContext;
27  import org.kuali.rice.kew.routeheader.DocumentContent;
28  
29  
30  /**
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class TestRuleAttribute implements WorkflowRuleAttribute, RoleAttribute, WorkflowAttributeXmlValidator {
34  
35  	private static final long serialVersionUID = -220808609566348066L;
36  
37  	public static boolean VALID_CLIENT_ROUTING_DATA_CALLED = false;
38  	
39  	private static Map<String, Map<String, List<String>>> roles = new HashMap<String, Map<String, List<String>>>();
40  	private boolean required;
41  		
42      public boolean isMatch(DocumentContent documentContent, List ruleExtensions) {
43          return true;
44      }
45      
46      public List getRoleNames() {
47          List roleNames = new ArrayList();
48          for (String roleName : roles.keySet()) {
49              roleNames.add(new RoleName(getClass().getName(), roleName, roleName));
50          }
51      	return roleNames;
52      }
53  
54      public List getRuleRows() {
55      	return new ArrayList();
56      }
57  
58      public List getRoutingDataRows() {
59      	return new ArrayList();
60      }
61  
62      public String getDocContent() {
63      	return "<testRuleAttributeContent/>";
64      }
65  
66      public List getRuleExtensionValues() {
67      	return new ArrayList();
68      }
69  
70      public List validateRoutingData(Map paramMap) {
71      	return new ArrayList();
72      }
73      
74      public String getAttributeLabel(){
75          return "";
76      }
77  
78      public List validateRuleData(Map paramMap) {
79      	return new ArrayList();
80      }
81  
82      public void setRequired(boolean required) {
83      	this.required = required;
84      }
85  
86      public boolean isRequired() {
87          return required;
88      }
89  
90  	public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
91  		List<String> qualifiedRoleNames = new ArrayList<String>();
92  		Map<String, List<String>> qualifiedRoles = roles.get(roleName);
93  		if (qualifiedRoles != null) {
94  			qualifiedRoleNames.addAll(qualifiedRoles.keySet());
95  		} else {
96  			throw new IllegalArgumentException("TestRuleAttribute does not support the given role " + roleName);
97  		}
98  		return qualifiedRoleNames;
99  	}
100 
101 	public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
102 		ResolvedQualifiedRole resolved = new ResolvedQualifiedRole();
103 		Map<String, List<String>> qualifiedRoles = (Map<String, List<String>>)roles.get(roleName);
104 		if (qualifiedRoles != null) {
105 			List<String> recipients = (List<String>)qualifiedRoles.get(qualifiedRole);
106 			if (recipients != null) {
107 				resolved.setQualifiedRoleLabel(qualifiedRole);
108 				resolved.setRecipients(convertPrincipalIdList(recipients));
109 			} else {
110 				throw new IllegalArgumentException("TestRuleAttribute does not support the qualified role " + qualifiedRole);
111 			}
112 		} else {
113 			throw new IllegalArgumentException("TestRuleAttribute does not support the given role " + roleName);
114 		}
115 		return resolved;
116 	}
117 	
118 	private static List<Id> convertPrincipalIdList(List<String> principalIds) {
119 		List<Id> idList = new ArrayList<Id>();
120 		for (String principalId : principalIds) {
121 			idList.add(new PrincipalId(principalId));
122 		}
123 		return idList;
124 	}
125 	
126 	public static void addRole(String roleName) {
127 		roles.put(roleName, new HashMap<String, List<String>>());
128 	}
129 	
130 	public static void removeRole(String roleName) {
131 		roles.remove(roleName);
132 	}
133 	
134 	public static Map<String, List<String>> getRole(String roleName) {
135 		return (Map<String, List<String>>)roles.get(roleName);
136 	}
137 	
138 	public static void addQualifiedRole(String roleName, String qualifiedRoleName) {
139 		getRole(roleName).put(qualifiedRoleName, new ArrayList<String>());
140 	}
141 	
142 	public static void removeQualifiedRole(String roleName, String qualifiedRoleName) {
143 		getRole(roleName).remove(qualifiedRoleName);
144 	}
145 	
146 	/**
147 	 * All you need to call now.  Simplies the previous 3 step process of adding role, qual role then recipients
148 	 * 
149 	 * @param roleName
150 	 * @param qualifiedRoleName
151 	 * @param recipients
152 	 */
153 	public static void setRecipientPrincipalIds(String roleName, String qualifiedRoleName, List<String> principalIds) {
154 		Map<String, List<String>> qualifiedRoles = getRole(roleName);
155 		if (qualifiedRoles == null) {
156 		    addRole(roleName);
157 		}
158 		if (qualifiedRoles == null) {
159 		    addRole(roleName);
160 		    addQualifiedRole(roleName, qualifiedRoleName);
161 		    qualifiedRoles = getRole(roleName);
162 		}
163 		qualifiedRoles.put(qualifiedRoleName, principalIds);
164 	}
165 	
166 	public static List<String> getRecipientPrincipalIds(String roleName, String qualifiedRoleName) {
167 		Map<String, List<String>> qualifiedRoles = getRole(roleName);
168 		return (List<String>)qualifiedRoles.get(qualifiedRoleName);
169 	}
170 
171 	public List<WorkflowAttributeValidationError> validateClientRoutingData() {
172 		return new ArrayList<WorkflowAttributeValidationError>();
173 	}
174 
175 }