/* * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package software.amazon.qldb.tutorial; import com.amazon.ion.IonReader; import com.amazon.ion.IonStruct; import com.amazon.ion.IonValue; import com.amazon.ion.system.IonReaderBuilder; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.qldb.Result; import software.amazon.qldb.TransactionExecutor; import software.amazon.qldb.tutorial.model.Owner; import software.amazon.qldb.tutorial.model.Person; import software.amazon.qldb.tutorial.model.SampleData; /** * Find primary owner for a particular vehicle's VIN. * Transfer to another primary owner for a particular vehicle's VIN. * * This code expects that you have AWS credentials setup per: * http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html */ public final class TransferVehicleOwnership { public static final Logger log = LoggerFactory.getLogger(TransferVehicleOwnership.class); private TransferVehicleOwnership() { } /** * Query a driver's information using the given ID. * * @param txn * The {@link TransactionExecutor} for lambda execute. * @param documentId * The unique ID of a document in the Person table. * @return a {@link Person} object. * @throws IllegalStateException if failed to convert parameter into {@link IonValue}. */ public static Person findPersonFromDocumentId(final TransactionExecutor txn, final String documentId) { try { log.info("Finding person for documentId: {}...", documentId); final String query = "SELECT p.* FROM Person AS p BY pid WHERE pid = ?"; Result result = txn.execute(query, Constants.MAPPER.writeValueAsIonValue(documentId)); if (result.isEmpty()) { throw new IllegalStateException("Unable to find person with ID: " + documentId); } return Constants.MAPPER.readValue(result.iterator().next(), Person.class); } catch (IOException ioe) { throw new IllegalStateException(ioe); } } /** * Find the primary owner for the given VIN. * * @param txn * The {@link TransactionExecutor} for lambda execute. * @param vin * Unique VIN for a vehicle. * @return a {@link Person} object. * @throws IllegalStateException if failed to convert parameter into {@link IonValue}. */ public static Person findPrimaryOwnerForVehicle(final TransactionExecutor txn, final String vin) { try { log.info("Finding primary owner for vehicle with Vin: {}...", vin); final String query = "SELECT Owners.PrimaryOwner.PersonId FROM VehicleRegistration AS v WHERE v.VIN = ?"; final List parameters = Collections.singletonList(Constants.MAPPER.writeValueAsIonValue(vin)); Result result = txn.execute(query, parameters); final List documents = ScanTable.toIonStructs(result); ScanTable.printDocuments(documents); if (documents.isEmpty()) { throw new IllegalStateException("Unable to find registrations with VIN: " + vin); } final IonReader reader = IonReaderBuilder.standard().build(documents.get(0)); final String personId = Constants.MAPPER.readValue(reader, LinkedHashMap.class).get("PersonId").toString(); return findPersonFromDocumentId(txn, personId); } catch (IOException ioe) { throw new IllegalStateException(ioe); } } /** * 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 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); } } public static void main(final String... args) { final String vin = SampleData.VEHICLES.get(0).getVin(); final String primaryOwnerGovId = SampleData.PEOPLE.get(0).getGovId(); final String newPrimaryOwnerGovId = SampleData.PEOPLE.get(1).getGovId(); ConnectToLedger.getDriver().execute(txn -> { final Person primaryOwner = findPrimaryOwnerForVehicle(txn, vin); if (!primaryOwner.getGovId().equals(primaryOwnerGovId)) { // Verify the primary owner. throw new IllegalStateException("Incorrect primary owner identified for vehicle, unable to transfer."); } final String newOwner = Person.getDocumentIdByGovId(txn, newPrimaryOwnerGovId); updateVehicleRegistration(txn, vin, newOwner); }); log.info("Successfully transferred vehicle ownership!"); } }