<?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="101-3" author="mdadhania">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            USE direct_delivery;;

            DROP PROCEDURE IF EXISTS sp_claim_asset_to_sync;;

            CREATE DEFINER='theorchard_dd'@'127.0.0.1' PROCEDURE sp_claim_asset_to_sync(IN sourceLocID INT, IN destLocID INT, IN strg_drive_id INT, IN drive_full INT, IN assetId INT, IN assetTypeId INT)
            BEGIN
                DECLARE sourceAssetLocationId INT DEFAULT 0;
                DECLARE dest_storage_drive INT DEFAULT 0;
                DECLARE db_asset_id INT DEFAULT 0;
                DECLARE row_cnt INT DEFAULT 0;
                
                START TRANSACTION;
                
                SELECT a.asset_id, al.storage_drive_id INTO db_asset_id, dest_storage_drive
                FROM asset a
                LEFT JOIN (asset_location al 
                    INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                    INNER JOIN `storage` s ON s.storage_id = sd.storage_id AND s.physical_location_id = destLocID
                ) ON a.asset_id = al.asset_id
                WHERE a.asset_id = assetId 
                    AND (al.storage_drive_id = strg_drive_id OR al.storage_drive_id IS NULL)
                    AND a.asset_type_id = assetTypeId;
                
                IF drive_full = 1 THEN
                    IF (dest_storage_drive > 0) THEN 
                        DELETE ald.* 
                        FROM asset_location_detail ald
                        INNER JOIN asset_location al ON ald.asset_location_id = al.asset_location_id
                        WHERE al.asset_id = assetId AND al.storage_drive_id = dest_storage_drive;
                    
                        DELETE FROM asset_location 
                        WHERE asset_id = assetId AND storage_drive_id = dest_storage_drive;
                    END IF;
                    
                    SELECT -2 AS success;
                ELSE
                    IF (dest_storage_drive > 0) THEN 
                        UPDATE asset_location SET `status` = 'syncing', last_modified = NOW()
                        WHERE asset_id = assetId AND storage_drive_id = dest_storage_drive AND `status` = 'N';
                        SELECT ROW_COUNT() INTO row_cnt;
                    ELSEIF (db_asset_id > 0) THEN
                        INSERT INTO asset_location (asset_id, storage_drive_id, `status`, upload_date, last_modified)                           
                        SELECT assetId, sd.storage_drive_id, 'syncing' AS status, NOW() AS upload, NOW() AS last_mod
                        FROM storage_drive sd
                        LEFT JOIN (`storage` s
                            INNER JOIN storage_drive sd2 ON sd2.storage_id = s.storage_id
                            INNER JOIN asset_location al ON al.asset_id = assetId 
                                AND sd2.storage_drive_id = al.storage_drive_id) 
                            ON s.physical_location_id = destLocID
                        WHERE al.asset_id IS NULL AND sd.storage_drive_id = strg_drive_id;
                        SELECT ROW_COUNT() INTO row_cnt;
                    END IF;
                    
                    IF (row_cnt > 0) THEN
                        SELECT al.asset_location_id, a.asset_id, a.upc, a.cd, a.track_id, a.clip_number, a.asset_type_id, 
                            sd.storage_drive_id, sd.remote_initial_dir, sda.initial_folder, strg_drive_id AS dest_drive_id, 
                            atp.file_type, atp.extension, atp.folder_structure, atp.initial_folder AS default_folder, 
                            s.domain_name, INET_NTOA(local_ip) AS local_ip, s.user_name, s.password, s.port, s.connection_type, s.authenticate_type
                        FROM asset_location al 
                        INNER JOIN asset a ON a.asset_id = al.asset_id
                        INNER JOIN asset_type atp ON atp.asset_type_id = a.asset_type_id
                        INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                        INNER JOIN `storage` s ON s.storage_id = sd.storage_id
                        LEFT JOIN storage_drive_asset_folder sda ON sda.storage_drive_id = sd.storage_drive_id 
                            AND sda.asset_type_id = a.asset_type_id
                        WHERE a.asset_id = assetId 
                            AND s.physical_location_id = sourceLocID 
                            AND al.status = 'Y' 
                        LIMIT 1;                                  
                    ELSE
                        SELECT -1 AS success;
                        ROLLBACK;
                    END IF;
                END IF;
                COMMIT;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                USE direct_delivery;;

                DROP PROCEDURE IF EXISTS sp_claim_asset_to_sync;;

                CREATE DEFINER='theorchard_dd'@'127.0.0.1' PROCEDURE sp_claim_asset_to_sync(IN sourceLocID INT, IN destLocID INT, IN strg_drive_id INT, IN drive_full INT, IN assetId INT, IN assetTypeId INT)
                BEGIN
                    DECLARE sourceAssetLocationId INT DEFAULT 0;
                    DECLARE dest_storage_drive INT DEFAULT 0;
                    
                    START TRANSACTION;
                    
                    SELECT al.storage_drive_id INTO dest_storage_drive
                    FROM asset a
                    LEFT JOIN (asset_location al 
                        INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                        INNER JOIN `storage` s ON s.storage_id = sd.storage_id AND s.physical_location_id = destLocID
                    ) ON a.asset_id = al.asset_id
                    WHERE al.asset_id = assetId 
                        AND (al.storage_drive_id = strg_drive_id OR al.storage_drive_id IS NULL)
                        AND a.asset_type_id = assetTypeId;
                    
                    IF drive_full = 1 THEN
                        IF (dest_storage_drive > 0) THEN 
                            DELETE ald.* 
                            FROM asset_location_detail ald
                            INNER JOIN asset_location al ON ald.asset_location_id = al.asset_location_id
                            WHERE al.asset_id = assetId AND al.storage_drive_id = dest_storage_drive;
                        
                            DELETE FROM asset_location 
                            WHERE asset_id = assetId AND storage_drive_id = dest_storage_drive;
                        END IF;
                        
                        SELECT -2 AS success;
                    ELSE
                        IF (dest_storage_drive > 0) THEN 
                            UPDATE asset_location SET `status` = 'syncing', last_modified = NOW()
                            WHERE asset_id = assetId AND storage_drive_id = dest_storage_drive AND `status` = 'N';
                        ELSE
                            INSERT INTO asset_location (asset_id, storage_drive_id, `status`, upload_date, last_modified)                           
                            SELECT assetId, sd.storage_drive_id, 'syncing' AS status, NOW() AS upload, NOW() AS last_mod
                            FROM storage_drive sd
                            LEFT JOIN (`storage` s
                                INNER JOIN storage_drive sd2 ON sd2.storage_id = s.storage_id
                                INNER JOIN asset_location al ON al.asset_id = assetId 
                                    AND sd2.storage_drive_id = al.storage_drive_id) 
                                ON s.physical_location_id = destLocID
                            WHERE al.asset_id IS NULL AND sd.storage_drive_id = strg_drive_id;
                        END IF;
                        
                        IF ROW_COUNT() > 0 THEN
                            SELECT al.asset_location_id, a.asset_id, a.upc, a.cd, a.track_id, a.clip_number, a.asset_type_id, 
                                sd.storage_drive_id, sd.remote_initial_dir, sda.initial_folder, strg_drive_id AS dest_drive_id, 
                                atp.file_type, atp.extension, atp.folder_structure, atp.initial_folder AS default_folder, 
                                s.domain_name, INET_NTOA(local_ip) AS local_ip, s.user_name, s.password, s.port, s.connection_type, s.authenticate_type
                            FROM asset_location al 
                            INNER JOIN asset a ON a.asset_id = al.asset_id
                            INNER JOIN asset_type atp ON atp.asset_type_id = a.asset_type_id
                            INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                            INNER JOIN `storage` s ON s.storage_id = sd.storage_id
                            LEFT JOIN storage_drive_asset_folder sda ON sda.storage_drive_id = sd.storage_drive_id 
                                AND sda.asset_type_id = a.asset_type_id
                            WHERE a.asset_id = assetId 
                                AND s.physical_location_id = sourceLocID 
                                AND al.status = 'Y' 
                            LIMIT 1;
                        ELSE
                            SELECT -1 AS success;
                            ROLLBACK;
                        END IF;
                    END IF;
                    COMMIT;
                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
