Search in sources :

Example 1 with ReasonSupplier

use of de.metas.ui.web.window.model.IDocumentChangesCollector.ReasonSupplier in project metasfresh-webui-api by metasfresh.

the class DocumentChanges method collectFrom.

private boolean collectFrom(final IDocumentFieldView documentField, final ReasonSupplier reason) {
    final DocumentFieldChange toEvent = fieldChangesOf(documentField);
    boolean collected = false;
    // Value
    if (!toEvent.isValueSet()) {
        final Object value = documentField.getValue();
        toEvent.setValue(value, reason);
        collected = true;
    } else {
        final Object value = documentField.getValue();
        final Object previousValue = toEvent.getValue();
        if (!DataTypes.equals(value, previousValue)) {
            final ReasonSupplier reasonNew = reason.addPreviousReason(toEvent.getValueReason(), previousValue == null ? "<NULL>" : previousValue);
            toEvent.setValue(value, reasonNew);
            collected = true;
        }
    }
    // 
    // Readonly
    final LogicExpressionResult readonly = documentField.getReadonly();
    if (!readonly.equals(toEvent.getReadonly())) {
        final ReasonSupplier reasonNew = reason.add("readonly", readonly).addPreviousReason(toEvent.getReadonlyReason());
        toEvent.setReadonly(readonly, reasonNew);
        collected = true;
    }
    // 
    // Mandatory
    final LogicExpressionResult mandatory = documentField.getMandatory();
    if (!mandatory.equals(toEvent.getMandatory())) {
        final ReasonSupplier reasonNew = reason.add("mandatory", mandatory).addPreviousReason(toEvent.getMandatoryReason());
        toEvent.setMandatory(mandatory, reasonNew);
        collected = true;
    }
    // 
    // Displayed
    final LogicExpressionResult displayed = documentField.getDisplayed();
    if (!displayed.equals(toEvent.getDisplayed())) {
        final ReasonSupplier reasonNew = reason.add("displayed", displayed).addPreviousReason(toEvent.getDisplayedReason());
        toEvent.setDisplayed(displayed, reasonNew);
        collected = true;
    }
    return collected;
}
Also used : LogicExpressionResult(org.adempiere.ad.expression.api.LogicExpressionResult) ReasonSupplier(de.metas.ui.web.window.model.IDocumentChangesCollector.ReasonSupplier)

Example 2 with ReasonSupplier

use of de.metas.ui.web.window.model.IDocumentChangesCollector.ReasonSupplier in project metasfresh-webui-api by metasfresh.

the class Document method updateOnDependencyChanged.

/**
 * Updates document or fields characteristics (e.g. readonly, mandatory, displayed, lookupValuesStaled etc).
 *
 * @param propertyName
 * @param documentField
 * @param triggeringFieldName optional field name which triggered this update
 * @param triggeringDependencyType
 * @param documentChangesCollector events collector (where to collect the change events)
 * @param collectEventsEventIfNoChange true if we shall collect the change event even if there was no change
 */
private void updateOnDependencyChanged(final String propertyName, final IDocumentField documentField, final String triggeringFieldName, final DependencyType triggeringDependencyType) {
    final ReasonSupplier reason = () -> "TriggeringField=" + triggeringFieldName + ", DependencyType=" + triggeringDependencyType;
    if (DependencyType.DocumentReadonlyLogic == triggeringDependencyType) {
        if (DocumentFieldDependencyMap.DOCUMENT_Readonly.equals(propertyName)) {
            updateReadonlyAndPropagate(reason);
        }
    } else if (DependencyType.ReadonlyLogic == triggeringDependencyType) {
        updateFieldReadOnlyAndCollect(documentField, reason);
    } else if (DependencyType.MandatoryLogic == triggeringDependencyType) {
        final LogicExpressionResult valueOld = documentField.getMandatory();
        final ILogicExpression mandatoryLogic = documentField.getDescriptor().getMandatoryLogic();
        try {
            final LogicExpressionResult mandatory = mandatoryLogic.evaluateToResult(asEvaluatee(), OnVariableNotFound.Fail);
            documentField.setMandatory(mandatory, changesCollector);
        } catch (final Exception e) {
            logger.warn("Failed evaluating mandatory logic {} for {}. Preserving {}", mandatoryLogic, documentField, documentField.getMandatory(), e);
        }
        changesCollector.collectMandatoryIfChanged(documentField, valueOld, reason);
    } else if (DependencyType.DisplayLogic == triggeringDependencyType) {
        final LogicExpressionResult valueOld = documentField.getDisplayed();
        updateFieldDisplayed(documentField);
        changesCollector.collectDisplayedIfChanged(documentField, valueOld, reason);
    } else if (DependencyType.LookupValues == triggeringDependencyType) {
        final boolean lookupValuesStaledOld = documentField.isLookupValuesStale();
        final boolean lookupValuesStaled = documentField.setLookupValuesStaled(triggeringFieldName);
        if (lookupValuesStaled && !lookupValuesStaledOld) {
            // https://github.com/metasfresh/metasfresh-webui-api/issues/551 check if we can leave the old value as it is
            final Object valueOld = documentField.getValue();
            if (valueOld != null) {
                final boolean currentValueStillValid = // because we did setLookupValuesStaled(), this causes a reload
                documentField.getLookupValues().stream().anyMatch(// check if the current value is still value after we reloaded the list
                value -> Objects.equals(value, valueOld));
                if (!currentValueStillValid) {
                    documentField.setValue(null, changesCollector);
                    changesCollector.collectValueIfChanged(documentField, valueOld, reason);
                }
            }
            // https://github.com/metasfresh/metasfresh-webui-frontend/issues/1165 - the value was not stale, but now it is => notify the frontend so it shall invalidate its cache
            changesCollector.collectLookupValuesStaled(documentField, reason);
        }
    } else if (DependencyType.FieldValue == triggeringDependencyType) {
        final IDocumentFieldValueProvider valueProvider = documentField.getDescriptor().getVirtualFieldValueProvider().orElse(null);
        if (valueProvider != null) {
            try {
                final Object valueOld = documentField.getValue();
                final Object valueNew = valueProvider.calculateValue(this);
                documentField.setInitialValue(valueNew, changesCollector);
                documentField.setValue(valueNew, changesCollector);
                changesCollector.collectValueIfChanged(documentField, valueOld, reason);
            } catch (final Exception ex) {
                logger.warn("Failed updating virtual field {} for {}", documentField, this, ex);
            }
        }
    } else {
        new AdempiereException("Unknown dependency type: " + triggeringDependencyType).throwIfDeveloperModeOrLogWarningElse(logger);
    }
}
Also used : ILogicExpression(org.adempiere.ad.expression.api.ILogicExpression) AdempiereException(org.adempiere.exceptions.AdempiereException) LogicExpressionResult(org.adempiere.ad.expression.api.LogicExpressionResult) ReasonSupplier(de.metas.ui.web.window.model.IDocumentChangesCollector.ReasonSupplier) DocumentFieldReadonlyException(de.metas.ui.web.window.exceptions.DocumentFieldReadonlyException) InvalidDocumentStateException(de.metas.ui.web.window.exceptions.InvalidDocumentStateException) DocumentNotFoundException(de.metas.ui.web.window.exceptions.DocumentNotFoundException) DocumentProcessingException(de.metas.document.exceptions.DocumentProcessingException) AdempiereException(org.adempiere.exceptions.AdempiereException) DocumentFieldNotFoundException(de.metas.ui.web.window.exceptions.DocumentFieldNotFoundException)

Aggregations

ReasonSupplier (de.metas.ui.web.window.model.IDocumentChangesCollector.ReasonSupplier)2 LogicExpressionResult (org.adempiere.ad.expression.api.LogicExpressionResult)2 DocumentProcessingException (de.metas.document.exceptions.DocumentProcessingException)1 DocumentFieldNotFoundException (de.metas.ui.web.window.exceptions.DocumentFieldNotFoundException)1 DocumentFieldReadonlyException (de.metas.ui.web.window.exceptions.DocumentFieldReadonlyException)1 DocumentNotFoundException (de.metas.ui.web.window.exceptions.DocumentNotFoundException)1 InvalidDocumentStateException (de.metas.ui.web.window.exceptions.InvalidDocumentStateException)1 ILogicExpression (org.adempiere.ad.expression.api.ILogicExpression)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1