Skip to content

Commit

Permalink
SqlViewGroupingBinding support
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Jun 2, 2017
1 parent cdda84c commit 6a837e8
Show file tree
Hide file tree
Showing 4 changed files with 626 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

import de.metas.logging.LogManager;
import de.metas.ui.web.document.filter.DocumentFilter;
import de.metas.ui.web.view.descriptor.SqlViewBinding;
import de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder;
import de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder.SqlAndParams;
import de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder.SqlCreateSelection;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.ui.web.window.datatypes.WindowId;
import de.metas.ui.web.window.descriptor.sql.SqlEntityBinding;
import de.metas.ui.web.window.model.DocumentQueryOrderBy;
import lombok.NonNull;

Expand Down Expand Up @@ -49,23 +51,23 @@

public class SqlViewRowIdsOrderedSelectionFactory implements ViewRowIdsOrderedSelectionFactory
{
public static final SqlViewRowIdsOrderedSelectionFactory of(final SqlEntityBinding sqlEntityBinding)
public static final SqlViewRowIdsOrderedSelectionFactory of(final SqlViewBinding viewBinding)
{
return new SqlViewRowIdsOrderedSelectionFactory(sqlEntityBinding);
return new SqlViewRowIdsOrderedSelectionFactory(viewBinding);
}

private static final Logger logger = LogManager.getLogger(SqlViewRowIdsOrderedSelectionFactory.class);

private final SqlEntityBinding sqlEntityBinding;
private final SqlViewBinding viewBinding;

private SqlViewRowIdsOrderedSelectionFactory(@NonNull final SqlEntityBinding sqlEntityBinding)
private SqlViewRowIdsOrderedSelectionFactory(@NonNull final SqlViewBinding viewBinding)
{
this.sqlEntityBinding = sqlEntityBinding;
this.viewBinding = viewBinding;
}

private SqlViewSelectionQueryBuilder newSqlViewSelectionQueryBuilder()
{
return SqlViewSelectionQueryBuilder.newInstance(sqlEntityBinding);
return SqlViewSelectionQueryBuilder.newInstance(viewBinding);
}

@Override
Expand All @@ -87,15 +89,28 @@ public ViewRowIdsOrderedSelection createOrderedSelection(final ViewEvaluationCtx

//
//
final List<Object> sqlParams = new ArrayList<>();
final String sql = newSqlViewSelectionQueryBuilder().buildSqlCreateSelectionFrom(sqlParams, viewEvalCtx, viewId, filters, orderBys, queryLimit);
final SqlCreateSelection sqlCreates = newSqlViewSelectionQueryBuilder().buildSqlCreateSelectionFrom(viewEvalCtx, viewId, filters, orderBys, queryLimit);
logger.trace("Creating selection using {}", sqlCreates);

//
// Execute it, so we insert in our T_WEBUI_ViewSelection
final Stopwatch stopwatch = Stopwatch.createStarted();
final long rowsCount = DB.executeUpdateEx(sql, sqlParams.toArray(), ITrx.TRXNAME_ThreadInherited);
stopwatch.stop();
logger.trace("Created selection {}, rowsCount={}, duration={} \n SQL: {} -- {}", viewId, rowsCount, stopwatch, sql, sqlParams);
// Create selection lines if any => insert into T_WEBUI_ViewSelectionLine
if (sqlCreates.getSqlCreateSelectionLines() != null)
{
final SqlAndParams sqlCreateSelectionLines = sqlCreates.getSqlCreateSelectionLines();
final Stopwatch stopwatch = Stopwatch.createStarted();
final long linesCount = DB.executeUpdateEx(sqlCreateSelectionLines.getSql(), sqlCreateSelectionLines.getSqlParamsArray(), ITrx.TRXNAME_ThreadInherited);
logger.trace("Created selection lines {}, linesCount={}, duration={}", viewId, linesCount, stopwatch);
}

//
// Create selection rows => insert into T_WEBUI_ViewSelection
final long rowsCount;
{
final SqlAndParams sqlCreateSelection = sqlCreates.getSqlCreateSelection();
final Stopwatch stopwatch = Stopwatch.createStarted();
rowsCount = DB.executeUpdateEx(sqlCreateSelection.getSql(), sqlCreateSelection.getSqlParamsArray(), ITrx.TRXNAME_ThreadInherited);
logger.trace("Created selection {}, rowsCount={}, duration={}", viewId, rowsCount, stopwatch);
}

return ViewRowIdsOrderedSelection.builder()
.setViewId(viewId)
Expand All @@ -110,13 +125,30 @@ public ViewRowIdsOrderedSelection createOrderedSelectionFromSelection(final View
{
final WindowId windowId = fromSelection.getWindowId();
final String fromSelectionId = fromSelection.getSelectionId();

final ViewId newViewId = ViewId.random(windowId);
final String newSelectionId = newViewId.getViewId();

final String sqlFinal = newSqlViewSelectionQueryBuilder().buildSqlCreateSelectionFromSelection(viewEvalCtx, orderBys);
final int rowsCount;
final SqlViewSelectionQueryBuilder viewQueryBuilder = newSqlViewSelectionQueryBuilder();
if (viewQueryBuilder.hasGroupingFields())
{
final SqlAndParams sqlCreateSelectionLines = viewQueryBuilder.buildSqlCreateSelectionLinesFromSelectionLines(viewEvalCtx, newViewId, fromSelectionId);
final int linesCount = DB.executeUpdateEx(sqlCreateSelectionLines.getSql(), sqlCreateSelectionLines.getSqlParamsArray(), ITrx.TRXNAME_ThreadInherited);

final int rowsCount = DB.executeUpdateEx(sqlFinal, new Object[] { newSelectionId, fromSelectionId }, ITrx.TRXNAME_ThreadInherited);
if (linesCount > 0)
{
final SqlAndParams sqlCreateSelection = viewQueryBuilder.buildSqlCreateSelectionFromSelectionLines(viewEvalCtx, newViewId, orderBys);
rowsCount = DB.executeUpdateEx(sqlCreateSelection.getSql(), sqlCreateSelection.getSqlParamsArray(), ITrx.TRXNAME_ThreadInherited);
}
else
{
rowsCount = 0;
}
}
else
{
final SqlAndParams sqlCreateSelection = viewQueryBuilder.buildSqlCreateSelectionFromSelection(viewEvalCtx, newViewId, fromSelectionId, orderBys);
rowsCount = DB.executeUpdateEx(sqlCreateSelection.getSql(), sqlCreateSelection.getSqlParamsArray(), ITrx.TRXNAME_ThreadInherited);
}

return ViewRowIdsOrderedSelection.builder()
.setViewId(newViewId)
Expand All @@ -134,7 +166,7 @@ public ViewRowIdsOrderedSelection addRowIdsToSelection(final ViewRowIdsOrderedSe
// nothing changed
return selection;
}
else if(rowIds.isAll())
else if (rowIds.isAll())
{
throw new IllegalArgumentException("Cannot add ALL to selection");
}
Expand Down Expand Up @@ -212,11 +244,11 @@ private final int retrieveSize(final String selectionId)

public boolean containsAnyOfRowIds(final String selectionId, final DocumentIdsSelection rowIds)
{
if(rowIds.isEmpty())
if (rowIds.isEmpty())
{
return false;
}

final List<Object> sqlParams = new ArrayList<>();
final String sqlCount = newSqlViewSelectionQueryBuilder().buildSqlCount(sqlParams, selectionId, rowIds);
final int count = DB.executeUpdateEx(sqlCount, sqlParams.toArray(), ITrx.TRXNAME_ThreadInherited);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Nullable;

import org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound;
import org.adempiere.ad.expression.api.IStringExpression;
import org.adempiere.ad.expression.api.impl.ConstantStringExpression;
import org.adempiere.util.Check;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -72,6 +76,8 @@ public static final Builder builder()
private final DocumentFilterDescriptorsProvider viewFilterDescriptors;
private final SqlDocumentFilterConvertersList viewFilterConverters;

private final SqlViewGroupingBinding groupingBinding;

private SqlViewBinding(final Builder builder)
{
super();
Expand All @@ -84,7 +90,8 @@ private SqlViewBinding(final Builder builder)
final Collection<String> displayFieldNames = builder.getDisplayFieldNames();

final Collection<SqlViewRowFieldBinding> allFields = _fieldsByFieldName.values();
final IStringExpression sqlSelect = SqlViewSelectionQueryBuilder.buildSqlSelect(_tableName, _tableAlias, _keyField.getColumnName(), displayFieldNames, allFields);
this.groupingBinding = builder.groupingBinding;
final IStringExpression sqlSelect = SqlViewSelectionQueryBuilder.buildSqlSelect(_tableName, _tableAlias, _keyField.getColumnName(), displayFieldNames, allFields, groupingBinding);

sqlWhereClause = builder.getSqlWhereClause();
sqlSelectByPage = sqlSelect.toComposer()
Expand Down Expand Up @@ -156,6 +163,15 @@ public String getKeyColumnName()
return getKeyField().getColumnName();
}

public boolean isKeyFieldName(final String fieldName)
{
if (_keyField == null)
{
return false;
}
return Objects.equal(fieldName, _keyField.getFieldName());
}

public Collection<SqlViewRowFieldBinding> getFields()
{
return _fieldsByFieldName.values();
Expand Down Expand Up @@ -227,6 +243,44 @@ public Map<String, String> getSqlOrderBysIndexedByFieldName(final ViewEvaluation
return sqlOrderBysIndexedByFieldName.build();
}

public Set<String> getGroupByFieldNames()
{
if (groupingBinding == null)
{
return ImmutableSet.of();
}
return groupingBinding.getGroupByFieldNames();
}

public boolean hasGroupingFields()
{
return !getGroupByFieldNames().isEmpty();
}

public boolean isGroupBy(final String fieldName)
{
return getGroupByFieldNames().contains(fieldName);
}

@Nullable
public String getSqlAggregatedColumn(final String fieldName)
{
if (groupingBinding == null)
{
return null;
}
return groupingBinding.getColumnSqlByFieldName(fieldName);
}

public boolean isAggregated(final String fieldName)
{
if (groupingBinding == null)
{
return false;
}
return groupingBinding.isAggregated(fieldName);
}

//
//
//
Expand All @@ -247,6 +301,8 @@ public static final class Builder
private DocumentFilterDescriptorsProvider viewFilterDescriptors = NullDocumentFilterDescriptorsProvider.instance;
private SqlDocumentFilterConvertersList.Builder viewFilterConverters = null;

private SqlViewGroupingBinding groupingBinding;

private Builder()
{
super();
Expand Down Expand Up @@ -387,6 +443,12 @@ private SqlDocumentFilterConvertersList buildViewFilterConverters()
}
return viewFilterConverters.build();
}

public Builder setGroupingBinding(SqlViewGroupingBinding groupingBinding)
{
this.groupingBinding = groupingBinding;
return this;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package de.metas.ui.web.view.descriptor;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import lombok.Builder;
import lombok.Singular;
import lombok.ToString;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@Builder
@ToString
public final class SqlViewGroupingBinding
{
@Singular("groupBy")
private final ImmutableSet<String> groupByFieldNames;
@Singular("columnSql")
private final ImmutableMap<String, String> columnSqlByFieldName;

public ImmutableSet<String> getGroupByFieldNames()
{
return groupByFieldNames;
}

public boolean isGroupBy(final String fieldName)
{
return groupByFieldNames.contains(fieldName);
}

public String getColumnSqlByFieldName(final String fieldName)
{
return columnSqlByFieldName.get(fieldName);
}

public boolean isAggregated(final String fieldName)
{
return columnSqlByFieldName.containsKey(fieldName);
}

}
Loading

0 comments on commit 6a837e8

Please sign in to comment.