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