src/org/ohmage/request/mobility/MobilityReadChunkedRequest.java
@Override
	public void respond(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
		LOGGER.info("Responding to the Mobility read chunked request.");
		
		Map<Long, List<MobilityPoint>> millisToPointMap =
				new HashMap<Long, List<MobilityPoint>>();
		
		// Bucket the items preserving order in the bucket.
		for(MobilityPoint mobilityPoint : result) {
			long time = mobilityPoint.getTime();
			
			// Get this point's bucket.
			time = (time / millisPerChunk) * millisPerChunk;
			
			List<MobilityPoint> bucket = millisToPointMap.get(time);
			
			if(bucket == null) {
				bucket = new LinkedList<MobilityPoint>();
				millisToPointMap.put(time, bucket);
			}
			
			bucket.add(mobilityPoint);
		}

		JSONArray outputArray = new JSONArray();
		
		// Process the buckets.
		try {
			for(Long time : millisToPointMap.keySet()) {
				Map<String, Integer> modeCountMap =
						new HashMap<String, Integer>();
				String timestamp = null;
				DateTimeZone timezone = null;
				LocationStatus locationStatus = null;
				Location location = null;
				
				for(MobilityPoint mobilityPoint : millisToPointMap.get(time)) {
					// The first point sets the information.
					if(timestamp == null) {
						timestamp = 
								DateTimeUtils.getIso8601DateString(
									mobilityPoint.getDate(), 
									true);
						timezone = mobilityPoint.getTimezone();
						
						locationStatus = mobilityPoint.getLocationStatus();
						location = mobilityPoint.getLocation();
					}
					
					// For all points, get the mode.
					String mode = mobilityPoint.getMode().toString().toLowerCase();
					Integer count = modeCountMap.get(mode);
					
					if(count == null) {
						modeCountMap.put(mode, 1);
					}
					else {
						modeCountMap.put(mode, count + 1);
					}
				}
				
				JSONObject currResult = new JSONObject();
				currResult.put(JSON_KEY_MODE_COUNT, modeCountMap);
				currResult.put(JSON_KEY_DURATION, millisPerChunk);
				currResult.put(JSON_KEY_TIMESTAMP, timestamp);
				currResult.put(JSON_KEY_TIMEZONE, timezone.getID());
				currResult.put(
						JSON_KEY_LOCATION_STATUS, 
						locationStatus.toString().toLowerCase());
				try {
					currResult.put(
							JSON_KEY_LOCATION, 
							((location == null) ? null : location.toJson(true, LocationColumnKey.ALL_COLUMNS)));
				} 
				catch(DomainException e) {
					LOGGER.error("Error creating the JSON.", e);
					setFailed();
				}
				
				outputArray.put(currResult);
			}
		}
		catch(JSONException e) {
			LOGGER.error("Error building the response." , e);
			setFailed();
		}
		
		super.respond(httpRequest, httpResponse, JSON_KEY_DATA, outputArray);
	}
/**
	 * Responds to the Mobility read chunked request.
	 */
Responds to the Mobility read chunked request.
Responds to the Mobility read chunked request .
@Override
	public void respond(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
		LOGGER.info("Responding to the Mobility read chunked request.");
		Map<Long, List<MobilityPoint>> millisToPointMap =
				new HashMap<Long, List<MobilityPoint>>();
		for(MobilityPoint mobilityPoint : result) {
			long time = mobilityPoint.getTime();
			time = (time / millisPerChunk) * millisPerChunk;
			List<MobilityPoint> bucket = millisToPointMap.get(time);
			if(bucket == null) {
				bucket = new LinkedList<MobilityPoint>();
				millisToPointMap.put(time, bucket);
			}
			bucket.add(mobilityPoint);
		}
		JSONArray outputArray = new JSONArray();
		try {
			for(Long time : millisToPointMap.keySet()) {
				Map<String, Integer> modeCountMap =
						new HashMap<String, Integer>();
				String timestamp = null;
				DateTimeZone timezone = null;
				LocationStatus locationStatus = null;
				Location location = null;
				for(MobilityPoint mobilityPoint : millisToPointMap.get(time)) {
					if(timestamp == null) {
						timestamp = 
								DateTimeUtils.getIso8601DateString(
									mobilityPoint.getDate(), 
									true);
						timezone = mobilityPoint.getTimezone();
						locationStatus = mobilityPoint.getLocationStatus();
						location = mobilityPoint.getLocation();
					}
					String mode = mobilityPoint.getMode().toString().toLowerCase();
					Integer count = modeCountMap.get(mode);
					if(count == null) {
						modeCountMap.put(mode, 1);
					}
					else {
						modeCountMap.put(mode, count + 1);
					}
				}
				JSONObject currResult = new JSONObject();
				currResult.put(JSON_KEY_MODE_COUNT, modeCountMap);
				currResult.put(JSON_KEY_DURATION, millisPerChunk);
				currResult.put(JSON_KEY_TIMESTAMP, timestamp);
				currResult.put(JSON_KEY_TIMEZONE, timezone.getID());
				currResult.put(
						JSON_KEY_LOCATION_STATUS, 
						locationStatus.toString().toLowerCase());
				try {
					currResult.put(
							JSON_KEY_LOCATION, 
							((location == null) ? null : location.toJson(true, LocationColumnKey.ALL_COLUMNS)));
				} 
				catch(DomainException e) {
					LOGGER.error("Error creating the JSON.", e);
					setFailed();
				}
				outputArray.put(currResult);
			}
		}
		catch(JSONException e) {
			LOGGER.error("Error building the response." , e);
			setFailed();
		}
		super.respond(httpRequest, httpResponse, JSON_KEY_DATA, outputArray);
	}
@ Override public void respond ( HttpServletRequest httpRequest , HttpServletResponse httpResponse ) { LOGGER . info ( "Responding to the Mobility read chunked request." ) ; Map < Long , List < MobilityPoint > > millisToPointMap = new HashMap < Long , List < MobilityPoint > > ( ) ; for ( MobilityPoint mobilityPoint : result ) { long time = mobilityPoint . getTime ( ) ; time = ( time / millisPerChunk ) * millisPerChunk ; List < MobilityPoint > bucket = millisToPointMap . get ( time ) ; if ( bucket == null ) { bucket = new LinkedList < MobilityPoint > ( ) ; millisToPointMap . put ( time , bucket ) ; } bucket . add ( mobilityPoint ) ; } JSONArray outputArray = new JSONArray ( ) ; try { for ( Long time : millisToPointMap . keySet ( ) ) { Map < String , Integer > modeCountMap = new HashMap < String , Integer > ( ) ; String timestamp = null ; DateTimeZone timezone = null ; LocationStatus locationStatus = null ; Location location = null ; for ( MobilityPoint mobilityPoint : millisToPointMap . get ( time ) ) { if ( timestamp == null ) { timestamp = DateTimeUtils . getIso8601DateString ( mobilityPoint . getDate ( ) , true ) ; timezone = mobilityPoint . getTimezone ( ) ; locationStatus = mobilityPoint . getLocationStatus ( ) ; location = mobilityPoint . getLocation ( ) ; } String mode = mobilityPoint . getMode ( ) . toString ( ) . toLowerCase ( ) ; Integer count = modeCountMap . get ( mode ) ; if ( count == null ) { modeCountMap . put ( mode , 1 ) ; } else { modeCountMap . put ( mode , count + 1 ) ; } } JSONObject currResult = new JSONObject ( ) ; currResult . put ( JSON_KEY_MODE_COUNT , modeCountMap ) ; currResult . put ( JSON_KEY_DURATION , millisPerChunk ) ; currResult . put ( JSON_KEY_TIMESTAMP , timestamp ) ; currResult . put ( JSON_KEY_TIMEZONE , timezone . getID ( ) ) ; currResult . put ( JSON_KEY_LOCATION_STATUS , locationStatus . toString ( ) . toLowerCase ( ) ) ; try { currResult . put ( JSON_KEY_LOCATION , ( ( location == null ) ? null : location . toJson ( true , LocationColumnKey . ALL_COLUMNS ) ) ) ; } catch ( DomainException e ) { LOGGER . error ( "Error creating the JSON." , e ) ; setFailed ( ) ; } outputArray . put ( currResult ) ; } } catch ( JSONException e ) { LOGGER . error ( "Error building the response." , e ) ; setFailed ( ) ; } super . respond ( httpRequest , httpResponse , JSON_KEY_DATA , outputArray ) ; }
Responds to the Mobility read chunked request.
Responds to the Mobility read chunked request .
// Bucket the items preserving order in the bucket. // Get this point's bucket. // Process the buckets. // The first point sets the information. // For all points, get the mode.