@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);
        }
      }
    }
  }
 /** Test the getPersonsInRole() service method. */
 @Test
 public void testGetPersonsInRole() throws Exception {
   ProposalDevelopmentDocument doc = createProposal("Proposal-9", "000001");
   PrincipalContract userChew = identityManagementService.getPrincipalByPrincipalName("chew");
   kraAuthService.addDocumentLevelRole(userChew.getPrincipalId(), RoleConstants.AGGREGATOR, doc);
   List<String> persons = kraAuthService.getPrincipalsInRole(RoleConstants.AGGREGATOR, doc);
   assertEquals(2, persons.size());
 }
 /** Test the hasRole() service method. */
 @Test
 public void testHasRole() throws Exception {
   ProposalDevelopmentDocument doc = createProposal("Proposal-7", "000001");
   PrincipalContract userChew = identityManagementService.getPrincipalByPrincipalName("chew");
   kraAuthService.addDocumentLevelRole(
       userChew.getPrincipalId(), RoleConstants.BUDGET_CREATOR, doc);
   assertTrue(
       kraAuthService.hasDocumentLevelRole(
           userChew.getPrincipalId(), RoleConstants.BUDGET_CREATOR, doc));
 }
  /**
   * Create the original set of Proposal Users for a new Proposal Development Document. The creator
   * the proposal is assigned to the AGGREGATOR role.
   */
  protected void initializeProposalUsers(ProposalDevelopmentDocument doc) {

    // Assign the creator of the proposal to the AGGREGATOR role.
    String userId = GlobalVariables.getUserSession().getPrincipalId();
    if (!kraAuthorizationService.hasDocumentLevelRole(userId, RoleConstants.AGGREGATOR, doc))
      kraAuthorizationService.addDocumentLevelRole(userId, RoleConstants.AGGREGATOR, doc);

    // Add the users defined in the role templates for the proposal's lead unit
    proposalRoleTemplateService.addUsers(doc);
  }
 /** Test the hasPermission() service method. */
 @Test
 public void testHasPermission() throws Exception {
   PrincipalContract userChew = identityManagementService.getPrincipalByPrincipalName("chew");
   ProposalDevelopmentDocument doc = createProposal("Proposal-6", "000001");
   kraAuthService.addDocumentLevelRole(
       userChew.getPrincipalId(), RoleConstants.NARRATIVE_WRITER, doc);
   assertTrue(
       kraAuthService.hasPermission(
           userChew.getPrincipalId(), doc, PermissionConstants.MODIFY_NARRATIVE));
   assertFalse(
       kraAuthService.hasPermission(
           userChew.getPrincipalId(), doc, PermissionConstants.MODIFY_BUDGET));
 }
  @Override
  protected List<KcPerson> getPersonsInRole(String roleName) {
    KcAuthorizationService kraAuthorizationService =
        KcServiceLocator.getService(KcAuthorizationService.class);
    KcPersonService kcPersonService = KcServiceLocator.getService(KcPersonService.class);
    List<String> users = kraAuthorizationService.getPrincipalsInRole(roleName, getProtocol());

    final List<KcPerson> persons = new ArrayList<KcPerson>();
    for (String userId : users) {
      KcPerson person = kcPersonService.getKcPersonByPersonId(userId);
      if (person != null && person.getActive()) {
        persons.add(person);
      }
    }

    return persons;
  }
 private void initializeProposalUsers(ProposalDevelopmentDocument doc) {
   // Assign the creator of the proposal to the AGGREGATOR role.
   String userId = GlobalVariables.getUserSession().getPrincipalId();
   kraAuthService.addDocumentLevelRole(userId, RoleConstants.AGGREGATOR, doc);
 }
 /**
  * Initialize the Authorizations for a new proposal. The initiator/creator is assigned the
  * Aggregator role.
  *
  * @param document the proposal development document
  */
 protected void initializeAuthorization(ProposalDevelopmentDocument document) {
   String userId = GlobalVariables.getUserSession().getPrincipalId();
   KcAuthorizationService kraAuthService =
       KcServiceLocator.getService(KcAuthorizationService.class);
   kraAuthService.addDocumentLevelRole(userId, RoleConstants.AGGREGATOR, document);
 }