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