mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 09:46:17 +00:00
#159: route model api
This commit is contained in:
@@ -41,7 +41,7 @@ public class AuditLogResource extends BaseResource {
|
||||
* @apiSuccess {String} logs.id ID
|
||||
* @apiSuccess {String} logs.username Username
|
||||
* @apiSuccess {String} logs.target Entity ID
|
||||
* @apiSuccess {String="Acl","Comment","Document","File","Group","Tag","User"} logs.class Entity type
|
||||
* @apiSuccess {String="Acl","Comment","Document","File","Group","Tag","User","RouteModel"} logs.class Entity type
|
||||
* @apiSuccess {String="CREATE","UPDATE","DELETE"} logs.type Type
|
||||
* @apiSuccess {String} logs.message Message
|
||||
* @apiSuccess {Number} logs.create_date Create date (timestamp)
|
||||
|
||||
@@ -3,15 +3,16 @@ package com.sismics.docs.rest.resource;
|
||||
import com.sismics.docs.core.dao.jpa.RouteModelDao;
|
||||
import com.sismics.docs.core.dao.jpa.criteria.RouteModelCriteria;
|
||||
import com.sismics.docs.core.dao.jpa.dto.RouteModelDto;
|
||||
import com.sismics.docs.core.model.jpa.RouteModel;
|
||||
import com.sismics.docs.core.util.jpa.SortCriteria;
|
||||
import com.sismics.docs.rest.constant.BaseFunction;
|
||||
import com.sismics.rest.exception.ForbiddenClientException;
|
||||
import com.sismics.rest.util.ValidationUtil;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonArrayBuilder;
|
||||
import javax.json.JsonObjectBuilder;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.List;
|
||||
|
||||
@@ -27,7 +28,7 @@ public class RouteModelResource extends BaseResource {
|
||||
*
|
||||
* @api {get} /routemodel Get route models
|
||||
* @apiName GetRouteModel
|
||||
* @apiGroup RouteModel
|
||||
* @apiRouteModel RouteModel
|
||||
* @apiParam {Number} sort_column Column index to sort on
|
||||
* @apiParam {Boolean} asc If true, sort in ascending order
|
||||
* @apiSuccess {Object[]} routemodels List of route models
|
||||
@@ -48,20 +49,190 @@ public class RouteModelResource extends BaseResource {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
|
||||
JsonArrayBuilder groups = Json.createArrayBuilder();
|
||||
JsonArrayBuilder routeModels = Json.createArrayBuilder();
|
||||
SortCriteria sortCriteria = new SortCriteria(sortColumn, asc);
|
||||
|
||||
RouteModelDao routeModelDao = new RouteModelDao();
|
||||
List<RouteModelDto> routeModelDtoList = routeModelDao.findByCriteria(new RouteModelCriteria(), sortCriteria);
|
||||
for (RouteModelDto routeModelDto : routeModelDtoList) {
|
||||
groups.add(Json.createObjectBuilder()
|
||||
routeModels.add(Json.createObjectBuilder()
|
||||
.add("id", routeModelDto.getId())
|
||||
.add("name", routeModelDto.getName())
|
||||
.add("create_date", routeModelDto.getCreateTimestamp()));
|
||||
}
|
||||
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("routemodels", groups);
|
||||
.add("routemodels", routeModels);
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a route model.
|
||||
*
|
||||
* @api {put} /routemodel Add a route model
|
||||
* @apiName PutRouteModel
|
||||
* @apiRouteModel RouteModel
|
||||
* @apiParam {String} name Route model name
|
||||
* @apiParam {String} steps Steps data in JSON
|
||||
* @apiSuccess {String} id Route model ID
|
||||
* @apiError (client) ForbiddenError Access denied
|
||||
* @apiError (client) ValidationError Validation error
|
||||
* @apiPermission admin
|
||||
* @apiVersion 1.5.0
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
@PUT
|
||||
public Response add(@FormParam("name") String name, @FormParam("steps") String steps) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
checkBaseFunction(BaseFunction.ADMIN);
|
||||
|
||||
// Validate input
|
||||
name = ValidationUtil.validateLength(name, "name", 1, 50, false);
|
||||
// TODO Validate steps data
|
||||
|
||||
// Create the route model
|
||||
RouteModelDao routeModelDao = new RouteModelDao();
|
||||
String id = routeModelDao.create(new RouteModel()
|
||||
.setName(name)
|
||||
.setSteps(steps), principal.getId());
|
||||
|
||||
// Always return OK
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("id", id);
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a route model.
|
||||
*
|
||||
* @api {post} /routemodel/:id Update a route model
|
||||
* @apiName PostRouteModel
|
||||
* @apiRouteModel RouteModel
|
||||
* @apiParam {String} name Route model name
|
||||
* @apiParam {String} steps Steps data in JSON
|
||||
* @apiSuccess {String} status Status OK
|
||||
* @apiError (client) ForbiddenError Access denied
|
||||
* @apiError (client) ValidationError Validation error
|
||||
* @apiError (client) NotFound Route model not found
|
||||
* @apiPermission admin
|
||||
* @apiVersion 1.5.0
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
@POST
|
||||
@Path("{id: [a-z0-9\\-]+}")
|
||||
public Response update(@PathParam("id") String id,
|
||||
@FormParam("name") String name,
|
||||
@FormParam("steps") String steps) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
checkBaseFunction(BaseFunction.ADMIN);
|
||||
|
||||
// Validate input
|
||||
name = ValidationUtil.validateLength(name, "name", 1, 50, false);
|
||||
// TODO Validate steps data
|
||||
|
||||
// Get the route model
|
||||
RouteModelDao routeModelDao = new RouteModelDao();
|
||||
RouteModel routeModel = routeModelDao.getActiveById(id);
|
||||
if (routeModel == null) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
// Update the route model
|
||||
routeModelDao.update(routeModel.setName(name)
|
||||
.setSteps(steps), principal.getId());
|
||||
|
||||
// Always return OK
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("status", "ok");
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a route model.
|
||||
*
|
||||
* @api {delete} /routemodel/:id Delete a route model
|
||||
* @apiName DeleteRouteModel
|
||||
* @apiRouteModel RouteModel
|
||||
* @apiParam {String} id Route model ID
|
||||
* @apiSuccess {String} status Status OK
|
||||
* @apiError (client) ForbiddenError Access denied
|
||||
* @apiError (client) NotFound Route model not found
|
||||
* @apiPermission admin
|
||||
* @apiVersion 1.5.0
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
@DELETE
|
||||
@Path("{id: [a-z0-9\\-]+}")
|
||||
public Response delete(@PathParam("id") String id) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
checkBaseFunction(BaseFunction.ADMIN);
|
||||
|
||||
// Get the route model
|
||||
RouteModelDao routeModelDao = new RouteModelDao();
|
||||
RouteModel routeModel = routeModelDao.getActiveById(id);
|
||||
if (routeModel == null) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
// Delete the route model
|
||||
routeModelDao.delete(routeModel.getId(), principal.getId());
|
||||
|
||||
// Always return OK
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("status", "ok");
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a route model.
|
||||
*
|
||||
* @api {get} /routemodel/:id Get a route model
|
||||
* @apiName GetRouteModel
|
||||
* @apiRouteModel RouteModel
|
||||
* @apiParam {String} id Route model ID
|
||||
* @apiSuccess {String} id Route model ID
|
||||
* @apiSuccess {String} name Route model name
|
||||
* @apiSuccess {String} create_date Create date (timestamp)
|
||||
* @apiSuccess {String} steps Steps data in JSON
|
||||
* @apiError (client) ForbiddenError Access denied
|
||||
* @apiError (client) NotFound Route model not found
|
||||
* @apiPermission admin
|
||||
* @apiVersion 1.5.0
|
||||
*
|
||||
* @param id RouteModel name
|
||||
* @return Response
|
||||
*/
|
||||
@GET
|
||||
@Path("{id: [a-z0-9\\-]+}")
|
||||
public Response get(@PathParam("id") String id) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
checkBaseFunction(BaseFunction.ADMIN);
|
||||
|
||||
// Get the route model
|
||||
RouteModelDao routeModelDao = new RouteModelDao();
|
||||
RouteModel routeModel = routeModelDao.getActiveById(id);
|
||||
if (routeModel == null) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
// Build the response
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("id", routeModel.getId())
|
||||
.add("name", routeModel.getName())
|
||||
.add("create_date", routeModel.getCreateDate().getTime())
|
||||
.add("steps", routeModel.getSteps());
|
||||
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ angular.module('docs').controller('SettingsWorkflow', function($scope, $state, R
|
||||
sort_column: 1,
|
||||
asc: true
|
||||
}).then(function(data) {
|
||||
$scope.workflows = data.workflows;
|
||||
$scope.workflows = data.routemodels;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
<a href="#/group/{{ log.message }}">{{ log.message }}</a>
|
||||
</span>
|
||||
<span ng-switch-when="RouteModel">
|
||||
<a href="#/settings/workflow/eidt/{{ log.target }}">{{ log.message }}</a>
|
||||
<a href="#/settings/workflow/edit/{{ log.target }}">{{ log.message }}</a>
|
||||
</span>
|
||||
</span>
|
||||
</td>
|
||||
|
||||
@@ -6,6 +6,8 @@ import org.junit.Test;
|
||||
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonObject;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.core.Form;
|
||||
|
||||
|
||||
/**
|
||||
@@ -29,7 +31,68 @@ public class TestRouteModelResource extends BaseJerseyTest {
|
||||
.request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.get(JsonObject.class);
|
||||
JsonArray groups = json.getJsonArray("routemodels");
|
||||
Assert.assertEquals(0, groups.size());
|
||||
JsonArray routeModels = json.getJsonArray("routemodels");
|
||||
Assert.assertEquals(0, routeModels.size());
|
||||
|
||||
// Create a route model
|
||||
json = target().path("/routemodel").request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.put(Entity.form(new Form()
|
||||
.param("name", "Workflow validation 1")
|
||||
.param("steps", "[]")), JsonObject.class);
|
||||
String routeModelId = json.getString("id");
|
||||
|
||||
// Get all route models
|
||||
json = target().path("/routemodel")
|
||||
.queryParam("sort_column", "1")
|
||||
.queryParam("asc", "true")
|
||||
.request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.get(JsonObject.class);
|
||||
routeModels = json.getJsonArray("routemodels");
|
||||
Assert.assertEquals(1, routeModels.size());
|
||||
Assert.assertEquals(routeModelId, routeModels.getJsonObject(0).getString("id"));
|
||||
Assert.assertEquals("Workflow validation 1", routeModels.getJsonObject(0).getString("name"));
|
||||
|
||||
// Get the route model
|
||||
json = target().path("/routemodel/" + routeModelId)
|
||||
.request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.get(JsonObject.class);
|
||||
Assert.assertEquals(routeModelId, json.getString("id"));
|
||||
Assert.assertEquals("Workflow validation 1", json.getString("name"));
|
||||
Assert.assertEquals("[]", json.getString("steps"));
|
||||
|
||||
// Update the route model
|
||||
json = target().path("/routemodel/" + routeModelId).request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.post(Entity.form(new Form()
|
||||
.param("name", "Workflow validation 2")
|
||||
.param("steps", "[{}]")), JsonObject.class);
|
||||
|
||||
// Get the route model
|
||||
json = target().path("/routemodel/" + routeModelId)
|
||||
.request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.get(JsonObject.class);
|
||||
Assert.assertEquals(routeModelId, json.getString("id"));
|
||||
Assert.assertEquals("Workflow validation 2", json.getString("name"));
|
||||
Assert.assertEquals("[{}]", json.getString("steps"));
|
||||
|
||||
// Delete the route model
|
||||
target().path("/routemodel/" + routeModelId)
|
||||
.request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.delete(JsonObject.class);
|
||||
|
||||
// Get all route models
|
||||
json = target().path("/routemodel")
|
||||
.queryParam("sort_column", "1")
|
||||
.queryParam("asc", "true")
|
||||
.request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.get(JsonObject.class);
|
||||
routeModels = json.getJsonArray("routemodels");
|
||||
Assert.assertEquals(0, routeModels.size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user