Search in sources :

Example 1 with SqlAndParams

use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.

the class SqlViewDataRepository method retrievePage.

@Override
public List<IViewRow> retrievePage(final ViewEvaluationCtx viewEvalCtx, final ViewRowIdsOrderedSelection orderedSelection, final int firstRow, final int pageLength) throws DBException {
    logger.debug("Getting page: firstRow={}, pageLength={} - {}", firstRow, pageLength, this);
    logger.debug("Using: {}", orderedSelection);
    final ViewId viewId = orderedSelection.getViewId();
    final SqlAndParams sqlAndParams = sqlViewSelect.selectByPage().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 List<IViewRow> page = loadViewRows(rs, viewEvalCtx, viewId, pageLength);
        return page;
    } 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) SqlAndParams(de.metas.ui.web.view.descriptor.SqlAndParams) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with SqlAndParams

use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.

the class SqlViewDataRepository method retrieveById.

@Override
public IViewRow retrieveById(final ViewEvaluationCtx viewEvalCtx, final ViewId viewId, final DocumentId rowId) {
    final SqlAndParams sqlAndParams = sqlViewSelect.selectById().viewEvalCtx(viewEvalCtx).viewId(viewId).rowId(rowId).build();
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        final int limit = 2;
        pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
        pstmt.setMaxRows(limit);
        DB.setParameters(pstmt, sqlAndParams.getSqlParams());
        rs = pstmt.executeQuery();
        final List<IViewRow> documents = loadViewRows(rs, viewEvalCtx, viewId, limit);
        if (documents.isEmpty()) {
            throw new EntityNotFoundException("No document found for rowId=" + rowId);
        } else if (documents.size() > 1) {
            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);
        }
    } 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) SqlAndParams(de.metas.ui.web.view.descriptor.SqlAndParams) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) EntityNotFoundException(de.metas.ui.web.exceptions.EntityNotFoundException)

Example 3 with SqlAndParams

use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.

the class SqlViewRowIdsOrderedSelectionFactory method retrieveRowIdsForLineIds.

public static Set<DocumentId> retrieveRowIdsForLineIds(@NonNull SqlViewKeyColumnNamesMap keyColumnNamesMap, final ViewId viewId, final Set<Integer> lineIds) {
    final SqlAndParams sqlAndParams = SqlViewSelectionQueryBuilder.buildSqlSelectRowIdsForLineIds(keyColumnNamesMap, viewId.getViewId(), lineIds);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
        DB.setParameters(pstmt, sqlAndParams.getSqlParams());
        rs = pstmt.executeQuery();
        final ImmutableSet.Builder<DocumentId> rowIds = ImmutableSet.builder();
        while (rs.next()) {
            final DocumentId rowId = keyColumnNamesMap.retrieveRowId(rs, "", false);
            rowIds.add(rowId);
        }
        return rowIds.build();
    } catch (final SQLException ex) {
        throw new DBException(ex, sqlAndParams.getSql(), sqlAndParams.getSqlParams());
    } finally {
        DB.close(rs, pstmt);
    }
}
Also used : DBException(org.adempiere.exceptions.DBException) ImmutableSet(com.google.common.collect.ImmutableSet) SQLException(java.sql.SQLException) 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 4 with SqlAndParams

use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.

the class SqlHUEditorViewRepository method retrieveHUIdsPage.

@Override
public Page<Integer> retrieveHUIdsPage(final ViewEvaluationCtx viewEvalCtx, final ViewRowIdsOrderedSelection selection, final int firstRow, final int maxRows) {
    final SqlAndParams sqlAndParams = sqlViewSelect.selectByPage().viewEvalCtx(viewEvalCtx).viewId(selection.getViewId()).firstRowZeroBased(firstRow).pageLength(maxRows).build();
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
        pstmt.setMaxRows(maxRows);
        DB.setParameters(pstmt, sqlAndParams.getSqlParams());
        rs = pstmt.executeQuery();
        final Set<Integer> huIds = new LinkedHashSet<>();
        int lastRowMax = -1;
        while (rs.next()) {
            final int huId = rs.getInt(I_M_HU.COLUMNNAME_M_HU_ID);
            if (huId <= 0) {
                continue;
            }
            huIds.add(huId);
            final int lastRow = rs.getInt(SqlViewSelectData.COLUMNNAME_Paging_SeqNo_OneBased);
            lastRowMax = Math.max(lastRowMax, lastRow);
        }
        if (huIds.isEmpty()) {
            // shall not happen
            return null;
        } else {
            final int lastRowZeroBased = lastRowMax - 1;
            return Page.ofRowsAndLastRowIndex(ImmutableList.copyOf(huIds), lastRowZeroBased);
        }
    } catch (final SQLException ex) {
        throw DBException.wrapIfNeeded(ex).setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
    } finally {
        DB.close(rs, pstmt);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLException(java.sql.SQLException) SqlAndParams(de.metas.ui.web.view.descriptor.SqlAndParams) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 5 with SqlAndParams

use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.

the class SqlViewDataRepository method retrieveRowLines.

private List<IViewRow> retrieveRowLines(final ViewEvaluationCtx viewEvalCtx, final ViewId viewId, final DocumentIdsSelection rowIds) {
    logger.debug("Getting row lines: rowId={} - {}", rowIds, this);
    logger.debug("Using: {}", viewId);
    final SqlAndParams sqlAndParams = sqlViewSelect.selectIncludedLines().viewEvalCtx(viewEvalCtx).viewId(viewId).rowIds(rowIds).build();
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
        DB.setParameters(pstmt, sqlAndParams.getSqlParams());
        rs = pstmt.executeQuery();
        final List<IViewRow> lines = loadViewRows(rs, viewEvalCtx, viewId, -1);
        return lines;
    } 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) SqlAndParams(de.metas.ui.web.view.descriptor.SqlAndParams) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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