Skip to content

Commit

Permalink
moved everything related to I_T_WEBUI_ViewSelection to SqlViewSelecti…
Browse files Browse the repository at this point in the history
…onQueryBuilder

#424
  • Loading branch information
teosarca committed Jun 1, 2017
1 parent af4eab2 commit 9185a1f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 68 deletions.
73 changes: 6 additions & 67 deletions src/main/java/de/metas/ui/web/view/descriptor/SqlViewBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@

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

import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import de.metas.ui.web.base.model.I_T_WEBUI_ViewSelection;
import de.metas.ui.web.document.filter.DocumentFilterDescriptorsProvider;
import de.metas.ui.web.document.filter.NullDocumentFilterDescriptorsProvider;
import de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverter;
Expand Down Expand Up @@ -60,12 +57,6 @@ public static final Builder builder()
return new Builder();
}

//
// Paging constants
private static final String COLUMNNAME_Paging_UUID = "_sel_UUID";
private static final String COLUMNNAME_Paging_SeqNo = "_sel_SeqNo";
private static final String COLUMNNAME_Paging_Record_ID = "_sel_Record_ID";

private final String _tableName;
private final String _tableAlias;

Expand Down Expand Up @@ -93,20 +84,20 @@ private SqlViewBinding(final Builder builder)
final Collection<String> displayFieldNames = builder.getDisplayFieldNames();

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

sqlWhereClause = builder.getSqlWhereClause();
sqlSelectByPage = sqlSelect.toComposer()
.append("\n WHERE ")
.append("\n " + SqlViewBinding.COLUMNNAME_Paging_UUID + "=?")
.append("\n AND " + SqlViewBinding.COLUMNNAME_Paging_SeqNo + " BETWEEN ? AND ?")
.append("\n ORDER BY " + SqlViewBinding.COLUMNNAME_Paging_SeqNo)
.append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_UUID + "=?")
.append("\n AND " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_SeqNo + " BETWEEN ? AND ?")
.append("\n ORDER BY " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_SeqNo)
.build();

sqlSelectById = sqlSelect.toComposer()
.append("\n WHERE ")
.append("\n " + SqlViewBinding.COLUMNNAME_Paging_UUID + "=?")
.append("\n AND " + SqlViewBinding.COLUMNNAME_Paging_Record_ID + "=?")
.append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_UUID + "=?")
.append("\n AND " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_Record_ID + "=?")
.build();

final List<SqlViewRowFieldLoader> rowFieldLoaders = new ArrayList<>(allFields.size());
Expand Down Expand Up @@ -181,58 +172,6 @@ public SqlViewRowFieldBinding getFieldByFieldName(final String fieldName)
return field;
}

private static IStringExpression buildSqlSelect( //
final String sqlTableName //
, final String sqlTableAlias //
, final String sqlKeyColumnName //
, final Collection<String> displayFieldNames //
, final Collection<SqlViewRowFieldBinding> allFields //
)
{
// final String sqlTableName = getTableName();
// final String sqlTableAlias = getTableAlias();
// final String sqlKeyColumnName = getKeyField().getColumnName();

final List<String> sqlSelectValuesList = new ArrayList<>();
final List<IStringExpression> sqlSelectDisplayNamesList = new ArrayList<>();
allFields.forEach(field -> {
// Collect the SQL select for internal value
// NOTE: we need to collect all fields because, even if the field is not needed it might be present in some where clause
sqlSelectValuesList.add(field.getSqlSelectValue());

// Collect the SQL select for displayed value,
// * if there is one
// * and if it was required by caller (i.e. present in fieldNames list)
if (field.isUsingDisplayColumn() && displayFieldNames.contains(field.getFieldName()))
{
sqlSelectDisplayNamesList.add(field.getSqlSelectDisplayValue());
}
});

// NOTE: we don't need access SQL here because we assume the records were already filtered

final CompositeStringExpression.Builder sql = IStringExpression.composer();
sql.append("SELECT ")
.append("\n").append(sqlTableAlias).append(".*"); // Value fields

if (!sqlSelectDisplayNamesList.isEmpty())
{
sql.append(", \n").appendAllJoining("\n, ", sqlSelectDisplayNamesList); // DisplayName fields
}

sql.append("\n FROM (")
.append("\n SELECT ")
.append("\n ").append(Joiner.on("\n , ").join(sqlSelectValuesList))
.append("\n , sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_Line + " AS " + COLUMNNAME_Paging_SeqNo)
.append("\n , sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + " AS " + COLUMNNAME_Paging_UUID)
.append("\n , sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_Record_ID + " AS " + COLUMNNAME_Paging_Record_ID)
.append("\n FROM " + I_T_WEBUI_ViewSelection.Table_Name + " sel")
.append("\n LEFT OUTER JOIN " + sqlTableName + " ON (" + sqlTableName + "." + sqlKeyColumnName + " = sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_Record_ID + ")")
.append("\n ) " + sqlTableAlias); // FROM

return sql.build().caching();
}

@Override
public IStringExpression getSqlWhereClause()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.metas.ui.web.view.descriptor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

Expand All @@ -17,6 +18,7 @@
import org.compiere.util.DB;
import org.slf4j.Logger;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

import de.metas.logging.LogManager;
Expand Down Expand Up @@ -61,7 +63,6 @@
*
* @see I_T_WEBUI_ViewSelection
*/
// TODO: used only in one place.. consider removing it from here
public final class SqlViewSelectionQueryBuilder
{
private static final transient Logger logger = LogManager.getLogger(SqlViewSelectionQueryBuilder.class);
Expand All @@ -70,6 +71,12 @@ public static final SqlViewSelectionQueryBuilder newInstance(final SqlEntityBind
{
return new SqlViewSelectionQueryBuilder(entityBinding);
}

//
// Paging constants
public static final String COLUMNNAME_Paging_UUID = "_sel_UUID";
public static final String COLUMNNAME_Paging_SeqNo = "_sel_SeqNo";
public static final String COLUMNNAME_Paging_Record_ID = "_sel_Record_ID";

private final SqlEntityBinding entityBinding;

Expand Down Expand Up @@ -372,5 +379,58 @@ public <T> IQueryFilter<T> buildInSelectionQueryFilter(final String selectionId)

return TypedSqlQueryFilter.of(sql, sqlParams);
}

public static IStringExpression buildSqlSelect( //
final String sqlTableName //
, final String sqlTableAlias //
, final String sqlKeyColumnName //
, final Collection<String> displayFieldNames //
, final Collection<SqlViewRowFieldBinding> allFields //
)
{
// final String sqlTableName = getTableName();
// final String sqlTableAlias = getTableAlias();
// final String sqlKeyColumnName = getKeyField().getColumnName();

final List<String> sqlSelectValuesList = new ArrayList<>();
final List<IStringExpression> sqlSelectDisplayNamesList = new ArrayList<>();
allFields.forEach(field -> {
// Collect the SQL select for internal value
// NOTE: we need to collect all fields because, even if the field is not needed it might be present in some where clause
sqlSelectValuesList.add(field.getSqlSelectValue());

// Collect the SQL select for displayed value,
// * if there is one
// * and if it was required by caller (i.e. present in fieldNames list)
if (field.isUsingDisplayColumn() && displayFieldNames.contains(field.getFieldName()))
{
sqlSelectDisplayNamesList.add(field.getSqlSelectDisplayValue());
}
});

// NOTE: we don't need access SQL here because we assume the records were already filtered

final CompositeStringExpression.Builder sql = IStringExpression.composer();
sql.append("SELECT ")
.append("\n").append(sqlTableAlias).append(".*"); // Value fields

if (!sqlSelectDisplayNamesList.isEmpty())
{
sql.append(", \n").appendAllJoining("\n, ", sqlSelectDisplayNamesList); // DisplayName fields
}

sql.append("\n FROM (")
.append("\n SELECT ")
.append("\n ").append(Joiner.on("\n , ").join(sqlSelectValuesList))
.append("\n , sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_Line + " AS " + COLUMNNAME_Paging_SeqNo)
.append("\n , sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + " AS " + COLUMNNAME_Paging_UUID)
.append("\n , sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_Record_ID + " AS " + COLUMNNAME_Paging_Record_ID)
.append("\n FROM " + I_T_WEBUI_ViewSelection.Table_Name + " sel")
.append("\n LEFT OUTER JOIN " + sqlTableName + " ON (" + sqlTableName + "." + sqlKeyColumnName + " = sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_Record_ID + ")")
.append("\n ) " + sqlTableAlias); // FROM

return sql.build().caching();
}


}

0 comments on commit 9185a1f

Please sign in to comment.