Search in sources :

Example 1 with ErrorModel

use of com.developmentontheedge.be5.model.jsonapi.ErrorModel in project be5 by DevelopmentOnTheEdge.

the class ResponseImpl method sendError.

// @Override
// public void sendErrorsAsJson(Object[] errors, Object meta)
// {
// throw new RuntimeException("");
// //TODO create ErrorObject, sendAsRawJson(new JsonApiModel(errors, meta, links));
// }
@Override
public void sendError(Be5Exception e) {
    ErrorModel errorModel;
    if (UserInfoHolder.isSystemDeveloper()) {
        errorModel = new ErrorModel(e);
    } else {
        errorModel = new ErrorModel(e.getHttpStatusCode(), "");
    }
    sendErrorAsJson(errorModel, null);
}
Also used : ErrorModel(com.developmentontheedge.be5.model.jsonapi.ErrorModel)

Example 2 with ErrorModel

use of com.developmentontheedge.be5.model.jsonapi.ErrorModel in project be5 by DevelopmentOnTheEdge.

the class Document method sendError.

private void sendError(Request req, Response res, HashUrl url, Be5Exception e) {
    String message = "";
    // message += GroovyRegister.getErrorCodeLine(e, query.getQuery());
    res.sendErrorAsJson(new ErrorModel(e, message, Collections.singletonMap(SELF_LINK, url.toString())), req.getDefaultMeta());
}
Also used : ErrorModel(com.developmentontheedge.be5.model.jsonapi.ErrorModel)

Example 3 with ErrorModel

use of com.developmentontheedge.be5.model.jsonapi.ErrorModel 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 ErrorModel

use of com.developmentontheedge.be5.model.jsonapi.ErrorModel in project be5 by DevelopmentOnTheEdge.

the class QueryBuilder method select.

private void select(String sql, Request req, Injector injector) {
    DocumentGenerator documentGenerator = injector.get(DocumentGenerator.class);
    String userQBuilderQueryName = UserInfoHolder.getUserName() + "Query";
    Map<String, String> parametersMap = req.getValuesFromJsonAsStrings(RestApiConstants.VALUES);
    Entity entity = new Entity(entityName, injector.getProject().getApplication(), EntityType.TABLE);
    DataElementUtils.save(entity);
    Query query = new Query(userQBuilderQueryName, entity);
    query.setType(QueryType.D1_UNKNOWN);
    if (sql != null) {
        query.setQuery(sql);
    }
    DataElementUtils.save(query);
    try {
        resourceDataList.add(new ResourceData("finalSql", FrontendConstants.STATIC_ACTION, new StaticPagePresentation("Final sql", new Be5QueryExecutor(query, parametersMap, injector).getFinalSql()), null));
    } catch (Be5Exception e) {
        errorModelList.add(new ErrorModel(e));
    }
    try {
        JsonApiModel document = documentGenerator.getDocument(query, parametersMap);
        // todo refactor documentGenerator
        document.getData().setId("result");
        resourceDataList.add(document.getData());
        resourceDataList.addAll(Arrays.asList(document.getIncluded()));
    } catch (Be5Exception e) {
        errorModelList.add(new ErrorModel(e));
    }
    entity.getOrigin().remove(entityName);
}
Also used : Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) Entity(com.developmentontheedge.be5.metadata.model.Entity) ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) StaticPagePresentation(com.developmentontheedge.be5.model.StaticPagePresentation) Query(com.developmentontheedge.be5.metadata.model.Query) SqlQuery(com.developmentontheedge.sql.model.SqlQuery) DocumentGenerator(com.developmentontheedge.be5.query.DocumentGenerator) ErrorModel(com.developmentontheedge.be5.model.jsonapi.ErrorModel) Be5QueryExecutor(com.developmentontheedge.be5.query.impl.model.Be5QueryExecutor) JsonApiModel(com.developmentontheedge.be5.model.jsonapi.JsonApiModel)

Example 5 with ErrorModel

use of com.developmentontheedge.be5.model.jsonapi.ErrorModel in project be5 by DevelopmentOnTheEdge.

the class QueryBuilder method generate.

@Override
public void generate(Request req, Response res, Injector injector) {
    if (UserInfoHolder.isSystemDeveloper()) {
        String sql = req.get("sql");
        boolean execute = sql != null;
        List<String> history;
        if (req.getAttribute(QUERY_BUILDER_HISTORY) != null) {
            history = (List<String>) req.getAttribute(QUERY_BUILDER_HISTORY);
        } else {
            history = new ArrayList<>();
        }
        if (sql == null) {
            if (!history.isEmpty()) {
                sql = history.get(history.size() - 1);
            } else {
                sql = "select * from users";
            }
        } else {
            if (history.isEmpty() || !history.get(history.size() - 1).equals(sql)) {
                history.add(sql);
                req.setAttribute(QUERY_BUILDER_HISTORY, history);
            }
        }
        ResourceData resourceData = new ResourceData("queryBuilder", new Data(sql, history), Collections.singletonMap(SELF_LINK, "queryBuilder"));
        try {
            SqlType type = getSqlType(sql);
            if (type == SqlType.SELECT) {
                select(sql, req, injector);
            } else {
                if (execute) {
                    switch(type) {
                        case INSERT:
                            insert(sql, injector);
                            break;
                        case UPDATE:
                            update(sql, injector);
                            break;
                        case DELETE:
                            update(sql, injector);
                            break;
                        default:
                            res.sendUnknownActionError();
                            return;
                    }
                }
            }
        } catch (Throwable e) {
            errorModelList.add(new ErrorModel(Be5Exception.internal(e)));
        }
        res.sendAsJson(JsonApiModel.data(resourceData, errorModelList.toArray(new ErrorModel[0]), resourceDataList.toArray(new ResourceData[0]), req.getDefaultMeta(), null));
    } else {
        res.sendErrorAsJson(new ErrorModel(Be5Exception.accessDenied(), "Role " + RoleType.ROLE_SYSTEM_DEVELOPER + " required.", Collections.singletonMap(SELF_LINK, "queryBuilder")), req.getDefaultMeta());
    }
}
Also used : ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) ErrorModel(com.developmentontheedge.be5.model.jsonapi.ErrorModel) ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData)

Aggregations

ErrorModel (com.developmentontheedge.be5.model.jsonapi.ErrorModel)6 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)4 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)2 StaticPagePresentation (com.developmentontheedge.be5.model.StaticPagePresentation)2 DocumentGenerator (com.developmentontheedge.be5.query.DocumentGenerator)2 OperationExecutor (com.developmentontheedge.be5.api.services.OperationExecutor)1 Entity (com.developmentontheedge.be5.metadata.model.Entity)1 Query (com.developmentontheedge.be5.metadata.model.Query)1 FormPresentation (com.developmentontheedge.be5.model.FormPresentation)1 JsonApiModel (com.developmentontheedge.be5.model.jsonapi.JsonApiModel)1 Operation (com.developmentontheedge.be5.operation.Operation)1 OperationResult (com.developmentontheedge.be5.operation.OperationResult)1 Be5QueryExecutor (com.developmentontheedge.be5.query.impl.model.Be5QueryExecutor)1 HashUrl (com.developmentontheedge.be5.util.HashUrl)1 SqlQuery (com.developmentontheedge.sql.model.SqlQuery)1