Search in sources :

Example 1 with HashUrl

use of com.developmentontheedge.be5.util.HashUrl 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 2 with HashUrl

use of com.developmentontheedge.be5.util.HashUrl in project be5 by DevelopmentOnTheEdge.

the class Document method generate.

@Override
public void generate(Request req, Response res, Injector injector) {
    DocumentGenerator documentGenerator = injector.get(DocumentGenerator.class);
    UserAwareMeta userAwareMeta = injector.get(UserAwareMeta.class);
    String entityName = req.getNonEmpty(RestApiConstants.ENTITY);
    String queryName = req.getNonEmpty(RestApiConstants.QUERY);
    int sortColumn = req.getInt("order[0][column]", -1);
    boolean sortDesc = "desc".equals(req.get("order[0][dir]"));
    Map<String, String> parametersMap = req.getValuesFromJsonAsStrings(RestApiConstants.VALUES);
    HashUrl url = new HashUrl(TABLE_ACTION, entityName, queryName).named(parametersMap);
    Query query;
    try {
        query = userAwareMeta.getQuery(entityName, queryName);
    } catch (Be5Exception e) {
        sendError(req, res, url, e);
        return;
    }
    try {
        switch(req.getRequestUri()) {
            case "":
                JsonApiModel document = documentGenerator.getDocument(query, parametersMap, sortColumn, sortDesc);
                document.setMeta(req.getDefaultMeta());
                res.sendAsJson(document);
                return;
            case "moreRows":
                res.sendAsRawJson(new MoreRowsGenerator(injector).generate(req));
                return;
            default:
                res.sendUnknownActionError();
        }
    } catch (Be5Exception e) {
        sendError(req, res, url, e);
    } catch (Throwable e) {
        sendError(req, res, url, Be5Exception.internalInQuery(e, query));
    }
}
Also used : Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) MoreRowsGenerator(com.developmentontheedge.be5.query.impl.MoreRowsGenerator) Query(com.developmentontheedge.be5.metadata.model.Query) DocumentGenerator(com.developmentontheedge.be5.query.DocumentGenerator) UserAwareMeta(com.developmentontheedge.be5.api.helpers.UserAwareMeta) HashUrl(com.developmentontheedge.be5.util.HashUrl) JsonApiModel(com.developmentontheedge.be5.model.jsonapi.JsonApiModel)

Example 3 with HashUrl

use of com.developmentontheedge.be5.util.HashUrl 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 HashUrl

use of com.developmentontheedge.be5.util.HashUrl in project be5 by DevelopmentOnTheEdge.

the class CellFormatter method format.

private Object format(TableModel.RawCellModel cell, VarResolver varResolver) {
    // ImmutableList<Object> formattedParts = getFormattedPartsWithoutLink(cell, varResolver);
    Object formattedContent = getFormattedPartsWithoutLink(cell, varResolver);
    // formattedContent = StreamEx.of(formattedParts).map(this::print).joining();
    if (formattedContent instanceof String) {
        formattedContent = userAwareMeta.getLocalizedCell((String) formattedContent, query.getEntity().getName(), query.getName());
    }
    // TODO && extraQuery == Be5QueryExecutor.ExtraQuery.DEFAULT
    Map<String, String> blankNullsProperties = cell.options.get(DatabaseConstants.COL_ATTR_BLANKNULLS);
    if (blankNullsProperties != null) {
        if (formattedContent == null || formattedContent.equals("null")) {
            formattedContent = blankNullsProperties.getOrDefault("value", "");
        }
    }
    Map<String, String> nullIfProperties = cell.options.get(DatabaseConstants.COL_ATTR_NULLIF);
    if (nullIfProperties != null) {
        if (formattedContent == null || formattedContent.equals(nullIfProperties.get("value"))) {
            formattedContent = nullIfProperties.getOrDefault("result", "");
        }
    }
    Map<String, String> linkProperties = cell.options.get(DatabaseConstants.COL_ATTR_LINK);
    if (linkProperties != null) {
        try {
            HashUrl url = new HashUrl("table").positional(linkProperties.get("table")).positional(linkProperties.getOrDefault("queryName", DatabaseConstants.ALL_RECORDS_VIEW));
            String cols = linkProperties.get("columns");
            String vals = linkProperties.get("using");
            if (cols != null && vals != null) {
                String[] colsArr = cols.split(",");
                String[] valuesArr = vals.split(",");
                for (int i = 0; i < colsArr.length; i++) {
                    String resolveValue = varResolver.resolve(valuesArr[i]);
                    if (resolveValue != null)
                        url = url.named(colsArr[i], resolveValue);
                }
            }
            cell.options.put(DatabaseConstants.COL_ATTR_LINK, Collections.singletonMap("url", url.toString()));
        } catch (Throwable e) {
            throw Be5Exception.internalInQuery(new RuntimeException("Error in process COL_ATTR_LINK: " + cell.name, e), query);
        }
    }
    return formattedContent;
}
Also used : HashUrl(com.developmentontheedge.be5.util.HashUrl)

Example 5 with HashUrl

use of com.developmentontheedge.be5.util.HashUrl 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

HashUrl (com.developmentontheedge.be5.util.HashUrl)4 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)2 FormPresentation (com.developmentontheedge.be5.model.FormPresentation)2 ErrorModel (com.developmentontheedge.be5.model.jsonapi.ErrorModel)2 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)2 OperationResult (com.developmentontheedge.be5.operation.OperationResult)2 DocumentGenerator (com.developmentontheedge.be5.query.DocumentGenerator)2 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)1 OperationExecutor (com.developmentontheedge.be5.api.services.OperationExecutor)1 Query (com.developmentontheedge.be5.metadata.model.Query)1 JsonApiModel (com.developmentontheedge.be5.model.jsonapi.JsonApiModel)1 Operation (com.developmentontheedge.be5.operation.Operation)1 MoreRowsGenerator (com.developmentontheedge.be5.query.impl.MoreRowsGenerator)1 ArrayList (java.util.ArrayList)1