Search in sources :

Example 1 with ViewEvaluationCtx

use of de.metas.ui.web.view.ViewEvaluationCtx in project metasfresh-webui-api by metasfresh.

the class SqlViewSelectionQueryBuilder method buildSqlCreateSelectionFromSelectionLines.

public SqlAndParams buildSqlCreateSelectionFromSelectionLines(final ViewEvaluationCtx viewEvalCtx, final ViewId newViewId, final List<DocumentQueryOrderBy> orderBys) {
    final String lineTableName = getTableName();
    final String lineTableAlias = getTableAlias();
    final SqlViewKeyColumnNamesMap keyColumnNamesMap = getSqlViewKeyColumnNamesMap();
    final String lineKeyColumnName = keyColumnNamesMap.getSingleKeyColumnName();
    final SqlOrderByBindings sqlOrderByBindings = fieldName -> {
        if (keyColumnNamesMap.isKeyPartFieldName(fieldName)) {
            return ConstantStringExpression.of("sl." + keyColumnNamesMap.getWebuiSelectionColumnNameForKeyColumnName(fieldName));
        } else if (isGroupBy(fieldName)) {
            return getFieldOrderBy(fieldName);
        } else if (isAggregated(fieldName)) {
            return ConstantStringExpression.of(getSqlAggregatedColumn(fieldName));
        } else {
            // shall not happen
            return null;
        }
    };
    final List<DocumentQueryOrderBy> orderBysEffective = orderBys.stream().flatMap(this::flatMapEffectiveFieldNames).filter(orderBy -> keyColumnNamesMap.isKeyPartFieldName(orderBy.getFieldName()) || isGroupBy(orderBy.getFieldName()) || isAggregated(orderBy.getFieldName())).collect(ImmutableList.toImmutableList());
    final IStringExpression sqlOrderByExpr = SqlDocumentOrderByBuilder.newInstance(sqlOrderByBindings).buildSqlOrderBy(orderBysEffective);
    final String sqlOrderBy;
    if (sqlOrderByExpr == null || sqlOrderByExpr.isNullExpression()) {
        sqlOrderBy = keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated("sl");
    } else {
        sqlOrderBy = sqlOrderByExpr.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail);
    }
    final String sqlFrom = "SELECT " + "\n sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_UUID + "\n, " + keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated("sl") + "\n FROM " + I_T_WEBUI_ViewSelectionLine.Table_Name + " sl " + "\n INNER JOIN " + lineTableName + " " + lineTableAlias + " ON (" + lineTableAlias + "." + lineKeyColumnName + " = sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_Line_ID + // join lines
    ")" + "\n WHERE " + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_UUID + "=?" + "\n GROUP BY " + "\n sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_UUID + "\n, " + keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated("sl") + "\n, " + getGroupByFieldNamesCommaSeparated() + "\n ORDER BY " + sqlOrderBy;
    final String sqlCreateSelectionFromLines = "INSERT INTO " + I_T_WEBUI_ViewSelection.Table_Name + "(" + "\n " + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + "\n, " + keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated() + "\n, " + // SeqNo
    I_T_WEBUI_ViewSelection.COLUMNNAME_Line + "\n)" + "\n SELECT " + "\n   sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_UUID + "\n , " + keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated("sl") + // SeqNo
    "\n , row_number() OVER ()" + "\n FROM (" + sqlFrom + ") sl";
    final List<Object> sqlCreateSelectionFromLinesParams = Arrays.asList(newViewId.getViewId());
    return SqlAndParams.of(sqlCreateSelectionFromLines, sqlCreateSelectionFromLinesParams);
}
Also used : LogManager(de.metas.logging.LogManager) Arrays(java.util.Arrays) ViewEvaluationCtx(de.metas.ui.web.view.ViewEvaluationCtx) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) SqlOptions(de.metas.ui.web.window.model.sql.SqlOptions) SqlOrderByBindings(de.metas.ui.web.window.model.sql.SqlDocumentOrderByBuilder.SqlOrderByBindings) I_T_WEBUI_ViewSelection(de.metas.ui.web.base.model.I_T_WEBUI_ViewSelection) ArrayList(java.util.ArrayList) Value(lombok.Value) IStringExpression(org.adempiere.ad.expression.api.IStringExpression) SqlDocumentFilterConverters(de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverters) DB(org.compiere.util.DB) ImmutableList(com.google.common.collect.ImmutableList) I_T_WEBUI_ViewSelectionLine(de.metas.ui.web.base.model.I_T_WEBUI_ViewSelectionLine) DocumentFilter(de.metas.ui.web.document.filter.DocumentFilter) DocumentQueryOrderBy(de.metas.ui.web.window.model.DocumentQueryOrderBy) Nullable(javax.annotation.Nullable) OnVariableNotFound(org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound) DocumentIdsSelection(de.metas.ui.web.window.datatypes.DocumentIdsSelection) Logger(org.slf4j.Logger) NonNull(lombok.NonNull) Collection(java.util.Collection) AccessSqlStringExpression(org.adempiere.ad.security.impl.AccessSqlStringExpression) SqlDocumentOrderByBuilder(de.metas.ui.web.window.model.sql.SqlDocumentOrderByBuilder) Set(java.util.Set) ConstantStringExpression(org.adempiere.ad.expression.api.impl.ConstantStringExpression) IUserRolePermissions(org.adempiere.ad.security.IUserRolePermissions) Collectors(java.util.stream.Collectors) SqlDocumentFilterConverter(de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverter) List(java.util.List) Stream(java.util.stream.Stream) Builder(lombok.Builder) AdempiereException(org.adempiere.exceptions.AdempiereException) Check(org.adempiere.util.Check) SqlParamsCollector(de.metas.ui.web.document.filter.sql.SqlParamsCollector) CompositeStringExpression(org.adempiere.ad.expression.api.impl.CompositeStringExpression) ViewId(de.metas.ui.web.view.ViewId) SqlOrderByBindings(de.metas.ui.web.window.model.sql.SqlDocumentOrderByBuilder.SqlOrderByBindings) DocumentQueryOrderBy(de.metas.ui.web.window.model.DocumentQueryOrderBy) IStringExpression(org.adempiere.ad.expression.api.IStringExpression)

Example 2 with ViewEvaluationCtx

use of de.metas.ui.web.view.ViewEvaluationCtx in project metasfresh-webui-api by metasfresh.

the class HUEditorViewBuffer_HighVolume method huIdsPageFetcher.

private PageFetcher<Integer> huIdsPageFetcher(final List<DocumentQueryOrderBy> orderBys) {
    final ViewEvaluationCtx viewEvalCtx = getViewEvaluationCtx();
    final ViewRowIdsOrderedSelection selection = getSelection(orderBys);
    return (firstRow, maxRows) -> huEditorRepo.retrieveHUIdsPage(viewEvalCtx, selection, firstRow, maxRows);
}
Also used : Iterables(com.google.common.collect.Iterables) ViewRowIdsOrderedSelection(de.metas.ui.web.view.ViewRowIdsOrderedSelection) CCache(org.compiere.util.CCache) ViewEvaluationCtx(de.metas.ui.web.view.ViewEvaluationCtx) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) UnaryOperator(java.util.function.UnaryOperator) HUIdsFilterData(de.metas.ui.web.handlingunits.HUIdsFilterHelper.HUIdsFilterData) Supplier(java.util.function.Supplier) SynchronizedMutable(org.adempiere.util.lang.SynchronizedMutable) Mutables(org.adempiere.util.lang.Mutables) ImmutableList(com.google.common.collect.ImmutableList) IteratorUtils(org.adempiere.util.collections.IteratorUtils) DocumentFilter(de.metas.ui.web.document.filter.DocumentFilter) DocumentQueryOrderBy(de.metas.ui.web.window.model.DocumentQueryOrderBy) I_M_HU(de.metas.handlingunits.model.I_M_HU) PageFetcher(org.adempiere.util.collections.PagedIterator.PageFetcher) DocumentIdsSelection(de.metas.ui.web.window.datatypes.DocumentIdsSelection) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) NonNull(lombok.NonNull) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Objects(java.util.Objects) EntityNotFoundException(de.metas.ui.web.exceptions.EntityNotFoundException) List(java.util.List) Stream(java.util.stream.Stream) ViewId(de.metas.ui.web.view.ViewId) ViewEvaluationCtx(de.metas.ui.web.view.ViewEvaluationCtx) ViewRowIdsOrderedSelection(de.metas.ui.web.view.ViewRowIdsOrderedSelection)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 DocumentFilter (de.metas.ui.web.document.filter.DocumentFilter)2 ViewEvaluationCtx (de.metas.ui.web.view.ViewEvaluationCtx)2 ViewId (de.metas.ui.web.view.ViewId)2 DocumentId (de.metas.ui.web.window.datatypes.DocumentId)2 DocumentIdsSelection (de.metas.ui.web.window.datatypes.DocumentIdsSelection)2 DocumentQueryOrderBy (de.metas.ui.web.window.model.DocumentQueryOrderBy)2 Collection (java.util.Collection)2 List (java.util.List)2 Set (java.util.Set)2 Stream (java.util.stream.Stream)2 NonNull (lombok.NonNull)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 I_M_HU (de.metas.handlingunits.model.I_M_HU)1 LogManager (de.metas.logging.LogManager)1 I_T_WEBUI_ViewSelection (de.metas.ui.web.base.model.I_T_WEBUI_ViewSelection)1 I_T_WEBUI_ViewSelectionLine (de.metas.ui.web.base.model.I_T_WEBUI_ViewSelectionLine)1 SqlDocumentFilterConverter (de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverter)1 SqlDocumentFilterConverters (de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverters)1