<?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="FeatureProp">

     <typeAlias alias="FeatureProp"
        type="org.gmod.schema.sequence.FeatureProp"/>
	 
     <resultMap id="map-featureprop-with-cvterm-lazy" 
                class="FeatureProp">
        <result property="value" column="value" />
        <result property="rank" column="rank" />
        <result property="cvTerm" column="type_id" select="getCvtermByCvTermId" />
     </resultMap>
	 
     <resultMap id="map-featureprop-with-cvterm" 
                class="FeatureProp">
        <result property="value" column="value" />
        <result property="rank" column="rank" />
		<result property="cvTerm" resultMap="CvTerm.lazy-cvterm" />
     </resultMap>
     
    <resultMap id="map-featureprop" 
               class="FeatureProp" groupBy="featurePropId">
		<result property="featurePropId" column="featureprop_id" />
        <result property="value" column="value" />
        <result property="rank"  column="prop_rank" />
        <result property="cvTerm.cvTermId" column="prop_type_id" />
    </resultMap>
	
	<resultMap id="map-feature-with-featureprops" 
                class="org.gmod.schema.sequence.Feature" groupBy="featureId">
        <result property="featureId" column="feature_id" />
        <result property="featureProps" resultMap="FeatureProp.map-featureprop-with-cvterm" />
    </resultMap>
     
   <!-- select featureprops e.g. for lazy loading -->
     <select id="getFeaturePropByFeatureIdLazy" resultMap="map-featureprop-with-cvterm-lazy">
       SELECT type_id, value, rank 
       FROM featureprop
       WHERE feature_id=#feature_id#
     </select>
     
	<select id="getFeaturePropByFeatureId" resultMap="map-featureprop-with-cvterm">
       SELECT value, rank, cvterm.* 
       FROM featureprop
	   LEFY JOIN cvterm ON type_id=cvterm_id
       WHERE feature_id=#feature_id#
     </select>
	
	 <select id="getFeaturePropByFeatureIds" resultMap="map-feature-with-featureprops">
       SELECT f.feature_id, value, rank, cvterm.* 
       FROM feature f
	   LEFT JOIN featureprop fp ON f.feature_id=fp.feature_id
       LEFT JOIN cvterm ON fp.type_id=cvterm_id
	   WHERE f.feature_id IN <iterate open="(" close=")" conjunction=","> $[]$ </iterate>
     </select>
	
    <update id="updateFeatureProp"
            parameterClass="FeatureProp">
      UPDATE featureprop
      SET value=#value#   
      WHERE rank=$rank$ AND type_id=$cvTerm.cvTermId$ AND 
            feature_id=(SELECT feature_id FROM feature WHERE uniquename=#feature.uniqueName#)
    </update>
    
    <!--  INSERT  -->
    <insert id="insertFeatureProp"
            parameterClass="FeatureProp">
      INSERT INTO featureprop
      ( feature_id, type_id, value, rank )
      VALUES
      ( 
        <isGreaterThan property="feature.featureId" compareValue="0">
          $feature.featureId$,
        </isGreaterThan>
        <isLessEqual property="feature.featureId" compareValue="0">
          (SELECT feature_id FROM feature WHERE uniquename=#feature.uniqueName#),
        </isLessEqual>
        $cvTerm.cvTermId$, #value#, $rank$ )
    </insert>
    
    <!--  DELETE  -->
   <delete id="deleteFeatureProp"
           parameterClass="FeatureProp">
      DELETE FROM featureprop
      WHERE
        <isNotNull property="feature.uniqueName">
          feature_id=
            (SELECT feature_id FROM feature WHERE uniquename=#feature.uniqueName#)
          AND
        </isNotNull>
        <isGreaterThan property="rank" compareValue="-1">
           rank=$rank$ AND
        </isGreaterThan>
        <isNotNull property="value">
          value=#value# AND
        </isNotNull>
        type_id=$cvTerm.cvTermId$
   </delete>
    
</sqlMap> 