001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.kim.service.impl;
017    
018    import org.kuali.rice.core.api.criteria.Predicate;
019    import org.kuali.rice.core.api.criteria.QueryByCriteria;
020    import org.kuali.rice.kew.api.KewApiConstants;
021    import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
022    import org.kuali.rice.kim.api.KimConstants;
023    import org.kuali.rice.kim.api.group.Group;
024    import org.kuali.rice.kim.api.group.GroupContract;
025    import org.kuali.rice.kim.api.group.GroupService;
026    import org.kuali.rice.kim.api.identity.CodedAttribute;
027    import org.kuali.rice.kim.api.identity.IdentityService;
028    import org.kuali.rice.kim.api.identity.Person;
029    import org.kuali.rice.kim.api.identity.PersonService;
030    import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationType;
031    import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierType;
032    import org.kuali.rice.kim.api.role.Role;
033    import org.kuali.rice.kim.api.role.RoleContract;
034    import org.kuali.rice.kim.api.role.RoleService;
035    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
036    import org.kuali.rice.kim.api.type.KimType;
037    import org.kuali.rice.kim.api.type.KimTypeContract;
038    import org.kuali.rice.kim.api.type.KimTypeInfoService;
039    import org.kuali.rice.kim.framework.group.GroupEbo;
040    import org.kuali.rice.kim.framework.identity.EntityTypeEbo;
041    import org.kuali.rice.kim.framework.identity.address.EntityAddressTypeEbo;
042    import org.kuali.rice.kim.framework.identity.affiliation.EntityAffiliationTypeEbo;
043    import org.kuali.rice.kim.framework.identity.citizenship.EntityCitizenshipStatusEbo;
044    import org.kuali.rice.kim.framework.identity.email.EntityEmailTypeContractEbo;
045    import org.kuali.rice.kim.framework.identity.email.EntityEmailTypeEbo;
046    import org.kuali.rice.kim.framework.identity.employment.EntityEmploymentStatusEbo;
047    import org.kuali.rice.kim.framework.identity.employment.EntityEmploymentTypeEbo;
048    import org.kuali.rice.kim.framework.identity.external.EntityExternalIdentifierTypeEbo;
049    import org.kuali.rice.kim.framework.identity.name.EntityNameTypeEbo;
050    import org.kuali.rice.kim.framework.identity.phone.EntityPhoneTypeEbo;
051    import org.kuali.rice.kim.framework.role.RoleEbo;
052    import org.kuali.rice.kim.impl.KIMPropertyConstants;
053    import org.kuali.rice.kim.impl.group.GroupBo;
054    import org.kuali.rice.kim.impl.role.RoleBo;
055    import org.kuali.rice.kim.util.KimCommonUtilsInternal;
056    import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
057    import org.kuali.rice.krad.service.impl.ModuleServiceBase;
058    import org.kuali.rice.krad.util.KRADConstants;
059    
060    import java.util.ArrayList;
061    import java.util.Collections;
062    import java.util.HashSet;
063    import java.util.List;
064    import java.util.Map;
065    import java.util.Properties;
066    import java.util.Set;
067    
068    import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
069    
070    /**
071     * This is a description of what this class does - kellerj don't forget to fill this in.
072     *
073     * @author Kuali Rice Team (rice.collab@kuali.org)
074     *
075     */
076    public class KimModuleService extends ModuleServiceBase {
077    
078            private PersonService personService;
079            private RoleService kimRoleService;
080            private GroupService groupService;
081        private IdentityService identityService;
082            private KimTypeInfoService kimTypeInfoService;
083    
084            /**
085             * This overridden method ...
086             *
087             * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObject(java.lang.Class, java.util.Map)
088             */
089            @SuppressWarnings("unchecked")
090            @Override
091            public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass, Map<String, Object> fieldValues) {
092                    if ( Person.class.isAssignableFrom( businessObjectClass ) ) {
093                            if ( fieldValues.containsKey( KIMPropertyConstants.Person.PRINCIPAL_ID ) ) {
094                                    return (T) getPersonService().getPerson( (String)fieldValues.get( KIMPropertyConstants.Person.PRINCIPAL_ID ) );
095                            } else if ( fieldValues.containsKey( KIMPropertyConstants.Person.PRINCIPAL_NAME ) ) {
096                                    return (T) getPersonService().getPersonByPrincipalName( (String)fieldValues.get( KIMPropertyConstants.Person.PRINCIPAL_NAME ) );
097                            }
098                            // otherwise, fall through since critieria is not known
099                    } else if(RoleContract.class.isAssignableFrom(businessObjectClass)){
100                            if(fieldValues.containsKey(KimConstants.PrimaryKeyConstants.ROLE_ID)){
101                                    Role role = getKimRoleService().getRole((String)fieldValues.get(KimConstants.PrimaryKeyConstants.ROLE_ID));
102                                    return (T) RoleEbo.from(role);
103                            }
104                    } else if(GroupContract.class.isAssignableFrom(businessObjectClass)){
105                            if(fieldValues.containsKey(KimConstants.PrimaryKeyConstants.GROUP_ID)) {
106                    Group group = getGroupService().getGroup((String)fieldValues.get(KimConstants.PrimaryKeyConstants.GROUP_ID));
107                                    return (T) GroupEbo.from(group);
108                            }
109                    } else if (EntityEmailTypeEbo.class.isAssignableFrom(businessObjectClass)) {
110                if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
111                    CodedAttribute codedAttribute = getIdentityService()
112                            .getEmailType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
113                    return (T)EntityEmailTypeEbo.from(codedAttribute);
114                }
115            } else if (EntityAddressTypeEbo.class.isAssignableFrom(businessObjectClass)) {
116                if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
117                    CodedAttribute codedAttribute = getIdentityService()
118                            .getAddressType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
119                    return (T)EntityAddressTypeEbo.from(codedAttribute);
120                }
121            } else if (EntityAffiliationTypeEbo.class.isAssignableFrom(businessObjectClass)) {
122                if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
123                    EntityAffiliationType codedAttribute = getIdentityService()
124                            .getAffiliationType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
125                    return (T)EntityAffiliationTypeEbo.from(codedAttribute);
126                }
127            } else if (EntityCitizenshipStatusEbo.class.isAssignableFrom(businessObjectClass)) {
128                if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
129                    CodedAttribute codedAttribute = getIdentityService()
130                            .getCitizenshipStatus((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
131                    return (T)EntityCitizenshipStatusEbo.from(codedAttribute);
132                }
133            } else if (EntityEmploymentStatusEbo.class.isAssignableFrom(businessObjectClass)) {
134                if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
135                    CodedAttribute codedAttribute = getIdentityService()
136                            .getEmploymentStatus((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
137                    return (T)EntityEmploymentStatusEbo.from(codedAttribute);
138                }
139            }  else if (EntityEmploymentTypeEbo.class.isAssignableFrom(businessObjectClass)) {
140                if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
141                    CodedAttribute codedAttribute = getIdentityService()
142                            .getEmploymentType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
143                    return (T)EntityEmploymentTypeEbo.from(codedAttribute);
144                }
145            } else if (EntityNameTypeEbo.class.isAssignableFrom(businessObjectClass)) {
146                if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
147                    CodedAttribute codedAttribute = getIdentityService()
148                            .getNameType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
149                    return (T)EntityNameTypeEbo.from(codedAttribute);
150                }
151            } else if (EntityTypeEbo.class.isAssignableFrom(businessObjectClass)) {
152                if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
153                    CodedAttribute codedAttribute = getIdentityService()
154                            .getEntityType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
155                    return (T)EntityTypeEbo.from(codedAttribute);
156                }
157            } else if (EntityExternalIdentifierTypeEbo.class.isAssignableFrom(businessObjectClass)) {
158                if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
159                    EntityExternalIdentifierType codedAttribute = getIdentityService()
160                            .getExternalIdentifierType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
161                    return (T)EntityExternalIdentifierTypeEbo.from(codedAttribute);
162                }
163            } else if (EntityPhoneTypeEbo.class.isAssignableFrom(businessObjectClass)) {
164                if (fieldValues.containsKey(KimConstants.PrimaryKeyConstants.CODE)) {
165                    CodedAttribute codedAttribute = getIdentityService()
166                            .getPhoneType((String) fieldValues.get(KimConstants.PrimaryKeyConstants.CODE));
167                    return (T)EntityPhoneTypeEbo.from(codedAttribute);
168                }
169            }
170                    // otherwise, use the default implementation
171                    return super.getExternalizableBusinessObject( businessObjectClass, fieldValues );
172            }
173    
174            /**
175             * This overridden method ...
176             *
177             * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObjectsList(java.lang.Class, java.util.Map)
178             */
179            @SuppressWarnings("unchecked")
180            @Override
181            public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList(
182                            Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues) {
183                    // for Person objects (which are not real PersistableBOs) pull them through the person service
184    
185                    if ( Person.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
186                            return (List)getPersonService().findPeople( (Map)fieldValues );
187                    }
188            else if ( RoleContract.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
189                List<Role> roles = getKimRoleService().findRoles(toQuery(fieldValues)).getResults();
190                List<RoleEbo> roleEbos = new ArrayList<RoleEbo>(roles.size());
191                for (Role role : roles) {
192                    roleEbos.add(RoleEbo.from(role));
193                }
194                return (List<T>)roleEbos;
195                    } else if ( GroupContract.class.isAssignableFrom(externalizableBusinessObjectClass) ) {
196                            List<Group> groups = getGroupService().findGroups(toQuery(fieldValues)).getResults();
197                List<GroupEbo> groupEbos = new ArrayList<GroupEbo>(groups.size());
198                for (Group group : groups) {
199                    groupEbos.add(GroupEbo.from(group));
200                }
201                return (List<T>)groupEbos;
202                    }
203                    // otherwise, use the default implementation
204                    return super.getExternalizableBusinessObjectsList( externalizableBusinessObjectClass, fieldValues );
205            }
206    
207        private QueryByCriteria toQuery(Map<String,?> fieldValues) {
208            Set<Predicate> preds = new HashSet<Predicate>();
209            for (String key : fieldValues.keySet()) {
210                preds.add(equal(key, fieldValues.get(key)));
211            }
212            Predicate[] predicates = new Predicate[0];
213            predicates = preds.toArray(predicates);
214            return QueryByCriteria.Builder.fromPredicates(predicates);
215        }
216    
217            /***
218             * @see org.kuali.rice.krad.service.ModuleService#getExternalizableBusinessObjectsListForLookup(java.lang.Class, java.util.Map, boolean)
219             */
220            @SuppressWarnings("unchecked")
221            @Override
222            public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsListForLookup(
223                            Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues, boolean unbounded) {
224                    // for Person objects (which are not real PersistableBOs) pull them through the person service
225                    if ( Person.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
226                            return (List)getPersonService().findPeople( (Map)fieldValues, unbounded );
227                    } else if ( RoleContract.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
228                            List<Role> roles = getKimRoleService().findRoles(toQuery(fieldValues)).getResults();
229                List<RoleEbo> roleEbos = new ArrayList<RoleEbo>(roles.size());
230                for (Role role : roles) {
231                    roleEbos.add(RoleEbo.from(role));
232                }
233                return (List<T>)roleEbos;
234                    } else if (GroupContract.class.isAssignableFrom( externalizableBusinessObjectClass)) {
235                List<Group> groups = getGroupService().findGroups(toQuery(fieldValues)).getResults();
236                List<GroupEbo> groupEbos = new ArrayList<GroupEbo>(groups.size());
237                for (Group group : groups) {
238                    groupEbos.add(GroupEbo.from(group));
239                }
240                return (List<T>)groupEbos;
241            }
242                    // otherwise, use the default implementation
243                    return super.getExternalizableBusinessObjectsListForLookup(externalizableBusinessObjectClass, fieldValues, unbounded);
244            }
245    
246            /**
247             * This overridden method ...
248             *
249             * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#listPrimaryKeyFieldNames(java.lang.Class)
250             */
251            @SuppressWarnings("unchecked")
252            @Override
253            public List listPrimaryKeyFieldNames(Class businessObjectInterfaceClass) {
254                    // for Person objects (which are not real PersistableBOs) pull them through the person service
255                    if ( Person.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
256                            return Collections.singletonList( KimConstants.PrimaryKeyConstants.PRINCIPAL_ID );
257                    } else if ( RoleEbo.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
258                            return Collections.singletonList( KimConstants.PrimaryKeyConstants.ROLE_ID );
259                    } else if ( GroupEbo.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
260                            return Collections.singletonList( KimConstants.PrimaryKeyConstants.GROUP_ID );
261                    } else if ( KimType.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
262                            return Collections.singletonList( KimConstants.PrimaryKeyConstants.KIM_TYPE_ID );
263                    } else if ( KimTypeContract.class.isAssignableFrom(businessObjectInterfaceClass)) {
264                            return Collections.singletonList( KimConstants.PrimaryKeyConstants.KIM_TYPE_CODE );
265                    }
266    
267                    // otherwise, use the default implementation
268                    return super.listPrimaryKeyFieldNames( businessObjectInterfaceClass );
269            }
270    
271            @SuppressWarnings("unchecked")
272            protected PersonService getPersonService() {
273                    if ( personService == null ) {
274                            personService = KimApiServiceLocator.getPersonService();
275                    }
276                    return personService;
277            }
278    
279            protected RoleService getKimRoleService() {
280                    if ( kimRoleService == null ) {
281                            kimRoleService = KimApiServiceLocator.getRoleService();
282                    }
283                    return kimRoleService;
284            }
285    
286            protected GroupService getGroupService() {
287                    if ( groupService == null ) {
288                            groupService = KimApiServiceLocator.getGroupService();
289                    }
290                    return groupService;
291            }
292    
293        protected IdentityService getIdentityService() {
294                    if ( identityService == null ) {
295                            identityService = KimApiServiceLocator.getIdentityService();
296                    }
297                    return identityService;
298            }
299    
300            protected KimTypeInfoService getTypeInfoService() {
301                    if(kimTypeInfoService == null){
302                            kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
303                    }
304                    return kimTypeInfoService;
305            }
306    
307            protected Properties getUrlParameters(String businessObjectClassAttribute, Map<String, String[]> parameters){
308                    Properties urlParameters = new Properties();
309                    for (String paramName : parameters.keySet()) {
310                            String[] parameterValues = parameters.get(paramName);
311                            if (parameterValues.length > 0) {
312                                    urlParameters.put(paramName, parameterValues[0]);
313                            }
314                    }
315                    urlParameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObjectClassAttribute);
316                    try{
317                            Class inquiryBusinessObjectClass = Class.forName(businessObjectClassAttribute);
318                            if(Role.class.isAssignableFrom(inquiryBusinessObjectClass) || 
319                                            Group.class.isAssignableFrom(inquiryBusinessObjectClass) || 
320                                            Person.class.isAssignableFrom(inquiryBusinessObjectClass)) {
321                            urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.PARAM_MAINTENANCE_VIEW_MODE_INQUIRY);
322                            } else{
323                            urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.CONTINUE_WITH_INQUIRY_METHOD_TO_CALL);
324                            }
325                    } catch(Exception eix){
326                            urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.CONTINUE_WITH_INQUIRY_METHOD_TO_CALL);
327                    }
328            urlParameters.put(KRADConstants.PARAMETER_COMMAND, KewApiConstants.INITIATE_COMMAND);
329                    return urlParameters;
330            }
331    
332            @Override
333            protected String getInquiryUrl(Class inquiryBusinessObjectClass){
334                    String inquiryUrl = KimCommonUtilsInternal.getKimBasePath();
335                    if (!inquiryUrl.endsWith("/")) {
336                            inquiryUrl = inquiryUrl + "/";
337                    }
338                    if(Role.class.isAssignableFrom(inquiryBusinessObjectClass)) {
339                            return inquiryUrl + KimConstants.KimUIConstants.KIM_ROLE_INQUIRY_ACTION;
340                    } else if(Group.class.isAssignableFrom(inquiryBusinessObjectClass)) {
341                            return inquiryUrl + KimConstants.KimUIConstants.KIM_GROUP_INQUIRY_ACTION;
342                    } else if(Person.class.isAssignableFrom(inquiryBusinessObjectClass)) {
343                            return inquiryUrl + KimConstants.KimUIConstants.KIM_PERSON_INQUIRY_ACTION;
344                    }
345                    return super.getInquiryUrl(inquiryBusinessObjectClass);
346            }
347    
348        @Override
349        public List<List<String>> listAlternatePrimaryKeyFieldNames(
350                Class businessObjectInterfaceClass) {
351                    if ( Person.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
352                ArrayList<List<String>> retList = new ArrayList<List<String>>();
353                ArrayList<String> keyList = new ArrayList<String>();
354    
355                keyList.add("principalName");
356                retList.add(keyList);
357                return retList;
358            }else{
359                return null;
360            }
361    
362        }
363    
364    }