]> source.dussan.org Git - sonarqube.git/commitdiff
Complete MyBatis mapper for PROPERTIES : add insert and update operations
authorSimon Brandhof <simon.brandhof@gmail.com>
Wed, 4 Apr 2012 09:08:35 +0000 (11:08 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Wed, 4 Apr 2012 09:09:06 +0000 (11:09 +0200)
sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java
sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java
sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java
sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper-oracle.xml [new file with mode: 0644]
sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml
sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update.xml [new file with mode: 0644]

index 78392eeb4733ed33461bcec2d8bd3d257a00b520..c21cd08f48ec7aa19ad48c4c2a6a6b270991588a 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.core.properties;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.ibatis.session.SqlSession;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.ServerComponent;
@@ -40,7 +41,7 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
    * @param resourceId the resource id
    * @return the list of logins (maybe be empty - obviously)
    */
-  public List<String> findUserIdsForFavouriteResource(Integer resourceId) {
+  public List<String> findUserIdsForFavouriteResource(Long resourceId) {
     SqlSession session = mybatis.openSession();
     PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
     try {
@@ -69,4 +70,23 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
       MyBatis.closeQuietly(session);
     }
   }
+
+  public void setProperty(PropertyDto property) {
+    SqlSession session = mybatis.openSession();
+    PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
+    try {
+      PropertyDto persistedProperty = mapper.selectByKey(property);
+      if (persistedProperty != null && !StringUtils.equals(persistedProperty.getValue(), property.getValue())) {
+        persistedProperty.setValue(property.getValue());
+        mapper.update(persistedProperty);
+        session.commit();
+
+      } else if (persistedProperty == null) {
+        mapper.insert(property);
+        session.commit();
+      }
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
 }
index be3e79db7b01d1cd7901e63e861008e0c2b5ecd7..90cf9ac8612e254451a0bf6cb7f4ceb21ec0a6cd 100644 (file)
@@ -21,12 +21,13 @@ package org.sonar.core.properties;
 
 import java.util.List;
 
-import org.apache.ibatis.annotations.Param;
-
 public interface PropertiesMapper {
 
-  List<String> findUserIdsForFavouriteResource(@Param("resource_id") Integer resourceId);
+  List<String> findUserIdsForFavouriteResource(Long resourceId);
   List<PropertyDto> selectGlobalProperties();
   List<PropertyDto> selectProjectProperties(String resourceKey);
+  PropertyDto selectByKey(PropertyDto key);
+  void update(PropertyDto property);
+  void insert(PropertyDto property);
 
 }
index 9623f5f686ea1a8112f17a738768c58d63c0d57f..afc42fbe6f639853d6b174ba71147eb62efb94ea 100644 (file)
 package org.sonar.core.properties;
 
 public final class PropertyDto {
-  private Integer id;
+  private Long id;
   private String key;
   private String value;
-  private Integer resourceId;
-  private Integer userId;
+  private Long resourceId;
+  private Long userId;
 
-  public Integer getId() {
+  public Long getId() {
     return id;
   }
 
-  public PropertyDto setId(Integer id) {
+  public PropertyDto setId(Long id) {
     this.id = id;
     return this;
   }
@@ -53,20 +53,20 @@ public final class PropertyDto {
     return this;
   }
 
-  public Integer getResourceId() {
+  public Long getResourceId() {
     return resourceId;
   }
 
-  public PropertyDto setResourceId(Integer resourceId) {
+  public PropertyDto setResourceId(Long resourceId) {
     this.resourceId = resourceId;
     return this;
   }
 
-  public Integer getUserId() {
+  public Long getUserId() {
     return userId;
   }
 
-  public PropertyDto setUserId(Integer userId) {
+  public PropertyDto setUserId(Long userId) {
     this.userId = userId;
     return this;
   }
diff --git a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper-oracle.xml b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper-oracle.xml
new file mode 100644 (file)
index 0000000..907983b
--- /dev/null
@@ -0,0 +1,54 @@
+<?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">
+
+<mapper namespace="org.sonar.core.properties.PropertiesMapper">
+
+  <select id="findUserIdsForFavouriteResource" parameterType="map" resultType="String">
+    SELECT U.login
+    FROM properties P, users U
+    WHERE P.prop_key = 'favourite' AND P.resource_id = #{id} AND P.user_id = U.id
+  </select>
+
+  <select id="selectGlobalProperties" resultType="Property">
+    select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+    from properties p
+    where p.resource_id is null and p.user_id is null
+  </select>
+
+  <select id="selectProjectProperties" parameterType="String" resultType="Property">
+    select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+    from properties p, projects r
+    where p.resource_id=r.id and p.user_id is null and r.kee=#{id}
+  </select>
+
+  <select id="selectByKey" parameterType="map" resultType="Property">
+    select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+    from properties p
+    where p.prop_key=#{key}
+    <if test="resourceId == null">
+      AND p.resource_id is null
+    </if>
+    <if test="resourceId != null">
+      AND p.resource_id=#{resourceId}
+    </if>
+    <if test="userId == null">
+      AND p.user_id is null
+    </if>
+    <if test="userId != null">
+      AND p.user_id=#{userId}
+    </if>
+  </select>
+
+  <update id="update" parameterType="Property">
+    update properties set text_value = #{value} where id = #{id}
+  </update>
+
+  <insert id="insert" parameterType="Property" useGeneratedKeys="true" keyProperty="id">
+    <selectKey order="BEFORE" resultType="Long" keyProperty="id">
+      select properties_seq.NEXTVAL from DUAL
+    </selectKey>
+    INSERT INTO properties (id, prop_key, resource_id, user_id, text_value)
+    VALUES (#{id}, #{key, jdbcType=VARCHAR}, #{resourceId, jdbcType=INTEGER}, #{userId, jdbcType=INTEGER}, #{value, jdbcType=VARCHAR})
+  </insert>
+
+</mapper>
index 1878b5f31afb3aea52f39ace2d03aedd1b36f727..f00498c4aba67dc34d7340a25a020771c27f8232 100644 (file)
@@ -6,7 +6,7 @@
   <select id="findUserIdsForFavouriteResource" parameterType="map" resultType="String">
     SELECT U.login
     FROM properties P, users U
-    WHERE P.prop_key = 'favourite' AND P.resource_id = #{resource_id} AND P.user_id = U.id
+    WHERE P.prop_key = 'favourite' AND P.resource_id = #{id} AND P.user_id = U.id
   </select>
 
   <select id="selectGlobalProperties" resultType="Property">
   </select>
 
   <select id="selectProjectProperties" parameterType="String" resultType="Property">
-      select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
-      from properties p, projects r
-      where p.resource_id=r.id and p.user_id is null and r.kee=#{id}
-    </select>
+    select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+    from properties p, projects r
+    where p.resource_id=r.id and p.user_id is null and r.kee=#{id}
+  </select>
+
+  <select id="selectByKey" parameterType="map" resultType="Property">
+    select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+    from properties p
+    where p.prop_key=#{key}
+    <if test="resourceId == null">
+      AND p.resource_id is null
+    </if>
+    <if test="resourceId != null">
+      AND p.resource_id=#{resourceId}
+    </if>
+    <if test="userId == null">
+      AND p.user_id is null
+    </if>
+    <if test="userId != null">
+      AND p.user_id=#{userId}
+    </if>
+  </select>
+
+  <update id="update" parameterType="Property">
+    update properties set text_value = #{value} where id = #{id}
+  </update>
+
+  <insert id="insert" parameterType="Property" useGeneratedKeys="true" keyProperty="id">
+    INSERT INTO properties (prop_key, resource_id, user_id, text_value)
+    VALUES (#{key, jdbcType=VARCHAR}, #{resourceId, jdbcType=INTEGER}, #{userId, jdbcType=INTEGER}, #{value, jdbcType=VARCHAR})
+  </insert>
 
 </mapper>
index 074e4f36a966ca70bc75d51803cd6b0503b371a8..092dd43cf5a189cdd4a82d67e4be3f9ea79cf7c0 100644 (file)
@@ -41,7 +41,7 @@ public class PropertiesDaoTest extends DaoTestCase {
   @Test
   public void shouldFindUserIdsForFavouriteResource() throws Exception {
     setupData("shouldFindUserIdsForFavouriteResource");
-    List<String> userIds = dao.findUserIdsForFavouriteResource(2);
+    List<String> userIds = dao.findUserIdsForFavouriteResource(2L);
     assertThat(userIds.size(), is(2));
     assertThat(userIds, hasItems("user3", "user4"));
   }
@@ -72,6 +72,28 @@ public class PropertiesDaoTest extends DaoTestCase {
     assertThat(first.getValue(), is("one"));
   }
 
+  @Test
+  public void setProperty_update() throws Exception {
+    setupData("update");
+
+    dao.setProperty(new PropertyDto().setKey("global.key").setValue("new_global"));
+    dao.setProperty(new PropertyDto().setKey("project.key").setResourceId(10L).setValue("new_project"));
+    dao.setProperty(new PropertyDto().setKey("user.key").setUserId(100L).setValue("new_user"));
+
+    checkTables("update", "properties");
+  }
+
+  @Test
+  public void setProperty_insert() throws Exception {
+    setupData("insert");
+
+    dao.setProperty(new PropertyDto().setKey("global.key").setValue("new_global"));
+    dao.setProperty(new PropertyDto().setKey("project.key").setResourceId(10L).setValue("new_project"));
+    dao.setProperty(new PropertyDto().setKey("user.key").setUserId(100L).setValue("new_user"));
+
+    checkTables("insert", new String[]{"id"}, "properties");
+  }
+
   private PropertyDto findById(List<PropertyDto> properties, int id) {
     for (PropertyDto property : properties) {
       if (property.getId() == id) {
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert-result.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert-result.xml
new file mode 100644 (file)
index 0000000..3d4da8c
--- /dev/null
@@ -0,0 +1,12 @@
+<dataset>
+
+  <!-- global -->
+  <properties prop_key="global.key" text_value="new_global" resource_id="[null]" user_id="[null]"/>
+
+  <!-- project -->
+  <properties prop_key="project.key" text_value="new_project" resource_id="10" user_id="[null]"/>
+
+  <!-- user -->
+  <properties prop_key="user.key" text_value="new_user" resource_id="[null]" user_id="100"/>
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert.xml
new file mode 100644 (file)
index 0000000..5ed00ba
--- /dev/null
@@ -0,0 +1 @@
+<dataset></dataset>
\ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update-result.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update-result.xml
new file mode 100644 (file)
index 0000000..3e5eb87
--- /dev/null
@@ -0,0 +1,12 @@
+<dataset>
+
+  <!-- global -->
+  <properties id="1" prop_key="global.key" text_value="new_global" resource_id="[null]" user_id="[null]"/>
+
+  <!-- project -->
+  <properties id="2" prop_key="project.key" text_value="new_project" resource_id="10" user_id="[null]"/>
+
+  <!-- user -->
+  <properties id="3" prop_key="user.key" text_value="new_user" resource_id="[null]" user_id="100"/>
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update.xml
new file mode 100644 (file)
index 0000000..6938740
--- /dev/null
@@ -0,0 +1,12 @@
+<dataset>
+
+  <!-- global -->
+  <properties id="1" prop_key="global.key" text_value="global" resource_id="[null]" user_id="[null]"/>
+
+  <!-- project -->
+  <properties id="2" prop_key="project.key" text_value="project" resource_id="10" user_id="[null]"/>
+
+  <!-- user -->
+  <properties id="3" prop_key="user.key" text_value="user" resource_id="[null]" user_id="100"/>
+
+</dataset>