Search in sources :

Example 1 with ResourceLineage

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);
    }
}
Also used : PersistentResource(com.yahoo.elide.core.security.PersistentResource) SimpleContext(de.odysseus.el.util.SimpleContext) PropertyNotFoundException(javax.el.PropertyNotFoundException) ResourceLineage(com.yahoo.elide.core.ResourceLineage) ToString(lombok.ToString) ValueExpression(javax.el.ValueExpression) List(java.util.List) ELException(javax.el.ELException) Principal(java.security.Principal)

Aggregations

ResourceLineage (com.yahoo.elide.core.ResourceLineage)1 PersistentResource (com.yahoo.elide.core.security.PersistentResource)1 SimpleContext (de.odysseus.el.util.SimpleContext)1 Principal (java.security.Principal)1 List (java.util.List)1 ELException (javax.el.ELException)1 PropertyNotFoundException (javax.el.PropertyNotFoundException)1 ValueExpression (javax.el.ValueExpression)1 ToString (lombok.ToString)1