<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<!--
   Copyright (C) 2015 Glyptodon LLC

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
-->

<mapper namespace="org.glyptodon.guacamole.auth.jdbc.connection.ConnectionMapper" >

    <!-- Result mapper for connection objects -->
    <resultMap id="ConnectionResultMap" type="org.glyptodon.guacamole.auth.jdbc.connection.ConnectionModel" >
        <id     column="connection_id"   property="objectID"         jdbcType="INTEGER"/>
        <result column="connection_name" property="name"             jdbcType="VARCHAR"/>
        <result column="parent_id"       property="parentIdentifier" jdbcType="INTEGER"/>
        <result column="protocol"        property="protocol"         jdbcType="VARCHAR"/>
    </resultMap>

    <!-- Select all connection identifiers -->
    <select id="selectIdentifiers" resultType="string">
        SELECT connection_id 
        FROM guacamole_connection
    </select>

    <!-- Select identifiers of all readable connections -->
    <select id="selectReadableIdentifiers" resultType="string">
        SELECT connection_id
        FROM guacamole_connection_permission
        WHERE
            user_id = #{user.objectID,jdbcType=INTEGER}
            AND permission = 'READ'
    </select>

    <!-- Select all connection identifiers within a particular connection group -->
    <select id="selectIdentifiersWithin" resultType="string">
        SELECT connection_id 
        FROM guacamole_connection
        WHERE
            <if test="parentIdentifier != null">parent_id = #{parentIdentifier,jdbcType=VARCHAR}</if>
            <if test="parentIdentifier == null">parent_id IS NULL</if>
    </select>

    <!-- Select identifiers of all readable connections within a particular connection group -->
    <select id="selectReadableIdentifiersWithin" resultType="string">
        SELECT guacamole_connection.connection_id
        FROM guacamole_connection
        JOIN guacamole_connection_permission ON guacamole_connection_permission.connection_id = guacamole_connection.connection_id
        WHERE
            <if test="parentIdentifier != null">parent_id = #{parentIdentifier,jdbcType=VARCHAR}</if>
            <if test="parentIdentifier == null">parent_id IS NULL</if>
            AND user_id = #{user.objectID,jdbcType=INTEGER}
            AND permission = 'READ'
    </select>

    <!-- Select multiple connections by identifier -->
    <select id="select" resultMap="ConnectionResultMap">

        SELECT
            connection_id,
            connection_name,
            parent_id,
            protocol 
        FROM guacamole_connection
        WHERE connection_id IN
            <foreach collection="identifiers" item="identifier"
                     open="(" separator="," close=")">
                #{identifier,jdbcType=VARCHAR}
            </foreach>

    </select>

    <!-- Select multiple connections by identifier only if readable -->
    <select id="selectReadable" resultMap="ConnectionResultMap">

        SELECT
            guacamole_connection.connection_id,
            connection_name,
            parent_id,
            protocol 
        FROM guacamole_connection
        JOIN guacamole_connection_permission ON guacamole_connection_permission.connection_id = guacamole_connection.connection_id
        WHERE guacamole_connection.connection_id IN
            <foreach collection="identifiers" item="identifier"
                     open="(" separator="," close=")">
                #{identifier,jdbcType=VARCHAR}
            </foreach>
            AND user_id = #{user.objectID,jdbcType=INTEGER}
            AND permission = 'READ'

    </select>

    <!-- Select single connection by name -->
    <select id="selectOneByName" resultMap="ConnectionResultMap">

        SELECT
            connection_id,
            connection_name,
            parent_id,
            protocol 
        FROM guacamole_connection
        WHERE 
            <if test="parentIdentifier != null">parent_id = #{parentIdentifier,jdbcType=VARCHAR}</if>
            <if test="parentIdentifier == null">parent_id IS NULL</if>
            AND connection_name = #{name,jdbcType=VARCHAR}

    </select>

    <!-- Delete single connection by identifier -->
    <delete id="delete">
        DELETE FROM guacamole_connection
        WHERE connection_id = #{identifier,jdbcType=VARCHAR}
    </delete>

    <!-- Insert single connection -->
    <insert id="insert" useGeneratedKeys="true" keyProperty="object.objectID"
            parameterType="org.glyptodon.guacamole.auth.jdbc.connection.ConnectionModel">

        INSERT INTO guacamole_connection (
            connection_name,
            parent_id,
            protocol 
        )
        VALUES (
            #{object.name,jdbcType=VARCHAR},
            #{object.parentIdentifier,jdbcType=VARCHAR},
            #{object.protocol,jdbcType=VARCHAR}
        )

    </insert>

    <!-- Update single connection -->
    <update id="update" parameterType="org.glyptodon.guacamole.auth.jdbc.connection.ConnectionModel">
        UPDATE guacamole_connection
        SET connection_name = #{object.name,jdbcType=VARCHAR},
            parent_id       = #{object.parentIdentifier,jdbcType=VARCHAR},
            protocol        = #{object.protocol,jdbcType=VARCHAR}
        WHERE connection_id = #{object.objectID,jdbcType=INTEGER}
    </update>

</mapper>