import org.sonar.core.component.ComponentDto;
import org.sonar.core.persistence.MyBatis;
+import javax.annotation.CheckForNull;
+
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
}
}
+ @CheckForNull
+ public ResourceDto getRootProjectByComponentKey(String componentKey) {
+ SqlSession session = mybatis.openSession();
+ try {
+ return session.getMapper(ResourceMapper.class).selectRootProjectByComponentKey(componentKey);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ @CheckForNull
+ public ResourceDto getRootProjectByComponentId(Long componentId) {
+ SqlSession session = mybatis.openSession();
+ try {
+ return session.getMapper(ResourceMapper.class).selectRootProjectByComponentId(componentId);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
public ComponentDto toComponent(ResourceDto resourceDto){
return new ComponentDto()
.setKey(resourceDto.getKey())
*/
List<ResourceDto> selectResourcesById(@Param("ids") List <List<Integer>> ids);
+ /**
+ * @since 3.6
+ */
+ ResourceDto selectRootProjectByComponentKey(@Param("componentKey") String componentKey);
+
+ /**
+ * @since 3.6
+ */
+ ResourceDto selectRootProjectByComponentId(@Param("componentId") Long componentId);
+
+
void insert(ResourceDto resource);
void update(ResourceDto resource);
select * from projects where scope='PRJ' and root_id=#{id}
</select>
+ <select id="selectRootProjectByComponentKey" parameterType="string" resultMap="resourceResultMap">
+ select rootProject.*
+ from projects p, snapshots s, projects rootProject
+ <where>
+ and p.kee=#{componentKey}
+ and s.project_id=p.id
+ and rootProject.id=s.root_project_id
+ </where>
+ </select>
+
+ <select id="selectRootProjectByComponentId" parameterType="long" resultMap="resourceResultMap">
+ select rootProject.*
+ from snapshots s, projects rootProject
+ <where>
+ and s.project_id=#{componentId}
+ and rootProject.id=s.root_project_id
+ </where>
+ </select>
+
<insert id="insert" parameterType="Resource" useGeneratedKeys="true" keyProperty="id">
insert into projects
(name, long_name, description, scope, qualifier, kee, language, root_id, copy_resource_id, person_id, enabled, created_at)
assertThat(component.qualifier()).isNotNull();
}
+ @Test
+ public void should_find_root_project_by_component_key() {
+ setupData("fixture");
+
+ ResourceDto resource = dao.getRootProjectByComponentKey("org.struts:struts:org.struts.RequestContext");
+ assertThat(resource.getName()).isEqualTo("Struts");
+
+ resource = dao.getRootProjectByComponentKey("org.struts:struts:org.struts");
+ assertThat(resource.getName()).isEqualTo("Struts");
+
+ resource = dao.getRootProjectByComponentKey("org.struts:struts-core");
+ assertThat(resource.getName()).isEqualTo("Struts");
+ }
+
+ @Test
+ public void should_find_root_project_by_component_Id() {
+ setupData("fixture");
+
+ ResourceDto resource = dao.getRootProjectByComponentId(4l);
+ assertThat(resource.getName()).isEqualTo("Struts");
+
+ resource = dao.getRootProjectByComponentId(3l);
+ assertThat(resource.getName()).isEqualTo("Struts");
+
+ resource = dao.getRootProjectByComponentId(2l);
+ assertThat(resource.getName()).isEqualTo("Struts");
+ }
+
@Test
public void should_update() {
setupData("update");
}
public ActionPlan create(ActionPlan actionPlan){
- actionPlanDao.save(ActionPlanDto.toActionDto(actionPlan, findProject(actionPlan.projectKey()).getId()));
+ actionPlanDao.save(ActionPlanDto.toActionDto(actionPlan, findComponent(actionPlan.projectKey()).getId()));
return actionPlan;
}
public ActionPlan update(ActionPlan actionPlan){
- actionPlanDao.update(ActionPlanDto.toActionDto(actionPlan, findProject(actionPlan.projectKey()).getId()));
+ actionPlanDao.update(ActionPlanDto.toActionDto(actionPlan, findComponent(actionPlan.projectKey()).getId()));
return actionPlan;
}
return toActionPlans(actionPlanDtos);
}
- public Collection<ActionPlan> findOpenByProjectKey(String projectKey) {
- Collection<ActionPlanDto> actionPlanDtos = actionPlanDao.findOpenByProjectId(findProject(projectKey).getId());
+ public Collection<ActionPlan> findOpenByComponentKey(String componentKey) {
+ Collection<ActionPlanDto> actionPlanDtos = actionPlanDao.findOpenByProjectId(findRootProject(componentKey).getId());
return toActionPlans(actionPlanDtos);
}
public List<ActionPlanStats> findActionPlanStats(String projectKey) {
- Collection<ActionPlanStatsDto> actionPlanStatsDtos = actionPlanStatsDao.findByProjectId(findProject(projectKey).getId());
+ Collection<ActionPlanStatsDto> actionPlanStatsDtos = actionPlanStatsDao.findByProjectId(findComponent(projectKey).getId());
return newArrayList(Iterables.transform(actionPlanStatsDtos, new Function<ActionPlanStatsDto, ActionPlanStats>() {
@Override
public ActionPlanStats apply(ActionPlanStatsDto actionPlanStatsDto) {
}
public boolean isNameAlreadyUsedForProject(String name, String projectKey) {
- return !actionPlanDao.findByNameAndProjectId(name, findProject(projectKey).getId()).isEmpty();
+ return !actionPlanDao.findByNameAndProjectId(name, findComponent(projectKey).getId()).isEmpty();
}
private Collection<ActionPlan> toActionPlans(Collection<ActionPlanDto> actionPlanDtos) {
}));
}
- private ResourceDto findProject(String projectKey){
- ResourceDto resourceDto = resourceDao.getResource(ResourceQuery.create().setKey(projectKey));
+ private ResourceDto findComponent(String componentKey){
+ ResourceDto resourceDto = resourceDao.getResource(ResourceQuery.create().setKey(componentKey));
if (resourceDto == null) {
- throw new IllegalArgumentException("Project " + projectKey + " does not exists.");
+ throw new IllegalArgumentException("Component " + componentKey + " does not exists.");
+ }
+ return resourceDto;
+ }
+
+ private ResourceDto findRootProject(String componentKey){
+ ResourceDto resourceDto = resourceDao.getRootProjectByComponentKey(componentKey);
+ if (resourceDto == null) {
+ throw new IllegalArgumentException("Component " + componentKey + " does not exists.");
}
return resourceDto;
}
import org.sonar.server.platform.UserSession;
import javax.annotation.Nullable;
+
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
return issueService.create((DefaultIssue) issue, UserSession.get());
}
- public Collection<ActionPlan> findOpenActionPlans(String projectKey) {
- return actionPlanService.findOpenByProjectKey(projectKey);
+ public Collection<ActionPlan> findOpenActionPlans(String issueKey) {
+ String componentKey = issueService.loadIssue(issueKey).componentKey();
+ return actionPlanService.findOpenByComponentKey(componentKey);
}
public ActionPlan findActionPlan(String actionPlanKey) {
return issue;
}
- private DefaultIssue loadIssue(String issueKey) {
+ public DefaultIssue loadIssue(String issueKey) {
return finder.findByKey(issueKey, UserRole.USER);
}
@Test
public void should_find_open_by_project_key() {
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
+ when(resourceDao.getRootProjectByComponentKey("org.sonar.Sample")).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
when(actionPlanDao.findOpenByProjectId(1l)).thenReturn(newArrayList(new ActionPlanDto().setKey("ABCD")));
- Collection<ActionPlan> results = actionPlanService.findOpenByProjectKey("org.sonar.Sample");
+ Collection<ActionPlan> results = actionPlanService.findOpenByComponentKey("org.sonar.Sample");
assertThat(results).hasSize(1);
assertThat(results.iterator().next().key()).isEqualTo("ABCD");
}
@Test(expected = IllegalArgumentException.class)
public void should_throw_exception_if_project_not_found_when_find_open_by_project_key() {
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(null);
- actionPlanService.findOpenByProjectKey("<Unkown>");
+ when(resourceDao.getRootProjectByComponentKey(anyString())).thenReturn(null);
+ actionPlanService.findOpenByComponentKey("<Unkown>");
}
@Test