Search in sources :

Example 26 with Injector

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

Example 27 with Injector

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

the class StaticPageComponent method generate.

@Override
public void generate(Request req, Response res, Injector injector) {
    String language = UserInfoHolder.getLanguage();
    String page = req.getRequestUri();
    String staticPageContent = injector.getProject().getStaticPageContent(language, page);
    if (staticPageContent == null) {
        // todo localize
        res.sendErrorAsJson(new ErrorModel("500", ErrorTitles.formatTitle(Be5ErrorCode.NOT_FOUND, page), Collections.singletonMap(SELF_LINK, "static/" + page)), req.getDefaultMeta());
    } else {
        res.sendAsJson(new ResourceData(STATIC_ACTION, new StaticPagePresentation("", staticPageContent), Collections.singletonMap(SELF_LINK, "static/" + page)), req.getDefaultMeta());
    }
}
Also used : ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) StaticPagePresentation(com.developmentontheedge.be5.model.StaticPagePresentation) ErrorModel(com.developmentontheedge.be5.model.jsonapi.ErrorModel)

Example 28 with Injector

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

the class MainServlet method runTemplateProcessor.

private void runTemplateProcessor(String componentId, Request req, Response res) {
    if (UserInfoHolder.getUserInfo() == null) {
        injector.get(UserHelper.class).initGuest(req);
    }
    try {
        runRequestPreprocessors(componentId, req, res);
        new TemplateProcessor(servletContext).generate(req, res, injector);
    } catch (Be5Exception ex) {
        if (ex.getCode().isInternal() || ex.getCode().isAccessDenied()) {
            log.log(Level.SEVERE, ex.getMessage(), ex);
        }
        res.sendError(ex);
    } catch (Throwable e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        res.sendError(Be5Exception.internal(e));
    }
}
Also used : Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) UserHelper(com.developmentontheedge.be5.api.helpers.UserHelper) TemplateProcessor(com.developmentontheedge.be5.components.TemplateProcessor)

Example 29 with Injector

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

the class DocumentGeneratorImpl method getTable.

public TablePresentation getTable(Query query, Map<String, String> parameters, int sortColumn, boolean sortDesc) {
    List<TableOperationPresentation> operations = collectOperations(query);
    final boolean selectable = !operations.isEmpty() && query.getType() == QueryType.D1;
    int limit = userAwareMeta.getQuerySettings(query).getMaxRecordsPerPage();
    if (limit == 0) {
        // todo delete defaultPageLimit, use getQuerySettings(query).getMaxRecordsPerPage()
        limit = Integer.parseInt(getLayoutObject(query).getOrDefault("defaultPageLimit", coreUtils.getSystemSetting("be5_defaultPageLimit", "10")).toString());
    }
    TableModel table = TableModel.from(query, parameters, selectable, injector).sortOrder(sortColumn, sortDesc).limit(limit).build();
    return getTable(query, parameters, table);
}
Also used : TableOperationPresentation(com.developmentontheedge.be5.model.TableOperationPresentation) TableModel(com.developmentontheedge.be5.query.impl.model.TableModel)

Example 30 with Injector

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

the class LanguageSelectorTest method generateSelect.

@Test
public void generateSelect() throws Exception {
    Response response = mock(Response.class);
    Request request = getSpyMockRequest("select", ImmutableMap.of("language", "RU"));
    component.generate(request, response, injector);
    verify(response).sendAsRawJson(eq(languageSelectorResponse));
}
Also used : Response(com.developmentontheedge.be5.api.Response) Request(com.developmentontheedge.be5.api.Request) Test(org.junit.Test) Be5ProjectTest(com.developmentontheedge.be5.test.Be5ProjectTest)

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