org.bonitasoft.engine.expression.impl.ConstantExpressionExecutorStrategy Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2019 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.bonitasoft.engine.expression.impl;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import org.bonitasoft.engine.expression.ContainerState;
import org.bonitasoft.engine.expression.ExpressionExecutorStrategy;
import org.bonitasoft.engine.expression.exception.SExpressionEvaluationException;
import org.bonitasoft.engine.expression.exception.SInvalidExpressionException;
import org.bonitasoft.engine.expression.model.ExpressionKind;
import org.bonitasoft.engine.expression.model.SExpression;
/**
* @author Zhao na
* @author Baptiste Mesta
* @author Celine Souchet
* @author Matthieu Chaffotte
*/
public class ConstantExpressionExecutorStrategy implements ExpressionExecutorStrategy {
private static final String REGEX_PARSE_DATE = "(\\d{4})(-([01]\\d)((-([0-3]\\d)(T(\\d\\d):(\\d\\d)(((:(\\d\\d))?(\\.(\\d\\d))?(([\\+-])(\\d\\d):(\\d\\d))?)?)?)?)?)?)?";
@Override
public void validate(final SExpression expression) throws SInvalidExpressionException {
if ("".equals(expression.getContent().trim())) {
throw new SInvalidExpressionException("The expression content cannot be empty. Expression : " + expression,
expression.getName());
}
}
@Override
public ExpressionKind getExpressionKind() {
return KIND_CONSTANT;
}
@Override
public Object evaluate(final SExpression expression, final Map context,
final Map resolvedExpressions,
final ContainerState containerState) throws SExpressionEvaluationException {
final String expressionContent = expression.getContent();
Serializable result;
final String returnType = expression.getReturnType();
// here need to improve
try {
if (Boolean.class.getName().equals(returnType)) {
result = Boolean.parseBoolean(expressionContent);
} else if (Long.class.getName().equals(returnType)) {
result = Long.parseLong(expressionContent);
} else if (Double.class.getName().equals(returnType)) {
result = Double.parseDouble(expressionContent);
} else if (Float.class.getName().equals(returnType)) {
result = Float.parseFloat(expressionContent);
} else if (Integer.class.getName().equals(returnType)) {
result = Integer.parseInt(expressionContent);
} else if (String.class.getName().equals(returnType)) {
result = expressionContent;
} else if (Date.class.getName().equals(returnType)) { // "2013-01-02T02:42:12.17+02:00"
result = parseDate(expressionContent);
} else if (LocalDate.class.getName().equals(returnType)) {
result = LocalDate.parse(expressionContent);
} else if (LocalDateTime.class.getName().equals(returnType)) {
result = LocalDateTime.parse(expressionContent);
} else if (OffsetDateTime.class.getName().equals(returnType)) {
result = OffsetDateTime.parse(expressionContent);
} else {
throw new SExpressionEvaluationException(
"Unknown return type: " + returnType + " for expression " + expression.getName() + " : "
+ expressionContent,
expression.getName());
}
} catch (final NumberFormatException e) {
throw new SExpressionEvaluationException(
"The content of the expression \"" + expression.getName() + "\" is not a number :"
+ expressionContent,
e,
expression.getName());
}
return result;
}
@Override
public List