View Javadoc

1   /*
2    * Copyright 2007-2008 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.identity.service.impl;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.kuali.rice.core.exception.RiceRuntimeException;
21  import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
22  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
23  import org.kuali.rice.kew.actionrequest.Recipient;
24  import org.kuali.rice.kew.dto.EmplIdDTO;
25  import org.kuali.rice.kew.dto.NetworkIdDTO;
26  import org.kuali.rice.kew.dto.UserIdDTO;
27  import org.kuali.rice.kew.dto.WorkflowIdDTO;
28  import org.kuali.rice.kew.identity.service.IdentityHelperService;
29  import org.kuali.rice.kew.user.AuthenticationUserId;
30  import org.kuali.rice.kew.user.EmplId;
31  import org.kuali.rice.kew.user.UserId;
32  import org.kuali.rice.kew.user.WorkflowUserId;
33  import org.kuali.rice.kew.util.KEWConstants;
34  import org.kuali.rice.kew.workgroup.GroupId;
35  import org.kuali.rice.kew.workgroup.GroupNameId;
36  import org.kuali.rice.kew.workgroup.WorkflowGroupId;
37  import org.kuali.rice.kim.bo.Group;
38  import org.kuali.rice.kim.bo.Person;
39  import org.kuali.rice.kim.bo.entity.KimPrincipal;
40  import org.kuali.rice.kim.service.KIMServiceLocator;
41  
42  /**
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   *
46   */
47  public class IdentityHelperServiceImpl implements IdentityHelperService {
48  
49  	private static final Log logger = LogFactory.getLog(IdentityHelperServiceImpl.class);
50  
51  		public String getIdForPrincipalName(String principalName) {
52  		if (principalName == null) {
53  			throw new IllegalArgumentException("Can't lookup a principal ID for a null principal name.");
54  		}
55  		KimPrincipal principal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName);
56  		if (principal == null) {
57  			throw new RiceRuntimeException("Given principal name of '" + principalName + "' was invalid.  Failed to lookup a corresponding principal ID.");
58  		}
59  		return principal.getPrincipalId();
60  	}
61  
62  		public void validatePrincipalId(String principalId) {
63  			// the getPrincipal method attempts to load the principal with the id and throws an exception if it fails
64  			getPrincipal(principalId);
65  		}
66  
67  	public String getIdForGroupName(String namespace, String groupName) {
68  		Group group = KIMServiceLocator.getIdentityManagementService().getGroupByName(namespace, groupName);
69  		if (group == null) {
70  			throw new RiceRuntimeException("Given namespace of '" + namespace + "' and name of '" + groupName + "' was invalid.  Failed to lookup a corresponding group ID.");
71  		}
72  		return group.getGroupId();
73  	}
74  
75  
76  	public Recipient getPrincipalRecipient(String principalId) {
77  		KimPrincipal principal = getPrincipal(principalId);
78  		return new KimPrincipalRecipient(principal);
79  	}
80  
81  	public KimPrincipal getPrincipal(String principalId) {
82  		KimPrincipal principal = KIMServiceLocator.getIdentityManagementService().getPrincipal(principalId);
83  		if (principal == null) {
84  			throw new RiceRuntimeException("Could not locate a principal with the given principalId of " + principalId);
85  		}
86  		return principal;
87  	}
88  
89  	public KimPrincipal getPrincipalByPrincipalName(String principalName) {
90  		KimPrincipal principal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName);
91  		if (principal == null) {
92  			throw new RiceRuntimeException("Could not locate a principal with the given principalName of " + principalName);
93  		}
94  		return principal;
95  	}
96  
97  	public Group getGroupByName(String namespaceCode, String name) {
98  		Group group = KIMServiceLocator.getIdentityManagementService().getGroupByName(namespaceCode, name);
99  		if (group == null) {
100 			throw new RiceRuntimeException("Could not locate a group with the given namspace of '" + namespaceCode + "' and group name of '" + name + "'");
101 		}
102 		return group;
103 	}
104 
105 	public Person getPerson(String principalId) {
106 		Person person = KIMServiceLocator.getPersonService().getPerson(principalId);
107 		if (person == null) {
108 			throw new RiceRuntimeException("Could not locate a person with the given principal id of " + principalId);
109 		}
110 		return person;
111 	}
112 
113 	public Person getPersonByPrincipalName(String principalName) {
114 		Person person = KIMServiceLocator.getPersonService().getPersonByPrincipalName(principalName);
115 		if (person == null) {
116 			throw new RiceRuntimeException("Could not locate a person with the given principal name of " + principalName);
117 		}
118 		return person;
119 	}
120 
121 	public Person getPersonByEmployeeId(String employeeId) {
122 		Person person = KIMServiceLocator.getPersonService().getPersonByEmployeeId(employeeId);
123 		if (person == null) {
124 			throw new RiceRuntimeException("Could not locate a person with the given employee id of " + employeeId);
125 		}
126 		return person;
127 	}
128 
129 
130 	public Group getGroup(String groupId) {
131 		Group group = KIMServiceLocator.getIdentityManagementService().getGroup(groupId);
132 		if (group == null) {
133 			throw new RiceRuntimeException("Could not locate a group with the given groupId of " + groupId);
134 		}
135 		return group;
136 	}
137 
138 	public Group getGroup(GroupId groupId) {
139 		if (groupId == null || groupId.isEmpty()) {
140 			return null;
141 		} else if (groupId instanceof WorkflowGroupId) {
142 			return KIMServiceLocator.getIdentityManagementService().getGroup(""+((WorkflowGroupId)groupId).getGroupId());
143 		} else if (groupId instanceof GroupNameId) {
144 			return KIMServiceLocator.getIdentityManagementService().getGroupByName(((GroupNameId)groupId).getNamespace(), ((GroupNameId)groupId).getNameId());
145 		}
146 		throw new IllegalArgumentException("Invalid GroupId type was passed: " + groupId);
147 	}
148 
149 	public String getGroupId(GroupId groupId) {
150 		if (groupId == null || groupId.isEmpty()) {
151 			return null;
152 		} else if (groupId instanceof WorkflowGroupId) {
153 			return ((WorkflowGroupId)groupId).getGroupId().toString();
154 		} else if (groupId instanceof GroupNameId) {
155 			Group group = getGroup(groupId);
156 			return group.getGroupId();
157 		}
158 		throw new IllegalArgumentException("Invalid GroupId type was passed: " + groupId);
159 	}
160 
161 	public KimPrincipal getPrincipal(UserId userId) {
162 		if (userId == null) {
163 			return null;
164 		} else if (userId instanceof WorkflowUserId) {
165 			String principalId = ((WorkflowUserId)userId).getWorkflowId();
166 			return KIMServiceLocator.getIdentityManagementService().getPrincipal(principalId);
167 		} else if (userId instanceof AuthenticationUserId) {
168 			String principalName = ((AuthenticationUserId)userId).getAuthenticationId();
169 			return KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName);
170 		} else if (userId instanceof EmplId) {
171 			String employeeId = ((EmplId)userId).getEmplId();
172 			Person person = getPersonByEmployeeId(employeeId);
173 			return getPrincipal(person.getPrincipalId());
174 		}
175 		throw new IllegalArgumentException("Invalid UserIdDTO type was passed: " + userId);
176 	}
177 
178 
179 	public KimPrincipal getPrincipal(UserIdDTO userId) {
180 		if (userId == null) {
181 			return null;
182 		} else if (userId instanceof WorkflowIdDTO) {
183 			String principalId = ((WorkflowIdDTO)userId).getWorkflowId();
184 			return KIMServiceLocator.getIdentityManagementService().getPrincipal(principalId);
185 		} else if (userId instanceof NetworkIdDTO) {
186 			String principalName = ((NetworkIdDTO)userId).getNetworkId();
187 			return KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName);
188 		} else if (userId instanceof EmplIdDTO) {
189 			String employeeId = ((EmplIdDTO)userId).getEmplId();
190 			Person person = getPersonByEmployeeId(employeeId);
191 			return getPrincipal(person.getPrincipalId());
192 		}
193 		throw new IllegalArgumentException("Invalid UserIdDTO type was passed: " + userId.getClass());
194 	}
195 
196 	public Recipient getGroupRecipient(String groupId) {
197 		Group group = KIMServiceLocator.getIdentityManagementService().getGroup(groupId);
198 		return new KimGroupRecipient(group);
199 	}
200 	
201 	public KimPrincipal getSystemPrincipal() {
202 		return getPrincipalByPrincipalName(KEWConstants.SYSTEM_USER);
203 	}
204 
205 }