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.

DefaultOrganizationUuidProviderImpl.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.platform.db.migration.version;
  21. import java.sql.SQLException;
  22. import org.sonar.server.platform.db.migration.step.DataChange;
  23. import org.sonar.server.platform.db.migration.step.Select;
  24. import static com.google.common.base.Preconditions.checkState;
  25. /**
  26. * Component which can be injected in steps which provides access to the UUID of the default organization, it reads
  27. * directly from the BD.
  28. */
  29. public class DefaultOrganizationUuidProviderImpl implements DefaultOrganizationUuidProvider {
  30. private static final String INTERNAL_PROPERTY_DEFAULT_ORGANIZATION = "organization.default";
  31. @Override
  32. public String get(DataChange.Context context) throws SQLException {
  33. Select select = context.prepareSelect("select text_value from internal_properties where kee=?");
  34. select.setString(1, INTERNAL_PROPERTY_DEFAULT_ORGANIZATION);
  35. String uuid = select.get(row -> row.getString(1));
  36. checkState(uuid != null, "Default organization uuid is missing");
  37. return uuid;
  38. }
  39. @Override
  40. public String getAndCheck(DataChange.Context context) throws SQLException {
  41. String organizationUuid = get(context);
  42. Select select = context.prepareSelect("select uuid from organizations where uuid=?")
  43. .setString(1, organizationUuid);
  44. checkState(select.get(row -> row.getString(1)) != null,
  45. "Default organization with uuid '%s' does not exist in table ORGANIZATIONS",
  46. organizationUuid);
  47. return organizationUuid;
  48. }
  49. }