View Javadoc

1   /**
2    * Copyright 2005-2013 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.api.exception.RiceIllegalArgumentException;
21  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
22  import org.kuali.rice.kew.actionrequest.Recipient;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.kuali.rice.kew.api.identity.EmployeeId;
25  import org.kuali.rice.kew.api.identity.PrincipalId;
26  import org.kuali.rice.kew.api.identity.PrincipalName;
27  import org.kuali.rice.kew.api.user.UserId;
28  import org.kuali.rice.kew.identity.service.IdentityHelperService;
29  import org.kuali.rice.kew.workgroup.GroupId;
30  import org.kuali.rice.kew.workgroup.GroupNameId;
31  import org.kuali.rice.kew.workgroup.WorkflowGroupId;
32  import org.kuali.rice.kim.api.group.Group;
33  import org.kuali.rice.kim.api.identity.Person;
34  import org.kuali.rice.kim.api.identity.principal.Principal;
35  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36  
37  /**
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  public class IdentityHelperServiceImpl implements IdentityHelperService {
43  
44  	private static final Log logger = LogFactory.getLog(IdentityHelperServiceImpl.class);
45  
46  		public String getIdForPrincipalName(String principalName) {
47  		if (principalName == null) {
48  			throw new RiceIllegalArgumentException("Can't lookup a principal ID for a null principal name.");
49  		}
50  		Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
51  		if (principal == null) {
52  			throw new RiceIllegalArgumentException("Given principal name of '" + principalName + "' was invalid.  Failed to lookup a corresponding principal ID.");
53  		}
54  		return principal.getPrincipalId();
55  	}
56  
57  		public void validatePrincipalId(String principalId) {
58  			// the getPrincipal method attempts to load the principal with the id and throws an exception if it fails
59  			getPrincipal(principalId);
60  		}
61  
62  	public String getIdForGroupName(String namespace, String groupName) {
63  		Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(namespace, groupName);
64  		if (group == null) {
65  			throw new RiceIllegalArgumentException("Given namespace of '" + namespace + "' and name of '" + groupName + "' was invalid.  Failed to lookup a corresponding group ID.");
66  		}
67  		return group.getId();
68  	}
69  
70  
71  	public Recipient getPrincipalRecipient(String principalId) {
72  		Principal principal = getPrincipal(principalId);
73  		return new KimPrincipalRecipient(principal);
74  	}
75  
76  	public Principal getPrincipal(String principalId) {
77  		Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
78  		if (principal == null) {
79  			throw new RiceIllegalArgumentException("Could not locate a principal with the given principalId of " + principalId);
80  		}
81  		return principal;
82  	}
83  
84  	public Principal getPrincipalByPrincipalName(String principalName) {
85  		Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
86  		if (principal == null) {
87  			throw new RiceIllegalArgumentException("Could not locate a principal with the given principalName of " + principalName);
88  		}
89  		return principal;
90  	}
91  
92  	public Group getGroupByName(String namespaceCode, String name) {
93  		Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(namespaceCode, name);
94  		if (group == null) {
95  			throw new RiceIllegalArgumentException("Could not locate a group with the given namspace of '" + namespaceCode + "' and group name of '" + name + "'");
96  		}
97  		return group;
98  	}
99  
100 	public Person getPerson(String principalId) {
101 		Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
102 		if (person == null) {
103 			throw new RiceIllegalArgumentException("Could not locate a person with the given principal id of " + principalId);
104 		}
105 		return person;
106 	}
107 
108 	public Person getPersonByPrincipalName(String principalName) {
109 		Person person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName);
110 		if (person == null) {
111 			throw new RiceIllegalArgumentException("Could not locate a person with the given principal name of " + principalName);
112 		}
113 		return person;
114 	}
115 
116 	public Person getPersonByEmployeeId(String employeeId) {
117 		Person person = KimApiServiceLocator.getPersonService().getPersonByEmployeeId(employeeId);
118 		if (person == null) {
119 			throw new RiceIllegalArgumentException("Could not locate a person with the given employee id of " + employeeId);
120 		}
121 		return person;
122 	}
123 
124 
125 	public Group getGroup(String groupId) {
126 		Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
127 		if (group == null) {
128 			throw new RiceIllegalArgumentException("Could not locate a group with the given groupId of " + groupId);
129 		}
130 		return group;
131 	}
132 
133 	public Group getGroup(GroupId groupId) {
134 		if (groupId == null || groupId.isEmpty()) {
135 			return null;
136 		} else if (groupId instanceof WorkflowGroupId) {
137 			return KimApiServiceLocator.getGroupService().getGroup(""+((WorkflowGroupId)groupId).getGroupId());
138 		} else if (groupId instanceof GroupNameId) {
139 			return KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(
140                     ((GroupNameId) groupId).getNamespace(), ((GroupNameId) groupId).getNameId());
141 		}
142 		throw new RiceIllegalArgumentException("Invalid GroupId type was passed: " + groupId);
143 	}
144 
145 	public Principal getPrincipal(UserId userId) {
146 		if (userId == null) {
147 			return null;
148 		} else if (userId instanceof PrincipalId) {
149 			String principalId = ((PrincipalId)userId).getPrincipalId();
150 			return KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
151 		} else if (userId instanceof PrincipalName) {
152 			String principalName = ((PrincipalName)userId).getId();
153 			return KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
154 		} else if (userId instanceof EmployeeId) {
155 			String employeeId = ((EmployeeId)userId).getEmployeeId();
156 			Person person = getPersonByEmployeeId(employeeId);
157 			return getPrincipal(person.getPrincipalId());
158 		}
159 		throw new RiceIllegalArgumentException("Invalid UserIdDTO type was passed: " + userId);
160 	}
161 	
162 	public Principal getSystemPrincipal() {
163 		return getPrincipalByPrincipalName(KewApiConstants.SYSTEM_USER);
164 	}
165 
166 }