Search in sources :

Example 1 with JsonApiModel

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

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

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

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

Example 5 with JsonApiModel

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

the class ResponseTest method testJsonObject.

@Test
public void testJsonObject() {
    JsonApiModel jsonApiModel = JsonApiModel.data(new ResourceData("testType", "test", Collections.singletonMap(SELF_LINK, "url")), Collections.singletonMap(TIMESTAMP_PARAM, 1503291145939L));
    response.sendAsJson(jsonApiModel);
    verify(writer).append(doubleQuotes("{" + "'data':{'attributes':'test','links':{'self':'url'},'type':'testType'}," + "'meta':{'_ts_':1503291145939}" + "}"));
}
Also used : ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) JsonApiModel(com.developmentontheedge.be5.model.jsonapi.JsonApiModel) Test(org.junit.Test) Be5ProjectTest(com.developmentontheedge.be5.test.Be5ProjectTest)

Aggregations

JsonApiModel (com.developmentontheedge.be5.model.jsonapi.JsonApiModel)3 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)3 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)2 Query (com.developmentontheedge.be5.metadata.model.Query)2 ErrorModel (com.developmentontheedge.be5.model.jsonapi.ErrorModel)2 DocumentGenerator (com.developmentontheedge.be5.query.DocumentGenerator)2 HashUrl (com.developmentontheedge.be5.util.HashUrl)2 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)1 Entity (com.developmentontheedge.be5.metadata.model.Entity)1 FormPresentation (com.developmentontheedge.be5.model.FormPresentation)1 StaticPagePresentation (com.developmentontheedge.be5.model.StaticPagePresentation)1 OperationResult (com.developmentontheedge.be5.operation.OperationResult)1 MoreRowsGenerator (com.developmentontheedge.be5.query.impl.MoreRowsGenerator)1 Be5QueryExecutor (com.developmentontheedge.be5.query.impl.model.Be5QueryExecutor)1 Be5ProjectTest (com.developmentontheedge.be5.test.Be5ProjectTest)1 SqlQuery (com.developmentontheedge.sql.model.SqlQuery)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1