Search in sources :

Example 1 with Evaluatee

use of org.compiere.util.Evaluatee in project metasfresh-webui-api by metasfresh.

the class DocumentCollection method assertDeleteDocumentAllowed.

private void assertDeleteDocumentAllowed(final DocumentEntityDescriptor entityDescriptor) {
    final Evaluatee evalCtx = Evaluatees.mapBuilder().put(WindowConstants.FIELDNAME_Processed, false).build().andComposeWith(userSession.toEvaluatee());
    final ILogicExpression allowExpr = entityDescriptor.getAllowDeleteLogic();
    final LogicExpressionResult allow = allowExpr.evaluateToResult(evalCtx, OnVariableNotFound.ReturnNoResult);
    if (allow.isFalse()) {
        throw new AdempiereException("Delete not allowed");
    }
}
Also used : ILogicExpression(org.adempiere.ad.expression.api.ILogicExpression) AdempiereException(org.adempiere.exceptions.AdempiereException) LogicExpressionResult(org.adempiere.ad.expression.api.LogicExpressionResult) Evaluatee(org.compiere.util.Evaluatee)

Example 2 with Evaluatee

use of org.compiere.util.Evaluatee in project metasfresh-webui-api by metasfresh.

the class DocumentField method getLookupValues.

@Override
public LookupValuesList getLookupValues() {
    final LookupDataSource lookupDataSource = getLookupDataSource();
    final Evaluatee ctx = getDocument().asEvaluatee();
    final LookupValuesList values = lookupDataSource.findEntities(ctx);
    lookupValuesStaled = false;
    return values == null ? LookupValuesList.EMPTY : values;
}
Also used : LookupDataSource(de.metas.ui.web.window.model.lookup.LookupDataSource) LookupValuesList(de.metas.ui.web.window.datatypes.LookupValuesList) Evaluatee(org.compiere.util.Evaluatee)

Example 3 with Evaluatee

use of org.compiere.util.Evaluatee in project metasfresh-webui-api by metasfresh.

the class KPIDataLoader method loadData.

private void loadData(final KPIDataResult.Builder data, final TimeRange timeRange) {
    logger.trace("Loading data for {}", timeRange);
    // 
    // Create query evaluation context
    final Evaluatee evalCtx = Evaluatees.mapBuilder().put("MainFromMillis", data.getRange().getFromMillis()).put("MainToMillis", data.getRange().getToMillis()).put("FromMillis", timeRange.getFromMillis()).put("ToMillis", timeRange.getToMillis()).build().andComposeWith(Evaluatees.ofCtx(Env.getCtx()));
    // 
    // Resolve esQuery's variables
    final IStringExpression esQuery = kpi.getESQuery();
    final String esQueryParsed = esQuery.evaluate(evalCtx, OnVariableNotFound.Preserve);
    // 
    // Execute the query
    final SearchResponse response;
    try {
        logger.trace("Executing: \n{}", esQueryParsed);
        response = elasticsearchClient.prepareSearch(kpi.getESSearchIndex()).setTypes(kpi.getESSearchTypes()).setSource(esQueryParsed).get();
        logger.trace("Got response: \n{}", response);
    } catch (final NoNodeAvailableException e) {
        // elastic search transport error => nothing to do about it
        throw e;
    } catch (final Exception e) {
        throw new AdempiereException("Failed executing query for " + this + ": " + e.getLocalizedMessage() + "\nQuery: " + esQueryParsed, e);
    }
    // Fetch data
    try {
        final List<Aggregation> aggregations = response.getAggregations().asList();
        for (final Aggregation agg : aggregations) {
            if (agg instanceof MultiBucketsAggregation) {
                final String aggName = agg.getName();
                final MultiBucketsAggregation multiBucketsAggregation = (MultiBucketsAggregation) agg;
                for (final Bucket bucket : multiBucketsAggregation.getBuckets()) {
                    final Object key = dataSetValueKeyExtractor.apply(bucket, timeRange);
                    for (final KPIField field : kpi.getFields()) {
                        final Object value = field.getBucketValueExtractor().extractValue(aggName, bucket);
                        final Object jsonValue = formatValue(field, value);
                        if (jsonValue == null) {
                            continue;
                        }
                        final String fieldName = fieldNameExtractor.apply(field, timeRange);
                        data.putValue(aggName, key, fieldName, jsonValue);
                    }
                    // 
                    // Make sure the groupByField's value is present in our dataSet value.
                    // If not exist, we can use the key as it's value.
                    final KPIField groupByField = kpi.getGroupByFieldOrNull();
                    if (groupByField != null) {
                        data.putValueIfAbsent(aggName, key, groupByField.getFieldName(), key);
                    }
                }
            } else if (agg instanceof NumericMetricsAggregation.SingleValue) {
                final NumericMetricsAggregation.SingleValue singleValueAggregation = (NumericMetricsAggregation.SingleValue) agg;
                // N/A
                final String key = "NO_KEY";
                for (final KPIField field : kpi.getFields()) {
                    final Object value;
                    if ("value".equals(field.getESPathAsString())) {
                        value = singleValueAggregation.value();
                    } else {
                        throw new IllegalStateException("Only ES path ending with 'value' allowed for field: " + field);
                    }
                    final Object jsonValue = field.convertValueToJson(value);
                    data.putValue(agg.getName(), key, field.getFieldName(), jsonValue);
                }
            } else {
                new AdempiereException("Aggregation type not supported: " + agg.getClass()).throwIfDeveloperModeOrLogWarningElse(logger);
            }
        }
    } catch (final Exception e) {
        throw new AdempiereException(e.getLocalizedMessage() + "\n KPI: " + this + "\n Query: " + esQueryParsed + "\n Response: " + response, e);
    }
}
Also used : NumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) AdempiereException(org.adempiere.exceptions.AdempiereException) Evaluatee(org.compiere.util.Evaluatee) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) NumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) AdempiereException(org.adempiere.exceptions.AdempiereException) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) IStringExpression(org.adempiere.ad.expression.api.IStringExpression)

Example 4 with Evaluatee

use of org.compiere.util.Evaluatee in project metasfresh-webui-api by metasfresh.

the class HUEditorRowAttributesHelper method extractValueAndResolve.

private static final Object extractValueAndResolve(final IAttributeStorage attributesStorage, final IAttributeValue attributeValue) {
    final Object value = attributeValue.getValue();
    if (!attributeValue.isList()) {
        return value;
    }
    final IAttributeValuesProvider valuesProvider = attributeValue.getAttributeValuesProvider();
    final Evaluatee evalCtx = valuesProvider.prepareContext(attributesStorage);
    final NamePair valueNP = valuesProvider.getAttributeValueOrNull(evalCtx, value);
    return LookupValue.fromNamePair(valueNP);
}
Also used : IAttributeValuesProvider(org.adempiere.mm.attributes.spi.IAttributeValuesProvider) NamePair(org.compiere.util.NamePair) Evaluatee(org.compiere.util.Evaluatee)

Example 5 with Evaluatee

use of org.compiere.util.Evaluatee in project metasfresh-webui-api by metasfresh.

the class SqlDocumentQueryBuilder method createEvaluationContext.

private Evaluatee createEvaluationContext() {
    final Evaluatee evalCtx = Evaluatees.mapBuilder().put(Env.CTXNAME_AD_Language, getAD_Language()).put(AccessSqlStringExpression.PARAM_UserRolePermissionsKey.getName(), getPermissionsKey()).build();
    final Evaluatee parentEvalCtx;
    if (parentDocument != null) {
        parentEvalCtx = parentDocument.asEvaluatee();
    } else {
        final Properties ctx = getCtx();
        // TODO: get the proper windowNo
        final int windowNo = Env.WINDOW_MAIN;
        final boolean onlyWindow = false;
        parentEvalCtx = Evaluatees.ofCtx(ctx, windowNo, onlyWindow);
    }
    return Evaluatees.compose(evalCtx, parentEvalCtx);
}
Also used : Properties(java.util.Properties) Evaluatee(org.compiere.util.Evaluatee)

Aggregations

Evaluatee (org.compiere.util.Evaluatee)9 IStringExpression (org.adempiere.ad.expression.api.IStringExpression)4 ImmutableList (com.google.common.collect.ImmutableList)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AdempiereException (org.adempiere.exceptions.AdempiereException)3 LookupValuesList (de.metas.ui.web.window.datatypes.LookupValuesList)2 LookupDataSource (de.metas.ui.web.window.model.lookup.LookupDataSource)2 Properties (java.util.Properties)1 ILogicExpression (org.adempiere.ad.expression.api.ILogicExpression)1 LogicExpressionResult (org.adempiere.ad.expression.api.LogicExpressionResult)1 CompositeStringExpression (org.adempiere.ad.expression.api.impl.CompositeStringExpression)1 IAttributeValuesProvider (org.adempiere.mm.attributes.spi.IAttributeValuesProvider)1 IPair (org.adempiere.util.lang.IPair)1 NamePair (org.compiere.util.NamePair)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)1 Aggregation (org.elasticsearch.search.aggregations.Aggregation)1 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)1 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)1