Search in sources :

Example 6 with SqlDocumentEntityDataBindingDescriptor

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

the class SqlDocumentsRepository method save.

@Override
public void save(final Document document) {
    Services.get(ITrxManager.class).assertThreadInheritedTrxExists();
    assertThisRepository(document.getEntityDescriptor());
    DocumentPermissionsHelper.assertCanEdit(document);
    // Runnables to be executed after the PO is saved
    final List<Runnable> afterSaveRunnables = new ArrayList<>();
    // 
    // Load the PO / Create new PO instance
    final PO po = retrieveOrCreatePO(document);
    // 
    // Set values to PO
    final boolean isNew = document.isNew();
    boolean changes = false;
    for (final IDocumentFieldView documentField : document.getFieldViews()) {
        if (!isNew && !documentField.hasChangesToSave()) {
            logger.trace("Skip setting PO value because document field has no changes: {}", documentField);
            continue;
        }
        if (DocumentFieldWidgetType.Labels == documentField.getWidgetType()) {
            // save labels after PO is saved because we want to make sure it's not new (so we can link to it)
            afterSaveRunnables.add(() -> saveLabels(document, documentField));
        }
        if (setPOValue(po, documentField)) {
            changes = true;
        }
    }
    // 
    // Save the PO
    boolean needsRefresh = false;
    if (changes) {
        // 
        // Actual save
        // TODO: advice the PO to not reload after save.
        InterfaceWrapperHelper.save(po);
        document.markAsNotNew();
        needsRefresh = true;
    } else {
        logger.trace("Skip saving {} because there was no actual change", po);
    }
    // Execute after save runnables
    if (!afterSaveRunnables.isEmpty()) {
        afterSaveRunnables.forEach(r -> r.run());
        needsRefresh = true;
    }
    // Reload the document
    if (needsRefresh) {
        final SqlDocumentEntityDataBindingDescriptor dataBinding = document.getEntityDescriptor().getDataBinding(SqlDocumentEntityDataBindingDescriptor.class);
        final DocumentId idNew = extractDocumentId(po, dataBinding);
        refresh(document, idNew);
    }
    // Notify the parent document that one of it's children were saved
    if (!document.isRootDocument()) {
        document.getParentDocument().onChildSaved(document);
    }
}
Also used : ITrxManager(org.adempiere.ad.trx.api.ITrxManager) SqlDocumentEntityDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor) ArrayList(java.util.ArrayList) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) IDocumentFieldView(de.metas.ui.web.window.model.IDocumentFieldView) PO(org.compiere.model.PO)

Example 7 with SqlDocumentEntityDataBindingDescriptor

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

the class SqlDocumentsRepository method retrieveNextDocumentId.

private static DocumentId retrieveNextDocumentId(final DocumentEntityDescriptor entityDescriptor) {
    final SqlDocumentEntityDataBindingDescriptor dataBinding = SqlDocumentEntityDataBindingDescriptor.cast(entityDescriptor.getDataBinding());
    final int adClientId = UserSession.getCurrent().getAD_Client_ID();
    final String tableName = dataBinding.getTableName();
    final int nextId = DB.getNextID(adClientId, tableName, ITrx.TRXNAME_ThreadInherited);
    if (nextId <= 0) {
        throw new DBException("Cannot retrieve next ID from database for " + entityDescriptor);
    }
    logger.trace("Acquired next ID={} for {}", nextId, entityDescriptor);
    return DocumentId.of(nextId);
}
Also used : DBException(org.adempiere.exceptions.DBException) SqlDocumentEntityDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor)

Aggregations

SqlDocumentEntityDataBindingDescriptor (de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor)7 WindowId (de.metas.ui.web.window.datatypes.WindowId)3 DocumentEntityDescriptor (de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)3 ArrayList (java.util.ArrayList)3 ImmutableList (com.google.common.collect.ImmutableList)2 IMsgBL (de.metas.i18n.IMsgBL)2 ITranslatableString (de.metas.i18n.ITranslatableString)2 LogManager (de.metas.logging.LogManager)2 DocumentFilter (de.metas.ui.web.document.filter.DocumentFilter)2 DocumentFilterDescriptorsProvider (de.metas.ui.web.document.filter.DocumentFilterDescriptorsProvider)2 SqlViewBinding (de.metas.ui.web.view.descriptor.SqlViewBinding)2 DocumentPath (de.metas.ui.web.window.datatypes.DocumentPath)2 DocumentFieldWidgetType (de.metas.ui.web.window.descriptor.DocumentFieldWidgetType)2 DocumentDescriptorFactory (de.metas.ui.web.window.descriptor.factory.DocumentDescriptorFactory)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 NonNull (lombok.NonNull)2 AdempiereException (org.adempiere.exceptions.AdempiereException)2