<?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.activities">

   <typeAlias alias="ActivityFeed" type="org.alfresco.repo.domain.activities.ActivityFeedEntity"/>
   <typeAlias alias="FeedControl" type="org.alfresco.repo.domain.activities.FeedControlEntity"/>
   <typeAlias alias="ActivityPost" type="org.alfresco.repo.domain.activities.ActivityPostEntity"/>
   
   <resultMap id="ActivityFeedResult" class="ActivityFeed">
      <result property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
      <result property="feedUserId" column="feed_user_id"/>
      <result property="postUserId" column="post_user_id"/>
      <result property="postDate" column="post_date"/>
      <result property="postId" column="post_id"/> <!-- not an explicit FK constraint, can dangle if and when activity post is deleted -->
      <result property="siteNetwork" column="site_network"/>
      <result property="activityType" column="activity_type"/>
      <result property="activitySummary" column="activity_summary"/>
      <result property="activitySummaryFormat" column="activity_format"/>
      <result property="feedDate" column="feed_date"/>
   </resultMap>
   
   <resultMap id="FeedControlResult" class="FeedControl">
      <result property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
      <result property="feedUserId" column="feed_user_id"/>
      <result property="siteNetwork" column="site_network"/>
      <result property="appTool" column="app_tool"/>
      <result property="lastModified" column="last_modified"/>
   </resultMap>
   
   <resultMap id="ActivityPostResult" class="ActivityPost">
      <result property="id" column="sequence_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
      <result property="activityData" column="activity_data"/>
      <result property="activityType" column="activity_type"/>
      <result property="userId" column="post_user_id"/>
      <result property="postDate" column="post_date"/>
      <result property="jobTaskNode" column="job_task_node"/>
      <result property="siteNetwork" column="site_network"/>
      <result property="appTool" column="app_tool"/>
      <result property="status" column="status"/>
      <result property="lastModified" column="last_modified"/>
   </resultMap>
   
   <!-- select feeds for cleaning -->
   <select id="select_activity_feed_greater_than_max" parameterClass="int" resultClass="ActivityFeed">
      <![CDATA[
      select site_network as siteNetwork, feed_user_id as feedUserId, activity_format as activitySummaryFormat
      from alf_activity_feed
      group by site_network, feed_user_id, activity_format
      having count(*) > #maxFeedSize#
      ]]>
   </select>
   
   <!-- user feed - all sites - everyone -->
   <select id="select_activity_feed_for_feeduser" parameterClass="ActivityFeed" resultClass="ActivityFeed">
      <![CDATA[
      select id as id, activity_type as activityType, activity_format as activitySummaryFormat, activity_summary as activitySummary, feed_user_id as feedUserId, post_user_id as postUserId, site_network as siteNetwork, post_date as postDate
      from alf_activity_feed
      where feed_user_id = #feedUserId#
      and activity_format = #activitySummaryFormat#
      order by post_date desc
      ]]>
   </select>
   
   <!-- user feed - all sites - others (not me) -->
   <select id="select_activity_feed_for_feeduser_others" parameterClass="ActivityFeed" resultClass="ActivityFeed">
      <![CDATA[
      select id as id, activity_type as activityType, activity_format as activitySummaryFormat, activity_summary as activitySummary, feed_user_id as feedUserId, post_user_id as postUserId, site_network as siteNetwork, post_date as postDate
      from alf_activity_feed
      where feed_user_id = #feedUserId#
      and post_user_id != #feedUserId#
      and activity_format = #activitySummaryFormat#
      order by post_date desc
      ]]>
   </select>
   
   <!-- user feed - all sites - me only (not others) -->
   <select id="select_activity_feed_for_feeduser_me" parameterClass="ActivityFeed" resultClass="ActivityFeed">
      <![CDATA[
      select id as id, activity_type as activityType, activity_format as activitySummaryFormat, activity_summary as activitySummary, feed_user_id as feedUserId, post_user_id as postUserId, site_network as siteNetwork, post_date as postDate
      from alf_activity_feed
      where feed_user_id = #feedUserId#
      and post_user_id = #feedUserId#
      and activity_format = #activitySummaryFormat#
      order by post_date desc
      ]]>
   </select>
   
   <!-- user feed - given site - everyone -->
   <select id="select_activity_feed_for_feeduser_and_site" parameterClass="ActivityFeed" resultClass="ActivityFeed">
      <![CDATA[
      select id as id, activity_type as activityType, activity_format as activitySummaryFormat, activity_summary as activitySummary, feed_user_id as feedUserId, post_user_id as postUserId, site_network as siteNetwork, post_date as postDate
      from alf_activity_feed
      where feed_user_id = #feedUserId#
      and site_network = #siteNetwork#
      and activity_format = #activitySummaryFormat#
      order by post_date desc
      ]]>
   </select>
   
   <!-- user feed - given site - others (not me) -->
   <select id="select_activity_feed_for_feeduser_others_and_site" parameterClass="ActivityFeed" resultClass="ActivityFeed">
      <![CDATA[
      select id as id, activity_type as activityType, activity_format as activitySummaryFormat, activity_summary as activitySummary, feed_user_id as feedUserId, post_user_id as postUserId, site_network as siteNetwork, post_date as postDate
      from alf_activity_feed
      where feed_user_id = #feedUserId#
      and post_user_id != #feedUserId#
      and site_network = #siteNetwork#
      and activity_format = #activitySummaryFormat#
      order by post_date desc
      ]]>
   </select>
   
   <!-- user feed - given site - me only (not others) -->
   <select id="select_activity_feed_for_feeduser_me_and_site" parameterClass="ActivityFeed" resultClass="ActivityFeed">
      <![CDATA[
      select id as id, activity_type as activityType, activity_format as activitySummaryFormat, activity_summary as activitySummary, feed_user_id as feedUserId, post_user_id as postUserId, site_network as siteNetwork, post_date as postDate
      from alf_activity_feed
      where feed_user_id = #feedUserId#
      and post_user_id = #feedUserId#
      and site_network = #siteNetwork#
      and activity_format = #activitySummaryFormat#
      order by post_date desc
      ]]>
   </select>
   
   <!-- site feed - given site -->
   <select id="select_activity_feed_for_site" parameterClass="ActivityFeed" resultClass="ActivityFeed">
      <![CDATA[
      select id as id, activity_type as activityType, activity_format as activitySummaryFormat, activity_summary as activitySummary, post_user_id as postUserId, site_network as siteNetwork, post_date as postDate
      from alf_activity_feed
      where (feed_user_id = '' or feed_user_id is null)
      and site_network = #siteNetwork#
      and activity_format = #activitySummaryFormat#
      order by post_date desc
      ]]>
   </select>
   
   <delete id="delete_activity_feed_entries_older_than_date" parameterClass="Date">
      <![CDATA[
      delete from alf_activity_feed where post_date < #keepdate#
      ]]>
   </delete>
   
   <delete id="delete_activity_feed_for_site_entries_older_than_date" parameterClass="ActivityFeed">
      <![CDATA[
      delete from alf_activity_feed 
      where post_date < #postDate#
      and site_network = #siteNetwork#
      and activity_format = #activitySummaryFormat#
      ]]>
   </delete>
   
   <delete id="delete_activity_feed_for_feeduser_entries_older_than_date" parameterClass="ActivityFeed">
      <![CDATA[
      delete from alf_activity_feed 
      where post_date < #postDate#
      and feed_user_id = #feedUserId#
      and activity_format = #activitySummaryFormat#
      ]]>
   </delete>
   
   <select id="select_activity_feedcontrols_for_user" parameterClass="FeedControl" resultClass="FeedControl">
      <![CDATA[
      select id as id, feed_user_id as feedUserId, site_network as siteNetwork, app_tool as appTool
      from alf_activity_feed_control
      where feed_user_id = #feedUserId#
      ]]>
   </select>
   
   <select id="select_activity_feedcontrol" parameterClass="FeedControl" resultClass="long">
      <![CDATA[
      select id as id
      from alf_activity_feed_control
      where feed_user_id = #feedUserId#
      and ((site_network = #siteNetwork#) or ((#siteNetwork# is null) and (site_network is null)))
      and ((app_tool = #appTool#) or ((#appTool# is null) and (app_tool is null)))
      ]]>
   </select>
   
   <delete id="delete_activity_feedcontrol" parameterClass="FeedControl">
      <![CDATA[
      delete from alf_activity_feed_control
      where feed_user_id = #feedUserId#
      and ((site_network = #siteNetwork#) or ((#siteNetwork# is null) and (site_network is null)))
      and ((app_tool = #appTool#) or ((#appTool# is null) and (app_tool is null)))
      ]]>
   </delete>
   
   <select id="select_activity_posts" parameterClass="ActivityPost" resultClass="ActivityPost">
      <![CDATA[
      select 
         sequence_id as id,
         activity_data as activityData,
         activity_type as activityType,
         post_user_id as userId,
         post_date as postDate,
         job_task_node as jobTaskNode,
         site_network as siteNetwork,
         app_tool as appTool,
         status as status
      from 
         alf_activity_post
      where 
         job_task_node = #jobTaskNode# and
         sequence_id >= #minId# and
         sequence_id <= #maxId# and
         status = #status#
      ]]>
   </select>
   
   <select id="select_activity_posts_by_status_only" parameterClass="ActivityPost" resultClass="ActivityPost">
      <![CDATA[
      select 
         sequence_id as id,
         activity_data as activityData,
         activity_type as activityType,
         post_user_id as userId,
         post_date as postDate,
         job_task_node as jobTaskNode,
         site_network as siteNetwork,
         app_tool as appTool,
         status as status
      from 
         alf_activity_post
      where 
         status = #status#
      ]]>
   </select>
   
   <select id="select_activity_post_max_seq" resultClass="long">
      select max(sequence_id) as maxId
      from alf_activity_post
      where status = 'POSTED'
   </select>
   
   <select id="select_activity_post_min_seq" resultClass="long">
      select min(sequence_id) as minId
      from alf_activity_post
      where status = 'POSTED'
   </select>
   
   <select id="select_activity_post_max_jobtasknode" resultClass="int">
      select max(job_task_node) as maxJobTaskNode
      from alf_activity_post
      where status = 'POSTED'
   </select>
   
   <delete id="delete_activity_posts_older_than_date" parameterClass="ActivityPost">
      <![CDATA[
      delete from alf_activity_post 
      where post_date < #postDate#
      and status = #status#
      ]]>
   </delete>
   
   <update id="update_activity_post_data" parameterClass="ActivityPost">
      update alf_activity_post set status = #status#, activity_data=#activityData#, site_network=#siteNetwork#, last_modified=#lastModified#
      where sequence_id = #id#
      and status != #status#
   </update>
   
   <update id="update_activity_post_status" parameterClass="ActivityPost">
      update alf_activity_post set status = #status#, last_modified=#lastModified#
      where sequence_id = #id#
      and status != #status#
   </update>
   
</sqlMap>