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