3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.component;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.function.Function;
27 import java.util.stream.Collectors;
28 import javax.annotation.CheckForNull;
29 import javax.annotation.Nullable;
30 import org.apache.commons.lang.StringUtils;
31 import org.sonar.api.resources.Qualifiers;
32 import org.sonar.api.resources.Scopes;
33 import org.sonar.core.util.Uuids;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.component.ComponentDto;
37 import org.sonar.db.component.ComponentWithModuleUuidDto;
38 import org.sonar.db.component.KeyWithUuidDto;
40 public class ComponentUuidFactoryWithMigration implements ComponentUuidFactory {
41 private final Map<String, String> uuidsByDbKey = new HashMap<>();
42 private final Map<String, String> uuidsByMigratedKey = new HashMap<>();
44 public ComponentUuidFactoryWithMigration(DbClient dbClient, DbSession dbSession, String rootKey, Function<String, String> pathToKey, Map<String, String> reportModulesPath) {
45 Map<String, String> modulePathsByUuid;
46 List<KeyWithUuidDto> keys = dbClient.componentDao().selectUuidsByKeyFromProjectKey(dbSession, rootKey);
47 keys.forEach(dto -> uuidsByDbKey.put(dto.key(), dto.uuid()));
49 if (!reportModulesPath.isEmpty()) {
50 modulePathsByUuid = loadModulePathsByUuid(dbClient, dbSession, rootKey, reportModulesPath);
52 if (!modulePathsByUuid.isEmpty()) {
53 doMigration(dbClient, dbSession, rootKey, pathToKey, modulePathsByUuid);
58 private void doMigration(DbClient dbClient, DbSession dbSession, String rootKey, Function<String, String> pathToKey, Map<String, String> modulePathsByUuid) {
59 List<ComponentWithModuleUuidDto> dtos = loadComponentsWithModuleUuid(dbClient, dbSession, rootKey);
60 for (ComponentWithModuleUuidDto dto : dtos) {
61 if ("/".equals(dto.path())) {
66 if (Scopes.PROJECT.equals(dto.scope())) {
67 String modulePathFromRootProject = modulePathsByUuid.get(dto.uuid());
68 if (modulePathFromRootProject != null || StringUtils.isEmpty(dto.moduleUuid())) {
69 // means that it's a root or a module with a valid path (to avoid overwriting key of root)
70 pathToKey.apply(modulePathFromRootProject);
71 uuidsByMigratedKey.put(pathToKey.apply(modulePathFromRootProject), dto.uuid());
74 String modulePathFromRootProject = modulePathsByUuid.get(dto.moduleUuid());
75 String componentPath = createComponentPath(dto, modulePathFromRootProject);
76 uuidsByMigratedKey.put(pathToKey.apply(componentPath), dto.uuid());
82 private static String createComponentPath(ComponentWithModuleUuidDto dto, @Nullable String modulePathFromRootProject) {
83 if (StringUtils.isEmpty(modulePathFromRootProject)) {
87 if (StringUtils.isEmpty(dto.path())) {
88 // will be the case for modules
89 return modulePathFromRootProject;
92 return modulePathFromRootProject + "/" + dto.path();
95 private static List<ComponentWithModuleUuidDto> loadComponentsWithModuleUuid(DbClient dbClient, DbSession dbSession, String rootKey) {
96 return dbClient.componentDao().selectEnabledComponentsWithModuleUuidFromProjectKey(dbSession, rootKey);
99 private static Map<String, String> loadModulePathsByUuid(DbClient dbClient, DbSession dbSession, String rootKey, Map<String, String> pathByModuleKey) {
100 List<ComponentDto> moduleDtos = dbClient.componentDao()
101 .selectProjectAndModulesFromProjectKey(dbSession, rootKey, true).stream()
102 .filter(c -> Qualifiers.MODULE.equals(c.qualifier()))
103 .collect(Collectors.toList());
105 if (moduleDtos.isEmpty()) {
106 return Collections.emptyMap();
109 Map<String, String> modulePathByUuid = new HashMap<>();
110 for (ComponentDto dto : moduleDtos) {
111 String relativePath = pathByModuleKey.get(dto.getKey());
112 if (relativePath != null) {
113 modulePathByUuid.put(dto.uuid(), relativePath);
116 return modulePathByUuid;
120 * Get UUID from component having the same key in database if it exists, otherwise look for migrated keys, and finally generate a new one.
123 public String getOrCreateForKey(String key) {
124 return uuidsByDbKey.computeIfAbsent(key, k1 -> uuidsByMigratedKey.computeIfAbsent(k1, k2 -> Uuids.create()));