001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.impl.services;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021import java.util.Map;
022
023import org.kuali.rice.core.api.criteria.Predicate;
024import org.kuali.rice.core.api.criteria.PredicateUtils;
025import org.kuali.rice.core.api.criteria.QueryByCriteria;
026import org.kuali.rice.kim.api.KimConstants;
027import org.kuali.rice.kim.api.group.Group;
028import org.kuali.rice.kim.api.group.GroupContract;
029import org.kuali.rice.kim.api.group.GroupService;
030import org.kuali.rice.kim.api.identity.CodedAttribute;
031import org.kuali.rice.kim.api.identity.CodedAttributeContract;
032import org.kuali.rice.kim.api.identity.IdentityService;
033import org.kuali.rice.kim.api.identity.Person;
034import org.kuali.rice.kim.api.identity.PersonService;
035import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationType;
036import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierType;
037import org.kuali.rice.kim.api.role.Role;
038import org.kuali.rice.kim.api.role.RoleContract;
039import org.kuali.rice.kim.api.role.RoleService;
040import org.kuali.rice.kim.api.services.KimApiServiceLocator;
041import org.kuali.rice.kim.api.type.KimTypeInfoService;
042import org.kuali.rice.kim.framework.group.GroupEbo;
043import org.kuali.rice.kim.framework.identity.EntityTypeEbo;
044import org.kuali.rice.kim.framework.identity.address.EntityAddressTypeEbo;
045import org.kuali.rice.kim.framework.identity.affiliation.EntityAffiliationTypeEbo;
046import org.kuali.rice.kim.framework.identity.citizenship.EntityCitizenshipStatusEbo;
047import org.kuali.rice.kim.framework.identity.email.EntityEmailTypeEbo;
048import org.kuali.rice.kim.framework.identity.employment.EntityEmploymentStatusEbo;
049import org.kuali.rice.kim.framework.identity.employment.EntityEmploymentTypeEbo;
050import org.kuali.rice.kim.framework.identity.external.EntityExternalIdentifierTypeEbo;
051import org.kuali.rice.kim.framework.identity.name.EntityNameTypeEbo;
052import org.kuali.rice.kim.framework.identity.phone.EntityPhoneTypeEbo;
053import org.kuali.rice.kim.framework.role.RoleEbo;
054import org.kuali.rice.kim.impl.KIMPropertyConstants;
055import org.kuali.rice.kim.impl.group.GroupBo;
056import org.kuali.rice.kim.impl.identity.EntityTypeBo;
057import org.kuali.rice.kim.impl.identity.address.EntityAddressTypeBo;
058import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationTypeBo;
059import org.kuali.rice.kim.impl.identity.citizenship.EntityCitizenshipStatusBo;
060import org.kuali.rice.kim.impl.identity.email.EntityEmailTypeBo;
061import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentStatusBo;
062import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentTypeBo;
063import org.kuali.rice.kim.impl.identity.external.EntityExternalIdentifierTypeBo;
064import org.kuali.rice.kim.impl.identity.name.EntityNameTypeBo;
065import org.kuali.rice.kim.impl.identity.phone.EntityPhoneTypeBo;
066import org.kuali.rice.kim.impl.role.RoleBo;
067import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
068import org.kuali.rice.krad.service.impl.RemoteModuleServiceBase;
069
070public class KimRemoteModuleService extends RemoteModuleServiceBase {
071
072    private PersonService personService;
073    private RoleService kimRoleService;
074    private GroupService groupService;
075    private IdentityService identityService;
076    private KimTypeInfoService kimTypeInfoService;
077
078    public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass, Map<String, Object> fieldValues) {
079        if ( Person.class.isAssignableFrom( businessObjectClass ) ) {
080            if ( fieldValues.containsKey( KIMPropertyConstants.Person.PRINCIPAL_ID ) ) {
081                return (T) getPersonService().getPerson( (String)fieldValues.get( KIMPropertyConstants.Person.PRINCIPAL_ID ) );
082            } else if ( fieldValues.containsKey( KIMPropertyConstants.Person.PRINCIPAL_NAME ) ) {
083                return (T) getPersonService().getPersonByPrincipalName( (String)fieldValues.get( KIMPropertyConstants.Person.PRINCIPAL_NAME ) );
084            }
085            // otherwise, fall through since critieria is not known
086        } else if(RoleContract.class.isAssignableFrom(businessObjectClass)){
087            if(fieldValues.containsKey(KimConstants.PrimaryKeyConstants.ROLE_ID)){
088                Role role = getKimRoleService().getRole((String) fieldValues.get(
089                        KimConstants.PrimaryKeyConstants.ROLE_ID));
090                return (T) RoleBo.from(role);
091            }
092        } else if(GroupContract.class.isAssignableFrom(businessObjectClass)){
093            if(fieldValues.containsKey(KimConstants.PrimaryKeyConstants.GROUP_ID)) {
094                Group group = getGroupService().getGroup((String) fieldValues.get(
095                        KimConstants.PrimaryKeyConstants.GROUP_ID));
096                return (T) GroupBo.from(group);
097            }
098        } else if (EntityEmailTypeEbo.class.isAssignableFrom(businessObjectClass)) {
099            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}