]> source.dussan.org Git - sonarqube.git/commitdiff
Complete the MyBatis DAO for PROPERTIES
authorSimon Brandhof <simon.brandhof@gmail.com>
Sun, 11 Mar 2012 17:55:54 +0000 (18:55 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 12 Mar 2012 10:29:00 +0000 (11:29 +0100)
sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java
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 [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/selectGlobalProperties.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectProjectProperties.xml [new file with mode: 0644]

index 68a0b83adb1c1b775a6820e63bf02d4b0b5c8530..a82ef62da35a4e16fd1f2b5d13f6445dca9c3f85 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.core.dashboard.*;
 import org.sonar.core.duplication.DuplicationMapper;
 import org.sonar.core.duplication.DuplicationUnitDto;
 import org.sonar.core.properties.PropertiesMapper;
+import org.sonar.core.properties.PropertyDto;
 import org.sonar.core.purge.PurgeMapper;
 import org.sonar.core.purge.PurgeVendorMapper;
 import org.sonar.core.purge.PurgeableSnapshotDto;
@@ -69,6 +70,7 @@ public class MyBatis implements BatchComponent, ServerComponent {
     loadAlias(conf, "Dashboard", DashboardDto.class);
     loadAlias(conf, "DuplicationUnit", DuplicationUnitDto.class);
     loadAlias(conf, "LoadedTemplate", LoadedTemplateDto.class);
+    loadAlias(conf, "Property", PropertyDto.class);
     loadAlias(conf, "PurgeableSnapshot", PurgeableSnapshotDto.class);
     loadAlias(conf, "Review", ReviewDto.class);
     loadAlias(conf, "Resource", ResourceDto.class);
index 3762d35e5297bb779b27484bb3973dfc31eae621..78392eeb4733ed33461bcec2d8bd3d257a00b520 100644 (file)
  */
 package org.sonar.core.properties;
 
-import java.util.List;
-
 import org.apache.ibatis.session.SqlSession;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.ServerComponent;
 import org.sonar.core.persistence.MyBatis;
 
+import java.util.List;
+
 public class PropertiesDao implements BatchComponent, ServerComponent {
 
   private MyBatis mybatis;
@@ -36,7 +36,7 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
 
   /**
    * Returns the logins of users who have flagged as favourite the resource identified by the given id.
-   *  
+   *
    * @param resourceId the resource id
    * @return the list of logins (maybe be empty - obviously)
    */
@@ -50,4 +50,23 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
     }
   }
 
+  public List<PropertyDto> selectGlobalProperties() {
+    SqlSession session = mybatis.openSession();
+    PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
+    try {
+      return mapper.selectGlobalProperties();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+  public List<PropertyDto> selectProjectProperties(String resourceKey) {
+    SqlSession session = mybatis.openSession();
+    PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
+    try {
+      return mapper.selectProjectProperties(resourceKey);
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
 }
index 4862efb038279fd29a3ed056142c8a2fe38fd63e..be3e79db7b01d1cd7901e63e861008e0c2b5ecd7 100644 (file)
@@ -26,5 +26,7 @@ import org.apache.ibatis.annotations.Param;
 public interface PropertiesMapper {
 
   List<String> findUserIdsForFavouriteResource(@Param("resource_id") Integer resourceId);
+  List<PropertyDto> selectGlobalProperties();
+  List<PropertyDto> selectProjectProperties(String resourceKey);
 
 }
diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java
new file mode 100644 (file)
index 0000000..9623f5f
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.properties;
+
+public final class PropertyDto {
+  private Integer id;
+  private String key;
+  private String value;
+  private Integer resourceId;
+  private Integer userId;
+
+  public Integer getId() {
+    return id;
+  }
+
+  public PropertyDto setId(Integer id) {
+    this.id = id;
+    return this;
+  }
+
+  public String getKey() {
+    return key;
+  }
+
+  public PropertyDto setKey(String key) {
+    this.key = key;
+    return this;
+  }
+
+  public String getValue() {
+    return value;
+  }
+
+  public PropertyDto setValue(String value) {
+    this.value = value;
+    return this;
+  }
+
+  public Integer getResourceId() {
+    return resourceId;
+  }
+
+  public PropertyDto setResourceId(Integer resourceId) {
+    this.resourceId = resourceId;
+    return this;
+  }
+
+  public Integer getUserId() {
+    return userId;
+  }
+
+  public PropertyDto setUserId(Integer userId) {
+    this.userId = userId;
+    return this;
+  }
+}
index 21dc926f5640b53f02a473ee6b1c595ef0fc318e..1878b5f31afb3aea52f39ace2d03aedd1b36f727 100644 (file)
@@ -9,4 +9,16 @@
     WHERE P.prop_key = 'favourite' AND P.resource_id = #{resource_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>
+
 </mapper>
index 590c24404d006df4c74bbf9ae5682ba8db1cbda6..074e4f36a966ca70bc75d51803cd6b0503b371a8 100644 (file)
  */
 package org.sonar.core.properties;
 
-import static org.hamcrest.Matchers.hasItems;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
-import java.util.List;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.core.persistence.DaoTestCase;
 
+import java.util.List;
+
+import static org.hamcrest.Matchers.hasItems;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
 public class PropertiesDaoTest extends DaoTestCase {
 
   private PropertiesDao dao;
@@ -45,4 +45,39 @@ public class PropertiesDaoTest extends DaoTestCase {
     assertThat(userIds.size(), is(2));
     assertThat(userIds, hasItems("user3", "user4"));
   }
+
+  @Test
+  public void selectGlobalProperties() throws Exception {
+    setupData("selectGlobalProperties");
+    List<PropertyDto> properties = dao.selectGlobalProperties();
+    assertThat(properties.size(), is(2));
+
+    PropertyDto first = findById(properties, 1);
+    assertThat(first.getKey(), is("global.one"));
+    assertThat(first.getValue(), is("one"));
+
+    PropertyDto second = findById(properties, 2);
+    assertThat(second.getKey(), is("global.two"));
+    assertThat(second.getValue(), is("two"));
+  }
+
+  @Test
+  public void selectProjectProperties() throws Exception {
+    setupData("selectProjectProperties");
+    List<PropertyDto> properties = dao.selectProjectProperties("org.struts:struts");
+    assertThat(properties.size(), is(1));
+
+    PropertyDto first = properties.get(0);
+    assertThat(first.getKey(), is("struts.one"));
+    assertThat(first.getValue(), is("one"));
+  }
+
+  private PropertyDto findById(List<PropertyDto> properties, int id) {
+    for (PropertyDto property : properties) {
+      if (property.getId() == id) {
+        return property;
+      }
+    }
+    return null;
+  }
 }
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectGlobalProperties.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectGlobalProperties.xml
new file mode 100644 (file)
index 0000000..e5aa737
--- /dev/null
@@ -0,0 +1,14 @@
+<dataset>
+
+  <!-- global -->
+  <properties id="1" prop_key="global.one" text_value="one" resource_id="[null]" user_id="[null]"/>
+  <properties id="2" prop_key="global.two" text_value="two" resource_id="[null]" user_id="[null]"/>
+
+  <!-- project -->
+  <properties id="3" prop_key="project.one" text_value="one" resource_id="10" user_id="[null]"/>
+
+  <!-- user -->
+  <properties id="4" prop_key="user.one" text_value="one" resource_id="[null]" user_id="100"/>
+  <properties id="5" prop_key="user.two" text_value="two" resource_id="10" user_id="100"/>
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectProjectProperties.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectProjectProperties.xml
new file mode 100644 (file)
index 0000000..a6ea934
--- /dev/null
@@ -0,0 +1,19 @@
+<dataset>
+
+  <!-- global -->
+  <properties id="1" prop_key="global.one" text_value="one" resource_id="[null]" user_id="[null]"/>
+  <properties id="2" prop_key="global.two" text_value="two" resource_id="[null]" user_id="[null]"/>
+
+  <!-- struts -->
+  <properties id="3" prop_key="struts.one" text_value="one" resource_id="10" user_id="[null]"/>
+
+  <!-- struts -->
+  <properties id="4" prop_key="commonslang.one" text_value="one" resource_id="11" user_id="[null]"/>
+
+  <!-- user -->
+  <properties id="5" prop_key="user.one" text_value="one" resource_id="[null]" user_id="100"/>
+  <properties id="6" prop_key="user.two" text_value="two" resource_id="10" user_id="100"/>
+
+  <projects id="10" kee="org.struts:struts"/>
+  <projects id="11" kee="org.apache:commons-lang"/>
+</dataset>