]> source.dussan.org Git - sonarqube.git/blob
bbdca2994dd0a5bc66412cb34636991505f8a7ca
[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.server.ws.Request;
36 import org.sonar.api.utils.Paging;
37 import org.sonar.db.DbClient;
38 import org.sonar.db.DbSession;
39 import org.sonar.db.component.ComponentDto;
40 import org.sonar.db.permission.CountByProjectAndPermissionDto;
41
42 import static java.util.Collections.singletonList;
43 import static org.sonar.api.server.ws.WebService.Param.PAGE;
44 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
45 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
46 import static org.sonar.api.utils.Paging.forPageIndex;
47 import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
48 import static org.sonar.server.permission.ws.SearchProjectPermissionsData.newBuilder;
49
50 public class SearchProjectPermissionsDataLoader {
51   private final DbClient dbClient;
52   private final PermissionDependenciesFinder finder;
53   private final Collection<String> rootQualifiers;
54
55   public SearchProjectPermissionsDataLoader(DbClient dbClient, PermissionDependenciesFinder finder, ResourceTypes resourceTypes) {
56     this.dbClient = dbClient;
57     this.finder = finder;
58     this.rootQualifiers = Collections2.transform(resourceTypes.getRoots(), RESOURCE_TYPE_TO_QUALIFIER);
59   }
60
61   SearchProjectPermissionsData load(Request wsRequest) {
62     DbSession dbSession = dbClient.openSession(false);
63     try {
64       SearchProjectPermissionsData.Builder data = newBuilder();
65       int countRootComponents = countRootComponents(dbSession, rootQualifiers, wsRequest);
66       List<ComponentDto> rootComponents = searchRootComponents(dbSession, wsRequest, paging(wsRequest, countRootComponents));
67       List<Long> rootComponentIds = Lists.transform(rootComponents, ComponentToIdFunction.INSTANCE);
68
69       data.rootComponents(rootComponents)
70         .paging(paging(wsRequest, countRootComponents))
71         .userCountByProjectIdAndPermission(userCountByRootComponentIdAndPermission(dbSession, rootComponentIds))
72         .groupCountByProjectIdAndPermission(groupCountByRootComponentIdAndPermission(dbSession, rootComponentIds));
73
74       return data.build();
75     } finally {
76       dbClient.closeSession(dbSession);
77     }
78   }
79
80   private static Paging paging(Request wsRequest, int total) {
81     return forPageIndex(wsRequest.mandatoryParamAsInt(PAGE))
82       .withPageSize(wsRequest.mandatoryParamAsInt(PAGE_SIZE))
83       .andTotal(total);
84   }
85
86   private int countRootComponents(DbSession dbSession, Collection<String> qualifiers, Request wsRequest) {
87     return dbClient.componentDao().countRootComponents(dbSession, qualifiers, wsRequest.param(TEXT_QUERY));
88   }
89
90   private List<ComponentDto> searchRootComponents(DbSession dbSession, Request wsRequest, Paging paging) {
91     String query = wsRequest.param(TEXT_QUERY);
92     Optional<WsProjectRef> project = WsProjectRef.optionalFromRequest(wsRequest);
93
94     if (project.isPresent()) {
95       return singletonList(finder.getProject(dbSession, project.get()));
96     }
97
98     return dbClient.componentDao().selectComponents(dbSession, rootQualifiers, paging.offset(), paging.pageSize(), query);
99   }
100
101   private Table<Long, String, Integer> userCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) {
102     final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create();
103
104     dbClient.permissionDao().usersCountByComponentIdAndPermission(dbSession, rootComponentIds, new ResultHandler() {
105       @Override
106       public void handleResult(ResultContext context) {
107         CountByProjectAndPermissionDto row = (CountByProjectAndPermissionDto) context.getResultObject();
108         userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount());
109       }
110     });
111
112     return userCountByRootComponentIdAndPermission;
113   }
114
115   private Table<Long, String, Integer> groupCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) {
116     final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create();
117
118     dbClient.permissionDao().groupsCountByComponentIdAndPermission(dbSession, rootComponentIds, new ResultHandler() {
119       @Override
120       public void handleResult(ResultContext context) {
121         CountByProjectAndPermissionDto row = (CountByProjectAndPermissionDto) context.getResultObject();
122         userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount());
123       }
124     });
125
126     return userCountByRootComponentIdAndPermission;
127   }
128
129   private enum ComponentToIdFunction implements Function<ComponentDto, Long> {
130     INSTANCE;
131
132     @Override
133     public Long apply(@Nonnull ComponentDto component) {
134       return component.getId();
135     }
136   }
137 }