<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:/var/app/InputFileSchema.xsd">
    <changeset id="NR-675_featured_contributions:1" author="rshield" run-on-change="true">
        <precondition>
            <sqlquery>
                <![CDATA[
                    SELECT 
                        rc.id AS EXTERNALID,
                        rc.recording_id AS EXTERNALSOUNDRECORDINGID,
                        rc.party_id AS EXTERNALCONTRIBUTORID,
                        t.iso_alpha_2_code AS COUNTRYOFPERFORMANCE,
                        an.name AS STAGENAME,
                        rcrd.definition AS INSTRUMENT
                        FROM awal_sax.sax.recording_contributors rc
                        INNER JOIN awal_sax.sax.recordings r 
                        ON rc.recording_id = r.id
                        INNER JOIN awal_sax.sax.interested_parties ip
                        ON rc.party_id = ip.id
                        LEFT JOIN awal_sax.sax.artist_names an
                        ON rc.performance_name_artist_id = an.artist_id
                        LEFT JOIN awal_sax.sax.territories t
                        ON rc.contribution_territory_id = t.id
                        LEFT JOIN awal_sax.sax.record_con_roles rcr
                        ON rc.id = rcr.recording_contributor_id
                        LEFT JOIN awal_sax.sax.record_con_role_defs rcrd
                        ON rcr.nr_role_id = rcrd.id
                        WHERE rc.contributor_type IN ('NR', 'Both')
                        AND rc.category = 'Featured'
                        AND an.name_type_id = 'ANT5'
                        GROUP BY 1,2,3,4,5,6;
                ]]>
            </sqlquery>
        </precondition>
        <cypherquery>
            <![CDATA[
            MERGE(nrc:NrContribution {externalId: event.EXTERNALID})
            ON CREATE SET
                nrc.countryOfPerformance = event.COUNTRYOFPERFORMANCE,
                nrc.instruments =
                    CASE 
                        WHEN event.INSTRUMENT IS NOT NULL 
                        THEN [event.INSTRUMENT]
                        ELSE []
                    END,
                nrc.performanceName = event.STAGENAME,
                nrc.createdBy = 'database/NR-675_featured_contributions',
                nrc.createdAt = datetime(),
                nrc.lastModifiedAt = datetime(),
                nrc.lastModifiedBy = 'database/NR-675_featured_contributions',
                nrc.id = apoc.create.uuid()
            ON MATCH SET
                nrc.countryOfPerformance = event.COUNTRYOFPERFORMANCE,
                nrc.instruments = 
                    CASE 
                        WHEN event.INSTRUMENT IN nrc.instruments 
                        THEN nrc.instruments 
                        ELSE nrc.instruments + event.INSTRUMENT
                    END,
                nrc.performanceName = event.STAGENAME,
                nrc.lastModifiedAt = datetime(),
                nrc.lastModifiedBy = 'database/NR-675_featured_contributions'
            WITH nrc, event
            MATCH (nc:NrContributor {externalId: event.EXTERNALCONTRIBUTORID})
            MERGE (nc)-[r:HAS_FEATURED_CONTRIBUTION]->(nrc)
            ON CREATE SET
                r.createdBy = 'database/NR-675_featured_contributions',
                r.createdAt = datetime(),
                r.lastModifiedAt = datetime(),
                r.lastModifiedBy = 'database/NR-675_featured_contributions'
            ON MATCH SET
                r.lastModifiedAt = datetime(),
                r.lastModifiedBy = 'database/NR-675_featured_contributions'
            WITH nrc, event
            MATCH(nrs:NrSoundRecording {externalId: event.EXTERNALSOUNDRECORDINGID})
            MERGE (nrc)-[rc:CONTRIBUTED_TO]->(nrs)
            ON CREATE SET
                rc.createdBy = 'database/NR-675_featured_contributions',
                rc.createdAt = datetime(),
                rc.lastModifiedAt = datetime(),
                rc.lastModifiedBy = 'database/NR-675_featured_contributions'
            ON MATCH SET
                rc.lastModifiedAt = datetime(),
                rc.lastModifiedBy = 'database/NR-675_featured_contributions'
            ]]>
        </cypherquery>
    </changeset>
</changelog>
