Search in sources :

Example 36 with MapContext

use of org.apache.commons.jexl3.MapContext in project syncope by apache.

the class JEXLItemTransformerImpl method beforePull.

@Override
public List<Object> beforePull(final Item item, final EntityTO entityTO, final List<Object> values) {
    if (StringUtils.isNotBlank(pullJEXL) && values != null) {
        List<Object> newValues = new ArrayList<>(values.size());
        values.forEach(value -> {
            JexlContext jexlContext = new MapContext();
            jexlContext.set("value", value);
            if (entityTO instanceof AnyTO) {
                JexlUtils.addFieldsToContext((AnyTO) entityTO, jexlContext);
                JexlUtils.addAttrTOsToContext(((AnyTO) entityTO).getPlainAttrs(), jexlContext);
                JexlUtils.addAttrTOsToContext(((AnyTO) entityTO).getDerAttrs(), jexlContext);
                JexlUtils.addAttrTOsToContext(((AnyTO) entityTO).getVirAttrs(), jexlContext);
            } else if (entityTO instanceof RealmTO) {
                JexlUtils.addFieldsToContext((RealmTO) entityTO, jexlContext);
                newValues.add(JexlUtils.evaluate(pullJEXL, jexlContext));
            }
            newValues.add(JexlUtils.evaluate(pullJEXL, jexlContext));
        });
        return newValues;
    }
    return JEXLItemTransformer.super.beforePull(item, entityTO, values);
}
Also used : AnyTO(org.apache.syncope.common.lib.to.AnyTO) ArrayList(java.util.ArrayList) JexlContext(org.apache.commons.jexl3.JexlContext) RealmTO(org.apache.syncope.common.lib.to.RealmTO) MapContext(org.apache.commons.jexl3.MapContext)

Example 37 with MapContext

use of org.apache.commons.jexl3.MapContext in project syncope by apache.

the class JEXLItemTransformerImpl method beforePropagation.

@Override
public List<PlainAttrValue> beforePropagation(final Item item, final Entity entity, final List<PlainAttrValue> values) {
    if (StringUtils.isNotBlank(propagationJEXL) && values != null) {
        values.forEach(value -> {
            JexlContext jexlContext = new MapContext();
            if (entity != null) {
                JexlUtils.addFieldsToContext(entity, jexlContext);
                if (entity instanceof Any) {
                    JexlUtils.addPlainAttrsToContext(((Any<?>) entity).getPlainAttrs(), jexlContext);
                    JexlUtils.addDerAttrsToContext(((Any<?>) entity), jexlContext);
                }
            }
            jexlContext.set("value", value.getValueAsString());
            value.setStringValue(JexlUtils.evaluate(propagationJEXL, jexlContext));
        });
        return values;
    }
    return values;
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) Any(org.apache.syncope.core.persistence.api.entity.Any)

Example 38 with MapContext

use of org.apache.commons.jexl3.MapContext in project syncope by apache.

the class TemplateUtils method apply.

@Transactional(readOnly = true)
public <T extends AnyTO> void apply(final T anyTO, final AnyTO template) {
    fill(anyTO, template);
    MapContext jexlContext = new MapContext();
    JexlUtils.addFieldsToContext(anyTO, jexlContext);
    JexlUtils.addAttrTOsToContext(anyTO.getPlainAttrs(), jexlContext);
    JexlUtils.addAttrTOsToContext(anyTO.getDerAttrs(), jexlContext);
    JexlUtils.addAttrTOsToContext(anyTO.getVirAttrs(), jexlContext);
    if (template instanceof AnyObjectTO) {
        fillRelationships((GroupableRelatableTO) anyTO, ((GroupableRelatableTO) template));
        fillMemberships((GroupableRelatableTO) anyTO, ((GroupableRelatableTO) template));
    } else if (template instanceof UserTO) {
        if (StringUtils.isNotBlank(((UserTO) template).getUsername())) {
            String evaluated = JexlUtils.evaluate(((UserTO) template).getUsername(), jexlContext);
            if (StringUtils.isNotBlank(evaluated)) {
                ((UserTO) anyTO).setUsername(evaluated);
            }
        }
        if (StringUtils.isNotBlank(((UserTO) template).getPassword())) {
            String evaluated = JexlUtils.evaluate(((UserTO) template).getPassword(), jexlContext);
            if (StringUtils.isNotBlank(evaluated)) {
                ((UserTO) anyTO).setPassword(evaluated);
            }
        }
        fillRelationships((GroupableRelatableTO) anyTO, ((GroupableRelatableTO) template));
        fillMemberships((GroupableRelatableTO) anyTO, ((GroupableRelatableTO) template));
    } else if (template instanceof GroupTO) {
        if (StringUtils.isNotBlank(((GroupTO) template).getName())) {
            String evaluated = JexlUtils.evaluate(((GroupTO) template).getName(), jexlContext);
            if (StringUtils.isNotBlank(evaluated)) {
                ((GroupTO) anyTO).setName(evaluated);
            }
        }
        if (((GroupTO) template).getUserOwner() != null) {
            final User userOwner = userDAO.find(((GroupTO) template).getUserOwner());
            if (userOwner != null) {
                ((GroupTO) anyTO).setUserOwner(userOwner.getKey());
            }
        }
        if (((GroupTO) template).getGroupOwner() != null) {
            final Group groupOwner = groupDAO.find(((GroupTO) template).getGroupOwner());
            if (groupOwner != null) {
                ((GroupTO) anyTO).setGroupOwner(groupOwner.getKey());
            }
        }
    }
}
Also used : AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) Group(org.apache.syncope.core.persistence.api.entity.group.Group) GroupableRelatableTO(org.apache.syncope.common.lib.to.GroupableRelatableTO) User(org.apache.syncope.core.persistence.api.entity.user.User) UserTO(org.apache.syncope.common.lib.to.UserTO) MapContext(org.apache.commons.jexl3.MapContext) GroupTO(org.apache.syncope.common.lib.to.GroupTO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 39 with MapContext

use of org.apache.commons.jexl3.MapContext in project syncope by apache.

the class TemplateUtils method fill.

private void fill(final AnyTO anyTO, final AnyTO template) {
    MapContext jexlContext = new MapContext();
    JexlUtils.addFieldsToContext(anyTO, jexlContext);
    JexlUtils.addAttrTOsToContext(anyTO.getPlainAttrs(), jexlContext);
    JexlUtils.addAttrTOsToContext(anyTO.getDerAttrs(), jexlContext);
    JexlUtils.addAttrTOsToContext(anyTO.getVirAttrs(), jexlContext);
    if (template.getRealm() != null) {
        String evaluated = JexlUtils.evaluate(template.getRealm(), jexlContext);
        if (StringUtils.isNotBlank(evaluated)) {
            anyTO.setRealm(evaluated);
        }
    }
    Map<String, AttrTO> currentAttrMap = EntityTOUtils.buildAttrMap(anyTO.getPlainAttrs());
    for (AttrTO templatePlainAttr : template.getPlainAttrs()) {
        if (!templatePlainAttr.getValues().isEmpty() && (!currentAttrMap.containsKey(templatePlainAttr.getSchema()) || currentAttrMap.get(templatePlainAttr.getSchema()).getValues().isEmpty())) {
            AttrTO evaluated = evaluateAttr(templatePlainAttr, jexlContext);
            if (!evaluated.getValues().isEmpty()) {
                anyTO.getPlainAttrs().add(evaluated);
                jexlContext.set(evaluated.getSchema(), evaluated.getValues().get(0));
            }
        }
    }
    currentAttrMap = EntityTOUtils.buildAttrMap(anyTO.getDerAttrs());
    for (AttrTO templateDerAttr : template.getDerAttrs()) {
        if (!currentAttrMap.containsKey(templateDerAttr.getSchema())) {
            anyTO.getDerAttrs().add(templateDerAttr);
        }
    }
    currentAttrMap = EntityTOUtils.buildAttrMap(anyTO.getVirAttrs());
    for (AttrTO templateVirAttr : template.getVirAttrs()) {
        if (!templateVirAttr.getValues().isEmpty() && (!currentAttrMap.containsKey(templateVirAttr.getSchema()) || currentAttrMap.get(templateVirAttr.getSchema()).getValues().isEmpty())) {
            AttrTO evaluated = evaluateAttr(templateVirAttr, jexlContext);
            if (!evaluated.getValues().isEmpty()) {
                anyTO.getVirAttrs().add(evaluated);
                jexlContext.set(evaluated.getSchema(), evaluated.getValues().get(0));
            }
        }
    }
    anyTO.getResources().addAll(template.getResources());
    anyTO.getAuxClasses().addAll(template.getAuxClasses());
}
Also used : AttrTO(org.apache.syncope.common.lib.to.AttrTO) MapContext(org.apache.commons.jexl3.MapContext)

Example 40 with MapContext

use of org.apache.commons.jexl3.MapContext in project syncope by apache.

the class DerAttrHandlerImpl method getValues.

private Map<DerSchema, String> getValues(final Any<?> any, final Set<DerSchema> schemas) {
    Map<DerSchema, String> result = new HashMap<>(schemas.size());
    for (DerSchema schema : schemas) {
        JexlContext jexlContext = new MapContext();
        JexlUtils.addPlainAttrsToContext(any.getPlainAttrs(), jexlContext);
        JexlUtils.addFieldsToContext(any, jexlContext);
        result.put(schema, JexlUtils.evaluate(schema.getExpression(), jexlContext));
    }
    return result;
}
Also used : DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) HashMap(java.util.HashMap) JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext)

Aggregations

MapContext (org.apache.commons.jexl2.MapContext)32 MapContext (org.apache.commons.jexl3.MapContext)26 JexlContext (org.apache.commons.jexl2.JexlContext)23 Expression (org.apache.commons.jexl2.Expression)20 JexlEngine (org.apache.commons.jexl2.JexlEngine)20 JexlContext (org.apache.commons.jexl3.JexlContext)17 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)7 Map (java.util.Map)7 HashSet (java.util.HashSet)4 JexlException (org.apache.commons.jexl2.JexlException)4 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)4 JexlExpression (org.apache.commons.jexl3.JexlExpression)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 JexlEngine (org.apache.commons.jexl3.JexlEngine)3 Test (org.junit.jupiter.api.Test)3 Device (org.traccar.model.Device)3 IOException (java.io.IOException)2