diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-05-15 16:41:00 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-05-15 16:41:00 +0200 |
commit | cc113fb821ec49d6380bba42b01d2c0dfdc16b4b (patch) | |
tree | 46620f5ac5bfc78f4b0bb2e55902b2b4aae7d5e0 /sonar-core | |
parent | 2c28cad1a7bb57da3b3aab5ba77bf7a911084725 (diff) | |
download | sonarqube-cc113fb821ec49d6380bba42b01d2c0dfdc16b4b.tar.gz sonarqube-cc113fb821ec49d6380bba42b01d2c0dfdc16b4b.zip |
SONAR-5305 Return component favourite information
Diffstat (limited to 'sonar-core')
6 files changed, 137 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java index 521077c4bf1..5ac44bf1c4e 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java @@ -29,6 +29,7 @@ import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import javax.annotation.Nullable; + import java.util.List; import java.util.Map; @@ -110,6 +111,16 @@ public class PropertiesDao implements BatchComponent, ServerComponent { } } + public List<PropertyDto> selectByQuery(PropertyQuery query) { + SqlSession session = mybatis.openSession(false); + PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); + try { + return mapper.selectByQuery(query); + } finally { + MyBatis.closeQuietly(session); + } + } + public void setProperty(PropertyDto property, SqlSession session) { PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); PropertyDto persistedProperty = mapper.selectByKey(property); diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java index 8dff3f887de..6cdf28160af 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java @@ -39,6 +39,8 @@ public interface PropertiesMapper { PropertyDto selectByKey(PropertyDto key); + List<PropertyDto> selectByQuery(@Param("query") PropertyQuery query); + void update(PropertyDto property); void insert(PropertyDto property); diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertyQuery.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertyQuery.java new file mode 100644 index 00000000000..da489e95bf4 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertyQuery.java @@ -0,0 +1,76 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.core.properties; + +public class PropertyQuery { + + private final String key; + private final Long componentId; + private final Integer userId; + + private PropertyQuery(Builder builder) { + this.key = builder.key; + this.componentId = builder.componentId; + this.userId = builder.userId; + } + + public String key() { + return key; + } + + public Long componentId() { + return componentId; + } + + public Integer userId() { + return userId; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private String key; + private Long componentId; + private Integer userId; + + public Builder setKey(String key) { + this.key = key; + return this; + } + + public Builder setComponentId(Long componentId) { + this.componentId = componentId; + return this; + } + + public Builder setUserId(Integer userId) { + this.userId = userId; + return this; + } + + public PropertyQuery build() { + return new PropertyQuery(this); + } + } + +} diff --git a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml index eff90628032..30f14b06672 100644 --- a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml @@ -64,6 +64,22 @@ </if> </select> + <select id="selectByQuery" 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> + <if test="query.key() != null"> + AND p.prop_key=#{query.key} + </if> + <if test="query.componentId() != null"> + AND p.resource_id=#{query.componentId} + </if> + <if test="query.userId() != null"> + AND p.user_id=#{query.userId} + </if> + </where> + </select> + <update id="update" parameterType="Property"> update properties set text_value = #{value} where id = #{id} </update> diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java index 974bf4da7d8..9e902a97357 100644 --- a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java @@ -143,6 +143,19 @@ public class PropertiesDaoTest extends AbstractDaoTestCase { } @Test + public void select_by_query() { + setupData("select_by_query"); + + List<PropertyDto> results = dao.selectByQuery(PropertyQuery.builder().setKey("user.two").setComponentId(10L).setUserId(100).build()); + assertThat(results).hasSize(1); + assertThat(results.get(0).getValue()).isEqualTo("two"); + + results = dao.selectByQuery(PropertyQuery.builder().setKey("user.one").setUserId(100).build()); + assertThat(results).hasSize(1); + assertThat(results.get(0).getValue()).isEqualTo("one"); + } + + @Test public void setProperty_update() { setupData("update"); diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/select_by_query.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/select_by_query.xml new file mode 100644 index 00000000000..2873d69a5bc --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/select_by_query.xml @@ -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]"/> + + <!-- commons --> + <properties id="4" prop_key="commonslang.one" text_value="two" 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"/> + + <properties id="7" prop_key="commonslang.one" text_value="one" resource_id="12" user_id="[null]"/> + +</dataset> |