Search in sources :

Example 1 with OperationExecutor

use of com.developmentontheedge.be5.api.services.OperationExecutor in project be5 by DevelopmentOnTheEdge.

the class Form method generate.

@Override
public void generate(Request req, Response res, Injector injector) {
    OperationExecutor operationExecutor = injector.get(OperationExecutor.class);
    DocumentGenerator documentGenerator = injector.get(DocumentGenerator.class);
    String entityName = req.getNonEmpty(RestApiConstants.ENTITY);
    String queryName = req.getNonEmpty(RestApiConstants.QUERY);
    String operationName = req.getNonEmpty(RestApiConstants.OPERATION);
    String[] selectedRows = ParseRequestUtils.selectedRows(nullToEmpty(req.get(RestApiConstants.SELECTED_ROWS)));
    Map<String, String> operationParams = req.getValuesFromJsonAsStrings(RestApiConstants.OPERATION_PARAMS);
    Map<String, Object> values = req.getValuesFromJson(RestApiConstants.VALUES);
    Operation operation;
    try {
        operation = operationExecutor.create(entityName, queryName, operationName, selectedRows, operationParams);
    } catch (Be5Exception e) {
        HashUrl url = new HashUrl(FORM_ACTION, entityName, queryName, operationName).named(operationParams);
        res.sendErrorAsJson(new ErrorModel(e, "", Collections.singletonMap(SELF_LINK, url.toString())), req.getDefaultMeta());
        return;
    }
    Either<FormPresentation, OperationResult> data;
    try {
        switch(req.getRequestUri()) {
            case "":
                data = documentGenerator.generateForm(operation, values);
                break;
            case "apply":
                data = documentGenerator.executeForm(operation, values);
                break;
            default:
                res.sendUnknownActionError();
                return;
        }
    } catch (Be5Exception e) {
        res.sendErrorAsJson(documentGenerator.getErrorModel(e, operation.getUrl()), req.getDefaultMeta());
        return;
    }
    res.sendAsJson(new ResourceData(data.isFirst() ? FORM_ACTION : OPERATION_RESULT, data.get(), Collections.singletonMap(SELF_LINK, operation.getUrl().toString())), req.getDefaultMeta());
}
Also used : Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) HashUrl(com.developmentontheedge.be5.util.HashUrl) OperationResult(com.developmentontheedge.be5.operation.OperationResult) Operation(com.developmentontheedge.be5.operation.Operation) DocumentGenerator(com.developmentontheedge.be5.query.DocumentGenerator) FormPresentation(com.developmentontheedge.be5.model.FormPresentation) OperationExecutor(com.developmentontheedge.be5.api.services.OperationExecutor) ErrorModel(com.developmentontheedge.be5.model.jsonapi.ErrorModel)

Example 2 with OperationExecutor

use of com.developmentontheedge.be5.api.services.OperationExecutor in project be5 by DevelopmentOnTheEdge.

the class DatabaseModel method getEntity.

@Override
public EntityModel getEntity(String entityName) {
    Objects.requireNonNull(entityName);
    Entity entity = meta.getEntity(entityName);
    if (entity == null)
        throw Be5Exception.unknownEntity(entityName);
    return new EntityModelBase(sqlService, dpsHelper, validator, operationHelper, operationExecutor, meta, entity);
}
Also used : Entity(com.developmentontheedge.be5.metadata.model.Entity)

Aggregations

Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)1 OperationExecutor (com.developmentontheedge.be5.api.services.OperationExecutor)1 Entity (com.developmentontheedge.be5.metadata.model.Entity)1 FormPresentation (com.developmentontheedge.be5.model.FormPresentation)1 ErrorModel (com.developmentontheedge.be5.model.jsonapi.ErrorModel)1 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)1 Operation (com.developmentontheedge.be5.operation.Operation)1 OperationResult (com.developmentontheedge.be5.operation.OperationResult)1 DocumentGenerator (com.developmentontheedge.be5.query.DocumentGenerator)1 HashUrl (com.developmentontheedge.be5.util.HashUrl)1