From c04694a7b2db32cdf9cc8aaf872bc90ffbf9c95d Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 24 Jan 2017 17:00:41 +0100 Subject: [PATCH] SONAR-8690 add support for default templates to OrganizationDao --- .../db/organization/DefaultTemplates.java | 59 +++++++ .../db/organization/OrganizationDao.java | 40 ++++- .../db/organization/OrganizationMapper.java | 5 + .../db/organization/OrganizationMapper.xml | 26 ++- .../db/organization/DefaultTemplatesTest.java | 55 ++++++ .../db/organization/OrganizationDaoTest.java | 158 +++++++++++++++++- 6 files changed, 328 insertions(+), 15 deletions(-) create mode 100644 sonar-db/src/main/java/org/sonar/db/organization/DefaultTemplates.java create mode 100644 sonar-db/src/test/java/org/sonar/db/organization/DefaultTemplatesTest.java diff --git a/sonar-db/src/main/java/org/sonar/db/organization/DefaultTemplates.java b/sonar-db/src/main/java/org/sonar/db/organization/DefaultTemplates.java new file mode 100644 index 00000000000..6a7b4ef288c --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/organization/DefaultTemplates.java @@ -0,0 +1,59 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.organization; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +import static java.util.Objects.requireNonNull; + +public class DefaultTemplates { + private String project; + private String view; + + @CheckForNull + public String getProject() { + return project; + } + + public DefaultTemplates setProject(String project) { + requireNonNull(project, "project default template can't be null"); + this.project = project; + return this; + } + + @CheckForNull + public String getView() { + return view; + } + + public DefaultTemplates setView(@Nullable String view) { + this.view = view; + return this; + } + + @Override + public String toString() { + return "DefaultTemplates{" + + "project='" + project + '\'' + + ", view='" + view + '\'' + + '}'; + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDao.java b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDao.java index 2705c8355e4..17ee6afe7a8 100644 --- a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDao.java +++ b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDao.java @@ -52,7 +52,7 @@ public class OrganizationDao implements Dao { } public Optional selectByUuid(DbSession dbSession, String uuid) { - requireNonNull(uuid, "uuid can't be null"); + checkUuid(uuid); return Optional.ofNullable(getMapper(dbSession).selectByUuid(uuid)); } @@ -61,6 +61,32 @@ public class OrganizationDao implements Dao { return Optional.ofNullable(getMapper(dbSession).selectByKey(key)); } + public List selectByUuids(DbSession dbSession, Set organizationUuids) { + if (organizationUuids.size() == 1) { + return Collections.singletonList(getMapper(dbSession).selectByUuid(organizationUuids.iterator().next())); + } + return executeLargeInputs(organizationUuids, getMapper(dbSession)::selectByUuids); + } + + /** + * Retrieve the default template of the specified organization if: + *
    + *
  1. the specified organization exists
  2. + *
  3. the project default permission template is defined
  4. + *
+ */ + public Optional getDefaultTemplates(DbSession dbSession, String organizationUuid) { + checkUuid(organizationUuid); + return Optional.ofNullable(getMapper(dbSession).selectDefaultTemplatesByUuid(organizationUuid)); + } + + public void setDefaultTemplates(DbSession dbSession, String uuid, DefaultTemplates defaultTemplates) { + checkUuid(uuid); + checkDefaultTemplates(defaultTemplates); + long now = system2.now(); + getMapper(dbSession).updateDefaultTemplates(uuid, defaultTemplates, now); + } + public int update(DbSession dbSession, OrganizationDto organization) { checkDto(organization); organization.setUpdatedAt(system2.now()); @@ -83,10 +109,12 @@ public class OrganizationDao implements Dao { return dbSession.getMapper(OrganizationMapper.class); } - public List selectByUuids(DbSession dbSession, Set organizationUuids) { - if (organizationUuids.size() == 1) { - return Collections.singletonList(getMapper(dbSession).selectByUuid(organizationUuids.iterator().next())); - } - return executeLargeInputs(organizationUuids, getMapper(dbSession)::selectByUuids); + private static void checkUuid(String uuid) { + requireNonNull(uuid, "uuid can't be null"); + } + + private static void checkDefaultTemplates(DefaultTemplates defaultTemplates) { + requireNonNull(defaultTemplates, "defaultTemplates can't be null"); + requireNonNull(defaultTemplates.getProject(), "defaultTemplates.project can't be null"); } } diff --git a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationMapper.java b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationMapper.java index 8a2b9b3c1fb..94a35be76be 100644 --- a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationMapper.java @@ -37,6 +37,8 @@ public interface OrganizationMapper { List selectByUuids(@Param("uuids") List uuids); + DefaultTemplates selectDefaultTemplatesByUuid(@Param("uuid") String uuid); + /** * Update the organization with UUID specified by {@link OrganizationDto#getUuid()}. *

@@ -46,6 +48,9 @@ public interface OrganizationMapper { */ int update(@Param("organization") OrganizationDto organization); + void updateDefaultTemplates(@Param("organizationUuid") String organizationUuid, + @Param("defaultTemplates") DefaultTemplates defaultTemplates, @Param("now") long now); + int deleteByUuid(@Param("uuid") String uuid); int deleteByKey(@Param("key") String key); diff --git a/sonar-db/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml b/sonar-db/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml index c7ab1ea8461..15de61bfaaa 100644 --- a/sonar-db/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml @@ -12,6 +12,11 @@ org.created_at as "createdAt", org.updated_at as "updatedAt" + + + org.default_perm_template_project as "project", + org.default_perm_template_view as "view" + + +