Search in sources :

Example 16 with Injector

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

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

the class TemplateProcessor method generate.

@Override
public void generate(Request req, Response res, Injector injector) {
    UserAwareMeta userAwareMeta = injector.get(UserAwareMeta.class);
    String title = userAwareMeta.getColumnTitle("index", "page", "title");
    String description = userAwareMeta.getColumnTitle("index", "page", "description");
    Context context = new Context();
    context.setVariable("lang", UserInfoHolder.getLanguage());
    context.setVariable("title", title);
    context.setVariable("description", description);
    String reqWithoutContext = req.getRequestUri().replaceFirst(req.getContextPath(), "");
    if (!reqWithoutContext.endsWith("/"))
        reqWithoutContext += "/";
    context.setVariable("baseUrl", req.getContextPath() + reqWithoutContext);
    context.setVariable("baseUrlWithoutContext", reqWithoutContext);
    res.sendHtml(templateEngine.process(reqWithoutContext + "index", context));
}
Also used : ServletContext(javax.servlet.ServletContext) Context(org.thymeleaf.context.Context) UserAwareMeta(com.developmentontheedge.be5.api.helpers.UserAwareMeta)

Example 18 with Injector

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

the class OperationHelper method getTagsFromCustomSelectionViewExecute.

// 
// public Map<String, String> getTagsMapFromQuery( Map<String, String> parameters, String query, Object... params )
// {
// //return getTagsListFromQuery( Collections.emptyMap(), query, params );
// List<String[]> tags = db.selectList("SELECT " + valueColumnName + ", " + textColumnName + " FROM " + tableName,
// rs -> new String[]{rs.getString(valueColumnName), rs.getString(textColumnName)}
// );
// String[][] stockArr = new String[tags.size()][2];
// return tags.toArray(stockArr);
// }
private String[][] getTagsFromCustomSelectionViewExecute(Query query, Map<String, ?> parameters) {
    String entityName = query.getEntity().getName();
    // todo refactoring Be5QueryExecutor,
    Map<String, String> stringStringMap = new HashMap<>();
    // parameters.forEach((key, value) -> stringStringMap.put(key, value.toString()));
    for (Map.Entry<String, ?> entry : parameters.entrySet()) {
        if (entry.getValue() != null)
            stringStringMap.put(entry.getKey(), entry.getValue().toString());
    }
    TableModel table = TableModel.from(query, stringStringMap, false, injector).limit(Integer.MAX_VALUE).build();
    String[][] stockArr = new String[table.getRows().size()][2];
    int i = 0;
    for (TableModel.RowModel row : table.getRows()) {
        String first = row.getCells().size() >= 1 ? row.getCells().get(0).content.toString() : "";
        String second = row.getCells().size() >= 2 ? row.getCells().get(1).content.toString() : "";
        stockArr[i++] = new String[] { first, userAwareMeta.getColumnTitle(entityName, second) };
    }
    return stockArr;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TableModel(com.developmentontheedge.be5.query.impl.model.TableModel)

Example 19 with Injector

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

the class DownloadComponent method generate.

@Override
public void generate(Request req, Response res, Injector injector) {
    String entity = req.getNonEmpty("_t_");
    String ID = req.get("ID");
    String typeColumn = req.get("_typeColumn_");
    String filenameColumn = req.get("_filenameColumn_");
    String dataColumn = req.getNonEmpty("_dataColumn_");
    String charsetColumn = req.get("_charsetColumn_");
    // String encoding       = req.get("_enc_");
    boolean download = "yes".equals(req.get("_download_"));
    RecordModel record = injector.get(DatabaseModel.class).getEntity(entity).get(ID);
    String filename = record.getValueAsString(filenameColumn);
    String contentType = record.getValueAsString(typeColumn);
    String charset = MoreObjects.firstNonNull(record.getValueAsString(charsetColumn), Charsets.UTF_8.name());
    Object data = record.getValue(dataColumn);
    InputStream in;
    if (data instanceof byte[]) {
        in = new ByteArrayInputStream((byte[]) data);
    } else // else if (data instanceof Blob)
    // {
    // in = ((Blob) data).getBinaryStream();
    // }
    // else if (data instanceof String)
    // {
    // in = new ByteArrayInputStream(((String) data).getBytes(charset));
    // }
    {
        throw Be5Exception.internal("Unknown data type");
    }
    HttpServletResponse response = res.getRawResponse();
    response.setContentType(contentType + "; charset=" + charset);
    if (download) {
        response.setHeader("Content-disposition", "attachment; filename=" + UrlEscapers.urlFormParameterEscaper().escape(filename));
    } else {
        response.setHeader("Content-disposition", "filename=" + UrlEscapers.urlFormParameterEscaper().escape(filename));
    }
    try {
        ByteStreams.copy(in, response.getOutputStream());
    } catch (IOException e) {
        throw Be5Exception.internal(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RecordModel(com.developmentontheedge.be5.databasemodel.RecordModel) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 20 with Injector

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

the class Menu method generateMenu.

private MenuResponse generateMenu(Injector injector, boolean withIds, EntityType entityType) {
    UserAwareMeta userAwareMeta = injector.get(UserAwareMeta.class);
    List<String> roles = UserInfoHolder.getCurrentRoles();
    String language = UserInfoHolder.getLanguage();
    List<RootNode> entities = collectEntities(injector.getMeta(), userAwareMeta, language, roles, withIds, entityType);
    return new MenuResponse(entities);
}
Also used : UserAwareMeta(com.developmentontheedge.be5.api.helpers.UserAwareMeta)

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