From b3101c1575b89000737c854abf87631f1ec22a6f Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 7 Sep 2015 10:51:25 +0200 Subject: [PATCH] add ComponentDtoFunctions to factor ComponentDto functions --- .../server/component/ComponentService.java | 11 +-- .../step/PersistComponentsStep.java | 17 +--- .../computation/step/ValidateProjectStep.java | 13 +-- .../sonar/server/issue/IssueQueryService.java | 55 +++++------- .../server/test/ws/CoveredFilesAction.java | 10 +-- .../org/sonar/server/test/ws/ListAction.java | 10 +-- .../db/component/ComponentDtoFunctions.java | 84 +++++++++++++++++++ 7 files changed, 116 insertions(+), 84 deletions(-) create mode 100644 sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java index aa11cb36b36..1530536274a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java @@ -20,7 +20,6 @@ package org.sonar.server.component; -import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Optional; import com.google.common.collect.Collections2; @@ -37,10 +36,10 @@ import org.sonar.api.i18n.I18n; import org.sonar.api.resources.Scopes; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.System2; -import org.sonar.core.util.Uuids; import org.sonar.api.web.UserRole; import org.sonar.core.component.ComponentKeys; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; @@ -49,6 +48,7 @@ import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; import static com.google.common.collect.Lists.newArrayList; +import static org.sonar.db.component.ComponentDtoFunctions.toKey; @ServerSide public class ComponentService { @@ -196,12 +196,7 @@ public class ComponentService { List components = dbClient.componentDao().selectByKeys(session, componentKeys); if (!ignoreMissingComponents && components.size() < componentKeys.size()) { - Collection foundKeys = Collections2.transform(components, new Function() { - @Override - public String apply(ComponentDto component) { - return component.key(); - } - }); + Collection foundKeys = Collections2.transform(components, toKey()); Set missingKeys = Sets.newHashSet(componentKeys); missingKeys.removeAll(foundKeys); throw new NotFoundException("The following component keys do not match any component:\n" + diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistComponentsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistComponentsStep.java index 65224e1f51b..a6f797b07a7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistComponentsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistComponentsStep.java @@ -20,7 +20,6 @@ package org.sonar.server.computation.step; -import com.google.common.base.Function; import com.google.common.base.Predicate; import java.util.Date; import java.util.Map; @@ -45,6 +44,7 @@ import org.sonar.server.computation.component.TreeRootHolder; import static com.google.common.collect.FluentIterable.from; import static org.sonar.db.component.ComponentDto.MODULE_UUID_PATH_SEP; +import static org.sonar.db.component.ComponentDtoFunctions.toKey; import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_ORDER; /** @@ -52,7 +52,6 @@ import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_ * Also feed the components cache {@link DbIdsRepositoryImpl} with component ids */ public class PersistComponentsStep implements ComputationStep { - private final DbClient dbClient; private final TreeRootHolder treeRootHolder; private final MutableDbIdsRepository dbIdsRepository; @@ -84,18 +83,8 @@ public class PersistComponentsStep implements ComputationStep { } private Map indexExistingDtosByKey(DbSession session) { - return from(dbClient.componentDao() - .selectAllComponentsFromProjectKey(session, treeRootHolder.getRoot().getKey())) - .uniqueIndex(ComponentKey.INSTANCE); - } - - private enum ComponentKey implements Function { - INSTANCE; - - @Override - public String apply(@Nonnull ComponentDto input) { - return input.key(); - } + return from(dbClient.componentDao().selectAllComponentsFromProjectKey(session, treeRootHolder.getRoot().getKey())) + .uniqueIndex(toKey()); } private class PersistComponentStepsVisitor extends PathAwareVisitorAdapter { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java index c3a9857430a..a6eeea2e770 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java @@ -20,7 +20,6 @@ package org.sonar.server.computation.step; -import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Optional; import com.google.common.collect.FluentIterable; @@ -29,7 +28,6 @@ import java.util.Date; import java.util.List; import java.util.Map; import javax.annotation.CheckForNull; -import javax.annotation.Nonnull; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; import org.sonar.api.utils.MessageException; @@ -49,6 +47,7 @@ import org.sonar.server.computation.component.TreeRootHolder; import org.sonar.server.computation.component.TypeAwareVisitorAdapter; import static org.sonar.api.utils.DateUtils.formatDateTime; +import static org.sonar.db.component.ComponentDtoFunctions.toKey; /** * Validate project and modules. It will fail in the following cases : @@ -82,7 +81,7 @@ public class ValidateProjectStep implements ComputationStep { DbSession session = dbClient.openSession(false); try { List baseModules = dbClient.componentDao().selectEnabledModulesFromProjectKey(session, treeRootHolder.getRoot().getKey()); - Map baseModulesByKey = FluentIterable.from(baseModules).uniqueIndex(ComponentDtoToKey.INSTANCE); + Map baseModulesByKey = FluentIterable.from(baseModules).uniqueIndex(toKey()); ValidateProjectsVisitor visitor = new ValidateProjectsVisitor(session, dbClient.componentDao(), settings.getBoolean(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION), baseModulesByKey); new DepthTraversalTypeAwareCrawler(visitor).visit(treeRootHolder.getRoot()); @@ -221,12 +220,4 @@ public class ValidateProjectStep implements ComputationStep { } } - private enum ComponentDtoToKey implements Function { - INSTANCE; - - @Override - public String apply(@Nonnull ComponentDto input) { - return input.key(); - } - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java index cf99bfbbec1..c14c2d845b6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java @@ -21,7 +21,6 @@ package org.sonar.server.issue; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; @@ -58,6 +57,9 @@ import org.sonar.server.user.UserSession; import org.sonar.server.util.RubyUtils; import static com.google.common.collect.Lists.newArrayList; +import static org.sonar.db.component.ComponentDtoFunctions.toCopyResourceId; +import static org.sonar.db.component.ComponentDtoFunctions.toProjectUuid; +import static org.sonar.db.component.ComponentDtoFunctions.toUuid; /** * This component is used to create an IssueQuery, in order to transform the component and component roots keys into uuid. @@ -120,9 +122,7 @@ public class IssueQueryService { RubyUtils.toStrings( ObjectUtils.defaultIfNull( params.get(IssueFilterParameters.PROJECT_KEYS), - params.get(IssueFilterParameters.PROJECTS) - ) - ), + params.get(IssueFilterParameters.PROJECTS))), RubyUtils.toStrings(params.get(IssueFilterParameters.MODULE_UUIDS)), RubyUtils.toStrings(params.get(IssueFilterParameters.DIRECTORIES)), RubyUtils.toStrings(params.get(IssueFilterParameters.FILE_UUIDS)), @@ -226,12 +226,12 @@ public class IssueQueryService { } private boolean mergeDeprecatedComponentParameters(DbSession session, Boolean onComponentOnly, - @Nullable Collection components, - @Nullable Collection componentUuids, - @Nullable Collection componentKeys, - @Nullable Collection componentRootUuids, - @Nullable Collection componentRoots, - Set allComponentUuids) { + @Nullable Collection components, + @Nullable Collection componentUuids, + @Nullable Collection componentKeys, + @Nullable Collection componentRootUuids, + @Nullable Collection componentRoots, + Set allComponentUuids) { boolean effectiveOnComponentOnly = false; failIfBothParametersSet(componentRootUuids, componentRoots, "componentRoots and componentRootUuids cannot be set simultaneously"); @@ -264,13 +264,13 @@ public class IssueQueryService { } private void addComponentParameters(IssueQuery.Builder builder, DbSession session, - boolean onComponentOnly, - Collection componentUuids, - @Nullable Collection projectUuids, @Nullable Collection projects, - @Nullable Collection moduleUuids, - @Nullable Collection directories, - @Nullable Collection fileUuids, - @Nullable Collection authors) { + boolean onComponentOnly, + Collection componentUuids, + @Nullable Collection projectUuids, @Nullable Collection projects, + @Nullable Collection moduleUuids, + @Nullable Collection directories, + @Nullable Collection fileUuids, + @Nullable Collection authors) { builder.onComponentOnly(onComponentOnly); if (onComponentOnly) { @@ -353,27 +353,12 @@ public class IssueQueryService { private void addDeveloperTechnicalProjects(IssueQuery.Builder builder, DbSession session, Collection componentUuids, Collection authors) { Collection technicalProjects = dbClient.componentDao().selectByUuids(session, componentUuids); - Collection developerUuids = Collections2.transform(technicalProjects, new Function() { - @Override - public String apply(ComponentDto input) { - return input.projectUuid(); - } - }); + Collection developerUuids = Collections2.transform(technicalProjects, toProjectUuid()); Collection authorsFromProjects = authorsFromParamsOrFromDeveloper(session, developerUuids, authors); builder.authors(authorsFromProjects); - Collection projectIds = Collections2.transform(technicalProjects, new Function() { - @Override - public Long apply(ComponentDto input) { - return input.getCopyResourceId(); - } - }); + Collection projectIds = Collections2.transform(technicalProjects, toCopyResourceId()); List originalProjects = dbClient.componentDao().selectByIds(session, projectIds); - Collection projectUuids = Collections2.transform(originalProjects, new Function() { - @Override - public String apply(ComponentDto input) { - return input.uuid(); - } - }); + Collection projectUuids = Collections2.transform(originalProjects, toUuid()); builder.projectUuids(projectUuids); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/test/ws/CoveredFilesAction.java b/server/sonar-server/src/main/java/org/sonar/server/test/ws/CoveredFilesAction.java index 3881c25a282..048228ad8c1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/test/ws/CoveredFilesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/test/ws/CoveredFilesAction.java @@ -35,6 +35,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentDtoFunctions; import org.sonar.server.test.index.CoveredFileDoc; import org.sonar.server.test.index.TestIndex; import org.sonar.server.user.UserSession; @@ -104,7 +105,7 @@ public class CoveredFilesAction implements TestsWsAction { } finally { MyBatis.closeQuietly(dbSession); } - return Maps.uniqueIndex(components, new ComponentToUuidFunction()); + return Maps.uniqueIndex(components, ComponentDtoFunctions.toUuid()); } private static class CoveredFileToFileUuidFunction implements Function { @@ -114,11 +115,4 @@ public class CoveredFilesAction implements TestsWsAction { } } - private static class ComponentToUuidFunction implements Function { - @Override - public String apply(ComponentDto component) { - return component.uuid(); - } - } - } diff --git a/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java index 2081e95baed..345d988d094 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java @@ -37,6 +37,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentDtoFunctions; import org.sonar.server.component.ComponentFinder; import org.sonar.server.es.SearchOptions; import org.sonar.server.es.SearchResult; @@ -169,7 +170,7 @@ public class ListAction implements TestsWsAction { List fileUuids = Lists.transform(tests, new TestToFileUuidFunction()); List components = dbClient.componentDao().selectByUuids(dbSession, fileUuids); - return Maps.uniqueIndex(components, new ComponentToUuidFunction()); + return Maps.uniqueIndex(components, ComponentDtoFunctions.toUuid()); } private static class TestToFileUuidFunction implements Function { @@ -179,13 +180,6 @@ public class ListAction implements TestsWsAction { } } - private static class ComponentToUuidFunction implements Function { - @Override - public String apply(@Nonnull ComponentDto componentDto) { - return componentDto.uuid(); - } - } - private SearchResult searchTests(DbSession dbSession, @Nullable String testUuid, @Nullable String testFileUuid, @Nullable String testFileKey, @Nullable String sourceFileUuid, @Nullable Integer sourceFileLineNumber, SearchOptions searchOptions) { if (testUuid != null) { diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java new file mode 100644 index 00000000000..c75518ebf86 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java @@ -0,0 +1,84 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 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.db.component; + +import com.google.common.base.Function; +import javax.annotation.Nonnull; + +/** + * Common Functions on ComponentDto. + */ +public final class ComponentDtoFunctions { + private ComponentDtoFunctions() { + // prevents instantiation + } + + public static Function toKey() { + return ToKey.INSTANCE; + } + + private enum ToKey implements Function { + INSTANCE; + + @Override + public String apply(@Nonnull ComponentDto input) { + return input.key(); + } + } + + public static Function toProjectUuid() { + return ToProjectUuid.INSTANCE; + } + + private enum ToProjectUuid implements Function { + INSTANCE; + + @Override + public String apply(ComponentDto input) { + return input.projectUuid(); + } + } + + public static Function toUuid() { + return ToUuid.INSTANCE; + } + + private enum ToUuid implements Function { + INSTANCE; + + @Override + public String apply(ComponentDto input) { + return input.uuid(); + } + } + + public static Function toCopyResourceId() { + return ToCopyResourceId.INSTANCE; + } + + private enum ToCopyResourceId implements Function { + INSTANCE; + + @Override + public Long apply(ComponentDto input) { + return input.getCopyResourceId(); + } + } +} -- 2.39.5