docs/docs-web/src/main/java/com/sismics/docs/rest/resource/RouteModelResource.java

239 lines
8.1 KiB
Java
Raw Normal View History

2018-01-26 11:26:34 +01:00
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;
2018-01-28 12:44:11 +01:00
import com.sismics.docs.core.model.jpa.RouteModel;
2018-01-26 11:26:34 +01:00
import com.sismics.docs.core.util.jpa.SortCriteria;
2018-01-28 12:44:11 +01:00
import com.sismics.docs.rest.constant.BaseFunction;
2018-01-26 11:26:34 +01:00
import com.sismics.rest.exception.ForbiddenClientException;
2018-01-28 12:44:11 +01:00
import com.sismics.rest.util.ValidationUtil;
2018-01-26 11:26:34 +01:00
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
2018-01-28 12:44:11 +01:00
import javax.ws.rs.*;
2018-01-26 11:26:34 +01:00
import javax.ws.rs.core.Response;
import java.util.List;
/**
* Route model REST resources.
*
* @author bgamard
*/
@Path("/routemodel")
public class RouteModelResource extends BaseResource {
/**
* Returns the list of all route models.
*
* @api {get} /routemodel Get route models
* @apiName GetRouteModel
2018-01-28 12:44:11 +01:00
* @apiRouteModel RouteModel
2018-01-26 11:26:34 +01:00
* @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
* @apiSuccess {String} routemodels.id ID
* @apiSuccess {String} routemodels.name Name
* @apiSuccess {Number} routemodels.create_date Create date (timestamp)
* @apiError (client) ForbiddenError Access denied
* @apiPermission user
* @apiVersion 1.5.0
*
* @return Response
*/
@GET
public Response list(
@QueryParam("sort_column") Integer sortColumn,
@QueryParam("asc") Boolean asc) {
if (!authenticate()) {
throw new ForbiddenClientException();
}
2018-01-28 12:44:11 +01:00
JsonArrayBuilder routeModels = Json.createArrayBuilder();
2018-01-26 11:26:34 +01:00
SortCriteria sortCriteria = new SortCriteria(sortColumn, asc);
RouteModelDao routeModelDao = new RouteModelDao();
List<RouteModelDto> routeModelDtoList = routeModelDao.findByCriteria(new RouteModelCriteria(), sortCriteria);
for (RouteModelDto routeModelDto : routeModelDtoList) {
2018-01-28 12:44:11 +01:00
routeModels.add(Json.createObjectBuilder()
2018-01-26 11:26:34 +01:00
.add("id", routeModelDto.getId())
.add("name", routeModelDto.getName())
.add("create_date", routeModelDto.getCreateTimestamp()));
}
JsonObjectBuilder response = Json.createObjectBuilder()
2018-01-28 12:44:11 +01:00
.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());
2018-01-26 11:26:34 +01:00
return Response.ok().entity(response.build()).build();
}
}