<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
            http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

    <property name="cmObjectTypeLength" value="1024" dbms="postgresql,oracle"/>
    <property name="cmObjectTypeLength" value="255" dbms="mysql,mssql"/>

    <property name="cmFileEcmIdLength" value="4000" dbms="postgresql,oracle"/>
    <property name="cmFileEcmIdLength" value="255" dbms="mysql,mssql"/>

    <changeSet id="files-04-update-file-type-column" author="dmiller">
        <update tableName="acm_file">
            <column name="cm_file_type" value="complaint"/>
            <where>cm_file_name like &apos;Complaint%.pdf&apos;</where>
        </update>
        <update tableName="acm_file">
            <column name="cm_file_type" value="complaint_xml"/>
            <where>cm_file_mime_type like &apos;%form_complaint.xml%&apos;</where>
        </update>
        <update tableName="acm_file">
            <column name="cm_file_type" value="roi"/>
            <where>cm_file_name like &apos;Report_of_Investigation%.pdf&apos;</where>
        </update>
        <update tableName="acm_file">
            <column name="cm_file_type" value="roi_xml"/>
            <where>cm_file_mime_type like &apos;%form_roi.xml%&apos;</where>
        </update>
        <update tableName="acm_file">
            <column name="cm_file_type" value="close_case"/>
            <where>cm_file_name like &apos;Close_Case%.pdf&apos;</where>
        </update>
        <update tableName="acm_file">
            <column name="cm_file_type" value="close_case_xml"/>
            <where>cm_file_mime_type like &apos;%form_close_case.xml%&apos;</where>
        </update>
        <update tableName="acm_file">
            <column name="cm_file_type" value="close_complaint"/>
            <where>cm_file_name like &apos;Close_Complaint%.pdf&apos;</where>
        </update>
        <update tableName="acm_file">
            <column name="cm_file_type" value="close_complaint_xml"/>
            <where>cm_file_mime_type like &apos;%form_close_complaint.xml%&apos;</where>
        </update>
        <update tableName="acm_file">
            <column name="cm_file_type" value="attachment"/>
            <where>cm_file_type IS NULL</where>
        </update>
    </changeSet>

    <changeSet id="files-06-populate-container-folder" author="dmiller" dbms="oracle">
        <preConditions onFail="WARN">
            <and>
                <dbms type="oracle"/>
                <tableExists tableName="acm_complaint"/>
                <columnExists tableName="acm_complaint" columnName="cm_complaint_ecm_folder_id"/>
            </and>
        </preConditions>

        <sql>
            <comment>Get complaint folder ids</comment>
            INSERT INTO acm_container_folder
            SELECT seq_gen_identity.nextval AS cm_container_folder_id,
            cm_complaint_created AS cm_container_folder_created,
            cm_complaint_creator AS cm_container_folder_creator,
            cm_complaint_created AS cm_container_folder_modified,
            cm_complaint_creator AS cm_container_folder_modifier,
            'COMPLAINT' AS cm_object_type,
            cm_complaint_id AS cm_object_id,
            cm_complaint_ecm_folder_id AS cm_cmis_folder_id
            FROM acm_complaint
            WHERE cm_complaint_ecm_folder_id IS NOT NULL
        </sql>
    </changeSet>

    <changeSet id="files-07-populate-container-folder-from-case-file" author="dmiller" dbms="oracle">
        <preConditions onFail="WARN">
            <and>
                <dbms type="oracle"/>
                <tableExists tableName="acm_case_file"/>
                <columnExists tableName="acm_case_file" columnName="cm_case_ecm_folder_id"/>
            </and>
        </preConditions>
        <sql>
            <comment>Get case file folder ids</comment>
            INSERT INTO acm_container_folder
            SELECT seq_gen_identity.nextval AS cm_container_folder_id,
            cm_case_created AS cm_container_folder_created,
            cm_case_creator AS cm_container_folder_creator,
            cm_case_created AS cm_container_folder_modified,
            cm_case_creator AS cm_container_folder_modifier,
            'CASE_FILE' AS cm_object_type,
            cm_case_id AS cm_object_id,
            cm_case_ecm_folder_id AS cm_cmis_folder_id
            FROM acm_case_file
            WHERE cm_case_ecm_folder_id IS NOT NULL
        </sql>
    </changeSet>

    <changeSet id="files-08-populate-container-folder-from-user-profile" author="dmiller" dbms="oracle">
        <preConditions onFail="WARN">
            <and>
                <dbms type="oracle"/>
                <tableExists tableName="acm_user_org"/>
                <columnExists tableName="acm_user_org" columnName="cm_profile_ecm_folder_id"/>
            </and>
        </preConditions>
        <sql>
            INSERT INTO acm_container_folder
            SELECT seq_gen_identity.nextval AS cm_container_folder_id,
            systimestamp AS cm_container_folder_created,
            cm_user AS cm_container_folder_creator,
            systimestamp AS cm_container_folder_modified,
            cm_user AS cm_container_folder_modifier,
            'USER_ORG' AS cm_object_type,
            cm_user_org_id AS cm_object_id,
            cm_profile_ecm_folder_id AS cm_cmis_folder_id
            FROM acm_user_org
            WHERE cm_profile_ecm_folder_id IS NOT NULL
        </sql>
    </changeSet>

    <changeSet id="files-09-rename-acm-container-folder" author="dmiller" failOnError="false">
        <renameTable oldTableName="acm_container_folder" newTableName="acm_container"/>
    </changeSet>

    <changeSet id="files-11-populate-folder-from-container-folder" author="dmiller" dbms="oracle">
        <preConditions onFail="WARN">
            <and>
                <dbms type="oracle"/>
            </and>
        </preConditions>
        <sql>
            INSERT INTO acm_folder
            SELECT seq_gen_identity.nextval AS cm_folder_id,
            ac.cm_container_folder_created AS cm_folder_created,
            ac.cm_container_folder_creator AS cm_folder_creator,
            ac.cm_container_folder_modified AS cm_folder_modified,
            ac.cm_container_folder_modifier AS cm_folder_modifier,
            ac.cm_cmis_folder_id AS cm_cmis_folder_id,
            'ROOT' AS cm_folder_name,
            NULL AS cm_parent_folder_id
            FROM acm_container ac
        </sql>
    </changeSet>

    <changeSet id="files-13-populate-container-folder-id" author="dmiller" dbms="oracle">
        <!-- ANSI standard SQL, should work on any database -->
        <sql>
            UPDATE acm_container ac
            SET cm_folder_id =
            (SELECT af.cm_folder_id
            FROM acm_folder af
            WHERE af.cm_cmis_folder_id = ac.cm_cmis_folder_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_folder af
            WHERE af.cm_cmis_folder_id = ac.cm_cmis_folder_id)
            AND ac.cm_folder_id IS NULL
        </sql>
    </changeSet>

    <changeSet id="files-16-populate-file-version-from-file" author="dmiller" dbms="oracle">
        <preConditions onFail="WARN">
            <and>
                <dbms type="oracle"/>
            </and>
        </preConditions>
        <sql>
            INSERT INTO acm_file_version
            SELECT seq_gen_identity.nextval AS cm_file_version_id,
            af.cm_file_id AS cm_file_id,
            af.cm_file_created AS cm_file_version_created,
            af.cm_file_creator AS cm_file_version_creator,
            af.cm_file_modified AS cm_file_version_modified,
            af.cm_file_modifier AS cm_file_version_modifier,
            '1.0' AS cm_file_version_version_flag,
            -- can only get away with the below since this is how Alfresco does things.
            af.cm_file_ecm_id || ';1.0' AS cm_cmis_object_id
            FROM acm_file af
        </sql>
    </changeSet>

    <changeSet id="files-18-update-new-columns-in-acm-file" author="dmiller" dbms="postgresql,oracle">
        <preConditions>
            <and>
                <tableExists tableName="acm_object_association"/>
                <columnExists tableName="acm_object_association" columnName="cm_target_category"/>
            </and>
        </preConditions>
        <!-- ANSI standard SQL, should work on any database -->
        <sql>
            UPDATE acm_file af
            SET cm_file_category =
            (SELECT DISTINCT aoa.cm_target_category
            FROM acm_object_association aoa
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_target_category IS NOT NULL
            AND aoa.cm_target_id = af.cm_file_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_object_association aoa
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_target_category IS NOT NULL
            AND aoa.cm_target_id = af.cm_file_id)
            AND af.cm_file_category IS NULL
        </sql>
    </changeSet>
    <changeSet id="files-18-update-new-columns-in-acm-file-mssql" author="dmiller" dbms="mssql">
        <preConditions>
            <and>
                <tableExists tableName="acm_object_association"/>
                <columnExists tableName="acm_object_association" columnName="cm_target_category"/>
            </and>
        </preConditions>
        <!-- ANSI standard SQL, should work on any database -->
        <sql>
            UPDATE acm_file
            SET cm_file_category =
            (SELECT DISTINCT aoa.cm_target_category
            FROM acm_object_association aoa
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_target_category IS NOT NULL
            AND aoa.cm_target_id = acm_file.cm_file_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_object_association aoa
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_target_category IS NOT NULL
            AND aoa.cm_target_id = acm_file.cm_file_id)
            AND acm_file.cm_file_category IS NULL
        </sql>
    </changeSet>

    <changeSet id="files-19-fix-acm-file-category" author="dmiller" dbms="postgresql,oracle">
        <sql>
            UPDATE acm_file
            SET cm_file_category = INITCAP(cm_file_category)
            WHERE cm_file_category IS NOT NULL
        </sql>
        <sql>
            UPDATE acm_file
            SET cm_file_category = 'Document'
            WHERE cm_file_category IS NULL
            OR cm_file_category NOT IN ('Document', 'Correspondence')
        </sql>
        <addNotNullConstraint tableName="acm_file" columnName="cm_file_category" columnDataType="VARCHAR(128)"/>
    </changeSet>
    <changeSet id="files-19-fix-acm-file-category-mssql" author="dmiller" dbms="mssql">
        <sql>
            UPDATE acm_file
            SET cm_file_category = 'Document'
            WHERE cm_file_category IS NULL
            OR cm_file_category NOT IN ('Document', 'Correspondence')
        </sql>
        <addNotNullConstraint tableName="acm_file" columnName="cm_file_category" columnDataType="VARCHAR(128)"/>
    </changeSet>

    <!-- we have multiple acm_object_association rows for the same file (same file is linked to
         multiple containers, it happens when a complaint is merged into a new case file).
         So we will try to get the complaint folder id into the existing file objects, then
         create new files for each case file that also has a file from a complaint... I hope
         that explanation is clear :-)  Basically, in the future, each ACM_FILE belongs to just the
         one folder.  -->
    <changeSet id="files-20-update-file-folder-id-for-task" author="dmiller" dbms="postgresql,oracle">
        <sql>
            UPDATE acm_file af
            SET cm_folder_id =
            (SELECT ac.cm_folder_id
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'TASK'
            AND aoa.cm_parent_type = ac.cm_object_type
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = af.cm_file_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'TASK'
            AND aoa.cm_parent_type = ac.cm_object_type
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = af.cm_file_id)
            AND af.cm_folder_id IS NULL
        </sql>
    </changeSet>
    <changeSet id="files-20-update-file-folder-id-for-task-mssql" author="dmiller" dbms="mssql">
        <sql>
            UPDATE acm_file
            SET cm_folder_id =
            (SELECT ac.cm_folder_id
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'TASK'
            AND aoa.cm_parent_type = ac.cm_object_type
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = acm_file.cm_file_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'TASK'
            AND aoa.cm_parent_type = ac.cm_object_type
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = acm_file.cm_file_id)
            AND acm_file.cm_folder_id IS NULL
        </sql>
    </changeSet>

    <changeSet id="files-20-update-file-folder-id-for-complaint" author="dmiller" dbms="postgresql,oracle">
        <sql>
            UPDATE acm_file af
            SET cm_folder_id =
            (SELECT ac.cm_folder_id
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'COMPLAINT'
            AND aoa.cm_parent_type = ac.cm_object_type
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = af.cm_file_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'COMPLAINT'
            AND aoa.cm_parent_type = ac.cm_object_type
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = af.cm_file_id)
            AND af.cm_folder_id IS NULL
        </sql>
    </changeSet>
    <changeSet id="files-20-update-file-folder-id-for-complaint-mssql" author="dmiller" dbms="mssql">
        <sql>
            UPDATE acm_file
            SET cm_folder_id =
            (SELECT ac.cm_folder_id
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'COMPLAINT'
            AND aoa.cm_parent_type = ac.cm_object_type
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = acm_file.cm_file_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'COMPLAINT'
            AND aoa.cm_parent_type = ac.cm_object_type
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = acm_file.cm_file_id)
            AND acm_file.cm_folder_id IS NULL
        </sql>
    </changeSet>

    <changeSet id="files-21-update-file-folder-id-for-case" author="dmiller" dbms="postgresql,oracle">
        <sql>
            UPDATE acm_file af
            SET cm_folder_id =
            (SELECT ac.cm_folder_id
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type IN ('CASE_FILE', 'CASE')
            AND ac.cm_object_type = 'CASE_FILE'
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = af.cm_file_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type IN ('CASE_FILE', 'CASE')
            AND ac.cm_object_type = 'CASE_FILE'
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = af.cm_file_id)
            AND af.cm_folder_id IS NULL
        </sql>
    </changeSet>
    <changeSet id="files-21-update-file-folder-id-for-case-mssql" author="dmiller" dbms="mssql">
        <sql>
            UPDATE acm_file
            SET cm_folder_id =
            (SELECT ac.cm_folder_id
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type IN ('CASE_FILE', 'CASE')
            AND ac.cm_object_type = 'CASE_FILE'
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = acm_file.cm_file_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type IN ('CASE_FILE', 'CASE')
            AND ac.cm_object_type = 'CASE_FILE'
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = acm_file.cm_file_id)
            AND acm_file.cm_folder_id IS NULL
        </sql>
    </changeSet>

    <changeSet id="files-22-update-file-folder-id-for-profile-images" author="dmiller" dbms="postgresql,oracle">
        <sql>
            UPDATE acm_file af
            SET cm_folder_id =
            (SELECT ac.cm_folder_id
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'PROFILE_IMG'
            AND ac.cm_object_type = 'USER_ORG'
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = af.cm_file_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'PROFILE_IMG'
            AND ac.cm_object_type = 'USER_ORG'
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = af.cm_file_id)
            AND af.cm_folder_id IS NULL
        </sql>
    </changeSet>
    <changeSet id="files-22-update-file-folder-id-for-profile-images" author="dmiller" dbms="mssql">
        <sql>
            UPDATE acm_file
            SET cm_folder_id =
            (SELECT ac.cm_folder_id
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'PROFILE_IMG'
            AND ac.cm_object_type = 'USER_ORG'
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = acm_file.cm_file_id)
            WHERE EXISTS
            (SELECT 1
            FROM acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = 'PROFILE_IMG'
            AND ac.cm_object_type = 'USER_ORG'
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_target_id = acm_file.cm_file_id)
            AND acm_file.cm_folder_id IS NULL
        </sql>
    </changeSet>

    <!-- need to fix the unique key on acm_file before inserting the files from cases where those
         files are also in a complaint -->
    <changeSet id="files-23-unique-key-on-file-and-folder" author="dmiller" failOnError="false">
        <dropUniqueConstraint tableName="acm_file" constraintName="uk_version_series_id"/>
        <addUniqueConstraint tableName="acm_file" columnNames="cm_version_series_id, cm_folder_id" constraintName="uk_file_folder"/>
    </changeSet>

    <!-- add rows to acm_file for files that belong to case files, where the case file was created
         based on a complaint.  This is to fix the situation where we have one row in acm_file
         linked to both the old complaint and the new case file. -->
    <changeSet id="files-24-add-rows-for-case-files-from-complaints" author="dmiller" dbms="oracle">
        <preConditions onFail="WARN">
            <dbms type="oracle"/>
        </preConditions>
        <sql>
            INSERT INTO acm_file
            (
            cm_file_id,
            cm_file_status,
            cm_file_created,
            cm_file_creator,
            cm_file_modified,
            cm_file_modifier,
            cm_file_mime_type,
            cm_version_series_id,
            cm_file_name,
            cm_file_type,
            cm_folder_id,
            cm_file_active_version_tag,
            cm_file_category
            )
            WITH dupes AS
            (
            SELECT aoa.cm_target_id,
            COUNT(*) as dupe_count
            FROM acm_container ac,
            acm_object_association aoa
            WHERE aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_type = ac.cm_object_type
            AND aoa.cm_parent_id = ac.cm_object_id
            GROUP BY aoa.cm_target_id
            having count(*) > 1
            )
            SELECT seq_gen_identity.nextval as cm_file_id,
            af.cm_file_status,
            af.cm_file_created,
            af.cm_file_creator,
            af.cm_file_modified,
            af.cm_file_modifier,
            af.cm_file_mime_type,
            af.cm_version_series_id,
            af.cm_file_name,
            af.cm_file_type,
            ac.cm_folder_id,
            af.cm_file_active_version_tag,
            af.cm_file_category
            FROM acm_file af,
            acm_object_association aoa,
            acm_container ac
            WHERE aoa.cm_parent_type= 'CASE_FILE'
            AND aoa.cm_target_type = 'FILE'
            AND aoa.cm_parent_id = ac.cm_object_id
            AND aoa.cm_parent_type = ac.cm_object_type
            AND af.cm_file_id IN (SELECT d.cm_target_id FROM dupes d)
            AND aoa.cm_target_id = af.cm_file_id
        </sql>
    </changeSet>

    <!-- any acm_file rows where the cm_folder_id is still null is an orphan... there's no way we can
         link them to a folder, short of writing a batch script to crawl Alfresco.  So we will
         remove them.  -->
    <changeSet id="files-25-remove-orphan-files" author="dmiller">
        <delete tableName="acm_file_version">
            <where>cm_file_id IN (SELECT cm_file_id FROM acm_file WHERE cm_folder_id IS NULL)</where>
        </delete>
        <delete tableName="acm_file">
            <where>cm_folder_id IS NULL</where>
        </delete>
    </changeSet>

    <changeSet id="files-29-populate-file-container-id" author="dmiller" dbms="oracle">
        <sql>
            UPDATE acm_file af
            SET af.cm_container_id =
            (
            SELECT ac.cm_container_id
            FROM acm_folder folder,
            acm_container ac
            WHERE af.cm_folder_id = folder.cm_folder_id
            AND folder.cm_folder_id = ac.cm_folder_id
            )
            WHERE EXISTS
            (
            SELECT ac.cm_container_id
            FROM acm_folder folder,
            acm_container ac
            WHERE af.cm_folder_id = folder.cm_folder_id
            AND folder.cm_folder_id = ac.cm_folder_id
            )
        </sql>
    </changeSet>

    <changeSet id="files-32-update-container-title-case-files" author="dmiller" dbms="oracle">
        <preConditions onFail="MARK_RAN">
            <and>
                <tableExists tableName="acm_case_file"/>
                <tableExists tableName="acm_container"/>
                <columnExists tableName="acm_container" columnName="cm_object_title"/>
            </and>
        </preConditions>
        <sql>
            UPDATE acm_container c
            SET cm_object_title =
            (
            SELECT acf.cm_case_number
            FROM acm_case_file acf
            WHERE c.cm_object_id = acf.cm_case_id
            AND c.cm_object_type = 'CASE_FILE'
            )
            WHERE EXISTS
            (
            SELECT 1
            FROM acm_case_file acf
            WHERE c.cm_object_id = acf.cm_case_id
            AND c.cm_object_type = 'CASE_FILE'
            )
            AND c.cm_object_title IS NULL
        </sql>
    </changeSet>

    <changeSet id="files-33-update-container-title-complaints" author="dmiller" dbms="oracle">
        <preConditions onFail="MARK_RAN">
            <and>
                <tableExists tableName="acm_complaint"/>
                <tableExists tableName="acm_container"/>
                <columnExists tableName="acm_container" columnName="cm_object_title"/>
            </and>
        </preConditions>
        <sql>
            UPDATE acm_container c
            SET cm_object_title =
            (
            SELECT ac.cm_complaint_number
            FROM acm_complaint ac
            WHERE c.cm_object_id = ac.cm_complaint_id
            AND c.cm_object_type = 'COMPLAINT'
            )
            WHERE EXISTS
            (
            SELECT 1
            FROM acm_complaint ac
            WHERE c.cm_object_id = ac.cm_complaint_id
            AND c.cm_object_type = 'COMPLAINT'
            )
            AND c.cm_object_title IS NULL
        </sql>
    </changeSet>

    <changeSet id="files-34-update-container-title-user-profiles" author="dmiller" dbms="oracle">
        <preConditions onFail="MARK_RAN">
            <and>
                <tableExists tableName="acm_user_org"/>
                <tableExists tableName="acm_container"/>
                <columnExists tableName="acm_container" columnName="cm_object_title"/>
            </and>
        </preConditions>
        <sql>
            UPDATE acm_container c
            SET cm_object_title =
            (
            SELECT auo.cm_user
            FROM acm_user_org auo
            WHERE c.cm_object_id = auo.cm_user_org_id
            AND c.cm_object_type = 'USER_ORG'
            )
            WHERE EXISTS
            (
            SELECT 1
            FROM acm_user_org auo
            WHERE c.cm_object_id = auo.cm_user_org_id
            AND c.cm_object_type = 'USER_ORG'
            )
            AND c.cm_object_title IS NULL
        </sql>
    </changeSet>

    <!-- This SQL is in general a REALLY BAD IDEA!!! We shouldn't query Activiti directly!
         I'm only doing this since it's only needed because I keep changing the document management
         data model during implementation.  -Dave -->
    <changeSet id="files-35-update-container-title-tasks" author="dmiller" dbms="oracle">
        <preConditions onFail="MARK_RAN">
            <and>
                <tableExists tableName="act_hi_taskinst"/>
                <tableExists tableName="acm_container"/>
                <columnExists tableName="acm_container" columnName="cm_object_title"/>
            </and>
        </preConditions>
        <sql>
            UPDATE acm_container c
            SET cm_object_title =
            (
            SELECT aht.name_
            FROM act_hi_taskinst aht
            WHERE c.cm_object_type = 'TASK'
            AND TO_CHAR(c.cm_object_id) = aht.id_
            )
            WHERE EXISTS
            (
            SELECT 1
            FROM act_hi_taskinst aht
            WHERE c.cm_object_type = 'TASK'
            AND TO_CHAR(c.cm_object_id) = aht.id_
            )
            AND c.cm_object_title IS NULL
        </sql>
    </changeSet>

    <changeSet id="files-38-update-attachment-folder-id-in-container-table" author="riste.tutureski" dbms="oracle">
        <preConditions onFail="MARK_RAN">
            <and>
                <tableExists tableName="acm_container"/>
                <columnExists tableName="acm_container" columnName="cm_folder_id"/>
                <columnExists tableName="acm_container" columnName="cm_attachment_folder_id"/>
            </and>
        </preConditions>
        <sql>
            UPDATE acm_container c
            SET c.cm_attachment_folder_id = c.cm_folder_id
            WHERE c.cm_attachment_folder_id IS NULL
        </sql>
    </changeSet>

    <changeSet id="files-39-acm_file_version-add-mime-type" author="sasko.tanaskoski" dbms="postgresql">
        <sql>
            UPDATE acm_file_version fv
            SET cm_file_version_mime_type =
            (
            SELECT f.cm_file_mime_type
            FROM acm_file f
            WHERE fv.cm_file_id = f.cm_file_id
            )
            WHERE EXISTS
            (
            SELECT 1
            FROM acm_file f
            WHERE fv.cm_file_id = f.cm_file_id
            )
            AND fv.cm_file_version_mime_type = 'MIMETYPE'
        </sql>
        <renameColumn tableName="acm_file" oldColumnName="cm_file_mime_type" newColumnName="cm_file_active_version_mime_type"
            columnDataType="VARCHAR(1024)"/>
        <addColumn tableName="acm_file">
            <column name="cm_file_active_version_name_extension" type="VARCHAR(255)"/>
        </addColumn>
    </changeSet>
    <changeSet id="files-39-acm_file_version-add-mime-type" author="sasko.tanaskoski" dbms="mysql,oracle">
        <renameColumn tableName="acm_file" oldColumnName="cm_file_mime_type" newColumnName="cm_file_active_version_mime_type"
            columnDataType="VARCHAR(1024)"/>
        <addColumn tableName="acm_file">
            <column name="cm_file_active_version_name_extension" type="VARCHAR(255)"/>
        </addColumn>
    </changeSet>
    <changeSet id="files-39-acm_file_version-add-mime-type" author="sasko.tanaskoski" dbms="mssql">
        <sql>
            UPDATE acm_file_version
            SET cm_file_version_mime_type =
            (
            SELECT f.cm_file_mime_type
            FROM acm_file f

            WHERE acm_file_version.cm_file_id = f.cm_file_id
            )
            WHERE EXISTS
            (
            SELECT 1
            FROM acm_file f
            WHERE acm_file_version.cm_file_id = f.cm_file_id
            )
            AND acm_file_version.cm_file_version_mime_type = 'MIMETYPE'
        </sql>
        <renameColumn tableName="acm_file" oldColumnName="cm_file_mime_type" newColumnName="cm_file_active_version_mime_type"
            columnDataType="VARCHAR(1024)"/>
        <addColumn tableName="acm_file">
            <column name="cm_file_active_version_name_extension" type="VARCHAR(255)"/>
        </addColumn>
    </changeSet>

    <changeSet id="files-40-acm-file-pop-extension" author="dmiller" dbms="postgresql">
        <sql>
            UPDATE acm_file
            SET cm_file_active_version_name_extension =
            SUBSTR(cm_file_name, LENGTH(cm_file_name) - STRPOS(REVERSE(cm_file_name), '.') + 1)
            WHERE cm_file_active_version_name_extension IS NULL;
        </sql>
    </changeSet>
    <changeSet id="files-40-acm-file-pop-extension" author="dmiller" dbms="mssql">
        <sql>
            UPDATE acm_file
            SET cm_file_active_version_name_extension =
            SUBSTRING(cm_file_name, LEN(cm_file_name) - CHARINDEX('.', REVERSE(cm_file_name)) + 1, CHARINDEX('.', REVERSE(cm_file_name)) +
            1)
            WHERE cm_file_active_version_name_extension IS NULL;
        </sql>
    </changeSet>

    <!-- this SQL accounts for the common MIME types as found on the core dev site -->
    <changeSet id="files-41-acm-file-version-pop-extension" author="dmiller" dbms="postgresql,mssql">
        <sql>
            UPDATE acm_file_version
            SET cm_file_version_name_extension = CASE
            WHEN (cm_file_version_mime_type like 'text/xml%') then '.xml'
            WHEN (cm_file_version_mime_type like 'image/png%') then '.png'
            WHEN (cm_file_version_mime_type like 'image/jpeg%') then '.jpg'
            WHEN (cm_file_version_mime_type like 'application/pdf%') then '.pdf'
            WHEN (cm_file_version_mime_type like 'text/plain%') then '.txt'
            WHEN (cm_file_version_mime_type like
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document%') then '.docx'
            WHEN (cm_file_version_mime_type like 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet%')
            then '.xlsx'
            WHEN (cm_file_version_mime_type like
            'application/vnd.openxmlformats-officedocument.presentationml.presentation%') then '.pptx'
            WHEN (cm_file_version_mime_type like 'application/msword') then '.doc'
            WHEN (cm_file_version_mime_type like 'text/x-java') then '.java'
            ELSE NULL
            END
            WHERE cm_file_version_name_extension IS NULL;
        </sql>
    </changeSet>

    <changeSet id="files-42-add-file-and-folder-discriminator" author="ilinpetar">
        <addColumn tableName="acm_file">
            <column name="cm_file_cmis_repository_id" type="VARCHAR(255)" defaultValue="alfresco">
                <constraints nullable="false"/>
            </column>
        </addColumn>
        <addColumn tableName="acm_folder">
            <column name="cm_folder_cmis_repository_id" type="VARCHAR(255)" defaultValue="alfresco">
                <constraints nullable="false"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="files-43-add-container-discriminator" author="ilinpetar" dbms="oracle,postgresql,mssql">
        <addColumn tableName="acm_container">
            <column name="cm_cmis_repository_id" type="VARCHAR(255)" defaultValue="alfresco">
                <constraints nullable="false"/>
            </column>
        </addColumn>
        <dropUniqueConstraint tableName="acm_container" constraintName="acm_container_folder_cm_object_id_cm_object_type_key"/>
        <addUniqueConstraint tableName="acm_container" columnNames="cm_object_id, cm_object_type, cm_cmis_repository_id"/>
    </changeSet>

    <changeSet id="files-43-add-container-discriminator" author="ilinpetar" dbms="mysql">
        <addColumn tableName="acm_container">
            <column name="cm_cmis_repository_id" type="VARCHAR(255)" defaultValue="alfresco">
                <constraints nullable="false"/>
            </column>
        </addColumn>
        <dropUniqueConstraint tableName="acm_container" constraintName="cm_object_id"/>
        <addUniqueConstraint tableName="acm_container" columnNames="cm_object_id, cm_object_type, cm_cmis_repository_id"/>
    </changeSet>

    <changeSet id="files-4819-update-other-file-type" author="dmiller">
        <update tableName="acm_file">
            <column name="cm_file_type" value="other"/>
            <column name="cm_file_modified" valueComputed="${timestampFunction}"/>
            <where>cm_file_type = 'Other'</where>
        </update>
    </changeSet>

</databaseChangeLog>

