src/org/ohmage/domain/campaign/SurveyResponse.java
private Map<Integer, Response> processResponses(
		final Map<Integer, SurveyItem> surveyItems, 
		final JSONArray currArray, 
		final Integer repeatableSetIteration,
		final Set<String> allowedPromptIds,
		final boolean allowPartialSurvey) 
		throws DomainException {
		
		int numResponses = currArray.length();
		Map<Integer, Response> results = 
			new HashMap<Integer, Response>(numResponses);
		
		for(int i = 0; i < numResponses; i++) {
			try {
				JSONObject currResponse = currArray.getJSONObject(i);
				
				try {
					String promptId = currResponse.getString(JSON_KEY_PROMPT_ID);
					Prompt prompt = null;
					for(SurveyItem surveyItem : surveyItems.values()) {
						if(surveyItem.getId().equals(promptId)) {
							prompt = (Prompt) surveyItem;
							break;
						}
					}
					if(prompt == null) {
						throw new DomainException(
								ErrorCode.SURVEY_INVALID_RESPONSES, 
								"The prompt ID is unknown: " + promptId);
					}
					
					results.put(
						prompt.getIndex(),
						processPromptResponse(
							prompt, 
							currResponse, 
							repeatableSetIteration));
				}
				catch(JSONException notPrompt) {
					try {
						String repeatableSetId = currResponse.getString(JSON_KEY_REPEATABLE_SET_ID);
						RepeatableSet repeatableSet = null;
						for(SurveyItem surveyItem : surveyItems.values()) {
							if(surveyItem.getId().equals(repeatableSetId)) {
								repeatableSet = (RepeatableSet) surveyItem;
							}
						}
						if(repeatableSet == null) {
							throw new DomainException(
									ErrorCode.SURVEY_INVALID_RESPONSES, 
									"The repeatable set ID is unknown: " + 
										repeatableSetId);
						}
						
						results.put(
								repeatableSet.getIndex(),
								processRepeatableSet(
										repeatableSet, 
										currResponse,
										allowedPromptIds));
					}
					catch(JSONException notRepeatableSet) {
						throw new DomainException(
								ErrorCode.SURVEY_INVALID_RESPONSES, 
								"The response wasn't a prompt response or repeatable set.");
					}
				}
				catch(ClassCastException e) {
					throw new DomainException(
							ErrorCode.SURVEY_INVALID_RESPONSES, 
							"The response and XML disagree on the type of a survey item.", 
							e);
				}
			}
			catch(JSONException e) {
				throw new DomainException(
						ErrorCode.SURVEY_INVALID_RESPONSES, 
						"A response was not valid JSON.");
			}
		}
		
		// For survey update, allow partial survey response.
		if (allowPartialSurvey == false ) {
		    // Cycle through the prompts and ensure that a response exists for each
		    // prompt, unless it was masked.
		    for(SurveyItem surveyItem : surveyItems.values()) {
			// If it's a message, it won't have a response.
			if(! (surveyItem instanceof Message)) {
				String surveyItemId = surveyItem.getId();
				
				// If it's not part of the mask, throw an exception.
				if(
					(allowedPromptIds != null) &&
					(! allowedPromptIds.contains(surveyItemId))) {
					
					continue;
				}
				
				boolean found = false;

				for(Response response : results.values()) {
					if(response.getId().equals(surveyItemId)) {
						found = true;
						break;
					}
				}
				
				if(! found) {
					throw new DomainException(
							ErrorCode.SURVEY_INVALID_RESPONSES, 
							"The response is missing a response for the prompt: " + 
								surveyItemId);
				}
			}
		    }
		}
		
		return results;
	}
/**
	 * Processes an JSONArray of survey responses based on their survey item
	 * counterparts. 
	 * 
	 * @param surveyItems The survey items to which the responses should 
	 * 					  pertain.
	 * 
	 * @param currArray A JSONArray of JSONObjects where each object is a 
	 * 					response.
	 * 
	 * @param repeatableSetIteration If this is processing the responses of a
	 * 								 repeatable set, then this is the iteration
	 * 								 of that repeatable set. Otherwise, it may
	 * 								 be null.
	 * 
	 * @return A map of a response's index to its Response object.
	 * 
	 * @throws DomainException Thrown if any of the responses are invalid 
	 * 						   either syntactically or as compared to the
	 * 						   survey objects.
	 */
Processes an JSONArray of survey responses based on their survey item
counterparts.

@param surveyItems The survey items to which the responses should
pertain.

@param currArray A JSONArray of JSONObjects where each object is a
response.

@param repeatableSetIteration If this is processing the responses of a
repeatable set, then this is the iteration
of that repeatable set. Otherwise, it may
be null.

@return A map of a response's index to its Response object.

@throws DomainException Thrown if any of the responses are invalid
either syntactically or as compared to the
survey objects.
Processes an JSONArray of survey responses based on their survey item counterparts . @param surveyItems The survey items to which the responses should pertain . @param currArray A JSONArray of JSONObjects where each object is a response . @param repeatableSetIteration If this is processing the responses of a repeatable set then this is the iteration of that repeatable set . Otherwise it may be null . @return A map of a response ' s index to its Response object . @throws DomainException Thrown if any of the responses are invalid either syntactically or as compared to the survey objects .
private Map<Integer, Response> processResponses(
		final Map<Integer, SurveyItem> surveyItems, 
		final JSONArray currArray, 
		final Integer repeatableSetIteration,
		final Set<String> allowedPromptIds,
		final boolean allowPartialSurvey) 
		throws DomainException {
		int numResponses = currArray.length();
		Map<Integer, Response> results = 
			new HashMap<Integer, Response>(numResponses);
		for(int i = 0; i < numResponses; i++) {
			try {
				JSONObject currResponse = currArray.getJSONObject(i);
				try {
					String promptId = currResponse.getString(JSON_KEY_PROMPT_ID);
					Prompt prompt = null;
					for(SurveyItem surveyItem : surveyItems.values()) {
						if(surveyItem.getId().equals(promptId)) {
							prompt = (Prompt) surveyItem;
							break;
						}
					}
					if(prompt == null) {
						throw new DomainException(
								ErrorCode.SURVEY_INVALID_RESPONSES, 
								"The prompt ID is unknown: " + promptId);
					}
					results.put(
						prompt.getIndex(),
						processPromptResponse(
							prompt, 
							currResponse, 
							repeatableSetIteration));
				}
				catch(JSONException notPrompt) {
					try {
						String repeatableSetId = currResponse.getString(JSON_KEY_REPEATABLE_SET_ID);
						RepeatableSet repeatableSet = null;
						for(SurveyItem surveyItem : surveyItems.values()) {
							if(surveyItem.getId().equals(repeatableSetId)) {
								repeatableSet = (RepeatableSet) surveyItem;
							}
						}
						if(repeatableSet == null) {
							throw new DomainException(
									ErrorCode.SURVEY_INVALID_RESPONSES, 
									"The repeatable set ID is unknown: " + 
										repeatableSetId);
						}
						results.put(
								repeatableSet.getIndex(),
								processRepeatableSet(
										repeatableSet, 
										currResponse,
										allowedPromptIds));
					}
					catch(JSONException notRepeatableSet) {
						throw new DomainException(
								ErrorCode.SURVEY_INVALID_RESPONSES, 
								"The response wasn't a prompt response or repeatable set.");
					}
				}
				catch(ClassCastException e) {
					throw new DomainException(
							ErrorCode.SURVEY_INVALID_RESPONSES, 
							"The response and XML disagree on the type of a survey item.", 
							e);
				}
			}
			catch(JSONException e) {
				throw new DomainException(
						ErrorCode.SURVEY_INVALID_RESPONSES, 
						"A response was not valid JSON.");
			}
		}
		if (allowPartialSurvey == false ) {
		    for(SurveyItem surveyItem : surveyItems.values()) {
			if(! (surveyItem instanceof Message)) {
				String surveyItemId = surveyItem.getId();
				if(
					(allowedPromptIds != null) &&
					(! allowedPromptIds.contains(surveyItemId))) {
					continue;
				}
				boolean found = false;
				for(Response response : results.values()) {
					if(response.getId().equals(surveyItemId)) {
						found = true;
						break;
					}
				}
				if(! found) {
					throw new DomainException(
							ErrorCode.SURVEY_INVALID_RESPONSES, 
							"The response is missing a response for the prompt: " + 
								surveyItemId);
				}
			}
		    }
		}
		return results;
	}
private Map < Integer , Response > processResponses ( final Map < Integer , SurveyItem > surveyItems , final JSONArray currArray , final Integer repeatableSetIteration , final Set < String > allowedPromptIds , final boolean allowPartialSurvey ) throws DomainException { int numResponses = currArray . length ( ) ; Map < Integer , Response > results = new HashMap < Integer , Response > ( numResponses ) ; for ( int i = 0 ; i < numResponses ; i ++ ) { try { JSONObject currResponse = currArray . getJSONObject ( i ) ; try { String promptId = currResponse . getString ( JSON_KEY_PROMPT_ID ) ; Prompt prompt = null ; for ( SurveyItem surveyItem : surveyItems . values ( ) ) { if ( surveyItem . getId ( ) . equals ( promptId ) ) { prompt = ( Prompt ) surveyItem ; break ; } } if ( prompt == null ) { throw new DomainException ( ErrorCode . SURVEY_INVALID_RESPONSES , "The prompt ID is unknown: " + promptId ) ; } results . put ( prompt . getIndex ( ) , processPromptResponse ( prompt , currResponse , repeatableSetIteration ) ) ; } catch ( JSONException notPrompt ) { try { String repeatableSetId = currResponse . getString ( JSON_KEY_REPEATABLE_SET_ID ) ; RepeatableSet repeatableSet = null ; for ( SurveyItem surveyItem : surveyItems . values ( ) ) { if ( surveyItem . getId ( ) . equals ( repeatableSetId ) ) { repeatableSet = ( RepeatableSet ) surveyItem ; } } if ( repeatableSet == null ) { throw new DomainException ( ErrorCode . SURVEY_INVALID_RESPONSES , "The repeatable set ID is unknown: " + repeatableSetId ) ; } results . put ( repeatableSet . getIndex ( ) , processRepeatableSet ( repeatableSet , currResponse , allowedPromptIds ) ) ; } catch ( JSONException notRepeatableSet ) { throw new DomainException ( ErrorCode . SURVEY_INVALID_RESPONSES , "The response wasn't a prompt response or repeatable set." ) ; } } catch ( ClassCastException e ) { throw new DomainException ( ErrorCode . SURVEY_INVALID_RESPONSES , "The response and XML disagree on the type of a survey item." , e ) ; } } catch ( JSONException e ) { throw new DomainException ( ErrorCode . SURVEY_INVALID_RESPONSES , "A response was not valid JSON." ) ; } } if ( allowPartialSurvey == false ) { for ( SurveyItem surveyItem : surveyItems . values ( ) ) { if ( ! ( surveyItem instanceof Message ) ) { String surveyItemId = surveyItem . getId ( ) ; if ( ( allowedPromptIds != null ) && ( ! allowedPromptIds . contains ( surveyItemId ) ) ) { continue ; } boolean found = false ; for ( Response response : results . values ( ) ) { if ( response . getId ( ) . equals ( surveyItemId ) ) { found = true ; break ; } } if ( ! found ) { throw new DomainException ( ErrorCode . SURVEY_INVALID_RESPONSES , "The response is missing a response for the prompt: " + surveyItemId ) ; } } } } return results ; }
Processes an JSONArray of survey responses based on their survey item
 counterparts.
Processes an JSONArray of survey responses based on their survey item counterparts .
// For survey update, allow partial survey response. // Cycle through the prompts and ensure that a response exists for each // prompt, unless it was masked. // If it's a message, it won't have a response. // If it's not part of the mask, throw an exception.