3 * Copyright (C) 2009-2017 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.server.permission.ws.template;
22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Table;
24 import com.google.common.collect.TreeBasedTable;
25 import java.util.List;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.organization.DefaultTemplates;
29 import org.sonar.db.permission.template.CountByTemplateAndPermissionDto;
30 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
31 import org.sonar.db.permission.template.PermissionTemplateDto;
32 import org.sonar.server.permission.ws.template.DefaultTemplatesResolver.ResolvedDefaultTemplates;
33 import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
35 import static org.sonar.server.permission.ws.template.SearchTemplatesData.builder;
36 import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
38 public class SearchTemplatesDataLoader {
39 private final DbClient dbClient;
40 private final DefaultTemplatesResolver defaultTemplatesResolver;
42 public SearchTemplatesDataLoader(DbClient dbClient, DefaultTemplatesResolver defaultTemplatesResolver) {
43 this.dbClient = dbClient;
44 this.defaultTemplatesResolver = defaultTemplatesResolver;
47 public SearchTemplatesData load(DbSession dbSession, SearchTemplatesWsRequest request) {
48 SearchTemplatesData.Builder data = builder();
49 List<PermissionTemplateDto> templates = searchTemplates(dbSession, request);
50 List<Long> templateIds = Lists.transform(templates, PermissionTemplateDto::getId);
52 DefaultTemplates defaultTemplates = checkFoundWithOptional(
53 dbClient.organizationDao().getDefaultTemplates(dbSession, request.getOrganizationUuid()),
54 "No Default templates for organization with uuid '%s'", request.getOrganizationUuid());
55 ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver.resolve(defaultTemplates);
57 data.templates(templates)
58 .defaultTemplates(resolvedDefaultTemplates)
59 .userCountByTemplateIdAndPermission(userCountByTemplateIdAndPermission(dbSession, templateIds))
60 .groupCountByTemplateIdAndPermission(groupCountByTemplateIdAndPermission(dbSession, templateIds))
61 .withProjectCreatorByTemplateIdAndPermission(withProjectCreatorsByTemplateIdAndPermission(dbSession, templateIds));
66 private List<PermissionTemplateDto> searchTemplates(DbSession dbSession, SearchTemplatesWsRequest request) {
67 return dbClient.permissionTemplateDao().selectAll(dbSession, request.getOrganizationUuid(), request.getQuery());
70 private Table<Long, String, Integer> userCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds) {
71 final Table<Long, String, Integer> userCountByTemplateIdAndPermission = TreeBasedTable.create();
73 dbClient.permissionTemplateDao().usersCountByTemplateIdAndPermission(dbSession, templateIds, context -> {
74 CountByTemplateAndPermissionDto row = context.getResultObject();
75 userCountByTemplateIdAndPermission.put(row.getTemplateId(), row.getPermission(), row.getCount());
78 return userCountByTemplateIdAndPermission;
81 private Table<Long, String, Integer> groupCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds) {
82 final Table<Long, String, Integer> userCountByTemplateIdAndPermission = TreeBasedTable.create();
84 dbClient.permissionTemplateDao().groupsCountByTemplateIdAndPermission(dbSession, templateIds, context -> {
85 CountByTemplateAndPermissionDto row = context.getResultObject();
86 userCountByTemplateIdAndPermission.put(row.getTemplateId(), row.getPermission(), row.getCount());
89 return userCountByTemplateIdAndPermission;
92 private Table<Long, String, Boolean> withProjectCreatorsByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds) {
93 final Table<Long, String, Boolean> templatePermissionsByTemplateIdAndPermission = TreeBasedTable.create();
95 List<PermissionTemplateCharacteristicDto> templatePermissions = dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, templateIds);
96 templatePermissions.stream()
97 .forEach(templatePermission -> templatePermissionsByTemplateIdAndPermission.put(templatePermission.getTemplateId(), templatePermission.getPermission(),
98 templatePermission.getWithProjectCreator()));
100 return templatePermissionsByTemplateIdAndPermission;