Search in sources :

Example 1 with DocumentQuery

use of de.metas.ui.web.window.model.DocumentQuery in project metasfresh-webui-api by metasfresh.

the class SqlDocumentsRepository method refresh.

private void refresh(@NonNull final Document document, @NonNull final DocumentId documentId) {
    logger.debug("Refreshing: {}, using ID={}", document, documentId);
    if (documentId.isNew()) {
        throw new AdempiereException("Invalid documentId to refresh: " + documentId);
    }
    final DocumentEntityDescriptor entityDescriptor = document.getEntityDescriptor();
    final DocumentQuery query = DocumentQuery.ofRecordId(entityDescriptor, documentId).setChangesCollector(document.getChangesCollector()).build();
    final SqlDocumentQueryBuilder sqlBuilder = SqlDocumentQueryBuilder.of(query);
    final List<Object> sqlParams = new ArrayList<>();
    final String sql = sqlBuilder.getSql(sqlParams);
    final String adLanguage = sqlBuilder.getAD_Language();
    logger.debug("Retrieving records: SQL={} -- {}", sql, sqlParams);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, ITrx.TRXNAME_ThreadInherited);
        DB.setParameters(pstmt, sqlParams);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            final ResultSetDocumentValuesSupplier fieldValueSupplier = new ResultSetDocumentValuesSupplier(entityDescriptor, adLanguage, rs);
            document.refreshFromSupplier(fieldValueSupplier);
        } else {
            // Document is no longer in our repository
            final DocumentPath documentPathEffective = document.getDocumentPath().withDocumentId(documentId);
            throw new DocumentNotFoundException(documentPathEffective);
        }
        if (rs.next()) {
            throw new AdempiereException("More than one record found while trying to reload document: " + document);
        }
    } catch (final SQLException e) {
        throw new DBException(e, sql, sqlParams);
    } finally {
        DB.close(rs, pstmt);
    }
}
Also used : DBException(org.adempiere.exceptions.DBException) DocumentNotFoundException(de.metas.ui.web.window.exceptions.DocumentNotFoundException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DocumentQuery(de.metas.ui.web.window.model.DocumentQuery) AdempiereException(org.adempiere.exceptions.AdempiereException) ResultSet(java.sql.ResultSet) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)

Aggregations

DocumentPath (de.metas.ui.web.window.datatypes.DocumentPath)1 DocumentEntityDescriptor (de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)1 DocumentNotFoundException (de.metas.ui.web.window.exceptions.DocumentNotFoundException)1 DocumentQuery (de.metas.ui.web.window.model.DocumentQuery)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1 DBException (org.adempiere.exceptions.DBException)1