View Javadoc

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