@Override
  public void setPrincipalInvestigator(
      ProtocolPersonBase newPrincipalInvestigator, ProtocolBase protocol) {
    if (protocol != null) {
      ProtocolPerson currentPrincipalInvestigator =
          (ProtocolPerson) getPrincipalInvestigator(protocol.getProtocolPersons());

      if (newPrincipalInvestigator != null) {
        newPrincipalInvestigator.setProtocolPersonRoleId(getPrincipalInvestigatorRole());
        if (currentPrincipalInvestigator == null) {
          protocol.getProtocolPersons().add(newPrincipalInvestigator);
        } else if (!isDuplicatePerson(protocol.getProtocolPersons(), newPrincipalInvestigator)) {
          protocol.getProtocolPersons().remove(currentPrincipalInvestigator);
          protocol.getProtocolPersons().add(newPrincipalInvestigator);
        }

        // Assign the PI the APPROVER role if PI has a personId (for doc cancel).
        if (newPrincipalInvestigator.getPersonId() != null) {
          personEditableService.populateContactFieldsFromPersonId(newPrincipalInvestigator);
          KcAuthorizationService kraAuthService =
              KcServiceLocator.getService(KcAuthorizationService.class);
          kraAuthService.addDocumentLevelRole(
              newPrincipalInvestigator.getPersonId(), RoleConstants.PROTOCOL_APPROVER, protocol);
        } else {
          personEditableService.populateContactFieldsFromRolodexId(newPrincipalInvestigator);
        }
      }
    }
  }
  @Override
  public boolean hasDerivedRole(
      String principalId,
      List<String> groupIds,
      String namespaceCode,
      String roleName,
      Map<String, String> qualification) {
    validateRequiredAttributesAgainstReceived(qualification);

    String protocolNumber = qualification.get(KcKimAttributes.PROTOCOL);

    ProtocolBase protocol = getProtocol(protocolNumber);

    if (protocol != null && CollectionUtils.isNotEmpty(protocol.getProtocolPersons())) {
      for (ProtocolPersonBase person : protocol.getProtocolPersons()) {
        // Find protocol person that matches the principal id
        if (StringUtils.equals(principalId, person.getPersonId())) {
          if (StringUtils.equals(roleName, person.getProtocolPersonRoleId())) {
            return true;
          }
        }
      }
    }

    return false;
  }
  private List<String> getPersonnelIds(ProtocolBase protocol) {
    List<String> PersonnelIds = new ArrayList<String>();
    for (ProtocolPersonBase person : protocol.getProtocolPersons()) {
      if (StringUtils.isNotBlank(person.getPersonId())) {
        PersonnelIds.add(person.getPersonId());
      } else {
        PersonnelIds.add(person.getRolodexId().toString());
      }
    }

    return PersonnelIds;
  }
  @Override
  public List<RoleMembership> getRoleMembersFromDerivedRole(
      String namespaceCode, String roleName, Map<String, String> qualification) {
    validateRequiredAttributesAgainstReceived(qualification);

    List<RoleMembership> members = new ArrayList<RoleMembership>();

    String protocolNumber = qualification.get(KcKimAttributes.PROTOCOL);
    ProtocolBase protocol = getProtocol(protocolNumber);

    if (protocol != null && CollectionUtils.isNotEmpty(protocol.getProtocolPersons())) {
      for (ProtocolPersonBase person : protocol.getProtocolPersons()) {
        if (StringUtils.equals(person.getProtocolPersonRoleId(), roleName)
            && StringUtils.isNotBlank(person.getPerson().getPersonId())) {
          members.add(
              RoleMembership.Builder.create(
                      null, null, person.getPerson().getPersonId(), MemberType.PRINCIPAL, null)
                  .build());
        }
      }
    }

    return members;
  }