}
SqlSession session = mybatis.openSession();
try {
- List<ResourceDto> resourceDtos = session.getMapper(ResourceMapper.class).selectComponentsByQualifiers(qualifiers);
- List<Component> components = newArrayList();
- for (ResourceDto resourceDto : resourceDtos) {
- components.add(toComponent(resourceDto));
- }
- return components;
+ return toComponents(session.getMapper(ResourceMapper.class).selectComponentsByQualifiers(qualifiers));
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ /**
+ * Return enabled components including not completed ones, ie without snapshots or without snapshot having islast=true
+ */
+ public List<Component> selectComponentsIncludingNotCompletedOnesByQualifiers(Collection<String> qualifiers) {
+ if (qualifiers.isEmpty()) {
+ return Collections.emptyList();
+ }
+ SqlSession session = mybatis.openSession();
+ try {
+ return toComponents(session.getMapper(ResourceMapper.class).selectComponentsIncludingNotCompletedOnesByQualifiers(qualifiers));
} finally {
MyBatis.closeQuietly(session);
}
.setQualifier(resourceDto.getQualifier());
}
- public static List<ComponentDto> toComponents(List<ResourceDto> resourceDto){
- return newArrayList(Iterables.transform(resourceDto, new Function<ResourceDto, ComponentDto>() {
+ public static List<Component> toComponents(List<ResourceDto> resourceDto){
+ return newArrayList(Iterables.transform(resourceDto, new Function<ResourceDto, Component>() {
@Override
- public ComponentDto apply(@Nullable ResourceDto resourceDto) {
+ public Component apply(@Nullable ResourceDto resourceDto) {
return resourceDto == null ? null : toComponent(resourceDto);
}
}));
List<Integer> selectAuthorizedChildrenComponentIds(@Param("componentRootKeys") Collection<String> componentRootKeys,
@Param("userId") @Nullable Integer userId, @Param("role") String role);
- /**
- * @since 3.7
- */
List<ResourceDto> selectComponentsByQualifiers(@Param("qualifiers") Collection<String> qualifier);
+ List<ResourceDto> selectComponentsIncludingNotCompletedOnesByQualifiers(@Param("qualifiers") Collection<String> qualifier);
+
void insert(ResourceDto resource);
void update(ResourceDto resource);
</select>
<select id="selectComponentsByQualifiers" parameterType="map" resultMap="resourceResultMap">
+ select * from projects p
+ inner join snapshots s on s.project_id=p.id
+ <where>
+ <if test="qualifiers != null and qualifiers.size() > 0">
+ and <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator=" or " close=")">p.qualifier=#{qualifier}
+ </foreach>
+ </if>
+ and p.enabled=${_true}
+ and p.copy_resource_id is null
+ and s.islast=${_true}
+ </where>
+ </select>
+
+ <select id="selectComponentsIncludingNotCompletedOnesByQualifiers" parameterType="map" resultMap="resourceResultMap">
select * from projects p
<where>
<if test="qualifiers != null and qualifiers.size() > 0">
@Test
public void should_select_components_by_qualifiers(){
- setupData("fixture", "technical-project");
+ setupData("fixture-including-technical-project-and-not-finished-projects");
List<Component> components = dao.selectComponentsByQualifiers(newArrayList("TRK"));
assertThat(components).hasSize(1);
assertThat(components.get(0).key()).isEqualTo("org.struts:struts");
assertThat(((ComponentDto)components.get(0)).getId()).isEqualTo(1L);
- assertThat(dao.selectComponentsByQualifiers(newArrayList("unknown"))).isEmpty();
- assertThat(dao.selectComponentsByQualifiers(Collections.<String>emptyList())).isEmpty();
+ assertThat(dao.selectComponentsIncludingNotCompletedOnesByQualifiers(newArrayList("unknown"))).isEmpty();
+ assertThat(dao.selectComponentsIncludingNotCompletedOnesByQualifiers(Collections.<String>emptyList())).isEmpty();
+ }
+
+ @Test
+ public void should_select_components_including_not_finished_by_qualifiers(){
+ setupData("fixture-including-technical-project-and-not-finished-projects");
+
+ List<Component> components = dao.selectComponentsIncludingNotCompletedOnesByQualifiers(newArrayList("TRK"));
+ assertThat(components).hasSize(3);
+
+ assertThat(dao.selectComponentsIncludingNotCompletedOnesByQualifiers(newArrayList("unknown"))).isEmpty();
+ assertThat(dao.selectComponentsIncludingNotCompletedOnesByQualifiers(Collections.<String>emptyList())).isEmpty();
}
}
--- /dev/null
+<dataset>
+
+ <!-- root project -->
+ <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+ <snapshots id="1" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]"
+ status="P" islast="[true]" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path=""/>
+ <snapshots id="10" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]"
+ status="P" islast="[false]" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-01 13:58:00.00" build_date="2008-12-01 13:58:00.00"
+ version="[null]" path=""/>
+
+ <!-- project -->
+ <projects id="2" root_id="1" kee="org.struts:struts-core" name="Struts Core"
+ scope="PRJ" qualifier="BRC" long_name="Struts Core"
+ description="[null]" enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+ <snapshots id="2" project_id="2" parent_snapshot_id="1" root_project_id="1" root_snapshot_id="1"
+ status="P" islast="[true]" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="PRJ" qualifier="BRC" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path="1."/>
+
+ <!-- directory -->
+ <projects long_name="org.struts" id="3" scope="DIR" qualifier="PAC" kee="org.struts:struts:org.struts"
+ name="org.struts" root_id="1"
+ description="[null]"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+ <snapshots id="3" project_id="3" parent_snapshot_id="2" root_project_id="1" root_snapshot_id="1"
+ status="P" islast="[true]" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="DIR" qualifier="PAC" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path="1.2."/>
+
+ <!-- file -->
+ <projects long_name="org.struts.RequestContext" id="4" scope="FIL" qualifier="CLA" kee="org.struts:struts:org.struts.RequestContext"
+ name="RequestContext" root_id="1"
+ description="[null]"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+
+ <snapshots id="4" project_id="4" parent_snapshot_id="3" root_project_id="1" root_snapshot_id="1"
+ status="P" islast="[true]" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="FIL" qualifier="CLA" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path="1.2.3."/>
+
+ <!-- technical project -->
+ <projects id="5" root_id="[null]" scope="PRJ" qualifier="TRK" kee="COPYorg.struts:struts" name="Struts"
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="java" copy_resource_id="1" person_id="[null]"/>
+
+ <!-- project without snapshot islast=true-->
+ <projects id="6" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.apache.shindig" name="Shinding"
+ description="the description" long_name="Shinding"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+ <snapshots id="6" project_id="6" parent_snapshot_id="[null]" root_project_id="6" root_snapshot_id="[null]"
+ status="P" islast="[false]" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path=""/>
+
+ <!-- project without snapshot -->
+ <projects id="7" root_id="[null]" kee="org.sample:sample" name="Sample"
+ scope="PRJ" qualifier="TRK" long_name="Sample"
+ description="[null]" enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+
+ <!-- project not enabled -->
+ <projects id="8" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.apache:tika" name="Tika"
+ description="the description" long_name="Tika"
+ enabled="[false]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+ <snapshots id="8" project_id="8" parent_snapshot_id="[null]" root_project_id="8" root_snapshot_id="[null]"
+ status="P" islast="[true]" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path=""/>
+
+
+
+</dataset>
+++ /dev/null
-<dataset>
-
- <!-- technical project -->
- <projects id="5" root_id="[null]" scope="PRJ" qualifier="TRK" kee="COPYorg.struts:struts" name="Struts"
- description="the description" long_name="Apache Struts"
- enabled="[true]" language="java" copy_resource_id="1" person_id="[null]"/>
-
-
-</dataset>
import org.slf4j.LoggerFactory;
import org.sonar.api.component.Component;
import org.sonar.api.utils.Paging;
-import org.sonar.core.resource.ResourceDao;
import java.util.Collection;
import java.util.List;
public class DefaultComponentFinder {
private static final Logger LOG = LoggerFactory.getLogger(DefaultComponentFinder.class);
- private final ResourceDao resourceDao;
- public DefaultComponentFinder(ResourceDao resourceDao) {
- this.resourceDao = resourceDao;
- }
-
- public DefaultComponentQueryResult find(ComponentQuery query) {
+ public DefaultComponentQueryResult find(ComponentQuery query, List<Component> allComponents) {
LOG.debug("ComponentQuery : {}", query);
- long start = System.currentTimeMillis();
- try {
- // 1. Search components for selected qualifiers
- List<Component> components = resourceDao.selectComponentsByQualifiers(query.qualifiers());
- Collection<Component> foundComponents = search(query, components);
-
- // 2. Sort components
- Collection<? extends Component> sortedComponents = new ComponentsFinderSort(foundComponents, query).sort();
-
- if(ComponentQuery.NO_PAGINATION == query.pageSize()) {
- return new DefaultComponentQueryResult(sortedComponents).setQuery(query);
- } else {
- // 3. Apply pagination
- Paging paging = Paging.create(query.pageSize(), query.pageIndex(), foundComponents.size());
- Collection<? extends Component> pagedComponents = pagedComponents(sortedComponents, paging);
-
- return new DefaultComponentQueryResult(pagedComponents).setPaging(paging).setQuery(query);
- }
-
- } finally {
- LOG.debug("ComponentQuery execution time : {} ms", System.currentTimeMillis() - start);
+ Collection<Component> foundComponents = search(query, allComponents);
+
+ // Sort components
+ Collection<? extends Component> sortedComponents = new ComponentsFinderSort(foundComponents, query).sort();
+
+ // Apply pagination if needed
+ if (ComponentQuery.NO_PAGINATION == query.pageSize()) {
+ return new DefaultComponentQueryResult(sortedComponents).setQuery(query);
+ } else {
+ Paging paging = Paging.create(query.pageSize(), query.pageIndex(), foundComponents.size());
+ Collection<? extends Component> pagedComponents = pagedComponents(sortedComponents, paging);
+ return new DefaultComponentQueryResult(pagedComponents).setPaging(paging).setQuery(query);
}
}
import org.sonar.core.resource.ResourceDao;
import org.sonar.server.util.RubyUtils;
+import java.util.List;
import java.util.Map;
public class DefaultRubyComponentService implements RubyComponentService {
}
public DefaultComponentQueryResult find(Map<String, Object> params) {
- return finder.find(toQuery(params));
+ ComponentQuery query = toQuery(params);
+ List<Component> components = resourceDao.selectComponentsByQualifiers(query.qualifiers());
+ return finder.find(query, components);
+ }
+
+ public DefaultComponentQueryResult findWithUncompleteProjects(Map<String, Object> params) {
+ ComponentQuery query = toQuery(params);
+ List<Component> components = resourceDao.selectComponentsIncludingNotCompletedOnesByQualifiers(query.qualifiers());
+ return finder.find(query, components);
}
static ComponentQuery toQuery(Map<String, Object> props) {
def projects
params['pageSize'] = 25
params['qualifiers'] ||= 'TRK'
- @query_result = Internal.component_api.find(params)
+ @query_result = Internal.component_api.findWithUncompleteProjects(params)
@available_qualifiers = java_facade.getQualifiersWithProperty('hasRolePolicy').collect { |qualifier| [message("qualifiers.#{qualifier}"), qualifier] }.sort
if params['components'].blank?
params['pageSize'] = -1
- components = Internal.component_api.find(params).components().to_a
+ components = Internal.component_api.findWithUncompleteProjects(params).components().to_a
params['components'] = components.collect{|component| component.getId()}.join(',')
end
import org.junit.Test;
import org.sonar.api.component.Component;
import org.sonar.api.resources.Project;
-import org.sonar.core.resource.ResourceDao;
import java.util.Iterator;
+import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Matchers.anyCollection;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class DefaultComponentFinderTest {
- ResourceDao dao = mock(ResourceDao.class);
- DefaultComponentFinder finder = new DefaultComponentFinder(dao);
-
+ DefaultComponentFinder finder = new DefaultComponentFinder();
@Test
public void should_return_all_components_when_no_parameter() {
- when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList(
+ List<Component> components = newArrayList(
createProject("org.codehaus.sonar", "Sonar"),
createProject("org.apache.tika:tika", "Apache Tika"),
createProject("org.picocontainer:picocontainer-parent", "PicoContainer Parent")
- ));
+ );
ComponentQuery query = ComponentQuery.builder().build();
- DefaultComponentQueryResult results = finder.find(query);
+ DefaultComponentQueryResult results = finder.find(query, components);
assertThat(results.components()).hasSize(3);
Component component = results.components().iterator().next();
@Test
public void should_find_components_by_key_pattern() {
- when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList(
+ List<Component> components = newArrayList(
createProject("org.codehaus.sonar", "Sonar"),
createProject("org.apache.tika:tika", "Apache Tika"),
createProject("org.apache.jackrabbit:jackrabbit", "Apache Jackrabbit")
- ));
+ );
ComponentQuery query = ComponentQuery.builder().keys(newArrayList("org.apache")).build();
- assertThat(finder.find(query).components()).hasSize(2);
+ assertThat(finder.find(query, components).components()).hasSize(2);
}
@Test
public void should_find_components_by_name_pattern() {
- when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList(
+ List<Component> components = newArrayList(
createProject("org.codehaus.sonar", "Sonar"),
createProject("org.apache.tika:tika", "Apache Tika"),
createProject("org.apache.jackrabbit:jackrabbit", "Apache Jackrabbit")
- ));
+ );
ComponentQuery query = ComponentQuery.builder().names(newArrayList("Apache")).build();
- assertThat(finder.find(query).components()).hasSize(2);
+ assertThat(finder.find(query, components).components()).hasSize(2);
}
@Test
public void should_sort_result_by_name() {
- when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList(
+ List<Component> components = newArrayList(
createProject("org.codehaus.sonar", "Sonar"),
createProject("org.apache.tika:tika", "Apache Tika"),
createProject("org.picocontainer:picocontainer-parent", "PicoContainer Parent")
- ));
+ );
ComponentQuery query = ComponentQuery.builder().build();
- DefaultComponentQueryResult results = finder.find(query);
+ DefaultComponentQueryResult results = finder.find(query, components);
assertThat(results.components()).hasSize(3);
Iterator<? extends Component> iterator = results.components().iterator();
public void should_find_paginate_result() {
ComponentQuery query = ComponentQuery.builder().pageSize(1).pageIndex(1).build();
- when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList(
+ List<Component> components = newArrayList(
createProject("org.codehaus.sonar", "Sonar"),
createProject("org.apache.tika:tika", "Apache Tika"),
createProject("org.picocontainer:picocontainer-parent", "PicoContainer Parent")
- ));
+ );
- DefaultComponentQueryResult results = finder.find(query);
+ DefaultComponentQueryResult results = finder.find(query, components);
assertThat(results.paging().offset()).isEqualTo(0);
assertThat(results.paging().pages()).isEqualTo(3);
assertThat(results.paging().total()).isEqualTo(3);
ComponentQuery query = ComponentQuery.builder().pageSize(ComponentQuery.NO_PAGINATION)
.pageIndex(ComponentQuery.DEFAULT_PAGE_INDEX).build();
- when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList(
+ List<Component> components = newArrayList(
createProject("org.codehaus.sonar", "Sonar"),
createProject("org.apache.tika:tika", "Apache Tika"),
createProject("org.picocontainer:picocontainer-parent", "PicoContainer Parent")
- ));
+ );
- DefaultComponentQueryResult results = finder.find(query);
+ DefaultComponentQueryResult results = finder.find(query, components);
assertThat(results.paging()).isNull();
assertThat(results.components().size()).isEqualTo(3);
}
- private Component createProject(String key, String name){
+ private Component createProject(String key, String name) {
return new Project(key, null, name);
}
import org.sonar.api.component.Component;
import org.sonar.core.resource.ResourceDao;
+import java.util.List;
import java.util.Map;
import static com.google.common.collect.Lists.newArrayList;
assertThat(componentService.findByKey("struts")).isEqualTo(component);
}
+ @Test
+ public void should_find_with_uncomplete_projects() {
+ List<String> qualifiers = newArrayList("TRK");
+
+ Map<String, Object> map = newHashMap();
+ map.put("keys", newArrayList("org.codehaus.sonar"));
+ map.put("names", newArrayList("Sonar"));
+ map.put("qualifiers", qualifiers);
+ map.put("pageSize", 10l);
+ map.put("pageIndex", 50);
+ map.put("sort", "NAME");
+ map.put("asc", true);
+
+ componentService.findWithUncompleteProjects(map);
+ verify(resourceDao).selectComponentsIncludingNotCompletedOnesByQualifiers(anyListOf(String.class));
+ verify(finder).find(any(ComponentQuery.class), anyListOf(Component.class));
+ }
+
@Test
public void should_find() {
+ List<String> qualifiers = newArrayList("TRK");
+
Map<String, Object> map = newHashMap();
map.put("keys", newArrayList("org.codehaus.sonar"));
map.put("names", newArrayList("Sonar"));
- map.put("qualifiers", newArrayList("TRK"));
+ map.put("qualifiers", qualifiers);
map.put("pageSize", 10l);
map.put("pageIndex", 50);
map.put("sort", "NAME");
map.put("asc", true);
componentService.find(map);
- verify(finder).find(any(ComponentQuery.class));
+ verify(resourceDao).selectComponentsByQualifiers(anyListOf(String.class));
+ verify(finder).find(any(ComponentQuery.class), anyListOf(Component.class));
}
@Test