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.kim.service.impl;
17  
18  import org.kuali.rice.core.api.criteria.Predicate;
19  import org.kuali.rice.core.api.criteria.QueryByCriteria;
20  import org.kuali.rice.kew.api.KewApiConstants;
21  import org.kuali.rice.kim.api.KimConstants;
22  import org.kuali.rice.kim.api.group.Group;
23  import org.kuali.rice.kim.api.group.GroupContract;
24  import org.kuali.rice.kim.api.group.GroupService;
25  import org.kuali.rice.kim.api.identity.CodedAttribute;
26  import org.kuali.rice.kim.api.identity.IdentityService;
27  import org.kuali.rice.kim.api.identity.Person;
28  import org.kuali.rice.kim.api.identity.PersonService;
29  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationType;
30  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierType;
31  import org.kuali.rice.kim.api.role.Role;
32  import org.kuali.rice.kim.api.role.RoleContract;
33  import org.kuali.rice.kim.api.role.RoleService;
34  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35  import org.kuali.rice.kim.api.type.KimType;
36  import org.kuali.rice.kim.api.type.KimTypeContract;
37  import org.kuali.rice.kim.api.type.KimTypeInfoService;
38  import org.kuali.rice.kim.framework.group.GroupEbo;
39  import org.kuali.rice.kim.framework.identity.EntityTypeEbo;
40  import org.kuali.rice.kim.framework.identity.address.EntityAddressTypeEbo;
41  import org.kuali.rice.kim.framework.identity.affiliation.EntityAffiliationTypeEbo;
42  import org.kuali.rice.kim.framework.identity.citizenship.EntityCitizenshipStatusEbo;
43  import org.kuali.rice.kim.framework.identity.email.EntityEmailTypeContractEbo;
44  import org.kuali.rice.kim.framework.identity.email.EntityEmailTypeEbo;
45  import org.kuali.rice.kim.framework.identity.employment.EntityEmploymentStatusEbo;
46  import org.kuali.rice.kim.framework.identity.employment.EntityEmploymentTypeEbo;
47  import org.kuali.rice.kim.framework.identity.external.EntityExternalIdentifierTypeEbo;
48  import org.kuali.rice.kim.framework.identity.name.EntityNameTypeEbo;
49  import org.kuali.rice.kim.framework.identity.phone.EntityPhoneTypeEbo;
50  import org.kuali.rice.kim.framework.role.RoleEbo;
51  import org.kuali.rice.kim.impl.KIMPropertyConstants;
52  import org.kuali.rice.kim.impl.group.GroupBo;
53  import org.kuali.rice.kim.impl.role.RoleBo;
54  import org.kuali.rice.kim.util.KimCommonUtilsInternal;
55  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
56  import org.kuali.rice.krad.service.impl.ModuleServiceBase;
57  import org.kuali.rice.krad.util.KRADConstants;
58  
59  import java.util.ArrayList;
60  import java.util.Collections;
61  import java.util.HashSet;
62  import java.util.List;
63  import java.util.Map;
64  import java.util.Properties;
65  import java.util.Set;
66  
67  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
68  
69  /**
70   * This is a description of what this class does - kellerj don't forget to fill this in.
71   *
72   * @author Kuali Rice Team (rice.collab@kuali.org)
73   *
74   */
75  public class KimModuleService extends ModuleServiceBase {
76  
77  	private PersonService personService;
78  	private RoleService kimRoleService;
79  	private GroupService groupService;
80      private IdentityService identityService;
81  	private KimTypeInfoService kimTypeInfoService;
82  
83  	/**
84  	 * This overridden method ...
85  	 *
86  	 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObject(java.lang.Class, java.util.Map)
87  	 */
88  	@SuppressWarnings("unchecked")
89  	@Override
90  	public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass, Map<String, Object> fieldValues) {
91  		if ( Person.class.isAssignableFrom( businessObjectClass ) ) {
92  			if ( fieldValues.containsKey( KIMPropertyConstants.Person.PRINCIPAL_ID ) ) {
93  				return (T) getPersonService().getPerson( (String)fieldValues.get( KIMPropertyConstants.Person.PRINCIPAL_ID ) );
94  			} else if ( fieldValues.containsKey( KIMPropertyConstants.Person.PRINCIPAL_NAME ) ) {
95  				return (T) getPersonService().getPersonByPrincipalName( (String)fieldValues.get( KIMPropertyConstants.Person.PRINCIPAL_NAME ) );
96  			}
97  			// otherwise, fall through since critieria is not known
98  		} else if(RoleContract.class.isAssignableFrom(businessObjectClass)){
99  			if(fieldValues.containsKey(KimConstants.PrimaryKeyConstants.ROLE_ID)){
100 				Role role = getKimRoleService().getRole((String)fieldValues.get(KimConstants.PrimaryKeyConstants.ROLE_ID));
101 				return (T) RoleEbo.from(role);
102 			}
103 		} else if(GroupContract.class.isAssignableFrom(businessObjectClass)){
104 			if(fieldValues.containsKey(KimConstants.PrimaryKeyConstants.GROUP_ID)) {
105                 Group group = getGroupService().getGroup((String)fieldValues.get(KimConstants.PrimaryKeyConstants.GROUP_ID));
106 				return (T) GroupEbo.from(group);
107 			}
108 		} else if (EntityEmailTypeEbo.class.isAssignableFrom(businessObjectClass)) {
109             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
110                 CodedAttribute codedAttribute = getIdentityService()
111                         .getEmailType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
112                 return (T)EntityEmailTypeEbo.from(codedAttribute);
113             }
114         } else if (EntityAddressTypeEbo.class.isAssignableFrom(businessObjectClass)) {
115             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
116                 CodedAttribute codedAttribute = getIdentityService()
117                         .getAddressType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
118                 return (T)EntityAddressTypeEbo.from(codedAttribute);
119             }
120         } else if (EntityAffiliationTypeEbo.class.isAssignableFrom(businessObjectClass)) {
121             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
122                 EntityAffiliationType codedAttribute = getIdentityService()
123                         .getAffiliationType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
124                 return (T)EntityAffiliationTypeEbo.from(codedAttribute);
125             }
126         } else if (EntityCitizenshipStatusEbo.class.isAssignableFrom(businessObjectClass)) {
127             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
128                 CodedAttribute codedAttribute = getIdentityService()
129                         .getCitizenshipStatus((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
130                 return (T)EntityCitizenshipStatusEbo.from(codedAttribute);
131             }
132         } else if (EntityEmploymentStatusEbo.class.isAssignableFrom(businessObjectClass)) {
133             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
134                 CodedAttribute codedAttribute = getIdentityService()
135                         .getEmploymentStatus((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
136                 return (T)EntityEmploymentStatusEbo.from(codedAttribute);
137             }
138         }  else if (EntityEmploymentTypeEbo.class.isAssignableFrom(businessObjectClass)) {
139             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
140                 CodedAttribute codedAttribute = getIdentityService()
141                         .getEmploymentType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
142                 return (T)EntityEmploymentTypeEbo.from(codedAttribute);
143             }
144         } else if (EntityNameTypeEbo.class.isAssignableFrom(businessObjectClass)) {
145             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
146                 CodedAttribute codedAttribute = getIdentityService()
147                         .getNameType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
148                 return (T)EntityNameTypeEbo.from(codedAttribute);
149             }
150         } else if (EntityTypeEbo.class.isAssignableFrom(businessObjectClass)) {
151             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
152                 CodedAttribute codedAttribute = getIdentityService()
153                         .getEntityType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
154                 return (T)EntityTypeEbo.from(codedAttribute);
155             }
156         } else if (EntityExternalIdentifierTypeEbo.class.isAssignableFrom(businessObjectClass)) {
157             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
158                 EntityExternalIdentifierType codedAttribute = getIdentityService()
159                         .getExternalIdentifierType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
160                 return (T)EntityExternalIdentifierTypeEbo.from(codedAttribute);
161             }
162         } else if (EntityPhoneTypeEbo.class.isAssignableFrom(businessObjectClass)) {
163             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
164                 CodedAttribute codedAttribute = getIdentityService()
165                         .getPhoneType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
166                 return (T)EntityPhoneTypeEbo.from(codedAttribute);
167             }
168         }
169 		// otherwise, use the default implementation
170 		return super.getExternalizableBusinessObject( businessObjectClass, fieldValues );
171 	}
172 
173 	/**
174 	 * This overridden method ...
175 	 *
176 	 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObjectsList(java.lang.Class, java.util.Map)
177 	 */
178 	@SuppressWarnings("unchecked")
179 	@Override
180 	public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList(
181 			Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues) {
182 		// for Person objects (which are not real PersistableBOs) pull them through the person service
183 
184 		if ( Person.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
185 			return (List)getPersonService().findPeople( (Map)fieldValues );
186 		}
187         else if ( RoleContract.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
188             List<Role> roles = getKimRoleService().findRoles(toQuery(fieldValues)).getResults();
189             List<RoleEbo> roleEbos = new ArrayList<RoleEbo>(roles.size());
190             for (Role role : roles) {
191                 roleEbos.add(RoleEbo.from(role));
192             }
193             return (List<T>)roleEbos;
194 		} else if ( GroupContract.class.isAssignableFrom(externalizableBusinessObjectClass) ) {
195 			List<Group> groups = getGroupService().findGroups(toQuery(fieldValues)).getResults();
196             List<GroupEbo> groupEbos = new ArrayList<GroupEbo>(groups.size());
197             for (Group group : groups) {
198                 groupEbos.add(GroupEbo.from(group));
199             }
200             return (List<T>)groupEbos;
201 		}
202 		// otherwise, use the default implementation
203 		return super.getExternalizableBusinessObjectsList( externalizableBusinessObjectClass, fieldValues );
204 	}
205 
206     private QueryByCriteria toQuery(Map<String,?> fieldValues) {
207         Set<Predicate> preds = new HashSet<Predicate>();
208         for (String key : fieldValues.keySet()) {
209             preds.add(equal(key, fieldValues.get(key)));
210         }
211         Predicate[] predicates = new Predicate[0];
212         predicates = preds.toArray(predicates);
213         return QueryByCriteria.Builder.fromPredicates(predicates);
214     }
215 
216 	/***
217 	 * @see org.kuali.rice.krad.service.ModuleService#getExternalizableBusinessObjectsListForLookup(java.lang.Class, java.util.Map, boolean)
218 	 */
219 	@SuppressWarnings("unchecked")
220 	@Override
221 	public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsListForLookup(
222 			Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues, boolean unbounded) {
223 		// for Person objects (which are not real PersistableBOs) pull them through the person service
224 		if ( Person.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
225 			return (List)getPersonService().findPeople( (Map)fieldValues, unbounded );
226 		} else if ( RoleContract.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
227 			List<Role> roles = getKimRoleService().findRoles(toQuery(fieldValues)).getResults();
228             List<RoleEbo> roleEbos = new ArrayList<RoleEbo>(roles.size());
229             for (Role role : roles) {
230                 roleEbos.add(RoleEbo.from(role));
231             }
232             return (List<T>)roleEbos;
233 		} else if (GroupContract.class.isAssignableFrom( externalizableBusinessObjectClass)) {
234             List<Group> groups = getGroupService().findGroups(toQuery(fieldValues)).getResults();
235             List<GroupEbo> groupEbos = new ArrayList<GroupEbo>(groups.size());
236             for (Group group : groups) {
237                 groupEbos.add(GroupEbo.from(group));
238             }
239             return (List<T>)groupEbos;
240         }
241 		// otherwise, use the default implementation
242 		return super.getExternalizableBusinessObjectsListForLookup(externalizableBusinessObjectClass, fieldValues, unbounded);
243 	}
244 
245 	/**
246 	 * This overridden method ...
247 	 *
248 	 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#listPrimaryKeyFieldNames(java.lang.Class)
249 	 */
250 	@SuppressWarnings("unchecked")
251 	@Override
252 	public List listPrimaryKeyFieldNames(Class businessObjectInterfaceClass) {
253 		// for Person objects (which are not real PersistableBOs) pull them through the person service
254 		if ( Person.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
255 			return Collections.singletonList( KimConstants.PrimaryKeyConstants.PRINCIPAL_ID );
256 		} else if ( RoleEbo.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
257 			return Collections.singletonList( KimConstants.PrimaryKeyConstants.ROLE_ID );
258 		} else if ( GroupEbo.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
259 			return Collections.singletonList( KimConstants.PrimaryKeyConstants.GROUP_ID );
260 		} else if ( KimType.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
261 			return Collections.singletonList( KimConstants.PrimaryKeyConstants.KIM_TYPE_ID );
262 		} else if ( KimTypeContract.class.isAssignableFrom(businessObjectInterfaceClass)) {
263 			return Collections.singletonList( KimConstants.PrimaryKeyConstants.KIM_TYPE_CODE );
264 		}
265 
266 		// otherwise, use the default implementation
267 		return super.listPrimaryKeyFieldNames( businessObjectInterfaceClass );
268 	}
269 
270 	@SuppressWarnings("unchecked")
271 	protected PersonService getPersonService() {
272 		if ( personService == null ) {
273 			personService = KimApiServiceLocator.getPersonService();
274 		}
275 		return personService;
276 	}
277 
278 	protected RoleService getKimRoleService() {
279 		if ( kimRoleService == null ) {
280 			kimRoleService = KimApiServiceLocator.getRoleService();
281 		}
282 		return kimRoleService;
283 	}
284 
285 	protected GroupService getGroupService() {
286 		if ( groupService == null ) {
287 			groupService = KimApiServiceLocator.getGroupService();
288 		}
289 		return groupService;
290 	}
291 
292     protected IdentityService getIdentityService() {
293 		if ( identityService == null ) {
294 			identityService = KimApiServiceLocator.getIdentityService();
295 		}
296 		return identityService;
297 	}
298 
299 	protected KimTypeInfoService getTypeInfoService() {
300 		if(kimTypeInfoService == null){
301 			kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
302 		}
303 		return kimTypeInfoService;
304 	}
305 
306 	protected Properties getUrlParameters(String businessObjectClassAttribute, Map<String, String[]> parameters){
307 		Properties urlParameters = new Properties();
308 		for (String paramName : parameters.keySet()) {
309 			String[] parameterValues = parameters.get(paramName);
310 			if (parameterValues.length > 0) {
311 				urlParameters.put(paramName, parameterValues[0]);
312 			}
313 		}
314 		urlParameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObjectClassAttribute);
315 		try{
316 			Class inquiryBusinessObjectClass = Class.forName(businessObjectClassAttribute);
317 			if(Role.class.isAssignableFrom(inquiryBusinessObjectClass) || 
318 					Group.class.isAssignableFrom(inquiryBusinessObjectClass) || 
319 					Person.class.isAssignableFrom(inquiryBusinessObjectClass)) {
320 		        urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.PARAM_MAINTENANCE_VIEW_MODE_INQUIRY);
321 			} else{
322 		        urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.CONTINUE_WITH_INQUIRY_METHOD_TO_CALL);
323 			}
324 		} catch(Exception eix){
325 			urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.CONTINUE_WITH_INQUIRY_METHOD_TO_CALL);
326 		}
327         urlParameters.put(KRADConstants.PARAMETER_COMMAND, KewApiConstants.INITIATE_COMMAND);
328 		return urlParameters;
329 	}
330 
331 	@Override
332 	protected String getInquiryUrl(Class inquiryBusinessObjectClass){
333 		String inquiryUrl = KimCommonUtilsInternal.getKimBasePath();
334 		if (!inquiryUrl.endsWith("/")) {
335 			inquiryUrl = inquiryUrl + "/";
336 		}
337 		if(Role.class.isAssignableFrom(inquiryBusinessObjectClass)) {
338 			return inquiryUrl + KimConstants.KimUIConstants.KIM_ROLE_INQUIRY_ACTION;
339 		} else if(Group.class.isAssignableFrom(inquiryBusinessObjectClass)) {
340 			return inquiryUrl + KimConstants.KimUIConstants.KIM_GROUP_INQUIRY_ACTION;
341 		} else if(Person.class.isAssignableFrom(inquiryBusinessObjectClass)) {
342 			return inquiryUrl + KimConstants.KimUIConstants.KIM_PERSON_INQUIRY_ACTION;
343 		}
344 		return super.getInquiryUrl(inquiryBusinessObjectClass);
345 	}
346 
347 }