Skip to content

Commit

Permalink
refactor, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed May 2, 2017
1 parent 40b58b3 commit 6aedae2
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 239 deletions.
31 changes: 20 additions & 11 deletions src/main/java/de/metas/ui/web/handlingunits/HUEditorView.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentPath;
import de.metas.ui.web.window.datatypes.LookupValuesList;
import de.metas.ui.web.window.datatypes.WindowId;
import de.metas.ui.web.window.descriptor.DocumentFieldWidgetType;
import de.metas.ui.web.window.model.DocumentQueryOrderBy;
import de.metas.ui.web.window.model.filters.DocumentFilter;
Expand Down Expand Up @@ -86,19 +87,27 @@ public static HUEditorView cast(final IView view)

private HUEditorView(final Builder builder)
{
super();

parentViewId = builder.getParentViewId();
viewId = builder.getViewId();


this.huAttributesProvider = HUEditorRowAttributesProvider.newInstance();
final HUEditorViewRepository huEditorRepo = HUEditorViewRepository.builder()
.windowId(viewId.getWindowId())
.windowId(builder.getWindowId())
.referencingTableName(builder.getReferencingTableName())
.attributesProvider(huAttributesProvider)
.build();

rowsBuffer = new HUEditorViewBuffer_FullyCached(huEditorRepo, builder.getHUIds());
// final boolean isHighVolume = false; // TODO: high volume is work in progress
// if (isHighVolume)
// {
// final HUEditorViewBuffer_HighVolume rowsBuffer = new HUEditorViewBuffer_HighVolume(builder.getWindowId(), huEditorRepo);
// this.rowsBuffer = rowsBuffer;
// this.viewId = rowsBuffer.getViewId();
// }
// else
{
this.rowsBuffer = new HUEditorViewBuffer_FullyCached(huEditorRepo, builder.getHUIds());
this.viewId = ViewId.random(builder.getWindowId());
}

referencingDocumentPaths = builder.getReferencingDocumentPaths();

Expand Down Expand Up @@ -367,7 +376,7 @@ public SelectViewRowsAction actionSelectHUsByBarcode( //
public static final class Builder
{
private ViewId parentViewId;
private ViewId viewId;
private WindowId windowId;

private String referencingTableName;
private Set<DocumentPath> referencingDocumentPaths;
Expand Down Expand Up @@ -396,15 +405,15 @@ private ViewId getParentViewId()
return parentViewId;
}

public Builder setViewId(final ViewId viewId)
public Builder setWindowId(WindowId windowId)
{
this.viewId = viewId;
this.windowId = windowId;
return this;
}

public ViewId getViewId()
private WindowId getWindowId()
{
return viewId;
return windowId;
}

public Builder setHUIds(final Collection<Integer> huIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import de.metas.ui.web.view.IViewFactory;
import de.metas.ui.web.view.ViewCreateRequest;
import de.metas.ui.web.view.ViewFactory;
import de.metas.ui.web.view.ViewId;
import de.metas.ui.web.view.descriptor.ViewLayout;
import de.metas.ui.web.view.json.JSONViewDataType;
import de.metas.ui.web.window.datatypes.DocumentPath;
Expand Down Expand Up @@ -174,8 +173,6 @@ public HUEditorView createView(final ViewCreateRequest request)
throw new IllegalArgumentException("Invalid request's windowId: " + request);
}

final ViewId viewId = ViewId.random(windowId);

//
// Referencing path and tableName (i.e. from where are we coming, e.g. receipt schedule)
final Set<DocumentPath> referencingDocumentPaths = request.getReferencingDocumentPaths();
Expand All @@ -193,7 +190,7 @@ public HUEditorView createView(final ViewCreateRequest request)

return HUEditorView.builder()
.setParentViewId(request.getParentViewId())
.setViewId(viewId)
.setWindowId(windowId)
.setHUIds(request.getFilterOnlyIds())
.setReferencingDocumentPaths(referencingTableName, referencingDocumentPaths)
.setActions(request.getActions())
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/de/metas/ui/web/view/SqlViewDataRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class SqlViewDataRepository implements IViewDataRepository
private final IStringExpression sqlSelectByPage;
private final ViewRowIdsOrderedSelectionFactory viewRowIdsOrderedSelectionFactory;
private final DocumentFilterDescriptorsProvider viewFilterDescriptors;
private final List<DocumentQueryOrderBy> defaultOrderBys;

private final String keyFieldName;
private final ImmutableMap<String, SqlViewRowFieldLoader> rowFieldLoaders;
Expand All @@ -77,7 +78,8 @@ class SqlViewDataRepository implements IViewDataRepository
sqlSelectById = sqlBindings.getSqlSelectById();
sqlSelectByPage = sqlBindings.getSqlSelectByPage();
viewFilterDescriptors = sqlBindings.getViewFilterDescriptors();
viewRowIdsOrderedSelectionFactory = new SqlViewRowIdsOrderedSelectionFactory(sqlBindings);
viewRowIdsOrderedSelectionFactory = SqlViewRowIdsOrderedSelectionFactory.of(sqlBindings);
defaultOrderBys = sqlBindings.getDefaultOrderBys();

String keyFieldName = null;
final ImmutableMap.Builder<String, SqlViewRowFieldLoader> rowFieldLoaders = ImmutableMap.builder();
Expand Down Expand Up @@ -120,7 +122,7 @@ public String getSqlWhereClause(final ViewId viewId, final Collection<DocumentId
@Override
public ViewRowIdsOrderedSelection createOrderedSelection(final ViewEvaluationCtx viewEvalCtx, final WindowId windowId, final List<DocumentFilter> filters)
{
return viewRowIdsOrderedSelectionFactory.createOrderedSelection(viewEvalCtx, windowId, filters);
return viewRowIdsOrderedSelectionFactory.createOrderedSelection(viewEvalCtx, windowId, filters, defaultOrderBys);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/de/metas/ui/web/view/SqlViewFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ private SqlViewBinding createViewBinding(final SqlViewBindingKey key)
.setTableAlias(entityBinding.getTableAlias())
.setDisplayFieldNames(displayFieldNames)
.setViewFilterDescriptors(filterDescriptors)
.setSqlWhereClause(entityBinding.getSqlWhereClause())
.setOrderBys(entityBinding.getDefaultOrderBys());

entityBinding.getFields()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import java.util.Collection;
import java.util.List;

import org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound;
import org.adempiere.ad.expression.api.IStringExpression;
import org.adempiere.ad.security.IUserRolePermissions;
import org.adempiere.ad.security.IUserRolePermissionsDAO;
import org.adempiere.ad.security.UserRolePermissionsKey;
Expand All @@ -18,12 +16,12 @@
import com.google.common.base.Stopwatch;

import de.metas.logging.LogManager;
import de.metas.ui.web.view.descriptor.SqlViewBinding;
import de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder;
import de.metas.ui.web.window.datatypes.DocumentId;
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 de.metas.ui.web.window.model.filters.DocumentFilter;
import de.metas.ui.web.window.model.sql.SqlDocumentOrderByBuilder;
import lombok.NonNull;

/*
Expand All @@ -36,12 +34,12 @@
* 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>.
Expand All @@ -50,39 +48,33 @@

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

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

private final String sqlCreateFromViewId;
private final List<DocumentQueryOrderBy> defaultOrderBys;
private final SqlViewBinding _sqlBindings;
private final SqlEntityBinding sqlEntityBinding;

SqlViewRowIdsOrderedSelectionFactory(@NonNull final SqlViewBinding sqlBindings)
private SqlViewRowIdsOrderedSelectionFactory(@NonNull final SqlEntityBinding sqlEntityBinding)
{
this._sqlBindings = sqlBindings;
sqlCreateFromViewId = sqlBindings.getSqlCreateSelectionFromSelection();

defaultOrderBys = sqlBindings.getDefaultOrderBys();
this.sqlEntityBinding = sqlEntityBinding;
}

private String getSqlCreateSelectionFrom( //
final List<Object> sqlParams //
, final ViewEvaluationCtx viewEvalCtx //
, final ViewId newViewId //
, final List<DocumentFilter> filters //
, final int queryLimit //
)
private SqlViewSelectionQueryBuilder newSqlViewSelectionQueryBuilder()
{
return _sqlBindings.getSqlCreateSelectionFrom(sqlParams, viewEvalCtx, newViewId, filters, queryLimit);
return SqlViewSelectionQueryBuilder.newInstance(sqlEntityBinding);
}

private final IStringExpression getFieldOrderBy(final String fieldName)
@Override
public String getSqlWhereClause(final ViewId viewId, final Collection<DocumentId> rowIds)
{
return _sqlBindings.getFieldOrderBy(fieldName);
return newSqlViewSelectionQueryBuilder().buildSqlWhereClause(viewId.getViewId(), rowIds);
}

@Override
public ViewRowIdsOrderedSelection createOrderedSelection(final ViewEvaluationCtx viewEvalCtx, final WindowId windowId, final List<DocumentFilter> filters)
public ViewRowIdsOrderedSelection createOrderedSelection(final ViewEvaluationCtx viewEvalCtx, final WindowId windowId, final List<DocumentFilter> filters, final List<DocumentQueryOrderBy> orderBys)
{
final ViewId viewId = ViewId.random(windowId);

Expand All @@ -95,7 +87,7 @@ public ViewRowIdsOrderedSelection createOrderedSelection(final ViewEvaluationCtx
//
//
final List<Object> sqlParams = new ArrayList<>();
final String sql = getSqlCreateSelectionFrom(sqlParams, viewEvalCtx, viewId, filters, queryLimit);
final String sql = newSqlViewSelectionQueryBuilder().buildSqlCreateSelectionFrom(sqlParams, viewEvalCtx, viewId, filters, orderBys, queryLimit);

//
// Execute it, so we insert in our T_WEBUI_ViewSelection
Expand All @@ -107,7 +99,7 @@ public ViewRowIdsOrderedSelection createOrderedSelection(final ViewEvaluationCtx
return ViewRowIdsOrderedSelection.builder()
.setViewId(viewId)
.setSize(rowsCount)
.setOrderBys(defaultOrderBys)
.setOrderBys(orderBys)
.setQueryLimit(queryLimit)
.build();
}
Expand All @@ -121,11 +113,7 @@ public ViewRowIdsOrderedSelection createOrderedSelectionFromSelection(final View
final ViewId newViewId = ViewId.random(windowId);
final String newSelectionId = newViewId.getViewId();

final String sqlOrderBys = SqlDocumentOrderByBuilder.newInstance(this::getFieldOrderBy)
.buildSqlOrderBy(orderBys)
.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail);

final String sqlFinal = sqlCreateFromViewId.replace(SqlViewBinding.PLACEHOLDER_OrderBy, sqlOrderBys);
final String sqlFinal = newSqlViewSelectionQueryBuilder().buildSqlCreateSelectionFromSelection(viewEvalCtx, orderBys);

final int rowsCount = DB.executeUpdateEx(sqlFinal, new Object[] { newSelectionId, fromSelectionId }, ITrx.TRXNAME_ThreadInherited);

Expand All @@ -136,11 +124,4 @@ public ViewRowIdsOrderedSelection createOrderedSelectionFromSelection(final View
.setQueryLimit(fromSelection.getQueryLimit())
.build();
}

@Override
public String getSqlWhereClause(final ViewId viewId, final Collection<DocumentId> rowIds)
{
return _sqlBindings.getSqlWhereClause(viewId.getViewId(), rowIds);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

public interface ViewRowIdsOrderedSelectionFactory
{
ViewRowIdsOrderedSelection createOrderedSelection(ViewEvaluationCtx viewEvalCtx, WindowId windowId, List<DocumentFilter> filters);
ViewRowIdsOrderedSelection createOrderedSelection(ViewEvaluationCtx viewEvalCtx, WindowId windowId, List<DocumentFilter> filters, final List<DocumentQueryOrderBy> orderBys);

ViewRowIdsOrderedSelection createOrderedSelectionFromSelection(ViewEvaluationCtx viewEvalCtx, ViewRowIdsOrderedSelection fromSelection, List<DocumentQueryOrderBy> orderBys);

Expand Down
Loading

1 comment on commit 6aedae2

@teosarca
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also fixes #339

Please sign in to comment.