use of com.yahoo.elide.core.ResourceLineage in project elide by yahoo.
the class LogMessageImpl method getMessage.
@Override
public String getMessage() {
final SimpleContext ctx = new SimpleContext();
final SimpleContext singleElementContext = new SimpleContext();
if (persistentResource != null) {
/* Create a new lineage which includes the passed in record */
com.yahoo.elide.core.PersistentResource internalResource = (com.yahoo.elide.core.PersistentResource) persistentResource;
ResourceLineage lineage = new ResourceLineage(internalResource.getLineage(), internalResource, null);
for (String name : lineage.getKeys()) {
List<com.yahoo.elide.core.PersistentResource> values = lineage.getRecord(name);
final ValueExpression expression;
final ValueExpression singleElementExpression;
if (values.size() == 1) {
expression = EXPRESSION_FACTORY.createValueExpression(values.get(0).getObject(), Object.class);
singleElementExpression = expression;
} else {
List<Object> objects = values.stream().map(PersistentResource::getObject).collect(Collectors.toList());
expression = EXPRESSION_FACTORY.createValueExpression(objects, List.class);
singleElementExpression = EXPRESSION_FACTORY.createValueExpression(values.get(values.size() - 1).getObject(), Object.class);
}
ctx.setVariable(name, expression);
singleElementContext.setVariable(name, singleElementExpression);
}
final Principal user = getUser().getPrincipal();
if (user != null) {
final ValueExpression opaqueUserValueExpression = EXPRESSION_FACTORY.createValueExpression(user, Object.class);
ctx.setVariable("opaqueUser", opaqueUserValueExpression);
singleElementContext.setVariable("opaqueUser", opaqueUserValueExpression);
}
}
Object[] results = new Object[expressions.length];
for (int idx = 0; idx < results.length; idx++) {
String expressionText = expressions[idx];
final ValueExpression expression;
final ValueExpression singleElementExpression;
try {
expression = EXPRESSION_FACTORY.createValueExpression(ctx, expressionText, Object.class);
singleElementExpression = EXPRESSION_FACTORY.createValueExpression(singleElementContext, expressionText, Object.class);
} catch (ELException e) {
throw new InvalidSyntaxException(e);
}
Object result;
try {
// Single element expressions are intended to allow for access to ${entityType.field} when there are
// multiple "entityType" types listed in the lineage. Without this, any access to an entityType
// without an explicit list index would otherwise result in a 500. Similarly, since we already
// supported lists (i.e. the ${entityType[idx].field} syntax), this also continues to support that.
// It should be noted, however, that list indexing is somewhat brittle unless properly accounted for
// from all possible paths.
result = singleElementExpression.getValue(singleElementContext);
} catch (PropertyNotFoundException e) {
// Try list syntax if not single element
result = expression.getValue(ctx);
}
results[idx] = result;
}
try {
return MessageFormat.format(template, results);
} catch (IllegalArgumentException e) {
throw new InvalidSyntaxException(e);
}
}
Aggregations