Skip to content

Commit

Permalink
load included lines together with the document
Browse files Browse the repository at this point in the history
also removed GET
/rest/api/documentView/{windowId}/{viewId}/{rowId}/includedRows because
it's no longer needed.

#424
  • Loading branch information
teosarca committed Jun 8, 2017
1 parent 9db4b6f commit e768de1
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 100 deletions.
10 changes: 0 additions & 10 deletions src/main/java/de/metas/ui/web/view/DefaultView.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,6 @@ public ViewResult getPage(final int firstRow, final int pageLength, final List<D
return ViewResult.ofViewAndPage(this, firstRow, pageLength, orderedSelection.getOrderBys(), page);
}

@Override
public List<IViewRow> getIncludedRows(final DocumentId rowId, final List<DocumentQueryOrderBy> orderBys)
{
final ViewEvaluationCtx evalCtx = ViewEvaluationCtx.of(Env.getCtx());
final ViewRowIdsOrderedSelection orderedSelection = getOrderedSelection(orderBys);

final List<IViewRow> lines = viewDataRepository.retrieveRowLines(evalCtx, orderedSelection, rowId);
return lines;
}

@Override
public IViewRow getById(final DocumentId rowId)
{
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/de/metas/ui/web/view/IView.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,11 @@ default boolean isIncludedView()

ViewResult getPage(int firstRow, int pageLength, List<DocumentQueryOrderBy> orderBys);

default List<? extends IViewRow> getIncludedRows(final DocumentId rowId, final List<DocumentQueryOrderBy> orderBys)
{
// TODO: apply the sorting.
// NOTE: not so important because this method is not used....
return getById(rowId).getIncludedRows();
}

IViewRow getById(DocumentId rowId) throws EntityNotFoundException;

default List<? extends IViewRow> getByIds(final DocumentIdsSelection rowId)
default List<? extends IViewRow> getByIds(final DocumentIdsSelection rowIds)
{
return streamByIds(rowId).collect(ImmutableList.toImmutableList());
return streamByIds(rowIds).collect(ImmutableList.toImmutableList());
}

LookupValuesList getFilterParameterDropdown(String filterId, String filterParameterName, Evaluatee ctx);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/de/metas/ui/web/view/IViewDataRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,4 @@ public interface IViewDataRepository
ViewRowIdsOrderedSelection createOrderedSelection(ViewEvaluationCtx viewEvalCtx, WindowId windowId, List<DocumentFilter> filters);

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

List<IViewRow> retrieveRowLines(ViewEvaluationCtx viewEvalCtx, ViewRowIdsOrderedSelection orderedSelection, DocumentId rowId);
}
136 changes: 87 additions & 49 deletions src/main/java/de/metas/ui/web/view/SqlViewDataRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.adempiere.ad.dao.IQueryBL;
import org.adempiere.ad.dao.impl.TypedSqlQueryFilter;
Expand All @@ -17,6 +20,8 @@
import org.adempiere.util.Check;
import org.adempiere.util.Services;
import org.compiere.util.DB;
import org.compiere.util.Evaluatee;
import org.compiere.util.Evaluatees;
import org.slf4j.Logger;

import com.google.common.base.MoreObjects;
Expand All @@ -27,9 +32,11 @@
import de.metas.ui.web.document.filter.DocumentFilter;
import de.metas.ui.web.document.filter.DocumentFilterDescriptorsProvider;
import de.metas.ui.web.exceptions.EntityNotFoundException;
import de.metas.ui.web.view.ViewRow.DefaultRowType;
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.SqlViewSelectionQueryBuilder;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.ui.web.window.datatypes.WindowId;
Expand Down Expand Up @@ -65,7 +72,7 @@ class SqlViewDataRepository implements IViewDataRepository
private final String tableName;
private final IStringExpression sqlSelectById;
private final IStringExpression sqlSelectByPage;
private final IStringExpression sqlSelectLinesByRowId;
private final IStringExpression sqlSelectLinesByRowIds;
private final ViewRowIdsOrderedSelectionFactory viewRowIdsOrderedSelectionFactory;
private final DocumentFilterDescriptorsProvider viewFilterDescriptors;
private final List<DocumentQueryOrderBy> defaultOrderBys;
Expand All @@ -78,7 +85,7 @@ class SqlViewDataRepository implements IViewDataRepository
tableName = sqlBindings.getTableName();
sqlSelectById = sqlBindings.getSqlSelectById();
sqlSelectByPage = sqlBindings.getSqlSelectByPage();
sqlSelectLinesByRowId = sqlBindings.getSqlSelectLinesByRowId();
sqlSelectLinesByRowIds = sqlBindings.getSqlSelectLinesByRowIds();
viewFilterDescriptors = sqlBindings.getViewFilterDescriptors();
viewRowIdsOrderedSelectionFactory = SqlViewRowIdsOrderedSelectionFactory.of(sqlBindings);
defaultOrderBys = sqlBindings.getDefaultOrderBys();
Expand Down Expand Up @@ -136,9 +143,7 @@ public ViewRowIdsOrderedSelection createOrderedSelectionFromSelection(final View
@Override
public IViewRow retrieveById(final ViewEvaluationCtx viewEvalCtx, final ViewId viewId, final DocumentId rowId)
{
final WindowId windowId = viewId.getWindowId();
final String viewSelectionId = viewId.getViewId();
final String adLanguage = viewEvalCtx.getAD_Language();

final String sql = sqlSelectById.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail);

Expand All @@ -147,38 +152,27 @@ public IViewRow retrieveById(final ViewEvaluationCtx viewEvalCtx, final ViewId v
ResultSet rs = null;
try
{
final int limit = 2;
pstmt = DB.prepareStatement(sql, ITrx.TRXNAME_ThreadInherited);
pstmt.setMaxRows(1 + 1);
pstmt.setMaxRows(limit);
DB.setParameters(pstmt, sqlParams);

rs = pstmt.executeQuery();

IViewRow firstDocument = null;
while (rs.next())
final List<IViewRow> documents = loadViewRows(rs, viewEvalCtx, viewId, limit);
if (documents.isEmpty())
{
final IViewRow document = loadViewRow(rs, windowId, adLanguage);
if (document == null)
{
continue;
}

if (firstDocument != null)
{
logger.warn("More than one document found for rowId={} in {}. Returning only the first one: {}", rowId, this, firstDocument);
return firstDocument;
}
else
{
firstDocument = document;
}
throw new EntityNotFoundException("No document found for rowId=" + rowId);
}

if (firstDocument == null)
else if (documents.size() > 1)
{
throw new EntityNotFoundException("No document found for rowId=" + rowId);
logger.warn("More than one document found for rowId={} in {}. Returning only the first one from: {}", rowId, this, documents);
return documents.get(0);
}
else
{
return documents.get(0);
}

return firstDocument;
}
catch (final SQLException | DBException e)
{
Expand All @@ -191,27 +185,71 @@ public IViewRow retrieveById(final ViewEvaluationCtx viewEvalCtx, final ViewId v
}
}

private final ImmutableList<IViewRow> loadViewRows(final ResultSet rs, final WindowId windowId, final String adLanguage) throws SQLException
private final ImmutableList<IViewRow> loadViewRows(final ResultSet rs, final ViewEvaluationCtx viewEvalCtx, final ViewId viewId, final int limit) throws SQLException
{
final ImmutableList.Builder<IViewRow> result = ImmutableList.builder();
final Map<DocumentId, ViewRow.Builder> rowBuilders = new LinkedHashMap<>();
final Set<DocumentId> rootRowIds = new HashSet<>();
while (rs.next())
{
final IViewRow row = loadViewRow(rs, windowId, adLanguage);
if (row == null)
final ViewRow.Builder rowBuilder = loadViewRow(rs, viewId.getWindowId(), viewEvalCtx.getAD_Language());
if (rowBuilder == null)
{
continue;
}

result.add(row);
final DocumentId rowId = rowBuilder.getRowId();
rowBuilders.put(rowId, rowBuilder);

if (rowBuilder.isRootRow())
{
rootRowIds.add(rowId);
}

// Enforce limit
if (limit > 0 && limit <= rowBuilders.size())
{
break;
}
}

//
// Load lines
if (!rootRowIds.isEmpty())
{
retrieveRowLines(viewEvalCtx, viewId, DocumentIdsSelection.of(rootRowIds))
.forEach(line -> {
final DocumentId parentId = ViewRow.cast(line).getParentId();
final ViewRow.Builder rowBuilder = rowBuilders.get(parentId);
if (rowBuilder == null)
{
// shall not happen
return;
}

rowBuilder.addIncludedRow(line);
});
}

return result.build();
return rowBuilders.values().stream()
.map(rowBuilder -> rowBuilder.build())
.collect(ImmutableList.toImmutableList());
}

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

final int parentIdInt = rs.getInt(SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_Parent_ID);
if (!rs.wasNull())
{
viewRowBuilder.setParentRowId(DocumentId.of(parentIdInt));
viewRowBuilder.setType(DefaultRowType.Line);
}
else
{
viewRowBuilder.setType(DefaultRowType.Row);
}

for (final Map.Entry<String, SqlViewRowFieldLoader> fieldNameAndLoader : rowFieldLoaders.entrySet())
{
final String fieldName = fieldNameAndLoader.getKey();
Expand All @@ -232,7 +270,7 @@ private IViewRow loadViewRow(final ResultSet rs, final WindowId windowId, final
viewRowBuilder.putFieldValue(fieldName, value);
}

return viewRowBuilder.build();
return viewRowBuilder;
}

@Override
Expand All @@ -250,9 +288,8 @@ public List<IViewRow> retrievePage(final ViewEvaluationCtx viewEvalCtx, final Vi
Check.assume(pageLength > 0, "pageLength > 0 but it was {}", pageLength);

logger.debug("Using: {}", orderedSelection);
final WindowId windowId = orderedSelection.getWindowId();
final String viewSelectionId = orderedSelection.getSelectionId();
final String adLanguage = viewEvalCtx.getAD_Language();
final ViewId viewId = orderedSelection.getViewId();
final String viewSelectionId = viewId.getViewId();

final int firstSeqNo = firstRow + 1; // NOTE: firstRow is 0-based while SeqNo are 1-based
final int lastSeqNo = firstRow + pageLength;
Expand All @@ -269,7 +306,7 @@ public List<IViewRow> retrievePage(final ViewEvaluationCtx viewEvalCtx, final Vi

rs = pstmt.executeQuery();

final List<IViewRow> page = loadViewRows(rs, windowId, adLanguage);
final List<IViewRow> page = loadViewRows(rs, viewEvalCtx, viewId, pageLength);
return page;
}
catch (final SQLException | DBException e)
Expand All @@ -283,19 +320,20 @@ public List<IViewRow> retrievePage(final ViewEvaluationCtx viewEvalCtx, final Vi
}
}

@Override
public List<IViewRow> retrieveRowLines(final ViewEvaluationCtx viewEvalCtx, final ViewRowIdsOrderedSelection orderedSelection, final DocumentId rowId)
private List<IViewRow> retrieveRowLines(final ViewEvaluationCtx viewEvalCtx, final ViewId viewId, final DocumentIdsSelection rowIds)
{
logger.debug("Getting row lines: rowId={} - {}", rowId, this);
logger.debug("Getting row lines: rowId={} - {}", rowIds, this);

logger.debug("Using: {}", orderedSelection);
final WindowId windowId = orderedSelection.getWindowId();
final String viewSelectionId = orderedSelection.getSelectionId();
final String adLanguage = viewEvalCtx.getAD_Language();
logger.debug("Using: {}", viewId);
final String viewSelectionId = viewId.getViewId();

// TODO: apply the ORDER BY from orderedSelection
String sql = sqlSelectLinesByRowId.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail);
final Object[] sqlParams = new Object[] { viewSelectionId, rowId.toInt() };
final String sqlRecordIds = DB.buildSqlList(rowIds.toIntSet());
final Evaluatee viewEvalCtxEffective = Evaluatees.ofSingleton(SqlViewSelectionQueryBuilder.Paging_Record_IDsPlaceholder.getName(), sqlRecordIds)
.andComposeWith(viewEvalCtx.toEvaluatee());

final String sql = sqlSelectLinesByRowIds.evaluate(viewEvalCtxEffective, OnVariableNotFound.Fail);
final Object[] sqlParams = new Object[] { viewSelectionId };
PreparedStatement pstmt = null;
ResultSet rs = null;
try
Expand All @@ -305,7 +343,7 @@ public List<IViewRow> retrieveRowLines(final ViewEvaluationCtx viewEvalCtx, fina

rs = pstmt.executeQuery();

final List<IViewRow> lines = loadViewRows(rs, windowId, adLanguage);
final List<IViewRow> lines = loadViewRows(rs, viewEvalCtx, viewId, -1/* limit */);
return lines;
}
catch (final SQLException | DBException e)
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/de/metas/ui/web/view/ViewRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import de.metas.ui.web.view.json.JSONViewResult;
import de.metas.ui.web.view.json.JSONViewRow;
import de.metas.ui.web.window.controller.WindowRestController;
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.datatypes.json.JSONLookupValuesList;
Expand Down Expand Up @@ -193,24 +192,6 @@ public JSONViewResult getViewData(
return JSONViewResult.of(result);
}

@GetMapping("/{viewId}/{rowId}/includedRows")
public List<JSONViewRow> getIncludedRows(
@PathVariable(PARAM_WindowId) final String windowId //
, @PathVariable("viewId") final String viewIdStr//
, @PathVariable("rowId") final String rowIdStr//
, @RequestParam(name = PARAM_OrderBy, required = false) @ApiParam(PARAM_OrderBy_Description) final String orderBysListStr //
)
{
userSession.assertLoggedIn();

final ViewId viewId = ViewId.of(windowId, viewIdStr);
final DocumentId rowId = DocumentId.of(rowIdStr);
final List<DocumentQueryOrderBy> orderBys = DocumentQueryOrderBy.parseOrderBysList(orderBysListStr);
final List<? extends IViewRow> result = viewsRepo.getView(viewId)
.getIncludedRows(rowId, orderBys);
return JSONViewRow.ofViewRows(result);
}

@GetMapping("/layout")
public ResponseEntity<JSONViewLayout> getViewLayout(
@PathVariable(PARAM_WindowId) final String windowIdStr,
Expand Down
Loading

0 comments on commit e768de1

Please sign in to comment.