use of de.metas.ui.web.window.model.OrderedDocumentsList in project metasfresh-webui-api by metasfresh.
the class SqlDocumentsRepository method retrieveDocument.
@Override
public Document retrieveDocument(final DocumentQuery query, final IDocumentChangesCollector changesCollector) {
final int limit = 2;
final OrderedDocumentsList documents = retriveDocuments(query, limit, changesCollector);
if (documents.isEmpty()) {
return null;
} else if (documents.size() > 1) {
throw new DBMoreThenOneRecordsFoundException("More than one record found for " + query + " on " + this + "\n First " + limit + " records: " + Joiner.on("\n").join(documents.toList()));
} else {
return documents.get(0);
}
}
use of de.metas.ui.web.window.model.OrderedDocumentsList in project metasfresh-webui-api by metasfresh.
the class SqlDocumentsRepository method retriveDocuments.
public OrderedDocumentsList retriveDocuments(final DocumentQuery query, final int limit, final IDocumentChangesCollector changesCollector) {
logger.debug("Retrieving records: query={}, limit={}", query, limit);
final DocumentEntityDescriptor entityDescriptor = query.getEntityDescriptor();
assertThisRepository(entityDescriptor);
final Document parentDocument = query.getParentDocument();
final Function<DocumentId, Document> existingDocumentsSupplier = query.getExistingDocumentsSupplier();
final List<Object> sqlParams = new ArrayList<>();
final SqlDocumentQueryBuilder sqlBuilder = SqlDocumentQueryBuilder.of(query);
final String sql = sqlBuilder.getSql(sqlParams);
final String adLanguage = sqlBuilder.getAD_Language();
logger.debug("Retrieving records: SQL={} -- {}", sql, sqlParams);
final int loadLimitWarn = this.loadLimitWarn;
final int loadLimitMax = this.loadLimitMax;
int maxRowsToFetch = limit;
if (maxRowsToFetch <= 0) {
maxRowsToFetch = loadLimitMax;
}
final OrderedDocumentsList documentsCollector = OrderedDocumentsList.newEmpty(query.getOrderBys());
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, ITrx.TRXNAME_ThreadInherited);
if (maxRowsToFetch > 0) {
pstmt.setMaxRows(maxRowsToFetch);
}
DB.setParameters(pstmt, sqlParams);
rs = pstmt.executeQuery();
boolean loadLimitWarnReported = false;
while (rs.next()) {
final ResultSetDocumentValuesSupplier documentValuesSupplier = new ResultSetDocumentValuesSupplier(entityDescriptor, adLanguage, rs);
Document document = null;
if (existingDocumentsSupplier != null) {
final DocumentId documentId = documentValuesSupplier.getDocumentId();
document = existingDocumentsSupplier.apply(documentId);
}
if (document == null) {
document = Document.builder(entityDescriptor).setParentDocument(parentDocument).setChangesCollector(changesCollector).initializeAsExistingRecord(documentValuesSupplier);
}
documentsCollector.addDocument(document);
final int loadCount = documentsCollector.size();
// Stop if we reached the limit
if (limit > 0 && loadCount >= limit) {
break;
}
// Stop if we reached the MAXIMUM limit
if (loadLimitMax > 0 && loadCount >= loadLimitMax) {
logger.warn("Reached load count MAXIMUM level. Stop loading. \n SQL: {} \n SQL Params: {} \n loadCount: {}", sql, sqlParams, loadCount);
break;
}
// WARN if we reached the Warning limit
if (!loadLimitWarnReported && loadLimitWarn > 0 && loadCount >= loadLimitWarn) {
logger.warn("Reached load count Warning level. Continue loading. \n SQL: {} \n SQL Params: {} \n loadCount: {}", sql, sqlParams, loadCount);
loadLimitWarnReported = true;
}
}
} catch (final SQLException e) {
throw new DBException(e, sql, sqlParams);
} finally {
DB.close(rs, pstmt);
}
logger.debug("Retrieved {} records.", documentsCollector.size());
return documentsCollector;
}
Aggregations