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.identity;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.criteria.CriteriaLookupService;
20  import org.kuali.rice.core.api.criteria.GenericQueryResults;
21  import org.kuali.rice.core.api.criteria.QueryByCriteria;
22  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
23  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
24  import org.kuali.rice.kim.api.identity.CodedAttribute;
25  import org.kuali.rice.kim.api.identity.IdentityService;
26  import org.kuali.rice.kim.api.identity.address.EntityAddress;
27  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
28  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationType;
29  import org.kuali.rice.kim.api.identity.citizenship.EntityCitizenship;
30  import org.kuali.rice.kim.api.identity.email.EntityEmail;
31  import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
32  import org.kuali.rice.kim.api.identity.entity.Entity;
33  import org.kuali.rice.kim.api.identity.entity.EntityDefault;
34  import org.kuali.rice.kim.api.identity.entity.EntityDefaultQueryResults;
35  import org.kuali.rice.kim.api.identity.entity.EntityQueryResults;
36  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
37  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierType;
38  import org.kuali.rice.kim.api.identity.name.EntityName;
39  import org.kuali.rice.kim.api.identity.name.EntityNameQueryResults;
40  import org.kuali.rice.kim.api.identity.personal.EntityBioDemographics;
41  import org.kuali.rice.kim.api.identity.personal.EntityEthnicity;
42  import org.kuali.rice.kim.api.identity.phone.EntityPhone;
43  import org.kuali.rice.kim.api.identity.principal.Principal;
44  import org.kuali.rice.kim.api.identity.principal.PrincipalQueryResults;
45  import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
46  import org.kuali.rice.kim.api.identity.residency.EntityResidency;
47  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
48  import org.kuali.rice.kim.api.identity.visa.EntityVisa;
49  import org.kuali.rice.kim.impl.KIMPropertyConstants;
50  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
51  import org.kuali.rice.kim.impl.identity.address.EntityAddressTypeBo;
52  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
53  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationTypeBo;
54  import org.kuali.rice.kim.impl.identity.citizenship.EntityCitizenshipBo;
55  import org.kuali.rice.kim.impl.identity.citizenship.EntityCitizenshipStatusBo;
56  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
57  import org.kuali.rice.kim.impl.identity.email.EntityEmailTypeBo;
58  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
59  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentStatusBo;
60  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentTypeBo;
61  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
62  import org.kuali.rice.kim.impl.identity.external.EntityExternalIdentifierBo;
63  import org.kuali.rice.kim.impl.identity.external.EntityExternalIdentifierTypeBo;
64  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
65  import org.kuali.rice.kim.impl.identity.name.EntityNameTypeBo;
66  import org.kuali.rice.kim.impl.identity.personal.EntityBioDemographicsBo;
67  import org.kuali.rice.kim.impl.identity.personal.EntityEthnicityBo;
68  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
69  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneTypeBo;
70  import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
71  import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
72  import org.kuali.rice.kim.impl.identity.privacy.EntityPrivacyPreferencesBo;
73  import org.kuali.rice.kim.impl.identity.residency.EntityResidencyBo;
74  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
75  import org.kuali.rice.kim.impl.identity.visa.EntityVisaBo;
76  import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
77  import org.kuali.rice.krad.service.BusinessObjectService;
78  
79  import javax.jws.WebParam;
80  import java.util.ArrayList;
81  import java.util.Collection;
82  import java.util.Collections;
83  import java.util.HashMap;
84  import java.util.List;
85  import java.util.Map;
86  
87  /**
88   * Base implementation of the identity (identity) service.  This version assumes the KimEntity
89   * and related data is located within the KIM database. 
90   * 
91   * @author Kuali Rice Team (rice.collab@kuali.org)
92   */
93  
94  public class IdentityServiceImpl implements IdentityService {
95  
96      private CriteriaLookupService criteriaLookupService;
97  	private BusinessObjectService businessObjectService;
98  
99      @Override
100 	public Entity getEntity(String entityId) throws RiceIllegalArgumentException {
101         incomingParamCheck(entityId, "entityId");
102 
103 		EntityBo entity = getEntityBo( entityId );
104 		if ( entity == null ) {
105 			return null;
106 		}
107 		return EntityBo.to( entity );
108 	}
109 	
110     @Override
111 	public Entity getEntityByPrincipalId(String principalId) throws RiceIllegalArgumentException {
112         incomingParamCheck(principalId, "principalId");
113 
114 		EntityBo entity = getEntityBoByPrincipalId(principalId);
115 		if ( entity == null ) {
116 			return null;
117 		}
118 		return EntityBo.to(entity);
119 	}
120 	
121     @Override
122 	public Entity getEntityByPrincipalName(String principalName) throws RiceIllegalArgumentException{
123         incomingParamCheck(principalName, "principalName");
124 
125 		EntityBo entity = getEntityBoByPrincipalName(principalName);
126 		if ( entity == null ) {
127 			return null;
128 		}
129 		return EntityBo.to(entity);
130 	}
131     
132     @Override
133 	public Entity getEntityByEmployeeId(String employeeId) throws RiceIllegalArgumentException{
134         incomingParamCheck(employeeId, "employeeId");
135 
136 		EntityBo entity = getEntityBoByEmployeeId(employeeId);
137 		if ( entity == null ) {
138 			return null;
139 		}
140 		return EntityBo.to(entity);
141 	}
142 	
143     @Override
144 	public EntityDefault getEntityDefault(String entityId) throws RiceIllegalArgumentException {
145         incomingParamCheck(entityId, "entityId");
146 
147 		EntityBo entity = getEntityBo( entityId );
148 		if ( entity == null ) {
149 			return null;
150 		}
151 		return EntityBo.toDefault( entity );
152 	}
153 	
154     @Override
155 	public EntityDefault getEntityDefaultByPrincipalId(String principalId) throws RiceIllegalArgumentException {
156         incomingParamCheck(principalId, "principalId");
157 
158 		EntityBo entity = getEntityBoByPrincipalId(principalId);
159 		if ( entity == null ) {
160 			return null;
161 		}
162 		return EntityBo.toDefault(entity);
163 	}
164 	
165     @Override
166 	public EntityDefault getEntityDefaultByPrincipalName(String principalName) throws RiceIllegalArgumentException {
167         incomingParamCheck(principalName, "principalName");
168 
169 		EntityBo entity = getEntityBoByPrincipalName(principalName);
170 		if ( entity == null ) {
171 			return null;
172 		}
173 		return EntityBo.toDefault(entity);
174 	}
175 	
176     @Override
177     public EntityDefault getEntityDefaultByEmployeeId(String employeeId) throws RiceIllegalArgumentException {
178         incomingParamCheck(employeeId, "employeeId");
179 
180         EntityBo entity = getEntityBoByEmployeeId(employeeId);
181         if ( entity == null ) {
182             return null;
183         }
184         return EntityBo.toDefault(entity);
185     }
186     
187     @Override
188 	public Principal getPrincipalByPrincipalNameAndPassword(String principalName, String password) throws RiceIllegalArgumentException {
189         incomingParamCheck(principalName, "principalName");
190         incomingParamCheck(password, "password");
191 
192 		Map<String,Object> criteria = new HashMap<String,Object>(3);
193         criteria.put(KIMPropertyConstants.Principal.PRINCIPAL_NAME, principalName);
194         criteria.put(KIMPropertyConstants.Principal.PASSWORD, password);
195         criteria.put(KIMPropertyConstants.Principal.ACTIVE, Boolean.TRUE);
196         Collection<PrincipalBo> principals = businessObjectService.findMatching(PrincipalBo.class, criteria);
197 
198         if (!principals.isEmpty()) {
199             return PrincipalBo.to(principals.iterator().next());
200         }
201         return null;
202 	}
203 
204     @Override
205     public Principal addPrincipalToEntity(Principal principal) throws RiceIllegalArgumentException, RiceIllegalStateException {
206         incomingParamCheck(principal, "principal");
207 
208         if (StringUtils.isEmpty(principal.getEntityId()) || StringUtils.isBlank(principal.getEntityId())
209                 || StringUtils.isEmpty(principal.getPrincipalName()) || StringUtils.isBlank(principal.getPrincipalName())) {
210             throw new RiceIllegalStateException("Principal's entityId and PrincipalName must be populated before creation");
211         }  else {
212             if (getPrincipalByPrincipalName(principal.getPrincipalName()) != null) {
213                 throw new RiceIllegalStateException("the Principal to create already exists: " + principal);
214             }
215         }
216         PrincipalBo bo = PrincipalBo.from(principal);
217         return PrincipalBo.to(businessObjectService.save(bo));
218     }
219 
220     @Override
221     public Principal updatePrincipal(Principal principal) throws RiceIllegalArgumentException, RiceIllegalStateException {
222         incomingParamCheck(principal, "principal");
223         PrincipalBo originalPrincipal = null;
224         if (StringUtils.isEmpty(principal.getEntityId()) || StringUtils.isBlank(principal.getEntityId())
225                 || StringUtils.isEmpty(principal.getPrincipalName()) || StringUtils.isBlank(principal.getPrincipalName())) {
226             throw new RiceIllegalStateException("Principal's entityId and PrincipalName must be populated before update");
227         }  else {
228              originalPrincipal = getPrincipalBoByPrincipalName(principal.getPrincipalName());
229             if (StringUtils.isEmpty(principal.getPrincipalId()) || originalPrincipal == null) {
230                 throw new RiceIllegalStateException("the Principal to update does not exist: " + principal);
231             }
232         }
233         
234         PrincipalBo bo = PrincipalBo.from(principal);
235         //Password is not set on the principal DTO, so we need to make sure the value is kept from existing principal
236         bo.setPassword(originalPrincipal.getPassword());
237         PrincipalBo updatedPrincipal = businessObjectService.save(bo);
238         if (originalPrincipal.isActive()
239                 && !updatedPrincipal.isActive()) {
240             KimImplServiceLocator.getRoleInternalService().principalInactivated(updatedPrincipal.getPrincipalId());
241         }
242         return PrincipalBo.to(updatedPrincipal);
243     }
244 
245     @Override
246     public Principal inactivatePrincipal(String principalId) throws RiceIllegalArgumentException, RiceIllegalStateException {
247         incomingParamCheck(principalId, "principalId");
248 
249         Principal principal = getPrincipal(principalId);
250         if (principal == null) {
251             throw new RiceIllegalStateException("Principal with principalId: " + principalId + " does not exist");
252         }
253         PrincipalBo bo = PrincipalBo.from(principal);
254         bo.setActive(false);
255         return PrincipalBo.to(businessObjectService.save(bo));
256     }
257 
258     @Override
259     public Principal inactivatePrincipalByName(String principalName) throws RiceIllegalArgumentException, RiceIllegalStateException {
260         incomingParamCheck(principalName, "principalName");
261 
262         Principal principal = getPrincipalByPrincipalName(principalName);
263         if (principal == null) {
264             throw new RiceIllegalStateException("Principal with principalName: " + principalName + " does not exist");
265         }
266         PrincipalBo bo = PrincipalBo.from(principal);
267         bo.setActive(false);
268         return PrincipalBo.to(businessObjectService.save(bo));
269     }
270 
271     @Override
272     public EntityTypeContactInfo addEntityTypeContactInfoToEntity(EntityTypeContactInfo entityTypeData) throws RiceIllegalArgumentException, RiceIllegalStateException {
273         incomingParamCheck(entityTypeData, "entityTypeData");
274 
275         if (StringUtils.isEmpty(entityTypeData.getEntityId()) || StringUtils.isBlank(entityTypeData.getEntityId())
276                 || StringUtils.isEmpty(entityTypeData.getEntityTypeCode()) || StringUtils.isBlank(entityTypeData.getEntityTypeCode())) {
277             throw new RiceIllegalStateException("EntityTypeData's entityId and entityTypeCode must be populated before creation");
278         }  else {
279             if (getEntityTypeDataBo(entityTypeData.getEntityId(), entityTypeData.getEntityTypeCode()) != null) {
280                 throw new RiceIllegalStateException("the entityTypeData to create already exists: " + entityTypeData);
281             }
282         }
283         EntityTypeContactInfoBo bo = EntityTypeContactInfoBo.from(entityTypeData);
284         return EntityTypeContactInfoBo.to(businessObjectService.save(bo));
285     }
286 
287     private EntityTypeContactInfoBo getEntityTypeDataBo(String entityId, String entityTypeCode) {
288         Map<String,Object> criteria = new HashMap<String,Object>(3);
289          criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, entityId);
290          criteria.put(KIMPropertyConstants.Entity.ENTITY_TYPE_CODE, entityTypeCode);
291          criteria.put(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE);
292          return businessObjectService.findByPrimaryKey(EntityTypeContactInfoBo.class, criteria);
293     }
294 
295     @Override
296     public EntityTypeContactInfo updateEntityTypeContactInfo(EntityTypeContactInfo entityTypeContactInfo) throws RiceIllegalArgumentException, RiceIllegalStateException {
297         incomingParamCheck(entityTypeContactInfo, "entityTypeContactInfo");
298 
299         if (StringUtils.isBlank(entityTypeContactInfo.getEntityId()) || StringUtils.isEmpty(entityTypeContactInfo.getEntityId())
300                 || StringUtils.isBlank(entityTypeContactInfo.getEntityTypeCode()) || StringUtils.isEmpty(entityTypeContactInfo.getEntityTypeCode())) {
301             throw new RiceIllegalStateException("EntityTypeData's entityId and entityTypeCode must be populated before update");
302         }  else {
303             if (getEntityTypeDataBo(entityTypeContactInfo.getEntityId(), entityTypeContactInfo.getEntityTypeCode()) == null) {
304                 throw new RiceIllegalStateException("the entityTypeData to update does not exist: " + entityTypeContactInfo);
305             }
306         }
307         EntityTypeContactInfoBo bo = EntityTypeContactInfoBo.from(entityTypeContactInfo);
308         return EntityTypeContactInfoBo.to(businessObjectService.save(bo));
309     }
310 
311     @Override
312     public EntityTypeContactInfo inactivateEntityTypeContactInfo(String entityId, String entityTypeCode) throws RiceIllegalArgumentException, RiceIllegalStateException {
313         incomingParamCheck(entityId, "entityId");
314         incomingParamCheck(entityTypeCode, "entityTypeCode");
315 
316         EntityTypeContactInfoBo bo = getEntityTypeDataBo(entityId, entityTypeCode);
317         if (bo == null) {
318             throw new RiceIllegalStateException("EntityTypeData with entityId: " + entityId + " entityTypeCode: " + entityTypeCode + " does not exist");
319         }
320         bo.setActive(false);
321         return EntityTypeContactInfoBo.to(businessObjectService.save(bo));
322     }
323 
324     private EntityAddressBo getEntityAddressBo(String entityId, String entityTypeCode, String addressTypeCode) {
325         Map<String,Object> criteria = new HashMap<String,Object>(4);
326         criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, entityId);
327         criteria.put(KIMPropertyConstants.Entity.ENTITY_TYPE_CODE, entityTypeCode);
328         criteria.put("addressTypeCode", addressTypeCode);
329         criteria.put(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE);
330         return businessObjectService.findByPrimaryKey(EntityAddressBo.class, criteria);
331     }
332 
333     private EntityAddressBo getEntityAddressBo(String addressId) {
334         Map<String,Object> criteria = new HashMap<String,Object>(4);
335         criteria.put(KIMPropertyConstants.Entity.ID, addressId);
336         return businessObjectService.findByPrimaryKey(EntityAddressBo.class, criteria);
337     }
338 
339     @Override
340     public EntityAddress addAddressToEntity(EntityAddress address) throws RiceIllegalArgumentException, RiceIllegalStateException {
341         incomingParamCheck(address, "address");
342 
343         if (StringUtils.isEmpty(address.getEntityId()) || StringUtils.isBlank(address.getEntityId())
344                 || StringUtils.isEmpty(address.getEntityTypeCode()) || StringUtils.isBlank(address.getEntityTypeCode())) {
345             throw new RiceIllegalStateException("Address's entityId and entityTypeCode must be populated before creation");
346         }  else {
347             if (address.getAddressType() == null) {
348                 throw new RiceIllegalStateException("Address's type must be populated before creation");
349             }
350             if (getEntityAddressBo(address.getEntityId(), address.getEntityTypeCode(), address.getAddressType().getCode()) != null) {
351                 throw new RiceIllegalStateException("the address to create already exists: " + address);
352             }
353         }
354         EntityAddressBo bo = EntityAddressBo.from(address);
355         return EntityAddressBo.to(businessObjectService.save(bo));
356     }
357 
358     @Override
359     public EntityAddress updateAddress(EntityAddress address) throws RiceIllegalArgumentException, RiceIllegalStateException {
360         incomingParamCheck(address, "address");
361 
362         if (StringUtils.isEmpty(address.getEntityId()) || StringUtils.isBlank(address.getEntityId())
363                 || StringUtils.isEmpty(address.getEntityTypeCode()) || StringUtils.isBlank(address.getEntityTypeCode())) {
364             throw new RiceIllegalStateException("Address's entityId and entityTypeCode must be populated before creation");
365         }  else {
366             if (address.getAddressType() == null) {
367                 throw new RiceIllegalStateException("Address's type must be populated before creation");
368             }
369             if (StringUtils.isEmpty(address.getId())
370                   ||  getEntityAddressBo(address.getEntityId(), address.getEntityTypeCode(), address.getAddressType().getCode()) == null) {
371                 throw new RiceIllegalStateException("the address to update does not exists: " + address);
372             }
373         }
374         EntityAddressBo bo = EntityAddressBo.from(address);
375         return EntityAddressBo.to(businessObjectService.save(bo));
376     }
377 
378     @Override
379     public EntityAddress inactivateAddress(String addressId) throws RiceIllegalArgumentException, RiceIllegalStateException {
380         incomingParamCheck(addressId, "addressId");
381 
382         EntityAddressBo bo = getEntityAddressBo(addressId);
383         if (bo == null) {
384             throw new RiceIllegalStateException("Address with addressId: " + addressId + " does not exist");
385         }
386         bo.setActive(false);
387         return EntityAddressBo.to(businessObjectService.save(bo));
388     }
389 
390     private EntityEmailBo getEntityEmailBo(String entityId, String entityTypeCode, String emailTypeCode) {
391         Map<String,Object> criteria = new HashMap<String,Object>(4);
392         criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, entityId);
393         criteria.put(KIMPropertyConstants.Entity.ENTITY_TYPE_CODE, entityTypeCode);
394         criteria.put("emailTypeCode", emailTypeCode);
395         criteria.put(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE);
396         return businessObjectService.findByPrimaryKey(EntityEmailBo.class, criteria);
397     }
398 
399     private EntityEmailBo getEntityEmailBo(String emailId) {
400         Map<String,Object> criteria = new HashMap<String,Object>(4);
401         criteria.put(KIMPropertyConstants.Entity.ID, emailId);
402         return businessObjectService.findByPrimaryKey(EntityEmailBo.class, criteria);
403     }
404     @Override
405     public EntityEmail addEmailToEntity(EntityEmail email) throws RiceIllegalArgumentException, RiceIllegalStateException {
406         incomingParamCheck(email, "email");
407 
408         if (StringUtils.isEmpty(email.getEntityId()) || StringUtils.isBlank(email.getEntityId())
409                 || StringUtils.isEmpty(email.getEntityTypeCode()) || StringUtils.isBlank(email.getEntityTypeCode())) {
410             throw new RiceIllegalStateException("Email's entityId and entityTypeCode must be populated before creation");
411         }  else {
412             if (email.getEmailType() == null) {
413                 throw new RiceIllegalStateException("Email's type must be populated before creation");
414             }
415             if (getEntityEmailBo(email.getEntityId(), email.getEntityTypeCode(), email.getEmailType().getCode()) != null) {
416                 throw new RiceIllegalStateException("the email to create already exists: " + email);
417             }
418         }
419         EntityEmailBo bo = EntityEmailBo.from(email);
420         return EntityEmailBo.to(businessObjectService.save(bo));
421     }
422 
423     @Override
424     public EntityEmail updateEmail(EntityEmail email) throws RiceIllegalArgumentException, RiceIllegalStateException {
425         incomingParamCheck(email, "email");
426 
427         if (StringUtils.isEmpty(email.getEntityId()) || StringUtils.isBlank(email.getEntityId())
428                 || StringUtils.isEmpty(email.getEntityTypeCode()) || StringUtils.isBlank(email.getEntityTypeCode())) {
429             throw new RiceIllegalStateException("Email's entityId and entityTypeCode must be populated before creation");
430         }  else {
431             if (email.getEmailType() == null) {
432                 throw new RiceIllegalStateException("Email's type must be populated before creation");
433             }
434             if (StringUtils.isEmpty(email.getId())
435                   ||  getEntityEmailBo(email.getEntityId(), email.getEntityTypeCode(), email.getEmailType().getCode()) == null) {
436                 throw new RiceIllegalStateException("the email to update does not exists: " + email);
437             }
438         }
439         EntityEmailBo bo = EntityEmailBo.from(email);
440         return EntityEmailBo.to(businessObjectService.save(bo));
441     }
442 
443     @Override
444     public EntityEmail inactivateEmail(String emailId) throws RiceIllegalArgumentException, RiceIllegalStateException {
445         incomingParamCheck(emailId, "emailId");
446 
447         EntityEmailBo bo = getEntityEmailBo(emailId);
448         if (bo == null) {
449             throw new RiceIllegalStateException("Email with emailId: " + emailId + " does not exist");
450         }
451         bo.setActive(false);
452         return EntityEmailBo.to(businessObjectService.save(bo));
453     }
454 
455     private EntityPhoneBo getEntityPhoneBo(String entityId, String entityTypeCode, String phoneTypeCode) {
456         Map<String,Object> criteria = new HashMap<String,Object>(4);
457         criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, entityId);
458         criteria.put(KIMPropertyConstants.Entity.ENTITY_TYPE_CODE, entityTypeCode);
459         criteria.put("phoneTypeCode", phoneTypeCode);
460         criteria.put(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE);
461         return businessObjectService.findByPrimaryKey(EntityPhoneBo.class, criteria);
462     }
463 
464     private EntityPhoneBo getEntityPhoneBo(String phoneId) {
465         Map<String,Object> criteria = new HashMap<String,Object>(4);
466         criteria.put(KIMPropertyConstants.Entity.ID, phoneId);
467         return businessObjectService.findByPrimaryKey(EntityPhoneBo.class, criteria);
468     }
469 
470     @Override
471     public EntityPhone addPhoneToEntity(EntityPhone phone) throws RiceIllegalArgumentException, RiceIllegalStateException {
472         incomingParamCheck(phone, "phone");
473 
474         if (StringUtils.isEmpty(phone.getEntityId()) || StringUtils.isBlank(phone.getEntityId())
475                 || StringUtils.isEmpty(phone.getEntityTypeCode()) || StringUtils.isBlank(phone.getEntityTypeCode())) {
476             throw new RiceIllegalStateException("Phone's entityId and entityTypeCode must be populated before creation");
477         }  else {
478             if (phone.getPhoneType() == null) {
479                 throw new RiceIllegalStateException("Phone's type must be populated before creation");
480             }
481             if (getEntityPhoneBo(phone.getEntityId(), phone.getEntityTypeCode(), phone.getPhoneType().getCode()) != null) {
482                 throw new RiceIllegalStateException("the phone to create already exists: " + phone);
483             }
484         }
485         EntityPhoneBo bo = EntityPhoneBo.from(phone);
486         return EntityPhoneBo.to(businessObjectService.save(bo));
487     }
488 
489     @Override
490     public EntityPhone updatePhone(EntityPhone phone) throws RiceIllegalArgumentException, RiceIllegalStateException {
491         incomingParamCheck(phone, "phone");
492 
493         if (StringUtils.isEmpty(phone.getEntityId()) || StringUtils.isBlank(phone.getEntityId())
494                 || StringUtils.isEmpty(phone.getEntityTypeCode()) || StringUtils.isBlank(phone.getEntityTypeCode())) {
495             throw new RiceIllegalStateException("Phone's entityId and entityTypeCode must be populated before creation");
496         }  else {
497             if (phone.getPhoneType() == null) {
498                 throw new RiceIllegalStateException("Phone's type must be populated before creation");
499             }
500             if (StringUtils.isEmpty(phone.getId())
501                   ||  getEntityPhoneBo(phone.getEntityId(), phone.getEntityTypeCode(), phone.getPhoneType().getCode()) == null) {
502                 throw new RiceIllegalStateException("the phone to update does not exists: " + phone);
503             }
504         }
505         EntityPhoneBo bo = EntityPhoneBo.from(phone);
506         return EntityPhoneBo.to(businessObjectService.save(bo));
507     }
508 
509     @Override
510     public EntityPhone inactivatePhone(String phoneId) throws RiceIllegalArgumentException, RiceIllegalStateException {
511         incomingParamCheck(phoneId, "phoneId");
512 
513         EntityPhoneBo bo = getEntityPhoneBo(phoneId);
514         if (bo == null) {
515             throw new RiceIllegalStateException("Phone with phoneId: " + phoneId + " does not exist");
516         }
517         bo.setActive(false);
518         return EntityPhoneBo.to(businessObjectService.save(bo));
519     }
520 
521 
522     private EntityExternalIdentifierBo getEntityExternalIdentifierBo(String entityId, String externalIdentifierTypeCode) {
523         Map<String,Object> criteria = new HashMap<String,Object>(4);
524         criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, entityId);
525         criteria.put("externalIdentifierTypeCode", externalIdentifierTypeCode);
526         return businessObjectService.findByPrimaryKey(EntityExternalIdentifierBo.class, criteria);
527     }
528 
529     @Override
530     public EntityExternalIdentifier addExternalIdentifierToEntity(EntityExternalIdentifier externalId) throws RiceIllegalArgumentException, RiceIllegalStateException {
531         incomingParamCheck(externalId, "externalId");
532 
533         if (StringUtils.isEmpty(externalId.getEntityId()) || StringUtils.isBlank(externalId.getEntityId())
534                 || StringUtils.isEmpty(externalId.getExternalIdentifierTypeCode()) || StringUtils.isBlank(externalId.getExternalIdentifierTypeCode())) {
535             throw new RiceIllegalStateException("EntityExternalIdentifier's entityId and entityTypeCode must be populated before creation");
536         }  else {
537             if (getEntityExternalIdentifierBo(externalId.getEntityId(), externalId.getExternalIdentifierTypeCode()) != null) {
538                 throw new RiceIllegalStateException("the EntityExternalIdentifier to create already exists: " + externalId);
539             }
540         }
541         EntityExternalIdentifierBo bo = EntityExternalIdentifierBo.from(externalId);
542         return EntityExternalIdentifierBo.to(businessObjectService.save(bo));
543     }
544 
545     @Override
546     public EntityExternalIdentifier updateExternalIdentifier(EntityExternalIdentifier externalId) throws RiceIllegalArgumentException, RiceIllegalStateException {
547         incomingParamCheck(externalId, "externalId");
548 
549         if (StringUtils.isEmpty(externalId.getEntityId()) || StringUtils.isBlank(externalId.getEntityId())
550                 || StringUtils.isEmpty(externalId.getExternalIdentifierTypeCode()) || StringUtils.isBlank(externalId.getExternalIdentifierTypeCode())) {
551             throw new RiceIllegalStateException("EntityExternalIdentifier's entityId and externalIdentifierTypeCode must be populated before creation");
552         }  else {
553             if (StringUtils.isEmpty(externalId.getId())
554                   ||  getEntityExternalIdentifierBo(externalId.getEntityId(), externalId.getExternalIdentifierTypeCode()) == null) {
555                 throw new RiceIllegalStateException("the external identifier to update does not exist: " + externalId);
556             }
557         }
558         EntityExternalIdentifierBo bo = EntityExternalIdentifierBo.from(externalId);
559         return EntityExternalIdentifierBo.to(businessObjectService.save(bo));
560     }
561 
562 
563     private EntityAffiliationBo getEntityAffiliationBo(String id) {
564         Map<String,Object> criteria = new HashMap<String,Object>();
565         criteria.put(KIMPropertyConstants.Entity.ID, id);
566         return businessObjectService.findByPrimaryKey(EntityAffiliationBo.class, criteria);
567     }
568 
569     @Override
570     public EntityAffiliation addAffiliationToEntity(EntityAffiliation affiliation) throws RiceIllegalArgumentException, RiceIllegalStateException {
571         incomingParamCheck(affiliation, "affiliation");
572 
573         if (StringUtils.isEmpty(affiliation.getEntityId()) || StringUtils.isBlank(affiliation.getEntityId())) {
574             throw new RiceIllegalStateException("Affiliation's entityId must be populated before creation");
575         }  else {
576             if (affiliation.getAffiliationType() == null) {
577                 throw new RiceIllegalStateException("EntityAffiliation's type must be populated before creation");
578             }
579             if (getEntityAffiliationBo(affiliation.getId()) != null) {
580                 throw new RiceIllegalStateException("the EntityAffiliation to create already exists: " + affiliation);
581             }
582         }
583         EntityAffiliationBo bo = EntityAffiliationBo.from(affiliation);
584         return EntityAffiliationBo.to(businessObjectService.save(bo));
585     }
586 
587     @Override
588     public EntityAffiliation updateAffiliation(EntityAffiliation affiliation) throws RiceIllegalArgumentException, RiceIllegalStateException {
589         incomingParamCheck(affiliation, "affiliation");
590 
591         if (StringUtils.isEmpty(affiliation.getEntityId()) || StringUtils.isBlank(affiliation.getEntityId())) {
592             throw new RiceIllegalStateException("Affiliation's entityId must be populated before creation");
593         }  else {
594             if (affiliation.getAffiliationType() == null) {
595                 throw new RiceIllegalStateException("EntityAffiliation's type must be populated before creation");
596             }
597             if (StringUtils.isEmpty(affiliation.getId())
598                   ||  getEntityAffiliationBo(affiliation.getId()) == null) {
599                 throw new RiceIllegalStateException("the EntityAffiliation to update already exists: " + affiliation);
600             }
601         }
602         EntityAffiliationBo bo = EntityAffiliationBo.from(affiliation);
603         return EntityAffiliationBo.to(businessObjectService.save(bo));
604     }
605 
606     @Override
607     public EntityAffiliation inactivateAffiliation(String id) throws RiceIllegalArgumentException, RiceIllegalStateException {
608         incomingParamCheck(id, "id");
609 
610         EntityAffiliationBo bo = getEntityAffiliationBo(id);
611         if (bo == null) {
612             throw new RiceIllegalStateException("EntityAffiliation with id: " + id + " does not exist");
613         }
614         bo.setActive(false);
615         return EntityAffiliationBo.to(businessObjectService.save(bo));
616     }
617 
618     @Override
619 	public EntityQueryResults findEntities(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
620 		incomingParamCheck(queryByCriteria, "queryByCriteria");
621 
622         GenericQueryResults<EntityBo> results = criteriaLookupService.lookup(EntityBo.class, queryByCriteria);
623 
624         EntityQueryResults.Builder builder = EntityQueryResults.Builder.create();
625         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
626         builder.setTotalRowCount(results.getTotalRowCount());
627 
628         final List<Entity.Builder> ims = new ArrayList<Entity.Builder>();
629         for (EntityBo bo : results.getResults()) {
630             ims.add(Entity.Builder.create(bo));
631         }
632 
633         builder.setResults(ims);
634         return builder.build();
635 	}
636 
637     @Override
638 	public EntityDefaultQueryResults findEntityDefaults(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
639 		incomingParamCheck(queryByCriteria, "queryByCriteria");
640 
641         GenericQueryResults<EntityBo> results = criteriaLookupService.lookup(EntityBo.class, queryByCriteria);
642 
643         EntityDefaultQueryResults.Builder builder = EntityDefaultQueryResults.Builder.create();
644         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
645         builder.setTotalRowCount(results.getTotalRowCount());
646 
647         final List<EntityDefault.Builder> ims = new ArrayList<EntityDefault.Builder>();
648         for (EntityBo bo : results.getResults()) {
649             ims.add(EntityDefault.Builder.create(bo));
650         }
651 
652         builder.setResults(ims);
653         return builder.build();
654 	}
655 
656 	protected EntityNameQueryResults findNames(QueryByCriteria queryByCriteria) {
657 		incomingParamCheck(queryByCriteria, "queryByCriteria");
658 
659         GenericQueryResults<EntityNameBo> results = criteriaLookupService.lookup(EntityNameBo.class, queryByCriteria);
660 
661         EntityNameQueryResults.Builder builder = EntityNameQueryResults.Builder.create();
662         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
663         builder.setTotalRowCount(results.getTotalRowCount());
664 
665         final List<EntityName.Builder> ims = new ArrayList<EntityName.Builder>();
666         for (EntityNameBo bo : results.getResults()) {
667             ims.add(EntityName.Builder.create(bo));
668         }
669 
670         builder.setResults(ims);
671         return builder.build();
672 	}
673 
674     @Override
675 	public EntityPrivacyPreferences getEntityPrivacyPreferences(String entityId) throws RiceIllegalArgumentException {
676         incomingParamCheck(entityId, "entityId");
677 		Map<String,String> criteria = new HashMap<String,String>(1);
678         criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, entityId);
679 		return EntityPrivacyPreferencesBo.to(businessObjectService.findByPrimaryKey(EntityPrivacyPreferencesBo.class, criteria));
680 	}
681 
682     @Override
683 	public Principal getPrincipal(String principalId) throws RiceIllegalArgumentException {
684 		incomingParamCheck(principalId, "principalId");
685 
686         PrincipalBo principal = getPrincipalBo(principalId);
687 		if ( principal == null ) {
688 			return null;
689 		}
690 		return PrincipalBo.to(principal);
691 	}
692 
693     @Override
694     public List<Principal> getPrincipals (List<String> principalIds) {
695         List<Principal>  ret = new ArrayList<Principal>();
696         for(String p: principalIds) {
697             Principal principalInfo = getPrincipal(p);
698 
699             if (principalInfo != null) {
700                 ret.add(principalInfo) ;
701             }
702         }
703         return ret;
704     }
705 	
706 	private PrincipalBo getPrincipalBo(String principalId) {
707 		Map<String,String> criteria = new HashMap<String,String>(1);
708         criteria.put(KIMPropertyConstants.Principal.PRINCIPAL_ID, principalId);
709 		return businessObjectService.findByPrimaryKey(PrincipalBo.class, criteria);
710 	}
711 
712 	private EntityBo getEntityBo(String entityId) {
713 		return businessObjectService.findByPrimaryKey(EntityBo.class, Collections.singletonMap("id", entityId));
714 	}
715 
716 	@Override
717 	public Principal getPrincipalByPrincipalName(String principalName) throws RiceIllegalArgumentException {
718 		incomingParamCheck(principalName, "principalName");
719 
720 		return PrincipalBo.to(getPrincipalBoByPrincipalName(principalName));
721     }
722 
723     private PrincipalBo getPrincipalBoByPrincipalName(String principalName) throws RiceIllegalArgumentException {
724 
725         Map<String,Object> criteria = new HashMap<String,Object>(1);
726         criteria.put(KIMPropertyConstants.Principal.PRINCIPAL_NAME, principalName.toLowerCase());
727         Collection<PrincipalBo> principals = businessObjectService.findMatching(PrincipalBo.class, criteria);
728         if (!principals.isEmpty() && principals.size() == 1) {
729             return principals.iterator().next();
730         }
731         return null;
732     }
733 
734 	/**
735 	 * @see org.kuali.rice.kim.api.identity.IdentityService#getEntityByPrincipalName(java.lang.String)
736 	 */
737 	protected EntityBo getEntityBoByPrincipalName(String principalName) {
738 		if ( StringUtils.isBlank( principalName ) ) {
739 			return null;
740 		}
741         return getEntityByKeyValue("principals." + KIMPropertyConstants.Principal.PRINCIPAL_NAME, principalName.toLowerCase());
742 	}
743 
744 	/**
745 	 * @see org.kuali.rice.kim.api.identity.IdentityService#getEntityByPrincipalId(java.lang.String)
746 	 */
747 	protected EntityBo getEntityBoByPrincipalId(String principalId) {
748 		if ( StringUtils.isBlank( principalId ) ) {
749 			return null;
750 		}
751         return getEntityByKeyValue("principals." + KIMPropertyConstants.Principal.PRINCIPAL_ID, principalId);
752 	}
753 
754 	/**
755      * @see org.kuali.rice.kim.api.identity.IdentityService#getEntityByEmployeeId(java.lang.String)
756      */
757     protected EntityBo getEntityBoByEmployeeId(String employeeId) {
758         if ( StringUtils.isBlank( employeeId ) ) {
759             return null;
760         }
761         return getEntityByKeyValue("employmentInformation." + KIMPropertyConstants.Person.EMPLOYEE_ID, employeeId);
762     }
763     
764 	/**
765 	 * Generic helper method for performing a lookup through the business object service.
766 	 */
767 	protected EntityBo getEntityByKeyValue(String key, String value) {
768 		Map<String,String> criteria = new HashMap<String,String>(1);
769         criteria.put(key, value);
770         Collection<EntityBo> entities = businessObjectService.findMatching(EntityBo.class, criteria);
771         if (entities != null && entities.size() >= 1) {
772         	return entities.iterator().next();
773         }
774 		return null;
775 	}
776 
777     @Override
778 	public CodedAttribute getAddressType( String code ) throws RiceIllegalArgumentException {
779         incomingParamCheck(code, "code");
780 		EntityAddressTypeBo impl = businessObjectService.findBySinglePrimaryKey(EntityAddressTypeBo.class, code);
781 		if ( impl == null ) {
782 			return null;
783 		}
784 		return EntityAddressTypeBo.to(impl);
785 	}
786 
787     @Override
788     public List<CodedAttribute> findAllAddressTypes() {
789         List<EntityAddressTypeBo> bos = (List<EntityAddressTypeBo>)businessObjectService
790                 .findMatching(EntityAddressTypeBo.class, Collections.singletonMap(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE));
791 
792         List<CodedAttribute> codedAttributes = new ArrayList<CodedAttribute>();
793         for (EntityAddressTypeBo bo : bos) {
794             codedAttributes.add(EntityAddressTypeBo.to(bo));
795         }
796         return Collections.unmodifiableList(codedAttributes);
797     }
798 
799     @Override
800     public EntityAffiliationType getAffiliationType( String code ) throws RiceIllegalArgumentException {
801         incomingParamCheck(code, "code");
802 
803         EntityAffiliationTypeBo impl = businessObjectService.findBySinglePrimaryKey(EntityAffiliationTypeBo.class, code);
804 		if ( impl == null ) {
805 			return null;
806 		}
807 		return EntityAffiliationTypeBo.to(impl);
808 	}
809 
810     @Override
811     public List<EntityAffiliationType> findAllAffiliationTypes() {
812         List<EntityAffiliationTypeBo> bos = (List<EntityAffiliationTypeBo>)businessObjectService
813                 .findMatching(EntityAffiliationTypeBo.class, Collections.singletonMap(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE));
814 
815         List<EntityAffiliationType> codedAttributes = new ArrayList<EntityAffiliationType>();
816         for (EntityAffiliationTypeBo bo : bos) {
817             codedAttributes.add(EntityAffiliationTypeBo.to(bo));
818         }
819         return Collections.unmodifiableList(codedAttributes);
820     }
821 
822     @Override
823     public CodedAttribute getCitizenshipStatus( String code ) throws RiceIllegalArgumentException {
824 		incomingParamCheck(code, "code");
825         EntityCitizenshipStatusBo impl = businessObjectService.findBySinglePrimaryKey(EntityCitizenshipStatusBo.class, code);
826 		if ( impl == null ) {
827 			return null;
828 		}
829 		return EntityCitizenshipStatusBo.to(impl);
830 	}
831 
832     @Override
833     public List<CodedAttribute> findAllCitizenshipStatuses() {
834         List<EntityCitizenshipStatusBo> bos = (List<EntityCitizenshipStatusBo>)businessObjectService
835                 .findMatching(EntityCitizenshipStatusBo.class, Collections.singletonMap(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE));
836 
837         List<CodedAttribute> codedAttributes = new ArrayList<CodedAttribute>();
838         for (EntityCitizenshipStatusBo bo : bos) {
839             codedAttributes.add(EntityCitizenshipStatusBo.to(bo));
840         }
841         return Collections.unmodifiableList(codedAttributes);
842     }
843 
844     @Override
845     public CodedAttribute getEmailType( String code ) throws RiceIllegalArgumentException {
846 		incomingParamCheck(code, "code");
847         EntityEmailTypeBo impl = businessObjectService.findBySinglePrimaryKey(EntityEmailTypeBo.class, code);
848 		if ( impl == null ) {
849 			return null;
850 		}
851 		return EntityEmailTypeBo.to(impl);
852 	}
853 
854     @Override
855     public List<CodedAttribute> findAllEmailTypes() {
856         List<EntityEmailTypeBo> bos = (List<EntityEmailTypeBo>)businessObjectService
857                 .findMatching(EntityEmailTypeBo.class, Collections.singletonMap(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE));
858 
859         List<CodedAttribute> codedAttributes = new ArrayList<CodedAttribute>();
860         for (EntityEmailTypeBo bo : bos) {
861             codedAttributes.add(EntityEmailTypeBo.to(bo));
862         }
863         return Collections.unmodifiableList(codedAttributes);
864     }
865 
866     @Override
867     public PrincipalQueryResults findPrincipals(
868             @WebParam(name = "query") QueryByCriteria query) throws RiceIllegalArgumentException {
869         incomingParamCheck(query, "query");
870 
871         GenericQueryResults<PrincipalBo> results = criteriaLookupService.lookup(PrincipalBo.class, query);
872 
873         PrincipalQueryResults.Builder builder = PrincipalQueryResults.Builder.create();
874         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
875         builder.setTotalRowCount(results.getTotalRowCount());
876 
877         final List<Principal.Builder> ims = new ArrayList<Principal.Builder>();
878         for (PrincipalBo bo : results.getResults()) {
879             ims.add(Principal.Builder.create(bo));
880         }
881 
882         builder.setResults(ims);
883         return builder.build();
884     }
885 
886     @Override
887     public CodedAttribute getEmploymentStatus( String code ) throws RiceIllegalArgumentException {
888 		incomingParamCheck(code, "code");
889         EntityEmploymentStatusBo impl = businessObjectService.findBySinglePrimaryKey(EntityEmploymentStatusBo.class, code);
890 		if ( impl == null ) {
891 			return null;
892 		}
893 		return EntityEmploymentStatusBo.to(impl);
894 	}
895 
896     @Override
897     public List<CodedAttribute> findAllEmploymentStatuses() {
898         List<EntityEmploymentStatusBo> bos = (List<EntityEmploymentStatusBo>)businessObjectService
899                 .findMatching(EntityEmploymentStatusBo.class, Collections.singletonMap(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE));
900 
901         List<CodedAttribute> codedAttributes = new ArrayList<CodedAttribute>();
902         for (EntityEmploymentStatusBo bo : bos) {
903             codedAttributes.add(EntityEmploymentStatusBo.to(bo));
904         }
905         return Collections.unmodifiableList(codedAttributes);
906     }
907 
908     @Override
909     public CodedAttribute getEmploymentType( String code ) throws RiceIllegalArgumentException {
910 		incomingParamCheck(code, "code");
911         EntityEmploymentTypeBo impl = businessObjectService.findBySinglePrimaryKey(EntityEmploymentTypeBo.class, code);
912 		if ( impl == null ) {
913 			return null;
914 		}
915 		return EntityEmploymentTypeBo.to(impl);
916 	}
917 
918     @Override
919     public List<CodedAttribute> findAllEmploymentTypes() {
920         List<EntityEmploymentTypeBo> bos = (List<EntityEmploymentTypeBo>)businessObjectService
921                 .findMatching(EntityEmploymentTypeBo.class, Collections.singletonMap(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE));
922 
923         List<CodedAttribute> codedAttributes = new ArrayList<CodedAttribute>();
924         for (EntityEmploymentTypeBo bo : bos) {
925             codedAttributes.add(EntityEmploymentTypeBo.to(bo));
926         }
927         return Collections.unmodifiableList(codedAttributes);
928     }
929 
930     @Override
931     public CodedAttribute getNameType(String code) throws RiceIllegalArgumentException {
932 		incomingParamCheck(code, "code");
933         EntityNameTypeBo impl = businessObjectService.findBySinglePrimaryKey(EntityNameTypeBo.class, code);
934 		if ( impl == null ) {
935 			return null;
936 		}
937 		return EntityNameTypeBo.to(impl);
938 	}
939 
940     @Override
941     public List<CodedAttribute> findAllNameTypes() {
942         List<EntityNameTypeBo> bos = (List<EntityNameTypeBo>)businessObjectService
943                 .findMatching(EntityNameTypeBo.class, Collections.singletonMap(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE));
944 
945         List<CodedAttribute> codedAttributes = new ArrayList<CodedAttribute>();
946         for (EntityNameTypeBo bo : bos) {
947             codedAttributes.add(EntityNameTypeBo.to(bo));
948         }
949         return Collections.unmodifiableList(codedAttributes);
950     }
951 
952     @Override
953     public CodedAttribute getEntityType( String code ) throws RiceIllegalArgumentException {
954 		incomingParamCheck(code, "code");
955         EntityTypeBo impl = businessObjectService.findBySinglePrimaryKey(EntityTypeBo.class, code);
956 		if ( impl == null ) {
957 			return null;
958 		}
959 		return EntityTypeBo.to(impl);
960 	}
961 
962     @Override
963     public List<CodedAttribute> findAllEntityTypes() {
964         List<EntityTypeBo> bos = (List<EntityTypeBo>)businessObjectService
965                 .findMatching(EntityTypeBo.class, Collections.singletonMap(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE));
966         
967         List<CodedAttribute> codedAttributes = new ArrayList<CodedAttribute>();
968         for (EntityTypeBo bo : bos) {
969             codedAttributes.add(EntityTypeBo.to(bo));    
970         }
971         return Collections.unmodifiableList(codedAttributes);
972     }
973 
974     @Override
975     public EntityExternalIdentifierType getExternalIdentifierType( String code ) throws RiceIllegalArgumentException {
976 		incomingParamCheck(code, "code");
977 
978         EntityExternalIdentifierTypeBo impl = businessObjectService.findBySinglePrimaryKey(EntityExternalIdentifierTypeBo.class, code);
979 		if ( impl == null ) {
980 			return null;
981 		}
982 		return EntityExternalIdentifierTypeBo.to(impl);
983 	}
984 
985     @Override
986     public List<EntityExternalIdentifierType> findAllExternalIdendtifierTypes() {
987         List<EntityExternalIdentifierTypeBo> bos = (List<EntityExternalIdentifierTypeBo>)businessObjectService
988                 .findMatching(EntityExternalIdentifierTypeBo.class, Collections.singletonMap(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE));
989 
990         List<EntityExternalIdentifierType> codedAttributes = new ArrayList<EntityExternalIdentifierType>();
991         for (EntityExternalIdentifierTypeBo bo : bos) {
992             codedAttributes.add(EntityExternalIdentifierTypeBo.to(bo));
993         }
994         return Collections.unmodifiableList(codedAttributes);
995     }
996 
997     @Override
998     public CodedAttribute getPhoneType( String code ) throws RiceIllegalArgumentException {
999 		incomingParamCheck(code, "code");
1000         EntityPhoneTypeBo impl = businessObjectService.findBySinglePrimaryKey(EntityPhoneTypeBo.class, code);
1001 		if ( impl == null ) {
1002 			return null;
1003 		}
1004 		return EntityPhoneTypeBo.to(impl);
1005 	}
1006 
1007     @Override
1008     public List<CodedAttribute> findAllPhoneTypes() {
1009         List<EntityPhoneTypeBo> bos = (List<EntityPhoneTypeBo>)businessObjectService
1010                 .findMatching(EntityPhoneTypeBo.class, Collections.singletonMap(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE));
1011 
1012         List<CodedAttribute> codedAttributes = new ArrayList<CodedAttribute>();
1013         for (EntityPhoneTypeBo bo : bos) {
1014             codedAttributes.add(EntityPhoneTypeBo.to(bo));
1015         }
1016         return Collections.unmodifiableList(codedAttributes);
1017     }
1018 
1019     @Override
1020     public Entity createEntity(Entity entity) throws RiceIllegalArgumentException, RiceIllegalStateException {
1021         incomingParamCheck(entity, "entity");
1022 
1023         if (StringUtils.isNotBlank(entity.getId()) && getEntity(entity.getId()) != null) {
1024             throw new RiceIllegalStateException("the Entity to create already exists: " + entity);
1025         }
1026 
1027         EntityBo bo = EntityBo.from(entity);
1028         return EntityBo.to(businessObjectService.save(bo));
1029     }
1030 
1031     @Override
1032     public Entity updateEntity(Entity entity) throws RiceIllegalArgumentException, RiceIllegalStateException {
1033         incomingParamCheck(entity, "entity");
1034         EntityBo oldBo = null;
1035         Map<String, String> passwdMap = new HashMap<String, String>();
1036         if (StringUtils.isBlank(entity.getId())) {
1037             throw new RiceIllegalStateException("the Entity does not exist: " + entity);
1038         } else {
1039             oldBo = getEntityBo(entity.getId());
1040             if (oldBo == null) {
1041                 throw new RiceIllegalStateException("the Entity does not exist: " + entity);    
1042             }
1043         }
1044 
1045         for (PrincipalBo principalBo : oldBo.getPrincipals()) {
1046             passwdMap.put(principalBo.getPrincipalId(), principalBo.getPassword());
1047         }
1048         EntityBo bo = EntityBo.from(entity);
1049         for (PrincipalBo principal : bo.getPrincipals()) {
1050             principal.setPassword(passwdMap.get(principal.getPrincipalId()));
1051         }
1052         return EntityBo.to(businessObjectService.save(bo));
1053     }
1054 
1055 
1056 
1057     @Override
1058     public Entity inactivateEntity(String entityId) throws RiceIllegalArgumentException, RiceIllegalStateException {
1059         incomingParamCheck(entityId, "entityId");
1060 
1061         EntityBo entity = getEntityBo(entityId);
1062         if (entity == null) {
1063             throw new RiceIllegalStateException("an Entity does not exist for entityId: " + entityId);
1064         }
1065 
1066         entity.setActive(false);
1067         return EntityBo.to(businessObjectService.save(entity));
1068     }
1069 
1070     @Override
1071     public EntityPrivacyPreferences addPrivacyPreferencesToEntity(EntityPrivacyPreferences privacyPreferences) throws RiceIllegalArgumentException, RiceIllegalStateException {
1072         incomingParamCheck(privacyPreferences, "privacyPreferences");
1073 
1074         if (StringUtils.isEmpty(privacyPreferences.getEntityId()) || StringUtils.isBlank(privacyPreferences.getEntityId())) {
1075             throw new RiceIllegalStateException("PrivacyPreferences' entityId must be populated before creation");
1076         }  else {
1077             if (getEntityPrivacyPreferences(privacyPreferences.getEntityId()) != null) {
1078                 throw new RiceIllegalStateException("the PrivacyPreferences to create already exists: " + privacyPreferences);
1079             }
1080         }
1081         EntityPrivacyPreferencesBo bo = EntityPrivacyPreferencesBo.from(privacyPreferences);
1082         return EntityPrivacyPreferencesBo.to(businessObjectService.save(bo));
1083     }
1084 
1085     @Override
1086     public EntityPrivacyPreferences updatePrivacyPreferences(EntityPrivacyPreferences privacyPreferences) throws RiceIllegalArgumentException, RiceIllegalStateException {
1087         incomingParamCheck(privacyPreferences, "privacyPreferences");
1088 
1089         if (StringUtils.isEmpty(privacyPreferences.getEntityId()) || StringUtils.isBlank(privacyPreferences.getEntityId())) {
1090             throw new RiceIllegalStateException("PrivacyPreferences' entityId must be populated before update");
1091         }  else {
1092             if (getEntityPrivacyPreferences(privacyPreferences.getEntityId()) == null) {
1093                 throw new RiceIllegalStateException("the PrivacyPreferences to update does not exist: " + privacyPreferences);
1094             }
1095         }
1096         EntityPrivacyPreferencesBo bo = EntityPrivacyPreferencesBo.from(privacyPreferences);
1097         return EntityPrivacyPreferencesBo.to(businessObjectService.save(bo));
1098     }
1099 
1100     private EntityCitizenshipBo getEntityCitizenshipBo(String entityId, String citizenshipStatusCode) {
1101         if (StringUtils.isEmpty(entityId) || StringUtils.isEmpty(citizenshipStatusCode)) {
1102             return null;
1103         }
1104         Map<String,Object> criteria = new HashMap<String,Object>(4);
1105         criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, entityId);
1106         criteria.put("statusCode", citizenshipStatusCode);
1107         criteria.put(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE);
1108         return businessObjectService.findByPrimaryKey(EntityCitizenshipBo.class, criteria);
1109     }
1110 
1111     private EntityCitizenshipBo getEntityCitizenshipBo(String id) {
1112         if (StringUtils.isEmpty(id)) {
1113             return null;
1114         }
1115         Map<String,Object> criteria = new HashMap<String,Object>();
1116         criteria.put(KIMPropertyConstants.Entity.ID, id);
1117         criteria.put(KIMPropertyConstants.Entity.ACTIVE, Boolean.TRUE);
1118         return businessObjectService.findByPrimaryKey(EntityCitizenshipBo.class, criteria);
1119     }
1120 
1121     @Override
1122     public EntityCitizenship addCitizenshipToEntity(EntityCitizenship citizenship) throws RiceIllegalArgumentException, RiceIllegalStateException {
1123         incomingParamCheck(citizenship, "citizenship");
1124 
1125         if (StringUtils.isEmpty(citizenship.getEntityId()) || StringUtils.isBlank(citizenship.getEntityId())) {
1126             throw new RiceIllegalStateException("Citizenship's entityId must be populated before creation");
1127         }  else {
1128             if (citizenship.getStatus() == null) {
1129                 throw new RiceIllegalStateException("Citizenship's status must be populated before creation");
1130             }
1131             if (getEntityCitizenshipBo(citizenship.getEntityId(), citizenship.getStatus().getCode()) != null) {
1132                 throw new RiceIllegalStateException("the EntityCitizenship to create already exists: " + citizenship);
1133             }
1134         }
1135         EntityCitizenshipBo bo = EntityCitizenshipBo.from(citizenship);
1136         return EntityCitizenshipBo.to(businessObjectService.save(bo));
1137     }
1138 
1139     @Override
1140     public EntityCitizenship updateCitizenship(EntityCitizenship citizenship) throws RiceIllegalArgumentException, RiceIllegalStateException {
1141         incomingParamCheck(citizenship, "citizenship");
1142 
1143         if (StringUtils.isEmpty(citizenship.getEntityId()) || StringUtils.isBlank(citizenship.getEntityId())) {
1144             throw new RiceIllegalStateException("Email's entityId must be populated before creation");
1145         }  else {
1146             if (citizenship.getStatus() == null) {
1147                 throw new RiceIllegalStateException("Citizenship's status must be populated before creation");
1148             }
1149             if (getEntityCitizenshipBo(citizenship.getEntityId(), citizenship.getStatus().getCode()) == null) {
1150                 throw new RiceIllegalStateException("the EntityCitizenship to update does not exist: " + citizenship);
1151             }
1152         }
1153         EntityCitizenshipBo bo = EntityCitizenshipBo.from(citizenship);
1154         return EntityCitizenshipBo.to(businessObjectService.save(bo));
1155     }
1156 
1157     @Override
1158     public EntityCitizenship inactivateCitizenship(String id) throws RiceIllegalArgumentException, RiceIllegalStateException {
1159         incomingParamCheck(id, "id");
1160 
1161         EntityCitizenshipBo bo = getEntityCitizenshipBo(id);
1162         if (bo == null) {
1163             throw new RiceIllegalStateException("the EntityCitizenship with id: " + id + " does not exist");
1164         }
1165         bo.setActive(false);
1166         return EntityCitizenshipBo.to(businessObjectService.save(bo));
1167     }
1168 
1169     private EntityEthnicityBo getEntityEthnicityBo(String ethnicityId) {
1170         if (StringUtils.isEmpty(ethnicityId)) {
1171             return null;
1172         }
1173         Map<String,Object> criteria = new HashMap<String,Object>();
1174         criteria.put(KIMPropertyConstants.Entity.ID, ethnicityId);
1175         return businessObjectService.findByPrimaryKey(EntityEthnicityBo.class, criteria);
1176     }
1177     @Override
1178     public EntityEthnicity addEthnicityToEntity(EntityEthnicity ethnicity) throws RiceIllegalArgumentException {
1179         incomingParamCheck(ethnicity, "ethnicity");
1180 
1181         if (StringUtils.isEmpty(ethnicity.getEntityId()) || StringUtils.isBlank(ethnicity.getEntityId())) {
1182             throw new RiceIllegalStateException("Ethnicity's entityId must be populated before creation");
1183         }  else {
1184             if (StringUtils.isNotEmpty(ethnicity.getId()) && getEntityEthnicityBo(ethnicity.getId()) != null) {
1185                 throw new RiceIllegalStateException("the EntityEthnicity to create already exists: " + ethnicity);
1186             }
1187         }
1188         EntityEthnicityBo bo = EntityEthnicityBo.from(ethnicity);
1189         return EntityEthnicityBo.to(businessObjectService.save(bo));
1190     }
1191 
1192     @Override
1193     public EntityEthnicity updateEthnicity(EntityEthnicity ethnicity) throws RiceIllegalArgumentException, RiceIllegalStateException {
1194         incomingParamCheck(ethnicity, "ethnicity");
1195 
1196         if (StringUtils.isEmpty(ethnicity.getEntityId()) || StringUtils.isBlank(ethnicity.getEntityId())) {
1197             throw new RiceIllegalStateException("Ethnicity's entityId must be populated before creation");
1198         }  else {
1199             if (StringUtils.isEmpty(ethnicity.getId()) || getEntityEthnicityBo(ethnicity.getId()) == null) {
1200                 throw new RiceIllegalStateException("the EntityEthnicity to update does not exist: " + ethnicity);
1201             }
1202         }
1203         EntityEthnicityBo bo = EntityEthnicityBo.from(ethnicity);
1204         return EntityEthnicityBo.to(businessObjectService.save(bo));
1205     }
1206 
1207     private EntityResidencyBo getEntityResidencyBo(String residencyId) {
1208         if (StringUtils.isEmpty(residencyId)) {
1209             return null;
1210         }
1211         Map<String,Object> criteria = new HashMap<String,Object>();
1212         criteria.put(KIMPropertyConstants.Entity.ID, residencyId);
1213         return businessObjectService.findByPrimaryKey(EntityResidencyBo.class, criteria);
1214     }
1215 
1216     @Override
1217     public EntityResidency addResidencyToEntity(EntityResidency residency) throws RiceIllegalArgumentException, RiceIllegalStateException {
1218         incomingParamCheck(residency, "residency");
1219 
1220         if (StringUtils.isEmpty(residency.getEntityId()) || StringUtils.isBlank(residency.getEntityId())) {
1221             throw new RiceIllegalStateException("Residency's entityId must be populated before creation");
1222         }  else {
1223             if (StringUtils.isNotEmpty(residency.getId()) && getEntityResidencyBo(residency.getId()) != null) {
1224                 throw new RiceIllegalStateException("the EntityResidency to create already exists: " + residency);
1225             }
1226         }
1227         EntityResidencyBo bo = EntityResidencyBo.from(residency);
1228         return EntityResidencyBo.to(businessObjectService.save(bo));
1229     }
1230 
1231     @Override
1232     public EntityResidency updateResidency(EntityResidency residency) throws RiceIllegalArgumentException, RiceIllegalStateException {
1233         incomingParamCheck(residency, "residency");
1234 
1235         if (StringUtils.isEmpty(residency.getEntityId()) || StringUtils.isBlank(residency.getEntityId())) {
1236             throw new RiceIllegalStateException("Residency's entityId must be populated before creation");
1237         }  else {
1238             if (StringUtils.isEmpty(residency.getId()) || getEntityResidencyBo(residency.getId()) == null) {
1239                 throw new RiceIllegalStateException("the EntityResidency to update does not exist: " + residency);
1240             }
1241         }
1242         EntityResidencyBo bo = EntityResidencyBo.from(residency);
1243         return EntityResidencyBo.to(businessObjectService.save(bo));
1244     }
1245 
1246     private EntityVisaBo getEntityVisaBo(String visaId) {
1247         if (StringUtils.isEmpty(visaId)) {
1248             return null;
1249         }
1250         Map<String,Object> criteria = new HashMap<String,Object>();
1251         criteria.put(KIMPropertyConstants.Entity.ID, visaId);
1252         return businessObjectService.findByPrimaryKey(EntityVisaBo.class, criteria);
1253     }
1254 
1255     @Override
1256     public EntityVisa addVisaToEntity(EntityVisa visa) throws RiceIllegalArgumentException, RiceIllegalStateException {
1257         incomingParamCheck(visa, "visa");
1258 
1259         if (StringUtils.isEmpty(visa.getEntityId()) || StringUtils.isBlank(visa.getEntityId())) {
1260             throw new RiceIllegalStateException("Visa's entityId must be populated before creation");
1261         }  else {
1262             if (StringUtils.isNotEmpty(visa.getId()) && getEntityVisaBo(visa.getId()) != null) {
1263                 throw new RiceIllegalStateException("the EntityVisa to create already exists: " + visa);
1264             }
1265         }
1266         EntityVisaBo bo = EntityVisaBo.from(visa);
1267         return EntityVisaBo.to(businessObjectService.save(bo));
1268     }
1269 
1270     @Override
1271     public EntityVisa updateVisa(EntityVisa visa) throws RiceIllegalArgumentException, RiceIllegalStateException {
1272         incomingParamCheck(visa, "visa");
1273 
1274         if (StringUtils.isEmpty(visa.getEntityId()) || StringUtils.isBlank(visa.getEntityId())) {
1275             throw new RiceIllegalStateException("Visa's entityId must be populated before creation");
1276         }  else {
1277             if (StringUtils.isEmpty(visa.getId()) || getEntityVisaBo(visa.getId()) == null) {
1278                 throw new RiceIllegalStateException("the EntityVisa to update does not exist: " + visa);
1279             }
1280         }
1281         EntityVisaBo bo = EntityVisaBo.from(visa);
1282         return EntityVisaBo.to(businessObjectService.save(bo));
1283     }
1284 
1285     private EntityNameBo getEntityNameBo(String entityId, String nameTypeCode) {
1286         if (StringUtils.isEmpty(entityId) || StringUtils.isEmpty(nameTypeCode)) {
1287             return null;
1288         }
1289         Map<String,Object> criteria = new HashMap<String,Object>();
1290         criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, entityId);
1291         criteria.put("nameCode", nameTypeCode);
1292         criteria.put(KIMPropertyConstants.Entity.ACTIVE, "Y");
1293         return businessObjectService.findByPrimaryKey(EntityNameBo.class, criteria);
1294     }
1295 
1296     private EntityNameBo getEntityNameBo(String id) {
1297         if (StringUtils.isEmpty(id)) {
1298             return null;
1299         }
1300         Map<String,Object> criteria = new HashMap<String,Object>();
1301         criteria.put(KIMPropertyConstants.Entity.ID, id);
1302         criteria.put(KIMPropertyConstants.Entity.ACTIVE, "Y");
1303         return businessObjectService.findByPrimaryKey(EntityNameBo.class, criteria);
1304     }
1305     
1306     @Override
1307     public EntityNamePrincipalName getDefaultNamesForPrincipalId(String principalId) {
1308     	EntityNamePrincipalName.Builder nameBuilder = EntityNamePrincipalName.Builder.create();
1309     	Map<String,String> criteria = new HashMap<String,String>();
1310     	criteria.put(KIMPropertyConstants.Principal.PRINCIPAL_ID, principalId);
1311     	PrincipalBo principal = (PrincipalBo) businessObjectService.findByPrimaryKey(PrincipalBo.class, criteria);
1312 
1313     	if (null != principal) {
1314     		nameBuilder.setPrincipalName(principal.getPrincipalName());
1315 
1316     		criteria.clear();
1317     		criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, principal.getEntityId());
1318     		criteria.put("DFLT_IND", "Y");
1319     		criteria.put("ACTV_IND", "Y");
1320     		EntityNameBo name = (EntityNameBo) businessObjectService.findByPrimaryKey(EntityNameBo.class, criteria);
1321 
1322     		if (name == null) {
1323     			// to make this simple for now, assume if there is no default name that this is a system entity we are dealing with here
1324     			EntityName.Builder defaultNameBuilder = EntityName.Builder.create();
1325     			defaultNameBuilder.setLastName(principal.getPrincipalName().toUpperCase());
1326     			nameBuilder.setDefaultName(defaultNameBuilder);
1327     		} else {
1328     			nameBuilder.setDefaultName( EntityName.Builder.create(name) );
1329     		}
1330     		EntityNamePrincipalName entityNamePrincipalName = nameBuilder.build(); 
1331     		return entityNamePrincipalName;
1332     	}
1333     	return null;
1334     }
1335 
1336     @Override
1337     public EntityName addNameToEntity(EntityName name) throws RiceIllegalArgumentException, RiceIllegalStateException {
1338         incomingParamCheck(name, "name");
1339 
1340         if (StringUtils.isEmpty(name.getEntityId()) || StringUtils.isBlank(name.getEntityId())) {
1341             throw new RiceIllegalStateException("Name's entityId must be populated before creation");
1342         }  else {
1343             if (name.getNameType() == null) {
1344                 throw new RiceIllegalStateException("EntityName's type must be populated before creation");
1345             }
1346             if (getEntityNameBo(name.getEntityId(), name.getNameType().getCode()) != null) {
1347                 throw new RiceIllegalStateException("the EntityName to create already exists: " + name);
1348             }
1349         }
1350         EntityNameBo bo = EntityNameBo.from(name);
1351         return EntityNameBo.to(businessObjectService.save(bo));
1352     }
1353 
1354     @Override
1355     public EntityName updateName(EntityName name) throws RiceIllegalArgumentException, RiceIllegalStateException {
1356         incomingParamCheck(name, "name");
1357 
1358         if (StringUtils.isEmpty(name.getEntityId()) || StringUtils.isBlank(name.getEntityId())) {
1359             throw new RiceIllegalStateException("Name's entityId must be populated before update");
1360         }  else {
1361             if (name.getNameType() == null) {
1362                 throw new RiceIllegalStateException("EntityName's type must be populated before update");
1363             }
1364             if (StringUtils.isEmpty(name.getId()) || getEntityNameBo(name.getId()) == null) {
1365                 throw new RiceIllegalStateException("the EntityName to update does not exist: " + name);
1366             }
1367         }
1368         EntityNameBo bo = EntityNameBo.from(name);
1369         return EntityNameBo.to(businessObjectService.save(bo));
1370     }
1371 
1372     @Override
1373     public EntityName inactivateName(String id) throws RiceIllegalArgumentException, RiceIllegalStateException {
1374         incomingParamCheck(id, "id");
1375 
1376         EntityNameBo bo = getEntityNameBo(id);
1377         if (bo == null) {
1378             throw new RiceIllegalStateException("the EntityName to inactivate does not exist");
1379         }
1380 
1381         bo.setActive(false);
1382         return EntityNameBo.to(businessObjectService.save(bo));
1383     }
1384 
1385     private EntityEmploymentBo getEntityEmploymentBo(String entityId, String employmentTypeCode,
1386                         String employmentStatusCode, String employmentAffiliationId) {
1387         if (StringUtils.isEmpty(entityId) || StringUtils.isEmpty(employmentTypeCode)
1388                 || StringUtils.isEmpty(employmentStatusCode) || StringUtils.isEmpty(employmentAffiliationId)) {
1389             return null;
1390         }
1391         Map<String,Object> criteria = new HashMap<String,Object>();
1392         criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, entityId);
1393         criteria.put("employeeTypeCode", employmentTypeCode);
1394         criteria.put("employeeStatusCode", employmentStatusCode);
1395         criteria.put("entityAffiliationId", employmentAffiliationId);
1396         criteria.put(KIMPropertyConstants.Entity.ACTIVE, "Y");
1397         return businessObjectService.findByPrimaryKey(EntityEmploymentBo.class, criteria);
1398     }
1399 
1400     private EntityEmploymentBo getEntityEmploymentBo(String id) {
1401         if (StringUtils.isEmpty(id)) {
1402             return null;
1403         }
1404         Map<String,Object> criteria = new HashMap<String,Object>();
1405         criteria.put(KIMPropertyConstants.Entity.ID, id);
1406         criteria.put(KIMPropertyConstants.Entity.ACTIVE, "Y");
1407         return businessObjectService.findByPrimaryKey(EntityEmploymentBo.class, criteria);
1408     }
1409     @Override
1410     public EntityEmployment addEmploymentToEntity(EntityEmployment employment) throws RiceIllegalArgumentException, RiceIllegalStateException {
1411         incomingParamCheck(employment, "employment");
1412 
1413         if (StringUtils.isEmpty(employment.getEntityId()) || StringUtils.isBlank(employment.getEntityId())) {
1414             throw new RiceIllegalStateException("EntityEmployment's entityId must be populated before creation");
1415         }  else {
1416             if (employment.getEmployeeType() == null
1417                     || employment.getEmployeeStatus() == null
1418                     || employment.getEntityAffiliation() == null) {
1419                 throw new RiceIllegalStateException("EntityEmployment's status, type, and entity affiliation must be populated before creation");
1420             }
1421             if (getEntityEmploymentBo(employment.getEntityId(), employment.getEmployeeType().getCode(), employment.getEmployeeStatus().getCode(), employment.getEntityAffiliation().getId()) != null) {
1422                 throw new RiceIllegalStateException("the EntityEmployment to create already exists: " + employment);
1423             }
1424         }
1425         EntityEmploymentBo bo = EntityEmploymentBo.from(employment);
1426         return EntityEmploymentBo.to(businessObjectService.save(bo));
1427     }
1428 
1429     @Override
1430     public EntityEmployment updateEmployment(EntityEmployment employment) throws RiceIllegalArgumentException, RiceIllegalStateException {
1431         incomingParamCheck(employment, "employment");
1432 
1433         if (StringUtils.isEmpty(employment.getEntityId()) || StringUtils.isBlank(employment.getEntityId())) {
1434             throw new RiceIllegalStateException("EntityEmployment's entityId must be populated before update");
1435         }  else {
1436             if (employment.getEmployeeType() == null
1437                     || employment.getEmployeeStatus() == null
1438                     || employment.getEntityAffiliation() == null) {
1439                 throw new RiceIllegalStateException("EntityEmployment's status, type, and entity affiliation must be populated before update");
1440             }
1441             if (getEntityEmploymentBo(employment.getEntityId(), employment.getEmployeeType().getCode(), employment.getEmployeeStatus().getCode(), employment.getEntityAffiliation().getId()) == null) {
1442                 throw new RiceIllegalStateException("the EntityEmployment to udpate does not exist: " + employment);
1443             }
1444         }
1445         EntityEmploymentBo bo = EntityEmploymentBo.from(employment);
1446         return EntityEmploymentBo.to(businessObjectService.save(bo));
1447     }
1448 
1449     @Override
1450     public EntityEmployment inactivateEmployment(String id) throws RiceIllegalArgumentException, RiceIllegalStateException {
1451         incomingParamCheck(id, "id");
1452 
1453         EntityEmploymentBo bo = getEntityEmploymentBo(id);
1454         if (bo == null) {
1455             throw new RiceIllegalStateException("the EntityEmployment to inactivate does not exist");
1456         }
1457         bo.setActive(false);
1458         return EntityEmploymentBo.to(businessObjectService.save(bo));
1459     }
1460 
1461 	private EntityBioDemographicsBo getEntityBioDemographicsBo(String entityId) {
1462         if (StringUtils.isEmpty(entityId)) {
1463             return null;
1464         }
1465 		Map<String,String> criteria = new HashMap<String,String>(1);
1466         criteria.put(KIMPropertyConstants.Entity.ENTITY_ID, entityId);
1467 		return businessObjectService.findByPrimaryKey(EntityBioDemographicsBo.class, criteria);
1468 	}
1469 
1470     @Override
1471     public EntityBioDemographics addBioDemographicsToEntity(EntityBioDemographics bioDemographics) throws RiceIllegalArgumentException, RiceIllegalStateException {
1472         incomingParamCheck(bioDemographics, "bioDemographics");
1473 
1474         if (StringUtils.isEmpty(bioDemographics.getEntityId()) || StringUtils.isBlank(bioDemographics.getEntityId())) {
1475             throw new RiceIllegalStateException("BioDemographics' entityId must be populated before creation");
1476         }  else {
1477             if (getEntityBioDemographicsBo(bioDemographics.getEntityId()) != null) {
1478                 throw new RiceIllegalStateException("the EntityBioDemographics to create already exists: " + bioDemographics);
1479             }
1480         }
1481         EntityBioDemographicsBo bo = EntityBioDemographicsBo.from(bioDemographics);
1482         return EntityBioDemographicsBo.to(businessObjectService.save(bo));
1483     }
1484 
1485     @Override
1486     public EntityBioDemographics updateBioDemographics(EntityBioDemographics bioDemographics) throws RiceIllegalArgumentException, RiceIllegalStateException {
1487         incomingParamCheck(bioDemographics, "bioDemographics");
1488 
1489         if (getEntityBioDemographicsBo(bioDemographics.getEntityId()) == null) {
1490             throw new RiceIllegalStateException("the EntityBioDemographics to update does not exist: " + bioDemographics);
1491         }
1492         EntityBioDemographicsBo bo = EntityBioDemographicsBo.from(bioDemographics);
1493         return EntityBioDemographicsBo.to(businessObjectService.save(bo));
1494     }
1495 
1496 
1497     public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) {
1498         this.criteriaLookupService = criteriaLookupService;
1499     }
1500 
1501     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
1502         this.businessObjectService = businessObjectService;
1503     }
1504 
1505     private void incomingParamCheck(Object object, String name) {
1506         if (object == null) {
1507             throw new RiceIllegalArgumentException(name + " was null");
1508         } else if (object instanceof String
1509                 && StringUtils.isBlank((String) object)) {
1510             throw new RiceIllegalArgumentException(name + " was blank");
1511         }
1512     }
1513 }