/* * 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 java.io.IOException; import java.util.Collections; import java.util.Iterator; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.amazon.ion.IonValue; import software.amazon.qldb.Result; import software.amazon.qldb.TransactionExecutor; import software.amazon.qldb.tutorial.model.Owner; import software.amazon.qldb.tutorial.model.Owners; import software.amazon.qldb.tutorial.model.Person; import software.amazon.qldb.tutorial.model.SampleData; /** * Finds and adds secondary owners for a vehicle. * * 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 AddSecondaryOwner { public static final Logger log = LoggerFactory.getLogger(AddSecondaryOwner.class); private AddSecondaryOwner() { } /** * 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 parameters = Collections.singletonList(Constants.MAPPER.writeValueAsIonValue(vin)); final Result result = txn.execute(query, parameters); final Iterator 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); } } /** * 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 = ?" + "INSERT INTO v.Owners.SecondaryOwners VALUE ?"); final IonValue newOwner = Constants.MAPPER.writeValueAsIonValue(new Owner(secondaryOwner)); final IonValue vinAsIonValue = Constants.MAPPER.writeValueAsIonValue(vin); Result result = txn.execute(query, vinAsIonValue, newOwner); log.info("VehicleRegistration Document IDs which had secondary owners added: "); ScanTable.printDocuments(result); } catch (IOException ioe) { throw new IllegalStateException(ioe); } } public static void main(final String... args) { final String vin = SampleData.VEHICLES.get(1).getVin(); final String govId = SampleData.PEOPLE.get(0).getGovId(); ConnectToLedger.getDriver().execute(txn -> { final String documentId = Person.getDocumentIdByGovId(txn, govId); if (isSecondaryOwnerForVehicle(txn, vin, documentId)) { log.info("Person with ID {} has already been added as a secondary owner of this vehicle.", govId); } else { addSecondaryOwnerForVin(txn, vin, documentId); } }); log.info("Secondary owners successfully updated."); } }