package de.unikoblenz.fgbks.api; import static javax.ws.rs.core.Response.Status.BAD_REQUEST; import de.unikoblenz.fgbks.core.dmn.verification.DmnVerificationService; import de.unikoblenz.fgbks.core.dmn.verification.metrics.DmnVerificationMetricsService; import de.unikoblenz.fgbks.core.dmn.verification.result.VerifierResultSet; import de.unikoblenz.fgbks.core.dmn.verification.verifier.classification.ClassificationType; import de.unikoblenz.fgbks.core.dmn.verification.verifier.types.VerificationType; import javax.inject.Inject; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; /** * API for the dmn verification service. Provides methods for generating verifications for dmn * tables. */ @Path("/api/dmn/verification") public class Verification { @Inject protected DmnVerificationService dmnVerificationService; @Inject protected DmnVerificationMetricsService dmnVerificationMetricsService; /** * Method to generate all verifications for a dmn with all registered verifiers. * * @param payload the dmn as XML format as * @return a JSON String, which represents a {@link VerifierResultSet} */ @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_XML) public Response verifyAll(String payload) { return checkResult(dmnVerificationService.generate(payload)); } /** * Method to get all registered verification types. * * @return a list of {@link VerificationType} as a JSON String. */ @GET @Path("/type") @Produces(MediaType.APPLICATION_JSON) public Response verifyType() { return Response.accepted(dmnVerificationService.getVerificationTypes()).build(); } /** * Method to generate all verifications for a dmn with the given name of a {@link * VerificationType}. * * @param typeName the name of a {@link VerificationType} * @param payload the dmn as XML format as * @return a JSON String, which represents a {@link VerifierResultSet} */ @POST @Path("/type/{typeName}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_XML) public Response verifyType(@PathParam("typeName") String typeName, String payload) { return checkResult(dmnVerificationService.generateFromType(typeName, payload)); } /** * Method to get all registered classification types. * * @return a list of {@link ClassificationType} as a JSON String. */ @GET @Path("/classification") @Produces(MediaType.APPLICATION_JSON) public Response verifyClassification() { return Response.accepted(dmnVerificationService.getVerificationClassificationTypes()).build(); } /** * Method to generate all verifications for a dmn with the given name of a {@link * ClassificationType}. * * @param classificationName the name of a {@link ClassificationType} * @param payload the dmn as XML format as * @return a JSON String, which represents a {@link VerifierResultSet} */ @POST @Path("/classification/{classificationName}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_XML) public Response verifyClassification( @PathParam("classificationName") String classificationName, String payload) { return checkResult( dmnVerificationService.generateFromClassification(classificationName, payload)); } /** * Method to get all registered classification types. * * @return a list of {@link ClassificationType} as a JSON String. */ @GET @Path("/metrics") @Produces(MediaType.APPLICATION_JSON) public Response metrics() { return Response.accepted(dmnVerificationMetricsService.getMetrics()).build(); } private Response checkResult(VerifierResultSet resultSet) { if (resultSet.getAmountOfVerifier() == 0) { return Response.status( BAD_REQUEST.getStatusCode(), "The dmn syntax is not correct. The dmn can not be parsed.") .build(); } else { return Response.accepted(resultSet).build(); } } }