]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4287 Fix issue when loading action plan form
authorJulien Lancelot <julien.lancelot@gmail.com>
Thu, 16 May 2013 10:13:34 +0000 (12:13 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Thu, 16 May 2013 10:13:45 +0000 (12:13 +0200)
sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java
sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml
sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
sonar-server/src/main/java/org/sonar/server/issue/ActionPlanService.java
sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java
sonar-server/src/main/java/org/sonar/server/issue/IssueService.java
sonar-server/src/test/java/org/sonar/server/issue/ActionPlanServiceTest.java

index 79e5d1b13c09f2be8d015e4258dc3244ccbae6a1..2b6b04a712292b33a7b2b54b63e90e7240e13ea1 100644 (file)
@@ -25,6 +25,8 @@ import org.sonar.api.component.Component;
 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;
@@ -152,6 +154,26 @@ public class ResourceDao {
     }
   }
 
+  @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())
index 13093cce06cf52da846480ff5c478a374cd5b2a6..7e500ac589a79ecde435ba06334ace06c868c063 100644 (file)
@@ -55,6 +55,17 @@ public interface ResourceMapper {
    */
   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);
index ded87d52b8f054c73c1269b8834a8cbc03af4232..1b1270b7dbe0de275d31992d200eb2ed0905583d 100644 (file)
     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)
index 961467b41779c0d29fd847a33790909a0d6e5eea..1c5eb3bf23a9d960b2fa83a966a4c9c6c843d890 100644 (file)
@@ -159,6 +159,34 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
     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");
index d600ac0564f1f45d2de5a2350749dca2cde746e4..370e51cc5d16b44eb04acfe4b2af6682e8add477 100644 (file)
@@ -57,12 +57,12 @@ public class ActionPlanService implements ServerComponent {
   }
 
   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;
   }
 
@@ -95,13 +95,13 @@ public class ActionPlanService implements ServerComponent {
     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) {
@@ -111,7 +111,7 @@ public class ActionPlanService implements ServerComponent {
   }
 
   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) {
@@ -123,10 +123,18 @@ public class ActionPlanService implements ServerComponent {
     }));
   }
 
-  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;
   }
index a7e846688d6104a0d344345a8b9b90840c42f24a..a136bcf1c6d4c263ee5cf3d61d657b81c1cf6935 100644 (file)
@@ -38,6 +38,7 @@ import org.sonar.core.resource.ResourceQuery;
 import org.sonar.server.platform.UserSession;
 
 import javax.annotation.Nullable;
+
 import java.text.SimpleDateFormat;
 import java.util.Collection;
 import java.util.Date;
@@ -117,8 +118,9 @@ public class InternalRubyIssueService implements ServerComponent {
     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) {
index fcf93b139b7eb5a69fc51f1cac1b88662e5e086a..7cf04f2e0de01614eb84bcfaf792772d96885273 100644 (file)
@@ -128,7 +128,7 @@ public class IssueService implements ServerComponent {
     return issue;
   }
 
-  private DefaultIssue loadIssue(String issueKey) {
+  public DefaultIssue loadIssue(String issueKey) {
     return finder.findByKey(issueKey, UserRole.USER);
   }
 
index 203460b97d6bd72f52e67c0c4a1e1ae704e395ba..f44aa68d721e295c56ce15aee76d3414e102cb92 100644 (file)
@@ -102,17 +102,17 @@ public class ActionPlanServiceTest {
 
   @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