Search in sources :

Example 21 with DocumentEntityDescriptor

use of de.metas.ui.web.window.descriptor.DocumentEntityDescriptor 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)

Example 22 with DocumentEntityDescriptor

use of de.metas.ui.web.window.descriptor.DocumentEntityDescriptor in project metasfresh-webui-api by metasfresh.

the class DocumentCollection method getDocumentPath.

/**
 * Retrieves document path for given ZoomInto info.
 *
 * @param zoomIntoInfo
 */
public DocumentPath getDocumentPath(@NonNull final DocumentZoomIntoInfo zoomIntoInfo) {
    if (!zoomIntoInfo.isRecordIdPresent()) {
        throw new IllegalArgumentException("recordId must be set in " + zoomIntoInfo);
    }
    // 
    // Find the root window ID
    final WindowId zoomIntoWindowIdEffective = getWindowId(zoomIntoInfo);
    final DocumentEntityDescriptor rootEntityDescriptor = getDocumentEntityDescriptor(zoomIntoWindowIdEffective);
    final String zoomIntoTableName = zoomIntoInfo.getTableName();
    // (i.e. root descriptor's table is matching record's table)
    if (Objects.equals(rootEntityDescriptor.getTableName(), zoomIntoTableName)) {
        final DocumentId rootDocumentId = DocumentId.of(zoomIntoInfo.getRecordId());
        return DocumentPath.rootDocumentPath(zoomIntoWindowIdEffective, rootDocumentId);
    } else // 
    // We are dealing with an included document
    {
        // Search the root descriptor for any child entity descriptor which would match record's TableName
        final List<DocumentEntityDescriptor> childEntityDescriptors = rootEntityDescriptor.getIncludedEntities().stream().filter(includedEntityDescriptor -> Objects.equals(includedEntityDescriptor.getTableName(), zoomIntoTableName)).collect(ImmutableList.toImmutableList());
        if (childEntityDescriptors.isEmpty()) {
            throw new EntityNotFoundException("Cannot find the detail tab to zoom into").setParameter("zoomIntoInfo", zoomIntoInfo).setParameter("zoomIntoWindowId", zoomIntoWindowIdEffective).setParameter("rootEntityDescriptor", rootEntityDescriptor);
        } else if (childEntityDescriptors.size() > 1) {
            logger.warn("More then one child descriptors matched our root descriptor. Picking the fist one. \nRoot descriptor: {} \nChild descriptors: {}", rootEntityDescriptor, childEntityDescriptors);
        }
        // 
        final DocumentEntityDescriptor childEntityDescriptor = childEntityDescriptors.get(0);
        // Find the root DocumentId
        final DocumentId rowId = DocumentId.of(zoomIntoInfo.getRecordId());
        final DocumentId rootDocumentId = DocumentQuery.ofRecordId(childEntityDescriptor, rowId).retrieveParentDocumentId(rootEntityDescriptor);
        // 
        return DocumentPath.includedDocumentPath(zoomIntoWindowIdEffective, rootDocumentId, childEntityDescriptor.getDetailId(), rowId);
    }
}
Also used : DocumentPermissionsHelper(de.metas.ui.web.window.controller.DocumentPermissionsHelper) DocumentZoomIntoInfo(de.metas.ui.web.window.model.lookup.DocumentZoomIntoInfo) ITrx(org.adempiere.ad.trx.api.ITrx) Env(org.compiere.util.Env) Autowired(org.springframework.beans.factory.annotation.Autowired) OutputType(de.metas.adempiere.report.jasper.OutputType) SourceDocument(de.metas.letters.model.MADBoilerPlate.SourceDocument) Evaluatee(org.compiere.util.Evaluatee) TableModelLoader(org.adempiere.ad.persistence.TableModelLoader) InterfaceWrapperHelper(org.adempiere.model.InterfaceWrapperHelper) DocumentWebsocketPublisher(de.metas.ui.web.window.events.DocumentWebsocketPublisher) CopyRecordSupport(org.adempiere.model.CopyRecordSupport) TableRecordReference(org.adempiere.util.lang.impl.TableRecordReference) RecordZoomWindowFinder(org.adempiere.model.RecordZoomWindowFinder) ImmutableSet(com.google.common.collect.ImmutableSet) WindowConstants(de.metas.ui.web.window.WindowConstants) NonNull(lombok.NonNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DocumentDescriptor(de.metas.ui.web.window.descriptor.DocumentDescriptor) Set(java.util.Set) Objects(java.util.Objects) ITrxManager(org.adempiere.ad.trx.api.ITrxManager) UserSession(de.metas.ui.web.session.UserSession) Services(org.adempiere.util.Services) EntityNotFoundException(de.metas.ui.web.exceptions.EntityNotFoundException) List(java.util.List) Builder(lombok.Builder) CopyRecordFactory(org.adempiere.model.CopyRecordFactory) CacheBuilder(com.google.common.cache.CacheBuilder) DocumentType(de.metas.ui.web.window.datatypes.DocumentType) LogManager(de.metas.logging.LogManager) ProcessInfo(de.metas.process.ProcessInfo) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) ProcessExecutionResult(de.metas.process.ProcessExecutionResult) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) ILogicExpression(org.adempiere.ad.expression.api.ILogicExpression) InvalidDocumentPathException(de.metas.ui.web.window.exceptions.InvalidDocumentPathException) Function(java.util.function.Function) PlainContextAware(org.adempiere.model.PlainContextAware) DocumentNotFoundException(de.metas.ui.web.window.exceptions.DocumentNotFoundException) Value(lombok.Value) CopyMode(de.metas.ui.web.window.model.Document.CopyMode) ImmutableList(com.google.common.collect.ImmutableList) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor) Nullable(javax.annotation.Nullable) OnVariableNotFound(org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound) MADBoilerPlate(de.metas.letters.model.MADBoilerPlate) Logger(org.slf4j.Logger) Evaluatees(org.compiere.util.Evaluatees) DocumentDescriptorFactory(de.metas.ui.web.window.descriptor.factory.DocumentDescriptorFactory) LogicExpressionResult(org.adempiere.ad.expression.api.LogicExpressionResult) MoreObjects(com.google.common.base.MoreObjects) WindowId(de.metas.ui.web.window.datatypes.WindowId) IAutoCloseable(org.adempiere.util.lang.IAutoCloseable) ExecutionException(java.util.concurrent.ExecutionException) BoilerPlateContext(de.metas.letters.model.MADBoilerPlate.BoilerPlateContext) Component(org.springframework.stereotype.Component) AdempiereException(org.adempiere.exceptions.AdempiereException) Check(org.adempiere.util.Check) Preconditions(com.google.common.base.Preconditions) PO(org.compiere.model.PO) Cache(com.google.common.cache.Cache) AllArgsConstructor(lombok.AllArgsConstructor) Collections(java.util.Collections) Immutable(javax.annotation.concurrent.Immutable) WindowId(de.metas.ui.web.window.datatypes.WindowId) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor) EntityNotFoundException(de.metas.ui.web.exceptions.EntityNotFoundException)

Example 23 with DocumentEntityDescriptor

use of de.metas.ui.web.window.descriptor.DocumentEntityDescriptor in project metasfresh-webui-api by metasfresh.

the class DocumentCollection method retrieveRootDocumentFromRepository.

/**
 * Retrieves document from repository
 */
private Document retrieveRootDocumentFromRepository(final DocumentKey documentKey) {
    final DocumentEntityDescriptor entityDescriptor = getDocumentEntityDescriptor(documentKey.getWindowId());
    if (documentKey.getDocumentId().isNew()) {
        throw new InvalidDocumentPathException("documentId cannot be NEW");
    }
    final Document document = DocumentQuery.ofRecordId(entityDescriptor, documentKey.getDocumentId()).setChangesCollector(NullDocumentChangesCollector.instance).retriveDocumentOrNull();
    if (document == null) {
        throw new DocumentNotFoundException(documentKey.getDocumentPath());
    }
    return document;
}
Also used : DocumentNotFoundException(de.metas.ui.web.window.exceptions.DocumentNotFoundException) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor) SourceDocument(de.metas.letters.model.MADBoilerPlate.SourceDocument) InvalidDocumentPathException(de.metas.ui.web.window.exceptions.InvalidDocumentPathException)

Example 24 with DocumentEntityDescriptor

use of de.metas.ui.web.window.descriptor.DocumentEntityDescriptor in project metasfresh-webui-api by metasfresh.

the class DocumentCollection method createDocumentPrint.

public DocumentPrint createDocumentPrint(final DocumentPath documentPath) {
    final Document document = getDocumentReadonly(documentPath);
    final int windowNo = document.getWindowNo();
    final DocumentEntityDescriptor entityDescriptor = document.getEntityDescriptor();
    final int printProcessId = entityDescriptor.getPrintProcessId();
    final TableRecordReference recordRef = getTableRecordReference(documentPath);
    final ProcessExecutionResult processExecutionResult = ProcessInfo.builder().setCtx(Env.getCtx()).setAD_Process_ID(printProcessId).setWindowNo(// important: required for ProcessInfo.findReportingLanguage
    windowNo).setRecord(recordRef).setPrintPreview(true).setJRDesiredOutputType(OutputType.PDF).buildAndPrepareExecution().onErrorThrowException().switchContextWhenRunning().executeSync().getResult();
    return DocumentPrint.builder().filename(processExecutionResult.getReportFilename()).reportContentType(processExecutionResult.getReportContentType()).reportData(processExecutionResult.getReportData()).build();
}
Also used : TableRecordReference(org.adempiere.util.lang.impl.TableRecordReference) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor) SourceDocument(de.metas.letters.model.MADBoilerPlate.SourceDocument) ProcessExecutionResult(de.metas.process.ProcessExecutionResult)

Example 25 with DocumentEntityDescriptor

use of de.metas.ui.web.window.descriptor.DocumentEntityDescriptor in project metasfresh-webui-api by metasfresh.

the class DocumentCollection method delete.

public void delete(final DocumentPath documentPath, final IDocumentChangesCollector changesCollector) {
    if (documentPath.isRootDocument()) {
        final DocumentEntityDescriptor entityDescriptor = documentDescriptorFactory.getDocumentEntityDescriptor(documentPath);
        assertDeleteDocumentAllowed(entityDescriptor);
    }
    final DocumentPath rootDocumentPath = documentPath.getRootDocumentPath();
    if (rootDocumentPath.isNewDocument()) {
        throw new InvalidDocumentPathException(rootDocumentPath);
    }
    forRootDocumentWritable(rootDocumentPath, changesCollector, rootDocument -> {
        if (documentPath.isRootDocument()) {
            if (!rootDocument.isNew()) {
                rootDocument.deleteFromRepository();
            }
            rootDocument.markAsDeleted();
        } else if (documentPath.hasIncludedDocuments()) {
            rootDocument.deleteIncludedDocuments(documentPath.getDetailId(), documentPath.getRowIds());
        } else {
            throw new InvalidDocumentPathException(documentPath);
        }
        // nothing to return
        return null;
    });
}
Also used : DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor) InvalidDocumentPathException(de.metas.ui.web.window.exceptions.InvalidDocumentPathException)

Aggregations

DocumentEntityDescriptor (de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)33 DocumentPath (de.metas.ui.web.window.datatypes.DocumentPath)9 WindowId (de.metas.ui.web.window.datatypes.WindowId)8 DocumentId (de.metas.ui.web.window.datatypes.DocumentId)7 ArrayList (java.util.ArrayList)5 NonNull (lombok.NonNull)5 AdempiereException (org.adempiere.exceptions.AdempiereException)5 Services (org.adempiere.util.Services)5 ImmutableList (com.google.common.collect.ImmutableList)4 IMsgBL (de.metas.i18n.IMsgBL)4 ProcessId (de.metas.ui.web.process.ProcessId)4 QuickInputLayoutDescriptor (de.metas.ui.web.quickinput.QuickInputLayoutDescriptor)4 ViewId (de.metas.ui.web.view.ViewId)4 SqlDocumentEntityDataBindingDescriptor (de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor)4 Document (de.metas.ui.web.window.model.Document)4 List (java.util.List)4 Env (org.compiere.util.Env)4 ITranslatableString (de.metas.i18n.ITranslatableString)3 SourceDocument (de.metas.letters.model.MADBoilerPlate.SourceDocument)3 EntityNotFoundException (de.metas.ui.web.exceptions.EntityNotFoundException)3