Skip to content

Commit

Permalink
move code from SqlViewBinding to SqlViewRepository; remove Attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Apr 29, 2017
1 parent 079f428 commit c3ff463
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 160 deletions.
5 changes: 5 additions & 0 deletions src/main/java/de/metas/ui/web/view/SqlViewEvaluationCtx.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public String toString()
.add("permissionsKey", permissionsKey)
.toString();
}

public UserRolePermissionsKey getPermissionsKey()
{
return permissionsKey;
}

public Evaluatee toEvaluatee()
{
Expand Down
55 changes: 44 additions & 11 deletions src/main/java/de/metas/ui/web/view/SqlViewRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound;
import org.adempiere.ad.security.IUserRolePermissions;
import org.adempiere.ad.security.IUserRolePermissionsDAO;
import org.adempiere.ad.security.UserRolePermissionsKey;
import org.adempiere.ad.security.permissions.WindowMaxQueryRecordsConstraint;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.DBException;
import org.adempiere.util.Check;
import org.adempiere.util.Services;
import org.compiere.util.DB;
import org.compiere.util.Evaluatee;
import org.slf4j.Logger;

import com.google.common.base.MoreObjects;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList;

import de.metas.logging.LogManager;
import de.metas.ui.web.exceptions.EntityNotFoundException;
import de.metas.ui.web.view.descriptor.SqlViewBinding;
import de.metas.ui.web.view.descriptor.SqlViewRowFieldBinding;
import de.metas.ui.web.view.descriptor.SqlViewRowFieldBinding.SqlViewRowFieldLoader;
import de.metas.ui.web.view.descriptor.SqlViewRowIdsOrderedSelectionFactory;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.WindowId;
import de.metas.ui.web.window.model.filters.DocumentFilter;
Expand Down Expand Up @@ -76,21 +85,48 @@ public String getSqlWhereClause(final ViewId viewId, final Collection<DocumentId
return sqlBindings.getSqlWhereClause(viewId.getViewId(), rowIds);
}

public IViewRowIdsOrderedSelectionFactory createOrderedSelectionFactory(final SqlViewEvaluationCtx evalCtx)
public IViewRowIdsOrderedSelectionFactory createOrderedSelectionFactory(final SqlViewEvaluationCtx viewEvalCtx)
{
return sqlBindings.createOrderedSelectionFactory(evalCtx);
final String sqlCreateFromViewId = sqlBindings.getSqlCreateSelectionFromSelection();
final Map<String, String> sqlOrderBysByFieldName = sqlBindings.getSqlOrderBysIndexedByFieldName(viewEvalCtx);
return new SqlViewRowIdsOrderedSelectionFactory(sqlCreateFromViewId, sqlOrderBysByFieldName);
}

public ViewRowIdsOrderedSelection createOrderedSelection(final SqlViewEvaluationCtx viewEvalCtx, final WindowId windowId, final List<DocumentFilter> filters)
{
return sqlBindings.createOrderedSelection(viewEvalCtx, windowId, filters);
final ViewId viewId = ViewId.random(windowId);

final UserRolePermissionsKey permissionsKey = viewEvalCtx.getPermissionsKey();
final IUserRolePermissions permissions = Services.get(IUserRolePermissionsDAO.class).retrieveUserRolePermissions(permissionsKey);
final int queryLimit = permissions.getConstraint(WindowMaxQueryRecordsConstraint.class)
.or(WindowMaxQueryRecordsConstraint.DEFAULT)
.getMaxQueryRecordsPerRole();

//
//
final List<Object> sqlParams = new ArrayList<>();
final String sql = sqlBindings.getSqlCreateSelectionFrom(sqlParams, viewEvalCtx, viewId, filters, queryLimit);

//
// 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();
final boolean queryLimitHit = queryLimit > 0 && rowsCount >= queryLimit;
logger.trace("Created selection {}, rowsCount={}, duration={} \n SQL: {} -- {}", viewId, rowsCount, stopwatch, sql, sqlParams);

return ViewRowIdsOrderedSelection.builder()
.setViewId(viewId)
.setSize(rowsCount)
.setOrderBys(sqlBindings.getDefaultOrderBys())
.setQueryLimit(queryLimit, queryLimitHit)
.build();
}

public IViewRow retrieveById(final SqlViewEvaluationCtx viewEvalCtx, final ViewId viewId, final DocumentId rowId)
{
final WindowId windowId = viewId.getWindowId();
final String viewSelectionId = viewId.getViewId();
final IViewRowAttributesProvider attributesProvider = null; // TODO

final Evaluatee evalCtx = viewEvalCtx.toEvaluatee();
final String sql = sqlBindings.getSqlSelectById().evaluate(evalCtx, OnVariableNotFound.Fail);
Expand All @@ -109,7 +145,7 @@ public IViewRow retrieveById(final SqlViewEvaluationCtx viewEvalCtx, final ViewI
IViewRow firstDocument = null;
while (rs.next())
{
final IViewRow document = loadViewRow(rs, windowId, attributesProvider);
final IViewRow document = loadViewRow(rs, windowId);
if (document == null)
{
continue;
Expand Down Expand Up @@ -144,10 +180,9 @@ public IViewRow retrieveById(final SqlViewEvaluationCtx viewEvalCtx, final ViewI
}
}

private IViewRow loadViewRow(final ResultSet rs, final WindowId windowId, final IViewRowAttributesProvider attributesProvider) throws SQLException
private IViewRow loadViewRow(final ResultSet rs, final WindowId windowId) throws SQLException
{
final ViewRow.Builder viewRowBuilder = ViewRow.builder(windowId)
.setAttributesProvider(attributesProvider);
final ViewRow.Builder viewRowBuilder = ViewRow.builder(windowId);

for (final SqlViewRowFieldBinding field : sqlBindings.getFields())
{
Expand Down Expand Up @@ -184,8 +219,6 @@ public List<IViewRow> retrievePage(final SqlViewEvaluationCtx viewEvalCtx, final
final WindowId windowId = orderedSelection.getWindowId();
final String viewSelectionId = orderedSelection.getSelectionId();

final IViewRowAttributesProvider attributesProvider = null; // TODO

final int firstSeqNo = firstRow + 1; // NOTE: firstRow is 0-based while SeqNo are 1-based
final int lastSeqNo = firstRow + pageLength;

Expand All @@ -205,7 +238,7 @@ public List<IViewRow> retrievePage(final SqlViewEvaluationCtx viewEvalCtx, final
final ImmutableList.Builder<IViewRow> pageBuilder = ImmutableList.builder();
while (rs.next())
{
final IViewRow row = loadViewRow(rs, windowId, attributesProvider);
final IViewRow row = loadViewRow(rs, windowId);
if (row == null)
{
continue;
Expand Down
51 changes: 10 additions & 41 deletions src/main/java/de/metas/ui/web/view/ViewRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -55,8 +53,6 @@ public static final Builder builder(final WindowId windowId)

private final Map<String, Object> values;

private final IViewRowAttributesProvider attributesProvider;

private final List<IViewRow> includedRows;

private ViewRow(final Builder builder)
Expand All @@ -66,11 +62,9 @@ private ViewRow(final Builder builder)
type = builder.getType();
processed = builder.isProcessed();

values = ImmutableMap.copyOf(builder.values);
values = ImmutableMap.copyOf(builder.getValues());

includedRows = builder.buildIncludedRows();

attributesProvider = builder.getAttributesProviderOrNull();
}

@Override
Expand All @@ -81,7 +75,6 @@ public String toString()
.add("id", rowId)
.add("type", type)
.add("values", values)
.add("attributesProvider", attributesProvider)
.add("includedRows.count", includedRows.size())
.add("processed", processed)
.toString();
Expand Down Expand Up @@ -120,27 +113,13 @@ public Map<String, Object> getFieldNameAndJsonValues()
@Override
public boolean hasAttributes()
{
return attributesProvider != null;
return false;
}

@Override
public IViewRowAttributes getAttributes()
{
if (rowId == null)
{
throw new EntityNotFoundException("row does not support attributes");
}
if (attributesProvider == null)
{
throw new EntityNotFoundException("row does not support attributes");
}

final IViewRowAttributes attributes = attributesProvider.getAttributes(rowId, null);
if (attributes == null)
{
throw new EntityNotFoundException("row does not support attributes");
}
return attributes;
throw new EntityNotFoundException("row does not support attributes");
}

@Override
Expand All @@ -164,16 +143,12 @@ public List<IViewRow> getIncludedRows()
public static final class Builder
{
private final WindowId windowId;
private DocumentId _rowId;
private DocumentId rowId;
private IViewRowType type;
private Boolean processed;

private final Map<String, Object> values = new LinkedHashMap<>();

private final Map<String, Object> values = new LinkedHashMap<>(); // preserve the insertion order of fields
private List<IViewRow> includedRows = null;

private IViewRowAttributesProvider attributesProvider;

private Builder(@NonNull final WindowId windowId)
{
this.windowId = windowId;
Expand All @@ -192,7 +167,7 @@ private DocumentPath getDocumentPath()

public Builder setRowId(final DocumentId rowId)
{
_rowId = rowId;
this.rowId = rowId;
return this;
}

Expand Down Expand Up @@ -232,11 +207,11 @@ else if (jsonRowIdObj instanceof JSONLookupValue)
/** @return view row ID */
private DocumentId getRowId()
{
if (_rowId == null)
if (rowId == null)
{
throw new IllegalStateException("No rowId was provided for " + this);
}
return _rowId;
return rowId;
}

private IViewRowType getType()
Expand Down Expand Up @@ -284,15 +259,9 @@ public Builder putFieldValue(final String fieldName, final Object jsonValue)
return this;
}

private IViewRowAttributesProvider getAttributesProviderOrNull()
private Map<String, Object> getValues()
{
return attributesProvider;
}

public Builder setAttributesProvider(@Nullable final IViewRowAttributesProvider attributesProvider)
{
this.attributesProvider = attributesProvider;
return this;
return values;
}

public Builder addIncludedRow(final IViewRow includedRow)
Expand Down
Loading

0 comments on commit c3ff463

Please sign in to comment.