Search in sources :

Example 1 with Owner

use of software.amazon.qldb.tutorial.model.Owner 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);
    }
}
Also used : IonValue(com.amazon.ion.IonValue) Owner(software.amazon.qldb.tutorial.model.Owner) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Result(software.amazon.qldb.Result)

Example 2 with Owner

use of software.amazon.qldb.tutorial.model.Owner 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);
    }
}
Also used : IonValue(com.amazon.ion.IonValue) Owner(software.amazon.qldb.tutorial.model.Owner) IOException(java.io.IOException) Result(software.amazon.qldb.Result)

Example 3 with Owner

use of software.amazon.qldb.tutorial.model.Owner 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);
    }
}
Also used : IonValue(com.amazon.ion.IonValue) Owner(software.amazon.qldb.tutorial.model.Owner) IOException(java.io.IOException) Result(software.amazon.qldb.Result) Owners(software.amazon.qldb.tutorial.model.Owners)

Aggregations

IonValue (com.amazon.ion.IonValue)3 IOException (java.io.IOException)3 Result (software.amazon.qldb.Result)3 Owner (software.amazon.qldb.tutorial.model.Owner)3 ArrayList (java.util.ArrayList)1 Owners (software.amazon.qldb.tutorial.model.Owners)1