List of usage examples for org.springframework.expression.spel.standard SpelExpressionParser SpelExpressionParser
public SpelExpressionParser()
From source file:gov.nih.nci.calims2.ui.generic.crud.CRUDTableDecorator.java
/** * Parse the value expressions for the given columns. * /* ww w. j a v a2 s . c o m*/ * @param columns The columns to process * @return A Map of column names to the expressions. */ private Map<String, Expression> getColumnExpressions(List<Column> columns) { ExpressionParser expressionParser = new SpelExpressionParser(); Map<String, Expression> expressions = new HashMap<String, Expression>(); for (Column column : columns) { if (column.getValue() != null) { expressions.put(column.getName(), expressionParser.parseExpression(column.getValue())); } } return expressions; }
From source file:com.px100systems.data.browser.controller.MainController.java
@SuppressWarnings("unchecked") private void browse(ModelAndView mav, String entityName, Integer tenantId, String filter, Integer orderBy, Integer order, String fields) { mav.setViewName("main"); mav.addObject("cluster", clusterName); LinkedHashMap<Integer, String> tenants = dataStorage.getTenants(); mav.addObject("tenants", tenants); mav.addObject("tenantId", tenantId != null ? tenantId : tenants.keySet().iterator().next()); Set<String> entities = dataStorage.entityNames(); mav.addObject("entities", entities); mav.addObject("entityName", entityName != null ? entityName : entities.iterator().next()); mav.addObject("filter", filter == null ? "" : filter.replace("\"", """)); mav.addObject("orderBy", orderBy); mav.addObject("order", order); mav.addObject("fields", fields == null ? "" : fields.replace("\"", """)); LinkedHashMap<Long, String> result = new LinkedHashMap<Long, String>(); mav.addObject("result", result); if (entityName != null) try {//from w ww .j a v a 2s. c om Class entityClass = dataStorage.entityClass(entityName); String fieldsEL = fields != null && !fields.trim().isEmpty() ? fields.trim() : "${#root.toString()}"; Expression expression = new SpelExpressionParser().parseExpression(fieldsEL, new ELTemplateParserContext()); Transaction tx = dataStorage.transaction(tenantId); Criteria criteria = parseCriteria(filter); for (Object row : tx.find(entityClass, criteria, Collections.singletonList(parseOrderBy(orderBy == 2, order == 2)), MAX_ROWS)) { result.put(((StoredBean) row).getId(), expression.getValue(new SpringELCtx(row), String.class).replace("\"", """)); } } catch (Exception e) { mav.addObject("error", e.getMessage()); } }
From source file:com.px100systems.util.serialization.SerializationDefinition.java
private SerializationDefinition(Class<?> cls) { definitions.put(cls, this); if (cls.getName().startsWith("java")) throw new RuntimeException("System classes are not supported: " + cls.getSimpleName()); try {//from ww w . ja v a2s. co m constructor = cls.getConstructor(); } catch (NoSuchMethodException e) { throw new RuntimeException("Missing no-arg constructor: " + cls.getSimpleName()); } serializingSetter = ReflectionUtils.findMethod(cls, "setSerializing", boolean.class); for (Class<?> c = cls; c != null && !c.equals(Object.class); c = c.getSuperclass()) { for (Field field : c.getDeclaredFields()) { if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) continue; FieldDefinition fd = new FieldDefinition(); fd.name = field.getName(); fd.type = field.getType(); if (fd.type.isPrimitive()) throw new RuntimeException("Primitives are not supported: " + fd.type.getSimpleName()); Calculated calc = field.getAnnotation(Calculated.class); if (!fd.type.equals(Integer.class) && !fd.type.equals(Long.class) && !fd.type.equals(Double.class) && !fd.type.equals(Boolean.class) && !fd.type.equals(Date.class) && !fd.type.equals(String.class)) if (fd.type.equals(List.class) || fd.type.equals(Set.class)) { SerializedCollection sc = field.getAnnotation(SerializedCollection.class); if (sc == null) throw new RuntimeException( cls.getSimpleName() + "." + fd.name + " is missing @SerializedCollection"); if (calc != null) throw new RuntimeException(cls.getSimpleName() + "." + fd.name + " cannot have a calculator because it is a collection"); fd.collectionType = sc.type(); fd.primitive = fd.collectionType.equals(Integer.class) || fd.collectionType.equals(Long.class) || fd.collectionType.equals(Double.class) || fd.collectionType.equals(Boolean.class) || fd.collectionType.equals(Date.class) || fd.collectionType.equals(String.class); if (!fd.primitive) { if (cls.getName().startsWith("java")) throw new RuntimeException(cls.getSimpleName() + "." + fd.name + ": system collection types are not supported: " + fd.collectionType.getSimpleName()); if (!definitions.containsKey(fd.collectionType)) new SerializationDefinition(fd.collectionType); } } else { if (cls.getName().startsWith("java")) throw new RuntimeException( "System classes are not supported: " + fd.type.getSimpleName()); if (!definitions.containsKey(fd.type)) new SerializationDefinition(fd.type); } else fd.primitive = true; try { fd.accessor = c.getMethod(PropertyAccessor.methodName("get", fd.name)); } catch (NoSuchMethodException e) { throw new RuntimeException(cls.getSimpleName() + "." + fd.name + " is missing getter"); } try { fd.mutator = c.getMethod(PropertyAccessor.methodName("set", fd.name), fd.type); } catch (NoSuchMethodException e) { throw new RuntimeException(cls.getSimpleName() + "." + fd.name + " is missing setter"); } if (calc != null) fd.calculator = new SpelExpressionParser().parseExpression(calc.value()); fields.add(fd); } for (Method method : c.getDeclaredMethods()) if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()) && method.getName().startsWith("get") && method.isAnnotationPresent(SerializedGetter.class)) { FieldDefinition fd = new FieldDefinition(); fd.name = method.getName().substring(3); fd.name = fd.name.substring(0, 1).toLowerCase() + fd.name.substring(1); fd.type = method.getReturnType(); fd.primitive = fd.type != null && (fd.type.equals(Integer.class) || fd.type.equals(Long.class) || fd.type.equals(Double.class) || fd.type.equals(Boolean.class) || fd.type.equals(Date.class) || fd.type.equals(String.class)); if (!fd.primitive) throw new RuntimeException("Not compact-serializable getter type: " + (fd.type == null ? "void" : fd.type.getSimpleName())); fd.accessor = method; gettersOnly.add(fd); } } }
From source file:com.px100systems.data.browser.controller.MainController.java
private Criteria parseCriteria(String filter) { Criteria criteria = null;/* w ww . j av a 2 s. c o m*/ filter = filter.trim(); if (!filter.isEmpty()) { String criteriaPrefix = "T(com.px100systems.data.core.Criteria)."; while (filter.indexOf(" (") != -1) filter = filter.replace(" (", "("); filter = filter.replace("icontainsText(", "icText("); for (String token : new String[] { "containsText", "startsWithText", "endsWithText", "between", "and", "or", "not", "eq", "ne", "lt", "le", "gt", "ge", "in" }) filter = filter.replace(token + "(", criteriaPrefix + token + "("); filter = filter.replace("icText(", criteriaPrefix + "icontainsText("); criteria = new SpelExpressionParser().parseExpression(filter).getValue(Criteria.class); } return criteria; }
From source file:com.px100systems.util.SpringELCtx.java
/** * Iterator-based sum using Spring EL//from ww w . j a va2 s . com * @param valueExpression how to get element values to sum * @param groupExpression what to group by elements by (optional) * @param iterator the iterator to iterate over * @return the sum */ public static Map<String, Double> isum(String valueExpression, String groupExpression, Iterator<?> iterator) { Expression val = new SpelExpressionParser().parseExpression(valueExpression); Expression grp = groupExpression == null ? null : new SpelExpressionParser().parseExpression(groupExpression); return isum(item -> new Double(val.getValue(new SpringELCtx(item)).toString()), grp == null ? null : item -> grp.getValue(new SpringELCtx(item), String.class), iterator); }
From source file:org.opentides.util.CrudUtil.java
/** * This method evaluates the given expression from the object. * This method now uses Spring Expression Language (SpEL). * /*from ww w . j a v a 2 s. c o m*/ * @param obj * @param expression * @return */ public static Boolean evaluateExpression(Object obj, String expression) { if (StringUtil.isEmpty(expression)) return false; try { ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression(expression); return exp.getValue(obj, Boolean.class); } catch (Exception e) { _log.debug("Failed to evaluate expression [" + expression + "] for object [" + obj.getClass() + "]."); _log.debug(e.getMessage()); return false; } }
From source file:com.px100systems.util.SpringELCtx.java
/** * Iterator-based average using Spring EL * @param valueExpression how to get element values to sum * @param groupExpression what to group by elements by (optional) * @param iterator the iterator to iterate over * @return the average/* w w w .jav a 2s. c o m*/ */ public static Map<String, Double> iaverage(String valueExpression, String groupExpression, Iterator<?> iterator) { Expression val = new SpelExpressionParser().parseExpression(valueExpression); Expression grp = groupExpression == null ? null : new SpelExpressionParser().parseExpression(groupExpression); return iaverage(item -> new Double(val.getValue(new SpringELCtx(item)).toString()), grp == null ? null : item -> grp.getValue(new SpringELCtx(item), String.class), iterator); }
From source file:com.px100systems.util.SpringELCtx.java
/** * Iterator-based minimum using Spring EL * @param valueExpression how to get element values to sum * @param groupExpression what to group by elements by (optional) * @param iterator the iterator to iterate over * @return the minimum//from w ww . j a va2 s . co m */ public static Map<String, Double> imin(String valueExpression, String groupExpression, Iterator<?> iterator) { Expression val = new SpelExpressionParser().parseExpression(valueExpression); Expression grp = groupExpression == null ? null : new SpelExpressionParser().parseExpression(groupExpression); return imin(item -> new Double(val.getValue(new SpringELCtx(item)).toString()), grp == null ? null : item -> grp.getValue(new SpringELCtx(item), String.class), iterator); }
From source file:com.px100systems.util.SpringELCtx.java
/** * Iterator-based maximum using Spring EL * @param valueExpression how to get element values to sum * @param groupExpression what to group by elements by (optional) * @param iterator the iterator to iterate over * @return the maximum//from www .j a v a2 s . c o m */ public static Map<String, Double> imax(String valueExpression, String groupExpression, Iterator<?> iterator) { Expression val = new SpelExpressionParser().parseExpression(valueExpression); Expression grp = groupExpression == null ? null : new SpelExpressionParser().parseExpression(groupExpression); return imax(item -> new Double(val.getValue(new SpringELCtx(item)).toString()), grp == null ? null : item -> grp.getValue(new SpringELCtx(item), String.class), iterator); }
From source file:com.px100systems.util.SpringELCtx.java
/** * Iterator-based count using Spring EL/*from w ww . j a v a 2 s . c o m*/ * @param groupExpression what to group by elements by (optional) * @param iterator the iterator to iterate over * @return the count */ public static Map<String, Long> icount(String groupExpression, Iterator<?> iterator) { Expression grp = groupExpression == null ? null : new SpelExpressionParser().parseExpression(groupExpression); return icount(grp == null ? null : item -> grp.getValue(new SpringELCtx(item), String.class), iterator); }