From: Simon Brandhof Date: Sat, 24 Nov 2012 20:40:32 +0000 (+0100) Subject: SONAR-3825 move existing measure filters X-Git-Tag: 3.4~279 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b06b8b6b59e3d90453d6b2aa15481bfd7fd5fcc5;p=sonarqube.git SONAR-3825 move existing measure filters --- diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java index 761f8d5c121..bff48d90249 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java @@ -19,8 +19,10 @@ */ package org.sonar.core.measure; +import com.google.common.base.Strings; import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerComponent; +import org.sonar.api.measures.Metric; import org.sonar.api.measures.MetricFinder; import org.sonar.api.utils.DateUtils; @@ -76,19 +78,41 @@ public class MeasureFilterFactory implements ServerComponent { filter.setSortOn(MeasureFilterSort.Field.valueOf(s.toUpperCase())); } } + for (int index = 1; index <= 3; index++) { + MeasureFilterCondition condition = toCondition(properties, index); + if (condition != null) { + filter.addCondition(condition); + } + } // if (map.containsKey("sortPeriod")) { // filter.setSortOnPeriod(((Long) map.get("sortPeriod")).intValue()); // } return filter; } + private MeasureFilterCondition toCondition(Map props, int index) { + MeasureFilterCondition condition = null; + String metricKey = (String) props.get("c" + index + "metric"); + String op = (String) props.get("c" + index + "op"); + String val = (String) props.get("c" + index + "val"); + if (!Strings.isNullOrEmpty(metricKey) && !Strings.isNullOrEmpty(op) && !Strings.isNullOrEmpty(val)) { + Metric metric = metricFinder.findByKey(metricKey); + condition = new MeasureFilterCondition(metric, op, Double.parseDouble(val)); + String period = (String) props.get("c" + index + "period"); + if (period != null) { + condition.setPeriod(Integer.parseInt(period)); + } + } + return condition; + } + private List toList(@Nullable Object obj) { List result = null; if (obj != null) { if (obj instanceof String) { - result = Arrays.asList((String)obj); + result = Arrays.asList((String) obj); } else { - result = (List)obj; + result = (List) obj; } } return result; diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java index 92f576c9a8f..dbf65739635 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java @@ -32,7 +32,7 @@ import java.util.List; */ public class DatabaseVersion implements BatchComponent, ServerComponent { - public static final int LAST_VERSION = 356; + public static final int LAST_VERSION = 357; public static enum Status { UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql index fbdfac52f67..a932334fdce 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql @@ -184,6 +184,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('353'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('354'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('355'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('356'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('357'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb index 3e187330e35..27dfc7c129b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb @@ -114,6 +114,11 @@ class MeasureFilter < ActiveRecord::Base class TreemapDisplay < Display KEY = :treemap + + def initialize(filter) + filter.set_criteria_default_value('columns', ['metric:ncloc', 'metric:violations']) + @metric_ids = @columns.map { |column| column.metric.id if column.metric }.compact.uniq + end end DISPLAYS = [ListDisplay, TreemapDisplay] @@ -205,8 +210,8 @@ class MeasureFilter < ActiveRecord::Base end -# ==== Options -# :user : the authenticated user + # ==== Options + # :user : the authenticated user def execute(controller, options={}) init_results diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/357_move_existing_measure_filters.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/357_move_existing_measure_filters.rb new file mode 100644 index 00000000000..f0b4b836f90 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/357_move_existing_measure_filters.rb @@ -0,0 +1,98 @@ +# +# Sonar, open source software quality management tool. +# Copyright (C) 2008-2012 SonarSource +# mailto:contact AT sonarsource DOT com +# +# Sonar 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. +# +# Sonar 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 Sonar; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 +# + +# +# Sonar 3.4 +# +class MoveExistingMeasureFilters < ActiveRecord::Migration + + # the new table + class MeasureFilter < ActiveRecord::Base + end + + # the old tables + class FilterColumn < ActiveRecord::Base + set_table_name 'filter_columns' + end + class Criteria < ActiveRecord::Base + set_table_name 'criteria' + end + class Resource < ActiveRecord::Base + set_table_name 'projects' + end + class Filter < ActiveRecord::Base + set_table_name 'filters' + end + + + def self.up + old_filters = Filter.find(:all) + say_with_time "Moving #{old_filters.size} measure filters" do + old_filters.each do |old_filter| + move(old_filter) + end + end + end + + private + + def self.move(old_filter) + new_filter = MeasureFilter.new + new_filter.name = old_filter.name + new_filter.user_id = old_filter.user_id + new_filter.shared = old_filter.shared + data = [] + data << 'onFavourites=true' if old_filter.favourites + if old_filter.resource_id + resource = Project.find(:first, :conditions => {:id => old_filter.id}) + data << "base=#{resource.kee}" if resource + end + data << "pageSize=#{old_filter.page_size}" if old_filter.page_size + + columns = [] + asc = nil + sort = nil + old_columns = FilterColumn.find(:all, :conditions => ['filter_id=?', old_filter.id], :order => 'order_index') + old_columns.each do |old_column| + column_key = old_column.family + column_key += ":#{old_column.kee}" if old_column.kee + columns << column_key + # TODO old_column.variation + if old_column.sort_direction=='ASC' + asc = true + sort = column_key + elsif old_column.sort_direction=='DESC' + asc = false + sort = column_key + end + end + data << "columns=#{columns.join(',')}" unless columns.empty? + if sort + data << "sort=#{sort}" + data << "asc=#{asc}" + end + + # TODO move criteria + + new_filter.data = data.join('|') unless data.empty? + new_filter.save + # TODO Filter.delete(old_filter.id) + end +end