From b601ef5f65a28cf018357fa3ab8c40355928804a Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 13 Jun 2013 10:40:53 +0200 Subject: [PATCH] SONAR-4383 Replace user by userLogin in IssueFilters table --- .../sonar/core/issue/DefaultIssueFilter.java | 75 +++++++++++++++++++ .../sonar/core/issue/db/IssueFilterDto.java | 14 ++-- .../sonar/core/issue/db/IssueFilterMapper.xml | 14 ++-- .../org/sonar/core/persistence/schema-h2.ddl | 6 +- .../core/issue/db/IssueFilterDaoTest.java | 4 +- .../issue/db/IssueFilterDaoTest/shared.xml | 4 +- .../should_delete-result.xml | 2 +- .../should_insert-result.xml | 6 +- .../should_select_by_user.xml | 6 +- .../should_update-result.xml | 4 +- .../db/migrate/410_create_issue_filters.rb | 17 +++-- .../411_create_issue_filter_favourites.rb | 10 +-- 12 files changed, 119 insertions(+), 43 deletions(-) diff --git a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueFilter.java b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueFilter.java index 639dafbb62d..e5047917679 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueFilter.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueFilter.java @@ -20,9 +20,20 @@ package org.sonar.core.issue; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Splitter; +import com.google.common.collect.Lists; +import org.apache.commons.lang.StringUtils; + import javax.annotation.CheckForNull; import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Maps.newHashMap; public class DefaultIssueFilter { @@ -116,4 +127,68 @@ public class DefaultIssueFilter { return this; } + public Map dataAsMap(){ + return dataAsMap(data); + } + + @VisibleForTesting + Map dataAsMap(String data) { + Map map = newHashMap(); + + Iterable keyValues = Splitter.on(DefaultIssueFilter.SEPARATOR).split(data); + for (String keyValue : keyValues) { + String[] keyValueSplit = StringUtils.split(keyValue, DefaultIssueFilter.KEY_VALUE_SEPARATOR); + if (keyValueSplit.length != 2) { + throw new IllegalArgumentException("Key value should be separate by a '"+ DefaultIssueFilter.KEY_VALUE_SEPARATOR + "'"); + } + String key = keyValueSplit[0]; + String value = keyValueSplit[1]; + String[] listValues = StringUtils.split(value, DefaultIssueFilter.LIST_SEPARATOR); + if (listValues.length > 1) { + map.put(key, newArrayList(listValues)); + } else { + map.put(key, value); + } + } + return map; + } + + @VisibleForTesting + String mapAsdata(Map map) { + StringBuilder stringBuilder = new StringBuilder(); + + for (Map.Entry entries : map.entrySet()){ + String key = entries.getKey(); + Object value = entries.getValue(); + + stringBuilder.append(key); + stringBuilder.append(DefaultIssueFilter.KEY_VALUE_SEPARATOR); + + List valuesList = newArrayList(); + if (value instanceof List) { + // assume that it contains only strings + valuesList = (List) value; + } else if (value instanceof CharSequence) { + valuesList = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().split((CharSequence) value)); + } else { + stringBuilder.append(value); + } + for (Iterator valueListIter = valuesList.iterator(); valueListIter.hasNext();) { + Object valueList = valueListIter.next(); + stringBuilder.append(valueList); + if (valueListIter.hasNext()) { + stringBuilder.append(DefaultIssueFilter.LIST_SEPARATOR); + } + } + stringBuilder.append(DefaultIssueFilter.SEPARATOR); + } + + if (stringBuilder.length() > 0) { + // Delete useless last separator character + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + } + + return stringBuilder.toString(); + } + } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterDto.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterDto.java index 29093953dc9..9fe5c1e6291 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterDto.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterDto.java @@ -32,7 +32,7 @@ public class IssueFilterDto { private Long id; private String name; - private String user; + private String userLogin; private Boolean shared; private String description; private String data; @@ -57,12 +57,12 @@ public class IssueFilterDto { return this; } - public String getUser() { - return user; + public String getUserLogin() { + return userLogin; } - public IssueFilterDto setUser(@Nullable String user) { - this.user = user; + public IssueFilterDto setUserLogin(@Nullable String userLogin) { + this.userLogin = userLogin; return this; } @@ -115,7 +115,7 @@ public class IssueFilterDto { return new DefaultIssueFilter() .setId(id) .setName(name) - .setUser(user) + .setUser(userLogin) .setDescription(description) .setShared(shared) .setData(data) @@ -127,7 +127,7 @@ public class IssueFilterDto { return new IssueFilterDto() .setId(issueFilter.id()) .setName(issueFilter.name()) - .setUser(issueFilter.user()) + .setUserLogin(issueFilter.user()) .setDescription(issueFilter.description()) .setShared(issueFilter.shared()) .setData(issueFilter.data()) diff --git a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueFilterMapper.xml b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueFilterMapper.xml index c1a1090343a..e9a0c522572 100644 --- a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueFilterMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueFilterMapper.xml @@ -7,7 +7,7 @@ if.id, if.name as name, - if.user as user, + if.user_login as userLogin, if.shared as shared, if.description as description, if.data as data, @@ -22,12 +22,12 @@ - INSERT INTO issue_filters (name, user, shared, description, data, created_at, updated_at) - VALUES (#{name}, #{user}, #{shared}, #{description}, #{data}, #{createdAt}, #{updatedAt}) + INSERT INTO issue_filters (name, user_login, shared, description, data, created_at, updated_at) + VALUES (#{name}, #{userLogin}, #{shared}, #{description}, #{data}, #{createdAt}, #{updatedAt}) @@ -35,14 +35,14 @@ select issue_filters_seq.NEXTVAL from DUAL - INSERT INTO issue_filters (id, name, user, shared, description, data, created_at, updated_at) - VALUES (#{id}, #{name}, #{user}, #{shared}, #{description}, #{data}, #{createdAt}, #{updatedAt}) + INSERT INTO issue_filters (id, name, user_login, shared, description, data, created_at, updated_at) + VALUES (#{id}, #{name}, #{userLogin}, #{shared}, #{description}, #{data}, #{createdAt}, #{updatedAt}) update issue_filters set name=#{name}, - user=#{user}, + user_login=#{userLogin}, shared=#{shared}, description=#{description}, data=#{data}, diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl index 2a7f1523100..d32ca781b6b 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl @@ -508,7 +508,7 @@ CREATE TABLE "ISSUE_FILTERS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "NAME" VARCHAR(100) NOT NULL, "SHARED" BOOLEAN NOT NULL DEFAULT FALSE, - "USER" VARCHAR(40), + "USER_LOGIN" VARCHAR(40), "DESCRIPTION" VARCHAR(4000), "DATA" CLOB(2147483647), "CREATED_AT" TIMESTAMP, @@ -517,7 +517,7 @@ CREATE TABLE "ISSUE_FILTERS" ( CREATE TABLE "ISSUE_FILTER_FAVOURITES" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "USER" VARCHAR(40) NOT NULL, + "USER_LOGIN" VARCHAR(40) NOT NULL, "ISSUE_FILTER_ID" INTEGER NOT NULL, "CREATED_AT" TIMESTAMP ); @@ -658,6 +658,6 @@ CREATE INDEX "ISSUE_CHANGES_ISSUE_KEY" ON "ISSUE_CHANGES" ("ISSUE_KEY"); CREATE INDEX "ISSUE_FILTERS_NAME" ON "ISSUE_FILTERS" ("NAME"); -CREATE INDEX "ISSUE_FILTER_FAVS_USER" ON "ISSUE_FILTER_FAVOURITES" ("USER"); +CREATE INDEX "ISSUE_FILTER_FAVS_USER" ON "ISSUE_FILTER_FAVOURITES" ("USER_LOGIN"); CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS" ("LOGIN"); diff --git a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueFilterDaoTest.java b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueFilterDaoTest.java index 5d5f905716a..b8619fae88b 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueFilterDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueFilterDaoTest.java @@ -65,7 +65,7 @@ public class IssueFilterDaoTest extends AbstractDaoTestCase { IssueFilterDto filterDto = new IssueFilterDto(); filterDto.setName("Sonar Open issues"); - filterDto.setUser("michael"); + filterDto.setUserLogin("michael"); filterDto.setShared(true); filterDto.setDescription("All open issues on Sonar"); filterDto.setData("statuses=OPEN|componentRoots=org.codehaus.sonar"); @@ -82,7 +82,7 @@ public class IssueFilterDaoTest extends AbstractDaoTestCase { IssueFilterDto filterDto = new IssueFilterDto(); filterDto.setId(2L); filterDto.setName("Closed issues"); - filterDto.setUser("henry"); + filterDto.setUserLogin("henry"); filterDto.setShared(false); filterDto.setDescription("All closed issues"); filterDto.setData("statuses=CLOSED"); diff --git a/sonar-core/src/test/resources/org/sonar/core/issue/db/IssueFilterDaoTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/issue/db/IssueFilterDaoTest/shared.xml index 9cc2014c435..cf3bab11156 100644 --- a/sonar-core/src/test/resources/org/sonar/core/issue/db/IssueFilterDaoTest/shared.xml +++ b/sonar-core/src/test/resources/org/sonar/core/issue/db/IssueFilterDaoTest/shared.xml @@ -3,7 +3,7 @@ false, :limit => 100 - t.column 'user', :string, :null => true, :limit => 40 - t.column 'shared', :boolean, :null => false, :default => false - t.column 'description', :string, :null => true, :limit => 4000 - t.column 'data', :text, :null => true - t.timestamps + create_table :issue_filters do |t| + t.column :name, :string, :null => false, :limit => 100 + t.column :user_login, :string, :null => true, :limit => 40 + t.column :shared, :boolean, :null => false, :default => false + t.column :description, :string, :null => true, :limit => 4000 + t.column :data, :text, :null => true + t.column :created_at, :datetime, :null => true + t.column :updated_at, :datetime, :null => true end - add_index 'issue_filters', 'name', :name => 'issue_filters_name' + add_index :issue_filters, :name, :name => 'issue_filters_name' end end \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/411_create_issue_filter_favourites.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/411_create_issue_filter_favourites.rb index a45f1005f57..20527c045b3 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/411_create_issue_filter_favourites.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/411_create_issue_filter_favourites.rb @@ -25,12 +25,12 @@ class CreateIssueFilterFavourites < ActiveRecord::Migration def self.up - create_table 'issue_filter_favourites' do |t| - t.column 'user', :string, :null => false, :limit => 40 - t.column 'issue_filter_id', :integer, :null => false - t.column 'created_at', :datetime + create_table :issue_filter_favourites do |t| + t.column :user_login, :string, :null => false, :limit => 40 + t.column :issue_filter_id, :integer, :null => false + t.column :created_at, :datetime end - add_index 'issue_filter_favourites', 'user', :name => 'issue_filter_favs_user' + add_index :issue_filter_favourites, :user_login, :name => 'issue_filter_favs_user' end end -- 2.39.5