Coverage Report - org.kuali.rice.kim.service.impl.UiDocumentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
UiDocumentServiceImpl
0%
0/1645
0%
0/1096
5.896
UiDocumentServiceImpl$GroupMemberNameComparator
0%
0/2
N/A
5.896
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.apache.log4j.Logger;
 21  
 import org.joda.time.DateTime;
 22  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 23  
 import org.kuali.rice.core.api.criteria.Predicate;
 24  
 import org.kuali.rice.core.api.criteria.QueryByCriteria;
 25  
 import org.kuali.rice.core.api.membership.MemberType;
 26  
 import org.kuali.rice.core.api.parameter.Parameter;
 27  
 import org.kuali.rice.core.api.uif.RemotableCheckboxGroup;
 28  
 import org.kuali.rice.core.framework.parameter.ParameterService;
 29  
 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
 30  
 import org.kuali.rice.kim.api.KimConstants;
 31  
 import org.kuali.rice.kim.api.KimConstants.KimGroupMemberTypes;
 32  
 import org.kuali.rice.kim.api.group.Group;
 33  
 import org.kuali.rice.kim.api.group.GroupMember;
 34  
 import org.kuali.rice.kim.api.group.GroupService;
 35  
 import org.kuali.rice.kim.api.identity.IdentityService;
 36  
 import org.kuali.rice.kim.api.identity.Person;
 37  
 import org.kuali.rice.kim.api.identity.address.EntityAddress;
 38  
 import org.kuali.rice.kim.api.identity.address.EntityAddressContract;
 39  
 import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
 40  
 import org.kuali.rice.kim.api.identity.email.EntityEmail;
 41  
 import org.kuali.rice.kim.api.identity.email.EntityEmailContract;
 42  
 import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
 43  
 import org.kuali.rice.kim.api.identity.entity.Entity;
 44  
 import org.kuali.rice.kim.api.identity.name.EntityName;
 45  
 import org.kuali.rice.kim.api.identity.phone.EntityPhone;
 46  
 import org.kuali.rice.kim.api.identity.phone.EntityPhoneContract;
 47  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 48  
 import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
 49  
 import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
 50  
 import org.kuali.rice.kim.api.permission.PermissionService;
 51  
 import org.kuali.rice.kim.api.responsibility.ResponsibilityService;
 52  
 import org.kuali.rice.kim.api.role.Role;
 53  
 import org.kuali.rice.kim.api.role.RoleMember;
 54  
 import org.kuali.rice.kim.api.role.RoleService;
 55  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 56  
 import org.kuali.rice.kim.api.type.KimAttributeField;
 57  
 import org.kuali.rice.kim.api.type.KimType;
 58  
 import org.kuali.rice.kim.api.type.KimTypeAttribute;
 59  
 import org.kuali.rice.kim.api.type.KimTypeInfoService;
 60  
 import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
 61  
 import org.kuali.rice.kim.bo.ui.GroupDocumentQualifier;
 62  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
 63  
 import org.kuali.rice.kim.bo.ui.KimDocumentRolePermission;
 64  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
 65  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibility;
 66  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibilityAction;
 67  
 import org.kuali.rice.kim.bo.ui.PersonDocumentAddress;
 68  
 import org.kuali.rice.kim.bo.ui.PersonDocumentAffiliation;
 69  
 import org.kuali.rice.kim.bo.ui.PersonDocumentEmail;
 70  
 import org.kuali.rice.kim.bo.ui.PersonDocumentEmploymentInfo;
 71  
 import org.kuali.rice.kim.bo.ui.PersonDocumentGroup;
 72  
 import org.kuali.rice.kim.bo.ui.PersonDocumentName;
 73  
 import org.kuali.rice.kim.bo.ui.PersonDocumentPhone;
 74  
 import org.kuali.rice.kim.bo.ui.PersonDocumentPrivacy;
 75  
 import org.kuali.rice.kim.bo.ui.PersonDocumentRole;
 76  
 import org.kuali.rice.kim.bo.ui.RoleDocumentDelegation;
 77  
 import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember;
 78  
 import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMemberQualifier;
 79  
 import org.kuali.rice.kim.document.IdentityManagementGroupDocument;
 80  
 import org.kuali.rice.kim.document.IdentityManagementPersonDocument;
 81  
 import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
 82  
 import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
 83  
 import org.kuali.rice.kim.framework.type.KimTypeService;
 84  
 import org.kuali.rice.kim.impl.KIMPropertyConstants;
 85  
 import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
 86  
 import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
 87  
 import org.kuali.rice.kim.impl.common.delegate.DelegateMemberAttributeDataBo;
 88  
 import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
 89  
 import org.kuali.rice.kim.impl.group.GroupAttributeBo;
 90  
 import org.kuali.rice.kim.impl.group.GroupBo;
 91  
 import org.kuali.rice.kim.impl.group.GroupMemberBo;
 92  
 import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
 93  
 import org.kuali.rice.kim.impl.identity.address.EntityAddressTypeBo;
 94  
 import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
 95  
 import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationTypeBo;
 96  
 import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
 97  
 import org.kuali.rice.kim.impl.identity.email.EntityEmailTypeBo;
 98  
 import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
 99  
 import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentStatusBo;
 100  
 import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentTypeBo;
 101  
 import org.kuali.rice.kim.impl.identity.entity.EntityBo;
 102  
 import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
 103  
 import org.kuali.rice.kim.impl.identity.name.EntityNameTypeBo;
 104  
 import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
 105  
 import org.kuali.rice.kim.impl.identity.phone.EntityPhoneTypeBo;
 106  
 import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
 107  
 import org.kuali.rice.kim.impl.identity.privacy.EntityPrivacyPreferencesBo;
 108  
 import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
 109  
 import org.kuali.rice.kim.impl.permission.PermissionBo;
 110  
 import org.kuali.rice.kim.impl.responsibility.ResponsibilityInternalService;
 111  
 import org.kuali.rice.kim.impl.role.RoleBo;
 112  
 import org.kuali.rice.kim.impl.role.RoleMemberAttributeDataBo;
 113  
 import org.kuali.rice.kim.impl.role.RoleMemberBo;
 114  
 import org.kuali.rice.kim.impl.role.RolePermissionBo;
 115  
 import org.kuali.rice.kim.impl.role.RoleResponsibilityActionBo;
 116  
 import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
 117  
 import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
 118  
 import org.kuali.rice.kim.impl.type.KimTypeBo;
 119  
 import org.kuali.rice.kim.service.UiDocumentService;
 120  
 import org.kuali.rice.kim.util.KimCommonUtilsInternal;
 121  
 import org.kuali.rice.kns.datadictionary.exporter.AttributesMapBuilder;
 122  
 import org.kuali.rice.kns.kim.type.DataDictionaryTypeServiceHelper;
 123  
 import org.kuali.rice.krad.bo.BusinessObject;
 124  
 import org.kuali.rice.krad.bo.PersistableBusinessObject;
 125  
 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
 126  
 import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
 127  
 import org.kuali.rice.krad.document.Document;
 128  
 import org.kuali.rice.krad.service.BusinessObjectService;
 129  
 import org.kuali.rice.krad.service.DocumentHelperService;
 130  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 131  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 132  
 import org.kuali.rice.krad.util.KRADConstants;
 133  
 import org.kuali.rice.krad.util.ObjectUtils;
 134  
 
 135  
 import java.sql.Timestamp;
 136  
 import java.util.ArrayList;
 137  
 import java.util.Collection;
 138  
 import java.util.Collections;
 139  
 import java.util.Comparator;
 140  
 import java.util.HashMap;
 141  
 import java.util.HashSet;
 142  
 import java.util.List;
 143  
 import java.util.Map;
 144  
 import java.util.Set;
 145  
 
 146  
 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
 147  
 
 148  
 /**
 149  
  * This is a description of what this class does - shyu don't forget to fill this in.
 150  
  *
 151  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 152  
  *
 153  
  */
 154  0
 public class UiDocumentServiceImpl implements UiDocumentService {
 155  0
         private static final Logger LOG = Logger.getLogger(UiDocumentServiceImpl.class);
 156  
         private static final String SHOW_BLANK_QUALIFIERS = "kim.show.blank.qualifiers";
 157  
         
 158  
         private RoleService roleService;
 159  
         private BusinessObjectService businessObjectService;
 160  
         private IdentityService identityService;
 161  
     private PermissionService permissionService;
 162  
         private GroupService groupService;
 163  
         private ResponsibilityService responsibilityService;
 164  
     private ResponsibilityInternalService responsibilityInternalService;
 165  
         private KimTypeInfoService kimTypeInfoService;
 166  
     private DocumentHelperService documentHelperService;
 167  
     private ParameterService parameterService;
 168  
 
 169  
 
 170  
         /**
 171  
          * @see org.kuali.rice.kim.service.UiDocumentService#saveEntityPerson(IdentityManagementPersonDocument)
 172  
          */
 173  
         public void saveEntityPerson(
 174  
             IdentityManagementPersonDocument identityManagementPersonDocument) {
 175  0
                 EntityBo kimEntity = new EntityBo();
 176  0
                 EntityBo origEntity = getEntityBo(identityManagementPersonDocument.getEntityId());
 177  0
                 boolean creatingNew = true;
 178  0
                 if (origEntity == null) {
 179  0
                         origEntity = new EntityBo();
 180  0
                         kimEntity.setActive(true);
 181  
                 } else {
 182  
                         // TODO : in order to resolve optimistic locking issue. has to get identity and set the version number if identity records matched
 183  
                         // Need to look into this.
 184  
                         //kimEntity = origEntity;
 185  0
                         kimEntity.setActive(origEntity.isActive());
 186  0
                         kimEntity.setVersionNumber(origEntity.getVersionNumber());
 187  0
                         creatingNew = false;
 188  
                 }
 189  
 
 190  0
                 kimEntity.setId(identityManagementPersonDocument.getEntityId());
 191  0
                 String initiatorPrincipalId = getInitiatorPrincipalId(identityManagementPersonDocument);
 192  0
                 boolean inactivatingPrincipal = false;
 193  0
                 if(canModifyEntity(initiatorPrincipalId, identityManagementPersonDocument.getPrincipalId())){
 194  0
                         inactivatingPrincipal = setupPrincipal(identityManagementPersonDocument, kimEntity, origEntity.getPrincipals());
 195  0
                         setupAffiliation(identityManagementPersonDocument, kimEntity, origEntity.getAffiliations(), origEntity.getEmploymentInformation());
 196  0
                         setupName(identityManagementPersonDocument, kimEntity, origEntity.getNames());
 197  
                 // entitytype
 198  0
                         List<EntityTypeContactInfoBo> entityTypes = new ArrayList<EntityTypeContactInfoBo>();
 199  0
                         EntityTypeContactInfoBo entityType = new EntityTypeContactInfoBo();
 200  0
                         entityType.setEntityId(identityManagementPersonDocument.getEntityId());
 201  0
                         entityType.setEntityTypeCode(KimConstants.EntityTypes.PERSON);
 202  0
                         entityType.setActive(true);
 203  0
                         entityTypes.add(entityType);
 204  0
                         EntityTypeContactInfoBo origEntityType = new EntityTypeContactInfoBo();
 205  0
                         for (EntityTypeContactInfoBo type : origEntity.getEntityTypeContactInfos()) {
 206  
                                 // should check identity.entitytypeid, but it's not persist in persondoc yet
 207  0
                                 if (type.getEntityTypeCode()!=null && StringUtils.equals(type.getEntityTypeCode(), entityType.getEntityTypeCode())) {
 208  0
                                         origEntityType = type;
 209  0
                                         entityType.setVersionNumber(type.getVersionNumber());
 210  0
                                         entityType.setActive(type.isActive());
 211  
                                 }
 212  
                         }
 213  0
                         setupPhone(identityManagementPersonDocument, entityType, origEntityType.getPhoneNumbers());
 214  0
                         setupEmail(identityManagementPersonDocument, entityType, origEntityType.getEmailAddresses());
 215  0
                         setupAddress(identityManagementPersonDocument, entityType, origEntityType.getAddresses());
 216  0
             kimEntity.setEntityTypeContactInfos(entityTypes);
 217  0
                 } else{
 218  0
                         if(ObjectUtils.isNotNull(origEntity.getExternalIdentifiers())) {
 219  0
                 kimEntity.setExternalIdentifiers(origEntity.getExternalIdentifiers());
 220  
             }
 221  0
                         if(ObjectUtils.isNotNull(origEntity.getEmploymentInformation())) {
 222  0
                 kimEntity.setEmploymentInformation(origEntity.getEmploymentInformation());
 223  
             }
 224  0
                         if(ObjectUtils.isNotNull(origEntity.getAffiliations())) {
 225  0
                 kimEntity.setAffiliations(origEntity.getAffiliations());
 226  
             }
 227  0
                         if(ObjectUtils.isNotNull(origEntity.getNames())) {
 228  0
                 kimEntity.setNames(origEntity.getNames());
 229  
             }
 230  0
                         if(ObjectUtils.isNotNull(origEntity.getEntityTypeContactInfos())) {
 231  0
                 kimEntity.setEntityTypeContactInfos(origEntity.getEntityTypeContactInfos());
 232  
             }
 233  
                 }
 234  0
                 if(creatingNew || canOverrideEntityPrivacyPreferences(getInitiatorPrincipalId(identityManagementPersonDocument), identityManagementPersonDocument.getPrincipalId())) {
 235  0
                         setupPrivacy(identityManagementPersonDocument, kimEntity, origEntity.getPrivacyPreferences());
 236  
                 } else {
 237  0
                         if(ObjectUtils.isNotNull(origEntity.getPrivacyPreferences())) {
 238  0
                                 kimEntity.setPrivacyPreferences(origEntity.getPrivacyPreferences());
 239  
                         }
 240  
                 }
 241  0
                 List <GroupMemberBo>  groupPrincipals = populateGroupMembers(identityManagementPersonDocument);
 242  0
                 List <RoleMemberBo>  rolePrincipals = populateRoleMembers(identityManagementPersonDocument);
 243  0
                 List <DelegateTypeBo> personDelegations = populateDelegations(identityManagementPersonDocument);
 244  0
                 List <PersistableBusinessObject> bos = new ArrayList<PersistableBusinessObject>();
 245  0
                 List <RoleResponsibilityActionBo> roleRspActions = populateRoleRspActions(identityManagementPersonDocument);
 246  0
                 List <RoleMemberAttributeDataBo> blankRoleMemberAttrs = getBlankRoleMemberAttrs(rolePrincipals);
 247  0
                 bos.add(kimEntity);
 248  
                 //if(ObjectUtils.isNotNull(kimEntity.getPrivacyPreferences()))
 249  
                 //        bos.add(kimEntity.getPrivacyPreferences());
 250  0
                 bos.addAll(groupPrincipals);
 251  0
                 bos.addAll(rolePrincipals);
 252  0
                 bos.addAll(roleRspActions);
 253  0
                 bos.addAll(personDelegations);
 254  
                 // boservice.save(bos) does not handle deleteawarelist
 255  0
                 getBusinessObjectService().save(bos);
 256  
 
 257  0
                 if (!blankRoleMemberAttrs.isEmpty()) {
 258  0
                         getBusinessObjectService().delete(blankRoleMemberAttrs);
 259  
                 }
 260  0
                 if ( inactivatingPrincipal ) {
 261  
                         //when a person is inactivated, inactivate their group, role, and delegation memberships
 262  0
                         KimImplServiceLocator.getRoleInternalService().principalInactivated(
 263  
                     identityManagementPersonDocument.getPrincipalId());
 264  
                 }
 265  0
         }
 266  
 
 267  
         private String getInitiatorPrincipalId(Document document){
 268  
                 try{
 269  0
                         return document.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId();
 270  0
                 } catch(Exception ex){
 271  0
                         return null;
 272  
                 }
 273  
         }
 274  
 
 275  
         public Map<String,Object> getAttributeEntries( List<KimAttributeField> definitions ) {
 276  0
                 final Map<String,Object> attributeEntries = new HashMap<String,Object>();
 277  0
                 if (definitions != null) {
 278  0
                 for (AttributeDefinition definition : DataDictionaryTypeServiceHelper.toKimAttributeDefinitions(definitions)) {
 279  0
                                 final AttributesMapBuilder builder = new AttributesMapBuilder();
 280  0
                 final ExportMap map = builder.buildAttributeMap(definition, "");
 281  0
                 attributeEntries.put(definition.getName(),map.getExportData());
 282  0
                         }
 283  
                 }
 284  0
         return attributeEntries;
 285  
         }
 286  
 
 287  
 
 288  
         /**
 289  
          *
 290  
          * @see org.kuali.rice.kim.service.UiDocumentService#loadEntityToPersonDoc(IdentityManagementPersonDocument, String)
 291  
          */
 292  
         public void loadEntityToPersonDoc(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId) {
 293  0
                 Principal principal = this.getIdentityService().getPrincipal(principalId);
 294  0
         if(principal==null) {
 295  0
                 throw new RuntimeException("Principal does not exist for principal id:"+principalId);
 296  
         }
 297  
 
 298  0
         identityManagementPersonDocument.setPrincipalId(principal.getPrincipalId());
 299  0
         identityManagementPersonDocument.setPrincipalName(principal.getPrincipalName());
 300  0
         identityManagementPersonDocument.setPassword(principal.getPassword());
 301  0
         identityManagementPersonDocument.setActive(principal.isActive());
 302  0
         Entity kimEntity = this.getIdentityService().getEntity(principal.getEntityId());
 303  0
                 identityManagementPersonDocument.setEntityId(kimEntity.getId());
 304  0
                 if ( ObjectUtils.isNotNull( kimEntity.getPrivacyPreferences() ) ) {
 305  0
                         identityManagementPersonDocument.setPrivacy(loadPrivacyReferences(kimEntity.getPrivacyPreferences()));
 306  
                 }
 307  
                 //identityManagementPersonDocument.setActive(kimEntity.isActive());
 308  0
                 identityManagementPersonDocument.setAffiliations(loadAffiliations(kimEntity.getAffiliations(),kimEntity.getEmploymentInformation()));
 309  0
                 identityManagementPersonDocument.setNames(loadNames( identityManagementPersonDocument, principalId, kimEntity.getNames(), identityManagementPersonDocument.getPrivacy().isSuppressName() ));
 310  0
                 EntityTypeContactInfo entityType = null;
 311  0
                 for (EntityTypeContactInfo type : kimEntity.getEntityTypeContactInfos()) {
 312  0
                         if (KimConstants.EntityTypes.PERSON.equals(type.getEntityTypeCode())) {
 313  0
                                 entityType = EntityTypeContactInfo.Builder.create(type).build();
 314  
                         }
 315  
                 }
 316  
 
 317  0
                 if(entityType!=null){
 318  0
                         identityManagementPersonDocument.setEmails(loadEmails(identityManagementPersonDocument, principalId, entityType.getEmailAddresses(), identityManagementPersonDocument.getPrivacy().isSuppressEmail()));
 319  0
                         identityManagementPersonDocument.setPhones(loadPhones(identityManagementPersonDocument, principalId, entityType.getPhoneNumbers(), identityManagementPersonDocument.getPrivacy().isSuppressPhone()));
 320  0
                         identityManagementPersonDocument.setAddrs(loadAddresses(identityManagementPersonDocument, principalId, entityType.getAddresses(), identityManagementPersonDocument.getPrivacy().isSuppressAddress()));
 321  
                 }
 322  
 
 323  0
                 List<Group> groups = getGroupService().getGroups(getGroupService().getDirectGroupIdsByPrincipalId(
 324  
                 identityManagementPersonDocument.getPrincipalId()));
 325  0
                 loadGroupToPersonDoc(identityManagementPersonDocument, groups);
 326  0
                 loadRoleToPersonDoc(identityManagementPersonDocument);
 327  0
                 loadDelegationsToPersonDoc(identityManagementPersonDocument);
 328  0
         }
 329  
 
 330  
     @SuppressWarnings("unchecked")
 331  
         public List<DelegateTypeBo> getPersonDelegations(String principalId){
 332  0
                 if(principalId==null) {
 333  0
                         return new ArrayList<DelegateTypeBo>();
 334  
         }
 335  0
                 Map<String,String> criteria = new HashMap<String,String>(1);
 336  0
                 criteria.put(KimConstants.PrimaryKeyConstants.MEMBER_ID, principalId);
 337  0
                 criteria.put( KIMPropertyConstants.DelegationMember.MEMBER_TYPE_CODE, MemberType.PRINCIPAL.getCode() );
 338  0
                 List<DelegateMemberBo> delegationMembers = (List<DelegateMemberBo>)getBusinessObjectService().findMatching(DelegateMemberBo.class, criteria);
 339  0
                 List<DelegateTypeBo> delegations = new ArrayList<DelegateTypeBo>();
 340  0
                 List<String> delegationIds = new ArrayList<String>();
 341  0
                 if(ObjectUtils.isNotNull(delegationMembers)){
 342  0
                         for(DelegateMemberBo delegationMember: delegationMembers){
 343  0
                                 if(!delegationIds.contains(delegationMember.getDelegationId())){
 344  0
                                         delegationIds.add(delegationMember.getDelegationId());
 345  0
                                         criteria = new HashMap<String,String>(1);
 346  0
                                         criteria.put(KimConstants.PrimaryKeyConstants.DELEGATION_ID, delegationMember.getDelegationId());
 347  0
                                         delegations.add(getBusinessObjectService().findByPrimaryKey(DelegateTypeBo.class, criteria));
 348  
                                 }
 349  
                         }
 350  
                 }
 351  0
                 return delegations;
 352  
         }
 353  
 
 354  
 
 355  
     protected void loadDelegationsToPersonDoc(IdentityManagementPersonDocument identityManagementPersonDocument){
 356  0
                 List<RoleDocumentDelegation> delList = new ArrayList<RoleDocumentDelegation>();
 357  
                 RoleDocumentDelegation documentDelegation;
 358  0
                 List<DelegateTypeBo> origDelegations = getPersonDelegations(identityManagementPersonDocument.getPrincipalId());
 359  0
                 if(ObjectUtils.isNotNull(origDelegations)){
 360  0
                         for(DelegateTypeBo del: origDelegations){
 361  0
                                 if(del.isActive()){
 362  0
                                         documentDelegation = new RoleDocumentDelegation();
 363  0
                                         documentDelegation.setActive(del.isActive());
 364  0
                                         documentDelegation.setDelegationId(del.getDelegationId());
 365  0
                                         documentDelegation.setDelegationTypeCode(del.getDelegationTypeCode());
 366  0
                                         documentDelegation.setKimTypeId(del.getKimTypeId());
 367  0
                                         documentDelegation.setMembers(
 368  
                                                         loadDelegationMembers(identityManagementPersonDocument,
 369  
                                                                         del.getMembers(), (RoleBo)getMember(MemberType.ROLE, del.getRoleId())));
 370  0
                                         documentDelegation.setRoleId(del.getRoleId());
 371  0
                                         documentDelegation.setEdit(true);
 372  0
                                         delList.add(documentDelegation);
 373  
                                 }
 374  
                         }
 375  
                 }
 376  0
                 identityManagementPersonDocument.setDelegations(delList);
 377  0
                 setDelegationMembersInDocument(identityManagementPersonDocument);
 378  0
         }
 379  
 
 380  
         public void setDelegationMembersInDocument(IdentityManagementPersonDocument identityManagementPersonDocument){
 381  0
                 if(CollectionUtils.isNotEmpty(identityManagementPersonDocument.getDelegations())){
 382  0
                         for(RoleDocumentDelegation delegation: identityManagementPersonDocument.getDelegations()){
 383  0
                                 if(CollectionUtils.isNotEmpty(delegation.getMembers())){
 384  0
                                         for(RoleDocumentDelegationMember member: delegation.getMembers()){
 385  0
                                                 if (StringUtils.equals(member.getMemberId(), identityManagementPersonDocument.getPrincipalId()))
 386  
                                                 {
 387  0
                                                         member.setDelegationTypeCode(delegation.getDelegationTypeCode());
 388  0
                                                         identityManagementPersonDocument.getDelegationMembers().add(member);
 389  
                                                 }
 390  
                                         }
 391  
                                 }
 392  
                         }
 393  
                 }
 394  0
         }
 395  
 
 396  
     protected List<RoleDocumentDelegationMember> loadDelegationMembers(
 397  
                     IdentityManagementPersonDocument identityManagementPersonDocument, List<DelegateMemberBo> members, RoleBo roleImpl){
 398  0
                 List<RoleDocumentDelegationMember> pndMembers = new ArrayList<RoleDocumentDelegationMember>();
 399  
                 RoleDocumentDelegationMember pndMember;
 400  
                 RoleMemberBo roleMember;
 401  0
                 if(ObjectUtils.isNotNull(members)){
 402  0
                         for(DelegateMemberBo member: members){
 403  0
                                 pndMember = new RoleDocumentDelegationMember();
 404  0
                                 pndMember.setActiveFromDate(member.getActiveFromDateValue());
 405  0
                                 pndMember.setActiveToDate(member.getActiveToDateValue());
 406  0
                                 pndMember.setActive(member.isActive(new Timestamp(System.currentTimeMillis())));
 407  0
                                 pndMember.setRoleBo(roleImpl);
 408  0
                                 if(pndMember.isActive()){
 409  0
                                         KimCommonUtilsInternal.copyProperties(pndMember, member);
 410  0
                                         pndMember.setRoleMemberId(member.getRoleMemberId());
 411  0
                                         roleMember = getRoleMemberForRoleMemberId(member.getRoleMemberId());
 412  0
                                         if(roleMember!=null){
 413  0
                                                 pndMember.setRoleMemberName(getMemberName(roleMember.getMemberType(), roleMember.getMemberId()));
 414  0
                                                 pndMember.setRoleMemberNamespaceCode(getMemberNamespaceCode(roleMember.getMemberType(), roleMember.getMemberId()));
 415  
                                         }
 416  0
                                         pndMember.setMemberNamespaceCode(getMemberNamespaceCode(member.getType(), member.getMemberId()));
 417  0
                                         pndMember.setMemberName(getMemberName(member.getType(), member.getMemberId()));
 418  0
                                         pndMember.setEdit(true);
 419  0
                                         pndMember.setQualifiers(loadDelegationMemberQualifiers(identityManagementPersonDocument, pndMember.getAttributesHelper().getDefinitions(), member.getAttributes()));
 420  0
                                         pndMembers.add(pndMember);
 421  
                                 }
 422  
                         }
 423  
                 }
 424  0
                 return pndMembers;
 425  
         }
 426  
 
 427  
     protected List<RoleDocumentDelegationMemberQualifier> loadDelegationMemberQualifiers(IdentityManagementPersonDocument identityManagementPersonDocument,
 428  
                     List<KimAttributeField> origAttributeDefinitions, List<DelegateMemberAttributeDataBo> attributeDataList){
 429  0
                 List<RoleDocumentDelegationMemberQualifier> pndMemberRoleQualifiers = new ArrayList<RoleDocumentDelegationMemberQualifier>();
 430  
                 RoleDocumentDelegationMemberQualifier pndMemberRoleQualifier;
 431  0
                 boolean attributePresent = false;
 432  
                 String origAttributeId;
 433  0
                 if(origAttributeDefinitions!=null){
 434  0
                         for(KimAttributeField key: origAttributeDefinitions) {
 435  0
                                 origAttributeId = identityManagementPersonDocument.getKimAttributeDefnId(key);
 436  0
                                 if(ObjectUtils.isNotNull(attributeDataList)){
 437  0
                                         for(DelegateMemberAttributeDataBo memberRoleQualifier: attributeDataList){
 438  0
                                                 if(StringUtils.equals(origAttributeId, memberRoleQualifier.getKimAttribute().getId())){
 439  0
                                                         pndMemberRoleQualifier = new RoleDocumentDelegationMemberQualifier();
 440  0
                                                         pndMemberRoleQualifier.setAttrDataId(memberRoleQualifier.getId());
 441  0
                                                         pndMemberRoleQualifier.setAttrVal(memberRoleQualifier.getAttributeValue());
 442  0
                                                         pndMemberRoleQualifier.setDelegationMemberId(memberRoleQualifier.getAssignedToId());
 443  0
                                                         pndMemberRoleQualifier.setKimTypId(memberRoleQualifier.getKimTypeId());
 444  0
                                                         pndMemberRoleQualifier.setKimAttrDefnId(memberRoleQualifier.getKimAttributeId());
 445  0
                                                         pndMemberRoleQualifier.setKimAttribute(memberRoleQualifier.getKimAttribute());
 446  0
                                                         pndMemberRoleQualifiers.add(pndMemberRoleQualifier);
 447  0
                                                         attributePresent = true;
 448  
                                                 }
 449  
                                         }
 450  
                                 }
 451  0
                                 if(!attributePresent){
 452  0
                                         pndMemberRoleQualifier = new RoleDocumentDelegationMemberQualifier();
 453  0
                                         pndMemberRoleQualifier.setKimAttrDefnId(origAttributeId);
 454  0
                                         pndMemberRoleQualifiers.add(pndMemberRoleQualifier);
 455  
                                 }
 456  0
                                 attributePresent = false;
 457  
                         }
 458  
                 }
 459  0
                 return pndMemberRoleQualifiers;
 460  
         }
 461  
 
 462  
         /**
 463  
          *
 464  
          * This method load related group data to pending document when usert initiate the 'edit'.
 465  
          *
 466  
          * @param identityManagementPersonDocument
 467  
          * @param groups
 468  
          */
 469  
     protected void loadGroupToPersonDoc(IdentityManagementPersonDocument identityManagementPersonDocument, List<? extends Group> groups) {
 470  0
         List <PersonDocumentGroup> docGroups = new ArrayList <PersonDocumentGroup>();
 471  0
         if(ObjectUtils.isNotNull(groups)){
 472  
             List<String> directMemberPrincipalIds;
 473  
             Collection<GroupMember> groupMemberships;
 474  0
             for (Group group: groups) {
 475  0
                 directMemberPrincipalIds = getGroupService().getDirectMemberPrincipalIds(group.getId());
 476  0
                 if(ObjectUtils.isNotNull(directMemberPrincipalIds)){
 477  0
                     directMemberPrincipalIds = new ArrayList<String>(new HashSet<String>(directMemberPrincipalIds));
 478  0
                     for (String memberId: directMemberPrincipalIds) {
 479  
                         // other more direct methods for this ?
 480  
                         // can't cast group to 'GroupImpl' because list is GroupInfo type
 481  0
                         if (StringUtils.equals(memberId, identityManagementPersonDocument.getPrincipalId())) {
 482  0
                             List<String> groupIds = new ArrayList<String>();
 483  0
                             groupIds.add(group.getId());
 484  0
                             groupMemberships = getGroupService().getMembers(groupIds);
 485  0
                             if(ObjectUtils.isNotNull(groupMemberships)){
 486  0
                                 for (GroupMember groupMember: groupMemberships) {
 487  0
                                     if (groupMember.isActive(new DateTime(System.currentTimeMillis())) && StringUtils.equals(groupMember.getMemberId(), identityManagementPersonDocument.getPrincipalId()) &&
 488  
                                         KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.equals(groupMember.getType())) {
 489  
                                         // create one PersonDocumentGroup per GroupMembershipInfo **
 490  0
                                         PersonDocumentGroup docGroup = new PersonDocumentGroup();
 491  0
                                         docGroup.setGroupId(group.getId());
 492  0
                                         docGroup.setGroupName(group.getName());
 493  0
                                         docGroup.setNamespaceCode(group.getNamespaceCode());
 494  0
                                         docGroup.setPrincipalId(memberId);
 495  0
                                         docGroup.setGroupMemberId(groupMember.getId());
 496  0
                                         if (groupMember.getActiveFromDate() != null) {
 497  0
                                                 docGroup.setActiveFromDate(groupMember.getActiveFromDate() == null ? null : new Timestamp(groupMember.getActiveFromDate().getMillis()));
 498  
                                         }
 499  0
                                         if (groupMember.getActiveToDate() != null) {
 500  0
                                                 docGroup.setActiveToDate(groupMember.getActiveToDate() == null ? null : new Timestamp(groupMember.getActiveToDate().getMillis()));
 501  
                                         }
 502  0
                                         docGroup.setEdit(true);
 503  0
                                         docGroups.add(docGroup);
 504  0
                                     }
 505  
                                 }
 506  
                             }
 507  0
                         }
 508  
                     }
 509  
                 }
 510  
             }
 511  
         }
 512  0
         identityManagementPersonDocument.setGroups(docGroups);
 513  0
     }
 514  
 
 515  
         protected void loadRoleToPersonDoc(IdentityManagementPersonDocument identityManagementPersonDocument) {
 516  0
                 List <PersonDocumentRole> docRoles = new ArrayList <PersonDocumentRole>();
 517  0
                 List<RoleBo> roles = getRolesForPrincipal(identityManagementPersonDocument.getPrincipalId());
 518  0
                 List<String> roleIds = new ArrayList<String>();
 519  0
                 if(ObjectUtils.isNotNull(roles)){
 520  0
                 for (RoleBo role : roles) {
 521  0
                         if (!roleIds.contains(role.getId())) {
 522  0
                                 PersonDocumentRole docRole = new PersonDocumentRole();
 523  0
                                 docRole.setKimTypeId(role.getKimTypeId());
 524  0
                                 docRole.setActive(role.isActive());
 525  0
                                 docRole.setNamespaceCode(role.getNamespaceCode());
 526  0
                                 docRole.setEdit(true);
 527  0
                                 docRole.setRoleId(role.getId());
 528  0
                                 docRole.setRoleName(role.getName());
 529  0
                                 docRole.setRolePrncpls(populateDocRolePrncpl(role.getNamespaceCode(), role.getMembers(), identityManagementPersonDocument.getPrincipalId(), getAttributeDefinitionsForRole(docRole)));
 530  0
                                 docRole.refreshReferenceObject("assignedResponsibilities");
 531  0
                                 if(docRole.getRolePrncpls()!=null && !docRole.getRolePrncpls().isEmpty()){
 532  0
                                         docRoles.add(docRole);
 533  0
                                         roleIds.add(role.getId());
 534  
                                 }
 535  0
                         }
 536  
                 }
 537  
                 }
 538  0
                 for (PersonDocumentRole role : docRoles) {
 539  0
                         role.setDefinitions(getAttributeDefinitionsForRole(role));
 540  
                 // when post again, it will need this during populate
 541  0
             role.setNewRolePrncpl(new KimDocumentRoleMember());
 542  0
             if(role.getDefinitions()!=null){
 543  0
                     for (KimAttributeField key : role.getDefinitions()) {
 544  0
                             KimDocumentRoleQualifier qualifier = new KimDocumentRoleQualifier();
 545  
                             //qualifier.setQualifierKey(key);
 546  0
                             setAttrDefnIdForQualifier(qualifier,key);
 547  0
                             role.getNewRolePrncpl().getQualifiers().add(qualifier);
 548  0
                     }
 549  
             }
 550  0
             loadRoleRstAction(role);
 551  0
             role.setAttributeEntry( getAttributeEntries( role.getDefinitions() ) );
 552  
                 }
 553  
         //
 554  
 
 555  0
         identityManagementPersonDocument.setRoles(docRoles);
 556  0
         }
 557  
 
 558  
         protected List<KimAttributeField> getAttributeDefinitionsForRole(PersonDocumentRole role) {
 559  0
             KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(KimTypeBo.to(
 560  
                 role.getKimRoleType()));
 561  
             //it is possible that the the kimTypeService is coming from a remote application
 562  
         // and therefore it can't be guarenteed that it is up and working, so using a try/catch to catch this possibility.
 563  
         try {
 564  0
                 if ( kimTypeService != null ) {
 565  0
                         return kimTypeService.getAttributeDefinitions(role.getKimTypeId());
 566  
                 }
 567  0
         } catch (Exception ex) {
 568  0
             LOG.warn("Not able to retrieve KimTypeService from remote system for KIM Role Type: " + role.getKimRoleType(), ex);
 569  0
         }
 570  0
             return Collections.emptyList();
 571  
         }
 572  
 
 573  
         protected void loadRoleRstAction(PersonDocumentRole role) {
 574  0
                 if(role!=null && CollectionUtils.isNotEmpty(role.getRolePrncpls())){
 575  0
                         for (KimDocumentRoleMember roleMbr : role.getRolePrncpls()) {
 576  0
                                 List<RoleResponsibilityActionBo> actions = getRoleRspActions( roleMbr.getRoleMemberId());
 577  0
                                 if(ObjectUtils.isNotNull(actions)){
 578  0
                                         for (RoleResponsibilityActionBo entRoleRspAction :actions) {
 579  0
                                                 KimDocumentRoleResponsibilityAction roleRspAction = new KimDocumentRoleResponsibilityAction();
 580  0
                                                 roleRspAction.setRoleResponsibilityId(entRoleRspAction.getRoleResponsibilityId());
 581  0
                                                 roleRspAction.setActionTypeCode(entRoleRspAction.getActionTypeCode());
 582  0
                                                 roleRspAction.setActionPolicyCode(entRoleRspAction.getActionPolicyCode());
 583  0
                                                 roleRspAction.setPriorityNumber(entRoleRspAction.getPriorityNumber());
 584  0
                                                 roleRspAction.setRoleResponsibilityActionId(entRoleRspAction.getId());
 585  0
                                                 roleRspAction.refreshReferenceObject("roleResponsibility");
 586  0
                                                 roleMbr.getRoleRspActions().add(roleRspAction);
 587  0
                                         }
 588  
                                 }
 589  0
                         }
 590  
                 }
 591  0
         }
 592  
 
 593  
         protected void setAttrDefnIdForQualifier(KimDocumentRoleQualifier qualifier, KimAttributeField definition) {
 594  0
             qualifier.setKimAttrDefnId(getAttributeDefnId(definition));
 595  0
             qualifier.refreshReferenceObject("kimAttribute");
 596  0
     }
 597  
 
 598  
         protected String getAttributeDefnId(KimAttributeField definition) {
 599  0
             return definition.getId();
 600  
     }
 601  
 
 602  
         private PrincipalBo getPrincipalImpl(String principalId) {
 603  0
                 Map<String,String> criteria = new HashMap<String,String>(1);
 604  0
         criteria.put(KIMPropertyConstants.Principal.PRINCIPAL_ID, principalId);
 605  0
                 return (PrincipalBo)getBusinessObjectService().findByPrimaryKey(PrincipalBo.class, criteria);
 606  
         }
 607  
 
 608  
         public List<EntityEmployment> getEntityEmploymentInformationInfo(String entityId) {
 609  0
         EntityBo entityImpl = getEntityBo(entityId);
 610  0
         List<EntityEmployment> empInfos = new ArrayList<EntityEmployment>();
 611  
         EntityEmployment empInfo;
 612  0
         if(ObjectUtils.isNotNull(entityImpl) && CollectionUtils.isNotEmpty(entityImpl.getEmploymentInformation())){
 613  0
                 for(EntityEmploymentBo empImpl: entityImpl.getEmploymentInformation()){
 614  0
                     empInfos.add(EntityEmploymentBo.to(empImpl));
 615  
                 }
 616  
         }
 617  0
         return empInfos;
 618  
         }
 619  
 
 620  
         private EntityBo getEntityBo(String entityId) {
 621  0
                 EntityBo entityImpl = getBusinessObjectService().findBySinglePrimaryKey(EntityBo.class, entityId);
 622  
         //TODO - remove this hack... This is here because currently jpa only seems to be going 2 levels deep on the eager fetching.
 623  0
                 if(entityImpl!=null  && entityImpl.getEntityTypeContactInfos() != null) {
 624  0
                 for (EntityTypeContactInfoBo et : entityImpl.getEntityTypeContactInfos()) {
 625  0
                         et.refresh();
 626  
                 }
 627  
         }
 628  0
                 return entityImpl;
 629  
         }
 630  
 
 631  
     @SuppressWarnings("unchecked")
 632  
         protected List<RoleBo> getRolesForPrincipal(String principalId) {
 633  0
                 if ( principalId == null ) {
 634  0
                         return new ArrayList<RoleBo>();
 635  
                 }
 636  0
                 Map<String,String> criteria = new HashMap<String,String>( 2 );
 637  0
                 criteria.put("members.memberId", principalId);
 638  0
                 criteria.put("members.memberTypeCode", MemberType.PRINCIPAL.getCode());
 639  0
                 return (List<RoleBo>)getBusinessObjectService().findMatching(RoleBo.class, criteria);
 640  
         }
 641  
 
 642  
         @SuppressWarnings("unchecked")
 643  
         protected List<RoleMemberBo> getRoleMembersForPrincipal(String principalId) {
 644  0
                 if ( principalId == null ) {
 645  0
                         return new ArrayList<RoleMemberBo>();
 646  
                 }
 647  0
                 Map<String,String> criteria = new HashMap<String,String>( 2 );
 648  0
                 criteria.put("memberId", principalId);
 649  0
                 criteria.put("memberTypeCode", MemberType.PRINCIPAL.getCode());
 650  0
                 return (List<RoleMemberBo>)getBusinessObjectService().findMatching(RoleMemberBo.class, criteria);
 651  
         }
 652  
 
 653  
         public RoleMemberBo getRoleMember(String roleMemberId) {
 654  0
                 if ( roleMemberId == null ) {
 655  0
                         return null;
 656  
                 }
 657  0
                 Map<String,String> criteria = new HashMap<String,String>( 2 );
 658  0
                 criteria.put("roleMemberId", roleMemberId);
 659  0
                 return getBusinessObjectService().findByPrimaryKey(RoleMemberBo.class, criteria);
 660  
         }
 661  
 
 662  
     @SuppressWarnings("unchecked")
 663  
         protected List<RoleResponsibilityActionBo> getRoleRspActions(String roleMemberId) {
 664  0
                 Map<String,String> criteria = new HashMap<String,String>( 1 );
 665  0
                 criteria.put(KIMPropertyConstants.RoleMember.ROLE_MEMBER_ID, roleMemberId);
 666  0
                 return (List<RoleResponsibilityActionBo>)getBusinessObjectService().findMatching(RoleResponsibilityActionBo.class, criteria);
 667  
         }
 668  
 
 669  
     protected List<KimDocumentRoleMember> populateDocRolePrncpl(String namespaceCode, List <RoleMemberBo> roleMembers, String principalId, List<KimAttributeField> definitions) {
 670  0
                 List <KimDocumentRoleMember> docRoleMembers = new ArrayList <KimDocumentRoleMember>();
 671  0
                 if(ObjectUtils.isNotNull(roleMembers)){
 672  0
                     for (RoleMemberBo rolePrincipal : roleMembers) {
 673  0
                             if (rolePrincipal.isActive(new Timestamp(System.currentTimeMillis())) && MemberType.PRINCIPAL.equals(
 674  
                         rolePrincipal.getMemberType()) &&
 675  
                                             StringUtils.equals(rolePrincipal.getMemberId(), principalId)) {
 676  0
                                 KimDocumentRoleMember docRolePrncpl = new KimDocumentRoleMember();
 677  0
                                 docRolePrncpl.setMemberId(rolePrincipal.getMemberId());
 678  0
                                 docRolePrncpl.setRoleMemberId(rolePrincipal.getRoleMemberId());
 679  0
                                 docRolePrncpl.setActive(rolePrincipal.isActive(new Timestamp(System.currentTimeMillis())));
 680  0
                                 docRolePrncpl.setRoleId(rolePrincipal.getRoleId());
 681  0
                                 docRolePrncpl.setActiveFromDate(rolePrincipal.getActiveFromDateValue());
 682  0
                                 docRolePrncpl.setActiveToDate(rolePrincipal.getActiveToDateValue());
 683  0
                                  docRolePrncpl.setQualifiers(populateDocRoleQualifier(namespaceCode, rolePrincipal.getAttributeDetails(), definitions));
 684  0
                                  docRolePrncpl.setEdit(true);
 685  0
                                 docRoleMembers.add(docRolePrncpl);
 686  0
                              }
 687  
                     }
 688  
                 }
 689  0
             return docRoleMembers;
 690  
     }
 691  
 
 692  
     // UI layout for rolequalifier is a little different from kimroleattribute set up.
 693  
     // each principal may have member with same role multiple times with different qualifier, but the role
 694  
     // only displayed once, and the qualifier displayed multiple times.
 695  
     protected List<KimDocumentRoleQualifier> populateDocRoleQualifier(String namespaceCode, List <RoleMemberAttributeDataBo> qualifiers, List<KimAttributeField> definitions) {
 696  
 
 697  0
                 List <KimDocumentRoleQualifier> docRoleQualifiers = new ArrayList <KimDocumentRoleQualifier>();
 698  0
                 if(definitions!=null){
 699  0
                         for (KimAttributeField definition : definitions) {
 700  0
                                 String attrDefId=definition.getId();
 701  0
                                 boolean qualifierFound = false;
 702  0
                                 if(ObjectUtils.isNotNull(qualifiers)){
 703  0
                                         for (RoleMemberAttributeDataBo qualifier : qualifiers) {
 704  0
                                                 if (attrDefId!=null && StringUtils.equals(attrDefId, qualifier.getKimAttributeId())) {
 705  0
                                                     KimDocumentRoleQualifier docRoleQualifier = new KimDocumentRoleQualifier();
 706  0
                                                     docRoleQualifier.setAttrDataId(qualifier.getId());
 707  0
                                                     docRoleQualifier.setAttrVal(qualifier.getAttributeValue());
 708  0
                                                     docRoleQualifier.setKimAttrDefnId(qualifier.getKimAttributeId());
 709  0
                                                     docRoleQualifier.setKimAttribute(qualifier.getKimAttribute());
 710  0
                                                     docRoleQualifier.setKimTypId(qualifier.getKimTypeId());
 711  0
                                                     docRoleQualifier.setRoleMemberId(qualifier.getAssignedToId());
 712  0
                                                     docRoleQualifier.setEdit(true);
 713  0
                                                     formatAttrValIfNecessary(docRoleQualifier);
 714  0
                                                     docRoleQualifiers.add(docRoleQualifier);
 715  0
                                                     qualifierFound = true;
 716  0
                                                     break;
 717  
                                                 }
 718  
                                         }
 719  
                                 }
 720  0
                                 if (!qualifierFound) {
 721  0
                                     KimDocumentRoleQualifier docRoleQualifier = new KimDocumentRoleQualifier();
 722  0
                                     docRoleQualifier.setAttrVal("");
 723  0
                                     docRoleQualifier.setKimAttrDefnId(attrDefId);
 724  0
                                     docRoleQualifier.refreshReferenceObject("kimAttribute");
 725  0
                                     docRoleQualifiers.add(docRoleQualifier);
 726  
                                 }
 727  0
                         }
 728  
                         // If all of the qualifiers are empty, return an empty list
 729  
                         // This is to prevent dynamic qualifiers from appearing in the
 730  
                         // person maintenance roles tab.  see KULRICE-3989 for more detail
 731  
                         // and KULRICE-5071 for detail on switching from config value to 
 732  
                         // application-scoped parameter
 733  0
                         if (!isBlankRoleQualifierVisible(namespaceCode)) {
 734  0
                                 int qualCount = 0;
 735  0
                                 for (KimDocumentRoleQualifier qual : docRoleQualifiers){
 736  0
                                         if (StringUtils.isEmpty(qual.getAttrVal())){
 737  0
                                                 qualCount++;
 738  
                                         }
 739  
                                 }
 740  0
                                 if (qualCount == docRoleQualifiers.size()){
 741  0
                                         return new ArrayList <KimDocumentRoleQualifier>();
 742  
                                 }
 743  
                         }
 744  
                 }
 745  0
             return docRoleQualifiers;
 746  
     }
 747  
 
 748  
     protected List<PersonDocumentName> loadNames( IdentityManagementPersonDocument personDoc, String principalId, List <EntityName> names, boolean suppressDisplay ) {
 749  0
                 List<PersonDocumentName> docNames = new ArrayList<PersonDocumentName>();
 750  0
                 if(ObjectUtils.isNotNull(names)){
 751  0
                         for (EntityName name: names) {
 752  0
                                 if(name.isActive()){
 753  0
                                         PersonDocumentName docName = new PersonDocumentName();
 754  0
                     if (name.getNameType() != null) {
 755  0
                                             docName.setNameCode(name.getNameType().getCode());
 756  
                     }
 757  
 
 758  
                                         //We do not need to check the privacy setting here - The UI should care of it
 759  0
                                         docName.setFirstName(name.getFirstNameUnmasked());
 760  0
                                         docName.setLastName(name.getLastNameUnmasked());
 761  0
                                         docName.setMiddleName(name.getMiddleNameUnmasked());
 762  0
                                         docName.setNamePrefix(name.getNamePrefixUnmasked());
 763  0
                                         docName.setNameSuffix(name.getNameSuffixUnmasked());
 764  
 
 765  0
                                         docName.setActive(name.isActive());
 766  0
                                         docName.setDflt(name.isDefaultValue());
 767  0
                                         docName.setEdit(true);
 768  0
                                         docName.setEntityNameId(name.getId());
 769  0
                                         docNames.add(docName);
 770  0
                                 }
 771  
                         }
 772  
                 }
 773  0
                 return docNames;
 774  
         }
 775  
 
 776  
         public boolean canModifyEntity( String currentUserPrincipalId, String toModifyPrincipalId ){
 777  0
                 return (StringUtils.isNotBlank(currentUserPrincipalId) && StringUtils.isNotBlank(toModifyPrincipalId) &&
 778  
                                 currentUserPrincipalId.equals(toModifyPrincipalId)) ||
 779  
                                 getPermissionService().isAuthorized(
 780  
                                                 currentUserPrincipalId,
 781  
                                                 KimConstants.NAMESPACE_CODE,
 782  
                                                 KimConstants.PermissionNames.MODIFY_ENTITY,
 783  
                                                 Collections.<String, String>emptyMap(),
 784  
                                                 Collections.singletonMap(KimConstants.AttributeConstants.PRINCIPAL_ID, currentUserPrincipalId));
 785  
         }
 786  
 
 787  
         public boolean canOverrideEntityPrivacyPreferences( String currentUserPrincipalId, String toModifyPrincipalId ){
 788  0
                 return (StringUtils.isNotBlank(currentUserPrincipalId) && StringUtils.isNotBlank(toModifyPrincipalId) &&
 789  
                                 currentUserPrincipalId.equals(toModifyPrincipalId)) ||
 790  
                                 getPermissionService().isAuthorized(
 791  
                                                 currentUserPrincipalId,
 792  
                                                 KimConstants.NAMESPACE_CODE,
 793  
                                                 KimConstants.PermissionNames.OVERRIDE_ENTITY_PRIVACY_PREFERENCES,
 794  
                                                 Collections.<String, String>emptyMap(),
 795  
                                                 Collections.singletonMap(KimConstants.AttributeConstants.PRINCIPAL_ID, currentUserPrincipalId) );
 796  
         }
 797  
 
 798  
         protected boolean canAssignToRole(IdentityManagementRoleDocument document, String initiatorPrincipalId){
 799  0
         boolean rulePassed = true;
 800  0
         Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
 801  0
         additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getRoleNamespace());
 802  0
         additionalPermissionDetails.put(KimConstants.AttributeConstants.ROLE_NAME, document.getRoleName());
 803  0
                 if(!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
 804  
                                 document, KimConstants.NAMESPACE_CODE, KimConstants.PermissionTemplateNames.ASSIGN_ROLE,
 805  
                                 initiatorPrincipalId, additionalPermissionDetails, null)){
 806  0
             rulePassed = false;
 807  
                 }
 808  0
                 return rulePassed;
 809  
         }
 810  
 
 811  
         protected List<PersonDocumentAffiliation> loadAffiliations(List <EntityAffiliation> affiliations, List<EntityEmployment> empInfos) {
 812  0
                 List<PersonDocumentAffiliation> docAffiliations = new ArrayList<PersonDocumentAffiliation>();
 813  0
                 if(ObjectUtils.isNotNull(affiliations)){
 814  0
                         for (EntityAffiliation affiliation: affiliations) {
 815  0
                                 if(affiliation.isActive()){
 816  0
                                         PersonDocumentAffiliation docAffiliation = new PersonDocumentAffiliation();
 817  0
                                         docAffiliation.setAffiliationTypeCode(affiliation.getAffiliationType().getCode());
 818  0
                                         docAffiliation.setCampusCode(affiliation.getCampusCode());
 819  0
                                         docAffiliation.setActive(affiliation.isActive());
 820  0
                                         docAffiliation.setDflt(affiliation.isDefaultValue());
 821  0
                                         docAffiliation.setEntityAffiliationId(affiliation.getId());
 822  0
                                         docAffiliation.refreshReferenceObject("affiliationType");
 823  
                                         // EntityAffiliationImpl does not define empinfos as collection
 824  0
                                         docAffiliations.add(docAffiliation);
 825  0
                                         docAffiliation.setEdit(true);
 826  
                                         // employment informations
 827  0
                                         List<PersonDocumentEmploymentInfo> docEmploymentInformations = new ArrayList<PersonDocumentEmploymentInfo>();
 828  0
                                         if(ObjectUtils.isNotNull(empInfos)){
 829  0
                                                 for (EntityEmployment empInfo: empInfos) {
 830  0
                                                         if (empInfo.isActive()
 831  
                                     && StringUtils.equals(docAffiliation.getEntityAffiliationId(),
 832  
                                                           (empInfo.getEntityAffiliation() != null ? empInfo.getEntityAffiliation().getId() : null))) {
 833  0
                                                                 PersonDocumentEmploymentInfo docEmpInfo = new PersonDocumentEmploymentInfo();
 834  0
                                                                 docEmpInfo.setEntityEmploymentId(empInfo.getEmployeeId());
 835  0
                                                                 docEmpInfo.setEmployeeId(empInfo.getEmployeeId());
 836  0
                                                                 docEmpInfo.setEmploymentRecordId(empInfo.getEmploymentRecordId());
 837  0
                                                                 docEmpInfo.setBaseSalaryAmount(empInfo.getBaseSalaryAmount());
 838  0
                                                                 docEmpInfo.setPrimaryDepartmentCode(empInfo.getPrimaryDepartmentCode());
 839  0
                                                                 docEmpInfo.setEmploymentStatusCode(empInfo.getEmployeeStatus() != null ? empInfo.getEmployeeStatus().getCode() : null);
 840  0
                                                                 docEmpInfo.setEmploymentTypeCode(empInfo.getEmployeeType() != null ? empInfo.getEmployeeType().getCode() : null);
 841  0
                                                                 docEmpInfo.setActive(empInfo.isActive());
 842  0
                                                                 docEmpInfo.setPrimary(empInfo.isPrimary());
 843  0
                                                                 docEmpInfo.setEntityAffiliationId(empInfo.getEntityAffiliation() != null ? empInfo.getEntityAffiliation().getId() : null);
 844  
                                                                 // there is no version number on KimEntityEmploymentInformationInfo
 845  
                                                                 //docEmpInfo.setVersionNumber(empInfo.getVersionNumber());
 846  0
                                                                 docEmpInfo.setEdit(true);
 847  0
                                                                 docEmpInfo.refreshReferenceObject("employmentType");
 848  0
                                                                 docEmploymentInformations.add(docEmpInfo);
 849  0
                                                         }
 850  
                                                 }
 851  
                                         }
 852  0
                                         docAffiliation.setEmpInfos(docEmploymentInformations);
 853  0
                                 }
 854  
                         }
 855  
                 }
 856  0
                 return docAffiliations;
 857  
 
 858  
         }
 859  
 
 860  
     protected boolean setupPrincipal(IdentityManagementPersonDocument identityManagementPersonDocument, EntityBo kimEntity, List<PrincipalBo> origPrincipals) {
 861  0
             boolean inactivatingPrincipal = false;
 862  0
                 List<PrincipalBo> principals = new ArrayList<PrincipalBo>();
 863  0
                 Principal.Builder principal = Principal.Builder.create(identityManagementPersonDocument.getPrincipalName());
 864  0
                 principal.setPrincipalId(identityManagementPersonDocument.getPrincipalId());
 865  0
                 principal.setPassword(identityManagementPersonDocument.getPassword());
 866  0
                 principal.setActive(identityManagementPersonDocument.isActive());
 867  0
                 principal.setEntityId(identityManagementPersonDocument.getEntityId());
 868  0
                 if(ObjectUtils.isNotNull(origPrincipals)){
 869  0
                         for (PrincipalBo prncpl : origPrincipals) {
 870  0
                                 if (prncpl.getPrincipalId()!=null && StringUtils.equals(prncpl.getPrincipalId(), principal.getPrincipalId())) {
 871  0
                                         principal.setVersionNumber(prncpl.getVersionNumber());
 872  0
                     principal.setObjectId(prncpl.getObjectId());
 873  
                                         // check if inactivating the principal
 874  0
                                         if ( prncpl.isActive() && !principal.isActive() ) {
 875  0
                                                 inactivatingPrincipal = true;
 876  
                                         }
 877  
                                 }
 878  
                         }
 879  
                 }
 880  0
                 principals.add(PrincipalBo.from(principal.build()));
 881  
 
 882  0
                 kimEntity.setPrincipals(principals);
 883  0
                 return inactivatingPrincipal;
 884  
         }
 885  
 
 886  
     protected void setupPrivacy(IdentityManagementPersonDocument identityManagementPersonDocument, EntityBo kimEntity, EntityPrivacyPreferencesBo origPrivacy) {
 887  0
                 EntityPrivacyPreferencesBo privacyPreferences = new EntityPrivacyPreferencesBo();
 888  0
                 privacyPreferences.setEntityId(identityManagementPersonDocument.getEntityId());
 889  0
                 privacyPreferences.setSuppressAddress(identityManagementPersonDocument.getPrivacy().isSuppressAddress());
 890  0
                 privacyPreferences.setSuppressEmail(identityManagementPersonDocument.getPrivacy().isSuppressEmail());
 891  0
                 privacyPreferences.setSuppressName(identityManagementPersonDocument.getPrivacy().isSuppressName());
 892  0
                 privacyPreferences.setSuppressPhone(identityManagementPersonDocument.getPrivacy().isSuppressPhone());
 893  0
                 privacyPreferences.setSuppressPersonal(identityManagementPersonDocument.getPrivacy().isSuppressPersonal());
 894  0
                 if (ObjectUtils.isNotNull(origPrivacy)) {
 895  0
                         privacyPreferences.setVersionNumber(origPrivacy.getVersionNumber());
 896  0
             privacyPreferences.setObjectId(origPrivacy.getObjectId());
 897  
                 }
 898  0
                 kimEntity.setPrivacyPreferences(privacyPreferences);
 899  0
         }
 900  
     protected PersonDocumentPrivacy loadPrivacyReferences(EntityPrivacyPreferences privacyPreferences) {
 901  0
                 PersonDocumentPrivacy docPrivacy = new PersonDocumentPrivacy();
 902  0
                 docPrivacy.setSuppressAddress(privacyPreferences.isSuppressAddress());
 903  0
                 docPrivacy.setSuppressEmail(privacyPreferences.isSuppressEmail());
 904  0
                 docPrivacy.setSuppressName(privacyPreferences.isSuppressName());
 905  0
                 docPrivacy.setSuppressPhone(privacyPreferences.isSuppressPhone());
 906  0
                 docPrivacy.setSuppressPersonal(privacyPreferences.isSuppressPersonal());
 907  0
                 docPrivacy.setEdit(true);
 908  0
                 return docPrivacy;
 909  
         }
 910  
 
 911  
     protected void setupName(IdentityManagementPersonDocument identityManagementPersonDocument, EntityBo kimEntity, List<EntityNameBo> origNames) {
 912  0
             if ( !identityManagementPersonDocument.getPrivacy().isSuppressName() ||
 913  
                             canOverrideEntityPrivacyPreferences( getInitiatorPrincipalId(identityManagementPersonDocument), identityManagementPersonDocument.getPrincipalId() ) ) {
 914  0
                     List<EntityNameBo> entityNames = new ArrayList<EntityNameBo>();
 915  0
                         if(CollectionUtils.isNotEmpty(identityManagementPersonDocument.getNames())){
 916  0
                                 for (PersonDocumentName name : identityManagementPersonDocument.getNames()) {
 917  0
                                     EntityNameBo entityName = new EntityNameBo();
 918  0
                                         entityName.setNameCode(name.getNameCode());
 919  0
                     if (name.getEntityNameType() != null) {
 920  0
                         entityName.setNameType(name.getEntityNameType());
 921  
                     } else {
 922  0
                         if (StringUtils.isNotEmpty(name.getNameCode())) {
 923  0
                             entityName.setNameType(
 924  
                                     EntityNameTypeBo.from(getIdentityService().getNameType(name.getNameCode())));
 925  
                         }
 926  
                     }
 927  0
                                         entityName.setFirstName(name.getFirstName());
 928  0
                                         entityName.setLastName(name.getLastName());
 929  0
                                         entityName.setMiddleName(name.getMiddleName());
 930  0
                                         entityName.setNamePrefix(name.getNamePrefix());
 931  0
                                         entityName.setNameSuffix(name.getNameSuffix());
 932  0
                                         entityName.setActive(name.isActive());
 933  0
                                         entityName.setDefaultValue(name.isDflt());
 934  0
                                         entityName.setId(name.getEntityNameId());
 935  0
                                         entityName.setEntityId(identityManagementPersonDocument.getEntityId());
 936  0
                                         if(ObjectUtils.isNotNull(origNames)){
 937  0
                                                 for (EntityNameBo origName : origNames) {
 938  0
                                                         if (origName.getId()!=null && StringUtils.equals(origName.getId(), entityName.getId())) {
 939  0
                                                                 entityName.setVersionNumber(origName.getVersionNumber());
 940  
                                                         }
 941  
 
 942  
                                                 }
 943  
                                         }
 944  0
                                         entityNames.add(entityName);
 945  0
                                 }
 946  
                         }
 947  0
                         kimEntity.setNames(entityNames);
 948  
             }
 949  0
         }
 950  
 
 951  
     protected void setupAffiliation(IdentityManagementPersonDocument identityManagementPersonDocument, EntityBo kimEntity,List<EntityAffiliationBo> origAffiliations, List<EntityEmploymentBo> origEmpInfos) {
 952  0
                 List<EntityAffiliationBo> entityAffiliations = new ArrayList<EntityAffiliationBo>();
 953  
                 // employment informations
 954  0
                 List<EntityEmploymentBo> entityEmploymentInformations = new ArrayList<EntityEmploymentBo>();
 955  0
                 if(CollectionUtils.isNotEmpty(identityManagementPersonDocument.getAffiliations())){
 956  0
                         for (PersonDocumentAffiliation affiliation : identityManagementPersonDocument.getAffiliations()) {
 957  0
                                 EntityAffiliationBo entityAffiliation = new EntityAffiliationBo();
 958  0
                                 entityAffiliation.setAffiliationTypeCode(affiliation.getAffiliationTypeCode());
 959  0
                 if (affiliation.getAffiliationType() != null) {
 960  0
                         entityAffiliation.setAffiliationType(affiliation.getAffiliationType());
 961  
                 } else {
 962  0
                     if (StringUtils.isNotEmpty(affiliation.getAffiliationTypeCode())) {
 963  0
                         entityAffiliation.setAffiliationType(EntityAffiliationTypeBo.from(getIdentityService().getAffiliationType(
 964  
                                 affiliation.getAffiliationTypeCode())));
 965  
                     }
 966  
                 }
 967  0
                                 entityAffiliation.setCampusCode(affiliation.getCampusCode());
 968  0
                                 entityAffiliation.setActive(affiliation.isActive());
 969  0
                                 entityAffiliation.setDefaultValue(affiliation.isDflt());
 970  0
                                 entityAffiliation.setEntityId(identityManagementPersonDocument.getEntityId());
 971  0
                                 entityAffiliation.setId(affiliation.getEntityAffiliationId());
 972  0
                                 if(ObjectUtils.isNotNull(origAffiliations)){
 973  
                                 // EntityAffiliationImpl does not define empinfos as collection
 974  0
                                         for (EntityAffiliationBo origAffiliation : origAffiliations) {
 975  0
                                                 if(isSameAffiliation(origAffiliation, entityAffiliation)){
 976  0
                                                         entityAffiliation.setId(origAffiliation.getId());
 977  
                                                 }
 978  0
                                                 if (origAffiliation.getId()!=null && StringUtils.equals(origAffiliation.getId(), entityAffiliation.getId())) {
 979  0
                                                         entityAffiliation.setVersionNumber(origAffiliation.getVersionNumber());
 980  
                                                 }
 981  
                                         }
 982  
                                 }
 983  0
                                 entityAffiliations.add(entityAffiliation);
 984  0
                                 int employeeRecordCounter = origEmpInfos==null?0:origEmpInfos.size();
 985  0
                                 if(CollectionUtils.isNotEmpty(affiliation.getEmpInfos())){
 986  0
                                         for (PersonDocumentEmploymentInfo empInfo : affiliation.getEmpInfos()) {
 987  0
                                                 EntityEmploymentBo entityEmpInfo = new EntityEmploymentBo();
 988  0
                                                 entityEmpInfo.setId(empInfo.getEntityEmploymentId());
 989  0
                                                 entityEmpInfo.setEmployeeId(empInfo.getEmployeeId());
 990  0
                                                 entityEmpInfo.setEmploymentRecordId(empInfo.getEmploymentRecordId());
 991  0
                                                 entityEmpInfo.setBaseSalaryAmount(empInfo.getBaseSalaryAmount());
 992  0
                                                 entityEmpInfo.setPrimaryDepartmentCode(empInfo.getPrimaryDepartmentCode());
 993  0
                                                 entityEmpInfo.setEmployeeStatusCode(empInfo.getEmploymentStatusCode());
 994  0
                         if (empInfo.getEmploymentStatus() != null) {
 995  0
                             entityEmpInfo.setEmployeeStatus(empInfo.getEmploymentStatus());
 996  
                         } else {
 997  0
                             if (StringUtils.isNotEmpty(empInfo.getEmploymentStatusCode())) {
 998  0
                                 entityEmpInfo.setEmployeeStatus(EntityEmploymentStatusBo
 999  
                                         .from(getIdentityService().getEmploymentStatus(empInfo.getEmploymentStatusCode())));
 1000  
                             }
 1001  
                         }
 1002  0
                                                 entityEmpInfo.setEmployeeTypeCode(empInfo.getEmploymentTypeCode());
 1003  0
                         if (empInfo.getEmploymentType() != null) {
 1004  0
                             entityEmpInfo.setEmployeeType(empInfo.getEmploymentType());
 1005  
                         } else {
 1006  0
                             if (StringUtils.isNotEmpty(empInfo.getEmploymentTypeCode())) {
 1007  0
                                 entityEmpInfo.setEmployeeType(EntityEmploymentTypeBo
 1008  
                                         .from(getIdentityService().getEmploymentType(empInfo.getEmploymentTypeCode())));
 1009  
                             }
 1010  
                         }
 1011  0
                                                 entityEmpInfo.setActive(empInfo.isActive());
 1012  0
                                                 entityEmpInfo.setPrimary(empInfo.isPrimary());
 1013  0
                                                 entityEmpInfo.setEntityId(identityManagementPersonDocument.getEntityId());
 1014  0
                                                 entityEmpInfo.setEntityAffiliationId(empInfo.getEntityAffiliationId());
 1015  0
                                                 if(ObjectUtils.isNotNull(origEmpInfos)){
 1016  0
                                                         for (EntityEmploymentBo origEmpInfo : origEmpInfos) {
 1017  0
                                                                 if(isSameEmpInfo(origEmpInfo, entityEmpInfo)){
 1018  0
                                                                         entityEmpInfo.setId(entityEmpInfo.getEntityId());
 1019  
                                                                 }
 1020  
 
 1021  0
                                                                 if (origEmpInfo.getId()!=null && StringUtils.equals(origEmpInfo.getId(), entityEmpInfo.getId())) {
 1022  0
                                                                         entityEmpInfo.setVersionNumber(origEmpInfo.getVersionNumber());
 1023  0
                                                                         entityEmpInfo.setEmploymentRecordId(empInfo.getEmploymentRecordId());
 1024  
                                                                 }
 1025  
                                                         }
 1026  
                                                 }
 1027  0
                                                 if(StringUtils.isEmpty(entityEmpInfo.getEmploymentRecordId())){
 1028  0
                                                         employeeRecordCounter++;
 1029  0
                                                         entityEmpInfo.setEmploymentRecordId(employeeRecordCounter+"");
 1030  
                                                 }
 1031  0
                                                 entityEmploymentInformations.add(entityEmpInfo);
 1032  0
                                         }
 1033  
                                 }
 1034  0
                         }
 1035  
                 }
 1036  0
                 kimEntity.setEmploymentInformation(entityEmploymentInformations);
 1037  0
                 kimEntity.setAffiliations(entityAffiliations);
 1038  0
         }
 1039  
 
 1040  
     /*
 1041  
      * Added to address KULRICE-5071 : "Move the 'show blank qualifier' kim toggle from a Config param to a System param"
 1042  
      * 
 1043  
      * This method first checks for a namespace specific parameter with a detailTypeCode of "All" and parameterName of "KIM_SHOW_BLANK_QUALIFIERS". 
 1044  
      * If no parameter is found, it checks for the config property "kim.show.blank.qualifiers", and defaults to true if no config property exists. 
 1045  
      *
 1046  
      */
 1047  
     private boolean isBlankRoleQualifierVisible(String namespaceCode) {
 1048  0
             boolean showBlankQualifiers = true;
 1049  
                 
 1050  0
                 Parameter param = getParameterService().getParameter(namespaceCode, KRADConstants.DetailTypes.ALL_DETAIL_TYPE, KimConstants.ParameterKey.SHOW_BLANK_QUALIFIERS);
 1051  0
             if (param != null) {
 1052  0
                     showBlankQualifiers = "Y".equals(param.getValue());
 1053  
             } else {
 1054  0
                     String configProperty = ConfigContext.getCurrentContextConfig().getProperty(SHOW_BLANK_QUALIFIERS);
 1055  0
                     if (configProperty != null) {
 1056  0
                             showBlankQualifiers = Boolean.valueOf(configProperty);
 1057  
             }
 1058  
             }
 1059  
             
 1060  0
             return showBlankQualifiers;
 1061  
     }
 1062  
     
 1063  
    private boolean isSameAffiliation(EntityAffiliationBo origAffiliation, EntityAffiliationBo entityAffiliation){
 1064  
             //entityId
 1065  
             //affiliationTypeCode
 1066  
             //campusCode
 1067  0
             return (origAffiliation!=null && entityAffiliation!=null) &&
 1068  
             (StringUtils.isNotEmpty(origAffiliation.getCampusCode()) && StringUtils.equals(origAffiliation.getCampusCode(), entityAffiliation.getCampusCode()))
 1069  
             &&
 1070  
             (StringUtils.isNotEmpty(origAffiliation.getAffiliationTypeCode()) && StringUtils.equals(origAffiliation.getAffiliationTypeCode(), entityAffiliation.getAffiliationTypeCode()))
 1071  
                  &&
 1072  
                  (StringUtils.isNotEmpty(origAffiliation.getEntityId()) && StringUtils.equals(origAffiliation.getEntityId(), entityAffiliation.getEntityId()));
 1073  
     }
 1074  
 
 1075  
     private boolean isSameEmpInfo(EntityEmploymentBo origEmpInfo, EntityEmploymentBo entityEmpInfo){
 1076  
             //emp_info:
 1077  
                     //employmentRecordId
 1078  
                     //entityId
 1079  
                     //These should be unique - add a business rule
 1080  0
             return (origEmpInfo!=null && entityEmpInfo!=null)
 1081  
                             && (StringUtils.isNotEmpty(origEmpInfo.getEmploymentRecordId())
 1082  
                                             && StringUtils.equals(origEmpInfo.getEmploymentRecordId(), entityEmpInfo.getEmploymentRecordId() )
 1083  
                                     )
 1084  
                             && StringUtils.equals( origEmpInfo.getEntityId(),entityEmpInfo.getEntityId());
 1085  
     }
 1086  
 
 1087  
     protected void setupPhone(IdentityManagementPersonDocument identityManagementPersonDocument, EntityTypeContactInfoBo entityType, List<EntityPhoneBo> origPhones) {
 1088  0
             if ( !identityManagementPersonDocument.getPrivacy().isSuppressPhone() || canOverrideEntityPrivacyPreferences(getInitiatorPrincipalId(identityManagementPersonDocument), identityManagementPersonDocument.getPrincipalId()) ) {
 1089  0
                         List<EntityPhoneBo> entityPhones = new ArrayList<EntityPhoneBo>();
 1090  0
                         if(CollectionUtils.isNotEmpty(identityManagementPersonDocument.getPhones())){
 1091  0
                                 for (PersonDocumentPhone phone : identityManagementPersonDocument.getPhones()) {
 1092  0
                                         EntityPhoneBo entityPhone = new EntityPhoneBo();
 1093  0
                                         entityPhone.setPhoneTypeCode(phone.getPhoneTypeCode());
 1094  0
                     if (phone.getPhoneType() != null) {
 1095  0
                         entityPhone.setPhoneType(phone.getPhoneType());
 1096  
                     } else {
 1097  0
                         if (StringUtils.isNotEmpty(phone.getPhoneTypeCode())) {
 1098  0
                             entityPhone.setPhoneType(EntityPhoneTypeBo
 1099  
                                     .from(getIdentityService().getAddressType(phone.getPhoneTypeCode())));
 1100  
                         }
 1101  
                     }
 1102  0
                                         entityPhone.setEntityId(identityManagementPersonDocument.getEntityId());
 1103  0
                                         entityPhone.setId(phone.getEntityPhoneId());
 1104  0
                                         entityPhone.setEntityTypeCode(entityType.getEntityTypeCode());
 1105  0
                                         entityPhone.setPhoneNumber(phone.getPhoneNumber());
 1106  0
                                         entityPhone.setCountryCode(phone.getCountryCode());
 1107  0
                                         entityPhone.setExtension(phone.getExtension());
 1108  0
                                         entityPhone.setExtensionNumber(phone.getExtensionNumber());
 1109  0
                                         entityPhone.setActive(phone.isActive());
 1110  0
                                         entityPhone.setDefaultValue(phone.isDflt());
 1111  0
                                         if(ObjectUtils.isNotNull(origPhones)){
 1112  0
                                                 for (EntityPhoneContract origPhone : origPhones) {
 1113  0
                                                         if (origPhone.getId()!=null && StringUtils.equals(origPhone.getId(), entityPhone.getId())) {
 1114  0
                                                                 entityPhone.setVersionNumber(origPhone.getVersionNumber());
 1115  
                                                         }
 1116  
                                                 }
 1117  
                                         }
 1118  0
                                         entityPhone.setId(phone.getEntityPhoneId());
 1119  0
                                         entityPhones.add(entityPhone);
 1120  0
                                 }
 1121  
                         }
 1122  0
                         entityType.setPhoneNumbers(entityPhones);
 1123  
             }
 1124  0
         }
 1125  
 
 1126  
     protected List<PersonDocumentPhone> loadPhones(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<EntityPhone> entityPhones, boolean suppressDisplay ) {
 1127  0
                 List<PersonDocumentPhone> docPhones = new ArrayList<PersonDocumentPhone>();
 1128  0
                 if(ObjectUtils.isNotNull(entityPhones)){
 1129  0
                         for (EntityPhone phone: entityPhones) {
 1130  0
                                 if(phone.isActive()){
 1131  0
                                         PersonDocumentPhone docPhone = new PersonDocumentPhone();
 1132  0
                     if (phone.getPhoneType() != null) {
 1133  0
                                             docPhone.setPhoneTypeCode(phone.getPhoneType().getCode());
 1134  
                     }
 1135  
                                         //docPhone.setPhoneType(((KimEntityPhoneImpl)phone).getPhoneType());
 1136  0
                                         docPhone.setEntityTypeCode(phone.getEntityTypeCode());
 1137  
                                         //We do not need to check the privacy setting here - The UI should care of it
 1138  0
                                         docPhone.setPhoneNumber(phone.getPhoneNumberUnmasked());
 1139  0
                                         docPhone.setCountryCode(phone.getCountryCodeUnmasked());
 1140  0
                                         docPhone.setExtensionNumber(phone.getExtensionNumberUnmasked());
 1141  
 
 1142  0
                                         docPhone.setActive(phone.isActive());
 1143  0
                                         docPhone.setDflt(phone.isDefaultValue());
 1144  0
                                         docPhone.setEntityPhoneId(phone.getId());
 1145  0
                                         docPhone.setEdit(true);
 1146  0
                                         docPhones.add(docPhone);
 1147  0
                                 }
 1148  
                         }
 1149  
                 }
 1150  0
                 return docPhones;
 1151  
 
 1152  
         }
 1153  
 
 1154  
     protected void setupEmail(
 1155  
                         IdentityManagementPersonDocument identityManagementPersonDocument,
 1156  
                         EntityTypeContactInfoBo entityType, List<EntityEmailBo> origEmails) {
 1157  0
             if ( !identityManagementPersonDocument.getPrivacy().isSuppressEmail() || canOverrideEntityPrivacyPreferences(getInitiatorPrincipalId(identityManagementPersonDocument), identityManagementPersonDocument.getPrincipalId()) ) {
 1158  0
                         List<EntityEmailBo> entityEmails = new ArrayList<EntityEmailBo>();
 1159  0
                         if(CollectionUtils.isNotEmpty(identityManagementPersonDocument.getEmails())){
 1160  0
                                 for (PersonDocumentEmail email : identityManagementPersonDocument.getEmails()) {
 1161  0
                                         EntityEmailBo entityEmail = new EntityEmailBo();
 1162  0
                                         entityEmail.setEntityId(identityManagementPersonDocument.getEntityId());
 1163  0
                                         entityEmail.setEntityTypeCode(entityType.getEntityTypeCode());
 1164  0
                     if (email.getEmailType() != null) {
 1165  0
                         entityEmail.setEmailType(email.getEmailType());
 1166  
                     } else {
 1167  0
                         if (StringUtils.isNotEmpty(email.getEmailTypeCode())) {
 1168  0
                             entityEmail.setEmailType(
 1169  
                                     EntityEmailTypeBo.from(getIdentityService().getEmailType(email.getEmailTypeCode())));
 1170  
                         }
 1171  
                     }
 1172  0
                                         entityEmail.setEmailTypeCode(email.getEmailTypeCode());
 1173  0
                                         entityEmail.setEmailAddress(email.getEmailAddress());
 1174  0
                                         entityEmail.setActive(email.isActive());
 1175  0
                                         entityEmail.setDefaultValue(email.isDflt());
 1176  0
                                         entityEmail.setId(email.getEntityEmailId());
 1177  0
                                         if(ObjectUtils.isNotNull(origEmails)){
 1178  0
                                                 for (EntityEmailContract origEmail : origEmails) {
 1179  0
                                                         if (origEmail.getId()!=null && StringUtils.equals(origEmail.getId(), entityEmail.getId())) {
 1180  0
                                                                 entityEmail.setVersionNumber(origEmail.getVersionNumber());
 1181  
                                                         }
 1182  
                                                 }
 1183  
                                         }
 1184  0
                                         entityEmails.add(entityEmail);
 1185  0
                                 }
 1186  
                         }
 1187  0
                         entityType.setEmailAddresses(entityEmails);
 1188  
             }
 1189  0
         }
 1190  
     protected List<PersonDocumentEmail> loadEmails(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<EntityEmail> entityEmails, boolean suppressDisplay ) {
 1191  0
                 List<PersonDocumentEmail> emails = new ArrayList<PersonDocumentEmail>();
 1192  0
                 if(ObjectUtils.isNotNull(entityEmails)){
 1193  0
                         for (EntityEmail email: entityEmails) {
 1194  0
                                 if(email.isActive()){
 1195  0
                                         PersonDocumentEmail docEmail = new PersonDocumentEmail();
 1196  
                                         //docEmail.setEntityId(email.getEntityId());
 1197  0
                                         docEmail.setEntityTypeCode(email.getEntityTypeCode());
 1198  0
                     if (email.getEmailType() != null) {
 1199  0
                                             docEmail.setEmailTypeCode(email.getEmailType().getCode());
 1200  
                     }
 1201  
                                         // EmailType not on info object.
 1202  
                                         //docEmail.setEmailType(((KimEntityEmailImpl)email).getEmailType());
 1203  
                                         //We do not need to check the privacy setting here - The UI should care of it
 1204  0
                                         docEmail.setEmailAddress(email.getEmailAddressUnmasked());
 1205  
 
 1206  0
                                         docEmail.setActive(email.isActive());
 1207  0
                                         docEmail.setDflt(email.isDefaultValue());
 1208  0
                                         docEmail.setEntityEmailId(email.getId());
 1209  0
                                         docEmail.setEdit(true);
 1210  0
                                         emails.add(docEmail);
 1211  0
                                 }
 1212  
                         }
 1213  
                 }
 1214  0
                 return emails;
 1215  
         }
 1216  
 
 1217  
     protected void setupAddress(
 1218  
                         IdentityManagementPersonDocument identityManagementPersonDocument,
 1219  
                         EntityTypeContactInfoBo entityType, List<EntityAddressBo> origAddresses) {
 1220  0
             if ( !identityManagementPersonDocument.getPrivacy().isSuppressAddress() || canOverrideEntityPrivacyPreferences(getInitiatorPrincipalId(identityManagementPersonDocument), identityManagementPersonDocument.getPrincipalId()) ) {
 1221  0
                         List<EntityAddressBo> entityAddresses = new ArrayList<EntityAddressBo>();
 1222  0
                         if(CollectionUtils.isNotEmpty(identityManagementPersonDocument.getAddrs())){
 1223  0
                                 for (PersonDocumentAddress address : identityManagementPersonDocument.getAddrs()) {
 1224  0
                                         EntityAddressBo entityAddress = new EntityAddressBo();
 1225  0
                                         entityAddress.setEntityId(identityManagementPersonDocument.getEntityId());
 1226  0
                                         entityAddress.setEntityTypeCode(entityType.getEntityTypeCode());
 1227  0
                                         entityAddress.setAddressTypeCode(address.getAddressTypeCode());
 1228  0
                     if (address.getAddressType() != null) {
 1229  0
                         entityAddress.setAddressType(address.getAddressType());
 1230  
                     } else {
 1231  0
                         if (StringUtils.isNotEmpty(address.getAddressTypeCode())) {
 1232  0
                             entityAddress.setAddressType(EntityAddressTypeBo.from(
 1233  
                                     getIdentityService().getAddressType(address.getAddressTypeCode())));
 1234  
                         }
 1235  
                     }
 1236  0
                                         entityAddress.setLine1(address.getLine1());
 1237  0
                                         entityAddress.setLine2(address.getLine2());
 1238  0
                                         entityAddress.setLine3(address.getLine3());
 1239  0
                                         entityAddress.setStateProvinceCode(address.getStateProvinceCode());
 1240  0
                                         entityAddress.setPostalCode(address.getPostalCode());
 1241  0
                                         entityAddress.setCountryCode(address.getCountryCode());
 1242  0
                                         entityAddress.setCity(address.getCity());
 1243  0
                                         entityAddress.setActive(address.isActive());
 1244  0
                                         entityAddress.setDefaultValue(address.isDflt());
 1245  0
                                         entityAddress.setId(address.getEntityAddressId());
 1246  0
                                         if(ObjectUtils.isNotNull(origAddresses)){
 1247  0
                                                 for (EntityAddressContract origAddress : origAddresses) {
 1248  0
                                                         if (origAddress.getId()!=null && StringUtils.equals(origAddress.getId(), entityAddress.getId())) {
 1249  0
                                                                 entityAddress.setVersionNumber(origAddress.getVersionNumber());
 1250  
                                                         }
 1251  
                                                 }
 1252  
                                         }
 1253  0
                                         entityAddresses.add(entityAddress);
 1254  0
                                 }
 1255  
                         }
 1256  0
                         entityType.setAddresses(entityAddresses);
 1257  
             }
 1258  0
         }
 1259  
 
 1260  
     protected List<PersonDocumentAddress> loadAddresses(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<EntityAddress> entityAddresses, boolean suppressDisplay ) {
 1261  0
                 List<PersonDocumentAddress> docAddresses = new ArrayList<PersonDocumentAddress>();
 1262  0
                 if(ObjectUtils.isNotNull(entityAddresses)){
 1263  0
                         for (EntityAddress address: entityAddresses) {
 1264  0
                                 if(address.isActive()){
 1265  0
                                         PersonDocumentAddress docAddress = new PersonDocumentAddress();
 1266  0
                                         docAddress.setEntityTypeCode(address.getEntityTypeCode());
 1267  0
                                         docAddress.setAddressTypeCode(address.getAddressType().getCode());
 1268  
 
 1269  
                                         //We do not need to check the privacy setting here - The UI should care of it
 1270  0
                                         docAddress.setLine1(address.getLine1Unmasked());
 1271  0
                                         docAddress.setLine2(address.getLine2Unmasked());
 1272  0
                                         docAddress.setLine3(address.getLine3Unmasked());
 1273  0
                                         docAddress.setStateProvinceCode(address.getStateProvinceCodeUnmasked());
 1274  0
                                         docAddress.setPostalCode(address.getPostalCodeUnmasked());
 1275  0
                                         docAddress.setCountryCode(address.getCountryCodeUnmasked());
 1276  0
                                         docAddress.setCity(address.getCityUnmasked());
 1277  
 
 1278  0
                                         docAddress.setActive(address.isActive());
 1279  0
                                         docAddress.setDflt(address.isDefaultValue());
 1280  0
                                         docAddress.setEntityAddressId(address.getId());
 1281  0
                                         docAddress.setEdit(true);
 1282  0
                                         docAddresses.add(docAddress);
 1283  0
                                 }
 1284  
                         }
 1285  
                 }
 1286  0
                 return docAddresses;
 1287  
         }
 1288  
 
 1289  
 
 1290  
     protected List <GroupMemberBo> populateGroupMembers(IdentityManagementPersonDocument identityManagementPersonDocument) {
 1291  0
                 List <GroupMemberBo>  groupPrincipals = new ArrayList<GroupMemberBo>();
 1292  
 //                List<? extends Group> origGroups = getGroupService().getGroupsByPrincipalId(identityManagementPersonDocument.getPrincipalId());
 1293  0
                 if(CollectionUtils.isNotEmpty(identityManagementPersonDocument.getGroups())){
 1294  0
                         for (PersonDocumentGroup group : identityManagementPersonDocument.getGroups()) {
 1295  0
                                 GroupMember.Builder groupPrincipalImpl = GroupMember.Builder.create(group.getGroupId(), identityManagementPersonDocument.getPrincipalId(), KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE);
 1296  0
                                 if (group.getActiveFromDate() != null) {
 1297  0
                                         groupPrincipalImpl.setActiveFromDate(new DateTime(group.getActiveFromDate().getTime()));
 1298  
                                 }
 1299  0
                                 if (group.getActiveToDate() != null) {
 1300  0
                                         groupPrincipalImpl.setActiveToDate(new DateTime(group.getActiveToDate().getTime()));
 1301  
                                 }
 1302  0
                                 groupPrincipalImpl.setId(group.getGroupMemberId());
 1303  
 
 1304  
 
 1305  
                 //groupPrincipalImpl.setVersionNumber(group.getVersionNumber());
 1306  
                                 // get the ORM-layer optimisic locking value
 1307  
                                 // TODO: this should be replaced with the retrieval and storage of that value
 1308  
                                 // in the document tables and not re-retrieved here
 1309  0
                                 Collection<GroupMember> currGroupMembers = getGroupService().getMembers(Collections.singletonList(group.getGroupId()));
 1310  0
                                 if(ObjectUtils.isNotNull(currGroupMembers)){
 1311  0
                                         for (GroupMember origGroupMember: currGroupMembers) {
 1312  0
                         if (origGroupMember.isActive(new DateTime(System.currentTimeMillis()))
 1313  
                             && KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.equals(origGroupMember.getType())) {
 1314  0
                             if(origGroupMember.getId()!=null && StringUtils.equals(origGroupMember.getId(), group.getGroupMemberId())){
 1315  0
                                 groupPrincipalImpl.setObjectId(origGroupMember.getObjectId());
 1316  0
                                 groupPrincipalImpl.setVersionNumber(origGroupMember.getVersionNumber());
 1317  
                             }
 1318  
                         }
 1319  
                                         }
 1320  
                                 }
 1321  
 
 1322  0
                                 groupPrincipals.add(GroupMemberBo.from(groupPrincipalImpl.build()));
 1323  
 
 1324  0
                         }
 1325  
                 }
 1326  0
                 return groupPrincipals;
 1327  
         }
 1328  
 
 1329  
     protected List<RoleMemberBo> populateRoleMembers(IdentityManagementPersonDocument identityManagementPersonDocument) {
 1330  0
                 List<RoleBo> origRoles = getRolesForPrincipal(identityManagementPersonDocument.getPrincipalId());
 1331  
 
 1332  0
                 List <RoleMemberBo> roleMembers = new ArrayList<RoleMemberBo>();
 1333  0
                 if(CollectionUtils.isNotEmpty(identityManagementPersonDocument.getRoles())){
 1334  0
                         for (PersonDocumentRole role : identityManagementPersonDocument.getRoles()) {
 1335  
                                 //if(role.isEditable()){
 1336  0
                                         List<RoleMemberBo> origRoleMembers = new ArrayList<RoleMemberBo>();
 1337  0
                                         if(ObjectUtils.isNotNull(origRoles)){
 1338  0
                                                 for (RoleBo origRole : origRoles) {
 1339  0
                                                         if (origRole.getId()!=null && StringUtils.equals(origRole.getId(), role.getRoleId())) {
 1340  0
                                                                 origRoleMembers = origRole.getMembers();
 1341  0
                                                                 break;
 1342  
                                                         }
 1343  
                                                 }
 1344  
                                         }
 1345  0
                                         if (role.getRolePrncpls().isEmpty()) {
 1346  0
                                                 if (!role.getDefinitions().isEmpty()) {
 1347  0
                                                         RoleMemberBo roleMemberImpl = new RoleMemberBo();
 1348  0
                                                         roleMemberImpl.setRoleId(role.getRoleId());
 1349  0
                                                         roleMemberImpl.setMemberId(identityManagementPersonDocument.getPrincipalId());
 1350  0
                                                         roleMemberImpl.setMemberType(MemberType.PRINCIPAL);
 1351  0
                                                         roleMembers.add(roleMemberImpl);
 1352  0
                                                 }
 1353  
                                         } else {
 1354  0
                                                 for (KimDocumentRoleMember roleMember : role.getRolePrncpls()) {
 1355  0
                                                         RoleMemberBo roleMemberImpl = new RoleMemberBo();
 1356  0
                                                         roleMemberImpl.setRoleId(role.getRoleId());
 1357  
                                                         // TODO : principalId is not ready here yet ?
 1358  0
                                                         roleMemberImpl.setMemberId(identityManagementPersonDocument.getPrincipalId());
 1359  0
                                                         roleMemberImpl.setMemberType(MemberType.PRINCIPAL);
 1360  0
                                                         roleMemberImpl.setRoleMemberId(roleMember.getRoleMemberId());
 1361  0
                                                         if (roleMember.getActiveFromDate() != null) {
 1362  0
                                                                 roleMemberImpl.setActiveFromDateValue(
 1363  
                                         new java.sql.Timestamp(roleMember.getActiveFromDate().getTime()));
 1364  
                                                         }
 1365  0
                                                         if (roleMember.getActiveToDate() != null) {
 1366  0
                                                                 roleMemberImpl.setActiveToDateValue(
 1367  
                                         new java.sql.Timestamp(roleMember.getActiveToDate().getTime()));
 1368  
                                                         }
 1369  0
                                                         List<RoleMemberAttributeDataBo> origAttributes = new ArrayList<RoleMemberAttributeDataBo>();
 1370  0
                                                         if(ObjectUtils.isNotNull(origRoleMembers)){
 1371  0
                                                                 for (RoleMemberBo origMember : origRoleMembers) {
 1372  0
                                                                         if (origMember.getRoleMemberId()!=null && StringUtils.equals(origMember.getRoleMemberId(), roleMember.getRoleMemberId())) {
 1373  0
                                                                                 origAttributes = origMember.getAttributeDetails();
 1374  0
                                                                                 roleMemberImpl.setVersionNumber(origMember.getVersionNumber());
 1375  
                                                                         }
 1376  
                                                                 }
 1377  
                                                         }
 1378  0
                                                         List<RoleMemberAttributeDataBo> attributes = new ArrayList<RoleMemberAttributeDataBo>();
 1379  0
                                                         if(CollectionUtils.isNotEmpty(roleMember.getQualifiers())){
 1380  0
                                                                 for (KimDocumentRoleQualifier qualifier : roleMember.getQualifiers()) {
 1381  
                                                                         //if (StringUtils.isNotBlank(qualifier.getAttrVal())) {
 1382  0
                                                                                 RoleMemberAttributeDataBo attribute = new RoleMemberAttributeDataBo();
 1383  0
                                                                                 attribute.setId(qualifier.getAttrDataId());
 1384  0
                                                                                 attribute.setAttributeValue(qualifier.getAttrVal());
 1385  0
                                                                                 attribute.setKimAttributeId(qualifier.getKimAttrDefnId());
 1386  0
                                                                                 attribute.setAssignedToId(qualifier.getRoleMemberId());
 1387  0
                                                                                 attribute.setKimTypeId(qualifier.getKimTypId());
 1388  
 
 1389  0
                                                                                 updateAttrValIfNecessary(attribute);
 1390  
 
 1391  0
                                                                                 if(ObjectUtils.isNotNull(origAttributes)){
 1392  0
                                                                                         for (RoleMemberAttributeDataBo origAttribute : origAttributes) {
 1393  0
                                                                                                 if (origAttribute.getId()!=null && StringUtils.equals(origAttribute.getId(), qualifier.getAttrDataId())) {
 1394  0
                                                                                                         attribute.setVersionNumber(origAttribute.getVersionNumber());
 1395  
                                                                                                 }
 1396  
                                                                                         }
 1397  
                                                                                 }
 1398  0
                                                                                 if (attribute.getVersionNumber() != null || StringUtils.isNotBlank(qualifier.getAttrVal())) {
 1399  0
                                                                                         attributes.add(attribute);
 1400  
                                                                                 }
 1401  
                                                                         //}
 1402  0
                                                                 }
 1403  
                                                         }
 1404  0
                                                         roleMemberImpl.setAttributeDetails(attributes);
 1405  0
                                                         roleMembers.add(roleMemberImpl);
 1406  0
                                                 }
 1407  
                                         }
 1408  
                                 //}
 1409  0
                         }
 1410  
                 }
 1411  0
                 return roleMembers;
 1412  
         }
 1413  
 
 1414  
         protected List<DelegateTypeBo> populateDelegations(IdentityManagementPersonDocument identityManagementPersonDocument){
 1415  0
                 List<DelegateTypeBo> origDelegations = getPersonDelegations(identityManagementPersonDocument.getPrincipalId());
 1416  0
                 List<DelegateTypeBo> kimDelegations = new ArrayList<DelegateTypeBo>();
 1417  
                 DelegateTypeBo newKimDelegation;
 1418  0
                 DelegateTypeBo origDelegationImplTemp = null;
 1419  
                 List<DelegateMemberBo> origMembers;
 1420  0
                 boolean activatingInactive = false;
 1421  0
                 String newDelegationIdAssigned = "";
 1422  0
                 if(CollectionUtils.isNotEmpty(identityManagementPersonDocument.getDelegations())){
 1423  0
                         for(RoleDocumentDelegation roleDocumentDelegation: identityManagementPersonDocument.getDelegations()){
 1424  0
                                 newKimDelegation = new DelegateTypeBo();
 1425  0
                                 KimCommonUtilsInternal.copyProperties(newKimDelegation, roleDocumentDelegation);
 1426  0
                                 newKimDelegation.setRoleId(roleDocumentDelegation.getRoleId());
 1427  0
                                 if(ObjectUtils.isNotNull(origDelegations)){
 1428  0
                                         for(DelegateTypeBo origDelegationImpl: origDelegations){
 1429  0
                                                 if((origDelegationImpl.getRoleId()!=null && StringUtils.equals(origDelegationImpl.getRoleId(), newKimDelegation.getRoleId())) &&
 1430  
                                                                 (origDelegationImpl.getDelegationId()!=null && StringUtils.equals(origDelegationImpl.getDelegationId(), newKimDelegation.getDelegationId()))){
 1431  
                                                         //TODO: verify if you want to add  && newRoleMember.isActive() condition to if...
 1432  0
                                                         newDelegationIdAssigned = newKimDelegation.getDelegationId();
 1433  0
                                                         newKimDelegation.setDelegationId(origDelegationImpl.getDelegationId());
 1434  0
                                                         activatingInactive = true;
 1435  
                                                 }
 1436  0
                                                 if(origDelegationImpl.getDelegationId()!=null && StringUtils.equals(origDelegationImpl.getDelegationId(), newKimDelegation.getDelegationId())){
 1437  0
                                                         newKimDelegation.setVersionNumber(origDelegationImpl.getVersionNumber());
 1438  0
                                                         origDelegationImplTemp = origDelegationImpl;
 1439  
                                                 }
 1440  
                                         }
 1441  
                                 }
 1442  0
                                 origMembers = (origDelegationImplTemp==null || origDelegationImplTemp.getMembers()==null)?
 1443  
                                                                         new ArrayList<DelegateMemberBo>():origDelegationImplTemp.getMembers();
 1444  0
                                 newKimDelegation.setMembers(getDelegationMembers(roleDocumentDelegation.getMembers(), origMembers, activatingInactive, newDelegationIdAssigned));
 1445  0
                                 kimDelegations.add(newKimDelegation);
 1446  0
                                 activatingInactive = false;
 1447  
                         }
 1448  
                 }
 1449  0
                 return kimDelegations;
 1450  
         }
 1451  
 
 1452  
     protected List <RoleMemberAttributeDataBo> getBlankRoleMemberAttrs(List <RoleMemberBo> rolePrncpls) {
 1453  
 
 1454  0
                 List <RoleMemberAttributeDataBo>  blankRoleMemberAttrs = new ArrayList<RoleMemberAttributeDataBo>();
 1455  0
                 if(ObjectUtils.isNotNull(rolePrncpls)){
 1456  0
                         for (RoleMemberBo roleMbr : rolePrncpls) {
 1457  0
                                 List <RoleMemberAttributeDataBo>  roleMemberAttrs = new ArrayList<RoleMemberAttributeDataBo>();
 1458  0
                                 if (CollectionUtils.isNotEmpty(roleMbr.getAttributeDetails())) {
 1459  0
                                         for (RoleMemberAttributeDataBo attr : roleMbr.getAttributeDetails()) {
 1460  0
                                                 if (StringUtils.isBlank(attr.getAttributeValue())) {
 1461  0
                                                         roleMemberAttrs.add(attr);
 1462  
                                                 }
 1463  
                                         }
 1464  0
                                         if (!roleMemberAttrs.isEmpty()) {
 1465  0
                                                 roleMbr.getAttributeDetails().removeAll(roleMemberAttrs);
 1466  0
                                                 blankRoleMemberAttrs.addAll(roleMemberAttrs);
 1467  
                                         }
 1468  
 
 1469  
                                 }
 1470  0
                         }
 1471  
                 }
 1472  
 
 1473  0
                 return blankRoleMemberAttrs;
 1474  
 
 1475  
         }
 1476  
 
 1477  
     protected List <RoleResponsibilityActionBo> populateRoleRspActions(IdentityManagementPersonDocument identityManagementPersonDocument) {
 1478  
 //                List<RoleImpl> origRoles = getRolesForPrincipal(identityManagementPersonDocument.getPrincipalId());
 1479  
 
 1480  0
                 List <RoleResponsibilityActionBo>  roleRspActions = new ArrayList<RoleResponsibilityActionBo>();
 1481  0
                 if(CollectionUtils.isNotEmpty(identityManagementPersonDocument.getRoles())){
 1482  0
                         for (PersonDocumentRole role : identityManagementPersonDocument.getRoles()) {
 1483  0
                                 if(CollectionUtils.isNotEmpty(role.getRolePrncpls())){
 1484  0
                                         for (KimDocumentRoleMember roleMbr : role.getRolePrncpls()) {
 1485  0
                                                 if(CollectionUtils.isNotEmpty(roleMbr.getRoleRspActions())){
 1486  0
                                                         for (KimDocumentRoleResponsibilityAction roleRspAction : roleMbr.getRoleRspActions()) {
 1487  0
                                                                 RoleResponsibilityActionBo entRoleRspAction = new RoleResponsibilityActionBo();
 1488  0
                                                                 entRoleRspAction.setId(roleRspAction.getRoleResponsibilityActionId());
 1489  0
                                                                 entRoleRspAction.setActionPolicyCode(roleRspAction.getActionPolicyCode());
 1490  0
                                                                 entRoleRspAction.setActionTypeCode(roleRspAction.getActionTypeCode());
 1491  0
                                                                 entRoleRspAction.setPriorityNumber(roleRspAction.getPriorityNumber());
 1492  0
                                                                 entRoleRspAction.setRoleMemberId(roleRspAction.getRoleMemberId());
 1493  0
                                                                 entRoleRspAction.setRoleResponsibilityId(roleRspAction.getRoleResponsibilityId());
 1494  0
                                                                 List<RoleResponsibilityActionBo> actions = getRoleRspActions( roleMbr.getRoleMemberId());
 1495  0
                                                                 if(ObjectUtils.isNotNull(actions)){
 1496  0
                                                                         for(RoleResponsibilityActionBo orgRspAction : actions) {
 1497  0
                                                                                 if (orgRspAction.getId()!=null && StringUtils.equals(orgRspAction.getId(), roleRspAction.getRoleResponsibilityActionId())) {
 1498  0
                                                                                         entRoleRspAction.setVersionNumber(orgRspAction.getVersionNumber());
 1499  
                                                                                 }
 1500  
                                                                         }
 1501  
                                                                 }
 1502  0
                                                                 roleRspActions.add(entRoleRspAction);
 1503  0
                                                         }
 1504  
                                                 }
 1505  
                                         }
 1506  
                                 }
 1507  
                         }
 1508  
                 }
 1509  0
                 return roleRspActions;
 1510  
 
 1511  
         }
 1512  
 
 1513  
         protected BusinessObjectService getBusinessObjectService() {
 1514  0
                 if ( businessObjectService == null ) {
 1515  0
                         businessObjectService = KRADServiceLocator.getBusinessObjectService();
 1516  
                 }
 1517  0
                 return businessObjectService;
 1518  
         }
 1519  
 
 1520  
         protected IdentityService getIdentityService() {
 1521  0
                 if ( identityService == null ) {
 1522  0
                         identityService = KimApiServiceLocator.getIdentityService();
 1523  
                 }
 1524  0
                 return identityService;
 1525  
         }
 1526  
 
 1527  
         protected GroupService getGroupService() {
 1528  0
                 if ( groupService == null ) {
 1529  0
                         groupService = KimApiServiceLocator.getGroupService();
 1530  
                 }
 1531  0
                 return groupService;
 1532  
         }
 1533  
 
 1534  
         protected DocumentHelperService getDocumentHelperService() {
 1535  0
             if ( documentHelperService == null ) {
 1536  0
                 documentHelperService = KRADServiceLocatorWeb.getDocumentHelperService();
 1537  
                 }
 1538  0
             return this.documentHelperService;
 1539  
         }
 1540  
 
 1541  
         protected RoleService getRoleService() {
 1542  0
                    if(roleService == null){
 1543  0
                            roleService = KimApiServiceLocator.getRoleService();
 1544  
             }
 1545  0
                 return roleService;
 1546  
         }
 1547  
 
 1548  
         public void setRoleService(RoleService roleService) {
 1549  0
                 this.roleService = roleService;
 1550  0
         }
 1551  
 
 1552  
         protected ResponsibilityService getResponsibilityService() {
 1553  0
                    if ( responsibilityService == null ) {
 1554  0
                     responsibilityService = KimApiServiceLocator.getResponsibilityService();
 1555  
             }
 1556  0
                 return responsibilityService;
 1557  
         }
 1558  
 
 1559  
         public void setResponsibilityService(ResponsibilityService responsibilityService) {
 1560  0
                 this.responsibilityService = responsibilityService;
 1561  0
         }
 1562  
 
 1563  
 
 1564  
         /* Role document methods */
 1565  
         @SuppressWarnings("unchecked")
 1566  
         public void loadRoleDoc(IdentityManagementRoleDocument identityManagementRoleDocument, Role role){
 1567  0
                 Map<String, String> criteria = new HashMap<String, String>();
 1568  0
                 criteria.put(KimConstants.PrimaryKeyConstants.ROLE_ID, role.getId());
 1569  0
                 RoleBo roleBo = getBusinessObjectService().findByPrimaryKey(RoleBo.class, criteria);
 1570  
 
 1571  0
         Map<String, String> subClassCriteria = new HashMap<String, String>();
 1572  0
                 subClassCriteria.put(KimConstants.PrimaryKeyConstants.SUB_ROLE_ID, role.getId());
 1573  
 
 1574  0
                 identityManagementRoleDocument.setRoleId(roleBo.getId());
 1575  0
                 identityManagementRoleDocument.setKimType(KimTypeBo.to(roleBo.getKimRoleType()));
 1576  0
                 identityManagementRoleDocument.setRoleTypeName(roleBo.getKimRoleType().getName());
 1577  0
                 identityManagementRoleDocument.setRoleTypeId(roleBo.getKimTypeId());
 1578  0
                 identityManagementRoleDocument.setRoleName(roleBo.getName());
 1579  0
                 identityManagementRoleDocument.setRoleDescription(roleBo.getDescription());
 1580  0
                 identityManagementRoleDocument.setActive(roleBo.isActive());
 1581  0
                 identityManagementRoleDocument.setRoleNamespace(roleBo.getNamespaceCode());
 1582  0
                 identityManagementRoleDocument.setEditing(true);
 1583  
 
 1584  0
                 identityManagementRoleDocument.setPermissions(loadPermissions((List<RolePermissionBo>)getBusinessObjectService().findMatching(RolePermissionBo.class, subClassCriteria)));
 1585  0
                 identityManagementRoleDocument.setResponsibilities(loadResponsibilities((List<RoleResponsibilityBo>)getBusinessObjectService().findMatching(RoleResponsibilityBo.class, subClassCriteria)));
 1586  0
                 loadResponsibilityRoleRspActions(identityManagementRoleDocument);
 1587  0
                 identityManagementRoleDocument.setMembers(loadRoleMembers(identityManagementRoleDocument, roleBo.getMembers()));
 1588  0
                 loadMemberRoleRspActions(identityManagementRoleDocument);
 1589  0
                 identityManagementRoleDocument.setDelegations(loadRoleDocumentDelegations(identityManagementRoleDocument, getRoleDelegations(roleBo.getId())));
 1590  
                 //Since delegation members are flattened out on the UI...
 1591  0
                 setDelegationMembersInDocument(identityManagementRoleDocument);
 1592  0
                 identityManagementRoleDocument.setKimType(KimTypeBo.to(roleBo.getKimRoleType()));
 1593  0
         }
 1594  
 
 1595  
         public void setDelegationMembersInDocument(IdentityManagementRoleDocument identityManagementRoleDocument){
 1596  0
                 if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getDelegations())){
 1597  0
                         for(RoleDocumentDelegation delegation: identityManagementRoleDocument.getDelegations()){
 1598  0
                                 if(CollectionUtils.isNotEmpty(delegation.getMembers())){
 1599  0
                                         for(RoleDocumentDelegationMember member: delegation.getMembers()){
 1600  0
                                                 member.setDelegationTypeCode(delegation.getDelegationTypeCode());
 1601  0
                                                 identityManagementRoleDocument.getDelegationMembers().add(member);
 1602  
                                         }
 1603  
                                 }
 1604  
                         }
 1605  
                 }
 1606  0
         }
 1607  
 
 1608  
         protected List<KimDocumentRoleResponsibility> loadResponsibilities(List<RoleResponsibilityBo> roleResponsibilities){
 1609  0
                 List<KimDocumentRoleResponsibility> documentRoleResponsibilities = new ArrayList<KimDocumentRoleResponsibility>();
 1610  0
                 if(ObjectUtils.isNotNull(roleResponsibilities)){
 1611  0
                         for(RoleResponsibilityBo roleResponsibility: roleResponsibilities){
 1612  0
                                 if(roleResponsibility.isActive()) {
 1613  0
                                         KimDocumentRoleResponsibility roleResponsibilityCopy = new KimDocumentRoleResponsibility();
 1614  0
                                         KimCommonUtilsInternal.copyProperties(roleResponsibilityCopy, roleResponsibility);
 1615  0
                                         roleResponsibilityCopy.setEdit(true);
 1616  0
                                         documentRoleResponsibilities.add(roleResponsibilityCopy);
 1617  0
                                 }
 1618  
                         }
 1619  
                 }
 1620  0
                 return documentRoleResponsibilities;
 1621  
         }
 1622  
 
 1623  
         protected List<KimDocumentRolePermission> loadPermissions(List<RolePermissionBo> rolePermissions){
 1624  0
                 List<KimDocumentRolePermission> documentRolePermissions = new ArrayList<KimDocumentRolePermission>();
 1625  
                 KimDocumentRolePermission rolePermissionCopy;
 1626  0
                 if(ObjectUtils.isNotNull(rolePermissions)){
 1627  0
                         for(RolePermissionBo rolePermission: rolePermissions){
 1628  0
                                 if ( rolePermission.isActive() ) {
 1629  0
                                         rolePermissionCopy = new KimDocumentRolePermission();
 1630  0
                                         rolePermissionCopy.setRolePermissionId(rolePermission.getId());
 1631  0
                                         rolePermissionCopy.setRoleId(rolePermission.getRoleId());
 1632  0
                                         rolePermissionCopy.setPermissionId(rolePermission.getPermissionId());
 1633  0
                                         rolePermissionCopy.setPermission(PermissionBo.to(rolePermission.getPermission()));
 1634  0
                                         rolePermissionCopy.setEdit(true);
 1635  0
                                         documentRolePermissions.add(rolePermissionCopy);
 1636  
                                 }
 1637  
                         }
 1638  
                 }
 1639  0
                 return documentRolePermissions;
 1640  
         }
 1641  
 
 1642  
         protected List<KimDocumentRoleMember> loadRoleMembers(
 1643  
                         IdentityManagementRoleDocument identityManagementRoleDocument, List<RoleMemberBo> members){
 1644  0
                 List<KimDocumentRoleMember> pndMembers = new ArrayList<KimDocumentRoleMember>();
 1645  
                 KimDocumentRoleMember pndMember;
 1646  0
                 if(ObjectUtils.isNotNull(members)){
 1647  0
                         for(RoleMemberBo member: members){
 1648  0
                                 pndMember = new KimDocumentRoleMember();
 1649  0
                                 pndMember.setActiveFromDate(member.getActiveFromDateValue());
 1650  0
                                 pndMember.setActiveToDate(member.getActiveToDateValue());
 1651  0
                                 pndMember.setActive(member.isActive(new Timestamp(System.currentTimeMillis())));
 1652  0
                                 if(pndMember.isActive()){
 1653  0
                                         pndMember.setRoleMemberId(member.getRoleMemberId());
 1654  0
                                         pndMember.setRoleId(member.getRoleId());
 1655  0
                                         pndMember.setMemberId(member.getMemberId());
 1656  0
                                         pndMember.setMemberNamespaceCode(getMemberNamespaceCode(member.getMemberType(), member.getMemberId()));
 1657  0
                                         pndMember.setMemberName(getMemberName(member.getMemberType(), member.getMemberId()));
 1658  0
                                         pndMember.setMemberFullName(getMemberFullName(member.getMemberType(), member.getMemberId()));
 1659  0
                                         pndMember.setMemberTypeCode(member.getMemberType().getCode());
 1660  0
                                         pndMember.setQualifiers(loadRoleMemberQualifiers(identityManagementRoleDocument, member.getAttributeDetails()));
 1661  0
                                         pndMember.setEdit(true);
 1662  0
                                         pndMembers.add(pndMember);
 1663  
                                 }
 1664  
                         }
 1665  
                 }
 1666  0
             Collections.sort(pndMembers, identityManagementRoleDocument.getMemberMetaDataType());
 1667  0
                 return pndMembers;
 1668  
         }
 1669  
 
 1670  
         protected void loadResponsibilityRoleRspActions(IdentityManagementRoleDocument identityManagementRoleDocument){
 1671  0
                 if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getResponsibilities())){
 1672  0
                         for(KimDocumentRoleResponsibility responsibility: identityManagementRoleDocument.getResponsibilities()){
 1673  0
                                 responsibility.getRoleRspActions().addAll(loadKimDocumentRoleRespActions(
 1674  
                                                 getRoleResponsibilityActionImpls(responsibility.getRoleResponsibilityId())));
 1675  
                         }
 1676  
                 }
 1677  0
         }
 1678  
 
 1679  
         @SuppressWarnings("unchecked")
 1680  
         protected List<RoleResponsibilityActionBo> getRoleResponsibilityActionImpls(String roleResponsibilityId){
 1681  0
                 Map<String, String> criteria = new HashMap<String, String>();
 1682  0
                 criteria.put(KimConstants.PrimaryKeyConstants.ROLE_MEMBER_ID, "*");
 1683  0
                 criteria.put(KimConstants.PrimaryKeyConstants.ROLE_RESPONSIBILITY_ID, roleResponsibilityId);
 1684  0
                 return (List<RoleResponsibilityActionBo>)
 1685  
                         getBusinessObjectService().findMatching(RoleResponsibilityActionBo.class, criteria);
 1686  
         }
 1687  
 
 1688  
         @SuppressWarnings("unchecked")
 1689  
         public List<RoleResponsibilityActionBo> getRoleMemberResponsibilityActionImpls(String roleMemberId){
 1690  0
                 Map<String, String> criteria = new HashMap<String, String>(1);
 1691  0
                 criteria.put(KimConstants.PrimaryKeyConstants.ROLE_MEMBER_ID, roleMemberId);
 1692  0
                 return (List<RoleResponsibilityActionBo>)
 1693  
                         getBusinessObjectService().findMatching(RoleResponsibilityActionBo.class, criteria);
 1694  
         }
 1695  
 
 1696  
         protected void loadMemberRoleRspActions(IdentityManagementRoleDocument identityManagementRoleDocument){
 1697  0
                 if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getMembers())){
 1698  0
                         for(KimDocumentRoleMember member: identityManagementRoleDocument.getMembers()){
 1699  0
                                 member.getRoleRspActions().addAll(loadKimDocumentRoleRespActions(
 1700  
                                                 getRoleMemberResponsibilityActionImpls(member.getRoleMemberId()) ) );
 1701  
                         }
 1702  
                 }
 1703  0
         }
 1704  
 
 1705  
         protected List<KimDocumentRoleResponsibilityAction> loadKimDocumentRoleRespActions(
 1706  
                         List<RoleResponsibilityActionBo> roleRespActionImpls){
 1707  0
                 List<KimDocumentRoleResponsibilityAction> documentRoleRespActions = new ArrayList<KimDocumentRoleResponsibilityAction>();
 1708  
                 KimDocumentRoleResponsibilityAction documentRoleRespAction;
 1709  0
                 if(ObjectUtils.isNotNull(roleRespActionImpls)){
 1710  0
                         for(RoleResponsibilityActionBo roleRespActionImpl: roleRespActionImpls){
 1711  0
                                 documentRoleRespAction = new KimDocumentRoleResponsibilityAction();
 1712  0
                                 KimCommonUtilsInternal.copyProperties(documentRoleRespAction, roleRespActionImpl);
 1713  
                                 // handle the roleResponsibility object being null since not all may be defined when ID value is "*"
 1714  0
                                 if ( ObjectUtils.isNotNull(roleRespActionImpl.getRoleResponsibility()) ) {
 1715  0
                                         documentRoleRespAction.setKimResponsibility(roleRespActionImpl.getRoleResponsibility().getKimResponsibility());
 1716  
                                 }
 1717  0
                                 documentRoleRespActions.add(documentRoleRespAction);
 1718  
                         }
 1719  
                 }
 1720  0
                 return documentRoleRespActions;
 1721  
         }
 1722  
 
 1723  
     public BusinessObject getMember(MemberType memberType, String memberId){
 1724  0
         Class<? extends BusinessObject> roleMemberTypeClass = null;
 1725  0
         String roleMemberIdName = "";
 1726  0
             if(MemberType.PRINCIPAL.equals(memberType)) {
 1727  0
                 roleMemberTypeClass = PrincipalBo.class;
 1728  0
                 roleMemberIdName = KimConstants.PrimaryKeyConstants.PRINCIPAL_ID;
 1729  0
                           Principal principalInfo = getIdentityService().getPrincipal(memberId);
 1730  0
                           if (principalInfo != null) {
 1731  
                                   
 1732  
                           }
 1733  0
         } else if(MemberType.GROUP.equals(memberType)){
 1734  0
                 roleMemberTypeClass = GroupBo.class;
 1735  0
                 roleMemberIdName = KimConstants.PrimaryKeyConstants.GROUP_ID;
 1736  0
                 Group groupInfo = null;
 1737  0
                           groupInfo = getGroupService().getGroup(memberId);
 1738  0
                           if (groupInfo != null) {
 1739  
                                   
 1740  
                           }
 1741  0
         } else if(MemberType.ROLE.equals(memberType)){
 1742  0
                 roleMemberTypeClass = RoleBo.class;
 1743  0
                 roleMemberIdName = KimConstants.PrimaryKeyConstants.ROLE_ID;
 1744  0
                           Role role = getRoleService().getRole(memberId);
 1745  0
                           if (role != null) {
 1746  
                                   
 1747  
                           }
 1748  
         }
 1749  0
         Map<String, String> criteria = new HashMap<String, String>();
 1750  0
         criteria.put(roleMemberIdName, memberId);
 1751  0
         return getBusinessObjectService().findByPrimaryKey(roleMemberTypeClass, criteria);
 1752  
     }
 1753  
 
 1754  
         public String getMemberName(MemberType memberType, String memberId){
 1755  0
                 if (memberType == null || StringUtils.isEmpty(memberId)) { return "";}
 1756  0
                 BusinessObject member = getMember(memberType, memberId);
 1757  0
                 if (member == null) { //not a REAL principal, try to fake the name
 1758  0
                         String fakeName = "";
 1759  0
                         Principal kp = KimApiServiceLocator.getIdentityService().getPrincipal(memberId);
 1760  0
                         if(kp != null && kp.getPrincipalName() != null && !"".equals(kp.getPrincipalName())){
 1761  0
                                 fakeName = kp.getPrincipalName();
 1762  
                         }
 1763  
 
 1764  0
                         return fakeName;
 1765  
                 }
 1766  0
                 return getMemberName(memberType, member);
 1767  
         }
 1768  
 
 1769  
         public String getMemberFullName(MemberType memberType, String memberId){
 1770  0
                 if(memberType == null || StringUtils.isEmpty(memberId)) {return "";}
 1771  0
                    String memberFullName = "";
 1772  0
         if(MemberType.PRINCIPAL.equals(memberType)){
 1773  0
                 Principal principalInfo = null;
 1774  0
                 principalInfo = getIdentityService().getPrincipal(memberId);
 1775  0
                 if (principalInfo != null) {
 1776  0
                         String principalName = principalInfo.getPrincipalName();
 1777  0
                         Person psn = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName);
 1778  0
                         memberFullName = psn.getFirstName() + " " + psn.getLastName();
 1779  
                 }                                
 1780  0
         } else if(MemberType.GROUP.equals(memberType)){
 1781  0
                 Group group = null;
 1782  0
                 group = getGroupService().getGroup(memberId);
 1783  0
                 if (group != null) {
 1784  0
                         memberFullName = group.getName();
 1785  
                 }
 1786  
                 
 1787  0
         } else if(MemberType.ROLE.equals(memberType)){
 1788  0
                 Role role = getRoleService().getRole(memberId);
 1789  0
                 memberFullName = role.getName();
 1790  
         }
 1791  0
         return memberFullName;
 1792  
         }
 1793  
 
 1794  
         public String getMemberNamespaceCode(MemberType memberType, String memberId){
 1795  0
                 if(memberType == null || StringUtils.isEmpty(memberId)) {return "";}
 1796  0
             String roleMemberNamespaceCode = "";
 1797  0
         if(MemberType.PRINCIPAL.equals(memberType)){
 1798  0
                 roleMemberNamespaceCode = "";
 1799  0
         } else if(MemberType.GROUP.equals(memberType)){
 1800  0
                 Group groupInfo = getGroupService().getGroup(memberId);
 1801  0
                 if (groupInfo!= null) {
 1802  0
                         roleMemberNamespaceCode = groupInfo.getNamespaceCode();
 1803  
                 }
 1804  0
         } else if(MemberType.ROLE.equals(memberType)){
 1805  0
                 Role role = getRoleService().getRole(memberId);
 1806  0
                 if (role != null) {
 1807  0
                         roleMemberNamespaceCode = role.getNamespaceCode();
 1808  
                 }                
 1809  
         }
 1810  0
         return roleMemberNamespaceCode;
 1811  
         }
 1812  
 
 1813  
     public String getMemberIdByName(MemberType memberType, String memberNamespaceCode, String memberName){
 1814  0
             String memberId = "";
 1815  0
         if(MemberType.PRINCIPAL.equals(memberType)){
 1816  0
             Principal principal = getIdentityService().getPrincipalByPrincipalName(memberName);
 1817  0
             if(principal!=null) {
 1818  0
                 memberId = principal.getPrincipalId();
 1819  
             }
 1820  
 
 1821  0
        } else if(MemberType.GROUP.equals(memberType)){
 1822  0
                 Group groupInfo = getGroupService().getGroupByNameAndNamespaceCode(memberNamespaceCode, memberName);
 1823  0
                 if (groupInfo!=null) {
 1824  0
                 memberId = groupInfo.getId();
 1825  
             }
 1826  
 
 1827  0
         } else if(MemberType.ROLE.equals(memberType)){
 1828  0
                 memberId = getRoleService().getRoleIdByNameAndNamespaceCode(memberNamespaceCode, memberName);
 1829  
         }
 1830  0
         return memberId;
 1831  
     }
 1832  
 
 1833  
     public String getMemberName(MemberType memberType, BusinessObject member){
 1834  0
             String roleMemberName = "";
 1835  0
         if(MemberType.PRINCIPAL.equals(memberType)){
 1836  0
                 roleMemberName = ((PrincipalBo)member).getPrincipalName();
 1837  0
         } else if(MemberType.GROUP.equals(memberType)){
 1838  0
                 roleMemberName = ((GroupBo)member).getName();
 1839  0
         } else if(MemberType.ROLE.equals(memberType)){
 1840  0
                 roleMemberName = ((RoleBo)member).getName();
 1841  
         }
 1842  0
         return roleMemberName;
 1843  
     }
 1844  
 
 1845  
     public String getMemberNamespaceCode(MemberType memberType, BusinessObject member){
 1846  0
             String roleMemberNamespaceCode = "";
 1847  0
         if(MemberType.PRINCIPAL.equals(memberType)){
 1848  0
                 roleMemberNamespaceCode = "";
 1849  0
         } else if(MemberType.GROUP.equals(memberType)){
 1850  0
                 roleMemberNamespaceCode = ((GroupBo)member).getNamespaceCode();
 1851  0
         } else if(MemberType.ROLE.equals(memberType)){
 1852  0
                 roleMemberNamespaceCode = ((RoleBo)member).getNamespaceCode();
 1853  
         }
 1854  0
         return roleMemberNamespaceCode;
 1855  
     }
 1856  
 
 1857  
     protected List<KimDocumentRoleQualifier> loadRoleMemberQualifiers(IdentityManagementRoleDocument identityManagementRoleDocument,
 1858  
                         List<RoleMemberAttributeDataBo> attributeDataList){
 1859  0
                 List<KimDocumentRoleQualifier> pndMemberRoleQualifiers = new ArrayList<KimDocumentRoleQualifier>();
 1860  
                 KimDocumentRoleQualifier pndMemberRoleQualifier;
 1861  
 
 1862  
                 // add all attributes from attributeDataList
 1863  0
                 if(attributeDataList!=null){
 1864  0
                         for(RoleMemberAttributeDataBo memberRoleQualifier: attributeDataList){
 1865  0
                                 pndMemberRoleQualifier = new KimDocumentRoleQualifier();
 1866  0
                                 pndMemberRoleQualifier.setAttrDataId(memberRoleQualifier.getId());
 1867  0
                                 pndMemberRoleQualifier.setAttrVal(memberRoleQualifier.getAttributeValue());
 1868  0
                                 pndMemberRoleQualifier.setRoleMemberId(memberRoleQualifier.getAssignedToId());
 1869  0
                                 pndMemberRoleQualifier.setKimTypId(memberRoleQualifier.getKimTypeId());
 1870  0
                                 pndMemberRoleQualifier.setKimAttrDefnId(memberRoleQualifier.getKimAttributeId());
 1871  0
                                 pndMemberRoleQualifier.setKimAttribute(memberRoleQualifier.getKimAttribute());
 1872  0
                                 formatAttrValIfNecessary(pndMemberRoleQualifier);
 1873  0
                                 pndMemberRoleQualifiers.add(pndMemberRoleQualifier);
 1874  
                         }
 1875  
                 }
 1876  
                 // also add any attributes already in the document that are not in the attributeDataList
 1877  0
                 int countOfOriginalAttributesNotPresent = 0;
 1878  0
                 List<KimDocumentRoleQualifier> fillerRoleQualifiers = new ArrayList<KimDocumentRoleQualifier>();
 1879  
 
 1880  0
                 List<KimAttributeField> origAttributes = identityManagementRoleDocument.getDefinitions();
 1881  0
                 if ( origAttributes != null ) {
 1882  0
                         for(KimAttributeField key: origAttributes) {
 1883  0
                                 boolean attributePresent = false;
 1884  0
                                 String origAttributeId = identityManagementRoleDocument.getKimAttributeDefnId(key);
 1885  0
                                 if(attributeDataList!=null){
 1886  0
                                         for(RoleMemberAttributeDataBo memberRoleQualifier: attributeDataList){
 1887  0
                                                 if(origAttributeId!=null && StringUtils.equals(origAttributeId, memberRoleQualifier.getKimAttribute().getId())){
 1888  0
                                                         attributePresent = true;
 1889  0
                                                         break;
 1890  
                                                 }
 1891  
                                         }
 1892  
                                 }
 1893  0
                                 if(!attributePresent){
 1894  0
                                         countOfOriginalAttributesNotPresent++;
 1895  0
                                         pndMemberRoleQualifier = new KimDocumentRoleQualifier();
 1896  0
                                         pndMemberRoleQualifier.setKimAttrDefnId(origAttributeId);
 1897  0
                                         pndMemberRoleQualifier.refreshReferenceObject("kimAttribute");
 1898  0
                                         fillerRoleQualifiers.add(pndMemberRoleQualifier);
 1899  
                                 }
 1900  0
                         }
 1901  
 
 1902  0
                         if(countOfOriginalAttributesNotPresent != origAttributes.size()) {
 1903  0
                                 pndMemberRoleQualifiers.addAll(fillerRoleQualifiers);
 1904  
                         }
 1905  
                 }
 1906  0
                 return pndMemberRoleQualifiers;
 1907  
         }
 1908  
 
 1909  
     @SuppressWarnings("unchecked")
 1910  
         public List<DelegateTypeBo> getRoleDelegations(String roleId){
 1911  0
                 if(roleId==null) {
 1912  0
                         return new ArrayList<DelegateTypeBo>();
 1913  
         }
 1914  0
                 Map<String,String> criteria = new HashMap<String,String>(1);
 1915  0
                 criteria.put("roleId", roleId);
 1916  0
                 return (List<DelegateTypeBo>)getBusinessObjectService().findMatching(DelegateTypeBo.class, criteria);
 1917  
         }
 1918  
 
 1919  
     protected List<RoleDocumentDelegation> loadRoleDocumentDelegations(IdentityManagementRoleDocument identityManagementRoleDocument, List<DelegateTypeBo> delegations){
 1920  0
                 List<RoleDocumentDelegation> delList = new ArrayList<RoleDocumentDelegation>();
 1921  
                 RoleDocumentDelegation documentDelegation;
 1922  0
                 if(ObjectUtils.isNotNull(delegations)){
 1923  0
                         for(DelegateTypeBo del: delegations){
 1924  0
                                 documentDelegation = new RoleDocumentDelegation();
 1925  0
                                 documentDelegation.setActive(del.isActive());
 1926  0
                                 if(documentDelegation.isActive()){
 1927  0
                                         documentDelegation.setDelegationId(del.getDelegationId());
 1928  0
                                         documentDelegation.setDelegationTypeCode(del.getDelegationTypeCode());
 1929  0
                                         documentDelegation.setKimTypeId(del.getKimTypeId());
 1930  0
                                         documentDelegation.setMembers(loadDelegationMembers(identityManagementRoleDocument, del.getMembers()));
 1931  0
                                         documentDelegation.setRoleId(del.getRoleId());
 1932  0
                                         documentDelegation.setEdit(true);
 1933  0
                                         delList.add(documentDelegation);
 1934  
                                 }
 1935  
                         }
 1936  
                 }
 1937  0
                 return delList;
 1938  
         }
 1939  
 
 1940  
     protected List<RoleDocumentDelegationMember> loadDelegationMembers(IdentityManagementRoleDocument identityManagementRoleDocument, List<DelegateMemberBo> members){
 1941  0
                 List<RoleDocumentDelegationMember> pndMembers = new ArrayList<RoleDocumentDelegationMember>();
 1942  
                 RoleDocumentDelegationMember pndMember;
 1943  
                 RoleMemberBo roleMember;
 1944  0
                 if(ObjectUtils.isNotNull(members)){
 1945  0
                         for(DelegateMemberBo member: members){
 1946  0
                                 pndMember = new RoleDocumentDelegationMember();
 1947  0
                                 pndMember.setActiveFromDate(member.getActiveFromDateValue());
 1948  0
                                 pndMember.setActiveToDate(member.getActiveToDateValue());
 1949  0
                                 pndMember.setActive(member.isActive(new Timestamp(System.currentTimeMillis())));
 1950  0
                                 if(pndMember.isActive()){
 1951  0
                                         KimCommonUtilsInternal.copyProperties(pndMember, member);
 1952  0
                                         pndMember.setRoleMemberId(member.getRoleMemberId());
 1953  0
                                         roleMember = getRoleMemberForRoleMemberId(member.getRoleMemberId());
 1954  0
                                         if(roleMember!=null){
 1955  0
                                                 pndMember.setRoleMemberName(getMemberName(roleMember.getMemberType(), roleMember.getMemberId()));
 1956  0
                                                 pndMember.setRoleMemberNamespaceCode(getMemberNamespaceCode(roleMember.getMemberType(), roleMember.getMemberId()));
 1957  
                                         }
 1958  0
                                         pndMember.setMemberNamespaceCode(getMemberNamespaceCode(member.getType(), member.getMemberId()));
 1959  0
                                         pndMember.setMemberName(getMemberName(member.getType(), member.getMemberId()));
 1960  0
                                         pndMember.setEdit(true);
 1961  0
                                         pndMember.setQualifiers(loadDelegationMemberQualifiers(identityManagementRoleDocument, member.getAttributes()));
 1962  0
                                         pndMembers.add(pndMember);
 1963  
                                 }
 1964  
                         }
 1965  
                 }
 1966  0
                 return pndMembers;
 1967  
         }
 1968  
 
 1969  
     protected RoleMemberBo getRoleMemberForRoleMemberId(String roleMemberId){
 1970  0
                 Map<String,String> criteria = new HashMap<String,String>( 2 );
 1971  0
                 criteria.put(KimConstants.PrimaryKeyConstants.ROLE_MEMBER_ID, roleMemberId);
 1972  0
                 return getBusinessObjectService().findByPrimaryKey(RoleMemberBo.class, criteria);
 1973  
     }
 1974  
 
 1975  
     protected List<RoleDocumentDelegationMemberQualifier> loadDelegationMemberQualifiers(IdentityManagementRoleDocument identityManagementRoleDocument,
 1976  
                         List<DelegateMemberAttributeDataBo> attributeDataList){
 1977  0
                 List<RoleDocumentDelegationMemberQualifier> pndMemberRoleQualifiers = new ArrayList<RoleDocumentDelegationMemberQualifier>();
 1978  
                 RoleDocumentDelegationMemberQualifier pndMemberRoleQualifier;
 1979  0
                 List<KimAttributeField> origAttributes = identityManagementRoleDocument.getDefinitions();
 1980  0
                 boolean attributePresent = false;
 1981  
                 String origAttributeId;
 1982  0
                 if(origAttributes!=null){
 1983  0
                         for(KimAttributeField key: origAttributes) {
 1984  0
                                 origAttributeId = identityManagementRoleDocument.getKimAttributeDefnId(key);
 1985  0
                                 if(attributeDataList!=null){
 1986  0
                                         for(DelegateMemberAttributeDataBo memberRoleQualifier: attributeDataList){
 1987  0
                                                 if(origAttributeId!=null && StringUtils.equals(origAttributeId, memberRoleQualifier.getKimAttribute().getId())){
 1988  0
                                                         pndMemberRoleQualifier = new RoleDocumentDelegationMemberQualifier();
 1989  0
                                                         pndMemberRoleQualifier.setAttrDataId(memberRoleQualifier.getId());
 1990  0
                                                         pndMemberRoleQualifier.setAttrVal(memberRoleQualifier.getAttributeValue());
 1991  0
                                                         pndMemberRoleQualifier.setDelegationMemberId(memberRoleQualifier.getAssignedToId());
 1992  0
                                                         pndMemberRoleQualifier.setKimTypId(memberRoleQualifier.getKimTypeId());
 1993  0
                                                         pndMemberRoleQualifier.setKimAttrDefnId(memberRoleQualifier.getKimAttributeId());
 1994  0
                                                         pndMemberRoleQualifier.setKimAttribute(memberRoleQualifier.getKimAttribute());
 1995  0
                                                         pndMemberRoleQualifiers.add(pndMemberRoleQualifier);
 1996  0
                                                         attributePresent = true;
 1997  
                                                 }
 1998  
                                         }
 1999  
                                 }
 2000  0
                                 if(!attributePresent){
 2001  0
                                         pndMemberRoleQualifier = new RoleDocumentDelegationMemberQualifier();
 2002  0
                                         pndMemberRoleQualifier.setKimAttrDefnId(origAttributeId);
 2003  0
                                         pndMemberRoleQualifier.refreshReferenceObject("kimAttribute");
 2004  0
                                         pndMemberRoleQualifiers.add(pndMemberRoleQualifier);
 2005  
                                 }
 2006  0
                                 attributePresent = false;
 2007  
                         }
 2008  
                 }
 2009  0
                 return pndMemberRoleQualifiers;
 2010  
         }
 2011  
 
 2012  
         /**
 2013  
          * @see org.kuali.rice.kim.service.UiDocumentService#saveEntityPerson(IdentityManagementPersonDocument)
 2014  
          */
 2015  
         @SuppressWarnings("unchecked")
 2016  
         public void saveRole(IdentityManagementRoleDocument identityManagementRoleDocument) {
 2017  0
                 RoleBo roleBo = new RoleBo();
 2018  0
                 Map<String, String> criteria = new HashMap<String, String>();
 2019  0
                 String roleId = identityManagementRoleDocument.getRoleId();
 2020  0
                 criteria.put(KimConstants.PrimaryKeyConstants.ID, roleId);
 2021  0
                 RoleBo origRole = getBusinessObjectService().findByPrimaryKey(RoleBo.class, criteria);
 2022  
 
 2023  0
                 List<RolePermissionBo> origRolePermissions = new ArrayList<RolePermissionBo>();
 2024  0
                 List<RoleResponsibilityBo> origRoleResponsibilities = new ArrayList<RoleResponsibilityBo>();
 2025  0
                 List<RoleMemberBo> origRoleMembers = new ArrayList<RoleMemberBo>();
 2026  0
                 List<DelegateTypeBo> origRoleDelegations = new ArrayList<DelegateTypeBo>();
 2027  
 
 2028  0
                 roleBo.setId(identityManagementRoleDocument.getRoleId());
 2029  0
                 roleBo.setKimTypeId(identityManagementRoleDocument.getRoleTypeId());
 2030  0
                 roleBo.setNamespaceCode(identityManagementRoleDocument.getRoleNamespace());
 2031  0
                 roleBo.setName(identityManagementRoleDocument.getRoleName());
 2032  0
                 roleBo.setDescription(identityManagementRoleDocument.getRoleDescription());
 2033  
 
 2034  0
                 if (origRole == null) {
 2035  0
                         origRole = new RoleBo();
 2036  0
                         roleBo.setActive(true);
 2037  
                 } else {
 2038  0
                         roleBo.setActive(identityManagementRoleDocument.isActive());
 2039  0
                         roleBo.setVersionNumber(origRole.getVersionNumber());
 2040  0
             Map<String, String> altCriteria = new HashMap<String, String>();
 2041  0
             altCriteria.put(KimConstants.PrimaryKeyConstants.SUB_ROLE_ID, roleId);
 2042  0
                         origRolePermissions = new ArrayList<RolePermissionBo>(getBusinessObjectService().findMatching(RolePermissionBo.class, altCriteria));
 2043  0
             origRoleResponsibilities = (List<RoleResponsibilityBo>)getBusinessObjectService().findMatching(RoleResponsibilityBo.class, altCriteria);
 2044  0
                         origRoleMembers = (List<RoleMemberBo>)getBusinessObjectService().findMatching(RoleMemberBo.class, altCriteria);
 2045  0
                         origRoleDelegations = (List<DelegateTypeBo>)getBusinessObjectService().findMatching(DelegateTypeBo.class, altCriteria);
 2046  
                 }
 2047  
 
 2048  0
                 if( getKimTypeInfoService().getKimType(identityManagementRoleDocument.getRoleTypeId()) == null ) {
 2049  0
                         LOG.error( "Kim type not found for:"+identityManagementRoleDocument.getRoleTypeId(), new Throwable() );
 2050  
                 }
 2051  
 
 2052  0
                 List<PersistableBusinessObject> bos = new ArrayList<PersistableBusinessObject>();
 2053  
 
 2054  
 
 2055  0
                 bos.addAll(getRolePermissions(identityManagementRoleDocument, origRolePermissions));
 2056  0
                 bos.addAll(getRoleResponsibilities(identityManagementRoleDocument, origRoleResponsibilities));
 2057  0
                 bos.addAll(getRoleResponsibilitiesActions(identityManagementRoleDocument));
 2058  0
                 String initiatorPrincipalId = getInitiatorPrincipalId(identityManagementRoleDocument);
 2059  0
                 if(canAssignToRole(identityManagementRoleDocument, initiatorPrincipalId)){
 2060  0
                         List<RoleMemberBo> newRoleMembersList = getRoleMembers(identityManagementRoleDocument, origRoleMembers);
 2061  0
             roleBo.setMembers(newRoleMembersList);
 2062  0
                         bos.addAll(getRoleMemberResponsibilityActions(newRoleMembersList));
 2063  
                         //bos.addAll(getRoleMemberResponsibilityActions(identityManagementRoleDocument));
 2064  0
                         bos.addAll(getRoleDelegations(identityManagementRoleDocument, origRoleDelegations));
 2065  
                 }
 2066  0
         bos.add(roleBo);
 2067  0
                 getBusinessObjectService().save(bos);
 2068  0
                 KimImplServiceLocator.getResponsibilityInternalService().updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(identityManagementRoleDocument, origRoleResponsibilities));
 2069  0
                 if(!roleBo.isActive()){
 2070  
                         // when a role is inactivated, inactivate the memberships of principals, groups, and roles in
 2071  
                         // that role, delegations, and delegation members, and that roles memberships in other roles
 2072  0
                         KimImplServiceLocator.getRoleInternalService().roleInactivated(identityManagementRoleDocument.getRoleId());
 2073  
                 }
 2074  0
         }
 2075  
 
 2076  
         protected List<RolePermissionBo> getRolePermissions(
 2077  
                         IdentityManagementRoleDocument identityManagementRoleDocument, List<RolePermissionBo> origRolePermissions){
 2078  0
                 List<RolePermissionBo> rolePermissions = new ArrayList<RolePermissionBo>();
 2079  0
                 if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getPermissions())){
 2080  0
                         for(KimDocumentRolePermission documentRolePermission: identityManagementRoleDocument.getPermissions()){
 2081  0
                                 RolePermissionBo newRolePermission = new RolePermissionBo();
 2082  0
                                 newRolePermission.setId(documentRolePermission.getRolePermissionId());
 2083  0
                                 newRolePermission.setRoleId(identityManagementRoleDocument.getRoleId());
 2084  0
                                 newRolePermission.setPermissionId(documentRolePermission.getPermissionId());
 2085  0
                                 newRolePermission.setActive( documentRolePermission.isActive() );
 2086  
 
 2087  0
                                 newRolePermission.setActive(documentRolePermission.isActive());
 2088  0
                                 if (ObjectUtils.isNotNull(origRolePermissions)) {
 2089  0
                     for (RolePermissionBo origPermissionImpl : origRolePermissions) {
 2090  0
                         if (!StringUtils.equals(origPermissionImpl.getRoleId(), newRolePermission.getRoleId())
 2091  
                                 && StringUtils.equals(origPermissionImpl.getPermissionId(), newRolePermission.getPermissionId())
 2092  
                                 && origPermissionImpl.isActive()
 2093  
                                 && newRolePermission.isActive()) {
 2094  0
                                                         newRolePermission.setId(origPermissionImpl.getId());
 2095  
                                                 }
 2096  0
                                                 if(origPermissionImpl.getId()!=null && StringUtils.equals(origPermissionImpl.getId(), newRolePermission.getId())){
 2097  0
                                                         newRolePermission.setVersionNumber(origPermissionImpl.getVersionNumber());
 2098  0
                             newRolePermission.setObjectId(origPermissionImpl.getObjectId());
 2099  
                                                 }
 2100  
                                         }
 2101  
                                 }
 2102  0
                                 rolePermissions.add(newRolePermission);
 2103  0
                         }
 2104  
                 }
 2105  0
                 return rolePermissions;
 2106  
         }
 2107  
 
 2108  
         protected List<RoleResponsibilityBo> getRoleResponsibilities(
 2109  
                         IdentityManagementRoleDocument identityManagementRoleDocument, List<RoleResponsibilityBo> origRoleResponsibilities){
 2110  0
                 List<RoleResponsibilityBo> roleResponsibilities = new ArrayList<RoleResponsibilityBo>();
 2111  
                 RoleResponsibilityBo newRoleResponsibility;
 2112  0
                 if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getResponsibilities())){
 2113  0
                         for(KimDocumentRoleResponsibility documentRoleResponsibility: identityManagementRoleDocument.getResponsibilities()){
 2114  0
                                 newRoleResponsibility = new RoleResponsibilityBo();
 2115  0
                                 KimCommonUtilsInternal.copyProperties(newRoleResponsibility, documentRoleResponsibility);
 2116  0
                                 newRoleResponsibility.setActive(documentRoleResponsibility.isActive());
 2117  0
                                 newRoleResponsibility.setRoleId(identityManagementRoleDocument.getRoleId());
 2118  0
                                 if(ObjectUtils.isNotNull(origRoleResponsibilities)){
 2119  0
                                         for(RoleResponsibilityBo origResponsibilityImpl: origRoleResponsibilities){
 2120  0
                                                 if(!StringUtils.equals(origResponsibilityImpl.getRoleId(), newRoleResponsibility.getRoleId()) &&
 2121  
                                                                 StringUtils.equals(origResponsibilityImpl.getResponsibilityId(), newRoleResponsibility.getResponsibilityId()) &&
 2122  
                                                                 !origResponsibilityImpl.isActive() && newRoleResponsibility.isActive()){
 2123  0
                                                         newRoleResponsibility.setRoleResponsibilityId(origResponsibilityImpl.getRoleResponsibilityId());
 2124  
                                                 }
 2125  0
                                                 if(origResponsibilityImpl.getRoleResponsibilityId()!=null && StringUtils.equals(origResponsibilityImpl.getRoleResponsibilityId(), newRoleResponsibility.getRoleResponsibilityId())) {
 2126  0
                             newRoleResponsibility.setVersionNumber(origResponsibilityImpl.getVersionNumber());
 2127  0
                             newRoleResponsibility.setObjectId(origResponsibilityImpl.getObjectId());
 2128  
                         }
 2129  
                                         }
 2130  
                                 }
 2131  0
                                 roleResponsibilities.add(newRoleResponsibility);
 2132  
                         }
 2133  
                 }
 2134  0
                 return roleResponsibilities;
 2135  
         }
 2136  
 
 2137  
 
 2138  
         protected List <RoleResponsibilityActionBo> getRoleResponsibilitiesActions(
 2139  
                         IdentityManagementRoleDocument identityManagementRoleDocument){
 2140  0
                 List <RoleResponsibilityActionBo>  roleRspActions = new ArrayList<RoleResponsibilityActionBo>();
 2141  0
                 if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getResponsibilities())){
 2142  
                 // loop over the responsibilities assigned to the role
 2143  0
                         for(KimDocumentRoleResponsibility roleResponsibility : identityManagementRoleDocument.getResponsibilities()){
 2144  
                                 // only process if the actions are not assigned at the role member level
 2145  0
                                 if(!getResponsibilityInternalService().areActionsAtAssignmentLevelById(roleResponsibility.getResponsibilityId())){
 2146  0
                                         List<KimDocumentRoleResponsibilityAction> documentRoleResponsibilityActions = roleResponsibility.getRoleRspActions();
 2147  0
                                         if( ObjectUtils.isNotNull(documentRoleResponsibilityActions)
 2148  
                                                         && !documentRoleResponsibilityActions.isEmpty()
 2149  
                                                         && StringUtils.isNotBlank(documentRoleResponsibilityActions.get(0).getRoleResponsibilityActionId() ) ) {
 2150  0
                                                 RoleResponsibilityActionBo roleRspAction = new RoleResponsibilityActionBo();
 2151  0
                                                 roleRspAction.setId(documentRoleResponsibilityActions.get(0).getRoleResponsibilityActionId());
 2152  0
                                                 roleRspAction.setActionPolicyCode(documentRoleResponsibilityActions.get(0).getActionPolicyCode());
 2153  0
                                                 roleRspAction.setActionTypeCode(documentRoleResponsibilityActions.get(0).getActionTypeCode());
 2154  0
                                                 roleRspAction.setPriorityNumber(documentRoleResponsibilityActions.get(0).getPriorityNumber());
 2155  0
                                                 roleRspAction.setForceAction(documentRoleResponsibilityActions.get(0).isForceAction());
 2156  0
                                                 roleRspAction.setRoleMemberId("*");
 2157  0
                                                 roleRspAction.setRoleResponsibilityId(documentRoleResponsibilityActions.get(0).getRoleResponsibilityId());
 2158  0
                                                 updateResponsibilityActionVersionNumber(roleRspAction, getRoleResponsibilityActionImpls(roleResponsibility.getRoleResponsibilityId()));
 2159  0
                                                 roleRspActions.add(roleRspAction);
 2160  
                                         }
 2161  0
                                 }
 2162  
                         }
 2163  
                 }
 2164  0
                 return roleRspActions;
 2165  
         }
 2166  
 
 2167  
         // FIXME: This should be pulling by the PK, not using another method which pulls multiple records and then finds
 2168  
         // the right one here!
 2169  
         protected void updateResponsibilityActionVersionNumber(RoleResponsibilityActionBo newRoleRspAction,
 2170  
                         List<RoleResponsibilityActionBo> origRoleRespActionImpls){
 2171  0
                 if(ObjectUtils.isNotNull(origRoleRespActionImpls)){
 2172  0
                         for(RoleResponsibilityActionBo origRoleResponsibilityActionImpl: origRoleRespActionImpls){
 2173  0
                                 if(origRoleResponsibilityActionImpl.getId()!=null && StringUtils.equals(origRoleResponsibilityActionImpl.getId(),
 2174  
                                                 newRoleRspAction.getId())) {
 2175  0
                                         newRoleRspAction.setVersionNumber(origRoleResponsibilityActionImpl.getVersionNumber());
 2176  0
                                         break;
 2177  
                                 }
 2178  
                         }
 2179  
                 }
 2180  0
         }
 2181  
 
 2182  
         protected List<RoleResponsibilityActionBo> getRoleMemberResponsibilityActions(List<RoleMemberBo> newRoleMembersList){
 2183  0
                 List<RoleResponsibilityActionBo> roleRspActions = new ArrayList<RoleResponsibilityActionBo>();
 2184  0
                 if(ObjectUtils.isNotNull(newRoleMembersList)){
 2185  0
                         for(RoleMemberBo roleMember: newRoleMembersList){
 2186  0
                                 roleRspActions.addAll(roleMember.getRoleRspActions());
 2187  
                         }
 2188  
                 }
 2189  0
                 return roleRspActions;
 2190  
         }
 2191  
 
 2192  
         protected List<RoleResponsibilityActionBo> getRoleMemberResponsibilityActions(IdentityManagementRoleDocument identityManagementRoleDocument){
 2193  0
                 List<RoleResponsibilityActionBo> roleRspActions = new ArrayList<RoleResponsibilityActionBo>();
 2194  0
                 if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getMembers())){
 2195  0
                         for(KimDocumentRoleMember roleMember: identityManagementRoleDocument.getMembers()){
 2196  0
                                 for(KimDocumentRoleResponsibilityAction roleRspAction : roleMember.getRoleRspActions()){
 2197  0
                                         RoleResponsibilityActionBo entRoleRspAction = new RoleResponsibilityActionBo();
 2198  0
                                         entRoleRspAction.setId(roleRspAction.getRoleResponsibilityActionId());
 2199  0
                                         entRoleRspAction.setActionPolicyCode(roleRspAction.getActionPolicyCode());
 2200  0
                                         entRoleRspAction.setActionTypeCode(roleRspAction.getActionTypeCode());
 2201  0
                                         entRoleRspAction.setPriorityNumber(roleRspAction.getPriorityNumber());
 2202  0
                                         entRoleRspAction.setRoleMemberId(roleRspAction.getRoleMemberId());
 2203  0
                                         entRoleRspAction.setForceAction(roleRspAction.isForceAction());
 2204  0
                                         entRoleRspAction.setRoleResponsibilityId(roleRspAction.getRoleResponsibilityId());
 2205  0
                                         List<RoleResponsibilityActionBo> actions = getRoleRspActions(roleMember.getRoleMemberId());
 2206  0
                                         if(ObjectUtils.isNotNull(actions)){
 2207  0
                                                 for(RoleResponsibilityActionBo orgRspAction : actions) {
 2208  0
                                                         if (orgRspAction.getId()!=null && StringUtils.equals(orgRspAction.getId(), roleRspAction.getRoleResponsibilityActionId())) {
 2209  0
                                                                 entRoleRspAction.setVersionNumber(orgRspAction.getVersionNumber());
 2210  
                                                         }
 2211  
                                                 }
 2212  
                                         }
 2213  0
                                         roleRspActions.add(entRoleRspAction);
 2214  0
                                 }
 2215  
                         }
 2216  
                 }
 2217  0
                 return roleRspActions;
 2218  
         }
 2219  
 
 2220  
     protected List<RoleMemberBo> getRoleMembers(IdentityManagementRoleDocument identityManagementRoleDocument, List<RoleMemberBo> origRoleMembers){
 2221  0
         List<RoleMemberBo> roleMembers = new ArrayList<RoleMemberBo>();
 2222  
         RoleMemberBo newRoleMember;
 2223  
         RoleMemberBo origRoleMemberImplTemp;
 2224  
         List<RoleMemberAttributeDataBo> origAttributes;
 2225  0
         boolean activatingInactive = false;
 2226  0
         String newRoleMemberIdAssigned = "";
 2227  
 
 2228  0
         identityManagementRoleDocument.setKimType(KimApiServiceLocator.getKimTypeInfoService().getKimType(identityManagementRoleDocument.getRoleTypeId()));
 2229  0
         KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(identityManagementRoleDocument.getKimType());
 2230  
 
 2231  0
         if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getMembers())){
 2232  0
             for(KimDocumentRoleMember documentRoleMember: identityManagementRoleDocument.getMembers()){
 2233  0
                 origRoleMemberImplTemp = null;
 2234  
 
 2235  0
                 newRoleMember = new RoleMemberBo();
 2236  0
                 KimCommonUtilsInternal.copyProperties(newRoleMember, documentRoleMember);
 2237  0
                 newRoleMember.setRoleId(identityManagementRoleDocument.getRoleId());
 2238  0
                 if(ObjectUtils.isNotNull(origRoleMembers)){
 2239  0
                     for(RoleMemberBo origRoleMemberImpl: origRoleMembers){
 2240  0
                         if((origRoleMemberImpl.getRoleId()!=null && StringUtils.equals(origRoleMemberImpl.getRoleId(), newRoleMember.getRoleId())) &&
 2241  
                             (origRoleMemberImpl.getMemberId()!=null && StringUtils.equals(origRoleMemberImpl.getMemberId(), newRoleMember.getMemberId())) &&
 2242  
                             (origRoleMemberImpl.getMemberType()!=null && org.apache.commons.lang.ObjectUtils.equals(origRoleMemberImpl.getMemberType(), newRoleMember.getMemberType())) &&
 2243  
                             !origRoleMemberImpl.isActive(new Timestamp(System.currentTimeMillis())) &&
 2244  
                             !kimTypeService.validateAttributesAgainstExisting(identityManagementRoleDocument.getKimType().getId(),
 2245  
                                     documentRoleMember.getQualifierAsMap(), origRoleMemberImpl.getAttributes()).isEmpty()) {
 2246  
 
 2247  
                             //TODO: verify if you want to add  && newRoleMember.isActive() condition to if...
 2248  
 
 2249  0
                             newRoleMemberIdAssigned = newRoleMember.getRoleMemberId();
 2250  0
                             newRoleMember.setRoleMemberId(origRoleMemberImpl.getRoleMemberId());
 2251  0
                             activatingInactive = true;
 2252  
                         }
 2253  0
                         if(origRoleMemberImpl.getRoleMemberId()!=null && StringUtils.equals(origRoleMemberImpl.getRoleMemberId(), newRoleMember.getRoleMemberId())){
 2254  0
                             newRoleMember.setVersionNumber(origRoleMemberImpl.getVersionNumber());
 2255  0
                             origRoleMemberImplTemp = origRoleMemberImpl;
 2256  
                         }
 2257  
                     }
 2258  
                 }
 2259  0
                 origAttributes = (origRoleMemberImplTemp==null || origRoleMemberImplTemp.getAttributes()==null)?
 2260  
                                     new ArrayList<RoleMemberAttributeDataBo>():origRoleMemberImplTemp.getAttributeDetails();
 2261  0
                 newRoleMember.setActiveFromDateValue(documentRoleMember.getActiveFromDate());
 2262  0
                 newRoleMember.setActiveToDateValue(documentRoleMember.getActiveToDate());
 2263  0
                 newRoleMember.setAttributeDetails(getRoleMemberAttributeData(documentRoleMember.getQualifiers(), origAttributes, activatingInactive, newRoleMemberIdAssigned));
 2264  0
                 newRoleMember.setRoleRspActions(getRoleMemberResponsibilityActions(documentRoleMember, origRoleMemberImplTemp, activatingInactive, newRoleMemberIdAssigned));
 2265  0
                 roleMembers.add(newRoleMember);
 2266  0
                 activatingInactive = false;
 2267  
             }
 2268  
         }
 2269  0
         return roleMembers;
 2270  
     }
 2271  
 
 2272  
         protected List<RoleResponsibilityActionBo> getRoleMemberResponsibilityActions(
 2273  
                         KimDocumentRoleMember documentRoleMember, RoleMemberBo origRoleMemberImplTemp, boolean activatingInactive, String newRoleMemberIdAssigned){
 2274  0
                 List<RoleResponsibilityActionBo> roleRspActions = new ArrayList<RoleResponsibilityActionBo>();
 2275  0
                 List<RoleResponsibilityActionBo> origActions = new ArrayList<RoleResponsibilityActionBo>();
 2276  0
                 if(origRoleMemberImplTemp!=null) {
 2277  0
                         origActions = getRoleRspActions(origRoleMemberImplTemp.getRoleMemberId());
 2278  
                 }
 2279  0
                 if(CollectionUtils.isNotEmpty(documentRoleMember.getRoleRspActions())){
 2280  0
                         for(KimDocumentRoleResponsibilityAction roleRspAction : documentRoleMember.getRoleRspActions()){
 2281  0
                                 RoleResponsibilityActionBo newRoleRspAction = new RoleResponsibilityActionBo();
 2282  0
                                 newRoleRspAction.setId(roleRspAction.getRoleResponsibilityActionId());
 2283  0
                                 newRoleRspAction.setActionPolicyCode(roleRspAction.getActionPolicyCode());
 2284  0
                                 newRoleRspAction.setActionTypeCode(roleRspAction.getActionTypeCode());
 2285  0
                                 newRoleRspAction.setPriorityNumber(roleRspAction.getPriorityNumber());
 2286  0
                                 newRoleRspAction.setRoleMemberId(roleRspAction.getRoleMemberId());
 2287  0
                                 newRoleRspAction.setForceAction(roleRspAction.isForceAction());
 2288  0
                                 newRoleRspAction.setRoleResponsibilityId("*");
 2289  0
                                 if(ObjectUtils.isNotNull(origActions)){
 2290  0
                                         for(RoleResponsibilityActionBo origRspAction: origActions) {
 2291  0
                                                 if(activatingInactive && StringUtils.equals(origRspAction.getRoleResponsibilityId(), newRoleRspAction.getRoleResponsibilityId()) &&
 2292  
                                                                 StringUtils.equals(newRoleRspAction.getRoleMemberId(), newRoleMemberIdAssigned)){
 2293  0
                                                         newRoleRspAction.setRoleMemberId(origRspAction.getRoleMemberId());
 2294  0
                                                         newRoleRspAction.setId(origRspAction.getId());
 2295  
                                                 }
 2296  0
                                                 if (origRspAction.getId()!=null && StringUtils.equals(origRspAction.getId(), newRoleRspAction.getId())) {
 2297  0
                                                         newRoleRspAction.setVersionNumber(origRspAction.getVersionNumber());
 2298  
                                                 }
 2299  
                                         }
 2300  
                                 }
 2301  0
                                 roleRspActions.add(newRoleRspAction);
 2302  0
                         }
 2303  
                 }
 2304  0
                 return roleRspActions;
 2305  
         }
 2306  
 
 2307  
         protected List<RoleMemberAttributeDataBo> getRoleMemberAttributeData(List<KimDocumentRoleQualifier> qualifiers,
 2308  
                         List<RoleMemberAttributeDataBo> origAttributes, boolean activatingInactive, String newRoleMemberIdAssigned){
 2309  0
                 List<RoleMemberAttributeDataBo> roleMemberAttributeDataList = new ArrayList<RoleMemberAttributeDataBo>();
 2310  
                 RoleMemberAttributeDataBo newRoleMemberAttributeData;
 2311  0
                 if(CollectionUtils.isNotEmpty(qualifiers)){
 2312  0
                         for(KimDocumentRoleQualifier memberRoleQualifier: qualifiers){
 2313  0
                                 if(StringUtils.isNotBlank(memberRoleQualifier.getAttrVal())){
 2314  0
                                         newRoleMemberAttributeData = new RoleMemberAttributeDataBo();
 2315  0
                                         newRoleMemberAttributeData.setId(memberRoleQualifier.getAttrDataId());
 2316  0
                                         newRoleMemberAttributeData.setAttributeValue(memberRoleQualifier.getAttrVal());
 2317  0
                                         newRoleMemberAttributeData.setAssignedToId(memberRoleQualifier.getRoleMemberId());
 2318  0
                                         newRoleMemberAttributeData.setKimTypeId(memberRoleQualifier.getKimTypId());
 2319  0
                                         newRoleMemberAttributeData.setKimAttributeId(memberRoleQualifier.getKimAttrDefnId());
 2320  
 
 2321  0
                                         updateAttrValIfNecessary(newRoleMemberAttributeData);
 2322  
 
 2323  0
                                         if(ObjectUtils.isNotNull(origAttributes)){
 2324  0
                                                 for(RoleMemberAttributeDataBo origAttribute: origAttributes){
 2325  0
                                                         if(activatingInactive && StringUtils.equals(origAttribute.getKimAttributeId(), newRoleMemberAttributeData.getKimAttributeId()) &&
 2326  
                                                                         StringUtils.equals(newRoleMemberAttributeData.getAssignedToId(), newRoleMemberIdAssigned)){
 2327  0
                                                                 newRoleMemberAttributeData.setAssignedToId(origAttribute.getAssignedToId());
 2328  0
                                                                 newRoleMemberAttributeData.setId(origAttribute.getId());
 2329  
                                                         }
 2330  0
                                                         if(origAttribute.getId()!=null && StringUtils.equals(origAttribute.getId(), newRoleMemberAttributeData.getId())){
 2331  0
                                                                 newRoleMemberAttributeData.setVersionNumber(origAttribute.getVersionNumber());
 2332  
                                                         }
 2333  
                                                 }
 2334  
                                         }
 2335  0
                                         roleMemberAttributeDataList.add(newRoleMemberAttributeData);
 2336  
                                 }
 2337  
                         }
 2338  
                 }
 2339  0
                 return roleMemberAttributeDataList;
 2340  
         }
 2341  
 
 2342  
         /**
 2343  
          * Determines if the attribute value on the attribute data should be updated; if so, it performs some attribute value formatting.
 2344  
          * In the default implementation, this method formats checkbox controls
 2345  
          *
 2346  
          * @param roleMemberAttributeData a role member qualifier attribute to update
 2347  
          */
 2348  
         protected void updateAttrValIfNecessary(RoleMemberAttributeDataBo roleMemberAttributeData) {
 2349  0
                 if (doCheckboxLogic(roleMemberAttributeData.getKimTypeId(), roleMemberAttributeData.getKimAttributeId())) {
 2350  0
                         convertCheckboxAttributeData(roleMemberAttributeData);
 2351  
                 }
 2352  0
         }
 2353  
 
 2354  
         protected void formatAttrValIfNecessary(KimDocumentRoleQualifier roleQualifier) {
 2355  0
         if (doCheckboxLogic(roleQualifier.getKimTypId(), roleQualifier.getKimAttrDefnId())) {
 2356  0
             formatCheckboxAttributeData(roleQualifier);
 2357  
         }
 2358  0
         }
 2359  
 
 2360  
     private boolean doCheckboxLogic(String kimTypeId, String attrId) {
 2361  0
         final KimAttributeField attributeDefinition = getAttributeDefinition(kimTypeId, attrId);
 2362  0
         return attributeDefinition != null && attributeDefinition.getAttributeField().getControl() != null && attributeDefinition.getAttributeField().getControl() instanceof RemotableCheckboxGroup;
 2363  
     }
 2364  
 
 2365  
         protected void formatCheckboxAttributeData(KimDocumentRoleQualifier roleQualifier) {
 2366  0
                 if (roleQualifier.getAttrVal().equals(KimConstants.KIM_ATTRIBUTE_BOOLEAN_TRUE_STR_VALUE)) {
 2367  0
                         roleQualifier.setAttrVal(KimConstants.KIM_ATTRIBUTE_BOOLEAN_TRUE_STR_VALUE_DISPLAY);
 2368  0
                 } else if (roleQualifier.getAttrVal().equals(KimConstants.KIM_ATTRIBUTE_BOOLEAN_FALSE_STR_VALUE)) {
 2369  0
                         roleQualifier.setAttrVal(KimConstants.KIM_ATTRIBUTE_BOOLEAN_FALSE_STR_VALUE_DISPLAY);
 2370  
                 }
 2371  0
         }
 2372  
 
 2373  
         /**
 2374  
          * Finds the KNS attribute used to render the given KimAttributeData
 2375  
          *
 2376  
      * @return the KNS attribute used to render that qualifier, or null if the AttributeDefinition cannot be determined
 2377  
          */
 2378  
         protected KimAttributeField getAttributeDefinition(String kimTypId, String attrDefnId) {
 2379  0
                 final KimType type = getKimTypeInfoService().getKimType(kimTypId);
 2380  0
                 if (type != null) {
 2381  0
                         final KimTypeService typeService = (KimTypeService) KimImplServiceLocator.getBean(type.getServiceName());
 2382  0
                         if (typeService != null) {
 2383  0
                                 final KimTypeAttribute attributeInfo = type.getAttributeDefinitionById(attrDefnId);
 2384  0
                                 if (attributeInfo != null) {
 2385  0
                                         final List<KimAttributeField> attributeMap = typeService.getAttributeDefinitions(type.getId());
 2386  0
                                         if (attributeMap != null) {
 2387  0
                                                 return DataDictionaryTypeServiceHelper.findAttributeField(
 2388  
                                 attributeInfo.getKimAttribute().getAttributeName(), attributeMap);
 2389  
                                         }
 2390  
                                 }
 2391  
                         }
 2392  
                 }
 2393  0
                 return null;
 2394  
         }
 2395  
 
 2396  
         /**
 2397  
          * Formats the attribute value on this checkbox attribute, changing "on" to "Y" and "off" to "N"
 2398  
          *
 2399  
          * @param roleMemberAttributeData the attribute data to format the attribute value of
 2400  
          */
 2401  
         protected void convertCheckboxAttributeData(RoleMemberAttributeDataBo roleMemberAttributeData) {
 2402  0
                 if (roleMemberAttributeData.getAttributeValue().equalsIgnoreCase(KimConstants.KIM_ATTRIBUTE_BOOLEAN_TRUE_STR_VALUE_DISPLAY)) {
 2403  0
                         roleMemberAttributeData.setAttributeValue(KimConstants.KIM_ATTRIBUTE_BOOLEAN_TRUE_STR_VALUE);
 2404  0
                 } else if (roleMemberAttributeData.getAttributeValue().equalsIgnoreCase(KimConstants.KIM_ATTRIBUTE_BOOLEAN_FALSE_STR_VALUE_DISPLAY)) {
 2405  0
                         roleMemberAttributeData.setAttributeValue(KimConstants.KIM_ATTRIBUTE_BOOLEAN_FALSE_STR_VALUE);
 2406  
                 }
 2407  0
         }
 2408  
 
 2409  
         protected List<DelegateTypeBo> getRoleDelegations(IdentityManagementRoleDocument identityManagementRoleDocument, List<DelegateTypeBo> origDelegations){
 2410  0
                 List<DelegateTypeBo> kimDelegations = new ArrayList<DelegateTypeBo>();
 2411  
                 DelegateTypeBo newKimDelegation;
 2412  0
                 DelegateTypeBo origDelegationImplTemp = null;
 2413  
                 List<DelegateMemberBo> origMembers;
 2414  0
                 boolean activatingInactive = false;
 2415  0
                 String newDelegationIdAssigned = "";
 2416  0
                 if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getDelegations())){
 2417  0
                         for(RoleDocumentDelegation roleDocumentDelegation: identityManagementRoleDocument.getDelegations()){
 2418  0
                                 newKimDelegation = new DelegateTypeBo();
 2419  0
                                 KimCommonUtilsInternal.copyProperties(newKimDelegation, roleDocumentDelegation);
 2420  0
                                 newKimDelegation.setRoleId(identityManagementRoleDocument.getRoleId());
 2421  0
                                 if(ObjectUtils.isNotNull(origDelegations)){
 2422  0
                                         for(DelegateTypeBo origDelegationImpl: origDelegations){
 2423  0
                                                 if(StringUtils.equals(origDelegationImpl.getRoleId(), newKimDelegation.getRoleId()) &&
 2424  
                                                                 StringUtils.equals(origDelegationImpl.getDelegationId(), newKimDelegation.getDelegationId())){
 2425  
                                                         //TODO: verify if you want to add  && newRoleMember.isActive() condition to if...
 2426  0
                                                         newDelegationIdAssigned = newKimDelegation.getDelegationId();
 2427  0
                                                         newKimDelegation.setDelegationId(origDelegationImpl.getDelegationId());
 2428  0
                                                         activatingInactive = true;
 2429  
                                                 }
 2430  0
                                                 if(origDelegationImpl.getDelegationId()!=null && StringUtils.equals(origDelegationImpl.getDelegationId(), newKimDelegation.getDelegationId())){
 2431  0
                                                         newKimDelegation.setVersionNumber(origDelegationImpl.getVersionNumber());
 2432  0
                                                         origDelegationImplTemp = origDelegationImpl;
 2433  
                                                 }
 2434  
                                         }
 2435  
                                 }
 2436  0
                                 origMembers = (origDelegationImplTemp == null || origDelegationImplTemp.getMembers()==null)?
 2437  
                                                                         new ArrayList<DelegateMemberBo>():origDelegationImplTemp.getMembers();
 2438  0
                                 newKimDelegation.setMembers(getDelegationMembers(roleDocumentDelegation.getMembers(), origMembers, activatingInactive, newDelegationIdAssigned));
 2439  0
                 kimDelegations.add(newKimDelegation);
 2440  0
                                 activatingInactive = false;
 2441  
                         }
 2442  
                 }
 2443  0
                 return kimDelegations;
 2444  
         }
 2445  
 
 2446  
         protected List<DelegateMemberBo> getDelegationMembers(List<RoleDocumentDelegationMember> delegationMembers,
 2447  
                         List<DelegateMemberBo> origDelegationMembers, boolean activatingInactive, String newDelegationIdAssigned){
 2448  0
                 List<DelegateMemberBo> delegationsMembersList = new ArrayList<DelegateMemberBo>();
 2449  
                 DelegateMemberBo newDelegationMemberImpl;
 2450  0
                 DelegateMemberBo origDelegationMemberImplTemp = null;
 2451  
                 List<DelegateMemberAttributeDataBo> origAttributes;
 2452  0
                 String delegationMemberId = "";
 2453  0
                 if(CollectionUtils.isNotEmpty(delegationMembers)){
 2454  0
                         for(RoleDocumentDelegationMember delegationMember: delegationMembers){
 2455  0
                                 newDelegationMemberImpl = new DelegateMemberBo();
 2456  0
                                 KimCommonUtilsInternal.copyProperties(newDelegationMemberImpl, delegationMember);
 2457  0
                 newDelegationMemberImpl.setType(MemberType.fromCode(delegationMember.getMemberTypeCode()));
 2458  0
                                 if(ObjectUtils.isNotNull(origDelegationMembers)){
 2459  0
                                         for(DelegateMemberBo origDelegationMember: origDelegationMembers){
 2460  0
                                                 if(activatingInactive && StringUtils.equals(origDelegationMember.getMemberId(), newDelegationMemberImpl.getMemberId()) &&
 2461  
                                                                 StringUtils.equals(newDelegationMemberImpl.getDelegationId(), newDelegationIdAssigned) &&
 2462  
                                                                 !origDelegationMember.isActive(new Timestamp(System.currentTimeMillis()))){
 2463  0
                                                         newDelegationMemberImpl.setDelegationId(origDelegationMember.getDelegationId());
 2464  0
                                                         delegationMemberId = newDelegationMemberImpl.getDelegationMemberId();
 2465  0
                                                         newDelegationMemberImpl.setDelegationMemberId(origDelegationMember.getDelegationMemberId());
 2466  
                                                 }
 2467  0
                                                 if(origDelegationMember.getDelegationMemberId()!=null && StringUtils.equals(origDelegationMember.getDelegationMemberId(), newDelegationMemberImpl.getDelegationMemberId())){
 2468  0
                                                         newDelegationMemberImpl.setVersionNumber(origDelegationMember.getVersionNumber());
 2469  0
                                                         origDelegationMemberImplTemp = origDelegationMember;
 2470  
                                                 }
 2471  
                                         }
 2472  
                                 }
 2473  0
                                 origAttributes = (origDelegationMemberImplTemp==null || origDelegationMemberImplTemp.getAttributes()==null)?
 2474  
                                                 new ArrayList<DelegateMemberAttributeDataBo>():origDelegationMemberImplTemp.getAttributes();
 2475  0
                                 newDelegationMemberImpl.setAttributes(getDelegationMemberAttributeData(delegationMember.getQualifiers(), origAttributes, activatingInactive, delegationMemberId));
 2476  0
                                 newDelegationMemberImpl.setActiveFromDateValue(delegationMember.getActiveFromDate());
 2477  0
                 newDelegationMemberImpl.setActiveToDateValue(delegationMember.getActiveToDate());
 2478  0
                 delegationsMembersList.add(newDelegationMemberImpl);
 2479  
                         }
 2480  
                 }
 2481  0
                 return delegationsMembersList;
 2482  
         }
 2483  
 
 2484  
         //TODO: implement logic same as role members - do not insert qualifiers with blank values
 2485  
         protected List<DelegateMemberAttributeDataBo> getDelegationMemberAttributeData(
 2486  
                         List<RoleDocumentDelegationMemberQualifier> qualifiers, List<DelegateMemberAttributeDataBo> origAttributes,
 2487  
                         boolean activatingInactive, String delegationMemberId){
 2488  0
                 List<DelegateMemberAttributeDataBo> delegationMemberAttributeDataList = new ArrayList<DelegateMemberAttributeDataBo>();
 2489  
                 DelegateMemberAttributeDataBo newDelegationMemberAttributeData;
 2490  0
                 if(CollectionUtils.isNotEmpty(qualifiers)){
 2491  0
                         for(RoleDocumentDelegationMemberQualifier memberRoleQualifier: qualifiers){
 2492  0
                                 if(StringUtils.isNotBlank(memberRoleQualifier.getAttrVal())){
 2493  0
                                         newDelegationMemberAttributeData = new DelegateMemberAttributeDataBo();
 2494  0
                                         newDelegationMemberAttributeData.setId(memberRoleQualifier.getAttrDataId());
 2495  0
                                         newDelegationMemberAttributeData.setAttributeValue(memberRoleQualifier.getAttrVal());
 2496  0
                                         newDelegationMemberAttributeData.setAssignedToId(memberRoleQualifier.getDelegationMemberId());
 2497  0
                                         newDelegationMemberAttributeData.setKimTypeId(memberRoleQualifier.getKimTypId());
 2498  0
                                         newDelegationMemberAttributeData.setKimAttributeId(memberRoleQualifier.getKimAttrDefnId());
 2499  0
                                         if(ObjectUtils.isNotNull(origAttributes)){
 2500  0
                                                 for(DelegateMemberAttributeDataBo origAttribute: origAttributes){
 2501  0
                                                         if(activatingInactive && StringUtils.equals(origAttribute.getKimAttributeId(), newDelegationMemberAttributeData.getKimAttributeId()) &&
 2502  
                                                                         StringUtils.equals(newDelegationMemberAttributeData.getAssignedToId(), delegationMemberId)){
 2503  0
                                                                 newDelegationMemberAttributeData.setAssignedToId(origAttribute.getAssignedToId());
 2504  0
                                                                 newDelegationMemberAttributeData.setId(origAttribute.getId());
 2505  
                                                         }
 2506  0
                                                         if(StringUtils.equals(origAttribute.getId(), newDelegationMemberAttributeData.getId())){
 2507  0
                                                                 newDelegationMemberAttributeData.setVersionNumber(origAttribute.getVersionNumber());
 2508  
                                                         }
 2509  
                                                 }
 2510  
                                         }
 2511  0
                                         delegationMemberAttributeDataList.add(newDelegationMemberAttributeData);
 2512  
                                 }
 2513  
                         }
 2514  
                 }
 2515  0
                 return delegationMemberAttributeDataList;
 2516  
         }
 2517  
 
 2518  
         /* Group document methods */
 2519  
         public void loadGroupDoc(IdentityManagementGroupDocument identityManagementGroupDocument, Group groupInfo){
 2520  
                 //Map<String, String> criteria = new HashMap<String, String>();
 2521  
                 //criteria.put(KimApiConstants.PrimaryKeyConstants.GROUP_ID, groupInfo.getId());
 2522  
                 //GroupImpl kimGroupImpl = (GroupImpl)
 2523  
                 //        getBusinessObjectService().findByPrimaryKey(GroupImpl.class, criteria);
 2524  
 
 2525  0
                 identityManagementGroupDocument.setGroupId(groupInfo.getId());
 2526  0
         KimType kimType = KimApiServiceLocator.getKimTypeInfoService().getKimType(groupInfo.getKimTypeId());
 2527  0
                 identityManagementGroupDocument.setKimType(kimType);
 2528  0
                 identityManagementGroupDocument.setGroupTypeName(kimType.getName());
 2529  0
                 identityManagementGroupDocument.setGroupTypeId(kimType.getId());
 2530  0
                 identityManagementGroupDocument.setGroupName(groupInfo.getName());
 2531  0
                 identityManagementGroupDocument.setGroupDescription(groupInfo.getDescription());
 2532  0
                 identityManagementGroupDocument.setActive(groupInfo.isActive());
 2533  0
                 identityManagementGroupDocument.setGroupNamespace(groupInfo.getNamespaceCode());
 2534  
 
 2535  0
         List<GroupMember> members = new ArrayList(KimApiServiceLocator.getGroupService().getMembersOfGroup(groupInfo.getId()));
 2536  0
         identityManagementGroupDocument.setMembers(loadGroupMembers(identityManagementGroupDocument, members));
 2537  
 
 2538  
 
 2539  
 
 2540  0
         identityManagementGroupDocument.setQualifiers(loadGroupQualifiers(identityManagementGroupDocument, groupInfo.getAttributes()));
 2541  0
                 identityManagementGroupDocument.setEditing(true);
 2542  0
         }
 2543  
 
 2544  0
         protected static class GroupMemberNameComparator implements Comparator<GroupDocumentMember> {
 2545  
                 /**
 2546  
                  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
 2547  
                  */
 2548  
                 public int compare(GroupDocumentMember m1, GroupDocumentMember m2) {
 2549  0
                         return m1.getMemberName().compareToIgnoreCase(m2.getMemberName());
 2550  
                 }
 2551  
         }
 2552  
 
 2553  0
         protected GroupMemberNameComparator groupMemberNameComparator = new GroupMemberNameComparator();
 2554  
 
 2555  
         protected List<GroupDocumentMember> loadGroupMembers(
 2556  
                         IdentityManagementGroupDocument identityManagementGroupDocument, List<GroupMember> members){
 2557  0
                 List<GroupDocumentMember> pndMembers = new ArrayList<GroupDocumentMember>();
 2558  0
                 GroupDocumentMember pndMember = new GroupDocumentMember();
 2559  0
                 if(ObjectUtils.isNotNull(members)){
 2560  0
                         for(GroupMember member: members){
 2561  0
                                 pndMember = new GroupDocumentMember();
 2562  
 
 2563  0
                                 pndMember.setActiveFromDate(member.getActiveFromDate() == null ? null : new Timestamp(member.getActiveFromDate().getMillis()));
 2564  0
                                 pndMember.setActiveToDate(member.getActiveToDate() == null ? null : new Timestamp(member.getActiveToDate().getMillis()));
 2565  
                                 //pndMember.setActive(member.isActive());
 2566  0
                                 if(pndMember.isActive()){
 2567  0
                                         pndMember.setGroupMemberId(member.getMemberId());
 2568  0
                                         pndMember.setGroupId(member.getGroupId());
 2569  0
                                         pndMember.setMemberId(member.getMemberId());
 2570  0
                                         pndMember.setMemberName(getMemberName(member.getType(), member.getMemberId()));
 2571  0
                                         pndMember.setMemberFullName(getMemberFullName(member.getType(), member.getMemberId()));
 2572  0
                                         pndMember.setMemberTypeCode(member.getType().getCode());
 2573  0
                                         pndMember.setEdit(true);
 2574  0
                                         pndMembers.add(pndMember);
 2575  
                                 }
 2576  
                         }
 2577  
                 }
 2578  0
                 Collections.sort(pndMembers, groupMemberNameComparator);
 2579  0
                 return pndMembers;
 2580  
         }
 2581  
 
 2582  
         protected List<GroupDocumentQualifier> loadGroupQualifiers(IdentityManagementGroupDocument IdentityManagementGroupDocument,
 2583  
                         Map<String, String> attributes){
 2584  0
                 List<GroupDocumentQualifier> pndGroupQualifiers = new ArrayList<GroupDocumentQualifier>();
 2585  0
                 GroupDocumentQualifier pndGroupQualifier = new GroupDocumentQualifier();
 2586  0
                 List<KimAttributeField> origAttributes = IdentityManagementGroupDocument.getDefinitions();
 2587  0
                 boolean attributePresent = false;
 2588  
                 String origAttributeId;
 2589  0
                 if(origAttributes!=null){
 2590  
 
 2591  0
                         for(KimAttributeField key: origAttributes) {
 2592  0
                                 origAttributeId = IdentityManagementGroupDocument.getKimAttributeDefnId(key);
 2593  0
                                 if(!attributes.isEmpty()){
 2594  
 
 2595  0
                                         for(GroupAttributeBo groupQualifier: KimAttributeDataBo.createFrom(GroupAttributeBo.class, attributes, IdentityManagementGroupDocument.getGroupTypeId())){
 2596  0
                                                 if(origAttributeId!=null && ObjectUtils.isNotNull(groupQualifier.getKimAttribute()) &&
 2597  
                                                                 StringUtils.equals(origAttributeId, groupQualifier.getKimAttribute().getId())){
 2598  0
                                                         pndGroupQualifier = new GroupDocumentQualifier();
 2599  0
                                                         KimCommonUtilsInternal.copyProperties(pndGroupQualifier, groupQualifier);
 2600  0
                                                         pndGroupQualifier.setAttrDataId(groupQualifier.getId());
 2601  0
                                                         pndGroupQualifier.setAttrVal(groupQualifier.getAttributeValue());
 2602  0
                                                         pndGroupQualifier.setKimAttrDefnId(groupQualifier.getKimAttribute().getId());
 2603  0
                                                         pndGroupQualifier.setKimTypId(groupQualifier.getKimType().getId());
 2604  0
                                                         pndGroupQualifier.setGroupId(groupQualifier.getAssignedToId());
 2605  0
                                                         pndGroupQualifiers.add(pndGroupQualifier);
 2606  0
                                                         attributePresent = true;
 2607  
                                                 }
 2608  
                                         }
 2609  
                                 }
 2610  0
                                 if(!attributePresent){
 2611  0
                                         pndGroupQualifier = new GroupDocumentQualifier();
 2612  0
                                         pndGroupQualifier.setKimAttrDefnId(origAttributeId);
 2613  0
                                         pndGroupQualifiers.add(pndGroupQualifier);
 2614  
                                 }
 2615  0
                                 attributePresent = false;
 2616  
                         }
 2617  
                 }
 2618  0
                 return pndGroupQualifiers;
 2619  
         }
 2620  
 
 2621  
         /**
 2622  
          * @see org.kuali.rice.kim.service.UiDocumentService#saveEntityPerson(IdentityManagementPersonDocument)
 2623  
          */
 2624  
         @SuppressWarnings("unchecked")
 2625  
         public void saveGroup(IdentityManagementGroupDocument identityManagementGroupDocument) {
 2626  0
                 GroupBo kimGroup = new GroupBo();
 2627  0
                 Map<String, String> criteria = new HashMap<String, String>();
 2628  0
                 String groupId = identityManagementGroupDocument.getGroupId();
 2629  0
                 criteria.put("groupId", groupId);
 2630  0
                 GroupBo origGroup = (GroupBo)getBusinessObjectService().findBySinglePrimaryKey(GroupBo.class, groupId);
 2631  0
                 List<GroupMemberBo> origGroupMembers = new ArrayList<GroupMemberBo>();
 2632  0
                 if (ObjectUtils.isNull(origGroup)) {
 2633  0
                         origGroup = new GroupBo();
 2634  0
                         kimGroup.setActive(true);
 2635  
                 } else {
 2636  0
                         kimGroup.setVersionNumber(origGroup.getVersionNumber());
 2637  
                         //TODO: when a group is inactivated, inactivate the memberships of principals in that group
 2638  
                         //and the memberships of that group in roles
 2639  0
                         kimGroup.setActive(identityManagementGroupDocument.isActive());
 2640  0
                         origGroupMembers = (List<GroupMemberBo>)getBusinessObjectService().findMatching(GroupMemberBo.class, criteria);
 2641  
                 }
 2642  
 
 2643  0
                 kimGroup.setId(identityManagementGroupDocument.getGroupId());
 2644  0
                 KimType kimType = getKimTypeInfoService().getKimType(identityManagementGroupDocument.getGroupTypeId());
 2645  0
                 if( kimType == null ) {
 2646  0
                         throw new RuntimeException("Kim type not found for:"+identityManagementGroupDocument.getGroupTypeId());
 2647  
                 }
 2648  
 
 2649  0
                 kimGroup.setKimTypeId(kimType.getId());
 2650  0
                 kimGroup.setNamespaceCode(identityManagementGroupDocument.getGroupNamespace());
 2651  0
                 kimGroup.setName(identityManagementGroupDocument.getGroupName());
 2652  0
                 kimGroup.setDescription(identityManagementGroupDocument.getGroupDescription());
 2653  0
                 kimGroup.setAttributeDetails(getGroupAttributeData(identityManagementGroupDocument, origGroup.getAttributeDetails()));
 2654  
 
 2655  
                 List<String> oldIds;
 2656  
                 List<String> newIds;
 2657  0
                 oldIds = getGroupService().getMemberPrincipalIds(kimGroup.getId()); // for the actionList update
 2658  
 
 2659  
 
 2660  0
                 List<GroupMemberBo> newGroupMembersList = getGroupMembers(identityManagementGroupDocument, origGroupMembers);
 2661  0
                 kimGroup.setMembers(newGroupMembersList);  // add the new, complete list to the group
 2662  
 
 2663  0
                 kimGroup = (GroupBo)getBusinessObjectService().save(kimGroup);
 2664  
 
 2665  0
                 newIds = kimGroup.getMemberPrincipalIds();
 2666  
                 //newIds = getGroupService().getMemberPrincipalIds(kimGroup.getGroupId()); // for the action list update
 2667  
 
 2668  
                 // Do an async update of the action list for the updated groups
 2669  0
                 org.kuali.rice.kim.service.KIMServiceLocatorInternal.getGroupInternalService().updateForWorkgroupChange(kimGroup.getId(), oldIds, newIds);
 2670  0
                 if(!kimGroup.isActive()){
 2671  
                         // when a group is inactivated, inactivate the memberships of principals in that group
 2672  
                         // and the memberships of that group in roles
 2673  0
                         KimImplServiceLocator.getRoleInternalService().groupInactivated(identityManagementGroupDocument.getGroupId());
 2674  
                 }
 2675  
 
 2676  0
         }
 2677  
 
 2678  
         protected List<GroupMemberBo> getGroupMembers(IdentityManagementGroupDocument identityManagementGroupDocument, List<GroupMemberBo> origGroupMembers){
 2679  0
                 List<GroupMemberBo> groupMembers = new ArrayList<GroupMemberBo>();
 2680  
                 GroupMemberBo newGroupMember;
 2681  0
                 if(CollectionUtils.isNotEmpty(identityManagementGroupDocument.getMembers())){
 2682  0
                         for(GroupDocumentMember documentGroupMember: identityManagementGroupDocument.getMembers()){
 2683  0
                                 newGroupMember = new GroupMemberBo();
 2684  
                                 //KimCommonUtilsInternalInternal.copyProperties(newGroupMember, documentGroupMember);
 2685  
                 //copy properties manually for now until new BO created for DocumentGroupMember
 2686  
 
 2687  0
                                 newGroupMember.setGroupId(identityManagementGroupDocument.getGroupId());
 2688  0
                 newGroupMember.setActiveFromDateValue(documentGroupMember.getActiveFromDate());
 2689  0
                 newGroupMember.setActiveToDateValue(documentGroupMember.getActiveToDate());
 2690  0
                 newGroupMember.setMemberId(documentGroupMember.getMemberId());
 2691  0
                 newGroupMember.setTypeCode(documentGroupMember.getMemberTypeCode());
 2692  0
                                 if(ObjectUtils.isNotNull(origGroupMembers)){
 2693  0
                                         for(GroupMemberBo origGroupMemberImpl: origGroupMembers){
 2694  0
                                                 if(StringUtils.equals(origGroupMemberImpl.getGroupId(), newGroupMember.getGroupId()) &&
 2695  
                                                                 StringUtils.equals(origGroupMemberImpl.getMemberId(), newGroupMember.getMemberId()) &&
 2696  
                                                                 !origGroupMemberImpl.isActive(new Timestamp(System.currentTimeMillis()))){
 2697  
                                                         //TODO: verify if you want to add  && newGroupMember.isActive() condition to if...
 2698  0
                                                         newGroupMember.setMemberId(origGroupMemberImpl.getMemberId());
 2699  
                                                 }
 2700  0
                         if(StringUtils.equals(origGroupMemberImpl.getGroupId(), newGroupMember.getGroupId()) &&
 2701  
                                                                 StringUtils.equals(origGroupMemberImpl.getMemberId(), newGroupMember.getMemberId()) &&
 2702  
                                                                 origGroupMemberImpl.isActive(new Timestamp(System.currentTimeMillis()))){
 2703  0
                                                         newGroupMember.setId(origGroupMemberImpl.getId());
 2704  0
                             newGroupMember.setVersionNumber(origGroupMemberImpl.getVersionNumber());
 2705  
                                                 }
 2706  
                                         }
 2707  
                                 }
 2708  0
                                 groupMembers.add(newGroupMember);
 2709  
                         }
 2710  
                 }
 2711  0
                 return groupMembers;
 2712  
         }
 2713  
 
 2714  
         protected List<GroupAttributeBo> getGroupAttributeData(IdentityManagementGroupDocument identityManagementGroupDocument,
 2715  
                         List<GroupAttributeBo> origAttributes){
 2716  0
                 List<GroupAttributeBo> groupAttributeDataList = new ArrayList<GroupAttributeBo>();
 2717  
                 GroupAttributeBo newGroupAttributeData;
 2718  0
                 if(CollectionUtils.isNotEmpty(identityManagementGroupDocument.getQualifiers())){
 2719  0
                         for(GroupDocumentQualifier groupQualifier: identityManagementGroupDocument.getQualifiers()){
 2720  0
                                 if(StringUtils.isNotBlank(groupQualifier.getAttrVal())){
 2721  0
                                         newGroupAttributeData = new GroupAttributeBo();
 2722  0
                                         newGroupAttributeData.setId(groupQualifier.getAttrDataId());
 2723  0
                                         newGroupAttributeData.setAttributeValue(groupQualifier.getAttrVal());
 2724  0
                                         newGroupAttributeData.setAssignedToId(groupQualifier.getGroupId());
 2725  0
                                         newGroupAttributeData.setKimTypeId(groupQualifier.getKimTypId());
 2726  0
                                         newGroupAttributeData.setKimAttributeId(groupQualifier.getKimAttrDefnId());
 2727  0
                                         if(ObjectUtils.isNotNull(origAttributes)){
 2728  0
                                                 for(GroupAttributeBo origAttribute: origAttributes){
 2729  0
                                                         if(StringUtils.equals(origAttribute.getKimAttributeId(), newGroupAttributeData.getKimAttributeId()) &&
 2730  
                                                                         StringUtils.equals(newGroupAttributeData.getAssignedToId(), origAttribute.getAssignedToId())){
 2731  0
                                                             newGroupAttributeData.setId(origAttribute.getId());
 2732  
                                                         }
 2733  0
                                                         if(origAttribute.getId()!=null && StringUtils.equals(origAttribute.getId(), newGroupAttributeData.getId())){
 2734  0
                                                             newGroupAttributeData.setVersionNumber(origAttribute.getVersionNumber());
 2735  
                                                         }
 2736  
                                                 }
 2737  
                                         }
 2738  0
                                         groupAttributeDataList.add(newGroupAttributeData);
 2739  
                                 }
 2740  
                         }
 2741  
                 }
 2742  0
                 return groupAttributeDataList;
 2743  
         }
 2744  
 
 2745  
     @SuppressWarnings("unchecked")
 2746  
         public KimDocumentRoleMember getKimDocumentRoleMember(MemberType memberType, String memberId, String roleId){
 2747  0
             if(memberType == null || StringUtils.isEmpty(memberId) || StringUtils.isEmpty(roleId)) {
 2748  0
                     return null;
 2749  
         }
 2750  0
             KimDocumentRoleMember documentRoleMember = new KimDocumentRoleMember();
 2751  0
             documentRoleMember.setRoleId(roleId);
 2752  0
             Map<String, String> criteria = new HashMap<String, String>();
 2753  0
             criteria.put("roleId", roleId);
 2754  0
             criteria.put("mbr_id", memberId);
 2755  
 
 2756  0
             List<RoleMemberBo> matchingRoleMembers = (List<RoleMemberBo>)getBusinessObjectService().findMatching(RoleMemberBo.class, criteria);
 2757  0
             if (matchingRoleMembers==null || matchingRoleMembers.size()<1) { return null; }
 2758  
 
 2759  0
             RoleMemberBo roleMemberImpl = matchingRoleMembers.get(0);
 2760  0
             documentRoleMember.setRoleMemberId(roleMemberImpl.getRoleMemberId());
 2761  0
             if(MemberType.PRINCIPAL.equals(memberType)){
 2762  0
                     Principal principal = getIdentityService().getPrincipal(memberId);
 2763  0
                     if (principal != null) {
 2764  0
                             documentRoleMember.setMemberId(principal.getPrincipalId());
 2765  0
                         documentRoleMember.setMemberName(principal.getPrincipalName());
 2766  0
                         documentRoleMember.setMemberTypeCode(MemberType.PRINCIPAL.getCode());
 2767  
                     }                    
 2768  0
         } else if(MemberType.GROUP.equals(memberType)){
 2769  0
                 Group group = getGroupService().getGroup(memberId);
 2770  0
                 if (group != null) {
 2771  0
                         documentRoleMember.setMemberNamespaceCode(group.getNamespaceCode());
 2772  0
                         documentRoleMember.setMemberId(group.getId());
 2773  0
                         documentRoleMember.setMemberName(group.getName());
 2774  0
                         documentRoleMember.setMemberTypeCode(MemberType.GROUP.getCode());
 2775  
                 }
 2776  
                 
 2777  0
         } else if(MemberType.ROLE.equals(memberType)){
 2778  0
                 Role role = getRoleService().getRole(memberId);
 2779  0
                 if (role != null) {
 2780  0
                         documentRoleMember.setMemberNamespaceCode(role.getNamespaceCode());
 2781  0
                         documentRoleMember.setMemberId(role.getId());
 2782  0
                         documentRoleMember.setMemberName(role.getName());
 2783  0
                         documentRoleMember.setMemberTypeCode(MemberType.ROLE.getCode());
 2784  
                 }                
 2785  
         }
 2786  0
             return documentRoleMember;
 2787  
     }
 2788  
 
 2789  
     protected Set<String> getChangedRoleResponsibilityIds(
 2790  
                         IdentityManagementRoleDocument identityManagementRoleDocument, List<RoleResponsibilityBo> origRoleResponsibilities){
 2791  0
                 Set<String> lRet = new HashSet<String>();
 2792  0
                 List<String> newResp = new ArrayList<String>();
 2793  0
                 List<String> oldResp = new ArrayList<String>();
 2794  0
                 if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getResponsibilities())){
 2795  0
                         for(KimDocumentRoleResponsibility documentRoleResponsibility: identityManagementRoleDocument.getResponsibilities()){
 2796  0
                                 newResp.add(documentRoleResponsibility.getResponsibilityId());
 2797  
                         }
 2798  
                 }
 2799  0
                 if(ObjectUtils.isNotNull(origRoleResponsibilities)){
 2800  0
                         for(RoleResponsibilityBo roleRespBo: origRoleResponsibilities){
 2801  0
                                 oldResp.add(roleRespBo.getResponsibilityId());
 2802  
                         }
 2803  
                 }
 2804  0
                 lRet.addAll(newResp);
 2805  0
                 lRet.addAll(oldResp);
 2806  
 
 2807  0
                 return lRet;
 2808  
         }
 2809  
 
 2810  
         public KimTypeInfoService getKimTypeInfoService() {
 2811  0
                 if ( kimTypeInfoService == null ) {
 2812  0
                         kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
 2813  
                 }
 2814  0
                 return kimTypeInfoService;
 2815  
         }
 2816  
 
 2817  
     public List<KimDocumentRoleMember> getRoleMembers(Map<String,String> fieldValues) {
 2818  0
                 List<KimDocumentRoleMember> matchingRoleMembers = new ArrayList<KimDocumentRoleMember>();
 2819  0
                 List<RoleMember> matchingRoleMembersTemp = getRoleService().findRoleMembers(toQuery(fieldValues)).getResults();
 2820  
                 KimDocumentRoleMember matchingRoleMember;
 2821  
                 BusinessObject roleMemberObject;
 2822  
                 RoleMemberBo roleMemberBo;
 2823  0
                 if(CollectionUtils.isNotEmpty(matchingRoleMembersTemp)){
 2824  0
                         for(RoleMember roleMember: matchingRoleMembersTemp){
 2825  0
                                 roleMemberBo = getRoleMember(roleMember.getRoleMemberId());
 2826  0
                                 roleMemberObject = getMember(roleMemberBo.getMemberType(), roleMemberBo.getMemberId());
 2827  0
                                 matchingRoleMember = new KimDocumentRoleMember();
 2828  0
                                 KimCommonUtilsInternal.copyProperties(matchingRoleMember, roleMemberBo);
 2829  0
                                 matchingRoleMember.setMemberName(getMemberName(roleMemberBo.getMemberType(), roleMemberObject));
 2830  0
                                 matchingRoleMember.setMemberNamespaceCode(getMemberNamespaceCode(roleMemberBo.getMemberType(), roleMemberObject));
 2831  0
                                 matchingRoleMember.setQualifiers(getQualifiers(roleMemberBo.getAttributeDetails()));
 2832  0
                                 matchingRoleMembers.add(matchingRoleMember);
 2833  
                         }
 2834  
                 }
 2835  0
                 return matchingRoleMembers;
 2836  
     }
 2837  
 
 2838  
    private QueryByCriteria toQuery(Map<String,?> fieldValues) {
 2839  0
         Set<Predicate> preds = new HashSet<Predicate>();
 2840  0
         for (String key : fieldValues.keySet()) {
 2841  0
             preds.add(equal(key, fieldValues.get(key)));
 2842  
         }
 2843  0
         Predicate[] predicates = new Predicate[0];
 2844  0
         predicates = preds.toArray(predicates);
 2845  0
         return QueryByCriteria.Builder.fromPredicates(predicates);
 2846  
     }
 2847  
 
 2848  
     private List<KimDocumentRoleQualifier> getQualifiers(List<RoleMemberAttributeDataBo> attributes){
 2849  0
             if (attributes==null) {return null;}
 2850  0
             List<KimDocumentRoleQualifier> qualifiers = new ArrayList<KimDocumentRoleQualifier>();
 2851  
             KimDocumentRoleQualifier qualifier;
 2852  0
             if(ObjectUtils.isNotNull(attributes)){
 2853  0
                     for(RoleMemberAttributeDataBo attribute: attributes){
 2854  0
                             qualifier = new KimDocumentRoleQualifier();
 2855  0
                                 qualifier.setAttrDataId(attribute.getId());
 2856  0
                                 qualifier.setAttrVal(attribute.getAttributeValue());
 2857  0
                                 qualifier.setRoleMemberId(attribute.getAssignedToId());
 2858  0
                                 qualifier.setKimTypId(attribute.getKimTypeId());
 2859  0
                                 qualifier.setKimAttrDefnId(attribute.getKimAttributeId());
 2860  0
                                 qualifier.setKimAttribute(attribute.getKimAttribute());
 2861  0
                                 qualifiers.add(qualifier);
 2862  
                     }
 2863  
             }
 2864  0
             return qualifiers;
 2865  
     }
 2866  
     
 2867  
         public ResponsibilityInternalService getResponsibilityInternalService() {
 2868  0
                 if ( responsibilityInternalService == null ) {
 2869  0
                                 responsibilityInternalService = KimImplServiceLocator.getResponsibilityInternalService();
 2870  
                 }
 2871  0
                 return responsibilityInternalService;
 2872  
         }
 2873  
 
 2874  
    public PermissionService getPermissionService() {
 2875  0
                 if ( permissionService == null ) {
 2876  0
                                 permissionService = KimApiServiceLocator.getPermissionService();
 2877  
                 }
 2878  0
                 return permissionService;
 2879  
         }
 2880  
 
 2881  
     public ParameterService getParameterService() {
 2882  0
             if ( parameterService == null ) {
 2883  0
                     parameterService = CoreFrameworkServiceLocator.getParameterService();
 2884  
             }
 2885  0
             return parameterService;
 2886  
     }
 2887  
 
 2888  
     public void setParameterService(ParameterService parameterService) {
 2889  0
             this.parameterService = parameterService;
 2890  0
     }
 2891  
 }