From cc113fb821ec49d6380bba42b01d2c0dfdc16b4b Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 15 May 2014 16:41:00 +0200 Subject: SONAR-5305 Return component favourite information --- .../org/sonar/core/properties/PropertiesDao.java | 11 ++++ .../sonar/core/properties/PropertiesMapper.java | 2 + .../org/sonar/core/properties/PropertyQuery.java | 76 ++++++++++++++++++++++ .../org/sonar/core/properties/PropertiesMapper.xml | 16 +++++ .../sonar/core/properties/PropertiesDaoTest.java | 13 ++++ .../PropertiesDaoTest/select_by_query.xml | 19 ++++++ 6 files changed, 137 insertions(+) create mode 100644 sonar-core/src/main/java/org/sonar/core/properties/PropertyQuery.java create mode 100644 sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/select_by_query.xml (limited to 'sonar-core') 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 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 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 @@ + + update properties set text_value = #{value} where id = #{id} 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 @@ -142,6 +142,19 @@ public class PropertiesDaoTest extends AbstractDaoTestCase { assertThat(property.getValue(), is("two")); } + @Test + public void select_by_query() { + setupData("select_by_query"); + + List 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 @@ + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3