use of de.metas.ui.web.window.model.Document in project metasfresh-webui-api by metasfresh.
the class ASIDocument method bindContextDocumentIfPossible.
public ASIDocument bindContextDocumentIfPossible(@NonNull final DocumentCollection documentsCollection) {
final DocumentPath contextDocumentPath = descriptor.getContextDocumentPath();
if (contextDocumentPath == null) {
return this;
}
if (!documentsCollection.isWindowIdSupported(contextDocumentPath.getWindowIdOrNull())) {
return this;
}
final Document contextDocument = documentsCollection.getDocumentReadonly(contextDocumentPath);
data.setShadowParentDocumentEvaluatee(contextDocument.asEvaluatee());
return this;
}
use of de.metas.ui.web.window.model.Document in project metasfresh-webui-api by metasfresh.
the class ASIRepository method loadReadonly.
/**
* Retrieves {@link ASIDocument} for given ASI. The document will be readonly and not save-able.
*
* IMPORTANT: the retrieved document is not cached, so next time it will be retrieved again
*
* @param attributeSetInstanceId
* @return ASI document
*/
public ASIDocument loadReadonly(@NonNull final AttributeSetInstanceId attributeSetInstanceId) {
if (!attributeSetInstanceId.isRegular()) {
throw new EntityNotFoundException("ASI " + attributeSetInstanceId);
}
//
// Get the ASI descriptor
final WebuiASIEditingInfo info = WebuiASIEditingInfo.readonlyASI(attributeSetInstanceId);
final ASIDescriptor asiDescriptor = descriptorsFactory.getASIDescriptor(info);
//
// Create the new ASI document
final Document asiDocData = Document.builder(asiDescriptor.getEntityDescriptor()).initializeAsNewDocument(createASIDocumentValuesSupplier(attributeSetInstanceId, asiDescriptor.getEntityDescriptor()));
//
// Validate, log and add the new ASI document to our index
asiDocData.checkAndGetValidStatus();
logger.trace("Created from ASI={}: {}", attributeSetInstanceId, asiDocData);
final ASIDocument asiDoc = new ASIDocument(asiDescriptor, asiDocData);
return asiDoc.copy(CopyMode.CheckInReadonly, NullDocumentChangesCollector.instance);
}
use of de.metas.ui.web.window.model.Document in project metasfresh-webui-api by metasfresh.
the class JSONDocument method ofDocument.
public static final JSONDocument ofDocument(final Document document, final JSONOptions jsonOpts) {
final JSONDocument jsonDocument = new JSONDocument(document.getDocumentPath());
//
// Fields
{
final List<JSONDocumentField> jsonFields = new ArrayList<>();
// Add pseudo "ID" field first
jsonFields.add(0, JSONDocumentField.idField(document.getDocumentIdAsJson()));
// Append the other fields
document.getFieldViews().stream().filter(jsonOpts.documentFieldFilter()).map(field -> JSONDocumentField.ofDocumentField(field, jsonOpts.getAD_Language())).peek(// apply permissions
jsonField -> jsonOpts.getDocumentPermissions().apply(document, jsonField)).forEach(jsonFields::add);
jsonDocument.setFields(jsonFields);
}
//
// Valid Status
final DocumentValidStatus documentValidStatus = document.getValidStatus();
if (documentValidStatus != null) {
jsonDocument.setValidStatus(documentValidStatus);
}
//
// Save Status
final DocumentSaveStatus documentSaveStatus = document.getSaveStatus();
if (documentSaveStatus != null) {
jsonDocument.setSaveStatus(documentSaveStatus);
}
//
// Included tabs info
document.getIncludedDocumentsCollections().stream().map(JSONDocument::createIncludedTabInfo).peek(jsonIncludedTabInfo -> jsonOpts.getDocumentPermissions().apply(document, jsonIncludedTabInfo)).forEach(jsonDocument::addIncludedTabInfo);
//
// Available standard actions
jsonDocument.setStandardActions(document.getStandardActions());
// Set debugging info
if (WindowConstants.isProtocolDebugging()) {
jsonDocument.putDebugProperty("tablename", document.getEntityDescriptor().getTableNameOrNull());
jsonDocument.putDebugProperty(JSONOptions.DEBUG_ATTRNAME, jsonOpts.toString());
jsonDocument.putDebugProperty("fields-count", jsonDocument.getFieldsCount());
}
return jsonDocument;
}
use of de.metas.ui.web.window.model.Document in project metasfresh-webui-api by metasfresh.
the class WindowRestController method getData.
private List<JSONDocument> getData(final DocumentPath documentPath, final String fieldsListStr, final boolean advanced, final List<DocumentQueryOrderBy> orderBys) {
userSession.assertLoggedIn();
final JSONOptions jsonOpts = newJSONOptions().setShowAdvancedFields(advanced).setDataFieldsList(fieldsListStr).build();
return documentCollection.forRootDocumentReadonly(documentPath, rootDocument -> {
List<Document> documents;
if (documentPath.isRootDocument()) {
documents = ImmutableList.of(rootDocument);
} else if (documentPath.isAnyIncludedDocument()) {
documents = rootDocument.getIncludedDocuments(documentPath.getDetailId(), orderBys).toList();
} else if (documentPath.isSingleIncludedDocument()) {
documents = ImmutableList.of(rootDocument.getIncludedDocument(documentPath.getDetailId(), documentPath.getSingleRowId()));
} else {
throw new InvalidDocumentPathException(documentPath);
}
return JSONDocument.ofDocumentsList(documents, jsonOpts);
});
}
use of de.metas.ui.web.window.model.Document 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 = getLoadLimitWarn();
final int loadLimitMax = getLoadLimitMax();
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: {}" + "\n To change this limit check {} sysconfig.", sql, sqlParams, loadCount, SYSCONFIG_LoadLimitMax);
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: {}" + "\n To change this limit check {} sysconfig.", sql, sqlParams, loadCount, SYSCONFIG_LoadLimitWarn);
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