3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.permission.template.CountByTemplateAndPermissionDto;
29 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
30 import org.sonar.db.permission.template.PermissionTemplateDto;
31 import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder.TemplateUuidQualifier;
32 import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
34 import static org.sonar.server.permission.ws.template.SearchTemplatesData.builder;
36 public class SearchTemplatesDataLoader {
37 private final DbClient dbClient;
38 private final DefaultPermissionTemplateFinder defaultPermissionTemplateFinder;
40 public SearchTemplatesDataLoader(DbClient dbClient, DefaultPermissionTemplateFinder defaultPermissionTemplateFinder) {
41 this.dbClient = dbClient;
42 this.defaultPermissionTemplateFinder = defaultPermissionTemplateFinder;
45 public SearchTemplatesData load(SearchTemplatesWsRequest request) {
46 try (DbSession dbSession = dbClient.openSession(false)) {
47 SearchTemplatesData.Builder data = builder();
48 List<PermissionTemplateDto> templates = searchTemplates(dbSession, request);
49 List<Long> templateIds = Lists.transform(templates, PermissionTemplateDto::getId);
50 List<TemplateUuidQualifier> defaultTemplates = defaultPermissionTemplateFinder.getDefaultTemplatesByQualifier();
52 data.templates(templates)
53 .defaultTemplates(defaultTemplates)
54 .userCountByTemplateIdAndPermission(userCountByTemplateIdAndPermission(dbSession, templateIds))
55 .groupCountByTemplateIdAndPermission(groupCountByTemplateIdAndPermission(dbSession, templateIds))
56 .withProjectCreatorByTemplateIdAndPermission(withProjectCreatorsByTemplateIdAndPermission(dbSession, templateIds));
62 private List<PermissionTemplateDto> searchTemplates(DbSession dbSession, SearchTemplatesWsRequest request) {
63 String nameMatch = request.getQuery();
65 return nameMatch == null ? dbClient.permissionTemplateDao().selectAll(dbSession)
66 : dbClient.permissionTemplateDao().selectAll(dbSession, nameMatch);
69 private Table<Long, String, Integer> userCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds) {
70 final Table<Long, String, Integer> userCountByTemplateIdAndPermission = TreeBasedTable.create();
72 dbClient.permissionTemplateDao().usersCountByTemplateIdAndPermission(dbSession, templateIds, context -> {
73 CountByTemplateAndPermissionDto row = (CountByTemplateAndPermissionDto) context.getResultObject();
74 userCountByTemplateIdAndPermission.put(row.getTemplateId(), row.getPermission(), row.getCount());
77 return userCountByTemplateIdAndPermission;
80 private Table<Long, String, Integer> groupCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds) {
81 final Table<Long, String, Integer> userCountByTemplateIdAndPermission = TreeBasedTable.create();
83 dbClient.permissionTemplateDao().groupsCountByTemplateIdAndPermission(dbSession, templateIds, context -> {
84 CountByTemplateAndPermissionDto row = (CountByTemplateAndPermissionDto) context.getResultObject();
85 userCountByTemplateIdAndPermission.put(row.getTemplateId(), row.getPermission(), row.getCount());
88 return userCountByTemplateIdAndPermission;
91 private Table<Long, String, Boolean> withProjectCreatorsByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds) {
92 final Table<Long, String, Boolean> templatePermissionsByTemplateIdAndPermission = TreeBasedTable.create();
94 List<PermissionTemplateCharacteristicDto> templatePermissions = dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, templateIds);
95 templatePermissions.stream()
96 .forEach(templatePermission -> templatePermissionsByTemplateIdAndPermission.put(templatePermission.getTemplateId(), templatePermission.getPermission(),
97 templatePermission.getWithProjectCreator()));
99 return templatePermissionsByTemplateIdAndPermission;