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;
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;
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 {
List<ComponentDto> components = dbClient.componentDao().selectByKeys(session, componentKeys);
if (!ignoreMissingComponents && components.size() < componentKeys.size()) {
- Collection<String> foundKeys = Collections2.transform(components, new Function<ComponentDto, String>() {
- @Override
- public String apply(ComponentDto component) {
- return component.key();
- }
- });
+ Collection<String> foundKeys = Collections2.transform(components, toKey());
Set<String> missingKeys = Sets.newHashSet(componentKeys);
missingKeys.removeAll(foundKeys);
throw new NotFoundException("The following component keys do not match any component:\n" +
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;
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;
/**
* 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;
}
private Map<String, ComponentDto> indexExistingDtosByKey(DbSession session) {
- return from(dbClient.componentDao()
- .selectAllComponentsFromProjectKey(session, treeRootHolder.getRoot().getKey()))
- .uniqueIndex(ComponentKey.INSTANCE);
- }
-
- private enum ComponentKey implements Function<ComponentDto, String> {
- 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<ComponentDtoHolder> {
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;
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;
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 :
DbSession session = dbClient.openSession(false);
try {
List<ComponentDto> baseModules = dbClient.componentDao().selectEnabledModulesFromProjectKey(session, treeRootHolder.getRoot().getKey());
- Map<String, ComponentDto> baseModulesByKey = FluentIterable.from(baseModules).uniqueIndex(ComponentDtoToKey.INSTANCE);
+ Map<String, ComponentDto> 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());
}
}
- private enum ComponentDtoToKey implements Function<ComponentDto, String> {
- INSTANCE;
-
- @Override
- public String apply(@Nonnull ComponentDto input) {
- return input.key();
- }
- }
}
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;
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.
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)),
}
private boolean mergeDeprecatedComponentParameters(DbSession session, Boolean onComponentOnly,
- @Nullable Collection<String> components,
- @Nullable Collection<String> componentUuids,
- @Nullable Collection<String> componentKeys,
- @Nullable Collection<String> componentRootUuids,
- @Nullable Collection<String> componentRoots,
- Set<String> allComponentUuids) {
+ @Nullable Collection<String> components,
+ @Nullable Collection<String> componentUuids,
+ @Nullable Collection<String> componentKeys,
+ @Nullable Collection<String> componentRootUuids,
+ @Nullable Collection<String> componentRoots,
+ Set<String> allComponentUuids) {
boolean effectiveOnComponentOnly = false;
failIfBothParametersSet(componentRootUuids, componentRoots, "componentRoots and componentRootUuids cannot be set simultaneously");
}
private void addComponentParameters(IssueQuery.Builder builder, DbSession session,
- boolean onComponentOnly,
- Collection<String> componentUuids,
- @Nullable Collection<String> projectUuids, @Nullable Collection<String> projects,
- @Nullable Collection<String> moduleUuids,
- @Nullable Collection<String> directories,
- @Nullable Collection<String> fileUuids,
- @Nullable Collection<String> authors) {
+ boolean onComponentOnly,
+ Collection<String> componentUuids,
+ @Nullable Collection<String> projectUuids, @Nullable Collection<String> projects,
+ @Nullable Collection<String> moduleUuids,
+ @Nullable Collection<String> directories,
+ @Nullable Collection<String> fileUuids,
+ @Nullable Collection<String> authors) {
builder.onComponentOnly(onComponentOnly);
if (onComponentOnly) {
private void addDeveloperTechnicalProjects(IssueQuery.Builder builder, DbSession session, Collection<String> componentUuids, Collection<String> authors) {
Collection<ComponentDto> technicalProjects = dbClient.componentDao().selectByUuids(session, componentUuids);
- Collection<String> developerUuids = Collections2.transform(technicalProjects, new Function<ComponentDto, String>() {
- @Override
- public String apply(ComponentDto input) {
- return input.projectUuid();
- }
- });
+ Collection<String> developerUuids = Collections2.transform(technicalProjects, toProjectUuid());
Collection<String> authorsFromProjects = authorsFromParamsOrFromDeveloper(session, developerUuids, authors);
builder.authors(authorsFromProjects);
- Collection<Long> projectIds = Collections2.transform(technicalProjects, new Function<ComponentDto, Long>() {
- @Override
- public Long apply(ComponentDto input) {
- return input.getCopyResourceId();
- }
- });
+ Collection<Long> projectIds = Collections2.transform(technicalProjects, toCopyResourceId());
List<ComponentDto> originalProjects = dbClient.componentDao().selectByIds(session, projectIds);
- Collection<String> projectUuids = Collections2.transform(originalProjects, new Function<ComponentDto, String>() {
- @Override
- public String apply(ComponentDto input) {
- return input.uuid();
- }
- });
+ Collection<String> projectUuids = Collections2.transform(originalProjects, toUuid());
builder.projectUuids(projectUuids);
}
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;
} finally {
MyBatis.closeQuietly(dbSession);
}
- return Maps.uniqueIndex(components, new ComponentToUuidFunction());
+ return Maps.uniqueIndex(components, ComponentDtoFunctions.toUuid());
}
private static class CoveredFileToFileUuidFunction implements Function<CoveredFileDoc, String> {
}
}
- private static class ComponentToUuidFunction implements Function<ComponentDto, String> {
- @Override
- public String apply(ComponentDto component) {
- return component.uuid();
- }
- }
-
}
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;
List<String> fileUuids = Lists.transform(tests, new TestToFileUuidFunction());
List<ComponentDto> components = dbClient.componentDao().selectByUuids(dbSession, fileUuids);
- return Maps.uniqueIndex(components, new ComponentToUuidFunction());
+ return Maps.uniqueIndex(components, ComponentDtoFunctions.toUuid());
}
private static class TestToFileUuidFunction implements Function<TestDoc, String> {
}
}
- private static class ComponentToUuidFunction implements Function<ComponentDto, String> {
- @Override
- public String apply(@Nonnull ComponentDto componentDto) {
- return componentDto.uuid();
- }
- }
-
private SearchResult<TestDoc> searchTests(DbSession dbSession, @Nullable String testUuid, @Nullable String testFileUuid, @Nullable String testFileKey,
@Nullable String sourceFileUuid, @Nullable Integer sourceFileLineNumber, SearchOptions searchOptions) {
if (testUuid != null) {
--- /dev/null
+/*
+ * 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<ComponentDto, String> toKey() {
+ return ToKey.INSTANCE;
+ }
+
+ private enum ToKey implements Function<ComponentDto, String> {
+ INSTANCE;
+
+ @Override
+ public String apply(@Nonnull ComponentDto input) {
+ return input.key();
+ }
+ }
+
+ public static Function<ComponentDto, String> toProjectUuid() {
+ return ToProjectUuid.INSTANCE;
+ }
+
+ private enum ToProjectUuid implements Function<ComponentDto, String> {
+ INSTANCE;
+
+ @Override
+ public String apply(ComponentDto input) {
+ return input.projectUuid();
+ }
+ }
+
+ public static Function<ComponentDto, String> toUuid() {
+ return ToUuid.INSTANCE;
+ }
+
+ private enum ToUuid implements Function<ComponentDto, String> {
+ INSTANCE;
+
+ @Override
+ public String apply(ComponentDto input) {
+ return input.uuid();
+ }
+ }
+
+ public static Function<ComponentDto, Long> toCopyResourceId() {
+ return ToCopyResourceId.INSTANCE;
+ }
+
+ private enum ToCopyResourceId implements Function<ComponentDto, Long> {
+ INSTANCE;
+
+ @Override
+ public Long apply(ComponentDto input) {
+ return input.getCopyResourceId();
+ }
+ }
+}