Search in sources :

Example 41 with Query

use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.

the class Menu method generateEntityOperations.

private List<OperationNode> generateEntityOperations(Entity entity, Meta meta, UserAwareMeta userAwareMeta, List<String> roles, boolean withIds) {
    List<OperationNode> operations = new ArrayList<>();
    Query allRecords = entity.getQueries().get(DatabaseConstants.ALL_RECORDS_VIEW);
    String insertOperationName = INSERT_OPERATION;
    if (allRecords != null && allRecords.getOperationNames().getFinalValues().contains(insertOperationName)) {
        Operation insertOperation = entity.getOperations().get(insertOperationName);
        if (insertOperation != null && meta.isAvailableFor(insertOperation, roles)) {
            String title = userAwareMeta.getLocalizedOperationTitle(entity.getName(), insertOperationName);
            Action action = ActionUtils.toAction(DatabaseConstants.ALL_RECORDS_VIEW, insertOperation);
            OperationId id = withIds ? new OperationId(entity.getName(), insertOperationName) : null;
            OperationNode operation = new OperationNode(id, title, action);
            operations.add(operation);
        }
    }
    return operations;
}
Also used : Action(com.developmentontheedge.be5.model.Action) Query(com.developmentontheedge.be5.metadata.model.Query) ArrayList(java.util.ArrayList) Operation(com.developmentontheedge.be5.metadata.model.Operation)

Example 42 with Query

use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.

the class Be5QueryExecutor method hasColumnWithLabel.

private boolean hasColumnWithLabel(AstStart ast, String idColumnLabel) {
    AstQuery query = ast.getQuery();
    Optional<AstSelect> selectOpt = query.children().select(AstSelect.class).collect(MoreCollectors.onlyOne());
    if (!selectOpt.isPresent())
        return false;
    AstSelect select = selectOpt.get();
    return select.getSelectList().children().select(AstDerivedColumn.class).map(AstDerivedColumn::getAlias).nonNull().map(alias -> alias.replaceFirst("^\"(.+)\"$", "$1")).has(idColumnLabel);
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) Connection(java.sql.Connection) DynamicPropertySet(com.developmentontheedge.beans.DynamicPropertySet) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) Meta(com.developmentontheedge.be5.api.services.Meta) Query(com.developmentontheedge.be5.metadata.model.Query) SqlQuery(com.developmentontheedge.sql.model.SqlQuery) ColumnAdder(com.developmentontheedge.sql.format.ColumnAdder) ResultSetParser(com.developmentontheedge.be5.api.sql.ResultSetParser) UserInfoHolder(com.developmentontheedge.be5.api.helpers.UserInfoHolder) Map(java.util.Map) AstLimit(com.developmentontheedge.sql.model.AstLimit) Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) Context(com.developmentontheedge.sql.format.Context) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) MoreCollectors(one.util.streamex.MoreCollectors) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) Logger(java.util.logging.Logger) EntityModel(com.developmentontheedge.be5.databasemodel.EntityModel) Objects(java.util.Objects) List(java.util.List) QueryType(com.developmentontheedge.be5.metadata.QueryType) LimitsApplier(com.developmentontheedge.sql.format.LimitsApplier) Simplifier(com.developmentontheedge.sql.format.Simplifier) AstBeParameterTag(com.developmentontheedge.sql.model.AstBeParameterTag) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) StreamEx(one.util.streamex.StreamEx) DatabaseConstants(com.developmentontheedge.be5.metadata.DatabaseConstants) Optional(java.util.Optional) FilterHelper(com.developmentontheedge.be5.api.helpers.FilterHelper) RecordModel(com.developmentontheedge.be5.databasemodel.RecordModel) ResultSetMetaData(java.sql.ResultSetMetaData) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstQuery(com.developmentontheedge.sql.model.AstQuery) DatabaseService(com.developmentontheedge.be5.api.services.DatabaseService) QueryContext(com.developmentontheedge.sql.format.QueryContext) HashMap(java.util.HashMap) AstStart(com.developmentontheedge.sql.model.AstStart) Token(com.developmentontheedge.sql.model.Token) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Entity(com.developmentontheedge.be5.metadata.model.Entity) SQLException(java.sql.SQLException) Formatter(com.developmentontheedge.sql.format.Formatter) Injector(com.developmentontheedge.be5.env.Injector) DynamicPropertySetSupport(com.developmentontheedge.beans.DynamicPropertySetSupport) ParserContext(com.developmentontheedge.sql.model.ParserContext) AstSelect(com.developmentontheedge.sql.model.AstSelect) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) SqlService(com.developmentontheedge.be5.api.services.SqlService) DynamicProperty(com.developmentontheedge.beans.DynamicProperty) DatabaseModel(com.developmentontheedge.be5.databasemodel.impl.DatabaseModel) DpsRecordAdapter(com.developmentontheedge.be5.api.sql.DpsRecordAdapter) Ast(com.developmentontheedge.sql.format.Ast) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstNumericConstant(com.developmentontheedge.sql.model.AstNumericConstant) UserAwareMeta(com.developmentontheedge.be5.api.helpers.UserAwareMeta) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) Collections(java.util.Collections) AstQuery(com.developmentontheedge.sql.model.AstQuery)

Example 43 with Query

use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.

the class Be5QueryExecutor method executeSubQuery.

@Override
public List<DynamicPropertySet> executeSubQuery(String subqueryName, CellFormatter.VarResolver varResolver) {
    AstBeSqlSubQuery subQuery = contextApplier.applyVars(subqueryName, varResolver::resolve);
    if (subQuery.getQuery() == null) {
        return Collections.emptyList();
    }
    String finalSql = new Formatter().format(subQuery.getQuery(), context, parserContext);
    List<DynamicPropertySet> dynamicPropertySets;
    try {
        dynamicPropertySets = listDps(finalSql);
    } catch (Throwable e) {
        // TODO only for Document presentation, for operations must be error throw
        Be5Exception be5Exception = Be5Exception.internalInQuery(e, query);
        log.log(Level.SEVERE, be5Exception.toString() + " Final SQL: " + finalSql, be5Exception);
        DynamicPropertySetSupport dynamicProperties = new DynamicPropertySetSupport();
        dynamicProperties.add(new DynamicProperty("___ID", String.class, "-1"));
        dynamicProperties.add(new DynamicProperty("error", String.class, UserInfoHolder.isSystemDeveloper() ? Be5Exception.getMessage(e) : "error"));
        dynamicPropertySets = Collections.singletonList(dynamicProperties);
    }
    // return Collections.singletonList(dynamicProperties);
    return dynamicPropertySets;
}
Also used : DynamicPropertySet(com.developmentontheedge.beans.DynamicPropertySet) Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) DynamicProperty(com.developmentontheedge.beans.DynamicProperty) Formatter(com.developmentontheedge.sql.format.Formatter) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) DynamicPropertySetSupport(com.developmentontheedge.beans.DynamicPropertySetSupport)

Example 44 with Query

use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.

the class CellFormatter method format.

private Object format(TableModel.RawCellModel cell, VarResolver varResolver) {
    // ImmutableList<Object> formattedParts = getFormattedPartsWithoutLink(cell, varResolver);
    Object formattedContent = getFormattedPartsWithoutLink(cell, varResolver);
    // formattedContent = StreamEx.of(formattedParts).map(this::print).joining();
    if (formattedContent instanceof String) {
        formattedContent = userAwareMeta.getLocalizedCell((String) formattedContent, query.getEntity().getName(), query.getName());
    }
    // TODO && extraQuery == Be5QueryExecutor.ExtraQuery.DEFAULT
    Map<String, String> blankNullsProperties = cell.options.get(DatabaseConstants.COL_ATTR_BLANKNULLS);
    if (blankNullsProperties != null) {
        if (formattedContent == null || formattedContent.equals("null")) {
            formattedContent = blankNullsProperties.getOrDefault("value", "");
        }
    }
    Map<String, String> nullIfProperties = cell.options.get(DatabaseConstants.COL_ATTR_NULLIF);
    if (nullIfProperties != null) {
        if (formattedContent == null || formattedContent.equals(nullIfProperties.get("value"))) {
            formattedContent = nullIfProperties.getOrDefault("result", "");
        }
    }
    Map<String, String> linkProperties = cell.options.get(DatabaseConstants.COL_ATTR_LINK);
    if (linkProperties != null) {
        try {
            HashUrl url = new HashUrl("table").positional(linkProperties.get("table")).positional(linkProperties.getOrDefault("queryName", DatabaseConstants.ALL_RECORDS_VIEW));
            String cols = linkProperties.get("columns");
            String vals = linkProperties.get("using");
            if (cols != null && vals != null) {
                String[] colsArr = cols.split(",");
                String[] valuesArr = vals.split(",");
                for (int i = 0; i < colsArr.length; i++) {
                    String resolveValue = varResolver.resolve(valuesArr[i]);
                    if (resolveValue != null)
                        url = url.named(colsArr[i], resolveValue);
                }
            }
            cell.options.put(DatabaseConstants.COL_ATTR_LINK, Collections.singletonMap("url", url.toString()));
        } catch (Throwable e) {
            throw Be5Exception.internalInQuery(new RuntimeException("Error in process COL_ATTR_LINK: " + cell.name, e), query);
        }
    }
    return formattedContent;
}
Also used : HashUrl(com.developmentontheedge.be5.util.HashUrl)

Example 45 with Query

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

Aggregations

Query (com.developmentontheedge.be5.metadata.model.Query)29 Entity (com.developmentontheedge.be5.metadata.model.Entity)16 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)15 Project (com.developmentontheedge.be5.metadata.model.Project)12 Module (com.developmentontheedge.be5.metadata.model.Module)8 Path (java.nio.file.Path)8 Operation (com.developmentontheedge.be5.metadata.model.Operation)6 HashMap (java.util.HashMap)6 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)4 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)4 Be5ProjectTest (com.developmentontheedge.be5.test.Be5ProjectTest)4 Map (java.util.Map)4 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)3 ReadException (com.developmentontheedge.be5.metadata.exception.ReadException)3 DataElementPath (com.developmentontheedge.be5.metadata.model.base.DataElementPath)3 LoadContext (com.developmentontheedge.be5.metadata.serialization.LoadContext)3 LinkedHashMap (java.util.LinkedHashMap)3 BeModelElement (com.developmentontheedge.be5.metadata.model.base.BeModelElement)2 Action (com.developmentontheedge.be5.model.Action)2