Search in sources :

Example 6 with SqlAndParams

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);
    }
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ImmutableList(com.google.common.collect.ImmutableList) SqlAndParams(de.metas.ui.web.view.descriptor.SqlAndParams) ResultSet(java.sql.ResultSet) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) PreparedStatement(java.sql.PreparedStatement)

Example 7 with SqlAndParams

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;
}
Also used : SqlAndParams(de.metas.ui.web.view.descriptor.SqlAndParams) WindowMaxQueryRecordsConstraint(org.adempiere.ad.security.permissions.WindowMaxQueryRecordsConstraint)

Example 8 with SqlAndParams

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();
}
Also used : DocumentId(de.metas.ui.web.window.datatypes.DocumentId) SqlAndParams(de.metas.ui.web.view.descriptor.SqlAndParams) WindowMaxQueryRecordsConstraint(org.adempiere.ad.security.permissions.WindowMaxQueryRecordsConstraint)

Example 9 with SqlAndParams

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();
}
Also used : UserRolePermissionsKey(org.adempiere.ad.security.UserRolePermissionsKey) SqlCreateSelection(de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder.SqlCreateSelection) SqlAndParams(de.metas.ui.web.view.descriptor.SqlAndParams) Stopwatch(com.google.common.base.Stopwatch) IUserRolePermissionsDAO(org.adempiere.ad.security.IUserRolePermissionsDAO) IUserRolePermissions(org.adempiere.ad.security.IUserRolePermissions) WindowMaxQueryRecordsConstraint(org.adempiere.ad.security.permissions.WindowMaxQueryRecordsConstraint)

Example 10 with SqlAndParams

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;
}
Also used : SqlAndParams(de.metas.ui.web.view.descriptor.SqlAndParams) WindowMaxQueryRecordsConstraint(org.adempiere.ad.security.permissions.WindowMaxQueryRecordsConstraint)

Aggregations

SqlAndParams (de.metas.ui.web.view.descriptor.SqlAndParams)11 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 WindowMaxQueryRecordsConstraint (org.adempiere.ad.security.permissions.WindowMaxQueryRecordsConstraint)5 DBException (org.adempiere.exceptions.DBException)5 DocumentId (de.metas.ui.web.window.datatypes.DocumentId)3 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 EntityNotFoundException (de.metas.ui.web.exceptions.EntityNotFoundException)1 SqlViewSelectionQueryBuilder (de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder)1 SqlCreateSelection (de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder.SqlCreateSelection)1 WindowId (de.metas.ui.web.window.datatypes.WindowId)1 LinkedHashSet (java.util.LinkedHashSet)1 IUserRolePermissions (org.adempiere.ad.security.IUserRolePermissions)1 IUserRolePermissionsDAO (org.adempiere.ad.security.IUserRolePermissionsDAO)1 UserRolePermissionsKey (org.adempiere.ad.security.UserRolePermissionsKey)1