# 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. # # This code expects that you have AWS credentials setup per: # https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html from logging import basicConfig, getLogger, INFO from pyqldbsamples.model.sample_data import to_ion_struct, get_document_ids, print_result, SampleData, \ convert_object_to_ion from pyqldbsamples.constants import Constants from pyqldbsamples.connect_to_ledger import create_qldb_driver logger = getLogger(__name__) basicConfig(level=INFO) def get_document_id_by_gov_id(driver, government_id): """ Find a driver's person ID using the given government ID. :type driver: :py:class:`pyqldb.driver.qldb_driver.QldbDriver` :param driver: An instance of the QldbDriver class. :type government_id: str :param government_id: A driver's government ID. :rtype: list :return: A list of document IDs. """ logger.info("Finding secondary owner's person ID using given government ID: {}.".format(government_id)) return driver.execute_lambda(lambda executor: get_document_ids(executor, Constants.PERSON_TABLE_NAME, 'GovId', government_id)) def is_secondary_owner_for_vehicle(driver, vin, secondary_owner_id): """ Check whether a secondary owner has already been registered for the given VIN. :type driver: :py:class:`pyqldb.driver.qldb_driver.QldbDriver` :param driver: An instance of the QldbDriver class. :type vin: str :param vin: VIN of the vehicle to query. :type secondary_owner_id: str :param secondary_owner_id: The secondary owner's person ID. :rtype: bool :return: If the driver has already been registered. """ logger.info('Finding secondary owners for vehicle with VIN: {}...'.format(vin)) query = 'SELECT Owners.SecondaryOwners FROM VehicleRegistration AS v WHERE v.VIN = ?' rows = driver.execute_lambda(lambda executor: executor.execute_statement(query, convert_object_to_ion(vin))) for row in rows: secondary_owners = row.get('SecondaryOwners') person_ids = map(lambda owner: owner.get('PersonId').text, secondary_owners) if secondary_owner_id in person_ids: return True return False def add_secondary_owner_for_vin(driver, vin, parameter): """ Add a secondary owner into `VehicleRegistration` table for a particular VIN. :type driver: :py:class:`pyqldb.driver.qldb_driver.QldbDriver` :param driver: An instance of the QldbDriver class. :type vin: str :param vin: VIN of the vehicle to add a secondary owner for. :type parameter: :py:class:`amazon.ion.simple_types.IonPyValue` :param parameter: The Ion value or Python native type that is convertible to Ion for filling in parameters of the statement. """ logger.info('Inserting secondary owner for vehicle with VIN: {}...'.format(vin)) statement = "FROM VehicleRegistration AS v WHERE v.VIN = ? INSERT INTO v.Owners.SecondaryOwners VALUE ?" cursor = driver.execute_lambda(lambda executor: executor.execute_statement(statement, convert_object_to_ion(vin), parameter)) logger.info('VehicleRegistration Document IDs which had secondary owners added: ') print_result(cursor) def register_secondary_owner(driver, vin, gov_id): """ Register a secondary owner for a vehicle if they are not already registered. :type driver: :py:class:`pyqldb.driver.qldb_driver.QldbDriver` :param driver: An instance of the QldbDriver class. :type vin: str :param vin: VIN of the vehicle to register a secondary owner for. :type gov_id: str :param gov_id: The government ID of the owner. """ logger.info('Finding the secondary owners for vehicle with VIN: {}.'.format(vin)) document_ids = get_document_id_by_gov_id(driver, gov_id) for document_id in document_ids: if is_secondary_owner_for_vehicle(driver, vin, document_id): logger.info('Person with ID {} has already been added as a secondary owner of this vehicle.'.format(gov_id)) else: add_secondary_owner_for_vin(driver, vin, to_ion_struct('PersonId', document_id)) def main(ledger_name=Constants.LEDGER_NAME): """ Finds and adds secondary owners for a vehicle. """ vin = SampleData.VEHICLE[1]['VIN'] gov_id = SampleData.PERSON[0]['GovId'] try: with create_qldb_driver(ledger_name) as driver: register_secondary_owner(driver, vin, gov_id) logger.info('Secondary owners successfully updated.') except Exception as e: logger.exception('Error adding secondary owner.') raise e if __name__ == '__main__': main()