]> source.dussan.org Git - sonarqube.git/blob
96d92b973788b1649d3b9053fc7e36bc5f5257bc
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.permission.ws;
21
22 import com.google.common.base.Function;
23 import com.google.common.base.Optional;
24 import com.google.common.collect.Collections2;
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Table;
27 import com.google.common.collect.TreeBasedTable;
28 import java.util.Collection;
29 import java.util.List;
30 import javax.annotation.Nonnull;
31 import javax.annotation.Nullable;
32 import org.apache.ibatis.session.ResultContext;
33 import org.apache.ibatis.session.ResultHandler;
34 import org.sonar.api.resources.ResourceTypes;
35 import org.sonar.api.utils.Paging;
36 import org.sonar.db.DbClient;
37 import org.sonar.db.DbSession;
38 import org.sonar.db.component.ComponentDto;
39 import org.sonar.db.permission.CountByProjectAndPermissionDto;
40 import org.sonarqube.ws.client.permission.SearchProjectPermissionsWsRequest;
41
42 import static java.util.Collections.singleton;
43 import static java.util.Collections.singletonList;
44 import static org.sonar.api.utils.Paging.forPageIndex;
45 import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
46 import static org.sonar.server.permission.ws.SearchProjectPermissionsData.newBuilder;
47 import static org.sonar.server.permission.ws.WsProjectRef.newOptionalWsProjectRef;
48
49 public class SearchProjectPermissionsDataLoader {
50   private final DbClient dbClient;
51   private final PermissionDependenciesFinder finder;
52   private final Collection<String> rootQualifiers;
53
54   public SearchProjectPermissionsDataLoader(DbClient dbClient, PermissionDependenciesFinder finder, ResourceTypes resourceTypes) {
55     this.dbClient = dbClient;
56     this.finder = finder;
57     this.rootQualifiers = Collections2.transform(resourceTypes.getRoots(), RESOURCE_TYPE_TO_QUALIFIER);
58   }
59
60   SearchProjectPermissionsData load(SearchProjectPermissionsWsRequest request) {
61     DbSession dbSession = dbClient.openSession(false);
62     try {
63       SearchProjectPermissionsData.Builder data = newBuilder();
64       int countRootComponents = countRootComponents(dbSession, qualifiers(request.getQualifier()), request);
65       List<ComponentDto> rootComponents = searchRootComponents(dbSession, request, paging(request, countRootComponents));
66       List<Long> rootComponentIds = Lists.transform(rootComponents, ComponentToIdFunction.INSTANCE);
67
68       data.rootComponents(rootComponents)
69         .paging(paging(request, countRootComponents))
70         .userCountByProjectIdAndPermission(userCountByRootComponentIdAndPermission(dbSession, rootComponentIds))
71         .groupCountByProjectIdAndPermission(groupCountByRootComponentIdAndPermission(dbSession, rootComponentIds));
72
73       return data.build();
74     } finally {
75       dbClient.closeSession(dbSession);
76     }
77   }
78
79   private static Paging paging(SearchProjectPermissionsWsRequest request, int total) {
80     return forPageIndex(request.getPage())
81       .withPageSize(request.getPageSize())
82       .andTotal(total);
83   }
84
85   private int countRootComponents(DbSession dbSession, Collection<String> qualifiers, SearchProjectPermissionsWsRequest request) {
86     return dbClient.componentDao().countRootComponents(dbSession, qualifiers, request.getQuery());
87   }
88
89   private List<ComponentDto> searchRootComponents(DbSession dbSession, SearchProjectPermissionsWsRequest request, Paging paging) {
90     String query = request.getQuery();
91     Optional<WsProjectRef> project = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
92
93     if (project.isPresent()) {
94       return singletonList(finder.getRootComponentOrModule(dbSession, project.get()));
95     }
96
97     return dbClient.componentDao().selectComponents(dbSession, qualifiers(request.getQualifier()), paging.offset(), paging.pageSize(), query);
98   }
99
100   private Collection<String> qualifiers(@Nullable String requestQualifier) {
101     return requestQualifier == null
102       ? rootQualifiers
103       : singleton(requestQualifier);
104   }
105
106   private Table<Long, String, Integer> userCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) {
107     final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create();
108
109     dbClient.permissionDao().usersCountByComponentIdAndPermission(dbSession, rootComponentIds, new ResultHandler() {
110       @Override
111       public void handleResult(ResultContext context) {
112         CountByProjectAndPermissionDto row = (CountByProjectAndPermissionDto) context.getResultObject();
113         userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount());
114       }
115     });
116
117     return userCountByRootComponentIdAndPermission;
118   }
119
120   private Table<Long, String, Integer> groupCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) {
121     final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create();
122
123     dbClient.permissionDao().groupsCountByComponentIdAndPermission(dbSession, rootComponentIds, new ResultHandler() {
124       @Override
125       public void handleResult(ResultContext context) {
126         CountByProjectAndPermissionDto row = (CountByProjectAndPermissionDto) context.getResultObject();
127         userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount());
128       }
129     });
130
131     return userCountByRootComponentIdAndPermission;
132   }
133
134   private enum ComponentToIdFunction implements Function<ComponentDto, Long> {
135     INSTANCE;
136
137     @Override
138     public Long apply(@Nonnull ComponentDto component) {
139       return component.getId();
140     }
141   }
142 }