]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5305 Return component favourite information
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 15 May 2014 14:41:00 +0000 (16:41 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 15 May 2014 14:41:00 +0000 (16:41 +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/PropertyQuery.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/select_by_query.xml [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java
sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java
sonar-server/src/test/java/org/sonar/server/component/ws/ComponentsWsTest.java
sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app.json

index 521077c4bf1cb4c5dbf0dbef229b5d42af5ab30b..5ac44bf1c4e5b2ae359b5818ed0536496b9c8a85 100644 (file)
@@ -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);
index 8dff3f887de29bc3ffea6e51741b0b2263ac1891..6cdf28160aff8c3737ae5e66eaeb4513c59172fd 100644 (file)
@@ -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 (file)
index 0000000..da489e9
--- /dev/null
@@ -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);
+    }
+  }
+
+}
index eff90628032f0cea30a148ec2e6236a4e5e27234..30f14b0667214114539710c39e60eebcc33982e9 100644 (file)
     </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>
index 974bf4da7d8b8cd1eab0215291272c90b0ce391e..9e902a973575feddd63cfdecda3c921e009b4069 100644 (file)
@@ -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<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 (file)
index 0000000..2873d69
--- /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]"/>
+
+  <!-- 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>
index e7832443a903956249615905bcb487b77ad80cf5..538fd564b60ae3e71ab0ca7afa798bbd829d165c 100644 (file)
@@ -29,6 +29,9 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.component.ComponentDto;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
+import org.sonar.core.properties.PropertyQuery;
 import org.sonar.core.resource.ResourceDao;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.user.UserSession;
@@ -36,14 +39,18 @@ import org.sonar.server.user.UserSession;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
+import java.util.List;
+
 public class ComponentAppAction implements RequestHandler {
 
   private static final String KEY = "key";
 
   private final ResourceDao resourceDao;
+  private final PropertiesDao propertiesDao;
 
-  public ComponentAppAction(ResourceDao resourceDao) {
+  public ComponentAppAction(ResourceDao resourceDao, PropertiesDao propertiesDao) {
     this.resourceDao = resourceDao;
+    this.propertiesDao = propertiesDao;
   }
 
   void define(WebService.NewController controller) {
@@ -64,7 +71,8 @@ public class ComponentAppAction implements RequestHandler {
   @Override
   public void handle(Request request, Response response) {
     String fileKey = request.mandatoryParam(KEY);
-    UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey);
+    UserSession userSession = UserSession.get();
+    userSession.checkComponentPermission(UserRole.CODEVIEWER, fileKey);
 
     JsonWriter json = response.newJsonWriter();
     json.beginObject();
@@ -74,6 +82,13 @@ public class ComponentAppAction implements RequestHandler {
       throw new NotFoundException(String.format("Component '%s' does not exists.", fileKey));
     }
 
+    List<PropertyDto> propertyDtos = propertiesDao.selectByQuery(PropertyQuery.builder()
+      .setKey("favourite")
+      .setComponentId(component.getId())
+      .setUserId(userSession.userId())
+      .build());
+    boolean isFavourite = propertyDtos.size() == 1;
+
     json.prop("key", component.key());
     json.prop("path", component.path());
     json.prop("name", component.name());
@@ -85,6 +100,8 @@ public class ComponentAppAction implements RequestHandler {
     Component project = componentById(component.projectId());
     json.prop("projectName", project != null ? project.longName() : null);
 
+    json.prop("fav", isFavourite);
+
     json.endObject();
     json.close();
   }
@@ -97,4 +114,12 @@ public class ComponentAppAction implements RequestHandler {
     return null;
   }
 
+//  private Map<Integer, Integer> findDataFromComponent(String fileKey, String metricKey) {
+//    MeasureDataDto data = measuresDao.findByComponentKeyAndMetricKey(fileKey, metricKey);
+//    if (data != null) {
+//      return KeyValueFormat.parseIntInt(data.getData());
+//    }
+//    return Maps.newHashMap();
+//  }
+
 }
index d2a3b02b4602f65609c56228a527f444a3f0ccbe..a333bf7f6217247878b709cc30cc75a9e6c894e3 100644 (file)
@@ -27,10 +27,15 @@ import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.component.ComponentDto;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
+import org.sonar.core.properties.PropertyQuery;
 import org.sonar.core.resource.ResourceDao;
 import org.sonar.server.user.MockUserSession;
 import org.sonar.server.ws.WsTester;
 
+import static com.google.common.collect.Lists.newArrayList;
+import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -39,11 +44,14 @@ public class ComponentAppActionTest {
   @Mock
   ResourceDao resourceDao;
 
+  @Mock
+  PropertiesDao propertiesDao;
+
   WsTester tester;
 
   @Before
   public void setUp() throws Exception {
-    tester = new WsTester(new ComponentsWs(new ComponentAppAction(resourceDao)));
+    tester = new WsTester(new ComponentsWs(new ComponentAppAction(resourceDao, propertiesDao)));
   }
 
   @Test
@@ -57,6 +65,7 @@ public class ComponentAppActionTest {
     when(resourceDao.selectComponentByKey(componentKey)).thenReturn(file);
     when(resourceDao.findById(5L)).thenReturn(new ComponentDto().setId(5L).setLongName("SonarQube :: Plugin API"));
     when(resourceDao.findById(1L)).thenReturn(new ComponentDto().setId(1L).setLongName("SonarQube"));
+    when(propertiesDao.selectByQuery(any(PropertyQuery.class))).thenReturn(newArrayList(new PropertyDto()));
 
     WsTester.TestRequest request = tester.newGetRequest("api/components", "app").setParam("key", componentKey);
     request.execute().assertJson(getClass(), "app.json");
index 25c817d3ca77976a9cfd28113675faad4eb0579b..2b2c548ae2a546267a604c39bb9d0b563b94bed1 100644 (file)
@@ -24,6 +24,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.server.ws.RailsHandler;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.core.properties.PropertiesDao;
 import org.sonar.core.resource.ResourceDao;
 import org.sonar.server.ws.WsTester;
 
@@ -36,7 +37,7 @@ public class ComponentsWsTest {
 
   @Before
   public void setUp() throws Exception {
-    WsTester tester = new WsTester(new ComponentsWs(new ComponentAppAction(mock(ResourceDao.class))));
+    WsTester tester = new WsTester(new ComponentsWs(new ComponentAppAction(mock(ResourceDao.class), mock(PropertiesDao.class))));
     controller = tester.controller("api/components");
   }
 
index 28f3d67474e624167c52a5d203b61031ce2e946d..ff887f9a77067856e1a404cf9b66a582c6a899f1 100644 (file)
@@ -5,4 +5,5 @@
   "q": "FIL",
   "subProjectName": "SonarQube :: Plugin API",
   "projectName": "SonarQube",
+  "fav": true
 }