src/org/ohmage/validator/ClassValidators.java
public static Map<String, Map<String, Clazz.Role>> validateClassRoster(
			final byte[] roster) throws ValidationException {
		
		LOGGER.info("Validating a class roster.");
		
		if((roster == null) || (roster.length == 0)) {
			return null;
		}
		
		String rosterString = new String(roster);
		
		// Excel (and most of Microsoft) saves newlines as carriage returns 
		// instead of newlines, so we substitute those here as we only deal 
		// with newlines.
		rosterString = rosterString.replace('\r', '\n');
		
		Map<String, Map<String, Clazz.Role>> result = new HashMap<String, Map<String, Clazz.Role>>();
		
		String[] rosterLines = rosterString.split("\n");
		for(int i = 0; i < rosterLines.length; i++) {
			if(StringUtils.isEmptyOrWhitespaceOnly(rosterLines[i])) {
				continue;
			}
			
			String[] rosterLine = rosterLines[i].split(",");
			
			if(rosterLine.length != 3) {
				throw new ValidationException(
						ErrorCode.CLASS_INVALID_ROSTER, 
						"The following line is malformed in the class roster: " + 
							rosterLines[i]);
			}
			
			String classId = ClassValidators.validateClassId(rosterLine[0]);
			String username = UserValidators.validateUsername(rosterLine[1]);
			Clazz.Role classRole = ClassValidators.validateClassRole(rosterLine[2]);
			
			Map<String, Clazz.Role> userRoleMap = result.get(classId);
			if(userRoleMap == null) {
				userRoleMap = new HashMap<String, Clazz.Role>();
				result.put(classId, userRoleMap);
			}
			
			Clazz.Role originalRole = userRoleMap.put(username, classRole);
			// Add the role but keep track of whether or not a role already 
			// existed for this user in this class. It is an error only if the
			// two roles do not match.
			if((originalRole != null) && (! originalRole.equals(classRole))) {
				throw new ValidationException(
						ErrorCode.CLASS_INVALID_ROSTER, 
						"Two different roles were found for the same user in the same class. The user was '" + 
							username + 
							"' and the class was '" + 
							classId + "'. The first role was '" + 
							originalRole + 
							"' and the second role was '" + 
							classRole + 
							"'");
			}
		}
		
		return result;
	}
/**
	 * Validates that a byte array is a valid class roster. If it is null or 
	 * has no length, null is returned. Otherwise, a Map of class IDs to Maps
	 * of usernames to class roles is returned. If the roster is not a valid 
	 * roster a ValidationException is thrown and the request is failed with 
	 * the error code, {@value ErrorCode.CLASS_INVALID_ROSTER}.
	 * 
	 * @param roster The class roster as a byte array. This should a series of
	 * 				 newline-deliminated rows where each row is a comma-
	 * 				 separated list of class ID, username, and the user's role
	 * 				 in the class. Excel for any OS, and many other Microsoft
	 * 				 products, deliminate lines with a carriage return instead 
	 * 				 of a newline; this is taken care of. 
	 * 
	 * @return Returns null if the roster is null or has a length of zero;
	 * 		   otherwise, it returns a Map of class IDs to Maps of usernames to
	 * 		   class roles.
	 * 
	 * @throws ValidationException Thrown if the roster is not a valid roster.
	 */
Validates that a byte array is a valid class roster. If it is null or
has no length, null is returned. Otherwise, a Map of class IDs to Maps
of usernames to class roles is returned. If the roster is not a valid
roster a ValidationException is thrown and the request is failed with
the error code,  ErrorCode.CLASS_INVALID_ROSTER.

@param roster The class roster as a byte array. This should a series of
newline-deliminated rows where each row is a comma
separated list of class ID, username, and the user's role
in the class. Excel for any OS, and many other Microsoft
products, deliminate lines with a carriage return instead
of a newline; this is taken care of.

@return Returns null if the roster is null or has a length of zero;
otherwise, it returns a Map of class IDs to Maps of usernames to
class roles.

@throws ValidationException Thrown if the roster is not a valid roster.
Validates that a byte array is a valid class roster . If it is null or has no length null is returned . Otherwise a Map of class IDs to Maps of usernames to class roles is returned . If the roster is not a valid roster a ValidationException is thrown and the request is failed with the error code ErrorCode . CLASS_INVALID_ROSTER . @param roster The class roster as a byte array . This should a series of newline - deliminated rows where each row is a comma separated list of class ID username and the user ' s role in the class . Excel for any OS and many other Microsoft products deliminate lines with a carriage return instead of a newline ; this is taken care of . @return Returns null if the roster is null or has a length of zero ; otherwise it returns a Map of class IDs to Maps of usernames to class roles . @throws ValidationException Thrown if the roster is not a valid roster .
public static Map<String, Map<String, Clazz.Role>> validateClassRoster(
			final byte[] roster) throws ValidationException {
		LOGGER.info("Validating a class roster.");
		if((roster == null) || (roster.length == 0)) {
			return null;
		}
		String rosterString = new String(roster);
		rosterString = rosterString.replace('\r', '\n');
		Map<String, Map<String, Clazz.Role>> result = new HashMap<String, Map<String, Clazz.Role>>();
		String[] rosterLines = rosterString.split("\n");
		for(int i = 0; i < rosterLines.length; i++) {
			if(StringUtils.isEmptyOrWhitespaceOnly(rosterLines[i])) {
				continue;
			}
			String[] rosterLine = rosterLines[i].split(",");
			if(rosterLine.length != 3) {
				throw new ValidationException(
						ErrorCode.CLASS_INVALID_ROSTER, 
						"The following line is malformed in the class roster: " + 
							rosterLines[i]);
			}
			String classId = ClassValidators.validateClassId(rosterLine[0]);
			String username = UserValidators.validateUsername(rosterLine[1]);
			Clazz.Role classRole = ClassValidators.validateClassRole(rosterLine[2]);
			Map<String, Clazz.Role> userRoleMap = result.get(classId);
			if(userRoleMap == null) {
				userRoleMap = new HashMap<String, Clazz.Role>();
				result.put(classId, userRoleMap);
			}
			Clazz.Role originalRole = userRoleMap.put(username, classRole);
			if((originalRole != null) && (! originalRole.equals(classRole))) {
				throw new ValidationException(
						ErrorCode.CLASS_INVALID_ROSTER, 
						"Two different roles were found for the same user in the same class. The user was '" + 
							username + 
							"' and the class was '" + 
							classId + "'. The first role was '" + 
							originalRole + 
							"' and the second role was '" + 
							classRole + 
							"'");
			}
		}
		return result;
	}
public static Map < String , Map < String , Clazz . Role > > validateClassRoster ( final byte [ ] roster ) throws ValidationException { LOGGER . info ( "Validating a class roster." ) ; if ( ( roster == null ) || ( roster . length == 0 ) ) { return null ; } String rosterString = new String ( roster ) ; rosterString = rosterString . replace ( '\r' , '\n' ) ; Map < String , Map < String , Clazz . Role > > result = new HashMap < String , Map < String , Clazz . Role > > ( ) ; String [ ] rosterLines = rosterString . split ( "\n" ) ; for ( int i = 0 ; i < rosterLines . length ; i ++ ) { if ( StringUtils . isEmptyOrWhitespaceOnly ( rosterLines [ i ] ) ) { continue ; } String [ ] rosterLine = rosterLines [ i ] . split ( "," ) ; if ( rosterLine . length != 3 ) { throw new ValidationException ( ErrorCode . CLASS_INVALID_ROSTER , "The following line is malformed in the class roster: " + rosterLines [ i ] ) ; } String classId = ClassValidators . validateClassId ( rosterLine [ 0 ] ) ; String username = UserValidators . validateUsername ( rosterLine [ 1 ] ) ; Clazz . Role classRole = ClassValidators . validateClassRole ( rosterLine [ 2 ] ) ; Map < String , Clazz . Role > userRoleMap = result . get ( classId ) ; if ( userRoleMap == null ) { userRoleMap = new HashMap < String , Clazz . Role > ( ) ; result . put ( classId , userRoleMap ) ; } Clazz . Role originalRole = userRoleMap . put ( username , classRole ) ; if ( ( originalRole != null ) && ( ! originalRole . equals ( classRole ) ) ) { throw new ValidationException ( ErrorCode . CLASS_INVALID_ROSTER , "Two different roles were found for the same user in the same class. The user was '" + username + "' and the class was '" + classId + "'. The first role was '" + originalRole + "' and the second role was '" + classRole + "'" ) ; } } return result ; }
Validates that a byte array is a valid class roster.
Validates that a byte array is a valid class roster .
// Excel (and most of Microsoft) saves newlines as carriage returns  // instead of newlines, so we substitute those here as we only deal  // with newlines. // Add the role but keep track of whether or not a role already  // existed for this user in this class. It is an error only if the // two roles do not match.