View Javadoc
1   /**
2    * Copyright 2005-2016 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.impl.services;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.rice.core.api.criteria.Predicate;
24  import org.kuali.rice.core.api.criteria.PredicateUtils;
25  import org.kuali.rice.core.api.criteria.QueryByCriteria;
26  import org.kuali.rice.kim.api.KimConstants;
27  import org.kuali.rice.kim.api.group.Group;
28  import org.kuali.rice.kim.api.group.GroupContract;
29  import org.kuali.rice.kim.api.group.GroupService;
30  import org.kuali.rice.kim.api.identity.CodedAttribute;
31  import org.kuali.rice.kim.api.identity.CodedAttributeContract;
32  import org.kuali.rice.kim.api.identity.IdentityService;
33  import org.kuali.rice.kim.api.identity.Person;
34  import org.kuali.rice.kim.api.identity.PersonService;
35  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationType;
36  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierType;
37  import org.kuali.rice.kim.api.role.Role;
38  import org.kuali.rice.kim.api.role.RoleContract;
39  import org.kuali.rice.kim.api.role.RoleService;
40  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
41  import org.kuali.rice.kim.api.type.KimTypeInfoService;
42  import org.kuali.rice.kim.framework.group.GroupEbo;
43  import org.kuali.rice.kim.framework.identity.EntityTypeEbo;
44  import org.kuali.rice.kim.framework.identity.address.EntityAddressTypeEbo;
45  import org.kuali.rice.kim.framework.identity.affiliation.EntityAffiliationTypeEbo;
46  import org.kuali.rice.kim.framework.identity.citizenship.EntityCitizenshipStatusEbo;
47  import org.kuali.rice.kim.framework.identity.email.EntityEmailTypeEbo;
48  import org.kuali.rice.kim.framework.identity.employment.EntityEmploymentStatusEbo;
49  import org.kuali.rice.kim.framework.identity.employment.EntityEmploymentTypeEbo;
50  import org.kuali.rice.kim.framework.identity.external.EntityExternalIdentifierTypeEbo;
51  import org.kuali.rice.kim.framework.identity.name.EntityNameTypeEbo;
52  import org.kuali.rice.kim.framework.identity.phone.EntityPhoneTypeEbo;
53  import org.kuali.rice.kim.framework.role.RoleEbo;
54  import org.kuali.rice.kim.impl.KIMPropertyConstants;
55  import org.kuali.rice.kim.impl.group.GroupBo;
56  import org.kuali.rice.kim.impl.identity.EntityTypeBo;
57  import org.kuali.rice.kim.impl.identity.address.EntityAddressTypeBo;
58  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationTypeBo;
59  import org.kuali.rice.kim.impl.identity.citizenship.EntityCitizenshipStatusBo;
60  import org.kuali.rice.kim.impl.identity.email.EntityEmailTypeBo;
61  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentStatusBo;
62  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentTypeBo;
63  import org.kuali.rice.kim.impl.identity.external.EntityExternalIdentifierTypeBo;
64  import org.kuali.rice.kim.impl.identity.name.EntityNameTypeBo;
65  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneTypeBo;
66  import org.kuali.rice.kim.impl.role.RoleBo;
67  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
68  import org.kuali.rice.krad.service.impl.RemoteModuleServiceBase;
69  
70  public class KimRemoteModuleService extends RemoteModuleServiceBase {
71  
72      private PersonService personService;
73      private RoleService kimRoleService;
74      private GroupService groupService;
75      private IdentityService identityService;
76      private KimTypeInfoService kimTypeInfoService;
77  
78      public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass, Map<String, Object> fieldValues) {
79          if ( Person.class.isAssignableFrom( businessObjectClass ) ) {
80              if ( fieldValues.containsKey( KIMPropertyConstants.Person.PRINCIPAL_ID ) ) {
81                  return (T) getPersonService().getPerson( (String)fieldValues.get( KIMPropertyConstants.Person.PRINCIPAL_ID ) );
82              } else if ( fieldValues.containsKey( KIMPropertyConstants.Person.PRINCIPAL_NAME ) ) {
83                  return (T) getPersonService().getPersonByPrincipalName( (String)fieldValues.get( KIMPropertyConstants.Person.PRINCIPAL_NAME ) );
84              }
85              // otherwise, fall through since critieria is not known
86          } else if(RoleContract.class.isAssignableFrom(businessObjectClass)){
87              if(fieldValues.containsKey(KimConstants.PrimaryKeyConstants.ROLE_ID)){
88                  Role role = getKimRoleService().getRole((String) fieldValues.get(
89                          KimConstants.PrimaryKeyConstants.ROLE_ID));
90                  return (T) RoleBo.from(role);
91              }
92          } else if(GroupContract.class.isAssignableFrom(businessObjectClass)){
93              if(fieldValues.containsKey(KimConstants.PrimaryKeyConstants.GROUP_ID)) {
94                  Group group = getGroupService().getGroup((String) fieldValues.get(
95                          KimConstants.PrimaryKeyConstants.GROUP_ID));
96                  return (T) GroupBo.from(group);
97              }
98          } else if (EntityEmailTypeEbo.class.isAssignableFrom(businessObjectClass)) {
99              if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
100                 CodedAttribute codedAttribute = getIdentityService()
101                         .getEmailType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
102                 return (T)EntityEmailTypeBo.from(codedAttribute);
103             }
104         } else if (EntityAddressTypeEbo.class.isAssignableFrom(businessObjectClass)) {
105             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
106                 CodedAttribute codedAttribute = getIdentityService()
107                         .getAddressType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
108                 return (T)EntityAddressTypeBo.from(codedAttribute);
109             }
110         } else if (EntityAffiliationTypeEbo.class.isAssignableFrom(businessObjectClass)) {
111             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
112                 EntityAffiliationType codedAttribute = getIdentityService()
113                         .getAffiliationType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
114                 return (T)EntityAffiliationTypeBo.from(codedAttribute);
115             }
116         } else if (EntityCitizenshipStatusEbo.class.isAssignableFrom(businessObjectClass)) {
117             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
118                 CodedAttribute codedAttribute = getIdentityService()
119                         .getCitizenshipStatus((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
120                 return (T)EntityCitizenshipStatusBo.from(codedAttribute);
121             }
122         } else if (EntityEmploymentStatusEbo.class.isAssignableFrom(businessObjectClass)) {
123             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
124                 CodedAttribute codedAttribute = getIdentityService()
125                         .getEmploymentStatus((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
126                 return (T)EntityEmploymentStatusBo.from(codedAttribute);
127             }
128         }  else if (EntityEmploymentTypeEbo.class.isAssignableFrom(businessObjectClass)) {
129             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
130                 CodedAttribute codedAttribute = getIdentityService()
131                         .getEmploymentType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
132                 return (T)EntityEmploymentTypeBo.from(codedAttribute);
133             }
134         } else if (EntityNameTypeEbo.class.isAssignableFrom(businessObjectClass)) {
135             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
136                 CodedAttribute codedAttribute = getIdentityService()
137                         .getNameType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
138                 return (T)EntityNameTypeBo.from(codedAttribute);
139             }
140         } else if (EntityTypeEbo.class.isAssignableFrom(businessObjectClass)) {
141             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
142                 CodedAttribute codedAttribute = getIdentityService()
143                         .getEntityType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
144                 return (T)EntityTypeBo.from(codedAttribute);
145             }
146         } else if (EntityExternalIdentifierTypeEbo.class.isAssignableFrom(businessObjectClass)) {
147             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
148                 EntityExternalIdentifierType codedAttribute = getIdentityService()
149                         .getExternalIdentifierType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
150                 return (T)EntityExternalIdentifierTypeBo.from(codedAttribute);
151             }
152         } else if (EntityPhoneTypeEbo.class.isAssignableFrom(businessObjectClass)) {
153             if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
154                 CodedAttribute codedAttribute = getIdentityService()
155                         .getPhoneType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
156                 return (T)EntityPhoneTypeBo.from(codedAttribute);
157             }
158         }
159         return null;
160     }
161 
162     @Override
163     public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList(
164             Class<T> businessObjectClass, Map<String, Object> fieldValues) {
165         //convert fieldValues to Query
166         QueryByCriteria.Builder queryBuilder = QueryByCriteria.Builder.create();
167         Predicate predicate = PredicateUtils.convertObjectMapToPredicate(fieldValues);
168         queryBuilder.setPredicates(predicate);
169         
170         return this.queryForEbos(businessObjectClass, queryBuilder.build(), fieldValues);
171     }
172 
173     @Override
174     public boolean isExternalizableBusinessObjectLookupable(Class boClass) {
175         return isExternalizable(boClass);
176     }
177 
178     @Override
179     public boolean isExternalizableBusinessObjectInquirable(Class boClass) {
180         return isExternalizable(boClass);
181     }
182 
183     @Override
184     public boolean isExternalizable(Class boClazz) {
185         if (boClazz == null) {
186             return false;
187         }
188         if(RoleContract.class.isAssignableFrom(boClazz)) {
189             return true;
190         } else if(GroupContract.class.isAssignableFrom(boClazz)) {
191             return true;
192         } else if(Person.class.isAssignableFrom(boClazz)) {
193             return true;
194         }
195         return ExternalizableBusinessObject.class.isAssignableFrom(boClazz);
196     }
197 
198     @Override
199     public List<String> listPrimaryKeyFieldNames(Class boClass) {
200 
201         //TODO:  I strongly dislike hard-coding these values, but have this here because the OJB stuff
202         //TODO: isn't available when loaded in REMOTE mode...
203         if(GroupContract.class.isAssignableFrom(boClass)
204                 || RoleContract.class.isAssignableFrom(boClass)){
205             return Collections.singletonList(KimConstants.PrimaryKeyConstants.ID);
206         } else if (Person.class.isAssignableFrom(boClass)) {
207             return Collections.singletonList(KimConstants.PrimaryKeyConstants.PRINCIPAL_ID);
208         } else if (CodedAttributeContract.class.isAssignableFrom(boClass)) {
209             return Collections.singletonList(KimConstants.PrimaryKeyConstants.CODE);
210         }
211         return Collections.emptyList();
212     }
213 
214     private <T extends ExternalizableBusinessObject> List<T> queryForEbos(
215             Class<T> businessObjectClass, QueryByCriteria query, Map<String, Object> fieldValues) {
216         if ( Person.class.isAssignableFrom( businessObjectClass ) ) {
217             return (List)getPersonService().findPeople( (Map)fieldValues );
218         }
219         else if ( RoleContract.class.isAssignableFrom( businessObjectClass ) ) {
220             List<Role> roles = getKimRoleService().findRoles(query).getResults();
221             List<RoleEbo> roleEbos = new ArrayList<RoleEbo>(roles.size());
222             for (Role role : roles) {
223                 roleEbos.add(RoleBo.from(role));
224             }
225             return (List<T>)roleEbos;
226         } else if ( GroupContract.class.isAssignableFrom(businessObjectClass) ) {
227             List<Group> groups = getGroupService().findGroups(query).getResults();
228             List<GroupEbo> groupEbos = new ArrayList<GroupEbo>(groups.size());
229             for (Group group : groups) {
230                 groupEbos.add(GroupBo.from(group));
231             }
232             return (List<T>)groupEbos;
233         } else if (EntityEmailTypeEbo.class.isAssignableFrom(businessObjectClass)) {
234             List<CodedAttribute> codedAttributes = getIdentityService().findAllEmailTypes();
235             List<EntityEmailTypeEbo> ebos = new ArrayList<EntityEmailTypeEbo>();
236             for(CodedAttribute attr : codedAttributes) {
237                 ebos.add(EntityEmailTypeBo.from(attr));
238             }
239             return (List<T>)ebos;
240         } else if (EntityAddressTypeEbo.class.isAssignableFrom(businessObjectClass)) {
241             List<CodedAttribute> codedAttributes = getIdentityService().findAllAddressTypes();
242             List<EntityAddressTypeEbo> ebos = new ArrayList<EntityAddressTypeEbo>();
243             for(CodedAttribute attr : codedAttributes) {
244                 ebos.add(EntityAddressTypeBo.from(attr));
245             }
246             return (List<T>)ebos;
247         } else if (EntityAffiliationTypeEbo.class.isAssignableFrom(businessObjectClass)) {
248             List<EntityAffiliationType> codedAttributes = getIdentityService().findAllAffiliationTypes();
249             List<EntityAffiliationTypeEbo> ebos = new ArrayList<EntityAffiliationTypeEbo>();
250             for(EntityAffiliationType attr : codedAttributes) {
251                 ebos.add(EntityAffiliationTypeBo.from(attr));
252             }
253             return (List<T>)ebos;
254         } else if (EntityCitizenshipStatusEbo.class.isAssignableFrom(businessObjectClass)) {
255             List<CodedAttribute> codedAttributes = getIdentityService().findAllCitizenshipStatuses();
256             List<EntityCitizenshipStatusEbo> ebos = new ArrayList<EntityCitizenshipStatusEbo>();
257             for(CodedAttribute attr : codedAttributes) {
258                 ebos.add(EntityCitizenshipStatusBo.from(attr));
259             }
260             return (List<T>)ebos;
261         } else if (EntityEmploymentStatusEbo.class.isAssignableFrom(businessObjectClass)) {
262             List<CodedAttribute> codedAttributes = getIdentityService().findAllEmploymentStatuses();
263             List<EntityEmploymentStatusEbo> ebos = new ArrayList<EntityEmploymentStatusEbo>();
264             for(CodedAttribute attr : codedAttributes) {
265                 ebos.add(EntityEmploymentStatusBo.from(attr));
266             }
267             return (List<T>)ebos;
268         }  else if (EntityEmploymentTypeEbo.class.isAssignableFrom(businessObjectClass)) {
269             List<CodedAttribute> codedAttributes = getIdentityService().findAllEmploymentTypes();
270             List<EntityEmploymentTypeEbo> ebos = new ArrayList<EntityEmploymentTypeEbo>();
271             for(CodedAttribute attr : codedAttributes) {
272                 ebos.add(EntityEmploymentTypeBo.from(attr));
273             }
274             return (List<T>)ebos;
275         } else if (EntityNameTypeEbo.class.isAssignableFrom(businessObjectClass)) {
276             List<CodedAttribute> codedAttributes = getIdentityService().findAllNameTypes();
277             List<EntityNameTypeEbo> ebos = new ArrayList<EntityNameTypeEbo>();
278             for(CodedAttribute attr : codedAttributes) {
279                 ebos.add(EntityNameTypeBo.from(attr));
280             }
281             return (List<T>)ebos;
282         } else if (EntityTypeEbo.class.isAssignableFrom(businessObjectClass)) {
283             List<CodedAttribute> codedAttributes = getIdentityService().findAllEntityTypes();
284             List<EntityTypeEbo> ebos = new ArrayList<EntityTypeEbo>();
285             for(CodedAttribute attr : codedAttributes) {
286                 ebos.add(EntityTypeBo.from(attr));
287             }
288             return (List<T>)ebos;
289         } else if (EntityExternalIdentifierTypeEbo.class.isAssignableFrom(businessObjectClass)) {
290             List<EntityExternalIdentifierType> codedAttributes = getIdentityService().findAllExternalIdendtifierTypes();
291             List<EntityExternalIdentifierTypeEbo> ebos = new ArrayList<EntityExternalIdentifierTypeEbo>();
292             for(EntityExternalIdentifierType attr : codedAttributes) {
293                 ebos.add(EntityExternalIdentifierTypeBo.from(attr));
294             }
295             return (List<T>)ebos;
296         } else if (EntityPhoneTypeEbo.class.isAssignableFrom(businessObjectClass)) {
297             List<CodedAttribute> codedAttributes = getIdentityService().findAllPhoneTypes();
298             List<EntityPhoneTypeEbo> ebos = new ArrayList<EntityPhoneTypeEbo>();
299             for(CodedAttribute attr : codedAttributes) {
300                 ebos.add(EntityPhoneTypeBo.from(attr));
301             }
302             return (List<T>)ebos;
303         }
304         return Collections.emptyList();
305 
306     }
307 
308     
309     protected PersonService getPersonService() {
310         if ( personService == null ) {
311             personService = KimApiServiceLocator.getPersonService();
312         }
313         return personService;
314     }
315 
316     protected RoleService getKimRoleService() {
317         if ( kimRoleService == null ) {
318             kimRoleService = KimApiServiceLocator.getRoleService();
319         }
320         return kimRoleService;
321     }
322 
323     protected GroupService getGroupService() {
324         if ( groupService == null ) {
325             groupService = KimApiServiceLocator.getGroupService();
326         }
327         return groupService;
328     }
329 
330     protected IdentityService getIdentityService() {
331         if ( identityService == null ) {
332             identityService = KimApiServiceLocator.getIdentityService();
333         }
334         return identityService;
335     }
336 
337     protected KimTypeInfoService getTypeInfoService() {
338         if(kimTypeInfoService == null){
339             kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
340         }
341         return kimTypeInfoService;
342     }
343 }