src/org/ohmage/query/impl/UserMobilityQueries.java
@Override
	public void createMobilityPoint(
			final String username, 
			final String client,
			final MobilityPoint mobilityPoint) 
			throws DataAccessException {
		
		// Create the transaction.
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setName("Creating a Mobility data point.");
		
		try {
			// Begin the transaction.
			PlatformTransactionManager transactionManager = new DataSourceTransactionManager(getDataSource());
			TransactionStatus status = transactionManager.getTransaction(def);
			
			JSONObject location = null;
			try {
				Location tLocation = mobilityPoint.getLocation();
				if(tLocation != null) {
					try {
						location = 
								tLocation.toJson(
										false, 
										LocationColumnKey.ALL_COLUMNS);
					}
					catch(DomainException e) {
						throw new DataAccessException(e);
					}
				}
			}
			catch(JSONException e) {
				throw new DataAccessException(e);
			}

			try {
				KeyHolder mobilityPointDatabaseKeyHolder = new GeneratedKeyHolder();
				getJdbcTemplate().update(
					new PreparedStatementCreator() {
						public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
							PreparedStatement ps = connection.prepareStatement(SQL_INSERT, new String[] {"id"});
							
							ps.setString(1, mobilityPoint.getId().toString());
							ps.setString(2, username);
							ps.setString(3, client);
							
							ps.setLong(4, mobilityPoint.getTime());
							ps.setString(5, mobilityPoint.getTimezone().getID());
							
							ps.setString(6, mobilityPoint.getLocationStatus().toString().toLowerCase());
							try {
								Location location = mobilityPoint.getLocation();
								ps.setString(7, ((location == null) ? null : location.toJson(false, LocationColumnKey.ALL_COLUMNS).toString()));
							} 
							catch(JSONException e) {
								throw new SQLException(
										"Could not create a JSONObject for the location.",
										e);
							}
							catch(DomainException e) {
								throw new SQLException(
										"Could not create a JSONObject for the location.",
										e);
							}
							
							ps.setString(8, mobilityPoint.getMode().toString().toLowerCase());
							
							ps.setString(9, mobilityPoint.getPrivacyState().toString());
							
							return ps;
						}
					},
					mobilityPointDatabaseKeyHolder
				);
				
				// If it's an extended entry, add the sensor data.
				if(SubType.SENSOR_DATA.equals(mobilityPoint.getSubType())) {
					JSONObject sensorData;
					try {
						sensorData = mobilityPoint.getSensorData().toJson(false, SensorDataColumnKey.ALL_COLUMNS);
					}
					catch(JSONException e) {
						throw new DataAccessException(e);
					}
					catch(DomainException e) {
						throw new DataAccessException(e);
					}
					
					JSONObject classifierData;
					try {
						ClassifierData tClassifierData = 
								mobilityPoint.getClassifierData();
						
						if(tClassifierData == null) {
							classifierData = null;
						}
						else {
							classifierData = 
									tClassifierData.toJson(
										false,
										ClassifierDataColumnKey.ALL_COLUMNS);
						}
					}
					catch(JSONException e) {
						throw new DataAccessException(e);
					}
					catch(DomainException e) {
						throw new DataAccessException(e);
					}
					
					try {
						getJdbcTemplate().update(
								SQL_INSERT_EXTENDED,
								mobilityPointDatabaseKeyHolder.getKey().longValue(),
								sensorData.toString(),
								(classifierData == null) ? (new JSONObject()).toString() : classifierData.toString(),
								MobilityClassifier.getVersion());
					}
					catch(org.springframework.dao.DataAccessException e) {
						transactionManager.rollback(status);
						throw new DataAccessException(
								"Error executing SQL '" + 
										SQL_INSERT_EXTENDED + 
									"' with parameters: " +
										mobilityPointDatabaseKeyHolder.getKey().longValue() + ", " +
										sensorData.toString() + ", " +
										((classifierData == null) ? (new JSONObject()).toString() : classifierData.toString()) + ", " +
										MobilityClassifier.getVersion(),
								e);
					}
				}
			}
			// If this is a duplicate upload, we will ignore it by rolling back
			// to where we were before we started and return.
			catch(org.springframework.dao.DataIntegrityViolationException e) {
				// FIXME: Now that we use UUIDs, the client should not be 
				// submitting duplicates. We probably want to, at the very 
				// least make a warning message and at most fail the request.
				if(! isDuplicate(e)) {
					transactionManager.rollback(status);
					throw new DataAccessException(
							"Error executing SQL '" + SQL_INSERT + "' with parameters: " +
								mobilityPoint.getId().toString() + ", " +
								username + ", " +
								client + ", " +
								mobilityPoint.getTime() + ", " +
								mobilityPoint.getTimezone().getID() + ", " +
								mobilityPoint.getLocationStatus().toString().toLowerCase() + ", " +
								((location == null) ? "null" : location.toString()) + ", " +
								mobilityPoint.getMode().toString().toLowerCase() + ", " +
								mobilityPoint.getPrivacyState(),
							e);
				}
			}
			catch(org.springframework.dao.DataAccessException e) {
				transactionManager.rollback(status);
				throw new DataAccessException(
						"Error executing SQL '" + SQL_INSERT + "' with parameters: " +
							mobilityPoint.getId().toString() + ", " +
							username + ", " +
							client + ", " +
							mobilityPoint.getTime() + ", " +
							mobilityPoint.getTimezone().getID() + ", " +
							mobilityPoint.getLocationStatus().toString().toLowerCase() + ", " +
							((location == null) ? "null" : location.toString()) + ", " +
							mobilityPoint.getMode().toString().toLowerCase() + ", " +
							mobilityPoint.getPrivacyState(),
						e);
			}
			
			// Commit the transaction.
			try {
				transactionManager.commit(status);
			}
			catch(TransactionException e) {
				transactionManager.rollback(status);
				throw new DataAccessException("Error while committing the transaction.", e);
			}
		}
		catch(TransactionException e) {
			throw new DataAccessException("Error while attempting to rollback the transaction.", e);
		}
	}
/**
	 * Creates a new Mobility point.
	 * 
	 * @param username The username of the user to which this point belongs.
	 * 
	 * @param client The client value given on upload.
	 * 
	 * @param mobilityPoint The Mobility point to be created.
	 * 
	 * @throws DataAccessException Thrown if there is an error.
	 */
Creates a new Mobility point.

@param username The username of the user to which this point belongs.

@param client The client value given on upload.

@param mobilityPoint The Mobility point to be created.

@throws DataAccessException Thrown if there is an error.
Creates a new Mobility point . @param username The username of the user to which this point belongs . @param client The client value given on upload . @param mobilityPoint The Mobility point to be created . @throws DataAccessException Thrown if there is an error .
@Override
	public void createMobilityPoint(
			final String username, 
			final String client,
			final MobilityPoint mobilityPoint) 
			throws DataAccessException {
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setName("Creating a Mobility data point.");
		try {
			PlatformTransactionManager transactionManager = new DataSourceTransactionManager(getDataSource());
			TransactionStatus status = transactionManager.getTransaction(def);
			JSONObject location = null;
			try {
				Location tLocation = mobilityPoint.getLocation();
				if(tLocation != null) {
					try {
						location = 
								tLocation.toJson(
										false, 
										LocationColumnKey.ALL_COLUMNS);
					}
					catch(DomainException e) {
						throw new DataAccessException(e);
					}
				}
			}
			catch(JSONException e) {
				throw new DataAccessException(e);
			}
			try {
				KeyHolder mobilityPointDatabaseKeyHolder = new GeneratedKeyHolder();
				getJdbcTemplate().update(
					new PreparedStatementCreator() {
						public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
							PreparedStatement ps = connection.prepareStatement(SQL_INSERT, new String[] {"id"});
							ps.setString(1, mobilityPoint.getId().toString());
							ps.setString(2, username);
							ps.setString(3, client);
							ps.setLong(4, mobilityPoint.getTime());
							ps.setString(5, mobilityPoint.getTimezone().getID());
							ps.setString(6, mobilityPoint.getLocationStatus().toString().toLowerCase());
							try {
								Location location = mobilityPoint.getLocation();
								ps.setString(7, ((location == null) ? null : location.toJson(false, LocationColumnKey.ALL_COLUMNS).toString()));
							} 
							catch(JSONException e) {
								throw new SQLException(
										"Could not create a JSONObject for the location.",
										e);
							}
							catch(DomainException e) {
								throw new SQLException(
										"Could not create a JSONObject for the location.",
										e);
							}
							ps.setString(8, mobilityPoint.getMode().toString().toLowerCase());
							ps.setString(9, mobilityPoint.getPrivacyState().toString());
							return ps;
						}
					},
					mobilityPointDatabaseKeyHolder
				);
				if(SubType.SENSOR_DATA.equals(mobilityPoint.getSubType())) {
					JSONObject sensorData;
					try {
						sensorData = mobilityPoint.getSensorData().toJson(false, SensorDataColumnKey.ALL_COLUMNS);
					}
					catch(JSONException e) {
						throw new DataAccessException(e);
					}
					catch(DomainException e) {
						throw new DataAccessException(e);
					}
					JSONObject classifierData;
					try {
						ClassifierData tClassifierData = 
								mobilityPoint.getClassifierData();
						if(tClassifierData == null) {
							classifierData = null;
						}
						else {
							classifierData = 
									tClassifierData.toJson(
										false,
										ClassifierDataColumnKey.ALL_COLUMNS);
						}
					}
					catch(JSONException e) {
						throw new DataAccessException(e);
					}
					catch(DomainException e) {
						throw new DataAccessException(e);
					}
					try {
						getJdbcTemplate().update(
								SQL_INSERT_EXTENDED,
								mobilityPointDatabaseKeyHolder.getKey().longValue(),
								sensorData.toString(),
								(classifierData == null) ? (new JSONObject()).toString() : classifierData.toString(),
								MobilityClassifier.getVersion());
					}
					catch(org.springframework.dao.DataAccessException e) {
						transactionManager.rollback(status);
						throw new DataAccessException(
								"Error executing SQL '" + 
										SQL_INSERT_EXTENDED + 
									"' with parameters: " +
										mobilityPointDatabaseKeyHolder.getKey().longValue() + ", " +
										sensorData.toString() + ", " +
										((classifierData == null) ? (new JSONObject()).toString() : classifierData.toString()) + ", " +
										MobilityClassifier.getVersion(),
								e);
					}
				}
			}
			catch(org.springframework.dao.DataIntegrityViolationException e) {
				if(! isDuplicate(e)) {
					transactionManager.rollback(status);
					throw new DataAccessException(
							"Error executing SQL '" + SQL_INSERT + "' with parameters: " +
								mobilityPoint.getId().toString() + ", " +
								username + ", " +
								client + ", " +
								mobilityPoint.getTime() + ", " +
								mobilityPoint.getTimezone().getID() + ", " +
								mobilityPoint.getLocationStatus().toString().toLowerCase() + ", " +
								((location == null) ? "null" : location.toString()) + ", " +
								mobilityPoint.getMode().toString().toLowerCase() + ", " +
								mobilityPoint.getPrivacyState(),
							e);
				}
			}
			catch(org.springframework.dao.DataAccessException e) {
				transactionManager.rollback(status);
				throw new DataAccessException(
						"Error executing SQL '" + SQL_INSERT + "' with parameters: " +
							mobilityPoint.getId().toString() + ", " +
							username + ", " +
							client + ", " +
							mobilityPoint.getTime() + ", " +
							mobilityPoint.getTimezone().getID() + ", " +
							mobilityPoint.getLocationStatus().toString().toLowerCase() + ", " +
							((location == null) ? "null" : location.toString()) + ", " +
							mobilityPoint.getMode().toString().toLowerCase() + ", " +
							mobilityPoint.getPrivacyState(),
						e);
			}
			try {
				transactionManager.commit(status);
			}
			catch(TransactionException e) {
				transactionManager.rollback(status);
				throw new DataAccessException("Error while committing the transaction.", e);
			}
		}
		catch(TransactionException e) {
			throw new DataAccessException("Error while attempting to rollback the transaction.", e);
		}
	}
@ Override public void createMobilityPoint ( final String username , final String client , final MobilityPoint mobilityPoint ) throws DataAccessException { DefaultTransactionDefinition def = new DefaultTransactionDefinition ( ) ; def . setName ( "Creating a Mobility data point." ) ; try { PlatformTransactionManager transactionManager = new DataSourceTransactionManager ( getDataSource ( ) ) ; TransactionStatus status = transactionManager . getTransaction ( def ) ; JSONObject location = null ; try { Location tLocation = mobilityPoint . getLocation ( ) ; if ( tLocation != null ) { try { location = tLocation . toJson ( false , LocationColumnKey . ALL_COLUMNS ) ; } catch ( DomainException e ) { throw new DataAccessException ( e ) ; } } } catch ( JSONException e ) { throw new DataAccessException ( e ) ; } try { KeyHolder mobilityPointDatabaseKeyHolder = new GeneratedKeyHolder ( ) ; getJdbcTemplate ( ) . update ( new PreparedStatementCreator ( ) { public PreparedStatement createPreparedStatement ( Connection connection ) throws SQLException { PreparedStatement ps = connection . prepareStatement ( SQL_INSERT , new String [ ] { "id" } ) ; ps . setString ( 1 , mobilityPoint . getId ( ) . toString ( ) ) ; ps . setString ( 2 , username ) ; ps . setString ( 3 , client ) ; ps . setLong ( 4 , mobilityPoint . getTime ( ) ) ; ps . setString ( 5 , mobilityPoint . getTimezone ( ) . getID ( ) ) ; ps . setString ( 6 , mobilityPoint . getLocationStatus ( ) . toString ( ) . toLowerCase ( ) ) ; try { Location location = mobilityPoint . getLocation ( ) ; ps . setString ( 7 , ( ( location == null ) ? null : location . toJson ( false , LocationColumnKey . ALL_COLUMNS ) . toString ( ) ) ) ; } catch ( JSONException e ) { throw new SQLException ( "Could not create a JSONObject for the location." , e ) ; } catch ( DomainException e ) { throw new SQLException ( "Could not create a JSONObject for the location." , e ) ; } ps . setString ( 8 , mobilityPoint . getMode ( ) . toString ( ) . toLowerCase ( ) ) ; ps . setString ( 9 , mobilityPoint . getPrivacyState ( ) . toString ( ) ) ; return ps ; } } , mobilityPointDatabaseKeyHolder ) ; if ( SubType . SENSOR_DATA . equals ( mobilityPoint . getSubType ( ) ) ) { JSONObject sensorData ; try { sensorData = mobilityPoint . getSensorData ( ) . toJson ( false , SensorDataColumnKey . ALL_COLUMNS ) ; } catch ( JSONException e ) { throw new DataAccessException ( e ) ; } catch ( DomainException e ) { throw new DataAccessException ( e ) ; } JSONObject classifierData ; try { ClassifierData tClassifierData = mobilityPoint . getClassifierData ( ) ; if ( tClassifierData == null ) { classifierData = null ; } else { classifierData = tClassifierData . toJson ( false , ClassifierDataColumnKey . ALL_COLUMNS ) ; } } catch ( JSONException e ) { throw new DataAccessException ( e ) ; } catch ( DomainException e ) { throw new DataAccessException ( e ) ; } try { getJdbcTemplate ( ) . update ( SQL_INSERT_EXTENDED , mobilityPointDatabaseKeyHolder . getKey ( ) . longValue ( ) , sensorData . toString ( ) , ( classifierData == null ) ? ( new JSONObject ( ) ) . toString ( ) : classifierData . toString ( ) , MobilityClassifier . getVersion ( ) ) ; } catch ( org . springframework . dao . DataAccessException e ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error executing SQL '" + SQL_INSERT_EXTENDED + "' with parameters: " + mobilityPointDatabaseKeyHolder . getKey ( ) . longValue ( ) + ", " + sensorData . toString ( ) + ", " + ( ( classifierData == null ) ? ( new JSONObject ( ) ) . toString ( ) : classifierData . toString ( ) ) + ", " + MobilityClassifier . getVersion ( ) , e ) ; } } } catch ( org . springframework . dao . DataIntegrityViolationException e ) { if ( ! isDuplicate ( e ) ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error executing SQL '" + SQL_INSERT + "' with parameters: " + mobilityPoint . getId ( ) . toString ( ) + ", " + username + ", " + client + ", " + mobilityPoint . getTime ( ) + ", " + mobilityPoint . getTimezone ( ) . getID ( ) + ", " + mobilityPoint . getLocationStatus ( ) . toString ( ) . toLowerCase ( ) + ", " + ( ( location == null ) ? "null" : location . toString ( ) ) + ", " + mobilityPoint . getMode ( ) . toString ( ) . toLowerCase ( ) + ", " + mobilityPoint . getPrivacyState ( ) , e ) ; } } catch ( org . springframework . dao . DataAccessException e ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error executing SQL '" + SQL_INSERT + "' with parameters: " + mobilityPoint . getId ( ) . toString ( ) + ", " + username + ", " + client + ", " + mobilityPoint . getTime ( ) + ", " + mobilityPoint . getTimezone ( ) . getID ( ) + ", " + mobilityPoint . getLocationStatus ( ) . toString ( ) . toLowerCase ( ) + ", " + ( ( location == null ) ? "null" : location . toString ( ) ) + ", " + mobilityPoint . getMode ( ) . toString ( ) . toLowerCase ( ) + ", " + mobilityPoint . getPrivacyState ( ) , e ) ; } try { transactionManager . commit ( status ) ; } catch ( TransactionException e ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error while committing the transaction." , e ) ; } } catch ( TransactionException e ) { throw new DataAccessException ( "Error while attempting to rollback the transaction." , e ) ; } }
Creates a new Mobility point.
Creates a new Mobility point .
// Create the transaction. // Begin the transaction. // If it's an extended entry, add the sensor data. // If this is a duplicate upload, we will ignore it by rolling back // to where we were before we started and return. // FIXME: Now that we use UUIDs, the client should not be  // submitting duplicates. We probably want to, at the very  // least make a warning message and at most fail the request. // Commit the transaction.