<commit_before>package org.sakaiproject.evaluation.tool.locators;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sakaiproject.evaluation.dao.EvalAdhocSupport;
import org.sakaiproject.evaluation.logic.EvalSettings;
import org.sakaiproject.evaluation.logic.externals.EvalExternalLogic;
import org.sakaiproject.evaluation.model.EvalAdhocGroup;
import org.sakaiproject.evaluation.model.EvalAdhocUser;
import org.sakaiproject.evaluation.utils.EvalUtils;

/**
 * This is not a true Bean Locator. It's primary purpose is
 * for handling the calls for adhoc groups from the
 * modify_adhoc_groups page.
 * 
 * @author <NAME>
 */
public class AdhocGroupsBean {
   private static Log log = LogFactory.getLog(AdhocGroupsBean.class);
   
   public static final String SAVED_NEW_ADHOCGROUP = "added-adhoc-group";
    
   private Long adhocGroupId;
   private String adhocGroupTitle;
   private String newAdhocGroupUsers;
   
   public List<String> acceptedInternalUsers = new ArrayList<String>();
   public List<String> acceptedAdhocUsers = new ArrayList<String>();
   public List<String> rejectedUsers = new ArrayList<String>();
   
   /*
    * Adds more users to an existing adhocgroup using the data entered with
    * adhocGroupId and newAdhocGroupUsers
    */
   public void addUsersToAdHocGroup() {
       String currentUserId = externalLogic.getCurrentUserId();
       EvalAdhocGroup group = evalAdhocSupport.getAdhocGroupById(new Long(adhocGroupId));
       adhocGroupId = group.getId();
       
       /*
        * You can only change the adhoc group if you are the owner.
        */
       if (!currentUserId.equals(group.getOwner())) {
          throw new SecurityException("Only EvalAdhocGroup owners can change their groups: " + group.getId() + " , " + currentUserId);
       }
   
   }
   
   /**
    * Adds a new Adhoc Group using the data entered into newAdhocGroupUsers.
    * 
    * @return
    */
   public String addNewAdHocGroup() {
      String currentUserId = externalLogic.getCurrentUserId();
      /*
       * At the moment we allow any registered user to create adhoc groups.
       */
      if (externalLogic.isUserAnonymous(currentUserId)) {
         throw new SecurityException("Anonymous users cannot create EvalAdhocGroups: " + currentUserId);
      }
      
      EvalAdhocGroup group = new EvalAdhocGroup(currentUserId, adhocGroupTitle);

      log.info("About to save Adhoc group: " + adhocGroupTitle);
      
      List<String> participants = new ArrayList<String>();
      checkAndAddToParticipantsList(newAdhocGroupUsers, participants);
     
      group.setParticipantIds(participants.toArray(new String[] {}));
      
      evalAdhocSupport.saveAdhocGroup(group);
      adhocGroupId = group.getId();

      log.info("Saved adhoc group: " + adhocGroupId);
	
      return SAVED_NEW_ADHOCGROUP;
   }
   
   /*
    * Adds folks to the participants list and does validation and stuff.
    */
   private void checkAndAddToParticipantsList(String data, List<String> participants) {
       String[] potentialMembers = data.split("\n");
       
       Boolean useAdhocusers = (Boolean) settings.get(EvalSettings.ENABLE_ADHOC_USERS);
       /*
        * As we go through the newline seperated list of folks we look for 2 things.
        * 1. Is the id for an existing user in the system?
        * 2. If Adhoc users are allowed, is this a valid email address?
        * 3. Otherwise add it to the garbage list.
        */
       for (String potentialId: potentialMembers) {
           if (externalLogic.getUserId(potentialId) != null) {
               participants.add(externalLogic.getUserId(potentialId));
               acceptedInternalUsers.add(potentialId);  
           }
           else if (useAdhocusers && EvalUtils.isValidEmail(potentialId)) {<commit_msg>EVALSYS-421 Trimmed the whitespace from the adhoc emails.<commit_after>package org.sakaiproject.evaluation.tool.locators;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sakaiproject.evaluation.dao.EvalAdhocSupport;
import org.sakaiproject.evaluation.logic.EvalSettings;
import org.sakaiproject.evaluation.logic.externals.EvalExternalLogic;
import org.sakaiproject.evaluation.model.EvalAdhocGroup;
import org.sakaiproject.evaluation.model.EvalAdhocUser;
import org.sakaiproject.evaluation.utils.EvalUtils;

/**
 * This is not a true Bean Locator. It's primary purpose is
 * for handling the calls for adhoc groups from the
 * modify_adhoc_groups page.
 * 
 * @author <NAME>
 */
public class AdhocGroupsBean {
   private static Log log = LogFactory.getLog(AdhocGroupsBean.class);
   
   public static final String SAVED_NEW_ADHOCGROUP = "added-adhoc-group";
    
   private Long adhocGroupId;
   private String adhocGroupTitle;
   private String newAdhocGroupUsers;
   
   public List<String> acceptedInternalUsers = new ArrayList<String>();
   public List<String> acceptedAdhocUsers = new ArrayList<String>();
   public List<String> rejectedUsers = new ArrayList<String>();
   
   /*
    * Adds more users to an existing adhocgroup using the data entered with
    * adhocGroupId and newAdhocGroupUsers
    */
   public void addUsersToAdHocGroup() {
       String currentUserId = externalLogic.getCurrentUserId();
       EvalAdhocGroup group = evalAdhocSupport.getAdhocGroupById(new Long(adhocGroupId));
       adhocGroupId = group.getId();
       
       /*
        * You can only change the adhoc group if you are the owner.
        */
       if (!currentUserId.equals(group.getOwner())) {
          throw new SecurityException("Only EvalAdhocGroup owners can change their groups: " + group.getId() + " , " + currentUserId);
       }
   
   }
   
   /**
    * Adds a new Adhoc Group using the data entered into newAdhocGroupUsers.
    * 
    * @return
    */
   public String addNewAdHocGroup() {
      String currentUserId = externalLogic.getCurrentUserId();
      /*
       * At the moment we allow any registered user to create adhoc groups.
       */
      if (externalLogic.isUserAnonymous(currentUserId)) {
         throw new SecurityException("Anonymous users cannot create EvalAdhocGroups: " + currentUserId);
      }
      
      EvalAdhocGroup group = new EvalAdhocGroup(currentUserId, adhocGroupTitle);

      log.info("About to save Adhoc group: " + adhocGroupTitle);
      
      List<String> participants = new ArrayList<String>();
      checkAndAddToParticipantsList(newAdhocGroupUsers, participants);
     
      group.setParticipantIds(participants.toArray(new String[] {}));
      
      evalAdhocSupport.saveAdhocGroup(group);
      adhocGroupId = group.getId();

      log.info("Saved adhoc group: " + adhocGroupId);
	
      return SAVED_NEW_ADHOCGROUP;
   }
   
   /*
    * Adds folks to the participants list and does validation and stuff.
    */
   private void checkAndAddToParticipantsList(String data, List<String> participants) {
       String[] potentialMembers = data.split("\n");
       
       Boolean useAdhocusers = (Boolean) settings.get(EvalSettings.ENABLE_ADHOC_USERS);
       /*
        * As we go through the newline seperated list of folks we look for 2 things.
        * 1. Is the id for an existing user in the system?
        * 2. If Adhoc users are allowed, is this a valid email address?
        * 3. Otherwise add it to the garbage list.
        */
       for (String next: potentialMembers) {
    	   String potentialId = next.trim();
           if (externalLogic.getUserId(potentialId) != null) {
               participants.add(externalLogic.getUserId(potentialId));
               acceptedInternalUsers.add(potentialId);  
           }
           else if (useAdhocusers && EvalUtils.isValidEmail(potentialId)) {
{'fraction_non_alphanumeric': 0.08782664117272147, 'fraction_numerical': 0.0014021669853409816, 'mean_word_length': 3.180074587107086, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3230990', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2232, 'n_words': 586}