View Javadoc

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