src/org/ohmage/validator/UserCampaignValidators.java
public static Map<String, Set<Campaign.Role>> validateUserAndCampaignRole(
			final String userAndCampaignRoleList) throws ValidationException {
		
		LOGGER.info("Validating a list of user and class role pairs.");
		
		// If it's null or empty, return null.
		if(StringUtils.isEmptyOrWhitespaceOnly(userAndCampaignRoleList)) {
			return null;
		}
		
		// Create the resulting object which will initially be empty.
		Map<String, Set<Campaign.Role>> result = new HashMap<String, Set<Campaign.Role>>();
		// Split the parameterized value into its pairs.
		String[] userAndRoleArray = userAndCampaignRoleList.split(InputKeys.LIST_ITEM_SEPARATOR);
		
		// For each of these pairs,
		for(int i = 0; i < userAndRoleArray.length; i++) {
			String currUserAndRole = userAndRoleArray[i].trim();
			
			// If the pair is empty, i.e. there were two list item separators
			// in a row, then skip it.
			if((! StringUtils.isEmptyOrWhitespaceOnly(currUserAndRole)) && 
					(! currUserAndRole.equals(InputKeys.ENTITY_ROLE_SEPARATOR))) {
				String[] userAndRole = currUserAndRole.split(InputKeys.ENTITY_ROLE_SEPARATOR);
				
				// If the pair isn't actually a pair, fail with an invalid 
				// campaign role error.
				if(userAndRole.length != 2) {
					throw new ValidationException(
							ErrorCode.CAMPAIGN_INVALID_ROLE, 
							"The user campaign-role list is invalid: " + 
								currUserAndRole);
				}
				
				// Validate the actual elements in the pair.
				String username = UserValidators.validateUsername(userAndRole[0].trim());
				if(username == null) {
					throw new ValidationException(
							ErrorCode.USER_INVALID_USERNAME, 
							"The username in the username, campaign role pair is missing: " + 
								currUserAndRole);
				}
				
				Campaign.Role role = CampaignValidators.validateRole(userAndRole[1].trim());
				if(role == null) {
					throw new ValidationException(
							ErrorCode.CAMPAIGN_INVALID_ROLE, 
							"The campaign role in the username, campaign role pair is missing: " + 
								currUserAndRole);
				}
				
				// Add the role to the list of roles.
				Set<Campaign.Role> roles = result.get(username);
				if(roles == null) {
					roles = new HashSet<Campaign.Role>();
					result.put(username, roles);
				}
				roles.add(role);
			}
		}
		
		// If the list is empty, return null.
		if(result.size() == 0) {
			return null;
		}
		else {
			return result;
		}
	}
/**
	 * Validates a String that should be a list of username, campaign role 
	 * pairs. Because a user can have more than one role in a campaign, the
	 * result is a map of usernames to a set of campaign roles. It is a set 
	 * instead of a list to prevent duplicate roles for the same user.
	 * 
	 * @param userAndCampaignRoleList A String representing a list of username
	 * 								  and campaign role pairs. The pairs should
	 * 								  be separated by 
	 * 								  {@value org.ohmage.request.InputKeys#LIST_ITEM_SEPARATOR}s
	 * 								  and the username and campaign role should
	 * 								  be separated by
	 * 								  {@value org.ohmage.request.InputKeys#ENTITY_ROLE_SEPARATOR}s.
	 * 
	 * @return A Map of usernames to a list of the campaign roles associated 
	 * 		   with them from the string list or null if the string is null,
	 * 		   whitespace only, or contains only separators and no meaningful
	 * 		   information.
	 * 
	 * @throws ValidationException Thrown if the list is invalid, any of the
	 * 							   pairs are invalid, or either the username or
	 * 							   class role are invalid.
	 */
Validates a String that should be a list of username, campaign role
pairs. Because a user can have more than one role in a campaign, the
result is a map of usernames to a set of campaign roles. It is a set
instead of a list to prevent duplicate roles for the same user.

@param userAndCampaignRoleList A String representing a list of username
and campaign role pairs. The pairs should
be separated by
org.ohmage.request.InputKeys#LIST_ITEM_SEPARATORs
and the username and campaign role should
be separated by
org.ohmage.request.InputKeys#ENTITY_ROLE_SEPARATORs.

@return A Map of usernames to a list of the campaign roles associated
with them from the string list or null if the string is null,
whitespace only, or contains only separators and no meaningful
information.

@throws ValidationException Thrown if the list is invalid, any of the
pairs are invalid, or either the username or
class role are invalid.
Validates a String that should be a list of username campaign role pairs . Because a user can have more than one role in a campaign the result is a map of usernames to a set of campaign roles . It is a set instead of a list to prevent duplicate roles for the same user . @param userAndCampaignRoleList A String representing a list of username and campaign role pairs . The pairs should be separated by org . ohmage . request . InputKeys#LIST_ITEM_SEPARATORs and the username and campaign role should be separated by org . ohmage . request . InputKeys#ENTITY_ROLE_SEPARATORs . @return A Map of usernames to a list of the campaign roles associated with them from the string list or null if the string is null whitespace only or contains only separators and no meaningful information . @throws ValidationException Thrown if the list is invalid any of the pairs are invalid or either the username or class role are invalid .
public static Map<String, Set<Campaign.Role>> validateUserAndCampaignRole(
			final String userAndCampaignRoleList) throws ValidationException {
		LOGGER.info("Validating a list of user and class role pairs.");
		if(StringUtils.isEmptyOrWhitespaceOnly(userAndCampaignRoleList)) {
			return null;
		}
		Map<String, Set<Campaign.Role>> result = new HashMap<String, Set<Campaign.Role>>();
		String[] userAndRoleArray = userAndCampaignRoleList.split(InputKeys.LIST_ITEM_SEPARATOR);
		for(int i = 0; i < userAndRoleArray.length; i++) {
			String currUserAndRole = userAndRoleArray[i].trim();
			if((! StringUtils.isEmptyOrWhitespaceOnly(currUserAndRole)) && 
					(! currUserAndRole.equals(InputKeys.ENTITY_ROLE_SEPARATOR))) {
				String[] userAndRole = currUserAndRole.split(InputKeys.ENTITY_ROLE_SEPARATOR);
				if(userAndRole.length != 2) {
					throw new ValidationException(
							ErrorCode.CAMPAIGN_INVALID_ROLE, 
							"The user campaign-role list is invalid: " + 
								currUserAndRole);
				}
				String username = UserValidators.validateUsername(userAndRole[0].trim());
				if(username == null) {
					throw new ValidationException(
							ErrorCode.USER_INVALID_USERNAME, 
							"The username in the username, campaign role pair is missing: " + 
								currUserAndRole);
				}
				Campaign.Role role = CampaignValidators.validateRole(userAndRole[1].trim());
				if(role == null) {
					throw new ValidationException(
							ErrorCode.CAMPAIGN_INVALID_ROLE, 
							"The campaign role in the username, campaign role pair is missing: " + 
								currUserAndRole);
				}
				Set<Campaign.Role> roles = result.get(username);
				if(roles == null) {
					roles = new HashSet<Campaign.Role>();
					result.put(username, roles);
				}
				roles.add(role);
			}
		}
		if(result.size() == 0) {
			return null;
		}
		else {
			return result;
		}
	}
public static Map < String , Set < Campaign . Role > > validateUserAndCampaignRole ( final String userAndCampaignRoleList ) throws ValidationException { LOGGER . info ( "Validating a list of user and class role pairs." ) ; if ( StringUtils . isEmptyOrWhitespaceOnly ( userAndCampaignRoleList ) ) { return null ; } Map < String , Set < Campaign . Role > > result = new HashMap < String , Set < Campaign . Role > > ( ) ; String [ ] userAndRoleArray = userAndCampaignRoleList . split ( InputKeys . LIST_ITEM_SEPARATOR ) ; for ( int i = 0 ; i < userAndRoleArray . length ; i ++ ) { String currUserAndRole = userAndRoleArray [ i ] . trim ( ) ; if ( ( ! StringUtils . isEmptyOrWhitespaceOnly ( currUserAndRole ) ) && ( ! currUserAndRole . equals ( InputKeys . ENTITY_ROLE_SEPARATOR ) ) ) { String [ ] userAndRole = currUserAndRole . split ( InputKeys . ENTITY_ROLE_SEPARATOR ) ; if ( userAndRole . length != 2 ) { throw new ValidationException ( ErrorCode . CAMPAIGN_INVALID_ROLE , "The user campaign-role list is invalid: " + currUserAndRole ) ; } String username = UserValidators . validateUsername ( userAndRole [ 0 ] . trim ( ) ) ; if ( username == null ) { throw new ValidationException ( ErrorCode . USER_INVALID_USERNAME , "The username in the username, campaign role pair is missing: " + currUserAndRole ) ; } Campaign . Role role = CampaignValidators . validateRole ( userAndRole [ 1 ] . trim ( ) ) ; if ( role == null ) { throw new ValidationException ( ErrorCode . CAMPAIGN_INVALID_ROLE , "The campaign role in the username, campaign role pair is missing: " + currUserAndRole ) ; } Set < Campaign . Role > roles = result . get ( username ) ; if ( roles == null ) { roles = new HashSet < Campaign . Role > ( ) ; result . put ( username , roles ) ; } roles . add ( role ) ; } } if ( result . size ( ) == 0 ) { return null ; } else { return result ; } }
Validates a String that should be a list of username, campaign role
 pairs.
Validates a String that should be a list of username campaign role pairs .
// If it's null or empty, return null. // Create the resulting object which will initially be empty. // Split the parameterized value into its pairs. // For each of these pairs, // If the pair is empty, i.e. there were two list item separators // in a row, then skip it. // If the pair isn't actually a pair, fail with an invalid  // campaign role error. // Validate the actual elements in the pair. // Add the role to the list of roles. // If the list is empty, return null.