Search in sources :

Example 1 with OperationResult

use of com.developmentontheedge.be5.operation.OperationResult in project be5 by DevelopmentOnTheEdge.

the class DateTimeTest method testOperation.

@Test
public void testOperation() {
    Operation operation = createOperation("dateTime", "All records", "Insert", "0");
    ImmutableMap<String, Object> values = ImmutableMap.of("activeFrom", "1901-02-03");
    Either<Object, OperationResult> generate = generateOperation(operation, values);
    Object parameters = generate.getFirst();
    assertEquals("{" + "'values':{'activeFrom':'1901-02-03'}," + "'meta':{'/activeFrom':{'displayName':'activeFrom','type':'Date'}}," + "'order':['/activeFrom']" + "}", oneQuotes(JsonFactory.bean(parameters)));
    OperationResult result = executeOperation(operation, values).getSecond();
    assertEquals(OperationResult.redirect("table/dateTime/All records"), result);
}
Also used : OperationResult(com.developmentontheedge.be5.operation.OperationResult) Operation(com.developmentontheedge.be5.operation.Operation) Test(org.junit.Test) SqlMockOperationTest(com.developmentontheedge.be5.test.SqlMockOperationTest)

Example 2 with OperationResult

use of com.developmentontheedge.be5.operation.OperationResult in project be5 by DevelopmentOnTheEdge.

the class StandardOperationsDBTest method editInvoke.

@Test
public void editInvoke() {
    OperationResult operationResult = executeOperation("testtableAdmin", "All records", "Edit", id.toString(), "{'name':'EditName','value':123}").getSecond();
    assertEquals(OperationResult.redirect("table/testtableAdmin/All records"), operationResult);
    assertEquals("EditName,123", db.select("SELECT name, value FROM testtableAdmin WHERE id = ?", TestUtils::resultSetToString, id));
}
Also used : OperationResult(com.developmentontheedge.be5.operation.OperationResult) OperationDBTest(com.developmentontheedge.be5.test.OperationDBTest) Test(org.junit.Test)

Example 3 with OperationResult

use of com.developmentontheedge.be5.operation.OperationResult 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 4 with OperationResult

use of com.developmentontheedge.be5.operation.OperationResult in project be5 by DevelopmentOnTheEdge.

the class OperationServiceImpl method execute.

@Override
public Either<Object, OperationResult> execute(Operation operation, Map<String, Object> values) {
    Map<String, Object> presetValues = getPresetValues(operation.getContext(), values);
    operation.setResult(OperationResult.execute());
    Object parameters = operationExecutor.execute(operation, presetValues);
    if (OperationStatus.ERROR == operation.getStatus()) {
        try {
            validator.isError(parameters);
        } catch (RuntimeException e) {
            log.log(Level.INFO, "error on execute in parameters", e);
            operation.setResult(OperationResult.error(e));
            return replaceNullValueToEmptyStringAndReturn(parameters);
        }
        OperationResult invokeResult = operation.getResult();
        operation.setResult(OperationResult.execute());
        Object newParameters = operationExecutor.generate(operation, presetValues);
        if (OperationStatus.ERROR == operation.getStatus()) {
            return Either.second(invokeResult);
        }
        if (newParameters != null) {
            operation.setResult(invokeResult);
            return replaceNullValueToEmptyStringAndReturn(newParameters);
        } else {
            return Either.second(invokeResult);
        }
    }
    return Either.second(operation.getResult());
}
Also used : OperationResult(com.developmentontheedge.be5.operation.OperationResult)

Example 5 with OperationResult

use of com.developmentontheedge.be5.operation.OperationResult in project be5 by DevelopmentOnTheEdge.

the class DocumentGeneratorImpl method getDocument.

@Override
public JsonApiModel getDocument(Query query, Map<String, String> parameters, int sortColumn, boolean sortDesc) {
    Object data = routeAndRun(query, parameters, sortColumn, sortDesc);
    HashUrl url = new HashUrl(TABLE_ACTION, query.getEntity().getName(), query.getName()).named(parameters);
    List<ResourceData> included = new ArrayList<>();
    String topForm = (String) ParseRequestUtils.getValuesFromJson(query.getLayout()).get(TOP_FORM);
    if (topForm != null) {
        com.developmentontheedge.be5.operation.Operation operation = operationExecutor.create(query.getEntity().getName(), query.getName(), topForm, new String[] {}, parameters);
        Either<FormPresentation, OperationResult> dataTopForm = generateForm(operation, Collections.emptyMap());
        included.add(new ResourceData(TOP_FORM, dataTopForm.isFirst() ? FORM_ACTION : OPERATION_RESULT, dataTopForm.get(), Collections.singletonMap(SELF_LINK, operation.getUrl().toString())));
    }
    return JsonApiModel.data(new ResourceData(TABLE_ACTION, data, Collections.singletonMap(SELF_LINK, url.toString())), included.toArray(new ResourceData[0]), null);
}
Also used : ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) FormPresentation(com.developmentontheedge.be5.model.FormPresentation) ArrayList(java.util.ArrayList) HashUrl(com.developmentontheedge.be5.util.HashUrl) OperationResult(com.developmentontheedge.be5.operation.OperationResult)

Aggregations

OperationResult (com.developmentontheedge.be5.operation.OperationResult)6 FormPresentation (com.developmentontheedge.be5.model.FormPresentation)2 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)2 Operation (com.developmentontheedge.be5.operation.Operation)2 HashUrl (com.developmentontheedge.be5.util.HashUrl)2 Test (org.junit.Test)2 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)1 OperationExecutor (com.developmentontheedge.be5.api.services.OperationExecutor)1 ErrorModel (com.developmentontheedge.be5.model.jsonapi.ErrorModel)1 DocumentGenerator (com.developmentontheedge.be5.query.DocumentGenerator)1 OperationDBTest (com.developmentontheedge.be5.test.OperationDBTest)1 SqlMockOperationTest (com.developmentontheedge.be5.test.SqlMockOperationTest)1 ArrayList (java.util.ArrayList)1