Search in sources :

Example 21 with Document

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;
}
Also used : DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) JSONDocument(de.metas.ui.web.window.datatypes.json.JSONDocument) Document(de.metas.ui.web.window.model.Document)

Example 22 with Document

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);
}
Also used : EntityNotFoundException(de.metas.ui.web.exceptions.EntityNotFoundException) Document(de.metas.ui.web.window.model.Document)

Example 23 with Document

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;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) DocumentValidStatus(de.metas.ui.web.window.model.DocumentValidStatus) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IIncludedDocumentsCollection(de.metas.ui.web.window.model.IIncludedDocumentsCollection) DocumentChanges(de.metas.ui.web.window.model.DocumentChanges) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) ToString(lombok.ToString) DocumentStandardAction(de.metas.ui.web.window.model.DocumentStandardAction) ImmutableSet(com.google.common.collect.ImmutableSet) WindowConstants(de.metas.ui.web.window.WindowConstants) Collection(java.util.Collection) LogicExpressionResult(org.adempiere.ad.expression.api.LogicExpressionResult) Set(java.util.Set) WindowId(de.metas.ui.web.window.datatypes.WindowId) Collectors(java.util.stream.Collectors) DocumentSaveStatus(de.metas.ui.web.window.model.DocumentSaveStatus) List(java.util.List) AdempiereException(org.adempiere.exceptions.AdempiereException) IDocumentChangesCollector(de.metas.ui.web.window.model.IDocumentChangesCollector) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) WebSocketConfig(de.metas.ui.web.websocket.WebSocketConfig) Document(de.metas.ui.web.window.model.Document) DocumentSaveStatus(de.metas.ui.web.window.model.DocumentSaveStatus) DocumentValidStatus(de.metas.ui.web.window.model.DocumentValidStatus) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 24 with Document

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);
    });
}
Also used : JSONOptions(de.metas.ui.web.window.datatypes.json.JSONOptions) JSONDocument(de.metas.ui.web.window.datatypes.json.JSONDocument) Document(de.metas.ui.web.window.model.Document) InvalidDocumentPathException(de.metas.ui.web.window.exceptions.InvalidDocumentPathException)

Example 25 with Document

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;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) OrderedDocumentsList(de.metas.ui.web.window.model.OrderedDocumentsList) Document(de.metas.ui.web.window.model.Document) ResultSet(java.sql.ResultSet) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)

Aggregations

Document (de.metas.ui.web.window.model.Document)36 DocumentId (de.metas.ui.web.window.datatypes.DocumentId)16 DocumentPath (de.metas.ui.web.window.datatypes.DocumentPath)12 IDocumentChangesCollector (de.metas.ui.web.window.model.IDocumentChangesCollector)10 List (java.util.List)9 Set (java.util.Set)9 AdempiereException (org.adempiere.exceptions.AdempiereException)9 ImmutableList (com.google.common.collect.ImmutableList)8 DocumentEntityDescriptor (de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)8 ImmutableSet (com.google.common.collect.ImmutableSet)7 EntityNotFoundException (de.metas.ui.web.exceptions.EntityNotFoundException)6 WindowConstants (de.metas.ui.web.window.WindowConstants)6 Services (de.metas.util.Services)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 JSONDocument (de.metas.ui.web.window.datatypes.json.JSONDocument)5 DocumentSaveStatus (de.metas.ui.web.window.model.DocumentSaveStatus)5 Collectors (java.util.stream.Collectors)5 NonNull (lombok.NonNull)5 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)4