use of com.amazon.ion.IonValue in project amazon-qldb-dmv-sample-java by aws-samples.
the class TransferVehicleOwnership method updateVehicleRegistration.
/**
* Update the primary owner for a vehicle registration with the given documentId.
*
* @param txn
* The {@link TransactionExecutor} for lambda execute.
* @param vin
* Unique VIN for a vehicle.
* @param documentId
* New PersonId for the primary owner.
* @throws IllegalStateException if no vehicle registration was found using the given document ID and VIN, or if failed
* to convert parameters into {@link IonValue}.
*/
public static void updateVehicleRegistration(final TransactionExecutor txn, final String vin, final String documentId) {
try {
log.info("Updating primary owner for vehicle with Vin: {}...", vin);
final String query = "UPDATE VehicleRegistration AS v SET v.Owners.PrimaryOwner = ? WHERE v.VIN = ?";
final List<IonValue> parameters = new ArrayList<>();
parameters.add(Constants.MAPPER.writeValueAsIonValue(new Owner(documentId)));
parameters.add(Constants.MAPPER.writeValueAsIonValue(vin));
Result result = txn.execute(query, parameters);
ScanTable.printDocuments(result);
if (result.isEmpty()) {
throw new IllegalStateException("Unable to transfer vehicle, could not find registration.");
} else {
log.info("Successfully transferred vehicle with VIN '{}' to new owner.", vin);
}
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
use of com.amazon.ion.IonValue in project amazon-qldb-dmv-sample-java by aws-samples.
the class SampleData method getDocumentId.
/**
* Get the document ID of a particular document.
*
* @param txn
* A transaction executor object.
* @param tableName
* Name of the table containing the document.
* @param identifier
* The identifier used to narrow down the search.
* @param value
* Value of the identifier.
* @return the list of document IDs in the result set.
*/
public static String getDocumentId(final TransactionExecutor txn, final String tableName, final String identifier, final String value) {
try {
final List<IonValue> parameters = Collections.singletonList(Constants.MAPPER.writeValueAsIonValue(value));
final String query = String.format("SELECT metadata.id FROM _ql_committed_%s AS p WHERE p.data.%s = ?", tableName, identifier);
Result result = txn.execute(query, parameters);
if (result.isEmpty()) {
throw new IllegalStateException("Unable to retrieve document ID using " + value);
}
return getStringValueOfStructField((IonStruct) result.iterator().next(), "id");
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
use of com.amazon.ion.IonValue in project amazon-qldb-dmv-sample-java by aws-samples.
the class AddSecondaryOwner method addSecondaryOwnerForVin.
/**
* Adds a secondary owner for the specified VIN.
*
* @param txn
* The {@link TransactionExecutor} for lambda execute.
* @param vin
* Unique VIN for a vehicle.
* @param secondaryOwner
* The secondary owner to add.
* @throws IllegalStateException if failed to convert parameter into an {@link IonValue}.
*/
public static void addSecondaryOwnerForVin(final TransactionExecutor txn, final String vin, final String secondaryOwner) {
try {
log.info("Inserting secondary owner for vehicle with VIN: {}...", vin);
final String query = String.format("FROM VehicleRegistration AS v WHERE v.VIN = '%s' " + "INSERT INTO v.Owners.SecondaryOwners VALUE ?", vin);
final IonValue newOwner = Constants.MAPPER.writeValueAsIonValue(new Owner(secondaryOwner));
Result result = txn.execute(query, newOwner);
log.info("VehicleRegistration Document IDs which had secondary owners added: ");
ScanTable.printDocuments(result);
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
use of com.amazon.ion.IonValue in project amazon-qldb-dmv-sample-java by aws-samples.
the class AddSecondaryOwner method isSecondaryOwnerForVehicle.
/**
* Check whether a secondary owner has already been registered for the given VIN.
*
* @param txn
* The {@link TransactionExecutor} for lambda execute.
* @param vin
* Unique VIN for a vehicle.
* @param secondaryOwnerId
* The secondary owner to add.
* @return {@code true} if the given secondary owner has already been registered, {@code false} otherwise.
* @throws IllegalStateException if failed to convert VIN to an {@link IonValue}.
*/
public static boolean isSecondaryOwnerForVehicle(final TransactionExecutor txn, final String vin, final String secondaryOwnerId) {
try {
log.info("Finding secondary owners for vehicle with VIN: {}...", vin);
final String query = "SELECT Owners.SecondaryOwners FROM VehicleRegistration AS v WHERE v.VIN = ?";
final List<IonValue> parameters = Collections.singletonList(Constants.MAPPER.writeValueAsIonValue(vin));
final Result result = txn.execute(query, parameters);
final Iterator<IonValue> itr = result.iterator();
if (!itr.hasNext()) {
return false;
}
final Owners owners = Constants.MAPPER.readValue(itr.next(), Owners.class);
if (null != owners.getSecondaryOwners()) {
for (Owner owner : owners.getSecondaryOwners()) {
if (secondaryOwnerId.equalsIgnoreCase(owner.getPersonId())) {
return true;
}
}
}
return false;
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
use of com.amazon.ion.IonValue in project ion-java by amzn.
the class BinaryWriterTest method testBinaryWriterReuseWithSymbols.
public void testBinaryWriterReuseWithSymbols(String symbol) throws Exception {
iw = makeWriter();
iw.writeSymbol(symbol);
iw.finish();
byte[] bytes1 = myOutputStream.toByteArray();
myOutputStream.reset();
IonValue dg1 = loader().load(bytes1);
iw.writeSymbol(symbol);
iw.finish();
byte[] bytes2 = myOutputStream.toByteArray();
myOutputStream.reset();
IonValue dg2 = loader().load(bytes2);
IonAssert.assertIonEquals(dg1, dg2);
Assert.assertArrayEquals(bytes1, bytes2);
iw.writeSymbol(symbol);
iw.finish();
byte[] bytes3 = myOutputStream.toByteArray();
IonValue dg3 = loader().load(bytes3);
IonAssert.assertIonEquals(dg1, dg3);
Assert.assertArrayEquals(bytes2, bytes3);
}
Aggregations