Search in sources :

Example 6 with ILogicExpression

use of org.adempiere.ad.expression.api.ILogicExpression in project metasfresh-webui-api by metasfresh.

the class Document method computeFieldReadOnly.

private final LogicExpressionResult computeFieldReadOnly(final IDocumentField documentField) {
    // Check document's readonly logic
    final DocumentReadonly documentReadonlyLogic = getReadonly();
    if (documentReadonlyLogic.computeFieldReadonly(documentField.getFieldName(), documentField.isAlwaysUpdateable())) {
        return LogicExpressionResult.TRUE;
    }
    // Check field's readonly logic
    final ILogicExpression fieldReadonlyLogic = documentField.getDescriptor().getReadonlyLogic();
    try {
        final LogicExpressionResult readonly = fieldReadonlyLogic.evaluateToResult(asEvaluatee(), OnVariableNotFound.Fail);
        return readonly;
    } catch (final Exception e) {
        logger.warn("Failed evaluating readonly logic {} for {}. Preserving {}", fieldReadonlyLogic, documentField, documentField.getReadonly(), e);
        return null;
    }
}
Also used : ILogicExpression(org.adempiere.ad.expression.api.ILogicExpression) LogicExpressionResult(org.adempiere.ad.expression.api.LogicExpressionResult) 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)

Example 7 with ILogicExpression

use of org.adempiere.ad.expression.api.ILogicExpression 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)

Example 8 with ILogicExpression

use of org.adempiere.ad.expression.api.ILogicExpression in project metasfresh-webui-api by metasfresh.

the class ADProcessDescriptorsFactory method createProcessParaDescriptor.

private DocumentFieldDescriptor.Builder createProcessParaDescriptor(final WebuiProcessClassInfo webuiProcesClassInfo, final I_AD_Process_Para adProcessParam) {
    final IModelTranslationMap adProcessParaTrlsMap = InterfaceWrapperHelper.getModelTranslationMap(adProcessParam);
    final String parameterName = adProcessParam.getColumnName();
    // 
    // Ask the provider if it has some custom lookup descriptor
    LookupDescriptorProvider lookupDescriptorProvider = webuiProcesClassInfo.getLookupDescriptorProviderOrNull(parameterName);
    // Fallback: create an SQL lookup descriptor based on adProcessParam
    if (lookupDescriptorProvider == null) {
        lookupDescriptorProvider = SqlLookupDescriptor.builder().setCtxTableName(null).setCtxColumnName(parameterName).setDisplayType(adProcessParam.getAD_Reference_ID()).setAD_Reference_Value_ID(adProcessParam.getAD_Reference_Value_ID()).setAD_Val_Rule_ID(adProcessParam.getAD_Val_Rule_ID()).setReadOnlyAccess().buildProvider();
    }
    // 
    final LookupDescriptor lookupDescriptor = lookupDescriptorProvider.provideForScope(LookupDescriptorProvider.LookupScope.DocumentField);
    final DocumentFieldWidgetType widgetType = extractWidgetType(parameterName, adProcessParam.getAD_Reference_ID(), lookupDescriptor, adProcessParam.isRange());
    final Class<?> valueClass = DescriptorsFactoryHelper.getValueClass(widgetType, lookupDescriptor);
    // process parameters shall always allow displaying the password
    final boolean allowShowPassword = widgetType == DocumentFieldWidgetType.Password ? true : false;
    final ILogicExpression readonlyLogic = expressionFactory.compileOrDefault(adProcessParam.getReadOnlyLogic(), ConstantLogicExpression.FALSE, ILogicExpression.class);
    final ILogicExpression displayLogic = expressionFactory.compileOrDefault(adProcessParam.getDisplayLogic(), ConstantLogicExpression.TRUE, ILogicExpression.class);
    final ILogicExpression mandatoryLogic = ConstantLogicExpression.of(adProcessParam.isMandatory());
    final Optional<IExpression<?>> defaultValueExpr = defaultValueExpressions.extractDefaultValueExpression(adProcessParam.getDefaultValue(), parameterName, widgetType, valueClass, mandatoryLogic.isConstantTrue(), // don't allow using auto sequence
    false);
    final DocumentFieldDescriptor.Builder paramDescriptor = DocumentFieldDescriptor.builder(parameterName).setCaption(adProcessParaTrlsMap.getColumnTrl(I_AD_Process_Para.COLUMNNAME_Name, adProcessParam.getName())).setDescription(adProcessParaTrlsMap.getColumnTrl(I_AD_Process_Para.COLUMNNAME_Description, adProcessParam.getDescription())).setValueClass(valueClass).setWidgetType(widgetType).setAllowShowPassword(allowShowPassword).setLookupDescriptorProvider(lookupDescriptorProvider).setDefaultValueExpression(defaultValueExpr).setReadonlyLogic(readonlyLogic).setDisplayLogic(displayLogic).setMandatoryLogic(mandatoryLogic).addCharacteristic(Characteristic.PublicField);
    // Add a callout to forward process parameter value (UI) to current process instance
    if (webuiProcesClassInfo.isForwardValueToJavaProcessInstance(parameterName)) {
        paramDescriptor.addCallout(ProcessParametersCallout::forwardValueToCurrentProcessInstance);
    }
    return paramDescriptor;
}
Also used : IModelTranslationMap(de.metas.i18n.IModelTranslationMap) IExpression(org.adempiere.ad.expression.api.IExpression) LookupDescriptorProvider(de.metas.ui.web.window.descriptor.LookupDescriptorProvider) DocumentFieldDescriptor(de.metas.ui.web.window.descriptor.DocumentFieldDescriptor) ILogicExpression(org.adempiere.ad.expression.api.ILogicExpression) DocumentFieldWidgetType(de.metas.ui.web.window.descriptor.DocumentFieldWidgetType) SqlLookupDescriptor(de.metas.ui.web.window.descriptor.sql.SqlLookupDescriptor) LookupDescriptor(de.metas.ui.web.window.descriptor.LookupDescriptor)

Example 9 with ILogicExpression

use of org.adempiere.ad.expression.api.ILogicExpression in project metasfresh-webui-api by metasfresh.

the class DocumentCollection method assertNewDocumentAllowed.

private void assertNewDocumentAllowed(final DocumentEntityDescriptor entityDescriptor) {
    final ILogicExpression allowExpr = entityDescriptor.getAllowCreateNewLogic();
    final LogicExpressionResult allow = allowExpr.evaluateToResult(userSession.toEvaluatee(), OnVariableNotFound.ReturnNoResult);
    if (allow.isFalse()) {
        throw new AdempiereException("Create not allowed");
    }
}
Also used : ILogicExpression(org.adempiere.ad.expression.api.ILogicExpression) AdempiereException(org.adempiere.exceptions.AdempiereException) LogicExpressionResult(org.adempiere.ad.expression.api.LogicExpressionResult)

Example 10 with ILogicExpression

use of org.adempiere.ad.expression.api.ILogicExpression in project metasfresh-webui-api by metasfresh.

the class LayoutFactory method layoutDetail.

/**
 * @return included entity grid layout
 */
public DocumentLayoutDetailDescriptor.Builder layoutDetail() {
    final DocumentEntityDescriptor.Builder entityDescriptor = documentEntity();
    logger.trace("Generating layout detail for {}", entityDescriptor);
    // If the detail is never displayed then don't add it to layout
    final ILogicExpression tabDisplayLogic = descriptorsFactory.getTabDisplayLogic();
    if (tabDisplayLogic.isConstantFalse()) {
        logger.trace("Skip adding detail tab to layout because it's never displayed: {}, tabDisplayLogic={}", entityDescriptor, tabDisplayLogic);
        return null;
    }
    final DocumentLayoutDetailDescriptor.Builder layoutDetail = DocumentLayoutDetailDescriptor.builder(entityDescriptor.getWindowId(), entityDescriptor.getDetailId()).setGridLayout(layoutGridView()).setSingleRowLayout(layoutSingleRow()).setQueryOnActivate(entityDescriptor.isQueryIncludedTabOnActivate());
    // 
    // Quick input
    {
        final boolean supportQuickInput = quickInputDescriptors.hasQuickInputEntityDescriptor(entityDescriptor.getDocumentType(), entityDescriptor.getDocumentTypeId(), entityDescriptor.getTableNameOrNull(), entityDescriptor.getDetailId(), entityDescriptor.getIsSOTrx());
        layoutDetail.setSupportQuickInput(supportQuickInput);
    }
    return layoutDetail;
}
Also used : ILogicExpression(org.adempiere.ad.expression.api.ILogicExpression) DocumentLayoutDetailDescriptor(de.metas.ui.web.window.descriptor.DocumentLayoutDetailDescriptor) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)

Aggregations

ILogicExpression (org.adempiere.ad.expression.api.ILogicExpression)12 AdempiereException (org.adempiere.exceptions.AdempiereException)7 LogicExpressionResult (org.adempiere.ad.expression.api.LogicExpressionResult)6 DocumentProcessingException (de.metas.document.exceptions.DocumentProcessingException)4 DocumentEntityDescriptor (de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)4 DocumentFieldNotFoundException (de.metas.ui.web.window.exceptions.DocumentFieldNotFoundException)4 DocumentFieldReadonlyException (de.metas.ui.web.window.exceptions.DocumentFieldReadonlyException)4 DocumentNotFoundException (de.metas.ui.web.window.exceptions.DocumentNotFoundException)4 InvalidDocumentStateException (de.metas.ui.web.window.exceptions.InvalidDocumentStateException)4 DocumentFieldDescriptor (de.metas.ui.web.window.descriptor.DocumentFieldDescriptor)3 DocumentFieldWidgetType (de.metas.ui.web.window.descriptor.DocumentFieldWidgetType)3 LookupDescriptor (de.metas.ui.web.window.descriptor.LookupDescriptor)3 LookupDescriptorProvider (de.metas.ui.web.window.descriptor.LookupDescriptorProvider)3 IExpression (org.adempiere.ad.expression.api.IExpression)3 SqlDocumentEntityDataBindingDescriptor (de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor)2 SqlLookupDescriptor (de.metas.ui.web.window.descriptor.sql.SqlLookupDescriptor)2 IModelTranslationMap (de.metas.i18n.IModelTranslationMap)1 Check (de.metas.printing.esb.base.util.Check)1 DocumentId (de.metas.ui.web.window.datatypes.DocumentId)1 DocumentType (de.metas.ui.web.window.datatypes.DocumentType)1