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