import org.sonar.core.persistence.MyBatis;
import javax.annotation.Nullable;
+
import java.util.List;
import java.util.Map;
}
}
+ 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);
PropertyDto selectByKey(PropertyDto key);
+ List<PropertyDto> selectByQuery(@Param("query") PropertyQuery query);
+
void update(PropertyDto property);
void insert(PropertyDto property);
--- /dev/null
+/*
+ * 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);
+ }
+ }
+
+}
</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>
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");
--- /dev/null
+<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>
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;
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) {
@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();
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());
Component project = componentById(component.projectId());
json.prop("projectName", project != null ? project.longName() : null);
+ json.prop("fav", isFavourite);
+
json.endObject();
json.close();
}
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();
+// }
+
}
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)
@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
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");
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;
@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");
}
"q": "FIL",
"subProjectName": "SonarQube :: Plugin API",
"projectName": "SonarQube",
+ "fav": true
}