Search in sources :

Example 6 with IStringExpression

use of org.adempiere.ad.expression.api.IStringExpression in project metasfresh-webui-api by metasfresh.

the class SqlDocumentQueryBuilder method buildSqlWhereClause.

private IPair<IStringExpression, List<Object>> buildSqlWhereClause() {
    final SqlParamsCollector sqlParams = SqlParamsCollector.newInstance();
    final CompositeStringExpression.Builder sqlWhereClauseBuilder = IStringExpression.composer();
    // 
    // Entity's WHERE clause
    {
        final IStringExpression entityWhereClauseExpression = entityBinding.getSqlWhereClause();
        if (!entityWhereClauseExpression.isNullExpression()) {
            sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
            sqlWhereClauseBuilder.append(" /* entity where clause */ (").append(entityWhereClauseExpression).append(")");
        }
    }
    // 
    // Key column
    // FIXME: handle AD_Reference/AD_Ref_List(s). In that case the recordId will be AD_Ref_List.Value,
    // so the SQL where clause which is currently build is AD_Ref_List_ID=<the AD_Ref_List.Value>.
    // The build SQL where clause shall be something like AD_Reference_ID=<the reference, i think we shall fetch it somehow from Lookup> AND Value=<the value, which currently is the recordId>
    final DocumentId recordId = getRecordId();
    if (recordId != null) {
        final List<SqlDocumentFieldDataBindingDescriptor> keyFields = entityBinding.getKeyFields();
        if (keyFields.isEmpty()) {
            throw new AdempiereException("Failed building where clause because there is no Key Column defined in " + entityBinding);
        } else // Single primary key
        if (keyFields.size() == 1) {
            final String keyColumnName = keyFields.get(0).getColumnName();
            sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
            sqlWhereClauseBuilder.append(" /* key */ ").append(keyColumnName).append("=").append(sqlParams.placeholder(recordId.toInt()));
        } else // Composed primary key
        {
            final Map<String, Object> keyColumnName2value = extractComposedKey(recordId, keyFields);
            keyColumnName2value.forEach((keyColumnName, value) -> {
                sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
                sqlWhereClauseBuilder.append(" /* key */ ").append(keyColumnName).append("=").append(sqlParams.placeholder(value));
            });
        }
    }
    // 
    // Parent link where clause (if any)
    final Document parentDocument = getParentDocument();
    if (parentDocument != null) {
        final String parentLinkColumnName = entityBinding.getParentLinkColumnName();
        final String linkColumnName = entityBinding.getLinkColumnName();
        if (parentLinkColumnName != null && linkColumnName != null) {
            final IDocumentFieldView parentLinkField = parentDocument.getFieldView(parentLinkColumnName);
            final Object parentLinkValue = parentLinkField.getValue();
            final DocumentFieldWidgetType parentLinkWidgetType = parentLinkField.getWidgetType();
            final Class<?> targetClass = entityBinding.getFieldByFieldName(linkColumnName).getSqlValueClass();
            final Object sqlParentLinkValue = SqlDocumentsRepository.convertValueToPO(parentLinkValue, parentLinkColumnName, parentLinkWidgetType, targetClass);
            sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
            sqlWhereClauseBuilder.append(" /* parent link */ ").append(linkColumnName).append("=").append(sqlParams.placeholder(sqlParentLinkValue));
        }
    }
    // 
    // Document filters
    {
        final String sqlFilters = SqlDocumentFilterConverters.createEntityBindingEffectiveConverter(entityBinding).getSql(sqlParams, getDocumentFilters(), SqlOptions.usingTableAlias(entityBinding.getTableAlias()));
        if (!Check.isEmpty(sqlFilters, true)) {
            sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
            sqlWhereClauseBuilder.append(" /* filters */ (\n").append(sqlFilters).append(")\n");
        }
    }
    // Build the final SQL where clause
    return ImmutablePair.of(sqlWhereClauseBuilder.build(), Collections.unmodifiableList(sqlParams.toList()));
}
Also used : IPair(org.adempiere.util.lang.IPair) SqlDocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) Env(org.compiere.util.Env) ArrayList(java.util.ArrayList) IStringExpression(org.adempiere.ad.expression.api.IStringExpression) Evaluatee(org.compiere.util.Evaluatee) SqlDocumentFilterConverters(de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverters) ImmutableList(com.google.common.collect.ImmutableList) SqlEntityFieldBinding(de.metas.ui.web.window.descriptor.sql.SqlEntityFieldBinding) SqlDocumentEntityDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor) IDocumentFieldView(de.metas.ui.web.window.model.IDocumentFieldView) Map(java.util.Map) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor) DocumentFilter(de.metas.ui.web.document.filter.DocumentFilter) DocumentQueryOrderBy(de.metas.ui.web.window.model.DocumentQueryOrderBy) OnVariableNotFound(org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound) Properties(java.util.Properties) WindowConstants(de.metas.ui.web.window.WindowConstants) UserRolePermissionsKey(org.adempiere.ad.security.UserRolePermissionsKey) Evaluatees(org.compiere.util.Evaluatees) ImmutableMap(com.google.common.collect.ImmutableMap) AccessSqlStringExpression(org.adempiere.ad.security.impl.AccessSqlStringExpression) MoreObjects(com.google.common.base.MoreObjects) Objects(java.util.Objects) DocumentFieldDescriptor(de.metas.ui.web.window.descriptor.DocumentFieldDescriptor) List(java.util.List) ImmutablePair(org.adempiere.util.lang.ImmutablePair) AdempiereException(org.adempiere.exceptions.AdempiereException) Check(org.adempiere.util.Check) DocumentFieldWidgetType(de.metas.ui.web.window.descriptor.DocumentFieldWidgetType) DocumentQuery(de.metas.ui.web.window.model.DocumentQuery) LookupValueByIdSupplier(de.metas.ui.web.window.model.lookup.LookupValueByIdSupplier) SqlParamsCollector(de.metas.ui.web.document.filter.sql.SqlParamsCollector) Collections(java.util.Collections) Document(de.metas.ui.web.window.model.Document) CompositeStringExpression(org.adempiere.ad.expression.api.impl.CompositeStringExpression) CompositeStringExpression(org.adempiere.ad.expression.api.impl.CompositeStringExpression) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) Document(de.metas.ui.web.window.model.Document) IDocumentFieldView(de.metas.ui.web.window.model.IDocumentFieldView) SqlDocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor) DocumentFieldWidgetType(de.metas.ui.web.window.descriptor.DocumentFieldWidgetType) AdempiereException(org.adempiere.exceptions.AdempiereException) IStringExpression(org.adempiere.ad.expression.api.IStringExpression) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SqlParamsCollector(de.metas.ui.web.document.filter.sql.SqlParamsCollector)

Example 7 with IStringExpression

use of org.adempiere.ad.expression.api.IStringExpression in project metasfresh-webui-api by metasfresh.

the class SqlDocumentQueryBuilder method buildSql.

private final IPair<IStringExpression, List<Object>> buildSql() {
    final List<Object> sqlParams = new ArrayList<>();
    final CompositeStringExpression.Builder sqlBuilder = IStringExpression.composer();
    // 
    // SELECT ... FROM ...
    sqlBuilder.append(getSqlSelectFrom());
    // NOTE: no need to add security here because it was already embedded in SqlSelectFrom
    // 
    // WHERE
    {
        final IPair<IStringExpression, List<Object>> sqlWhereClauseAndParams = getSqlWhereAndParams();
        final IStringExpression sqlWhereClause = sqlWhereClauseAndParams.getLeft();
        if (!sqlWhereClause.isNullExpression()) {
            sqlBuilder.append("\n WHERE ").append(sqlWhereClause);
            sqlParams.addAll(sqlWhereClauseAndParams.getRight());
        }
    }
    // ORDER BY
    if (isSorting()) {
        final IStringExpression sqlOrderBy = getSqlOrderByEffective();
        if (sqlOrderBy != null && !sqlOrderBy.isNullExpression()) {
            sqlBuilder.append("\n ORDER BY ").append(sqlOrderBy);
        }
    }
    // 
    // LIMIT/OFFSET
    {
        final int firstRow = getFirstRow();
        if (firstRow > 0) {
            sqlBuilder.append("\n OFFSET ?");
            sqlParams.add(firstRow);
        }
        final int pageLength = getPageLength();
        if (pageLength > 0) {
            sqlBuilder.append("\n LIMIT ?");
            sqlParams.add(pageLength);
        }
    }
    // 
    return ImmutablePair.of(sqlBuilder.build(), Collections.unmodifiableList(sqlParams));
}
Also used : CompositeStringExpression(org.adempiere.ad.expression.api.impl.CompositeStringExpression) ArrayList(java.util.ArrayList) IPair(org.adempiere.util.lang.IPair) IStringExpression(org.adempiere.ad.expression.api.IStringExpression)

Example 8 with IStringExpression

use of org.adempiere.ad.expression.api.IStringExpression in project metasfresh-webui-api by metasfresh.

the class SqlDocumentQueryBuilder method getSql.

/**
 * @return SQL to fully load the documents matched by this query.
 */
public String getSql(final List<Object> outSqlParams) {
    final Evaluatee evalCtx = getEvaluationContext();
    final IPair<IStringExpression, List<Object>> sqlAndParams = getSqlAndParams();
    final String sql = sqlAndParams.getLeft().evaluate(evalCtx, OnVariableNotFound.Fail);
    final List<Object> sqlParams = sqlAndParams.getRight();
    outSqlParams.addAll(sqlParams);
    return sql;
}
Also used : ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) IStringExpression(org.adempiere.ad.expression.api.IStringExpression) Evaluatee(org.compiere.util.Evaluatee)

Example 9 with IStringExpression

use of org.adempiere.ad.expression.api.IStringExpression in project metasfresh-webui-api by metasfresh.

the class SqlDocumentQueryBuilder method getSqlWhere.

private String getSqlWhere(final List<Object> sqlParams) {
    final IPair<IStringExpression, List<Object>> sqlWhereAndParams = getSqlWhereAndParams();
    final Evaluatee evalCtx = getEvaluationContext();
    final String sqlWhere = sqlWhereAndParams.getLeft().evaluate(evalCtx, OnVariableNotFound.Fail);
    sqlParams.addAll(sqlWhereAndParams.getRight());
    return sqlWhere;
}
Also used : ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) IStringExpression(org.adempiere.ad.expression.api.IStringExpression) Evaluatee(org.compiere.util.Evaluatee)

Example 10 with IStringExpression

use of org.adempiere.ad.expression.api.IStringExpression in project metasfresh-webui-api by metasfresh.

the class SqlDocumentQueryBuilder method getSqlSelectParentId.

/**
 * SQL to fetch the parent's ID based on given child document.
 *
 * @return SELECT KeyColumnName from ParentTableName WHERE ....
 */
public String getSqlSelectParentId(final List<Object> outSqlParams, final DocumentEntityDescriptor parentEntityDescriptor) {
    final String linkColumnName = entityBinding.getLinkColumnName();
    final String parentLinkColumnName = entityBinding.getParentLinkColumnName();
    if (parentLinkColumnName == null || linkColumnName == null) {
        throw new AdempiereException("Selecting parent ID is not possible because this entity does not have a parent link").setParameter("linkColumnName", linkColumnName).setParameter("parentLinkColumnName", parentLinkColumnName).setParameter("entityBinding", entityBinding);
    }
    // 
    // SELECT linkColumnName from current(child) tableName
    final List<Object> sqlSelectLinkColumnNameParams = new ArrayList<>();
    final CompositeStringExpression.Builder sqlSelectLinkColumnName = IStringExpression.composer();
    {
        final IPair<IStringExpression, List<Object>> sqlWhereAndParams = getSqlWhereAndParams();
        final IStringExpression sqlWhere = sqlWhereAndParams.getLeft();
        final List<Object> sqlWhereParams = sqlWhereAndParams.getRight();
        sqlSelectLinkColumnName.append("SELECT " + linkColumnName).append(// NOTE: we need table alias because the where clause is using it
        " FROM " + entityBinding.getTableName() + " " + entityBinding.getTableAlias()).append("\n WHERE ").append(sqlWhere);
        sqlSelectLinkColumnNameParams.addAll(sqlWhereParams);
    }
    // 
    // 
    final String parentKeyColumnName = extractSingleKeyColumnName(parentEntityDescriptor);
    if (Objects.equals(parentKeyColumnName, parentLinkColumnName)) {
        final Evaluatee evalCtx = getEvaluationContext();
        final String sql = sqlSelectLinkColumnName.build().evaluate(evalCtx, OnVariableNotFound.Fail);
        outSqlParams.addAll(sqlSelectLinkColumnNameParams);
        return sql;
    } else {
        final Evaluatee evalCtx = getEvaluationContext();
        final String sql = IStringExpression.composer().append("SELECT " + parentKeyColumnName + " FROM " + parentEntityDescriptor.getTableName()).append("\n WHERE " + parentLinkColumnName + " IN (").append(sqlSelectLinkColumnName).append(")").build().evaluate(evalCtx, OnVariableNotFound.Fail);
        outSqlParams.addAll(sqlSelectLinkColumnNameParams);
        return sql;
    }
}
Also used : AdempiereException(org.adempiere.exceptions.AdempiereException) CompositeStringExpression(org.adempiere.ad.expression.api.impl.CompositeStringExpression) ArrayList(java.util.ArrayList) IPair(org.adempiere.util.lang.IPair) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) IStringExpression(org.adempiere.ad.expression.api.IStringExpression) Evaluatee(org.compiere.util.Evaluatee)

Aggregations

IStringExpression (org.adempiere.ad.expression.api.IStringExpression)15 ArrayList (java.util.ArrayList)9 CompositeStringExpression (org.adempiere.ad.expression.api.impl.CompositeStringExpression)7 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 Evaluatee (org.compiere.util.Evaluatee)5 Builder (lombok.Builder)4 AdempiereException (org.adempiere.exceptions.AdempiereException)4 DateTimeTranslatableString (de.metas.i18n.DateTimeTranslatableString)3 ITranslatableString (de.metas.i18n.ITranslatableString)3 ImmutableTranslatableString (de.metas.i18n.ImmutableTranslatableString)3 NumberTranslatableString (de.metas.i18n.NumberTranslatableString)3 IPair (org.adempiere.util.lang.IPair)3 DocumentFilter (de.metas.ui.web.document.filter.DocumentFilter)2 SqlDocumentFilterConverters (de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverters)2 SqlParamsCollector (de.metas.ui.web.document.filter.sql.SqlParamsCollector)2 DocumentId (de.metas.ui.web.window.datatypes.DocumentId)2 SqlLookupDescriptor (de.metas.ui.web.window.descriptor.sql.SqlLookupDescriptor)2 DocumentQueryOrderBy (de.metas.ui.web.window.model.DocumentQueryOrderBy)2 ToString (lombok.ToString)2