<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="alfresco.content">

    <!--                -->
    <!-- Type Defs      -->
    <!--                -->
    
    <typeAlias alias="Mimetype" type="org.alfresco.repo.domain.mimetype.MimetypeEntity"/>
    <typeAlias alias="Encoding" type="org.alfresco.repo.domain.encoding.EncodingEntity"/>
    <typeAlias alias="ContentUrl" type="org.alfresco.repo.domain.contentdata.ContentUrlEntity"/>
    <typeAlias alias="ContentData" type="org.alfresco.repo.domain.contentdata.ContentDataEntity"/>
    <typeAlias alias="ContentClean" type="org.alfresco.repo.domain.contentclean.ContentCleanEntity"/>
    <typeAlias alias="Ids" type="org.alfresco.ibatis.IdsEntity"/>

    <!--                -->
    <!-- Result Maps    -->
    <!--                -->

    <resultMap id="result_Mimetype" class="Mimetype">
        <result property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="version" column="version" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="mimetype" column="mimetype_str" jdbcType="VARCHAR" javaType="java.lang.String"/>
    </resultMap>
  
    <resultMap id="result_Encoding" class="Encoding">
        <result property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="version" column="version" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="encoding" column="encoding_str" jdbcType="VARCHAR" javaType="java.lang.String"/>
    </resultMap>
  
    <resultMap id="result_ContentUrl" class="ContentUrl">
        <result property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="contentUrl" column="content_url" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="contentUrlShort" column="content_url_short" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="contentUrlCrc" column="content_url_crc" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="size" column="content_size" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="orphanTime" column="orphan_time" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </resultMap>
  
    <resultMap id="result_ContentData" class="ContentData">
        <result property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="version" column="version" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="contentUrlId" column="content_url_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="contentUrl" column="content_url" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="size" column="content_size" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="mimetypeId" column="content_mimetype_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="encodingId" column="content_encoding_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="localeId" column="content_locale_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </resultMap>
  
    <!--                -->
    <!-- Parameter Maps -->
    <!--                -->

    <parameterMap id="parameter_IdMap" class="map">
        <parameter property="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>
  
    <parameterMap id="parameter_NodeAndQNameMap" class="map">
        <parameter property="nodeId" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="qnameId" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>
  
    <parameterMap id="parameter_ContentUrl" class="ContentUrl">
        <parameter property="contentUrl" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <parameter property="contentUrlShort" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <parameter property="contentUrlCrc" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="size" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="orphanTime" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>

    <parameterMap id="parameter_ContentData" class="ContentData">
        <parameter property="version" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="contentUrlId" jdbcType="BIGINT" javaType="java.lang.Long"/>
		<parameter property="mimetypeId" jdbcType="BIGINT" javaType="java.lang.Long"/>
		<parameter property="encodingId" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="localeId" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>
  
    <!--                -->
    <!-- SQL Snippets   -->
    <!--                -->
    
    <sql id="insert_Mimetype_AutoIncrement">
        insert into alf_mimetype (version, mimetype_str) 
        values (#version#, #mimetype#)
    </sql>
    
    <sql id="insert_Encoding_AutoIncrement">
        insert into alf_encoding (version, encoding_str) 
        values (#version#, #encoding#)
    </sql>
    
    <sql id="insert_ContentUrl_AutoIncrement">
        insert into alf_content_url (content_url, content_url_short, content_url_crc, content_size, orphan_time) 
        values (?, ?, ?, ?, ?)
    </sql>
    
    <sql id="insert_ContentData_AutoIncrement">
        insert into alf_content_data (version, content_url_id, content_mimetype_id, content_encoding_id, content_locale_id) 
        values (?, ?, ?, ?, ?)
    </sql>
    
    <!--                -->
    <!-- Statements     -->
    <!--                -->
    
    <!-- Get the mimetype entity by mimetype -->
    <select id="select_MimetypeByKey" parameterClass="Mimetype" resultMap="result_Mimetype">
        select
            *
        from
            alf_mimetype
        where
            mimetype_str = #mimetype#
    </select>

    <!-- Get a mimetype based on the ID -->
    <select id="select_MimetypeById" parameterClass="Mimetype" resultMap="result_Mimetype">
        select
            *
        from
            alf_mimetype
        where
            id = #id#
    </select>

    <!-- Get the encoding entity by encoding -->
    <select id="select_EncodingByKey" parameterClass="Encoding" resultMap="result_Encoding">
        select
            *
        from
            alf_encoding
        where
            encoding_str = #encoding#
    </select>

    <!-- Get a encoding based on the ID -->
    <select id="select_EncodingById" parameterClass="Encoding" resultMap="result_Encoding">
        select
            *
        from
            alf_encoding
        where
            id = #id#
    </select>

    <!-- Get the content URL entity by ID -->
    <select id="select_ContentUrlById" parameterClass="ContentUrl" resultMap="result_ContentUrl">
        select
            *
        from
            alf_content_url
        where
            id = #id#
    </select>

    <!-- Get the content URL entity by unique key -->
    <select id="select_ContentUrlByKey" parameterClass="ContentUrl" resultMap="result_ContentUrl">
        select
            *
        from
            alf_content_url
        where
            content_url_short = #contentUrlShort# and
            content_url_crc = #contentUrlCrc#
    </select>

    <!-- Get unreferenced content URL entity by unique key -->
    <select id="select_ContentUrlByKeyUnreferenced" parameterClass="ContentUrl" resultMap="result_ContentUrl">
        select
            cu.*
        from
            alf_content_url cu
            left outer join alf_content_data cd on (cd.content_url_id = cu.id)
        where
            content_url_short = #contentUrlShort# and
            content_url_crc = #contentUrlCrc# and
            cd.id is null
    </select>
    
    <!-- Get content URL entities that were orphaned before a give time -->
    <select id="select_ContentUrlByOrphanTime" parameterClass="ContentUrl" resultMap="result_ContentUrl">
        <![CDATA[
        select
            cu.*
        from
            alf_content_url cu
        where
            cu.orphan_time <= #orphanTime# and cu.orphan_time is not null
        ]]>
    </select>
    
    <!-- Mark a content URL entity as orphaned -->
    <update id="update_ContentUrlOrphanTime" parameterClass="ContentUrl">
        update
            alf_content_url
        set
            orphan_time = #orphanTime#
        where
            id = #id#
    </update>

    <!-- Delete ContentUrl entity -->
    <delete id="delete_ContentUrls" parameterClass="list">
        delete
        from
            alf_content_url
        where
            id in
        <iterate open="(" close=")" conjunction=",">
            #[]#
        </iterate>
    </delete>

    <!-- Delete ContentUrl entities orphaned before a given time -->
    <delete id="delete_ContentUrlByOrphanTime" parameterClass="ContentUrl">
        <![CDATA[
        delete
        from
            alf_content_url
        where
            orphan_time <= #orphanTime# and orphan_time is not null
        ]]>
    </delete>

    <!-- Get the ContentData entity by ID -->
    <select id="select_ContentDataById" parameterMap="parameter_IdMap" resultMap="result_ContentData">
        select
            cd.id as id,
            cd.version as version,
            cd.content_url_id as content_url_id,
            cu.content_url as content_url,
            cu.content_size as content_size,
            cd.content_mimetype_id as content_mimetype_id,
            cd.content_encoding_id as content_encoding_id,
            cd.content_locale_id as content_locale_id
        from
            alf_content_data cd
            left join alf_content_url cu on (cd.content_url_id = cu.id)
        where
            cd.id = ?
    </select>

    <!-- Get the ContentData entity by Node and property QName -->
    <select id="select_ContentDataByNodeAndQName" parameterClass="Ids" resultClass="long">
        select
            np.long_value as id
        from
            alf_node_properties np
        where
            np.node_id = #idOne# and
            np.qname_id in <iterate property="ids" open="(" close=")" conjunction=",">#ids[]#</iterate> and
            (np.actual_type_n = 3 or np.actual_type_n = 21)
    </select>
    
    <update id="update_ContentData" parameterClass="ContentData">
        update
           alf_content_data
        set
           version = #version#,
           content_url_id = #contentUrlId#,
           content_mimetype_id = #mimetypeId#,
           content_encoding_id = #encodingId#,
           content_locale_id = #localeId#
        where
           id = #id#
        <isGreaterThan property="version" compareValue="1">
            and version = (#version#-1)
        </isGreaterThan>
    </update>

    <!-- Delete ContentData entity -->
    <delete id="delete_ContentData" parameterMap="parameter_IdMap">
        delete
        from
            alf_content_data
        where
            id = ?
    </delete>

</sqlMap>