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");
}
}
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;
}
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);
}
}
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);
}
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);
}
Aggregations