src/org/ohmage/request/survey/SurveyResponseFunctionReadRequest.java
private void functionResponsePrivacyState(
			final HttpServletRequest httpRequest,
			final HttpServletResponse httpResponse) {

		// This maps each of the privacy states to a collection of collections
		// of survey responses. The inner collection is all of the survey 
		// responses that have the same parameters as specified by the 
		// 'privacyStateGroupItems'. The outer collection is the aggregation of
		// all of those inner collections associated with this privacy state.
		// For example, if the 'privacyStateGroupItems' is empty, then the 
		// outer collection would contain exactly one inner collection which
		// would be all survey responses with the privacy state for the key.
		// If the 'privacyStateGroupItems' contains a 'date' item, then for 
		// each key the outer collection is a collection of all of the survey
		// responses with the same privacy state and each inner collection is 
		// all of the survey responses with the same date. If a 'survey ID' 
		// item was also given, then each of the inner collections would be
		// those survey responses that share the same date and survey ID.
		Map<PrivacyState, Collection<Collection<SurveyResponse>>> privacyStateBucket =
				new HashMap<PrivacyState, Collection<Collection<SurveyResponse>>>(
						PrivacyState.values().length);
		
		// Cycle through all of the survey responses and populate their initial
		// buckets.
		for(SurveyResponse surveyResponse : surveyResponses) {
			// Get this response's privacy state.
			PrivacyState privacyState = 
					surveyResponse.getPrivacyState();
			
			// Get the outer collection if it exists.
			Collection<Collection<SurveyResponse>> outerCollection =
					privacyStateBucket.get(privacyState);
			
			// If it doesn't exist, create and add it.
			if(outerCollection == null) {
				outerCollection = new LinkedList<Collection<SurveyResponse>>();
				privacyStateBucket.put(privacyState, outerCollection);
			}
			
			// The outer collection should have exactly zero or one inner 
			// collections. If it has zero, create the one.
			Collection<SurveyResponse> innerCollection;
			if(outerCollection.size() == 0) {
				innerCollection = new LinkedList<SurveyResponse>();
				outerCollection.add(innerCollection);
			}
			// If it already has one then it should have exactly one and we
			// retrieve that one.
			else {
				innerCollection = outerCollection.iterator().next();
			}
			
			// Add the survey response to the inner collection.
			innerCollection.add(surveyResponse);
		}
		
		if(privacyStateGroupItems.contains(FunctionPrivacyStateItem.DATE)) {
			LOGGER.info("Subdividing buckets by date.");
			subdivideDate(privacyStateBucket);
		}

		if(privacyStateGroupItems.contains(FunctionPrivacyStateItem.SURVEY)) {
			LOGGER.info("Subdividing buckets by survey ID.");
			subdivideSurveyId(privacyStateBucket);
		}
		
		try {
			// Create the resulting JSONObject and populate it.
			JSONObject result = new JSONObject();
			for(PrivacyState privacyState : privacyStateBucket.keySet()) {
				Collection<Collection<SurveyResponse>> currPrivacyStateBucket =
						privacyStateBucket.get(privacyState);
				
				// Create the array of buckets for this privacy state.
				JSONArray jsonBuckets = new JSONArray();
				
				// Cycle through each of the buckets of collections, create a 
				// JSONObject representing that bucket, and add it to the array for
				// this privacy state.
				for(Collection<SurveyResponse> currBucket : currPrivacyStateBucket) {
					JSONObject jsonBucket = new JSONObject();
					
					jsonBucket.put("count", currBucket.size());
					
					if(privacyStateGroupItems.contains(FunctionPrivacyStateItem.DATE)) {
						// Get a representative survey response.
						SurveyResponse surveyResponse = 
								currBucket.iterator().next();
						
						// Create a calendar with the timezone from the 
						// response and set its time based on the epoch 
						// milliseconds.
						Calendar timezoneAdjustedCalendar =
								Calendar.getInstance(surveyResponse.getTimezone().toTimeZone());
						timezoneAdjustedCalendar.setTimeInMillis(
								surveyResponse.getTime());

						// Create the date string to represent this date based on
						// the creator's timezone at the time they took it.
						String dateString = 
								timezoneAdjustedCalendar.get(Calendar.YEAR) + 
								"-" +
								(timezoneAdjustedCalendar.get(Calendar.MONTH) + 1) +
								"-" +
								timezoneAdjustedCalendar.get(Calendar.DAY_OF_MONTH);
						
						jsonBucket.put("date", dateString);
					}
					
					if(privacyStateGroupItems.contains(FunctionPrivacyStateItem.SURVEY)) {
						jsonBucket.put(
								"survey_id", 
								currBucket.iterator().next().getSurvey().getId());
					}
					
					jsonBuckets.put(jsonBucket);
				}
				
				result.put(privacyState.toString(), jsonBuckets);
			}
			
			super.respond(httpRequest, httpResponse, result);
		}
		catch(JSONException e) {
			LOGGER.error("There was a problem creating the response.", e);
			setFailed();
			super.respond(httpRequest, httpResponse, (JSONObject) null);
		}
	}
/**
	 * This responds to a "privacy state" function request.
	 * 
	 * @param httpRequest The HttpServletRequest
	 * 
	 * @param httpResponse The HttpServletResponse
	 */
This responds to a "privacy state" function request.

@param httpRequest The HttpServletRequest

@param httpResponse The HttpServletResponse
This responds to a " privacy state " function request . @param httpRequest The HttpServletRequest @param httpResponse The HttpServletResponse
private void functionResponsePrivacyState(
			final HttpServletRequest httpRequest,
			final HttpServletResponse httpResponse) {
		Map<PrivacyState, Collection<Collection<SurveyResponse>>> privacyStateBucket =
				new HashMap<PrivacyState, Collection<Collection<SurveyResponse>>>(
						PrivacyState.values().length);
		for(SurveyResponse surveyResponse : surveyResponses) {
			PrivacyState privacyState = 
					surveyResponse.getPrivacyState();
			Collection<Collection<SurveyResponse>> outerCollection =
					privacyStateBucket.get(privacyState);
			if(outerCollection == null) {
				outerCollection = new LinkedList<Collection<SurveyResponse>>();
				privacyStateBucket.put(privacyState, outerCollection);
			}
			Collection<SurveyResponse> innerCollection;
			if(outerCollection.size() == 0) {
				innerCollection = new LinkedList<SurveyResponse>();
				outerCollection.add(innerCollection);
			}
			else {
				innerCollection = outerCollection.iterator().next();
			}
			innerCollection.add(surveyResponse);
		}
		if(privacyStateGroupItems.contains(FunctionPrivacyStateItem.DATE)) {
			LOGGER.info("Subdividing buckets by date.");
			subdivideDate(privacyStateBucket);
		}
		if(privacyStateGroupItems.contains(FunctionPrivacyStateItem.SURVEY)) {
			LOGGER.info("Subdividing buckets by survey ID.");
			subdivideSurveyId(privacyStateBucket);
		}
		try {
			JSONObject result = new JSONObject();
			for(PrivacyState privacyState : privacyStateBucket.keySet()) {
				Collection<Collection<SurveyResponse>> currPrivacyStateBucket =
						privacyStateBucket.get(privacyState);
				JSONArray jsonBuckets = new JSONArray();
				for(Collection<SurveyResponse> currBucket : currPrivacyStateBucket) {
					JSONObject jsonBucket = new JSONObject();
					jsonBucket.put("count", currBucket.size());
					if(privacyStateGroupItems.contains(FunctionPrivacyStateItem.DATE)) {
						SurveyResponse surveyResponse = 
								currBucket.iterator().next();
						Calendar timezoneAdjustedCalendar =
								Calendar.getInstance(surveyResponse.getTimezone().toTimeZone());
						timezoneAdjustedCalendar.setTimeInMillis(
								surveyResponse.getTime());
						String dateString = 
								timezoneAdjustedCalendar.get(Calendar.YEAR) + 
								"-" +
								(timezoneAdjustedCalendar.get(Calendar.MONTH) + 1) +
								"-" +
								timezoneAdjustedCalendar.get(Calendar.DAY_OF_MONTH);
						jsonBucket.put("date", dateString);
					}
					if(privacyStateGroupItems.contains(FunctionPrivacyStateItem.SURVEY)) {
						jsonBucket.put(
								"survey_id", 
								currBucket.iterator().next().getSurvey().getId());
					}
					jsonBuckets.put(jsonBucket);
				}
				result.put(privacyState.toString(), jsonBuckets);
			}
			super.respond(httpRequest, httpResponse, result);
		}
		catch(JSONException e) {
			LOGGER.error("There was a problem creating the response.", e);
			setFailed();
			super.respond(httpRequest, httpResponse, (JSONObject) null);
		}
	}
private void functionResponsePrivacyState ( final HttpServletRequest httpRequest , final HttpServletResponse httpResponse ) { Map < PrivacyState , Collection < Collection < SurveyResponse > > > privacyStateBucket = new HashMap < PrivacyState , Collection < Collection < SurveyResponse > > > ( PrivacyState . values ( ) . length ) ; for ( SurveyResponse surveyResponse : surveyResponses ) { PrivacyState privacyState = surveyResponse . getPrivacyState ( ) ; Collection < Collection < SurveyResponse > > outerCollection = privacyStateBucket . get ( privacyState ) ; if ( outerCollection == null ) { outerCollection = new LinkedList < Collection < SurveyResponse > > ( ) ; privacyStateBucket . put ( privacyState , outerCollection ) ; } Collection < SurveyResponse > innerCollection ; if ( outerCollection . size ( ) == 0 ) { innerCollection = new LinkedList < SurveyResponse > ( ) ; outerCollection . add ( innerCollection ) ; } else { innerCollection = outerCollection . iterator ( ) . next ( ) ; } innerCollection . add ( surveyResponse ) ; } if ( privacyStateGroupItems . contains ( FunctionPrivacyStateItem . DATE ) ) { LOGGER . info ( "Subdividing buckets by date." ) ; subdivideDate ( privacyStateBucket ) ; } if ( privacyStateGroupItems . contains ( FunctionPrivacyStateItem . SURVEY ) ) { LOGGER . info ( "Subdividing buckets by survey ID." ) ; subdivideSurveyId ( privacyStateBucket ) ; } try { JSONObject result = new JSONObject ( ) ; for ( PrivacyState privacyState : privacyStateBucket . keySet ( ) ) { Collection < Collection < SurveyResponse > > currPrivacyStateBucket = privacyStateBucket . get ( privacyState ) ; JSONArray jsonBuckets = new JSONArray ( ) ; for ( Collection < SurveyResponse > currBucket : currPrivacyStateBucket ) { JSONObject jsonBucket = new JSONObject ( ) ; jsonBucket . put ( "count" , currBucket . size ( ) ) ; if ( privacyStateGroupItems . contains ( FunctionPrivacyStateItem . DATE ) ) { SurveyResponse surveyResponse = currBucket . iterator ( ) . next ( ) ; Calendar timezoneAdjustedCalendar = Calendar . getInstance ( surveyResponse . getTimezone ( ) . toTimeZone ( ) ) ; timezoneAdjustedCalendar . setTimeInMillis ( surveyResponse . getTime ( ) ) ; String dateString = timezoneAdjustedCalendar . get ( Calendar . YEAR ) + "-" + ( timezoneAdjustedCalendar . get ( Calendar . MONTH ) + 1 ) + "-" + timezoneAdjustedCalendar . get ( Calendar . DAY_OF_MONTH ) ; jsonBucket . put ( "date" , dateString ) ; } if ( privacyStateGroupItems . contains ( FunctionPrivacyStateItem . SURVEY ) ) { jsonBucket . put ( "survey_id" , currBucket . iterator ( ) . next ( ) . getSurvey ( ) . getId ( ) ) ; } jsonBuckets . put ( jsonBucket ) ; } result . put ( privacyState . toString ( ) , jsonBuckets ) ; } super . respond ( httpRequest , httpResponse , result ) ; } catch ( JSONException e ) { LOGGER . error ( "There was a problem creating the response." , e ) ; setFailed ( ) ; super . respond ( httpRequest , httpResponse , ( JSONObject ) null ) ; } }
This responds to a "privacy state" function request.
This responds to a " privacy state " function request .
// This maps each of the privacy states to a collection of collections // of survey responses. The inner collection is all of the survey  // responses that have the same parameters as specified by the  // 'privacyStateGroupItems'. The outer collection is the aggregation of // all of those inner collections associated with this privacy state. // For example, if the 'privacyStateGroupItems' is empty, then the  // outer collection would contain exactly one inner collection which // would be all survey responses with the privacy state for the key. // If the 'privacyStateGroupItems' contains a 'date' item, then for  // each key the outer collection is a collection of all of the survey // responses with the same privacy state and each inner collection is  // all of the survey responses with the same date. If a 'survey ID'  // item was also given, then each of the inner collections would be // those survey responses that share the same date and survey ID. // Cycle through all of the survey responses and populate their initial // buckets. // Get this response's privacy state. // Get the outer collection if it exists. // If it doesn't exist, create and add it. // The outer collection should have exactly zero or one inner  // collections. If it has zero, create the one. // If it already has one then it should have exactly one and we // retrieve that one. // Add the survey response to the inner collection. // Create the resulting JSONObject and populate it. // Create the array of buckets for this privacy state. // Cycle through each of the buckets of collections, create a  // JSONObject representing that bucket, and add it to the array for // this privacy state. // Get a representative survey response. // Create a calendar with the timezone from the  // response and set its time based on the epoch  // milliseconds. // Create the date string to represent this date based on // the creator's timezone at the time they took it.