You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentService.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.component;
  21. import org.sonar.api.server.ServerSide;
  22. import org.sonar.api.web.UserRole;
  23. import org.sonar.db.DbClient;
  24. import org.sonar.db.DbSession;
  25. import org.sonar.db.project.ProjectDto;
  26. import org.sonar.server.es.ProjectIndexer;
  27. import org.sonar.server.es.ProjectIndexers;
  28. import org.sonar.server.project.Project;
  29. import org.sonar.server.project.ProjectLifeCycleListeners;
  30. import org.sonar.server.project.RekeyedProject;
  31. import org.sonar.server.user.UserSession;
  32. import static java.util.Collections.singleton;
  33. import static java.util.Collections.singletonList;
  34. import static org.sonar.core.component.ComponentKeys.checkProjectKey;
  35. @ServerSide
  36. public class ComponentService {
  37. private final DbClient dbClient;
  38. private final UserSession userSession;
  39. private final ProjectIndexers projectIndexers;
  40. private final ProjectLifeCycleListeners projectLifeCycleListeners;
  41. public ComponentService(DbClient dbClient, UserSession userSession, ProjectIndexers projectIndexers, ProjectLifeCycleListeners projectLifeCycleListeners) {
  42. this.dbClient = dbClient;
  43. this.userSession = userSession;
  44. this.projectIndexers = projectIndexers;
  45. this.projectLifeCycleListeners = projectLifeCycleListeners;
  46. }
  47. public void updateKey(DbSession dbSession, ProjectDto project, String newKey) {
  48. userSession.checkProjectPermission(UserRole.ADMIN, project);
  49. checkProjectKey(newKey);
  50. dbClient.componentKeyUpdaterDao().updateKey(dbSession, project.getUuid(), newKey);
  51. projectIndexers.commitAndIndexProjects(dbSession, singletonList(project), ProjectIndexer.Cause.PROJECT_KEY_UPDATE);
  52. Project newProject = new Project(project.getUuid(), newKey, project.getName(), project.getDescription(), project.getTags());
  53. projectLifeCycleListeners.onProjectsRekeyed(singleton(new RekeyedProject(newProject, project.getKey())));
  54. }
  55. }