src/org/ohmage/domain/campaign/SurveyResponse.java
public final JSONObject toJson(
			final boolean withUsername, 
			final boolean withCampaignId, 
			final boolean withClient, 
			final boolean withPrivacyState, 
			final boolean withTime, 
			final boolean withTimezone, 
			final boolean withLocationStatus, 
			final boolean withLocation, 
			final boolean withSurveyId, 
			final boolean withSurveyTitle, 
			final boolean withSurveyDescription,
			final boolean withLaunchContextShort, 
			final boolean withLaunchContextLong, 
			final boolean withResponses, 
			final boolean arrayInsteadOfObject, 
			final boolean withId,
			final boolean withCount) 
			throws JSONException, DomainException {
		
		JSONObject result = new JSONObject();
		
		if(withUsername) {
			result.put(JSON_KEY_USERNAME, username);
		}
		
		if(withCampaignId) {
			result.put(JSON_KEY_CAMPAIGN_ID, campaignId);
		}
		
		if(withClient) {
			result.put(JSON_KEY_CLIENT, client);
		}
		
		if(withPrivacyState) {
			result.put(JSON_KEY_PRIVACY_STATE, privacyState.toString());
		}
		
		if(withTime) {
			result.put(JSON_KEY_TIME, time);
		}
		
		if(withTimezone) {
			result.put(JSON_KEY_TIMEZONE, timezone.getID());
		}
		
		if(withLocationStatus) {
			result.put(JSON_KEY_LOCATION_STATUS, locationStatus.toString());
		}
		
		if(withLocation && (location != null)) {
			result.put(JSON_KEY_LOCATION, location.toJson(false, LocationColumnKey.ALL_COLUMNS));
		}
		
		if(withSurveyId && (survey != null)) {
			result.put(JSON_KEY_SURVEY_ID, survey.getId());
		}
		
		if(withSurveyTitle && (survey != null)) {
			result.put(JSON_KEY_SURVEY_NAME, survey.getTitle());
		}
		
		if(withSurveyDescription && (survey != null)) {
			result.put(JSON_KEY_SURVEY_DESCRIPTION, survey.getDescription());
		}
		
		if(withLaunchContextShort) {
			result.put(JSON_KEY_SURVEY_LAUNCH_CONTEXT_SHORT, launchContext.toJson(false));
		}
		
		if(withLaunchContextLong) {
			result.put(JSON_KEY_SURVEY_LAUNCH_CONTEXT_LONG, launchContext.toJson(true));
		}
		
		if(withResponses) {
			List<Integer> indices = new ArrayList<Integer>(responses.keySet());
			Collections.sort(indices);
			
			if(arrayInsteadOfObject) {
				JSONArray responses = new JSONArray();
				for(Integer index : indices) {
					responses.put(this.responses.get(index).toJson(true));
				}
				result.put(JSON_KEY_RESPONSES, responses);
			}
			else {
				// HT: attempt to preserve the order of json object
				JSONObject responses = new JSONObject(new LinkedHashMap<String, String>());
				for(Integer index : indices) {
					Response response = this.responses.get(index);
		
					responses.put(response.getId(), response.toJson(false));
				}
				
				result.put(JSON_KEY_RESPONSES, responses);
			}
		}
		
		if(withId) {
			result.put(JSON_KEY_SURVEY_RESPONSE_ID, surveyResponseId.toString());
		}
		
		if(withCount) {
			result.put(JSON_KEY_COUNT, count);
		}
		
		// LOGGER.debug("HT: result:" + result.toString());
		return result;
	}
/**
	 * Creates a JSONObject that represents this survey response object based
	 * on the given flags.
	 * 
	 * @param withUsername Whether or not to include the username.
	 *  
	 * @param withCampaignId Whether or not to include the campaign's ID.
	 *  
	 * @param withClient Whether or not to include the client value.
	 *  
	 * @param withPrivacyState Whether or not to include the privacy state.
	 *  
	 * @param withDate Whether or not to include the date.
	 *  
	 * @param withTime Whether or not to include the time.
	 *  
	 * @param withTimezone Whether or not to include the timezone.
	 *  
	 * @param withLocationStatus Whether or not to include the location status.
	 *  
	 * @param withLocation Whether or not to include the location information.
	 * 					   This may not be included anyway if there was no
	 * 					   location information for this survey response.
	 *  
	 * @param withSurveyId Whether or not to include the survey's ID.
	 *  
	 * @param withSurveyTitle Whether or not to include the survey's title.
	 *  
	 * @param withSurveyDescription Whether or not to include the survey's
	 * 								description.
	 *  
	 * @param withSurveyLaunchContext Whether or not to include the survey's
	 * 								  launch context.
	 *  
	 * @param surveyLaunchContextLong If we are including the launch context,
	 * 								  this indicates whether we are adding 
	 * 								  everything in the launch context or only
	 * 								  the launch date and time.
	 *  
	 * @param withResponses Whether or not to include the prompt responses.
	 * 
	 * @param arrayInsteadOfObject Valid only if 'withResponses' is true, this
	 * 							   will determine how the responses are output,
	 * 							   either as a JSONObject where the keys are 
	 * 							   the prompt IDs and their value is another 
	 * 							   JSONObject that describes the prompt and the
	 * 							   user's response or as a JSONArray of 
	 * 							   JSONObjects that describe the prompt and the
	 * 							   user's response. If false, the former will
	 * 							   happen; if true, the latter will happen.
	 * 
	 * @return A JSONObject that represents this object or null if there was an
	 * 		   error.
	 * 
	 * @throws JSONException There was a problem creating the JSONObject.
	 */
Creates a JSONObject that represents this survey response object based
on the given flags.

@param withUsername Whether or not to include the username.

@param withCampaignId Whether or not to include the campaign's ID.

@param withClient Whether or not to include the client value.

@param withPrivacyState Whether or not to include the privacy state.

@param withDate Whether or not to include the date.

@param withTime Whether or not to include the time.

@param withTimezone Whether or not to include the timezone.

@param withLocationStatus Whether or not to include the location status.

@param withLocation Whether or not to include the location information.
This may not be included anyway if there was no
location information for this survey response.

@param withSurveyId Whether or not to include the survey's ID.

@param withSurveyTitle Whether or not to include the survey's title.

@param withSurveyDescription Whether or not to include the survey's
description.

@param withSurveyLaunchContext Whether or not to include the survey's
launch context.

@param surveyLaunchContextLong If we are including the launch context,
this indicates whether we are adding
everything in the launch context or only
the launch date and time.

@param withResponses Whether or not to include the prompt responses.

@param arrayInsteadOfObject Valid only if 'withResponses' is true, this
will determine how the responses are output,
either as a JSONObject where the keys are
the prompt IDs and their value is another
JSONObject that describes the prompt and the
user's response or as a JSONArray of
JSONObjects that describe the prompt and the
user's response. If false, the former will
happen; if true, the latter will happen.

@return A JSONObject that represents this object or null if there was an
error.

@throws JSONException There was a problem creating the JSONObject.
Creates a JSONObject that represents this survey response object based on the given flags . @param withUsername Whether or not to include the username . @param withCampaignId Whether or not to include the campaign ' s ID . @param withClient Whether or not to include the client value . @param withPrivacyState Whether or not to include the privacy state . @param withDate Whether or not to include the date . @param withTime Whether or not to include the time . @param withTimezone Whether or not to include the timezone . @param withLocationStatus Whether or not to include the location status . @param withLocation Whether or not to include the location information . This may not be included anyway if there was no location information for this survey response . @param withSurveyId Whether or not to include the survey ' s ID . @param withSurveyTitle Whether or not to include the survey ' s title . @param withSurveyDescription Whether or not to include the survey ' s description . @param withSurveyLaunchContext Whether or not to include the survey ' s launch context . @param surveyLaunchContextLong If we are including the launch context this indicates whether we are adding everything in the launch context or only the launch date and time . @param withResponses Whether or not to include the prompt responses . @param arrayInsteadOfObject Valid only if ' withResponses ' is true this will determine how the responses are output either as a JSONObject where the keys are the prompt IDs and their value is another JSONObject that describes the prompt and the user ' s response or as a JSONArray of JSONObjects that describe the prompt and the user ' s response . If false the former will happen ; if true the latter will happen . @return A JSONObject that represents this object or null if there was an error . @throws JSONException There was a problem creating the JSONObject .
public final JSONObject toJson(
			final boolean withUsername, 
			final boolean withCampaignId, 
			final boolean withClient, 
			final boolean withPrivacyState, 
			final boolean withTime, 
			final boolean withTimezone, 
			final boolean withLocationStatus, 
			final boolean withLocation, 
			final boolean withSurveyId, 
			final boolean withSurveyTitle, 
			final boolean withSurveyDescription,
			final boolean withLaunchContextShort, 
			final boolean withLaunchContextLong, 
			final boolean withResponses, 
			final boolean arrayInsteadOfObject, 
			final boolean withId,
			final boolean withCount) 
			throws JSONException, DomainException {
		JSONObject result = new JSONObject();
		if(withUsername) {
			result.put(JSON_KEY_USERNAME, username);
		}
		if(withCampaignId) {
			result.put(JSON_KEY_CAMPAIGN_ID, campaignId);
		}
		if(withClient) {
			result.put(JSON_KEY_CLIENT, client);
		}
		if(withPrivacyState) {
			result.put(JSON_KEY_PRIVACY_STATE, privacyState.toString());
		}
		if(withTime) {
			result.put(JSON_KEY_TIME, time);
		}
		if(withTimezone) {
			result.put(JSON_KEY_TIMEZONE, timezone.getID());
		}
		if(withLocationStatus) {
			result.put(JSON_KEY_LOCATION_STATUS, locationStatus.toString());
		}
		if(withLocation && (location != null)) {
			result.put(JSON_KEY_LOCATION, location.toJson(false, LocationColumnKey.ALL_COLUMNS));
		}
		if(withSurveyId && (survey != null)) {
			result.put(JSON_KEY_SURVEY_ID, survey.getId());
		}
		if(withSurveyTitle && (survey != null)) {
			result.put(JSON_KEY_SURVEY_NAME, survey.getTitle());
		}
		if(withSurveyDescription && (survey != null)) {
			result.put(JSON_KEY_SURVEY_DESCRIPTION, survey.getDescription());
		}
		if(withLaunchContextShort) {
			result.put(JSON_KEY_SURVEY_LAUNCH_CONTEXT_SHORT, launchContext.toJson(false));
		}
		if(withLaunchContextLong) {
			result.put(JSON_KEY_SURVEY_LAUNCH_CONTEXT_LONG, launchContext.toJson(true));
		}
		if(withResponses) {
			List<Integer> indices = new ArrayList<Integer>(responses.keySet());
			Collections.sort(indices);
			if(arrayInsteadOfObject) {
				JSONArray responses = new JSONArray();
				for(Integer index : indices) {
					responses.put(this.responses.get(index).toJson(true));
				}
				result.put(JSON_KEY_RESPONSES, responses);
			}
			else {
				JSONObject responses = new JSONObject(new LinkedHashMap<String, String>());
				for(Integer index : indices) {
					Response response = this.responses.get(index);
					responses.put(response.getId(), response.toJson(false));
				}
				result.put(JSON_KEY_RESPONSES, responses);
			}
		}
		if(withId) {
			result.put(JSON_KEY_SURVEY_RESPONSE_ID, surveyResponseId.toString());
		}
		if(withCount) {
			result.put(JSON_KEY_COUNT, count);
		}
		return result;
	}
public final JSONObject toJson ( final boolean withUsername , final boolean withCampaignId , final boolean withClient , final boolean withPrivacyState , final boolean withTime , final boolean withTimezone , final boolean withLocationStatus , final boolean withLocation , final boolean withSurveyId , final boolean withSurveyTitle , final boolean withSurveyDescription , final boolean withLaunchContextShort , final boolean withLaunchContextLong , final boolean withResponses , final boolean arrayInsteadOfObject , final boolean withId , final boolean withCount ) throws JSONException , DomainException { JSONObject result = new JSONObject ( ) ; if ( withUsername ) { result . put ( JSON_KEY_USERNAME , username ) ; } if ( withCampaignId ) { result . put ( JSON_KEY_CAMPAIGN_ID , campaignId ) ; } if ( withClient ) { result . put ( JSON_KEY_CLIENT , client ) ; } if ( withPrivacyState ) { result . put ( JSON_KEY_PRIVACY_STATE , privacyState . toString ( ) ) ; } if ( withTime ) { result . put ( JSON_KEY_TIME , time ) ; } if ( withTimezone ) { result . put ( JSON_KEY_TIMEZONE , timezone . getID ( ) ) ; } if ( withLocationStatus ) { result . put ( JSON_KEY_LOCATION_STATUS , locationStatus . toString ( ) ) ; } if ( withLocation && ( location != null ) ) { result . put ( JSON_KEY_LOCATION , location . toJson ( false , LocationColumnKey . ALL_COLUMNS ) ) ; } if ( withSurveyId && ( survey != null ) ) { result . put ( JSON_KEY_SURVEY_ID , survey . getId ( ) ) ; } if ( withSurveyTitle && ( survey != null ) ) { result . put ( JSON_KEY_SURVEY_NAME , survey . getTitle ( ) ) ; } if ( withSurveyDescription && ( survey != null ) ) { result . put ( JSON_KEY_SURVEY_DESCRIPTION , survey . getDescription ( ) ) ; } if ( withLaunchContextShort ) { result . put ( JSON_KEY_SURVEY_LAUNCH_CONTEXT_SHORT , launchContext . toJson ( false ) ) ; } if ( withLaunchContextLong ) { result . put ( JSON_KEY_SURVEY_LAUNCH_CONTEXT_LONG , launchContext . toJson ( true ) ) ; } if ( withResponses ) { List < Integer > indices = new ArrayList < Integer > ( responses . keySet ( ) ) ; Collections . sort ( indices ) ; if ( arrayInsteadOfObject ) { JSONArray responses = new JSONArray ( ) ; for ( Integer index : indices ) { responses . put ( this . responses . get ( index ) . toJson ( true ) ) ; } result . put ( JSON_KEY_RESPONSES , responses ) ; } else { JSONObject responses = new JSONObject ( new LinkedHashMap < String , String > ( ) ) ; for ( Integer index : indices ) { Response response = this . responses . get ( index ) ; responses . put ( response . getId ( ) , response . toJson ( false ) ) ; } result . put ( JSON_KEY_RESPONSES , responses ) ; } } if ( withId ) { result . put ( JSON_KEY_SURVEY_RESPONSE_ID , surveyResponseId . toString ( ) ) ; } if ( withCount ) { result . put ( JSON_KEY_COUNT , count ) ; } return result ; }
Creates a JSONObject that represents this survey response object based
 on the given flags.
Creates a JSONObject that represents this survey response object based on the given flags .
// HT: attempt to preserve the order of json object // LOGGER.debug("HT: result:" + result.toString());