]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4923 profiles page now uses Java face (expect for alerts)
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 7 Jan 2014 16:04:47 +0000 (17:04 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 7 Jan 2014 16:04:47 +0000 (17:04 +0100)
17 files changed:
sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java
sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java
sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml
sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java
sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/inheritance.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/parent.xml [deleted file]
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfile.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectService.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRule.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileSearch.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb
sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectServiceTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileSearchTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java

index f8a313b7aed2919e5ce7f2b827ec26e45ba2a119..646d3edf2d9cd48426684a2eb07ad6efe5168d56 100644 (file)
@@ -36,6 +36,40 @@ public class QualityProfileDao implements ServerComponent {
     this.mybatis = mybatis;
   }
 
+  public void insert(QualityProfileDto dto, SqlSession session) {
+    session.getMapper(QualityProfileMapper.class).insert(dto);
+  }
+
+  public void insert(QualityProfileDto dto) {
+    SqlSession session = mybatis.openSession();
+    try {
+      insert(dto, session);
+      session.commit();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+  public void update(QualityProfileDto dto) {
+    SqlSession session = mybatis.openSession();
+    try {
+      session.getMapper(QualityProfileMapper.class).update(dto);
+      session.commit();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+  public void delete(Integer id) {
+    SqlSession session = mybatis.openSession();
+    try {
+      session.getMapper(QualityProfileMapper.class).delete(id);
+      session.commit();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
   public List<QualityProfileDto> selectAll() {
     SqlSession session = mybatis.openSession();
     try {
@@ -81,53 +115,46 @@ public class QualityProfileDao implements ServerComponent {
     }
   }
 
-  public QualityProfileDto selectByNameAndLanguage(String name, String language) {
+  public List<QualityProfileDto> selectChildren(String name, String language) {
     SqlSession session = mybatis.openSession();
     try {
-      return session.getMapper(QualityProfileMapper.class).selectByNameAndLanguage(StringUtils.upperCase(name), language);
+      return session.getMapper(QualityProfileMapper.class).selectChildren(StringUtils.upperCase(name), language);
     } finally {
       MyBatis.closeQuietly(session);
     }
   }
 
-  public List<ComponentDto> selectProjects(String propertyKey, String propertyValue) {
+  public int countChildren(String name, String language) {
     SqlSession session = mybatis.openSession();
     try {
-      return session.getMapper(QualityProfileMapper.class).selectProjects(propertyKey, propertyValue);
+      return session.getMapper(QualityProfileMapper.class).countChildren(StringUtils.upperCase(name), language);
     } finally {
       MyBatis.closeQuietly(session);
     }
   }
 
-  public void insert(QualityProfileDto dto, SqlSession session) {
-    session.getMapper(QualityProfileMapper.class).insert(dto);
-  }
-
-  public void insert(QualityProfileDto dto) {
+  public QualityProfileDto selectByNameAndLanguage(String name, String language) {
     SqlSession session = mybatis.openSession();
     try {
-      insert(dto, session);
-      session.commit();
+      return session.getMapper(QualityProfileMapper.class).selectByNameAndLanguage(StringUtils.upperCase(name), language);
     } finally {
       MyBatis.closeQuietly(session);
     }
   }
 
-  public void update(QualityProfileDto dto) {
+  public List<ComponentDto> selectProjects(String propertyKey, String propertyValue) {
     SqlSession session = mybatis.openSession();
     try {
-      session.getMapper(QualityProfileMapper.class).update(dto);
-      session.commit();
+      return session.getMapper(QualityProfileMapper.class).selectProjects(propertyKey, propertyValue);
     } finally {
       MyBatis.closeQuietly(session);
     }
   }
 
-  public void delete(Integer id) {
+  public int countProjects(String propertyKey, String propertyValue) {
     SqlSession session = mybatis.openSession();
     try {
-      session.getMapper(QualityProfileMapper.class).delete(id);
-      session.commit();
+      return session.getMapper(QualityProfileMapper.class).countProjects(propertyKey, propertyValue);
     } finally {
       MyBatis.closeQuietly(session);
     }
index f101e1036bed57f6dd3df97027963524d8913615..089e6b5d8bd358bb1e853a227676f4c8f1b15291 100644 (file)
@@ -29,6 +29,12 @@ import java.util.List;
 
 public interface QualityProfileMapper {
 
+  void insert(QualityProfileDto dto);
+
+  void update(QualityProfileDto dto);
+
+  void delete(Integer id);
+
   List<QualityProfileDto> selectAll();
 
   @CheckForNull
@@ -40,17 +46,23 @@ public interface QualityProfileMapper {
   @CheckForNull
   QualityProfileDto selectById(@Param("id") Integer id);
 
+
+  // INHERITANCE
+
   @CheckForNull
   QualityProfileDto selectParent(@Param("childId") Integer childId);
 
-  List<ComponentDto> selectProjects(@Param("value") String propertyValue, @Param("key") String propertyKey);
+  List<QualityProfileDto> selectChildren(@Param("name") String name, @Param("language") String language);
 
-  List<QualityProfileDto> selectByProject(@Param("projectId") Long projectId, @Param("key") String propertyKeyPrefix);
+  int countChildren(@Param("name") String name, @Param("language") String language);
 
-  void insert(QualityProfileDto dto);
+  // PROJECTS
 
-  void update(QualityProfileDto dto);
+  List<ComponentDto> selectProjects(@Param("value") String propertyValue, @Param("key") String propertyKey);
+
+  int countProjects(@Param("value") String propertyValue, @Param("key") String propertyKey);
+
+  List<QualityProfileDto> selectByProject(@Param("projectId") Long projectId, @Param("key") String propertyKeyPrefix);
 
-  void delete(Integer id);
 
 }
index 12943752f2343cd0aec03cd96a91ab478f791105..3fdf3073ce16e99ff37c7189de849b04ee84ed45 100644 (file)
     p.used_profile as used
   </sql>
 
+  <insert id="insert" parameterType="QualityProfile" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+    INSERT INTO rules_profiles (name, language, parent_name, version, used_profile)
+    VALUES (#{name}, #{language}, #{parent}, #{version}, #{used})
+  </insert>
+
+  <update id="update" parameterType="QualityProfile">
+    UPDATE rules_profiles SET
+    name=#{name},
+    language=#{language},
+    parent_name=#{parent},
+    version=#{version},
+    used_profile=#{used}
+    WHERE id=#{id}
+  </update>
+
+  <update id="delete" parameterType="Integer">
+    DELETE FROM rules_profiles WHERE id=#{id}
+  </update>
+
   <select id="selectAll" parameterType="map" resultType="QualityProfile">
     SELECT <include refid="profilesColumns"/>
     FROM rules_profiles p
     INNER JOIN rules_profiles child ON child.parent_name=p.name and child.language=p.language and child.id=#{childId}
   </select>
 
+  <select id="selectChildren" parameterType="map" resultType="QualityProfile">
+    SELECT <include refid="profilesColumns"/>
+    FROM rules_profiles p
+    <where>
+      AND UPPER(p.parent_name)=#{name}
+      AND p.language=#{language}
+    </where>
+    ORDER BY p.name
+  </select>
+
+  <select id="countChildren" parameterType="map" resultType="Integer">
+    SELECT count(p.id)
+    FROM rules_profiles p
+    <where>
+      AND UPPER(p.parent_name)=#{name}
+      AND p.language=#{language}
+    </where>
+  </select>
+
   <select id="selectDefaultProfile" parameterType="Integer" resultType="QualityProfile">
     SELECT <include refid="profilesColumns"/>
     FROM rules_profiles p
     </where>
   </select>
 
+  <select id="countProjects" parameterType="Integer" resultType="Integer">
+    SELECT count(projects.id)
+    FROM projects projects
+    LEFT JOIN properties ON properties.resource_id = projects.id
+    <where>
+      AND properties.resource_id IS NOT NULL
+      AND properties.prop_key=#{key}
+      AND properties.text_value LIKE #{value}
+    </where>
+  </select>
+
   <select id="selectByProject" parameterType="map" resultType="QualityProfile">
     SELECT DISTINCT <include refid="profilesColumns"/>
     FROM rules_profiles p
       AND prop.text_value LIKE p.name
   </select>
 
-  <insert id="insert" parameterType="QualityProfile" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
-    INSERT INTO rules_profiles (name, language, parent_name, version, used_profile)
-    VALUES (#{name}, #{language}, #{parent}, #{version}, #{used})
-  </insert>
-
-  <update id="update" parameterType="QualityProfile">
-    UPDATE rules_profiles SET
-    name=#{name},
-    language=#{language},
-    parent_name=#{parent},
-    version=#{version},
-    used_profile=#{used}
-    WHERE id=#{id}
-  </update>
-
-  <update id="delete" parameterType="Integer">
-    DELETE FROM rules_profiles WHERE id=#{id}
-  </update>
-
 </mapper>
 
index ba0d7e3d7e1e7f191867668618e794d66dad0cfc..f35bb8765bc6a341fa9141bbcd87896f1fdcb881 100644 (file)
@@ -37,6 +37,49 @@ public class QualityProfileDaoTest extends AbstractDaoTestCase {
     dao = new QualityProfileDao(getMyBatis());
   }
 
+
+  @Test
+  public void insert() {
+    setupData("shared");
+
+    QualityProfileDto dto = new QualityProfileDto()
+      .setName("Sonar Way with Findbugs")
+      .setLanguage("xoo")
+      .setParent("Sonar Way")
+      .setVersion(2)
+      .setUsed(true);
+
+    dao.insert(dto);
+
+    checkTables("insert", "rules_profiles");
+  }
+
+  @Test
+  public void update() {
+    setupData("shared");
+
+    QualityProfileDto dto = new QualityProfileDto()
+      .setId(1)
+      .setName("New Sonar Way with Findbugs")
+      .setLanguage("js")
+      .setParent("New Sonar Way")
+      .setVersion(3)
+      .setUsed(false);
+
+    dao.update(dto);
+
+    checkTables("update", "rules_profiles");
+  }
+
+  @Test
+  public void delete() {
+    setupData("shared");
+
+    dao.delete(1);
+
+    checkTables("delete", "rules_profiles");
+  }
+
   @Test
   public void select_all() {
     setupData("shared");
@@ -115,66 +158,60 @@ public class QualityProfileDaoTest extends AbstractDaoTestCase {
 
   @Test
   public void select_parent() {
-    setupData("parent");
+    setupData("inheritance");
 
     QualityProfileDto dto = dao.selectParent(1);
-    assertThat(dto.getId()).isEqualTo(2);
+    assertThat(dto.getId()).isEqualTo(3);
   }
 
   @Test
-  public void select_projects() {
-    setupData("projects");
+  public void select_children() {
+    setupData("inheritance");
 
-    assertThat(dao.selectProjects("Sonar Way", "sonar.profile.java")).hasSize(2);
-  }
+    List<QualityProfileDto> dtos = dao.selectChildren("Parent", "java");
 
-  @Test
-  public void select_by_project() {
-    setupData("projects");
+    assertThat(dtos).hasSize(2);
 
-    assertThat(dao.selectByProject(1L, "sonar.profile.%")).hasSize(2);
+    QualityProfileDto dto1 = dtos.get(0);
+    assertThat(dto1.getId()).isEqualTo(1);
+    assertThat(dto1.getName()).isEqualTo("Child1");
+    assertThat(dto1.getLanguage()).isEqualTo("java");
+    assertThat(dto1.getParent()).isEqualTo("Parent");
+
+    QualityProfileDto dto2 = dtos.get(1);
+    assertThat(dto2.getId()).isEqualTo(2);
+    assertThat(dto2.getName()).isEqualTo("Child2");
+    assertThat(dto2.getLanguage()).isEqualTo("java");
+    assertThat(dto2.getParent()).isEqualTo("Parent");
   }
 
   @Test
-  public void insert() {
-    setupData("shared");
+  public void count_children() {
+    setupData("inheritance");
 
-    QualityProfileDto dto = new QualityProfileDto()
-      .setName("Sonar Way with Findbugs")
-      .setLanguage("xoo")
-      .setParent("Sonar Way")
-      .setVersion(2)
-      .setUsed(true);
-
-    dao.insert(dto);
-
-    checkTables("insert", "rules_profiles");
+    assertThat(dao.countChildren("Parent", "java")).isEqualTo(2);
   }
 
   @Test
-  public void update() {
-    setupData("shared");
+  public void select_projects() {
+    setupData("projects");
 
-    QualityProfileDto dto = new QualityProfileDto()
-      .setId(1)
-      .setName("New Sonar Way with Findbugs")
-      .setLanguage("js")
-      .setParent("New Sonar Way")
-      .setVersion(3)
-      .setUsed(false);
+    assertThat(dao.selectProjects("Sonar Way", "sonar.profile.java")).hasSize(2);
+  }
 
-    dao.update(dto);
+  @Test
+  public void count_projects() {
+    setupData("projects");
 
-    checkTables("update", "rules_profiles");
+    assertThat(dao.countProjects("Sonar Way", "sonar.profile.java")).isEqualTo(2);
   }
 
   @Test
-  public void delete() {
-    setupData("shared");
-
-    dao.delete(1);
+  public void select_by_project() {
+    setupData("projects");
 
-    checkTables("delete", "rules_profiles");
+    assertThat(dao.selectByProject(1L, "sonar.profile.%")).hasSize(2);
   }
 
+
 }
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/inheritance.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/inheritance.xml
new file mode 100644 (file)
index 0000000..b74e6a0
--- /dev/null
@@ -0,0 +1,23 @@
+<dataset>
+
+  <rules_profiles id="1" name="Child1" language="java" parent_name="Parent" version="1"
+                  used_profile="[false]"/>
+
+  <rules_profiles id="2" name="Child2" language="java" parent_name="Parent" version="1"
+                  used_profile="[false]"/>
+
+  <rules_profiles id="3" name="Parent" language="java" parent_name="[null]" version="1"
+                  used_profile="[false]"/>
+
+  <!-- Same profile for another language -->
+
+  <rules_profiles id="4" name="Child1" language="js" parent_name="Parent" version="1"
+                  used_profile="[false]"/>
+
+  <rules_profiles id="5" name="Child2" language="js" parent_name="Parent" version="1"
+                  used_profile="[false]"/>
+
+  <rules_profiles id="6" name="Parent" language="js" parent_name="[null]" version="1"
+                  used_profile="[false]"/>
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/parent.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/parent.xml
deleted file mode 100644 (file)
index 2c97850..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-<dataset>
-
-  <rules_profiles id="1" name="Child" language="java" parent_name="Parent" version="1"
-                  used_profile="[false]"/>
-
-  <rules_profiles id="2" name="Parent" language="java" parent_name="[null]" version="1"
-                  used_profile="[false]"/>
-
-</dataset>
index 393f956c833a9ad91c45b6d6a37ab536ff28e069..cd1c97068bad8656480b6a547925fa7c17f6ec37 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.sonar.server.qualityprofile;
 
+import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 import org.sonar.core.qualityprofile.db.QualityProfileDto;
 
 public class QProfile {
@@ -98,4 +99,9 @@ public class QProfile {
       .setVersion(dto.getVersion())
       .setUsed(dto.isUsed());
   }
+
+  @Override
+  public String toString() {
+    return new ReflectionToStringBuilder(this).toString();
+  }
 }
index f67c5d1c8a1fbdfd3cb74bca3245d1cff8153931..e9c9c0d17fdce8ae89fd62351fb70134cf0259e7 100644 (file)
@@ -59,6 +59,10 @@ public class QProfileProjectService implements ServerComponent {
     return new QProfileProjects(QProfile.from(qualityProfile), projects);
   }
 
+  public int countProjects(QProfile profile) {
+    return qualityProfileDao.countProjects(profile.name(), PROPERTY_PREFIX + profile.language());
+  }
+
   public List<QProfile> profiles(long projectId) {
     List<QualityProfileDto> dtos = qualityProfileDao.selectByProject(projectId, PROPERTY_PREFIX  + "%");
     return newArrayList(Iterables.transform(dtos, new Function<QualityProfileDto, QProfile>() {
index 1cf94932a04e4ec0add1deefd13a57fa733b5d18..ebadde626785e8f00f4865a106dbb8fdee583b5a 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.qualityprofile;
 
+import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 import org.elasticsearch.common.collect.Lists;
 import org.elasticsearch.common.collect.Maps;
 import org.elasticsearch.common.joda.time.format.ISODateTimeFormat;
@@ -28,6 +29,7 @@ import org.sonar.server.rule.ActiveRuleDocument;
 import org.sonar.server.rule.RuleDocument;
 
 import javax.annotation.CheckForNull;
+
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -218,5 +220,9 @@ public class QProfileRule {
     return activeRuleNote;
   }
 
+  @Override
+  public String toString() {
+    return new ReflectionToStringBuilder(this).toString();
+  }
 
 }
index 0fbc2a093e7bc1e594e436e94ea6c02231af855a..27d9ad2abc93465352c4825eea0b4f4b34231477 100644 (file)
@@ -41,13 +41,7 @@ public class QProfileSearch implements ServerComponent {
   }
 
   public List<QProfile> allProfiles() {
-    List<QualityProfileDto> dtos = dao.selectAll();
-    return newArrayList(Iterables.transform(dtos, new Function<QualityProfileDto, QProfile>() {
-      @Override
-      public QProfile apply(QualityProfileDto input) {
-        return QProfile.from(input);
-      }
-    }));
+    return toQProfiles(dao.selectAll());
   }
 
   @CheckForNull
@@ -59,4 +53,20 @@ public class QProfileSearch implements ServerComponent {
     return null;
   }
 
+  public List<QProfile> children(QProfile profile) {
+    return toQProfiles(dao.selectChildren(profile.name(), profile.language()));
+  }
+
+  public int countChildren(QProfile profile) {
+    return dao.countChildren(profile.name(), profile.language());
+  }
+
+  private List<QProfile> toQProfiles(List<QualityProfileDto> dtos){
+    return newArrayList(Iterables.transform(dtos, new Function<QualityProfileDto, QProfile>() {
+      @Override
+      public QProfile apply(QualityProfileDto input) {
+        return QProfile.from(input);
+      }
+    }));
+  }
 }
index 59194e1d41068e35330548bc60398b3c9a772e3b..3efe0d21145e7a94b128db39ffd4fdfba4dbccd8 100644 (file)
@@ -79,8 +79,7 @@ public class QProfiles implements ServerComponent {
   // TODO
   //
   // PROFILES
-  // list all profiles (including activated rules count)
-  // delete profile from profile id (Delete alerts, activeRules, activeRuleParams, activeRuleNotes, Projects)
+  // delete profile from profile id (Delete alerts, activeRules, activeRuleParams, activeRuleNotes, Projects and check profile is not a default one and has no child)
   // copy profile
   // export profile from profile id
   // export profile from profile id and plugin key
@@ -96,10 +95,19 @@ public class QProfiles implements ServerComponent {
   // active rule parameter validation (only Integer types are checked)
 
 
+  public List<QProfile> allProfiles() {
+    return search.allProfiles();
+  }
+
   public QProfile profile(int id) {
     return QProfile.from(findNotNull(id));
   }
 
+  @CheckForNull
+  public QProfile defaultProfile(String language) {
+    return search.defaultProfile(language);
+  }
+
   @CheckForNull
   public QProfile parent(QProfile profile) {
     QualityProfileDto parent = findQualityProfile(profile.parent(), profile.language());
@@ -109,13 +117,12 @@ public class QProfiles implements ServerComponent {
     return null;
   }
 
-  public List<QProfile> allProfiles() {
-    return search.allProfiles();
+  public List<QProfile> children(QProfile profile) {
+    return search.children(profile);
   }
 
-  @CheckForNull
-  public QProfile defaultProfile(String language) {
-    return search.defaultProfile(language);
+  public int countChildren(QProfile profile) {
+    return search.countChildren(profile);
   }
 
   public NewProfileResult newProfile(String name, String language, Map<String, String> xmlProfilesByPlugin) {
@@ -149,6 +156,10 @@ public class QProfiles implements ServerComponent {
     return projectService.projects(qualityProfile);
   }
 
+  public int countProjects(QProfile profile) {
+    return projectService.countProjects(profile);
+  }
+
   /**
    * Used in /project/profile
    */
@@ -202,6 +213,10 @@ public class QProfiles implements ServerComponent {
     return rules.countInactiveProfileRules(query);
   }
 
+  public long countProfileRules(QProfile profile) {
+    return rules.countProfileRules(ProfileRuleQuery.create(profile.id()));
+  }
+
   public ProfileRuleChanged activateRule(int profileId, int ruleId, String severity) {
     QualityProfileDto qualityProfile = findNotNull(profileId);
     RuleDto rule = findRuleNotNull(ruleId);
@@ -234,9 +249,9 @@ public class QProfiles implements ServerComponent {
       activeRuleOperations.deleteActiveRuleParam(activeRule, activeRuleParam, userSession);
     } else if (activeRuleParam != null) {
       activeRuleOperations.updateActiveRuleParam(activeRule, activeRuleParam, value, userSession);
-    } else {
-      // No active rule param and no value -> do nothing
     }
+    // If no active rule param and no value -> do nothing
+
     return activeRuleChanged(qualityProfile, activeRule);
   }
 
index 84386facf7f7e9688baab260727fb78bc2332e8b..b4bce6d2c8313513d665b031040ecef81bef8ba0 100644 (file)
@@ -28,11 +28,12 @@ class ProfilesController < ApplicationController
   # GET /profiles/index
   def index
     add_breadcrumbs ProfilesController::root_breadcrumb
-    @profiles = Profile.all
-    Api::Utils.insensitive_sort!(@profiles){|profile| profile.name}
+    call_backend do
+      @profiles = Internal.quality_profiles.allProfiles().to_a
+    end
+    Api::Utils.insensitive_sort!(@profiles){|profile| profile.name()}
   end
 
-
   # GET /profiles/create_form?language=<language>
   def create_form
     require_parameters 'language'
index dc7e680a736e75a6204206869cbd3b4c2c6c5d0c..eb019e18398d618cfd29ca998fc8e2e85e1f19c2 100644 (file)
@@ -18,6 +18,7 @@
 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 #
 module ProfilesHelper
+
   def languages
     controller.java_facade.getLanguages()
   end
@@ -44,4 +45,25 @@ module ProfilesHelper
     end
     html
   end
-end
\ No newline at end of file
+
+  def profile_key(qProfile)
+    "#{qProfile.language().to_s}_#{qProfile.name().to_s}"
+  end
+
+  def alerts_count(qProfile)
+    Alert.count(:all, :conditions => ['profile_id=?', qProfile.id()])
+  end
+
+  def projects_count(qProfile)
+    Internal.quality_profiles.countProjects(qProfile).to_i
+  end
+
+  def profile_rules_count(qProfile)
+    Internal.quality_profiles.countProfileRules(qProfile).to_i
+  end
+
+  def children_count(qProfile)
+    Internal.quality_profiles.countChildren(qProfile).to_i
+  end
+
+end
index 2d87322db48c040b2a565b6cef77919b6969a652..4b2b3648620a73e9b4b126dd16fdca34b6761900 100644 (file)
@@ -16,6 +16,7 @@
 
 <%
    Api::Utils.insensitive_sort(languages){|l| l.getName()}.each do |language|
+      default_profile = Internal.quality_profiles.defaultProfile(language.getKey())
 %>
   <div class="line-block">
     <% if profiles_administrator? %>
     </tr>
     </thead>
     <tbody>
-    <% @profiles.select { |p| p.language == language.getKey() }.each do |profile| %>
-      <tr class="<%= cycle 'even', 'odd', :name => language.getKey() -%>" id="<%= u profile.key %>">
+    <% @profiles.select { |p| p.language == language.getKey() }.each do |profile| 
+       key = profile_key(profile)
+       alerts_count = alerts_count(profile)
+       projects_count = projects_count(profile)
+       profile_rules_count = profile_rules_count(profile)
+       is_default_profile = !default_profile.nil? && (default_profile.name() == profile.name())
+    %>
+      <tr class="<%= cycle 'even', 'odd', :name => language.getKey() -%>" id="<%= u key %>">
         <td  width="40%">
-          <a href="<%= url_for :controller => 'rules_configuration', :action => 'index', :id => profile.id -%>" id="rules-<%= language.getKey() -%>-<%= u(profile.name) -%>"><%= h profile.name %></a>
+          <a href="<%= url_for :controller => 'rules_configuration', :action => 'index', :id => profile.id() -%>" 
+             id="rules-<%= language.getKey() -%>-<%= u(profile.name()) -%>"><%= h profile.name() %></a>
         </td>
 
         <td align="right" width="10%">
-          <span id="activated_rules_<%= u profile.key -%>"><%= profile.count_active_rules -%></span>
+          <span id="activated_rules_<%= u key -%>"><%= profile_rules_count -%></span>
         </td>
 
-        <td align="right" width="10%"><span id="alerts_<%= u profile.key -%>"><%= profile.alerts.size -%></span></td>
+        <td align="right" width="10%"><span id="alerts_<%= u key -%>"><%= alerts_count -%></span></td>
 
         <td align="right" width="10%" nowrap>
-          <% unless profile.default_profile? %>
-            <span id="projects_<%= u profile.key -%>"><%= profile.projects.size -%></span>
+          <% unless is_default_profile %>
+            <span id="projects_<%= u key -%>"><%= projects_count -%></span>
           <% end %>
         </td>
 
         <td align="right" width="10%" nowrap>
-          <% if !profile.default_profile? && profiles_administrator? %>
-            <%= link_to_action message('set_as_default'), "#{ApplicationController.root_context}/profiles/set_as_default?id=#{profile.id}",
-                               :id => "activate_#{profile.key.parameterize}",
+          <% if !is_default_profile && profiles_administrator? %>
+            <%= link_to_action message('set_as_default'), "#{ApplicationController.root_context}/profiles/set_as_default?id=#{profile.id()}",
+                               :id => "activate_#{key.parameterize}",
                                :class => 'link-action',
                                :confirm_title => message('set_as_default'),
-                               :confirm_msg => message('quality_profiles.are_you_sure_want_x_profile_as_default', :params => [profile.name]),
+                               :confirm_msg => message('quality_profiles.are_you_sure_want_x_profile_as_default', :params => [profile.name()]),
                                :confirm_button => message('set_as_default')
             -%>
           <% end %>
-          <% if profile.default_profile? %>
-            <%= image_tag 'tick.png', :id => "is_active_#{u profile.key}" %>
+          <% if is_default_profile %>
+            <%= image_tag 'tick.png', :id => "is_active_#{u key}" %>
           <% end %>
         </td>
 
-
         <% if profiles_administrator? %>
 
           <td align="right" nowrap>
-            <form method="post" action="<%= ApplicationController.root_context -%>/profiles/backup/<%= profile.id -%>" id="backup-<%= profile.key.parameterize -%>-form">
-              <a href="#" class="link-action" name="button_backup" id="backup_<%= u profile.key %>" onclick="$j('#backup-<%= profile.key.parameterize -%>-form').submit();return false;"><%= message('backup_verb') -%></a>
+            <form method="post" action="<%= ApplicationController.root_context -%>/profiles/backup/<%= profile.id() -%>" id="backup-<%= key.parameterize -%>-form">
+              <a href="#" class="link-action" name="button_backup" id="backup_<%= u key %>" onclick="$j('#backup-<%= key.parameterize -%>-form').submit();return false;"><%= message('backup_verb') -%></a>
             </form>
           </td>
 
           <td align="right">
-            <a id="rename-<%= profile.key.parameterize -%>" href="<%= ApplicationController.root_context -%>/profiles/rename_form/<%= profile.id -%>" class="link-action open-modal"><%= message('rename') -%></a>
+            <a id="rename-<%= key.parameterize -%>" href="<%= ApplicationController.root_context -%>/profiles/rename_form/<%= profile.id() -%>" class="link-action open-modal"><%= message('rename') -%></a>
           </td>
 
           <td align="right">
-            <a id="copy-<%= profile.key.parameterize -%>" href="<%= ApplicationController.root_context -%>/profiles/copy_form/<%= profile.id -%>" class="link-action open-modal"><%= message('copy') -%></a>
+            <a id="copy-<%= key.parameterize -%>" href="<%= ApplicationController.root_context -%>/profiles/copy_form/<%= profile.id() -%>" class="link-action open-modal"><%= message('copy') -%></a>
           </td>
 
           <td>
-            <% if profile.deletable? %>
-              <%= link_to_action message('delete'), "#{ApplicationController.root_context}/profiles/delete/#{profile.id}",
+            <% if !is_default_profile && (children_count(profile) == 0) %>
+              <%= link_to_action message('delete'), "#{ApplicationController.root_context}/profiles/delete/#{profile.id()}",
                                  :class => 'link-action link-red',
-                                 :id => "delete_#{profile.key.parameterize}",
+                                 :id => "delete_#{key.parameterize}",
                                  :confirm_button => message('delete'),
                                  :confirm_title => 'quality_profiles.delete_confirm_title',
                                  :confirm_msg => 'quality_profiles.are_you_sure_want_delete_profile_x',
-                                 :confirm_msg_params => [profile.name]
+                                 :confirm_msg_params => [profile.name()]
               -%>
             <% end %>
           </td>
index 81a8283041040b73fe1203e2f22bdc81decad9df..b360eda5029e90314055befe69c8735d9e61196d 100644 (file)
@@ -71,6 +71,12 @@ public class QProfileProjectServiceTest {
     assertThat(result.projects()).hasSize(1);
   }
 
+  @Test
+  public void count_projects() throws Exception {
+    service.countProjects(new QProfile().setId(1).setName("My profile").setLanguage("java"));
+    verify(qualityProfileDao).countProjects("My profile", "sonar.profile.java");
+  }
+
   @Test
   public void search_profiles_from_project() throws Exception {
     QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java");
index 973f4f89c89c1e7497a351e8a633c6db03439201..1dcb06b73ab82702fd6f1de5d35596cfb3cdf969 100644 (file)
@@ -32,6 +32,7 @@ import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -65,6 +66,18 @@ public class QProfileSearchTest {
     assertThat(qProfile.used()).isFalse();
   }
 
+  @Test
+  public void search_children_profiles() throws Exception {
+    search.children(new QProfile().setName("Sonar Way").setLanguage("java"));
+    verify(dao).selectChildren("Sonar Way", "java");
+  }
+
+  @Test
+  public void count_children_profiles() throws Exception {
+    search.countChildren(new QProfile().setName("Sonar Way").setLanguage("java"));
+    verify(dao).countChildren("Sonar Way", "java");
+  }
+
   @Test
   public void default_profile() throws Exception {
     when(dao.selectDefaultProfile("java", "sonar.profile.java")).thenReturn(
index 160a637c43921389c99f18b16f5656f8b4b9d19b..461605e6e17614a7a56fe2f7763df76aef1e7f73 100644 (file)
@@ -99,6 +99,18 @@ public class QProfilesTest {
     verify(qualityProfileDao).selectById(1);
   }
 
+  @Test
+  public void search_profiles() throws Exception {
+    qProfiles.allProfiles();
+    verify(search).allProfiles();
+  }
+
+  @Test
+  public void search_default_profile_by_language() throws Exception {
+    qProfiles.defaultProfile("java");
+    verify(search).defaultProfile("java");
+  }
+
   @Test
   public void search_parent_profile() throws Exception {
     QualityProfileDto parent = new QualityProfileDto().setId(2).setName("Parent").setLanguage("java");
@@ -113,15 +125,17 @@ public class QProfilesTest {
   }
 
   @Test
-  public void search_profiles() throws Exception {
-    qProfiles.allProfiles();
-    verify(search).allProfiles();
+  public void search_children() throws Exception {
+    QProfile profile = new QProfile();
+    qProfiles.children(profile);
+    verify(search).children(profile);
   }
 
   @Test
-  public void search_default_profile_by_language() throws Exception {
-    qProfiles.defaultProfile("java");
-    verify(search).defaultProfile("java");
+  public void count_children() throws Exception {
+    QProfile profile = new QProfile();
+    qProfiles.countChildren(profile);
+    verify(search).countChildren(profile);
   }
 
   @Test
@@ -221,6 +235,13 @@ public class QProfilesTest {
     verify(projectService).projects(qualityProfile);
   }
 
+  @Test
+  public void count_projects() throws Exception {
+    QProfile profile = new QProfile();
+    qProfiles.countProjects(profile);
+    verify(projectService).countProjects(profile);
+  }
+
   @Test
   public void get_profiles_from_project_id() throws Exception {
     qProfiles.profiles(1);