use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.
the class SqlViewDataRepository method retrieveRowIdsByPage.
@Override
public List<DocumentId> retrieveRowIdsByPage(final ViewEvaluationCtx viewEvalCtx, final ViewRowIdsOrderedSelection orderedSelection, final int firstRow, final int pageLength) {
logger.debug("Getting page: firstRow={}, pageLength={} - {}", firstRow, pageLength, this);
logger.debug("Using: {}", orderedSelection);
final ViewId viewId = orderedSelection.getViewId();
final SqlAndParams sqlAndParams = sqlViewSelect.selectRowIdsByPage().viewEvalCtx(viewEvalCtx).viewId(viewId).firstRowZeroBased(firstRow).pageLength(pageLength).build();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
pstmt.setMaxRows(pageLength);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());
rs = pstmt.executeQuery();
final ImmutableList.Builder<DocumentId> rowIds = ImmutableList.builder();
// N/A, not important
final String adLanguage = null;
while (rs.next()) {
final DocumentId rowId = retrieveRowId(rs, adLanguage);
if (rowId == null) {
continue;
}
rowIds.add(rowId);
}
return rowIds.build();
} catch (final SQLException | DBException e) {
throw DBException.wrapIfNeeded(e).setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
} finally {
DB.close(rs, pstmt);
}
}
use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.
the class SqlViewRowIdsOrderedSelectionFactory method containsAnyOfRowIds.
@Override
public boolean containsAnyOfRowIds(final ViewRowIdsOrderedSelection selection, final DocumentIdsSelection rowIds) {
if (rowIds.isEmpty()) {
return false;
}
final SqlAndParams sqlCount = newSqlViewSelectionQueryBuilder().buildSqlCount(selection.getSelectionId(), rowIds);
final int count = DB.getSQLValueEx(ITrx.TRXNAME_ThreadInherited, sqlCount.getSql(), sqlCount.getSqlParamsArray());
return count > 0;
}
use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.
the class SqlViewRowIdsOrderedSelectionFactory method addRowIdsToSelection.
@Override
public ViewRowIdsOrderedSelection addRowIdsToSelection(final ViewRowIdsOrderedSelection selection, final DocumentIdsSelection rowIds) {
if (rowIds.isEmpty()) {
// nothing changed
return selection;
} else if (rowIds.isAll()) {
throw new IllegalArgumentException("Cannot add ALL to selection");
}
//
// Add
boolean hasChanges = false;
final String selectionId = selection.getSelectionId();
// TODO: add all rowIds in one query!!! Not so urgent because usually there are added just a couple of rowIds, not much
for (final DocumentId rowId : rowIds.toSet()) {
final SqlAndParams sqlAdd = newSqlViewSelectionQueryBuilder().buildSqlAddRowIdsFromSelection(selectionId, rowId);
final int added = DB.executeUpdateEx(sqlAdd.getSql(), sqlAdd.getSqlParamsArray(), ITrx.TRXNAME_ThreadInherited);
if (added <= 0) {
continue;
}
hasChanges = true;
}
if (!hasChanges) {
// nothing changed
return selection;
}
//
// Retrieve current size
// NOTE: we are querying it instead of adding how many we added to current "size" because it might be that the size is staled
final int size = retrieveSize(selectionId);
return selection.toBuilder().setSize(size).build();
}
use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.
the class SqlViewRowIdsOrderedSelectionFactory method createOrderedSelection.
@Override
public ViewRowIdsOrderedSelection createOrderedSelection(final ViewEvaluationCtx viewEvalCtx, final ViewId viewId, final List<DocumentFilter> filters, final List<DocumentQueryOrderBy> orderBys) {
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 SqlCreateSelection sqlCreates = newSqlViewSelectionQueryBuilder().buildSqlCreateSelectionFrom(viewEvalCtx, viewId, filters, orderBys, queryLimit);
logger.trace("Creating selection using {}", sqlCreates);
// 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).setSize(rowsCount).setOrderBys(orderBys).setQueryLimit(queryLimit).build();
}
use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.
the class SqlViewRowIdsOrderedSelectionFactory method retrieveSize.
private final int retrieveSize(final String selectionId) {
final SqlAndParams sqlCount = newSqlViewSelectionQueryBuilder().buildSqlRetrieveSize(selectionId);
final int size = DB.getSQLValueEx(ITrx.TRXNAME_ThreadInherited, sqlCount.getSql(), sqlCount.getSqlParams());
return size <= 0 ? 0 : size;
}
Aggregations