src/org/ohmage/validator/SurveyResponseValidators.java
public static Map<String, Set<String>> validatePromptIdMap(
		final String promptIdMap) 
		throws ValidationException {
		
		if(StringUtils.isEmptyOrWhitespaceOnly(promptIdMap)) {
			return null;
		}
		
		// Get the array of JSON objects, where each object represents one
		// survey and its prompts.
		JSONArray surveysArray;
		try {
			surveysArray = new JSONArray(promptIdMap);
		}
		catch(JSONException e) {
			throw
				new ValidationException(
					ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
					"The survey-prompt ID map was not a valid JSON array: " +
						e.getMessage(),
					e);
		}
		
		// Create the result map.
		Map<String, Set<String>> result = new HashMap<String, Set<String>>();
		
		// Cycle through the elements, processing them.
		int numSurveys = surveysArray.length();
		for(int i = 0; i < numSurveys; i++) {
			// Get the survey object.
			JSONObject surveyObject;
			try {
				surveyObject = surveysArray.getJSONObject(i);
			}
			catch(JSONException e) {
				throw
					new ValidationException(
						ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
						"An element in the survey-prompt map was not an " +
							"object: " +
							e.getMessage(),
						e);
			}
			
			// Get the survey ID.
			String surveyId;
			try {
				surveyId = surveyObject.getString("survey_id");
			}
			catch(JSONException e) {
				throw
					new ValidationException(
						ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
						"The survey ID was not a string: " + e.getMessage(),
						e);
			}
			
			// Get the array of prompt IDs.
			JSONArray promptIdsArray;
			try {
				promptIdsArray = surveyObject.getJSONArray("prompt_ids");
			}
			catch(JSONException e) {
				throw
					new ValidationException(
						ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
						"The prompt ID list was not a JSON array: " +
							e.getMessage(),
						e);
			}
			
			// Create the Set and add the elements in the array to it.
			Set<String> promptIds = new HashSet<String>();
			int numPromptIds = promptIdsArray.length();
			for(int j = 0; j < numPromptIds; j++) {
				// Get the prompt ID.
				String promptId;
				try {
					promptId = promptIdsArray.getString(j); 
				}
				catch(JSONException e) {
					throw
						new ValidationException(
							ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
							"The prompt ID is not a string: " + e.getMessage(),
							e);
				}
				
				// Validate the prompt ID.
				if(StringUtils.isEmptyOrWhitespaceOnly(promptId)) {
					throw
						new ValidationException(
							ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
							"The prompt ID is only whitespace.");
				}
				
				// Save the prompt ID.
				promptIds.add(promptId);
			}
			
			// Save the survey ID - prompt IDs collection to the result map.
			result.put(surveyId, promptIds);
		}
		
		// Return the result.
		return result;
	}
/**
	 * Validates a string representing a map of survey IDs to a list of prompt
	 * IDs for their respective surveys.
	 * 
	 * @param promptIdMap  The string map of survey IDs to their prompt IDs.
	 * 
	 * @return A Map of survey IDs to prompt IDs.
	 */
Validates a string representing a map of survey IDs to a list of prompt
IDs for their respective surveys.

@param promptIdMap  The string map of survey IDs to their prompt IDs.

@return A Map of survey IDs to prompt IDs.
Validates a string representing a map of survey IDs to a list of prompt IDs for their respective surveys . @param promptIdMap The string map of survey IDs to their prompt IDs . @return A Map of survey IDs to prompt IDs .
public static Map<String, Set<String>> validatePromptIdMap(
		final String promptIdMap) 
		throws ValidationException {
		if(StringUtils.isEmptyOrWhitespaceOnly(promptIdMap)) {
			return null;
		}
		JSONArray surveysArray;
		try {
			surveysArray = new JSONArray(promptIdMap);
		}
		catch(JSONException e) {
			throw
				new ValidationException(
					ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
					"The survey-prompt ID map was not a valid JSON array: " +
						e.getMessage(),
					e);
		}
		Map<String, Set<String>> result = new HashMap<String, Set<String>>();
		int numSurveys = surveysArray.length();
		for(int i = 0; i < numSurveys; i++) {
			JSONObject surveyObject;
			try {
				surveyObject = surveysArray.getJSONObject(i);
			}
			catch(JSONException e) {
				throw
					new ValidationException(
						ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
						"An element in the survey-prompt map was not an " +
							"object: " +
							e.getMessage(),
						e);
			}
			String surveyId;
			try {
				surveyId = surveyObject.getString("survey_id");
			}
			catch(JSONException e) {
				throw
					new ValidationException(
						ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
						"The survey ID was not a string: " + e.getMessage(),
						e);
			}
			JSONArray promptIdsArray;
			try {
				promptIdsArray = surveyObject.getJSONArray("prompt_ids");
			}
			catch(JSONException e) {
				throw
					new ValidationException(
						ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
						"The prompt ID list was not a JSON array: " +
							e.getMessage(),
						e);
			}
			Set<String> promptIds = new HashSet<String>();
			int numPromptIds = promptIdsArray.length();
			for(int j = 0; j < numPromptIds; j++) {
				String promptId;
				try {
					promptId = promptIdsArray.getString(j); 
				}
				catch(JSONException e) {
					throw
						new ValidationException(
							ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
							"The prompt ID is not a string: " + e.getMessage(),
							e);
				}
				if(StringUtils.isEmptyOrWhitespaceOnly(promptId)) {
					throw
						new ValidationException(
							ErrorCode.SURVEY_INVALID_SURVEY_PROMPT_MAP,
							"The prompt ID is only whitespace.");
				}
				promptIds.add(promptId);
			}
			result.put(surveyId, promptIds);
		}
		return result;
	}
public static Map < String , Set < String > > validatePromptIdMap ( final String promptIdMap ) throws ValidationException { if ( StringUtils . isEmptyOrWhitespaceOnly ( promptIdMap ) ) { return null ; } JSONArray surveysArray ; try { surveysArray = new JSONArray ( promptIdMap ) ; } catch ( JSONException e ) { throw new ValidationException ( ErrorCode . SURVEY_INVALID_SURVEY_PROMPT_MAP , "The survey-prompt ID map was not a valid JSON array: " + e . getMessage ( ) , e ) ; } Map < String , Set < String > > result = new HashMap < String , Set < String > > ( ) ; int numSurveys = surveysArray . length ( ) ; for ( int i = 0 ; i < numSurveys ; i ++ ) { JSONObject surveyObject ; try { surveyObject = surveysArray . getJSONObject ( i ) ; } catch ( JSONException e ) { throw new ValidationException ( ErrorCode . SURVEY_INVALID_SURVEY_PROMPT_MAP , "An element in the survey-prompt map was not an " + "object: " + e . getMessage ( ) , e ) ; } String surveyId ; try { surveyId = surveyObject . getString ( "survey_id" ) ; } catch ( JSONException e ) { throw new ValidationException ( ErrorCode . SURVEY_INVALID_SURVEY_PROMPT_MAP , "The survey ID was not a string: " + e . getMessage ( ) , e ) ; } JSONArray promptIdsArray ; try { promptIdsArray = surveyObject . getJSONArray ( "prompt_ids" ) ; } catch ( JSONException e ) { throw new ValidationException ( ErrorCode . SURVEY_INVALID_SURVEY_PROMPT_MAP , "The prompt ID list was not a JSON array: " + e . getMessage ( ) , e ) ; } Set < String > promptIds = new HashSet < String > ( ) ; int numPromptIds = promptIdsArray . length ( ) ; for ( int j = 0 ; j < numPromptIds ; j ++ ) { String promptId ; try { promptId = promptIdsArray . getString ( j ) ; } catch ( JSONException e ) { throw new ValidationException ( ErrorCode . SURVEY_INVALID_SURVEY_PROMPT_MAP , "The prompt ID is not a string: " + e . getMessage ( ) , e ) ; } if ( StringUtils . isEmptyOrWhitespaceOnly ( promptId ) ) { throw new ValidationException ( ErrorCode . SURVEY_INVALID_SURVEY_PROMPT_MAP , "The prompt ID is only whitespace." ) ; } promptIds . add ( promptId ) ; } result . put ( surveyId , promptIds ) ; } return result ; }
Validates a string representing a map of survey IDs to a list of prompt
 IDs for their respective surveys.
Validates a string representing a map of survey IDs to a list of prompt IDs for their respective surveys .
// Get the array of JSON objects, where each object represents one // survey and its prompts. // Create the result map. // Cycle through the elements, processing them. // Get the survey object. // Get the survey ID. // Get the array of prompt IDs. // Create the Set and add the elements in the array to it. // Get the prompt ID. // Validate the prompt ID. // Save the prompt ID. // Save the survey ID - prompt IDs collection to the result map. // Return the result.