]> source.dussan.org Git - sonarqube.git/commitdiff
Complete MyBatis ResourceDao
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 9 Jul 2012 16:02:05 +0000 (18:02 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 9 Jul 2012 16:21:46 +0000 (18:21 +0200)
* new method insertOrUpdate()
* new criteria in ResourceQuery

12 files changed:
sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
sonar-core/src/main/java/org/sonar/core/resource/ResourceDto.java
sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerDao.java
sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java
sonar-core/src/main/java/org/sonar/core/resource/ResourceQuery.java
sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper-oracle.xml [new file with mode: 0644]
sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml
sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml
sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/getResources_exclude_disabled.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate.xml [new file with mode: 0644]

index 480e75ce054fc3597053db1b390b93ba292b76cd..260b4abcb7e2df990f2039ec7963ee94ffd4ad41 100644 (file)
@@ -86,4 +86,22 @@ public class ResourceDao {
       appendChildProjects(subProject.getId(), mapper, resources);
     }
   }
+
+  public ResourceDao insertOrUpdate(ResourceDto... resources) {
+    SqlSession session = mybatis.openSession();
+    ResourceMapper mapper = session.getMapper(ResourceMapper.class);
+    try {
+      for (ResourceDto resource : resources) {
+        if (resource.getId()==null) {
+          mapper.insert(resource);
+        } else {
+          mapper.update(resource);
+        }
+      }
+      session.commit();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+    return this;
+  }
 }
index 6eeeab179e1eb11536b72c3636ae918eb68f79aa..4049df31883301832cbc1b4cfe655bfc4f2e398e 100644 (file)
@@ -25,9 +25,15 @@ public final class ResourceDto {
   private String key;
   private String name;
   private String longName;
-  private Integer rootId;
+  private Long rootId;
   private String scope;
   private String qualifier;
+  private boolean enabled = true;
+  private String description;
+  private String language;
+  private Long copyResourceId;
+  private Long personId;
+  private Long profileId;
 
   public Long getId() {
     return id;
@@ -56,11 +62,11 @@ public final class ResourceDto {
     return this;
   }
 
-  public Integer getRootId() {
+  public Long getRootId() {
     return rootId;
   }
 
-  public ResourceDto setRootId(Integer rootId) {
+  public ResourceDto setRootId(Long rootId) {
     this.rootId = rootId;
     return this;
   }
@@ -92,34 +98,57 @@ public final class ResourceDto {
     return this;
   }
 
-  @Override
-  public int hashCode() {
-    final int prime = 31;
-    int result = 1;
-    result = prime * result + ((id == null) ? 0 : id.hashCode());
-    return result;
-  }
-
-  @Override
-  public boolean equals(Object obj) {
-    if (this == obj) {
-      return true;
-    }
-    if (obj == null) {
-      return false;
-    }
-    if (getClass() != obj.getClass()) {
-      return false;
-    }
-    ResourceDto other = (ResourceDto) obj;
-    if (id == null) {
-      if (other.id != null) {
-        return false;
-      }
-    } else if (!id.equals(other.id)) {
-      return false;
-    }
-    return true;
+  public boolean isEnabled() {
+    return enabled;
   }
 
+  public ResourceDto setEnabled(boolean b) {
+    this.enabled = b;
+    return this;
+  }
+
+  public String getDescription() {
+    return description;
+  }
+
+  public ResourceDto setDescription(String description) {
+    this.description = description;
+    return this;
+  }
+
+  public String getLanguage() {
+    return language;
+  }
+
+  public ResourceDto setLanguage(String language) {
+    this.language = language;
+    return this;
+  }
+
+  public Long getCopyResourceId() {
+    return copyResourceId;
+  }
+
+  public ResourceDto setCopyResourceId(Long copyResourceId) {
+    this.copyResourceId = copyResourceId;
+    return this;
+  }
+
+  public Long getPersonId() {
+    return personId;
+  }
+
+  public ResourceDto setPersonId(Long personId) {
+    this.personId = personId;
+    return this;
+  }
+
+  public Long getProfileId() {
+    return profileId;
+  }
+
+  public ResourceDto setProfileId(Long profileId) {
+    this.profileId = profileId;
+    return this;
+  }
 }
index 77e2831efeef7a8a41814c288c8459411e60b0a6..b5f14b2c044c2252382ef090364a2158ad5716c2 100644 (file)
@@ -139,11 +139,11 @@ public class ResourceIndexerDao {
       ResourceIndexerMapper mapper = session.getMapper(ResourceIndexerMapper.class);
       ResourceDto resource = mapper.selectResourceToIndex(id);
       if (resource != null) {
-        Integer rootId = resource.getRootId();
+        Long rootId = resource.getRootId();
         if (rootId == null) {
-          rootId = resource.getId().intValue();
+          rootId = resource.getId();
         }
-        indexed = indexResource(resource.getId().intValue(), resource.getName(), resource.getQualifier(), rootId, session, mapper);
+        indexed = indexResource(resource.getId(), resource.getName(), resource.getQualifier(), rootId, session, mapper);
       }
       return indexed;
     } finally {
@@ -163,7 +163,7 @@ public class ResourceIndexerDao {
     return indexed;
   }
 
-  private boolean indexResource(int id, String name, String qualifier, int rootId, SqlSession session, ResourceIndexerMapper mapper) {
+  private boolean indexResource(long id, String name, String qualifier, long rootId, SqlSession session, ResourceIndexerMapper mapper) {
     boolean indexed = false;
     String key = nameToKey(name);
     if (key.length() >= MINIMUM_KEY_SIZE) {
@@ -193,7 +193,7 @@ public class ResourceIndexerDao {
    * If the resource is indexed with a different key, then this index is dropped and the
    * resource must be indexed again.
    */
-  private boolean sanitizeIndex(int resourceId, String key, ResourceIndexerMapper mapper) {
+  private boolean sanitizeIndex(long resourceId, String key, ResourceIndexerMapper mapper) {
     ResourceIndexDto masterIndex = mapper.selectMasterIndexByResourceId(resourceId);
     if (masterIndex != null && !StringUtils.equals(key, masterIndex.getKey())) {
       // resource has been renamed -> drop existing indexes
index bd54734db461bdc0da6a2a172f81e9eeb18e170c..cba769cf2c0150c67744b8999f789e260aa5205c 100644 (file)
@@ -44,4 +44,8 @@ public interface ResourceMapper {
    * @since 3.2
    */
   void selectResources(ResourceQuery query, ResultHandler resultHandler);
+
+  void insert(ResourceDto resource);
+
+  void update(ResourceDto resource);
 }
index a9aa2e9a229eb80a0581ba861925e3fca06e837b..6ae769715cdac2cfffffcf1cd3dfe89785cd662e 100644 (file)
@@ -24,6 +24,8 @@ package org.sonar.core.resource;
  */
 public final class ResourceQuery {
   private String[] qualifiers = null;
+  private String key = null;
+  private boolean excludeDisabled = false;
 
   private ResourceQuery() {
   }
@@ -40,4 +42,22 @@ public final class ResourceQuery {
     this.qualifiers = qualifiers;
     return this;
   }
+
+  public String getKey() {
+    return key;
+  }
+
+  public ResourceQuery setKey(String key) {
+    this.key = key;
+    return this;
+  }
+
+  public boolean isExcludeDisabled() {
+    return excludeDisabled;
+  }
+
+  public ResourceQuery setExcludeDisabled(boolean b) {
+    this.excludeDisabled = b;
+    return this;
+  }
 }
diff --git a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper-oracle.xml b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper-oracle.xml
new file mode 100644 (file)
index 0000000..558e4a3
--- /dev/null
@@ -0,0 +1,107 @@
+<?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.resource.ResourceMapper">
+
+  <resultMap id="snapshotResultMap" type="Snapshot">
+    <id property="id" column="id"/>
+    <result property="parentId" column="parent_snapshot_id"/>
+    <result property="rootId" column="root_snapshot_id"/>
+    <result property="date" column="created_at"/>
+    <result property="buildDate" column="build_date"/>
+    <result property="resourceId" column="project_id"/>
+    <result property="status" column="status"/>
+    <result property="purgeStatus" column="purge_status"/>
+    <result property="last" column="islast"/>
+    <result property="scope" column="scope"/>
+    <result property="qualifier" column="qualifier"/>
+    <result property="version" column="version"/>
+    <result property="path" column="path"/>
+    <result property="depth" column="depth"/>
+    <result property="rootProjectId" column="root_project_id"/>
+  </resultMap>
+
+  <resultMap id="resourceResultMap" type="Resource">
+    <id property="id" column="id"/>
+    <result property="key" column="kee"/>
+    <result property="name" column="name"/>
+    <result property="longName" column="long_name"/>
+    <result property="rootId" column="root_id"/>
+    <result property="scope" column="scope"/>
+    <result property="qualifier" column="qualifier"/>
+    <result property="enabled" column="enabled"/>
+    <result property="description" column="description"/>
+    <result property="language" column="language"/>
+    <result property="copyResourceId" column="copy_resource_id"/>
+    <result property="personId" column="person_id"/>
+    <result property="profileId" column="profile_id"/>
+  </resultMap>
+
+  <select id="selectResources" parameterType="map" resultMap="resourceResultMap">
+    select *
+    from projects p
+    <where>
+      <if test="qualifiers != null and qualifiers.length!=0">
+        and p.qualifier in
+        <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach>
+      </if>
+      <if test="key != null">
+        and p.kee=#{key}
+      </if>
+      <if test="excludeDisabled">
+        and p.enabled=${_true}
+      </if>
+    </where>
+  </select>
+
+  <select id="selectResourceIds" parameterType="map" resultType="long">
+    select p.id
+    from projects p
+    <where>
+      <if test="qualifiers != null and qualifiers.length!=0">
+        and p.qualifier in
+        <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach>
+      </if>
+      <if test="key != null">
+        and p.kee=#{key}
+      </if>
+      <if test="excludeDisabled">
+        and p.enabled=${_true}
+      </if>
+    </where>
+  </select>
+
+  <select id="selectResource" parameterType="long" resultMap="resourceResultMap">
+    select * from projects where id=#{id}
+  </select>
+
+  <select id="selectSnapshot" parameterType="long" resultMap="snapshotResultMap">
+    select * from snapshots where id=#{id}
+  </select>
+
+  <select id="selectDescendantProjects" parameterType="long" resultMap="resourceResultMap">
+    select * from projects where scope='PRJ' and root_id=#{id}
+  </select>
+
+  <insert id="insert" parameterType="Resource" useGeneratedKeys="true" keyProperty="id">
+    <selectKey order="BEFORE" resultType="Long" keyProperty="id">
+      select projects_seq.NEXTVAL from DUAL
+    </selectKey>
+
+    insert into projects
+    (id, name, long_name, description, scope, qualifier, kee, language, root_id, copy_resource_id, person_id, profile_id, enabled)
+    values (
+    #{id}, #{name, jdbcType=VARCHAR}, #{longName, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{scope, jdbcType=VARCHAR}, #{qualifier, jdbcType=VARCHAR},
+    #{key, jdbcType=VARCHAR}, #{language, jdbcType=VARCHAR}, #{rootId, jdbcType=INTEGER}, #{copyResourceId, jdbcType=INTEGER},
+    #{personId, jdbcType=INTEGER}, #{profileId, jdbcType=INTEGER}, #{enabled, jdbcType=BOOLEAN}
+    )
+  </insert>
+
+  <update id="update" parameterType="Resource">
+    update projects set name=#{name, jdbcType=VARCHAR}, long_name=#{longName, jdbcType=VARCHAR}, description=#{description, jdbcType=VARCHAR},
+    scope=#{scope, jdbcType=VARCHAR}, qualifier=#{qualifier, jdbcType=VARCHAR}, kee=#{key, jdbcType=VARCHAR},
+    language=#{language, jdbcType=VARCHAR}, root_id=#{rootId, jdbcType=INTEGER}, copy_resource_id=#{copyResourceId, jdbcType=INTEGER},
+    person_id=#{personId, jdbcType=INTEGER}, profile_id=#{profileId, jdbcType=INTEGER}, enabled=#{enabled, jdbcType=BOOLEAN} where id=#{id,jdbcType=INTEGER}
+  </update>
+</mapper>
+
index c604f28d154e865d0f9506cdb658f5901319af5e..728cb9dda0f60f1a4ba4b13b8593471620b633bb 100644 (file)
     <result property="rootId" column="root_id"/>
     <result property="scope" column="scope"/>
     <result property="qualifier" column="qualifier"/>
+    <result property="enabled" column="enabled"/>
+    <result property="description" column="description"/>
+    <result property="language" column="language"/>
+    <result property="copyResourceId" column="copy_resource_id"/>
+    <result property="personId" column="person_id"/>
+    <result property="profileId" column="profile_id"/>
   </resultMap>
 
   <select id="selectResources" parameterType="map" resultMap="resourceResultMap">
         and p.qualifier in
         <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach>
       </if>
+      <if test="key != null">
+        and p.kee=#{key}
+      </if>
+      <if test="excludeDisabled">
+        and p.enabled=${_true}
+      </if>
     </where>
   </select>
 
         and p.qualifier in
         <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach>
       </if>
+      <if test="key != null">
+        and p.kee=#{key}
+      </if>
+      <if test="excludeDisabled">
+        and p.enabled=${_true}
+      </if>
     </where>
   </select>
 
   <select id="selectDescendantProjects" parameterType="long" resultMap="resourceResultMap">
     select * from projects where scope='PRJ' and root_id=#{id}
   </select>
+
+  <insert id="insert" parameterType="Resource" useGeneratedKeys="true" keyProperty="id">
+    insert into projects
+    (name, long_name, description, scope, qualifier, kee, language, root_id, copy_resource_id, person_id, profile_id, enabled)
+    values (
+    #{name, jdbcType=VARCHAR}, #{longName, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{scope, jdbcType=VARCHAR}, #{qualifier, jdbcType=VARCHAR},
+    #{key, jdbcType=VARCHAR}, #{language, jdbcType=VARCHAR}, #{rootId, jdbcType=INTEGER}, #{copyResourceId, jdbcType=INTEGER},
+    #{personId, jdbcType=INTEGER}, #{profileId, jdbcType=INTEGER}, #{enabled, jdbcType=BOOLEAN}
+    )
+  </insert>
+
+  <update id="update" parameterType="Resource">
+    update projects set name=#{name, jdbcType=VARCHAR}, long_name=#{longName, jdbcType=VARCHAR}, description=#{description, jdbcType=VARCHAR},
+    scope=#{scope, jdbcType=VARCHAR}, qualifier=#{qualifier, jdbcType=VARCHAR}, kee=#{key, jdbcType=VARCHAR},
+    language=#{language, jdbcType=VARCHAR}, root_id=#{rootId, jdbcType=INTEGER}, copy_resource_id=#{copyResourceId, jdbcType=INTEGER},
+    person_id=#{personId, jdbcType=INTEGER}, profile_id=#{profileId, jdbcType=INTEGER}, enabled=#{enabled, jdbcType=BOOLEAN} where id=#{id,jdbcType=INTEGER}
+  </update>
 </mapper>
 
index a16eec194c8bbb82259515bbb68053d0fb1c2c16..26f8513367ac5bdfd0c1100c5553e2e1f9f4c9c0 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.core.resource;
 
 import org.junit.Before;
 import org.junit.Test;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Scopes;
 import org.sonar.core.persistence.AbstractDaoTestCase;
 
 import java.util.List;
@@ -63,6 +65,9 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
     assertThat(resource.getName()).isEqualTo("Struts");
     assertThat(resource.getLongName()).isEqualTo("Apache Struts");
     assertThat(resource.getScope()).isEqualTo("PRJ");
+    assertThat(resource.getDescription()).isEqualTo("the description");
+    assertThat(resource.getLanguage()).isEqualTo("java");
+    assertThat(resource.isEnabled()).isTrue();
   }
 
   @Test
@@ -95,6 +100,15 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
     assertThat(resources).hasSize(4);
   }
 
+  @Test
+  public void getResources_filter_by_key() {
+    setupData("fixture");
+
+    List<ResourceDto> resources = dao.getResources(ResourceQuery.create().setKey("org.struts:struts-core"));
+    assertThat(resources).hasSize(1);
+    assertThat(resources.get(0).getKey()).isEqualTo("org.struts:struts-core");
+  }
+
   @Test
   public void getResourceIds_all() {
     setupData("fixture");
@@ -117,5 +131,40 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
     ids = dao.getResourceIds(ResourceQuery.create().setQualifiers(new String[]{}));
     assertThat(ids).hasSize(4);
   }
+
+  @Test
+  public void getResources_exclude_disabled() {
+    setupData("getResources_exclude_disabled");
+
+    assertThat(dao.getResourceIds(ResourceQuery.create().setExcludeDisabled(false))).containsOnly(1L, 2L);
+    assertThat(dao.getResourceIds(ResourceQuery.create().setExcludeDisabled(true))).containsOnly(2L);
+  }
+
+  @Test
+  public void insertOrUpdate() {
+    setupData("insertOrUpdate");
+
+    // update because already persisted
+    ResourceDto project = new ResourceDto()
+      .setKey("org.struts:struts").setScope(Scopes.PROJECT).setQualifier(Qualifiers.PROJECT)
+      .setName("Struts").setLongName("Apache Struts").setLanguage("java").setDescription("MVC Framework")
+      .setId(1L);
+
+    // insert
+    ResourceDto file1 = new ResourceDto()
+      .setKey("org.struts:struts:org.struts.Action").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE)
+      .setLanguage("java").setName("Action").setLongName("org.struts.Action");
+    ResourceDto file2 = new ResourceDto()
+          .setKey("org.struts:struts:org.struts.Filter").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE)
+          .setLanguage("java").setName("Filter").setLongName("org.struts.Filter");
+
+    dao.insertOrUpdate(project, file1, file2);
+
+    assertThat(project.getId()).isNotNull();
+    assertThat(file1.getId()).isNotNull();
+    assertThat(file2.getId()).isNotNull();
+    checkTables("insertOrUpdate", "projects");
+  }
+
 }
 
index e92a256f5537110e0b91f1f827004dbf54c4f573..6c362ca796300b8b9a6afad0b0ce085b75faf5f6 100644 (file)
@@ -2,7 +2,7 @@
 
   <!-- root project -->
   <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
-            description="[null]" long_name="Apache Struts"
+            description="the description" long_name="Apache Struts"
             enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
   <snapshots id="1" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]"
                status="P" islast="[false]" purge_status="[null]"
diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/getResources_exclude_disabled.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/getResources_exclude_disabled.xml
new file mode 100644 (file)
index 0000000..f210035
--- /dev/null
@@ -0,0 +1,12 @@
+<dataset>
+
+  <!-- disabled -->
+  <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+            description="the description" long_name="Apache Struts"
+            enabled="[false]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+
+  <!-- enabled -->
+  <projects id="2" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+            description="the description" long_name="Apache Struts"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate-result.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate-result.xml
new file mode 100644 (file)
index 0000000..668f78e
--- /dev/null
@@ -0,0 +1,15 @@
+<dataset>
+
+  <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+            description="MVC Framework" long_name="Apache Struts"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" profile_id="[null]"/>
+
+  <projects id="2" root_id="[null]" scope="FIL" qualifier="FIL" kee="org.struts:struts:org.struts.Action" name="Action"
+            description="[null]" long_name="org.struts.Action"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" profile_id="[null]"/>
+
+  <projects id="3" root_id="[null]" scope="FIL" qualifier="FIL" kee="org.struts:struts:org.struts.Filter" name="Filter"
+            description="[null]" long_name="org.struts.Filter"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" profile_id="[null]"/>
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate.xml
new file mode 100644 (file)
index 0000000..10fdddc
--- /dev/null
@@ -0,0 +1,7 @@
+<dataset>
+
+  <projects id="1" root_id="200" scope="PRJ" qualifier="TRK" kee="old key" name="old name"
+            description="old name" long_name="old long name"
+            enabled="[false]" language="old" copy_resource_id="2" person_id="3" profile_id="4"/>
+
+</dataset>