Search in sources :

Example 11 with Injector

use of com.developmentontheedge.be5.env.Injector in project be5 by DevelopmentOnTheEdge.

the class UserInfoComponentTest method generateSelectRolesAndSendEmpty.

@Test
public void generateSelectRolesAndSendEmpty() throws Exception {
    Response response = mock(Response.class);
    component.generate(getSpyMockRequest("", ImmutableMap.of("roles", "")), response, injector);
    verify(response).sendAsRawJson(new UserInfoComponent.State(true, TEST_USER, Collections.singletonList(RoleType.ROLE_ADMINISTRATOR), Collections.singletonList(RoleType.ROLE_ADMINISTRATOR), any(Date.class)));
}
Also used : Response(com.developmentontheedge.be5.api.Response) Test(org.junit.Test) Be5ProjectTest(com.developmentontheedge.be5.test.Be5ProjectTest)

Example 12 with Injector

use of com.developmentontheedge.be5.env.Injector 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 13 with Injector

use of com.developmentontheedge.be5.env.Injector 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 14 with Injector

use of com.developmentontheedge.be5.env.Injector in project be5 by DevelopmentOnTheEdge.

the class LanguageSelector method getState.

private LanguageSelectorResponse getState(Injector injector) {
    Project project = injector.getProject();
    List<String> languages = Arrays.stream(project.getLanguages()).map(String::toUpperCase).collect(Collectors.toList());
    String selectedLanguage = UserInfoHolder.getLanguage().toUpperCase();
    Map<String, String> messages = readMessages(project, selectedLanguage);
    return new LanguageSelectorResponse(languages, selectedLanguage, messages);
}
Also used : Project(com.developmentontheedge.be5.metadata.model.Project)

Example 15 with Injector

use of com.developmentontheedge.be5.env.Injector in project be5 by DevelopmentOnTheEdge.

the class QueryBuilder method insert.

private void insert(String sql, Injector injector) {
    Object id = injector.getSqlService().insert(sql);
    resourceDataList.add(new ResourceData("result", FrontendConstants.STATIC_ACTION, new StaticPagePresentation("Insert was successful", "New primaryKey: " + id), null));
}
Also used : ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) StaticPagePresentation(com.developmentontheedge.be5.model.StaticPagePresentation)

Aggregations

Test (org.junit.Test)23 Be5ProjectTest (com.developmentontheedge.be5.test.Be5ProjectTest)21 Response (com.developmentontheedge.be5.api.Response)20 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)7 Action (com.developmentontheedge.be5.model.Action)6 ErrorModel (com.developmentontheedge.be5.model.jsonapi.ErrorModel)5 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)4 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)4 StaticPagePresentation (com.developmentontheedge.be5.model.StaticPagePresentation)4 JsonApiModel (com.developmentontheedge.be5.model.jsonapi.JsonApiModel)4 Request (com.developmentontheedge.be5.api.Request)3 DocumentGenerator (com.developmentontheedge.be5.query.DocumentGenerator)3 Map (java.util.Map)3 UserHelper (com.developmentontheedge.be5.api.helpers.UserHelper)2 Injector (com.developmentontheedge.be5.env.Injector)2 Project (com.developmentontheedge.be5.metadata.model.Project)2 Query (com.developmentontheedge.be5.metadata.model.Query)2 TableModel (com.developmentontheedge.be5.query.impl.model.TableModel)2 SqlMockOperationTest (com.developmentontheedge.be5.test.SqlMockOperationTest)2 HashUrl (com.developmentontheedge.be5.util.HashUrl)2