Coverage Report - org.kuali.rice.kim.document.rule.IdentityManagementPersonDocumentRule
 
Classes in this File Line Coverage Branch Coverage Complexity
IdentityManagementPersonDocumentRule
0%
0/372
0%
0/260
4.525
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kim.document.rule;
 17  
 
 18  
 import org.apache.commons.collections.CollectionUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.core.util.RiceKeyConstants;
 21  
 import org.kuali.rice.kim.api.identity.entity.EntityDefault;
 22  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 23  
 import org.kuali.rice.kim.api.role.RoleService;
 24  
 import org.kuali.rice.kim.api.services.IdentityService;
 25  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 26  
 import org.kuali.rice.kim.api.type.KimType;
 27  
 import org.kuali.rice.kim.api.type.KimTypeService;
 28  
 import org.kuali.rice.kim.bo.types.dto.AttributeDefinitionMap;
 29  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
 30  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
 31  
 import org.kuali.rice.kim.bo.ui.PersonDocumentAffiliation;
 32  
 import org.kuali.rice.kim.bo.ui.PersonDocumentBoDefaultBase;
 33  
 import org.kuali.rice.kim.bo.ui.PersonDocumentEmploymentInfo;
 34  
 import org.kuali.rice.kim.bo.ui.PersonDocumentGroup;
 35  
 import org.kuali.rice.kim.bo.ui.PersonDocumentName;
 36  
 import org.kuali.rice.kim.bo.ui.PersonDocumentRole;
 37  
 import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember;
 38  
 import org.kuali.rice.kim.document.IdentityManagementPersonDocument;
 39  
 import org.kuali.rice.kim.document.authorization.IdentityManagementKimDocumentAuthorizer;
 40  
 import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
 41  
 import org.kuali.rice.kim.impl.role.RoleMemberBo;
 42  
 import org.kuali.rice.kim.impl.type.KimTypeBo;
 43  
 import org.kuali.rice.kim.rule.event.ui.AddGroupEvent;
 44  
 import org.kuali.rice.kim.rule.event.ui.AddPersonDelegationMemberEvent;
 45  
 import org.kuali.rice.kim.rule.event.ui.AddRoleEvent;
 46  
 import org.kuali.rice.kim.rule.ui.AddGroupRule;
 47  
 import org.kuali.rice.kim.rule.ui.AddPersonDelegationMemberRule;
 48  
 import org.kuali.rice.kim.rule.ui.AddPersonDocumentRoleQualifierRule;
 49  
 import org.kuali.rice.kim.rule.ui.AddRoleRule;
 50  
 import org.kuali.rice.kim.rules.ui.PersonDocumentDelegationMemberRule;
 51  
 import org.kuali.rice.kim.rules.ui.PersonDocumentGroupRule;
 52  
 import org.kuali.rice.kim.rules.ui.PersonDocumentRoleRule;
 53  
 import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
 54  
 import org.kuali.rice.kim.service.KIMServiceLocatorWeb;
 55  
 import org.kuali.rice.kim.service.UiDocumentService;
 56  
 import org.kuali.rice.kim.util.KIMPropertyConstants;
 57  
 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
 58  
 import org.kuali.rice.krad.document.Document;
 59  
 import org.kuali.rice.krad.rules.TransactionalDocumentRuleBase;
 60  
 import org.kuali.rice.krad.service.BusinessObjectService;
 61  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 62  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 63  
 import org.kuali.rice.krad.util.GlobalVariables;
 64  
 import org.kuali.rice.krad.util.KRADConstants;
 65  
 import org.kuali.rice.krad.util.ObjectUtils;
 66  
 
 67  
 import java.sql.Timestamp;
 68  
 import java.util.ArrayList;
 69  
 import java.util.HashMap;
 70  
 import java.util.HashSet;
 71  
 import java.util.List;
 72  
 import java.util.Map;
 73  
 import java.util.Set;
 74  
 
 75  
 /**
 76  
  * This is a description of what this class does - shyu don't forget to fill this in.
 77  
  *
 78  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 79  
  *
 80  
  */
 81  0
 public class IdentityManagementPersonDocumentRule extends TransactionalDocumentRuleBase implements AddGroupRule, AddRoleRule, AddPersonDocumentRoleQualifierRule, AddPersonDelegationMemberRule {
 82  
 
 83  
 //        protected static final Logger LOG = Logger.getLogger( IdentityManagementPersonDocumentRule.class );
 84  
 
 85  
         protected AddGroupRule addGroupRule;
 86  
         protected AddRoleRule  addRoleRule;
 87  
         protected AddPersonDelegationMemberRule addPersonDelegationMemberRule;
 88  
         protected IdentityManagementKimDocumentAuthorizer authorizer;
 89  
         protected BusinessObjectService businessObjectService;
 90  
         protected IdentityService identityService;
 91  
         protected RoleService roleService;
 92  
         protected UiDocumentService uiDocumentService;
 93  0
         protected Class<? extends AddGroupRule> addGroupRuleClass = PersonDocumentGroupRule.class;
 94  0
         protected Class<? extends AddRoleRule> addRoleRuleClass = PersonDocumentRoleRule.class;
 95  0
         protected Class<? extends AddPersonDelegationMemberRule> addPersonDelegationMemberRuleClass = PersonDocumentDelegationMemberRule.class;
 96  
 
 97  0
         protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper();
 98  
 
 99  
     @Override
 100  
     protected boolean processCustomSaveDocumentBusinessRules(Document document) {
 101  0
         if (!(document instanceof IdentityManagementPersonDocument)) {
 102  0
             return false;
 103  
         }
 104  
 
 105  0
         IdentityManagementPersonDocument personDoc = (IdentityManagementPersonDocument)document;
 106  0
         boolean valid = true;
 107  
 
 108  0
         GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
 109  
 
 110  
         //KRADServiceLocatorInternal.getDictionaryValidationService().validateDocument(document);
 111  0
         getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(document, getMaxDictionaryValidationDepth(), true, false);
 112  0
         valid &= validDuplicatePrincipalName(personDoc);
 113  0
         EntityDefault origEntity = getIdentityService().getEntityDefault(personDoc.getEntityId());
 114  0
         boolean isCreatingNew = origEntity==null?true:false;
 115  0
         if(getUIDocumentService().canModifyEntity(GlobalVariables.getUserSession().getPrincipalId(), personDoc.getPrincipalId()) || isCreatingNew) {
 116  0
                 valid &= validateEntityInformation(isCreatingNew, personDoc);
 117  
         }
 118  
         // kimtypeservice.validateAttributes is not working yet.
 119  0
         valid &= validateRoleQualifier (personDoc.getRoles());
 120  0
         valid &= validateDelegationMemberRoleQualifier(personDoc.getDelegationMembers());
 121  0
         if (StringUtils.isNotBlank(personDoc.getPrincipalName())) {
 122  0
                 valid &= doesPrincipalNameExist (personDoc.getPrincipalName(), personDoc.getPrincipalId());
 123  
         }
 124  
 
 125  0
         valid &= validActiveDatesForRole (personDoc.getRoles());
 126  0
         valid &= validActiveDatesForGroup (personDoc.getGroups());
 127  0
         valid &= validActiveDatesForDelegations (personDoc.getDelegationMembers());
 128  
 
 129  
 
 130  
         // all failed at this point.
 131  
 //        valid &= checkUnassignableRoles(personDoc);
 132  
 //        valid &= checkUnpopulatableGroups(personDoc);
 133  
 
 134  0
         GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
 135  
 
 136  0
         return valid;
 137  
     }
 138  
 
 139  
     protected boolean validateEntityInformation(boolean isCreatingNew, IdentityManagementPersonDocument personDoc){
 140  0
         boolean valid = true;
 141  0
         boolean canOverridePrivacyPreferences = getUIDocumentService().canOverrideEntityPrivacyPreferences(GlobalVariables.getUserSession().getPrincipalId(), personDoc.getPrincipalId());
 142  0
         valid &= checkMultipleDefault (personDoc.getAffiliations(), "affiliations");
 143  0
         if(isCreatingNew || canOverridePrivacyPreferences || !personDoc.getPrivacy().isSuppressName()) {
 144  0
                 valid &= checkMultipleDefault (personDoc.getNames(), "names");
 145  
         }
 146  0
         if(isCreatingNew || canOverridePrivacyPreferences || !personDoc.getPrivacy().isSuppressAddress()) {
 147  0
                 valid &= checkMultipleDefault (personDoc.getAddrs(), "addrs");
 148  
         }
 149  0
         if(isCreatingNew || canOverridePrivacyPreferences || !personDoc.getPrivacy().isSuppressPhone()) {
 150  0
                 valid &= checkMultipleDefault (personDoc.getPhones(), "phones");
 151  
         }
 152  0
         if(isCreatingNew || canOverridePrivacyPreferences || !personDoc.getPrivacy().isSuppressEmail()) {
 153  0
                 valid &= checkMultipleDefault (personDoc.getEmails(), "emails");
 154  
         }
 155  0
         valid &= checkPrimaryEmploymentInfo (personDoc.getAffiliations());
 156  0
         valid &= validEmployeeIDForAffiliation(personDoc.getAffiliations());
 157  0
         valid &= checkAffiliationTypeChange (personDoc.getAffiliations());
 158  0
         valid &= checkUniqueAffiliationTypePerCampus(personDoc.getAffiliations());
 159  0
             return valid;
 160  
     }
 161  
 
 162  
     @SuppressWarnings("unchecked")
 163  
         protected boolean validDuplicatePrincipalName(IdentityManagementPersonDocument personDoc){
 164  0
             Map<String, String> criteria = new HashMap<String, String>();
 165  0
             criteria.put("principalName", personDoc.getPrincipalName());
 166  0
             List<PrincipalBo> prncplImpls = (List<PrincipalBo>)getBusinessObjectService().findMatching(PrincipalBo.class, criteria);
 167  0
             boolean rulePassed = true;
 168  0
             if(prncplImpls!=null && prncplImpls.size()>0){
 169  0
                     if(prncplImpls.size()==1 && prncplImpls.get(0).getPrincipalId().equals(personDoc.getPrincipalId())) {
 170  0
                             rulePassed = true;
 171  
             }
 172  
                     else{
 173  0
                             GlobalVariables.getMessageMap().putError("document.principalName",
 174  
                                             RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Principal Name"});
 175  0
                             rulePassed = false;
 176  
                     }
 177  
             }
 178  0
             return rulePassed;
 179  
     }
 180  
 
 181  
         protected boolean checkUnassignableRoles(IdentityManagementPersonDocument document) {
 182  0
                 boolean valid = true;
 183  0
             Map<String,Set<String>> unassignableRoles = getAuthorizer( document ).getUnassignableRoles(document, GlobalVariables.getUserSession().getPerson());
 184  0
         for (String namespaceCode : unassignableRoles.keySet()) {
 185  0
                 for (String roleName : unassignableRoles.get(namespaceCode)) {
 186  0
                         int i = 0;
 187  0
                         for (PersonDocumentRole role : document.getRoles()) {
 188  0
                                 if (role.isEditable() && namespaceCode.endsWith(role.getNamespaceCode()) && roleName.equals(role.getRoleName())) {
 189  0
                                         GlobalVariables.getMessageMap().putError("roles["+i+"].roleId", RiceKeyConstants.ERROR_ASSIGN_ROLE, new String[] {namespaceCode, roleName});
 190  0
                                 valid = false;
 191  
                                 }
 192  0
                                 i++;
 193  
                         }
 194  0
                 }
 195  
         }
 196  0
         return valid;
 197  
         }
 198  
 
 199  
         protected boolean checkUnpopulatableGroups(IdentityManagementPersonDocument document) {
 200  0
                 boolean valid = true;
 201  0
             Map<String,Set<String>> unpopulatableGroups = getAuthorizer( document ).getUnpopulateableGroups(document, GlobalVariables.getUserSession().getPerson());
 202  0
         for (String namespaceCode : unpopulatableGroups.keySet()) {
 203  0
                 for (String groupName : unpopulatableGroups.get(namespaceCode)) {
 204  0
                         int i = 0;
 205  0
                         for (PersonDocumentGroup group : document.getGroups()) {
 206  0
                                 if ( (group.getNamespaceCode() != null && namespaceCode.endsWith(group.getNamespaceCode())) && (group.getGroupName() != null && groupName.equals(group.getGroupName()))) {
 207  0
                                         GlobalVariables.getMessageMap().putError("groups["+i+"].groupId", RiceKeyConstants.ERROR_POPULATE_GROUP, new String[] {namespaceCode, groupName});
 208  
                                 }
 209  0
                                 i++;
 210  
                         }
 211  0
                 }
 212  0
                 valid = false;
 213  
         }
 214  0
         return valid;
 215  
         }
 216  
 
 217  
     @Override
 218  
         protected boolean processCustomRouteDocumentBusinessRules(Document document) {
 219  0
                 super.processCustomRouteDocumentBusinessRules(document);
 220  0
         IdentityManagementPersonDocument personDoc = (IdentityManagementPersonDocument)document;
 221  0
         boolean valid = true;
 222  0
         GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
 223  0
         valid &= validateAffiliationAndName( personDoc );
 224  0
         valid &= checkAffiliationEithOneEMpInfo (personDoc.getAffiliations());
 225  0
         GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
 226  
 
 227  0
         return valid;
 228  
         }
 229  
 
 230  
 
 231  
         protected boolean checkMultipleDefault (List <? extends PersonDocumentBoDefaultBase> boList, String listName) {
 232  0
             boolean valid = true;
 233  0
             boolean isDefaultSet = false;
 234  0
             int i = 0;
 235  0
             for (PersonDocumentBoDefaultBase item : boList) {
 236  0
                      if (item.isDflt()) {
 237  0
                              if (isDefaultSet) {
 238  0
                                      GlobalVariables.getMessageMap().putError(listName+"[" + i + "].dflt",RiceKeyConstants.ERROR_MULTIPLE_DEFAULT_SELETION);
 239  0
                                      valid = false;
 240  
                              } else {
 241  0
                                      isDefaultSet = true;
 242  
                              }
 243  
                      }
 244  0
                      i++;
 245  
             }
 246  0
             if (!boList.isEmpty() && !isDefaultSet) {
 247  0
                                 GlobalVariables.getMessageMap().putError(listName+"[0].dflt",RiceKeyConstants.ERROR_NO_DEFAULT_SELETION);
 248  
             }
 249  0
             return valid;
 250  
     }
 251  
 
 252  
     protected boolean checkPrimaryEmploymentInfo (List <PersonDocumentAffiliation> affiliations) {
 253  0
             boolean valid = true;
 254  0
             int i = 0;
 255  0
             int firstAfflnCounter = -1;
 256  0
             boolean isPrimarySet = false;
 257  0
             for (PersonDocumentAffiliation affiliation : affiliations) {
 258  0
                     int j = 0;
 259  0
                     for (PersonDocumentEmploymentInfo empInfo : affiliation.getEmpInfos()) {
 260  0
                              if(firstAfflnCounter==-1) {
 261  0
                                      firstAfflnCounter = i;
 262  
                 }
 263  0
                             if (empInfo.isPrimary()) {
 264  0
                                      if (isPrimarySet) {
 265  
                                              // primary per principal or primary per affiliation ?
 266  0
                                              GlobalVariables.getMessageMap().putError("affiliations[" + i + "].empInfos["+ j +"].primary",RiceKeyConstants.ERROR_MULTIPLE_PRIMARY_EMPLOYMENT);
 267  0
                                              valid = false;
 268  
                                      } else {
 269  0
                                              isPrimarySet = true;
 270  
                                      }
 271  0
                                      j++;
 272  
                              }
 273  
                     }
 274  0
                      i++;
 275  0
             }
 276  0
             if(!isPrimarySet && firstAfflnCounter!=-1){
 277  0
                     GlobalVariables.getMessageMap().putError("affiliations[" + firstAfflnCounter + "].empInfos[0].primary",RiceKeyConstants.ERROR_NO_PRIMARY_EMPLOYMENT);
 278  0
                     valid = false;
 279  
             }
 280  0
             return valid;
 281  
     }
 282  
 
 283  
     protected boolean checkAffiliationTypeChange (List <PersonDocumentAffiliation> affiliations) {
 284  0
             boolean valid = true;
 285  0
             int i = 0;
 286  0
             for (PersonDocumentAffiliation affiliation : affiliations) {
 287  0
                     if (affiliation.getAffiliationType() != null && !affiliation.getAffiliationTypeCode().equals(affiliation.getAffiliationType().getCode())) {
 288  0
                             PersonDocumentAffiliation copiedAffiliation = (PersonDocumentAffiliation)ObjectUtils.deepCopy(affiliation);
 289  0
                             copiedAffiliation.refreshReferenceObject("affiliationType");
 290  0
                             if (!copiedAffiliation.getAffiliationType().isEmploymentAffiliationType() && affiliation.getAffiliationType().isEmploymentAffiliationType() && !copiedAffiliation.getEmpInfos().isEmpty()) {
 291  0
                                      GlobalVariables.getMessageMap().putError("affiliations[" + i + "].affiliationTypeCode",RiceKeyConstants.ERROR_NOT_EMPLOYMENT_AFFILIATION_TYPE,new String[] {affiliation.getAffiliationType().getName(), copiedAffiliation.getAffiliationType().getName()});
 292  0
                                      valid = false;
 293  
                             }
 294  
                     }
 295  0
                 i++;
 296  
             }
 297  0
             return valid;
 298  
     }
 299  
 
 300  
     protected boolean validEmployeeIDForAffiliation(List <PersonDocumentAffiliation> affiliations) {
 301  0
             boolean valid = true;
 302  0
             int i = 0;
 303  0
             int j = 0;
 304  0
             for(PersonDocumentAffiliation affiliation : affiliations) {
 305  0
                     if(affiliation.getAffiliationType() != null && affiliation.getAffiliationType().isEmploymentAffiliationType()){
 306  0
                             if(affiliation.getEmpInfos()!=null){
 307  0
                                 j = 0;
 308  0
                                 for (PersonDocumentEmploymentInfo empInfo : affiliation.getEmpInfos()) {
 309  0
                                         if (StringUtils.isEmpty(empInfo.getEmployeeId())) {
 310  0
                                                         GlobalVariables.getMessageMap().putError("affiliations[" + i + "].empInfos["+ j +"].employeeId", RiceKeyConstants.ERROR_REQUIRED_CONDITIONALLY, new String[] {"Employee ID", "an employee"});
 311  0
                                                         valid = false;
 312  0
                                                  j++;
 313  
                                          }
 314  
                                 }
 315  
                             }
 316  
                     }
 317  0
                 i++;
 318  
             }
 319  0
             return valid;
 320  
     }
 321  
 
 322  
     protected boolean isPersonAnEmployee(List<PersonDocumentAffiliation> affiliations){
 323  0
             boolean isEmployee = false;
 324  0
             for (PersonDocumentAffiliation affiliation : affiliations){
 325  0
                     if (affiliation.getAffiliationType() != null && affiliation.getAffiliationType().isEmploymentAffiliationType()){
 326  0
                             isEmployee = true;
 327  0
                             break;
 328  
                     }
 329  
             }
 330  0
             return isEmployee;
 331  
     }
 332  
 
 333  
     protected boolean checkUniqueAffiliationTypePerCampus (List <PersonDocumentAffiliation> affiliations) {
 334  0
             boolean valid = true;
 335  0
             int i = 0;
 336  0
             for (PersonDocumentAffiliation affiliation : affiliations) {
 337  0
                     int j = 0;
 338  0
                 for (PersonDocumentAffiliation affiliation1 : affiliations) {
 339  0
                             if (j > i && affiliation.getAffiliationTypeCode() .equals(affiliation1.getAffiliationTypeCode()) && affiliation.getCampusCode().equals(affiliation1.getCampusCode())) {
 340  0
                                              GlobalVariables.getMessageMap().putError("affiliations[" + j + "].affiliationTypeCode",RiceKeyConstants.ERROR_NOT_UNIQUE_AFFILIATION_TYPE_PER_CAMPUE, affiliation.getAffiliationType().getName());
 341  0
                                              valid = false;
 342  
                             }
 343  0
                             j++;
 344  
                 }
 345  0
                 i++;
 346  0
             }
 347  0
             return valid;
 348  
     }
 349  
 
 350  
     protected boolean checkAffiliationEithOneEMpInfo (List <PersonDocumentAffiliation> affiliations) {
 351  0
             boolean valid = true;
 352  0
             int i = 0;
 353  0
             for (PersonDocumentAffiliation affiliation : affiliations) {
 354  0
                             if (affiliation.getAffiliationType() .isEmploymentAffiliationType() && affiliation.getEmpInfos().isEmpty()) {
 355  0
                                              GlobalVariables.getMessageMap().putError("affiliations[" + i + "].affiliationTypeCode",RiceKeyConstants.ERROR_ONE_ITEM_REQUIRED, "Employment Information");
 356  0
                                              valid = false;
 357  
                             }
 358  0
                 i++;
 359  
             }
 360  0
             return valid;
 361  
     }
 362  
 
 363  
     /*
 364  
      * Verify at least one affiliation and one default name
 365  
      */
 366  
     protected boolean validateAffiliationAndName(IdentityManagementPersonDocument personDoc) {
 367  0
             boolean valid = true;
 368  0
             if (personDoc.getAffiliations().isEmpty()) {
 369  0
                      GlobalVariables.getMessageMap().putError("affiliations[0]",RiceKeyConstants.ERROR_ONE_ITEM_REQUIRED, "affiliation");
 370  0
                      valid = false;
 371  
             }
 372  0
             if (personDoc.getNames().isEmpty()) {
 373  0
                      GlobalVariables.getMessageMap().putError("names[0]",RiceKeyConstants.ERROR_ONE_ITEM_REQUIRED, "name");
 374  0
                      valid = false;
 375  
             } else{
 376  0
                 boolean activeExists = false;
 377  0
                 for(PersonDocumentName name: personDoc.getNames()){
 378  0
                         if(name.isActive()){
 379  0
                                 activeExists = true;
 380  
                                }
 381  
                 }
 382  0
                 if(!activeExists){
 383  0
                         GlobalVariables.getMessageMap().putError("names[0]", RiceKeyConstants.ERROR_ONE_ACTIVE_ITEM_REQUIRED, "name");
 384  0
                              valid = false;
 385  
                 }
 386  0
                 return valid;
 387  
 
 388  
             }
 389  0
             return valid;
 390  
     }
 391  
 
 392  
     protected boolean doesPrincipalNameExist (String principalName, String principalId) {
 393  0
             Principal principal = getIdentityService().getPrincipalByPrincipalName(principalName);
 394  0
             if (principal != null && (StringUtils.isBlank(principalId) || !principal.getPrincipalId().equals(principalId))) {
 395  0
                 GlobalVariables.getMessageMap().putError(KIMPropertyConstants.Person.PRINCIPAL_NAME,RiceKeyConstants.ERROR_EXIST_PRINCIPAL_NAME, principalName);
 396  0
                         return false;
 397  
             }
 398  0
             return true;
 399  
     }
 400  
 
 401  
     protected boolean validateRoleQualifier( List<PersonDocumentRole> roles ) {
 402  0
                 Map<String, String> validationErrors = new HashMap<String, String>();
 403  0
         GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
 404  0
         int i = 0;
 405  0
             for(PersonDocumentRole role : roles ) {
 406  0
                     KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(KimTypeBo.to(role.getKimRoleType()));
 407  0
                 if(CollectionUtils.isEmpty(role.getRolePrncpls()) && !role.getDefinitions().isEmpty()){
 408  0
                         KimType kimTypeInfo = KimApiServiceLocator.getKimTypeInfoService().getKimType(role.getKimRoleType().getId());
 409  0
                         Map<String, String> blankQualifiers = attributeValidationHelper.getBlankValueQualifiersMap(kimTypeInfo.getAttributeDefinitions());
 410  0
                         Map<String, String> localErrors = kimTypeService.validateAttributes(
 411  
                                 role.getKimRoleType().getId(), blankQualifiers);
 412  0
                         if(localErrors!=null && !localErrors.isEmpty()){
 413  0
                                 GlobalVariables.getMessageMap().putError("document.roles["+i+"].newRolePrncpl.qualifiers[0].attrVal",
 414  
                                                 RiceKeyConstants.ERROR_ONE_ITEM_REQUIRED, "Role Qualifier");
 415  0
                                 return false;
 416  
                         }
 417  
                 }
 418  
 
 419  0
                 final AttributeDefinitionMap attributeDefinitions = role.getDefinitions();
 420  0
                 final Set<String> uniqueQualifierAttributes = findUniqueQualificationAttributes(role, attributeDefinitions);
 421  
 
 422  0
                 if ( kimTypeService != null ) {
 423  0
                         int j = 0;
 424  0
                         for ( KimDocumentRoleMember rolePrincipal : role.getRolePrncpls() ) {
 425  0
                                 Map<String, String> localErrors = kimTypeService.validateAttributes( role.getKimRoleType().getId(), attributeValidationHelper.convertQualifiersToMap( rolePrincipal.getQualifiers() ) );
 426  0
                                 validationErrors.putAll( attributeValidationHelper.convertErrors(
 427  
                             "roles[" + i + "].rolePrncpls[" + j + "]",
 428  
                             attributeValidationHelper.convertQualifiersToAttrIdxMap(rolePrincipal.getQualifiers()),
 429  
                             localErrors));
 430  
 
 431  0
                                 if (uniqueQualifierAttributes.size() > 0) {
 432  0
                                         validateUniquePersonRoleQualifiersUniqueForMembership(role, rolePrincipal, j, uniqueQualifierAttributes, i, validationErrors);
 433  
                                 }
 434  
 
 435  0
                                 j++;
 436  0
                         }
 437  
                 }
 438  0
                 i++;
 439  0
             }
 440  0
         GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
 441  0
             if (validationErrors.isEmpty()) {
 442  0
                     return true;
 443  
             } else {
 444  0
                     attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors);
 445  0
                     return false;
 446  
             }
 447  
     }
 448  
 
 449  
     /**
 450  
      * Checks all the qualifiers for the given membership, so that all qualifiers which should be unique are guaranteed to be unique
 451  
      *
 452  
      * @param membership the membership to check
 453  
      * @param attributeDefinitions the Map of attribute definitions used by the role
 454  
      * @param roleIndex the index of the role on the document (for error reporting purposes)
 455  
      * @param memberIndex the index of the person's membership in the role (for error reporting purposes)
 456  
      * @return true if all unique values are indeed unique, false otherwise
 457  
      */
 458  
     protected boolean validateUniquePersonRoleQualifiersUniqueForMembership(PersonDocumentRole role, KimDocumentRoleMember membershipToCheck, int membershipToCheckIndex, Set<String> uniqueQualifierAttributes, int roleIndex, Map<String, String> validationErrors) {
 459  0
             boolean foundError = false;
 460  0
             int count = 0;
 461  
 
 462  0
             for (KimDocumentRoleMember membership : role.getRolePrncpls()) {
 463  0
                     if (membershipToCheckIndex != count) {
 464  0
                             if (sameMembershipQualifications(membershipToCheck, membership, uniqueQualifierAttributes)) {
 465  0
                                     foundError = true;
 466  
 
 467  0
                                     int qualifierCount = 0;
 468  
 
 469  0
                                         for (KimDocumentRoleQualifier qualifier : membership.getQualifiers()) {
 470  0
                                                 if (qualifier != null && uniqueQualifierAttributes.contains(qualifier.getKimAttrDefnId())) {
 471  0
                                                         validationErrors.put("document.roles["+roleIndex+"].rolePrncpls["+membershipToCheckIndex+"].qualifiers["+qualifierCount+"].attrVal", RiceKeyConstants.ERROR_DOCUMENT_IDENTITY_MANAGEMENT_PERSON_QUALIFIER_VALUE_NOT_UNIQUE+":"+qualifier.getKimAttribute().getAttributeName()+";"+qualifier.getAttrVal());
 472  
                                                 }
 473  0
                                                 qualifierCount += 1;
 474  
                                         }
 475  
                             }
 476  
                     }
 477  
 
 478  0
                     count += 1;
 479  
             }
 480  0
             return foundError;
 481  
     }
 482  
 
 483  
     /**
 484  
      * Determines if two seperate memberships have the same qualifications
 485  
      * @param membershipA the first membership to check
 486  
      * @param membershipB the second membership to check
 487  
      * @param uniqueQualifierAttributes the set of qualifier attributes which need to be unique
 488  
      * @return true if equal, false if otherwise
 489  
      */
 490  
     protected boolean sameMembershipQualifications(KimDocumentRoleMember membershipA, KimDocumentRoleMember membershipB, Set<String> uniqueQualifierAttributes) {
 491  0
             boolean equalSoFar = true;
 492  0
             for (String uniqueQualifierAttributeDefinitionId : uniqueQualifierAttributes) {
 493  0
                     final KimDocumentRoleQualifier qualifierA = membershipA.getQualifier(uniqueQualifierAttributeDefinitionId);
 494  0
                     final KimDocumentRoleQualifier qualifierB = membershipB.getQualifier(uniqueQualifierAttributeDefinitionId);
 495  
 
 496  0
                     if (qualifierA != null && qualifierB != null) {
 497  0
                             equalSoFar &= (qualifierA.getAttrVal() == null && qualifierB.getAttrVal() == null) || (qualifierA.getAttrVal() == null || qualifierA.getAttrVal().equals(qualifierB.getAttrVal()));
 498  
                     }
 499  0
             }
 500  0
             return equalSoFar;
 501  
     }
 502  
 
 503  
     /**
 504  
      * Finds the set of unique qualification attributes for the given role
 505  
      *
 506  
      * @param role the role associated with this person
 507  
      * @param attributeDefinitions the Map of attribute definitions where we can find out if a KimAttribute is supposed to be unique
 508  
      * @return a Set of attribute definition ids for qualifications which are supposed to be unique
 509  
      */
 510  
     public Set<String> findUniqueQualificationAttributes(PersonDocumentRole role, AttributeDefinitionMap attributeDefinitions) {
 511  0
             Set<String> uniqueQualifications = new HashSet<String>();
 512  
 
 513  0
             if (role.getRolePrncpls() != null && role.getRolePrncpls().size() > 1) {
 514  0
                     final KimDocumentRoleMember membership = role.getRolePrncpls().get(0);
 515  0
                     for (KimDocumentRoleQualifier qualifier: membership.getQualifiers()) {
 516  0
                             if (qualifier != null && qualifier.getKimAttribute() != null && !StringUtils.isBlank(qualifier.getKimAttribute().getAttributeName())) {
 517  0
                                 final AttributeDefinition relatedDefinition = attributeDefinitions.getByAttributeName(qualifier.getKimAttribute().getAttributeName());
 518  
 
 519  0
                                 if (relatedDefinition != null && relatedDefinition.getUnique() != null && relatedDefinition.getUnique().booleanValue()) {
 520  0
                                         uniqueQualifications.add(qualifier.getKimAttrDefnId());
 521  
                                 }
 522  0
                             }
 523  
                     }
 524  
             }
 525  
 
 526  0
             return uniqueQualifications;
 527  
     }
 528  
 
 529  
     protected boolean validActiveDatesForRole (List<PersonDocumentRole> roles ) {
 530  0
             boolean valid = true;
 531  0
                 int i = 0;
 532  0
             for(PersonDocumentRole role : roles ) {
 533  0
                         int j = 0;
 534  0
                     for (KimDocumentRoleMember principal : role.getRolePrncpls()) {
 535  0
                             valid &= validateActiveDate("roles["+i+"].rolePrncpls["+j+"].activeToDate",principal.getActiveFromDate(), principal.getActiveToDate());
 536  0
                             j++;
 537  
                     }
 538  0
                     i++;
 539  0
             }
 540  0
             return valid;
 541  
     }
 542  
 
 543  
     protected boolean validActiveDatesForGroup (List<PersonDocumentGroup> groups ) {
 544  0
             boolean valid = true;
 545  0
                 int i = 0;
 546  0
             for(PersonDocumentGroup group : groups ) {
 547  0
                      valid &= validateActiveDate("groups["+i+"].activeToDate",group.getActiveFromDate(), group.getActiveToDate());
 548  0
                     i++;
 549  
             }
 550  0
             return valid;
 551  
     }
 552  
 
 553  
     protected boolean validActiveDatesForDelegations(List<RoleDocumentDelegationMember> delegationMembers) {
 554  0
             boolean valid = true;
 555  0
                 int i = 0;
 556  0
                 for(RoleDocumentDelegationMember delegationMember: delegationMembers){
 557  0
                      valid &= validateActiveDate("delegationMembers["+i+"].activeToDate", delegationMember.getActiveFromDate(), delegationMember.getActiveToDate());
 558  0
                     i++;
 559  
                 }
 560  0
             return valid;
 561  
     }
 562  
 
 563  
         protected boolean validateActiveDate(String errorPath, Timestamp activeFromDate, Timestamp activeToDate) {
 564  
                 // TODO : do not have detail bus rule yet, so just check this for now.
 565  0
                 boolean valid = true;
 566  0
                 if (activeFromDate != null && activeToDate !=null && activeToDate.before(activeFromDate)) {
 567  0
                 GlobalVariables.getMessageMap().putError(errorPath, RiceKeyConstants.ERROR_ACTIVE_TO_DATE_BEFORE_FROM_DATE);
 568  0
             valid = false;
 569  
                 }
 570  0
                 return valid;
 571  
         }
 572  
 
 573  
     public boolean processAddGroup(AddGroupEvent addGroupEvent) {
 574  0
         return getAddGroupRule().processAddGroup(addGroupEvent);
 575  
     }
 576  
 
 577  
     public boolean processAddRole(AddRoleEvent addRoleEvent) {
 578  0
         return getAddRoleRule().processAddRole(addRoleEvent);
 579  
     }
 580  
 
 581  
     public boolean processAddPersonDelegationMember(AddPersonDelegationMemberEvent addPersonDelegationMemberEvent){
 582  0
             return getAddPersonDelegationMemberRule().processAddPersonDelegationMember(addPersonDelegationMemberEvent);
 583  
     }
 584  
 
 585  
         public IdentityService getIdentityService() {
 586  0
                 if ( identityService == null ) {
 587  0
                         identityService = KimApiServiceLocator.getIdentityService();
 588  
                 }
 589  0
                 return identityService;
 590  
         }
 591  
 
 592  
         public RoleService getRoleService() {
 593  0
                 if ( roleService == null ) {
 594  0
                         roleService = KimApiServiceLocator.getRoleService();
 595  
                 }
 596  0
                 return roleService;
 597  
         }
 598  
 
 599  
         public UiDocumentService getUIDocumentService() {
 600  0
                 if ( uiDocumentService == null ) {
 601  0
                         uiDocumentService = KIMServiceLocatorInternal.getUiDocumentService();
 602  
                 }
 603  0
                 return uiDocumentService;
 604  
         }
 605  
 
 606  
         public IdentityManagementKimDocumentAuthorizer getAuthorizer(IdentityManagementPersonDocument document) {
 607  0
                 if ( authorizer == null ) {
 608  0
                         authorizer = (IdentityManagementKimDocumentAuthorizer) KRADServiceLocatorWeb.getDocumentHelperService().getDocumentAuthorizer(document);
 609  
                 }
 610  0
                 return authorizer;
 611  
         }
 612  
 
 613  
 
 614  
 
 615  
         /**
 616  
          * @return the addGroupRuleClass
 617  
          */
 618  
         public Class<? extends AddGroupRule> getAddGroupRuleClass() {
 619  0
                 return this.addGroupRuleClass;
 620  
         }
 621  
 
 622  
 
 623  
 
 624  
         /**
 625  
          * Can be overridden by subclasses to indicate the rule class to use when adding groups.
 626  
          *
 627  
          * @param addGroupRuleClass the addGroupRuleClass to set
 628  
          */
 629  
         public void setAddGroupRuleClass(Class<? extends AddGroupRule> addGroupRuleClass) {
 630  0
                 this.addGroupRuleClass = addGroupRuleClass;
 631  0
         }
 632  
 
 633  
 
 634  
 
 635  
         /**
 636  
          * @return the addRoleRuleClass
 637  
          */
 638  
         public Class<? extends AddRoleRule> getAddRoleRuleClass() {
 639  0
                 return this.addRoleRuleClass;
 640  
         }
 641  
 
 642  
 
 643  
 
 644  
         /**
 645  
          * Can be overridden by subclasses to indicate the rule class to use when adding roles.
 646  
          *
 647  
          * @param addRoleRuleClass the addRoleRuleClass to set
 648  
          */
 649  
         public void setAddRoleRuleClass(Class<? extends AddRoleRule> addRoleRuleClass) {
 650  0
                 this.addRoleRuleClass = addRoleRuleClass;
 651  0
         }
 652  
 
 653  
 
 654  
 
 655  
         /**
 656  
          * @return the addGroupRule
 657  
          */
 658  
         public AddGroupRule getAddGroupRule() {
 659  0
                 if ( addGroupRule == null ) {
 660  
                         try {
 661  0
                                 addGroupRule = addGroupRuleClass.newInstance();
 662  0
                         } catch ( Exception ex ) {
 663  0
                                 throw new RuntimeException( "Unable to create AddGroupRule instance using class: " + addGroupRuleClass, ex );
 664  0
                         }
 665  
                 }
 666  0
                 return addGroupRule;
 667  
         }
 668  
 
 669  
 
 670  
 
 671  
         /**
 672  
          * @return the addRoleRule
 673  
          */
 674  
         public AddRoleRule getAddRoleRule() {
 675  0
                 if ( addRoleRule == null ) {
 676  
                         try {
 677  0
                                 addRoleRule = addRoleRuleClass.newInstance();
 678  0
                         } catch ( Exception ex ) {
 679  0
                                 throw new RuntimeException( "Unable to create AddRoleRule instance using class: " + addRoleRuleClass, ex );
 680  0
                         }
 681  
                 }
 682  0
                 return addRoleRule;
 683  
         }
 684  
 
 685  
         /**
 686  
          * @return the addRoleRule
 687  
          */
 688  
         public AddPersonDelegationMemberRule getAddPersonDelegationMemberRule() {
 689  0
                 if(addPersonDelegationMemberRule == null){
 690  
                         try {
 691  0
                                 addPersonDelegationMemberRule = addPersonDelegationMemberRuleClass.newInstance();
 692  0
                         } catch ( Exception ex ) {
 693  0
                                 throw new RuntimeException( "Unable to create AddPersonDelegationMemberRuleClass instance using class: " + addPersonDelegationMemberRuleClass, ex );
 694  0
                         }
 695  
                 }
 696  0
                 return addPersonDelegationMemberRule;
 697  
         }
 698  
 
 699  
         /**
 700  
          * @return the businessObjectService
 701  
          */
 702  
         public BusinessObjectService getBusinessObjectService() {
 703  0
                 if ( businessObjectService == null ) {
 704  0
                         businessObjectService = KRADServiceLocator.getBusinessObjectService();
 705  
                 }
 706  0
                 return businessObjectService;
 707  
         }
 708  
 
 709  
         public boolean processAddPersonDocumentRoleQualifier(IdentityManagementPersonDocument document, PersonDocumentRole role, KimDocumentRoleMember kimDocumentRoleMember, int selectedRoleIdx) {
 710  0
                 boolean dateValidationSuccess = validateActiveDate("document.roles[" + selectedRoleIdx + "].newRolePrncpl.activeFromDate", kimDocumentRoleMember.getActiveFromDate(), kimDocumentRoleMember.getActiveToDate());
 711  0
                 String errorPath = "roles[" + selectedRoleIdx + "].newRolePrncpl";
 712  0
                 Map<String, String> validationErrors = new HashMap<String, String>();
 713  0
         GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
 714  0
         KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(KimTypeBo.to(role.getKimRoleType()));
 715  
 
 716  
         boolean attributesUnique;
 717  
                 Map<String, String> errorsAttributesAgainstExisting;
 718  0
             int i = 0;
 719  0
             boolean rulePassed = true;
 720  0
             Map<String, String> newMemberQualifiers = attributeValidationHelper.convertQualifiersToMap(kimDocumentRoleMember.getQualifiers());
 721  
             Map<String, String> oldMemberQualifiers;
 722  0
             List<String> roleIds = new ArrayList<String>();
 723  0
             roleIds.add(role.getRoleId());
 724  0
             for(KimDocumentRoleMember member: role.getRolePrncpls()){
 725  0
                     oldMemberQualifiers = member.getQualifierAsMap();
 726  0
                     errorsAttributesAgainstExisting = kimTypeService.validateAttributesAgainstExisting(
 727  
                                     role.getKimRoleType().getId(), newMemberQualifiers, oldMemberQualifiers);
 728  0
                     validationErrors.putAll(
 729  
                                         attributeValidationHelper.convertErrors(errorPath, attributeValidationHelper
 730  
                             .convertQualifiersToAttrIdxMap(kimDocumentRoleMember.getQualifiers()),
 731  
                             errorsAttributesAgainstExisting));
 732  
 
 733  0
                     attributesUnique = kimTypeService.validateUniqueAttributes(
 734  
                                     role.getKimRoleType().getId(), newMemberQualifiers, oldMemberQualifiers);
 735  0
                     if (!attributesUnique){
 736  0
                     GlobalVariables.getMessageMap().putError("document."+errorPath+".qualifiers[0].attrVal", RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Role Qualifier"});
 737  0
                     return false;
 738  
                     }
 739  0
                     i++;
 740  
             }
 741  
 
 742  0
         if ( kimTypeService != null ) {
 743  0
                 Map<String, String> localErrors = kimTypeService.validateAttributes( role.getKimRoleType().getId(), newMemberQualifiers );
 744  0
                 validationErrors.putAll( attributeValidationHelper.convertErrors(errorPath,
 745  
                     attributeValidationHelper.convertQualifiersToAttrIdxMap(kimDocumentRoleMember.getQualifiers()),
 746  
                     localErrors));
 747  
         }
 748  
 
 749  0
         GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
 750  0
             if (validationErrors.isEmpty()) {
 751  0
                     rulePassed = dateValidationSuccess;
 752  
             } else {
 753  0
                     attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors);
 754  0
                     rulePassed = false;
 755  
             }
 756  0
             return rulePassed;
 757  
         }
 758  
 
 759  
     protected boolean validateDelegationMemberRoleQualifier(List<RoleDocumentDelegationMember> delegationMembers){
 760  0
                 Map<String, String> validationErrors = new HashMap<String, String>();
 761  
                 boolean valid;
 762  0
                 int memberCounter = 0;
 763  
                 Map<String, String> errorsTemp;
 764  
                 Map<String, String> mapToValidate;
 765  
         KimTypeService kimTypeService;
 766  0
         GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
 767  
         RoleMemberBo roleMember;
 768  
         String errorPath;
 769  
         ArrayList<String> roleIds;
 770  
         KimType kimType;
 771  0
                 for(RoleDocumentDelegationMember delegationMember: delegationMembers) {
 772  0
                         kimType = KimTypeBo.to(delegationMember.getRoleBo().getKimRoleType());
 773  0
                         kimTypeService = KIMServiceLocatorWeb.getKimTypeService(kimType);
 774  0
                         roleIds = new ArrayList<String>();
 775  0
                         roleIds.add(delegationMember.getRoleBo().getId());
 776  0
                         errorPath = "delegationMembers["+memberCounter+"]";
 777  0
                         mapToValidate = attributeValidationHelper.convertQualifiersToMap(delegationMember.getQualifiers());
 778  0
                         errorsTemp = kimTypeService.validateAttributes(kimType.getId(), mapToValidate);
 779  0
                         validationErrors.putAll(
 780  
                                         attributeValidationHelper.convertErrors(errorPath,
 781  
                             attributeValidationHelper.convertQualifiersToAttrIdxMap(delegationMember.getQualifiers()),
 782  
                             errorsTemp));
 783  
 
 784  0
                         roleMember = getUIDocumentService().getRoleMember(delegationMember.getRoleMemberId());
 785  0
                         if(roleMember==null){
 786  0
                                 valid = false;
 787  0
                                 GlobalVariables.getMessageMap().putError("document."+errorPath, RiceKeyConstants.ERROR_DELEGATE_ROLE_MEMBER_ASSOCIATION, new String[]{});
 788  
                         } else{
 789  0
                                 errorsTemp = kimTypeService.validateUnmodifiableAttributes(
 790  
                                                                 kimType.getId(), roleMember.getAttributes(), mapToValidate);
 791  0
                                 validationErrors.putAll(
 792  
                                                 attributeValidationHelper.convertErrors(errorPath, attributeValidationHelper
 793  
                                 .convertQualifiersToAttrIdxMap(delegationMember.getQualifiers()), errorsTemp));
 794  
                         }
 795  0
                 memberCounter++;
 796  
             }
 797  0
                 GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
 798  0
             if (validationErrors.isEmpty()) {
 799  0
                     valid = true;
 800  
             } else {
 801  0
                     attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors);
 802  0
                     valid = false;
 803  
             }
 804  0
             return valid;
 805  
     }
 806  
 
 807  
 }