use of software.amazon.qldb.Result in project amazon-qldb-dmv-sample-java by aws-samples.
the class FindVehicles method findVehiclesForOwner.
/**
* Find vehicles registered under a driver using their government ID.
*
* @param txn
* The {@link TransactionExecutor} for lambda execute.
* @param govId
* The government ID of the owner.
* @throws IllegalStateException if failed to convert parameters into {@link IonValue}.
*/
public static void findVehiclesForOwner(final TransactionExecutor txn, final String govId) {
try {
final String documentId = Person.getDocumentIdByGovId(txn, govId);
final String query = "SELECT v FROM Vehicle AS v INNER JOIN VehicleRegistration AS r " + "ON v.VIN = r.VIN WHERE r.Owners.PrimaryOwner.PersonId = ?";
final Result result = txn.execute(query, Constants.MAPPER.writeValueAsIonValue(documentId));
log.info("List of Vehicles for owner with GovId: {}...", govId);
ScanTable.printDocuments(result);
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
use of software.amazon.qldb.Result in project amazon-qldb-dmv-sample-java by aws-samples.
the class GetRevision method queryRegistrationsByVin.
/**
* Query the registration history for the given VIN.
*
* @param txn
* The {@link TransactionExecutor} for lambda execute.
* @param vin
* The unique VIN to query.
* @return a list of {@link IonStruct} representing the registration history.
* @throws IllegalStateException if failed to convert parameters into {@link IonValue}
*/
public static List<IonStruct> queryRegistrationsByVin(final TransactionExecutor txn, final String vin) {
log.info(String.format("Let's query the 'VehicleRegistration' table for VIN: %s...", vin));
log.info("Let's query the 'VehicleRegistration' table for VIN: {}...", vin);
final String query = String.format("SELECT * FROM _ql_committed_%s WHERE data.VIN = ?", Constants.VEHICLE_REGISTRATION_TABLE_NAME);
try {
final List<IonValue> parameters = Collections.singletonList(Constants.MAPPER.writeValueAsIonValue(vin));
final Result result = txn.execute(query, parameters);
List<IonStruct> list = ScanTable.toIonStructs(result);
log.info(String.format("Found %d document(s)!", list.size()));
return list;
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
use of software.amazon.qldb.Result in project amazon-qldb-dmv-sample-java by aws-samples.
the class InsertIonTypes method updateRecordAndVerifyType.
/**
* Update a document's Name value in the database. Then, query the value of the Name key and verify the expected Ion type was
* saved.
*
* @param txn
* The {@link TransactionExecutor} for statement execution.
* @param ionValue
* The {@link IonValue} to set the document's Name value to.
*
* @throws AssertionError when no value is returned for the Name key or if the value does not match the expected type.
*/
public static void updateRecordAndVerifyType(final TransactionExecutor txn, final IonValue ionValue) {
final String updateStatement = String.format("UPDATE %s SET Name = ?", TABLE_NAME);
final List<IonValue> parameters = Collections.singletonList(ionValue);
txn.execute(updateStatement, parameters);
log.info("Updated document.");
final String searchQuery = String.format("SELECT VALUE Name FROM %s", TABLE_NAME);
final Result result = txn.execute(searchQuery);
if (result.isEmpty()) {
throw new AssertionError("Did not find any values for the Name key.");
}
for (IonValue value : result) {
if (!ionValue.getClass().isInstance(value)) {
throw new AssertionError(String.format("The queried value, %s, is not an instance of %s.", value.getClass().toString(), ionValue.getClass().toString()));
}
if (!value.getType().equals(ionValue.getType())) {
throw new AssertionError(String.format("The queried value type, %s, does not match %s.", value.getType().toString(), ionValue.getType().toString()));
}
}
log.info("Successfully verified value is instance of {} with type {}.", ionValue.getClass().toString(), ionValue.getType().toString());
}
use of software.amazon.qldb.Result in project amazon-qldb-dmv-sample-java by aws-samples.
the class RegisterDriversLicense method registerNewDriversLicense.
/**
* Register a new driver's license.
*
* @param txn
* The {@link TransactionExecutor} for lambda execute.
* @param govId
* The government ID of the new owner.
* @param license
* The new license to register.
* @param personId
* The unique personId of the new owner.
* @throws IllegalStateException if failed to convert document ID to an IonValue.
*/
public static void registerNewDriversLicense(final TransactionExecutor txn, final String govId, final DriversLicense license, final String personId) {
try {
if (personHadDriversLicense(txn, personId)) {
log.info("Person with government ID '{}' already has a license! No new license added.", govId);
return;
}
final String query = "INSERT INTO DriversLicense ?";
log.info(new IonObjectMapper().writeValueAsIonValue(license).toPrettyString());
final List<IonValue> parameters = Collections.singletonList(Constants.MAPPER.writeValueAsIonValue(license));
txn.execute(query, parameters);
Result result = queryDriversLicenseByPersonId(txn, govId);
if (ScanTable.toIonStructs(result).size() > 0) {
log.info("Problem occurred while inserting new license, please review the results.");
} else {
log.info("Successfully registered new driver.");
}
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
use of software.amazon.qldb.Result in project amazon-qldb-dmv-sample-java by aws-samples.
the class RegisterDriversLicense method personAlreadyExists.
/**
* Verify whether a driver already exists in the database.
*
* @param txn
* The {@link TransactionExecutor} for lambda execute.
* @param govId
* The government ID of the new owner.
* @return {@code true} if the driver has already been registered; {@code false} otherwise.
* @throws IOException if failed to convert parameter into an IonValue.
*/
public static boolean personAlreadyExists(final TransactionExecutor txn, final String govId) throws IOException {
final String query = "SELECT * FROM Person AS p WHERE p.GovId = ?";
final Result result = txn.execute(query, Constants.MAPPER.writeValueAsIonValue(govId));
return !result.isEmpty();
}
Aggregations