Search in sources :

Example 6 with SqlSelectValue

use of de.metas.ui.web.window.descriptor.sql.SqlSelectValue in project metasfresh-webui-api by metasfresh.

the class SqlViewSelectionQueryBuilder method buildSqlCreateSelectionFromSelectionLines.

public SqlAndParams buildSqlCreateSelectionFromSelectionLines(@NonNull final ViewEvaluationCtx viewEvalCtx, @NonNull final ViewId newViewId, @NonNull final DocumentQueryOrderByList 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 SqlOrderByValue.ofColumnName("sl", keyColumnNamesMap.getWebuiSelectionColumnNameForKeyColumnName(fieldName));
        } else if (isGroupBy(fieldName)) {
            return getFieldOrderBy(fieldName);
        } else if (isAggregated(fieldName)) {
            return SqlOrderByValue.builder().sqlSelectValue(getSqlAggregatedColumn(fieldName)).build();
        } else {
            // shall not happen
            return null;
        }
    };
    final DocumentQueryOrderByList orderBysEffective = orderBys.stream().flatMap(this::flatMapEffectiveFieldNames).filter(orderBy -> keyColumnNamesMap.isKeyPartFieldName(orderBy.getFieldName()) || isGroupBy(orderBy.getFieldName()) || isAggregated(orderBy.getFieldName())).collect(DocumentQueryOrderByList.toDocumentQueryOrderByList());
    final String sqlOrderBy = SqlDocumentOrderByBuilder.newInstance(sqlOrderByBindings).joinOnTableNameOrAlias(lineTableAlias).useColumnNameAlias(false).buildSqlOrderBy(orderBysEffective).map(sqlOrderByExpr -> sqlOrderByExpr.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail)).orElseGet(() -> keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated("sl"));
    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) SqlSelectDisplayValue(de.metas.ui.web.window.descriptor.sql.SqlSelectDisplayValue) Access(de.metas.security.permissions.Access) 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) HashSet(java.util.HashSet) IUserRolePermissions(de.metas.security.IUserRolePermissions) IStringExpression(org.adempiere.ad.expression.api.IStringExpression) SqlDocumentFilterConverters(de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverters) DB(org.compiere.util.DB) DocumentFilterList(de.metas.ui.web.document.filter.DocumentFilterList) SqlSelectValue(de.metas.ui.web.window.descriptor.sql.SqlSelectValue) I_T_WEBUI_ViewSelectionLine(de.metas.ui.web.base.model.I_T_WEBUI_ViewSelectionLine) DocumentQueryOrderBy(de.metas.ui.web.window.model.DocumentQueryOrderBy) SqlOrderByValue(de.metas.ui.web.window.descriptor.sql.SqlOrderByValue) Nullable(javax.annotation.Nullable) OnVariableNotFound(org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound) DocumentIdsSelection(de.metas.ui.web.window.datatypes.DocumentIdsSelection) Check(de.metas.util.Check) SqlDocumentFilterConverterContext(de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverterContext) Logger(org.slf4j.Logger) DocumentQueryOrderByList(de.metas.ui.web.window.model.DocumentQueryOrderByList) NonNull(lombok.NonNull) Collection(java.util.Collection) SqlDocumentOrderByBuilder(de.metas.ui.web.window.model.sql.SqlDocumentOrderByBuilder) Set(java.util.Set) AccessSqlStringExpression(de.metas.security.impl.AccessSqlStringExpression) ConstantStringExpression(org.adempiere.ad.expression.api.impl.ConstantStringExpression) IStringExpressionWrapper(org.adempiere.ad.expression.api.IStringExpressionWrapper) 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) 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) DocumentQueryOrderByList(de.metas.ui.web.window.model.DocumentQueryOrderByList)

Aggregations

SqlSelectValue (de.metas.ui.web.window.descriptor.sql.SqlSelectValue)6 LogManager (de.metas.logging.LogManager)3 DocumentFilterList (de.metas.ui.web.document.filter.DocumentFilterList)3 SqlDocumentFilterConverter (de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverter)3 SqlDocumentFilterConverterContext (de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverterContext)3 SqlParamsCollector (de.metas.ui.web.document.filter.sql.SqlParamsCollector)3 ViewId (de.metas.ui.web.view.ViewId)3 SqlOptions (de.metas.ui.web.window.model.sql.SqlOptions)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NonNull (lombok.NonNull)3 AdempiereException (org.adempiere.exceptions.AdempiereException)3 DB (org.compiere.util.DB)3 IUserRolePermissions (de.metas.security.IUserRolePermissions)2 AccessSqlStringExpression (de.metas.security.impl.AccessSqlStringExpression)2 Access (de.metas.security.permissions.Access)2 I_T_WEBUI_ViewSelection (de.metas.ui.web.base.model.I_T_WEBUI_ViewSelection)2 I_T_WEBUI_ViewSelectionLine (de.metas.ui.web.base.model.I_T_WEBUI_ViewSelectionLine)2 SqlDocumentFilterConverters (de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverters)2 ViewEvaluationCtx (de.metas.ui.web.view.ViewEvaluationCtx)2