]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6947 api/permissions/delete_template use DeleteTemplateWsRequest
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 12 Nov 2015 15:23:37 +0000 (16:23 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 17 Nov 2015 12:41:01 +0000 (13:41 +0100)
server/sonar-server/src/main/java/org/sonar/server/permission/ws/DeleteTemplateAction.java
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/DeleteTemplateWsRequest.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java

index 2a2b008b5bd0b4477cf62807f4f30ca172239a0a..38ec85e049e7a84f1a2e5a4892e1f86cf0f758fe 100644 (file)
@@ -29,11 +29,14 @@ import org.sonar.db.DbSession;
 import org.sonar.db.permission.PermissionTemplateDto;
 import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder;
 import org.sonar.server.user.UserSession;
+import org.sonarqube.ws.client.permission.DeleteTemplateWsRequest;
 
 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
-import static org.sonar.server.permission.ws.WsTemplateRef.fromRequest;
+import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
 import static org.sonar.server.ws.WsUtils.checkRequest;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
 
 public class DeleteTemplateAction implements PermissionsWsAction {
   private final DbClient dbClient;
@@ -61,20 +64,28 @@ public class DeleteTemplateAction implements PermissionsWsAction {
   }
 
   @Override
-  public void handle(Request wsRequest, Response wsResponse) throws Exception {
+  public void handle(Request request, Response response) throws Exception {
     checkGlobalAdminUser(userSession);
+    doHandle(toDeleteTemplateWsRequest(request));
+    response.noContent();
+  }
 
+  private void doHandle(DeleteTemplateWsRequest request) {
     DbSession dbSession = dbClient.openSession(false);
     try {
-      PermissionTemplateDto template = finder.getTemplate(dbSession, fromRequest(wsRequest));
+      PermissionTemplateDto template = finder.getTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
       checkTemplateUuidIsNotDefault(template.getUuid());
       dbClient.permissionTemplateDao().deleteById(dbSession, template.getId());
       dbSession.commit();
     } finally {
       dbClient.closeSession(dbSession);
     }
+  }
 
-    wsResponse.noContent();
+  private static DeleteTemplateWsRequest toDeleteTemplateWsRequest(Request request) {
+    return new DeleteTemplateWsRequest()
+      .setTemplateId(request.param(PARAM_TEMPLATE_ID))
+      .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
   }
 
   private void checkTemplateUuidIsNotDefault(String key) {
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/DeleteTemplateWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/DeleteTemplateWsRequest.java
new file mode 100644 (file)
index 0000000..7a7ff94
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonarqube.ws.client.permission;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+public class DeleteTemplateWsRequest {
+  private String templateId;
+  private String templateName;
+
+  @CheckForNull
+  public String getTemplateId() {
+    return templateId;
+  }
+
+  public DeleteTemplateWsRequest setTemplateId(@Nullable String templateId) {
+    this.templateId = templateId;
+    return this;
+  }
+
+  @CheckForNull
+  public String getTemplateName() {
+    return templateName;
+  }
+
+  public DeleteTemplateWsRequest setTemplateName(@Nullable String templateName) {
+    this.templateName = templateName;
+    return this;
+  }
+}
index 8f48106fe8e6e82449dfd9cade4997cae4b32008..6eb702f03a4a4378758093086d742639b1298776 100644 (file)
@@ -108,6 +108,12 @@ public class PermissionsWsClient {
       CreateTemplateWsResponse.parser());
   }
 
+  public void deleteTemplate(DeleteTemplateWsRequest request) {
+    wsClient.execute(newPostRequest(action("delete_template"))
+      .setParam(PARAM_TEMPLATE_ID, request.getTemplateId())
+      .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName()));
+  }
+
   private static String action(String action) {
     return PermissionsWsParameters.ENDPOINT + "/" + action;
   }