From: Julien Lancelot Date: Wed, 19 Jun 2013 09:36:10 +0000 (+0200) Subject: SONAR-4394 shared issue filters name must be unique X-Git-Tag: 3.7~444 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9bdc6b392d81a344d660a16fbbced7df97291e2a;p=sonarqube.git SONAR-4394 shared issue filters name must be unique --- 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 db2b2b9fbf7..f2269b81a3e 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,25 +20,10 @@ 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 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 { - public static final String SEPARATOR = "|"; - public static final String KEY_VALUE_SEPARATOR = "="; - public static final String LIST_SEPARATOR = ","; - private Long id; private String name; private String user; @@ -60,10 +45,6 @@ public class DefaultIssueFilter { return issueFilter; } - public DefaultIssueFilter(Map mapData) { - setData(mapData); - } - public Long id() { return id; } @@ -136,80 +117,4 @@ public class DefaultIssueFilter { return this; } - public final DefaultIssueFilter setData(Map mapData) { - this.data = mapAsdata(mapData); - return this; - } - - /** - * Used by ui - */ - public Map dataAsMap(){ - return dataAsMap(data); - } - - @VisibleForTesting - final 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) { - 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 - final String mapAsdata(Map map) { - StringBuilder stringBuilder = new StringBuilder(); - - for (Map.Entry entries : map.entrySet()){ - String key = entries.getKey(); - Object value = entries.getValue(); - if (value != null) { - 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(key); - stringBuilder.append(DefaultIssueFilter.KEY_VALUE_SEPARATOR); - stringBuilder.append(value); - stringBuilder.append(DefaultIssueFilter.SEPARATOR); - } - if (!valuesList.isEmpty()) { - stringBuilder.append(key); - stringBuilder.append(DefaultIssueFilter.KEY_VALUE_SEPARATOR); - 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/IssueFilterSerializer.java b/sonar-core/src/main/java/org/sonar/core/issue/IssueFilterSerializer.java new file mode 100644 index 00000000000..3a1f5d20c9c --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/issue/IssueFilterSerializer.java @@ -0,0 +1,107 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.core.issue; + +import com.google.common.base.Splitter; +import com.google.common.collect.Lists; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.ServerComponent; + +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 IssueFilterSerializer implements ServerComponent { + + public static final String SEPARATOR = "|"; + public static final String KEY_VALUE_SEPARATOR = "="; + public static final String LIST_SEPARATOR = ","; + + public IssueFilterSerializer() { + + } + + public String serialize(Map map) { + StringBuilder stringBuilder = new StringBuilder(); + + for (Map.Entry entries : map.entrySet()) { + String key = entries.getKey(); + Object value = entries.getValue(); + if (value != null) { + 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(key); + stringBuilder.append(IssueFilterSerializer.KEY_VALUE_SEPARATOR); + stringBuilder.append(value); + stringBuilder.append(IssueFilterSerializer.SEPARATOR); + } + if (!valuesList.isEmpty()) { + stringBuilder.append(key); + stringBuilder.append(IssueFilterSerializer.KEY_VALUE_SEPARATOR); + for (Iterator valueListIter = valuesList.iterator(); valueListIter.hasNext(); ) { + Object valueList = valueListIter.next(); + stringBuilder.append(valueList); + if (valueListIter.hasNext()) { + stringBuilder.append(IssueFilterSerializer.LIST_SEPARATOR); + } + } + stringBuilder.append(IssueFilterSerializer.SEPARATOR); + } + } + } + + if (stringBuilder.length() > 0) { + // Delete useless last separator character + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + } + + return stringBuilder.toString(); + } + + public Map deserialize(String data) { + Map map = newHashMap(); + + Iterable keyValues = Splitter.on(IssueFilterSerializer.SEPARATOR).split(data); + for (String keyValue : keyValues) { + String[] keyValueSplit = StringUtils.split(keyValue, IssueFilterSerializer.KEY_VALUE_SEPARATOR); + if (keyValueSplit.length == 2) { + String key = keyValueSplit[0]; + String value = keyValueSplit[1]; + String[] listValues = StringUtils.split(value, IssueFilterSerializer.LIST_SEPARATOR); + if (listValues.length > 1) { + map.put(key, newArrayList(listValues)); + } else { + map.put(key, value); + } + } + } + return map; + } + +} diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterDao.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterDao.java index b4ffcaeb880..572d23e41ed 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterDao.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterDao.java @@ -80,10 +80,19 @@ public class IssueFilterDao implements BatchComponent, ServerComponent { } } - public List selectSharedForUser(String user) { + public List selectSharedWithoutUserFilters(String user) { SqlSession session = mybatis.openSession(); try { - return getMapper(session).selectSharedForUser(user); + return getMapper(session).selectSharedWithoutUserFilters(user); + } finally { + MyBatis.closeQuietly(session); + } + } + + public IssueFilterDto selectSharedWithoutUserFiltersByName(String name, String user, @Nullable Long existingId) { + SqlSession session = mybatis.openSession(); + try { + return getMapper(session).selectSharedWithoutUserFiltersByName(name, user, existingId); } finally { MyBatis.closeQuietly(session); } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterMapper.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterMapper.java index 53cf2b936eb..eb634af22b6 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueFilterMapper.java @@ -41,7 +41,10 @@ public interface IssueFilterMapper { List selectByUserWithOnlyFavoriteFilters(String user); - List selectSharedForUser(String user); + List selectSharedWithoutUserFilters(String user); + + @CheckForNull + IssueFilterDto selectSharedWithoutUserFiltersByName(@Param("name") String name, @Param("userLogin") String userLogin, @Nullable @Param("existingId") Long existingId); void insert(IssueFilterDto filter); 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 a31b00273c5..a85abad3bbe 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 @@ -52,12 +52,25 @@ - select from issue_filters filters filters.shared=${_true} - and filters.user_login<>#{user} + and filters.user_login<>#{userLogin} + + + + diff --git a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml index 06fa350595e..a6b5c76457f 100644 --- a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml @@ -217,7 +217,7 @@ +