]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6485 WS permissions/search_project_permissions add paging field in response
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 19 Aug 2015 13:10:38 +0000 (15:10 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 19 Aug 2015 13:10:47 +0000 (15:10 +0200)
server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsData.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java
server/sonar-server/src/main/resources/org/sonar/server/permission/ws/search_project_permissions-example.json
server/sonar-server/src/test/resources/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest/empty.json
sonar-db/src/main/java/org/sonar/db/permission/PermissionDao.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/Paging.java

index 3ae75eb7e1e50e94ce3b2cee6fb7dd629d45e8b7..d55be8594f3dbe091e373bcd4bcc3975382daf55 100644 (file)
@@ -24,6 +24,7 @@ import org.sonar.api.i18n.I18n;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.Paging;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.permission.ComponentPermissions;
 import org.sonar.core.permission.GlobalPermissions;
@@ -31,6 +32,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.user.UserSession;
+import org.sonarqube.ws.Common;
 import org.sonarqube.ws.Permissions.Permission;
 import org.sonarqube.ws.Permissions.SearchProjectPermissionsResponse;
 import org.sonarqube.ws.Permissions.SearchProjectPermissionsResponse.Project;
@@ -147,6 +149,14 @@ public class SearchProjectPermissionsAction implements PermissionsWsAction {
         );
     }
 
+    Paging paging = data.paging();
+    response.setPaging(
+      Common.Paging.newBuilder()
+        .setPageIndex(paging.pageIndex())
+        .setPageSize(paging.pageSize())
+        .setTotal(paging.total())
+      );
+
     return response.build();
   }
 
index 951428cf2a782101dd6246e4b68874d64679aeba..fd3308b3db7d5a77c0a0db2ea05827aac71bf9ee 100644 (file)
@@ -26,6 +26,7 @@ import com.google.common.collect.Ordering;
 import com.google.common.collect.Table;
 import java.util.List;
 import java.util.Set;
+import org.sonar.api.utils.Paging;
 import org.sonar.db.component.ComponentDto;
 
 import static com.google.common.base.Objects.firstNonNull;
@@ -35,13 +36,13 @@ import static com.google.common.collect.ImmutableTable.copyOf;
 
 public class SearchProjectPermissionsData {
   private final List<ComponentDto> rootComponents;
-  private final int total;
+  private final Paging paging;
   private final Table<Long, String, Integer> userCountByProjectIdAndPermission;
   private final Table<Long, String, Integer> groupCountByProjectIdAndPermission;
 
   private SearchProjectPermissionsData(Builder builder) {
     this.rootComponents = copyOf(builder.projects);
-    this.total = builder.total;
+    this.paging = builder.paging;
     this.userCountByProjectIdAndPermission = copyOf(builder.userCountByProjectIdAndPermission);
     this.groupCountByProjectIdAndPermission = copyOf(builder.groupCountByProjectIdAndPermission);
   }
@@ -54,8 +55,8 @@ public class SearchProjectPermissionsData {
     return rootComponents;
   }
 
-  public int total() {
-    return total;
+  public Paging paging() {
+    return paging;
   }
 
   public int userCount(long rootComponentId, String permission) {
@@ -77,7 +78,7 @@ public class SearchProjectPermissionsData {
 
   public static class Builder {
     private List<ComponentDto> projects;
-    private int total;
+    private Paging paging;
     private Table<Long, String, Integer> userCountByProjectIdAndPermission;
     private Table<Long, String, Integer> groupCountByProjectIdAndPermission;
 
@@ -98,8 +99,8 @@ public class SearchProjectPermissionsData {
       return this;
     }
 
-    public Builder total(int total) {
-      this.total = total;
+    public Builder paging(Paging paging) {
+      this.paging = paging;
       return this;
     }
 
index 1337c93fe86c36a0978f4da9f7343baaef52c232..8a787adf15b62cac7eea33f95763685d4a2b91bd 100644 (file)
@@ -69,7 +69,7 @@ public class SearchProjectPermissionsDataLoader {
       List<Long> rootComponentIds = Lists.transform(rootComponents, ComponentToIdFunction.INSTANCE);
 
       data.rootComponents(rootComponents)
-        .total(countRootComponents)
+        .paging(paging(wsRequest, countRootComponents))
         .userCountByProjectIdAndPermission(userCountByRootComponentIdAndPermission(dbSession, rootComponentIds))
         .groupCountByProjectIdAndPermission(groupCountByRootComponentIdAndPermission(dbSession, rootComponentIds));
 
@@ -79,7 +79,7 @@ public class SearchProjectPermissionsDataLoader {
     }
   }
 
-  private Paging paging(Request wsRequest, int total) {
+  private static Paging paging(Request wsRequest, int total) {
     return forPageIndex(wsRequest.mandatoryParamAsInt(PAGE))
       .withPageSize(wsRequest.mandatoryParamAsInt(PAGE_SIZE))
       .andTotal(total);
index 935f1521901d695d0f1f0b0e84dff394569be22f..c51b259452159f18c78b907046ce2f6eba6ed44f 100644 (file)
       "name": "See Source Code",
       "description": "Ability to view the project\u0027s source code. (Users will also need \"Browse\" permission)"
     }
-  ]
+  ],
+  "paging": {
+    "pageIndex": 1,
+    "pageSize": 25,
+    "total": 4
+  }
 }
index 1b677af4e960792391621cbb52de882b2be00924..5789d2975ede32cc5148fc834ac9771b78ac80c6 100644 (file)
       "name": "See Source Code",
       "description": "Ability to view the project's source code. (Users will also need \"Browse\" permission)"
     }
-  ]
+  ],
+  "paging": {
+    "pageIndex": 1,
+    "pageSize": 25,
+    "total": 0
+  }
 }
index 48d4c327736a34021bdcdd901c756e8d4990e2fa..773c28a44e0318641f9617762bbc32d1180f1dcc 100644 (file)
@@ -41,6 +41,7 @@ public class PermissionDao implements Dao {
 
   private static final String QUERY_PARAMETER = "query";
   private static final String COMPONENT_ID_PARAMETER = "componentId";
+  private static final String ANYONE_GROUP_PARAMETER = "anyoneGroup";
 
   private final MyBatis myBatis;
 
@@ -93,7 +94,7 @@ public class PermissionDao implements Dao {
   public int countGroups(DbSession session, String permission, @Nullable Long componentId) {
     Map<String, Object> parameters = new HashMap<>();
     parameters.put("permission", permission);
-    parameters.put("anyoneGroup", DefaultGroups.ANYONE);
+    parameters.put(ANYONE_GROUP_PARAMETER, DefaultGroups.ANYONE);
     parameters.put(COMPONENT_ID_PARAMETER, componentId);
 
     return mapper(session).countGroups(parameters);
@@ -120,7 +121,7 @@ public class PermissionDao implements Dao {
    */
   public void groupsCountByComponentIdAndPermission(final DbSession dbSession, final List<Long> componentIds, final ResultHandler resultHandler) {
     final Map<String, Object> parameters = new HashMap<>();
-    parameters.put("anyoneGroup", DefaultGroups.ANYONE);
+    parameters.put(ANYONE_GROUP_PARAMETER, DefaultGroups.ANYONE);
 
     DatabaseUtils.executeLargeInputsWithoutOutput(componentIds, new Function<List<Long>, Void>() {
       @Override
@@ -136,7 +137,7 @@ public class PermissionDao implements Dao {
     Map<String, Object> params = newHashMap();
     params.put(QUERY_PARAMETER, query);
     params.put(COMPONENT_ID_PARAMETER, componentId);
-    params.put("anyoneGroup", DefaultGroups.ANYONE);
+    params.put(ANYONE_GROUP_PARAMETER, DefaultGroups.ANYONE);
     return params;
   }
 
index 1945e1cc402215c70b0593325a354820b23fc874..d9439514a3c458b2231448569074fe62ae7008de 100644 (file)
@@ -45,10 +45,10 @@ public class Paging {
     this.total = total;
   }
 
-  @Deprecated
   /**
    * @deprecated since 5.2 please use the forPageIndex(...) builder method
    */
+  @Deprecated
   public static Paging create(int pageSize, int pageIndex, int totalItems) {
     return new Paging(pageSize, pageIndex, totalItems);
   }