<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <changeSet id="1" author="asingh">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            DROP TRIGGER IF EXISTS after_update_vendor;;
            CREATE DEFINER = 'theorchard'@'10.10.%' TRIGGER after_update_vendor AFTER UPDATE ON vendor FOR EACH ROW 
            BEGIN
            IF (
                    NEW.company != OLD.company 
                    OR (ISNULL(NEW.company) AND NOT ISNULL(OLD.company))
                    OR (NOT ISNULL(NEW.company) AND ISNULL(OLD.company))
                   )
                   OR (
                    NEW.owner != OLD.owner
                    OR (ISNULL(NEW.owner) AND NOT ISNULL(OLD.owner))
                    OR (NOT ISNULL(NEW.owner) AND ISNULL(OLD.owner))
                   )
                   OR (
                    NEW.assigned_to != OLD.assigned_to
                    OR (ISNULL(NEW.assigned_to) AND NOT ISNULL(OLD.assigned_to))
                    OR (NOT ISNULL(NEW.assigned_to) AND ISNULL(OLD.assigned_to))
                   )
                   OR (
                    (
                        NEW.status != OLD.status
                        OR (ISNULL(NEW.owner) AND NOT ISNULL(OLD.owner))
                        OR (NOT ISNULL(NEW.owner) AND ISNULL(OLD.owner))
                    )
                    AND NEW.status IN ('signed', 'deletion')
                   )
                   OR (
                    NEW.label_identifier != OLD.label_identifier
                    OR (ISNULL(NEW.label_identifier) AND NOT ISNULL(OLD.label_identifier))
                    OR (NOT ISNULL(NEW.label_identifier) AND ISNULL(OLD.label_identifier))
                   )
                   THEN
                    CALL art_relations.sp_cloudsearch_upsert_subaccounts_by_vendor_id(NEW.vendor_id);
                    CALL art_relations.sp_cloudsearch_upsert_releases_by_vendor_id(NEW.vendor_id);
                    CALL art_relations.sp_cloudsearch_upsert_artists_by_vendor_id(NEW.vendor_id);
                    CALL cloudsearch.sp_upsert_vendor(NEW.vendor_id);
                END IF;
                INSERT INTO art_relations_log.vendor_log SET
                    log_timestamp = NOW(),
                    log_action = 'update',
                    vendor_id = NEW.vendor_id,
                    is_distributor= NEW.is_distributor,
                    NAME = NEW.name,
                    company = NEW.company,
                    newsletter = NEW.newsletter,
                    login = NEW.login,
                    old_passwords = NEW.old_passwords,
                    bademail = NEW.bademail,
                    last_update = NEW.last_update,
                    passwords = NEW.passwords,
                    is_login_null = NEW.is_login_null,
                    referral = NEW.referral,
                    join_vendor_id = NEW.join_vendor_id,
                    OWNER = NEW.owner,
                    priority = NEW.priority,
                    region = NEW.region,
                    primary_genre = NEW.primary_genre,
                    est_total_releases = NEW.est_total_releases,
                    est_total_tracks = NEW.est_total_tracks,
                    date_created = NEW.date_created,
                    overall_priority = NEW.overall_priority,
                    assigned_to = NEW.assigned_to,
                    STATUS = NEW.status,
                    website = NEW.website,
                    ca_rep = NEW.ca_rep,
                    tax_form_received = NEW.tax_form_received,
                    w8_tax_form_date = NEW.w8_tax_form_date,
                    date_signed = NEW.date_signed,
                    payment_type = NEW.payment_type,
                    wire_info = NEW.wire_info,
                    wel_email_sender = NEW.wel_email_sender,
                    wel_email_send_date = NEW.wel_email_send_date,
                    label_summary = NEW.label_summary,
                    myspace_url = NEW.myspace_url,
                    additional_id = NEW.additional_id,
                    monthly_accounting = NEW.monthly_accounting,
                    original_owner = NEW.original_owner,
                    show_release_builder = NEW.show_release_builder,
                    recurring_payment_threshold = NEW.recurring_payment_threshold,
                    encrypted_tin = NEW.encrypted_tin,
                    checks = NEW.checks,
                    tax_name = NEW.tax_name,
                    tax_id_type = NEW.tax_id_type,
                    tax_id_country = NEW.tax_id_country,
                    encrypted_foreign_tin = NEW.encrypted_foreign_tin,
                    country = NEW.country,
                    number_format = NEW.number_format,
                    LANGUAGE = NEW.language,
                    token = NEW.token,
                    allow_orchard_credit = NEW.allow_orchard_credit,
                    api_vendor_id = NEW.api_vendor_id,
                    contact_email = NEW.contact_email,
                    label_identifier = NEW.label_identifier,
                    is_owned = NEW.is_owned,
                    last_modified_by = NEW.last_modified_by,
                    support_contact_email = NEW.support_contact_email,
                    first_statement_period = NEW.first_statement_period,
                    quarterback_label_manager = NEW.quarterback_label_manager;
    
                  IF  (
		      NEW.assigned_to != OLD.assigned_to
                    OR (ISNULL(NEW.assigned_to) AND NOT ISNULL(OLD.assigned_to))
                    OR (NOT ISNULL(NEW.assigned_to) AND ISNULL(OLD.assigned_to))
		        )
		    THEN
			    UPDATE art_relations.subaccount SET last_updated = NOW()
			    WHERE vendor_id = NEW.vendor_id;
		    END IF;
            END;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
            USE art_relations;;

            DROP TRIGGER IF EXISTS after_update_vendor;;

            CREATE DEFINER = 'theorchard'@'10.10.%' TRIGGER after_update_vendor AFTER UPDATE ON vendor FOR EACH ROW

            BEGIN
                IF (
                    NEW.company != OLD.company 
                    OR (ISNULL(NEW.company) AND NOT ISNULL(OLD.company))
                    OR (NOT ISNULL(NEW.company) AND ISNULL(OLD.company))
                   )
                   OR (
                    NEW.owner != OLD.owner
                    OR (ISNULL(NEW.owner) AND NOT ISNULL(OLD.owner))
                    OR (NOT ISNULL(NEW.owner) AND ISNULL(OLD.owner))
                   )
                   OR (
                    NEW.assigned_to != OLD.assigned_to
                    OR (ISNULL(NEW.assigned_to) AND NOT ISNULL(OLD.assigned_to))
                    OR (NOT ISNULL(NEW.assigned_to) AND ISNULL(OLD.assigned_to))
                   )
                   OR (
                    (
                        NEW.status != OLD.status
                        OR (ISNULL(NEW.owner) AND NOT ISNULL(OLD.owner))
                        OR (NOT ISNULL(NEW.owner) AND ISNULL(OLD.owner))
                    )
                    AND NEW.status IN ('signed', 'deletion')
                   )
                   OR (
                    NEW.label_identifier != OLD.label_identifier
                    OR (ISNULL(NEW.label_identifier) AND NOT ISNULL(OLD.label_identifier))
                    OR (NOT ISNULL(NEW.label_identifier) AND ISNULL(OLD.label_identifier))
                   )
                   THEN
                    CALL art_relations.sp_cloudsearch_upsert_subaccounts_by_vendor_id(NEW.vendor_id);
                    CALL art_relations.sp_cloudsearch_upsert_releases_by_vendor_id(NEW.vendor_id);
                    CALL art_relations.sp_cloudsearch_upsert_artists_by_vendor_id(NEW.vendor_id);
                    CALL cloudsearch.sp_upsert_vendor(NEW.vendor_id);
                END IF;
                INSERT INTO art_relations_log.vendor_log SET
                    log_timestamp = NOW(),
                    log_action = 'update',
                    vendor_id = NEW.vendor_id,
                    is_distributor= NEW.is_distributor,
                    NAME = NEW.name,
                    company = NEW.company,
                    newsletter = NEW.newsletter,
                    login = NEW.login,
                    old_passwords = NEW.old_passwords,
                    bademail = NEW.bademail,
                    last_update = NEW.last_update,
                    passwords = NEW.passwords,
                    is_login_null = NEW.is_login_null,
                    referral = NEW.referral,
                    join_vendor_id = NEW.join_vendor_id,
                    OWNER = NEW.owner,
                    priority = NEW.priority,
                    region = NEW.region,
                    primary_genre = NEW.primary_genre,
                    est_total_releases = NEW.est_total_releases,
                    est_total_tracks = NEW.est_total_tracks,
                    date_created = NEW.date_created,
                    overall_priority = NEW.overall_priority,
                    assigned_to = NEW.assigned_to,
                    STATUS = NEW.status,
                    website = NEW.website,
                    ca_rep = NEW.ca_rep,
                    tax_form_received = NEW.tax_form_received,
                    w8_tax_form_date = NEW.w8_tax_form_date,
                    date_signed = NEW.date_signed,
                    payment_type = NEW.payment_type,
                    wire_info = NEW.wire_info,
                    wel_email_sender = NEW.wel_email_sender,
                    wel_email_send_date = NEW.wel_email_send_date,
                    label_summary = NEW.label_summary,
                    myspace_url = NEW.myspace_url,
                    additional_id = NEW.additional_id,
                    monthly_accounting = NEW.monthly_accounting,
                    original_owner = NEW.original_owner,
                    show_release_builder = NEW.show_release_builder,
                    recurring_payment_threshold = NEW.recurring_payment_threshold,
                    encrypted_tin = NEW.encrypted_tin,
                    checks = NEW.checks,
                    tax_name = NEW.tax_name,
                    tax_id_type = NEW.tax_id_type,
                    tax_id_country = NEW.tax_id_country,
                    encrypted_foreign_tin = NEW.encrypted_foreign_tin,
                    country = NEW.country,
                    number_format = NEW.number_format,
                    LANGUAGE = NEW.language,
                    token = NEW.token,
                    allow_orchard_credit = NEW.allow_orchard_credit,
                    api_vendor_id = NEW.api_vendor_id,
                    contact_email = NEW.contact_email,
                    label_identifier = NEW.label_identifier,
                    is_owned = NEW.is_owned,
                    last_modified_by = NEW.last_modified_by,
                    support_contact_email = NEW.support_contact_email,
                    first_statement_period = NEW.first_statement_period,
                    quarterback_label_manager = NEW.quarterback_label_manager;
            END;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
