Search in sources :

Example 6 with Request

use of com.developmentontheedge.be5.api.Request 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 7 with Request

use of com.developmentontheedge.be5.api.Request 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 8 with Request

use of com.developmentontheedge.be5.api.Request in project be5 by DevelopmentOnTheEdge.

the class DocumentGeneratorImpl method routeAndRun.

@Override
public Object routeAndRun(Query query, Map<String, String> parameters, int sortColumn, boolean sortDesc) {
    switch(query.getType()) {
        case STATIC:
            if (ActionUtils.isStaticPage(query)) {
                return getStatic(query);
            } else {
                throw Be5Exception.internalInQuery(new IllegalStateException("Unsupported static request"), query);
            }
        case D1:
        case D1_UNKNOWN:
            return getTable(query, parameters, sortColumn, sortDesc);
        case D2:
        case CONTAINER:
        case CUSTOM:
        case JAVASCRIPT:
            throw Be5Exception.internal("Not support operation type: " + query.getType());
        case GROOVY:
            try {
                Class aClass = groovyRegister.getClass(query.getEntity() + query.getName(), query.getQuery(), query.getFileName());
                if (aClass != null) {
                    TableBuilder tableBuilder = (TableBuilder) aClass.newInstance();
                    tableBuilder.initialize(query, parameters);
                    injector.injectAnnotatedFields(tableBuilder);
                    return getTable(query, parameters, tableBuilder.getTableModel());
                } else {
                    throw Be5Exception.internal("Class " + query.getQuery() + " is null.");
                }
            } catch (NoClassDefFoundError | IllegalAccessException | InstantiationException e) {
                throw new UnsupportedOperationException("Groovy feature has been excluded", e);
            }
        default:
            throw Be5Exception.internal("Unknown action type '" + query.getType() + "'");
    }
}
Also used : TableBuilder(com.developmentontheedge.be5.query.TableBuilder)

Example 9 with Request

use of com.developmentontheedge.be5.api.Request in project be5 by DevelopmentOnTheEdge.

the class MoreRowsGenerator method generate.

/**
 * Generates more rows for a table.
 * @throws IllegalArgumentException
 */
public MoreRows generate(Request req) {
    String entityName = req.getNonEmpty(RestApiConstants.ENTITY);
    String queryName = req.getNonEmpty(RestApiConstants.QUERY);
    Map<String, String> parametersMap = req.getValuesFromJsonAsStrings(RestApiConstants.VALUES);
    String startStr = req.getNonEmpty(RestApiConstants.START);
    String lengthStr = req.getNonEmpty(RestApiConstants.LENGTH);
    String drawStr = req.getNonEmpty(RestApiConstants.DRAW);
    String selectableStr = req.getNonEmpty(RestApiConstants.SELECTABLE);
    String totalNumberOfRowsStr = req.getNonEmpty(RestApiConstants.TOTAL_NUMBER_OF_ROWS);
    int draw;
    int start;
    int totalNumberOfRows;
    int length;
    try {
        draw = Integer.parseInt(drawStr);
        start = Integer.parseInt(startStr);
        length = Integer.parseInt(lengthStr);
        totalNumberOfRows = Integer.parseInt(totalNumberOfRowsStr);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException();
    }
    boolean selectable = Boolean.parseBoolean(selectableStr);
    Query query = injector.get(UserAwareMeta.class).getQuery(entityName, queryName);
    return generateMoreRows(query, req, parametersMap, selectable, draw, start, length, totalNumberOfRows);
}
Also used : Query(com.developmentontheedge.be5.metadata.model.Query) UserAwareMeta(com.developmentontheedge.be5.api.helpers.UserAwareMeta)

Example 10 with Request

use of com.developmentontheedge.be5.api.Request 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)

Aggregations

Request (com.developmentontheedge.be5.api.Request)8 Response (com.developmentontheedge.be5.api.Response)6 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)5 ErrorModel (com.developmentontheedge.be5.model.jsonapi.ErrorModel)5 Be5ProjectTest (com.developmentontheedge.be5.test.Be5ProjectTest)5 Test (org.junit.Test)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)3 Query (com.developmentontheedge.be5.metadata.model.Query)3 DocumentGenerator (com.developmentontheedge.be5.query.DocumentGenerator)3 Map (java.util.Map)3 Session (com.developmentontheedge.be5.api.Session)2 UserHelper (com.developmentontheedge.be5.api.helpers.UserHelper)2 RequestImpl (com.developmentontheedge.be5.api.impl.RequestImpl)2 StaticPagePresentation (com.developmentontheedge.be5.model.StaticPagePresentation)2 UserInfo (com.developmentontheedge.be5.model.UserInfo)2 JsonApiModel (com.developmentontheedge.be5.model.jsonapi.JsonApiModel)2 HashUrl (com.developmentontheedge.be5.util.HashUrl)2