/* * 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.ArrayList; import java.util.Collections; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.amazon.ion.IonValue; import software.amazon.qldb.TransactionExecutor; import software.amazon.qldb.tutorial.model.DriversLicense; import software.amazon.qldb.tutorial.model.SampleData; import software.amazon.qldb.tutorial.model.VehicleRegistration; /** * Insert documents into a table in a QLDB ledger. * * 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 InsertDocument { public static final Logger log = LoggerFactory.getLogger(InsertDocument.class); private InsertDocument() { } /** * Insert the given list of documents into the specified table and return the document IDs of the inserted documents. * * @param txn * The {@link TransactionExecutor} for lambda execute. * @param tableName * Name of the table to insert documents into. * @param documents * List of documents to insert into the specified table. * @return a list of document IDs. * @throws IllegalStateException if failed to convert documents into an {@link IonValue}. */ public static List insertDocuments(final TransactionExecutor txn, final String tableName, final List documents) { log.info("Inserting some documents in the {} table...", tableName); try { final String query = String.format("INSERT INTO %s ?", tableName); final IonValue ionDocuments = Constants.MAPPER.writeValueAsIonValue(documents); return SampleData.getDocumentIdsFromDmlResult(txn.execute(query, ionDocuments)); } catch (IOException ioe) { throw new IllegalStateException(ioe); } } /** * Update PersonIds in driver's licenses and in vehicle registrations using document IDs. * * @param documentIds * List of document IDs representing the PersonIds in DriversLicense and PrimaryOwners in VehicleRegistration. * @param licenses * List of driver's licenses to update. * @param registrations * List of registrations to update. */ public static void updatePersonId(final List documentIds, final List licenses, final List registrations) { for (int i = 0; i < documentIds.size(); ++i) { DriversLicense license = SampleData.LICENSES.get(i); VehicleRegistration registration = SampleData.REGISTRATIONS.get(i); licenses.add(SampleData.updatePersonIdDriversLicense(license, documentIds.get(i))); registrations.add(SampleData.updateOwnerVehicleRegistration(registration, documentIds.get(i))); } } public static void main(final String... args) { final List newDriversLicenses = new ArrayList<>(); final List newVehicleRegistrations = new ArrayList<>(); ConnectToLedger.getDriver().execute(txn -> { List documentIds = insertDocuments(txn, Constants.PERSON_TABLE_NAME, SampleData.PEOPLE); updatePersonId(documentIds, newDriversLicenses, newVehicleRegistrations); insertDocuments(txn, Constants.VEHICLE_TABLE_NAME, SampleData.VEHICLES); insertDocuments(txn, Constants.VEHICLE_REGISTRATION_TABLE_NAME, Collections.unmodifiableList(newVehicleRegistrations)); insertDocuments(txn, Constants.DRIVERS_LICENSE_TABLE_NAME, Collections.unmodifiableList(newDriversLicenses)); }); log.info("Documents inserted successfully!"); } }