001 /*
002 * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003 * Copyright (C) 2011 NightLabs Consulting GmbH
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
017 */
018 package org.cumulus4j.store.query.eval;
019
020 import java.util.Collection;
021 import java.util.Date;
022 import java.util.Map;
023 import java.util.Set;
024
025 import org.cumulus4j.store.query.QueryEvaluator;
026 import org.cumulus4j.store.query.method.MethodEvaluator;
027 import org.datanucleus.query.expression.InvokeExpression;
028 import org.datanucleus.query.expression.PrimaryExpression;
029 import org.datanucleus.query.expression.VariableExpression;
030 import org.datanucleus.query.symbol.Symbol;
031
032 /**
033 * Evaluator handling method invocations like <code>Collection.contains(...)</code>.
034 *
035 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
036 */
037 public class InvokeExpressionEvaluator
038 extends AbstractExpressionEvaluator<InvokeExpression>
039 {
040 public InvokeExpressionEvaluator(QueryEvaluator queryEvaluator, AbstractExpressionEvaluator<?> parent, InvokeExpression expression)
041 {
042 super(queryEvaluator, parent, expression);
043 }
044
045 @Override
046 protected Set<Long> _queryResultDataEntryIDs(ResultDescriptor resultDescriptor)
047 {
048 // The invocationTarget is always the left side of the InvokeExpression. It can be one of the following:
049 // 1) PrimaryExpression
050 // 2) VariableExpression
051 // 3) ParameterExpression
052 // 4) InvokeExpression
053 // 5) null (for static methods or aggregates)
054
055 if (this.getLeft() instanceof PrimaryExpressionEvaluator) {
056 if (!getLeft().getResultSymbols().contains(resultDescriptor.getSymbol()))
057 return null;
058
059 // Evaluate the left-hand expression on which we perform the method invocation
060 PrimaryExpressionEvaluator primaryEval = (PrimaryExpressionEvaluator) this.getLeft();
061 PrimaryExpression primaryExpr = primaryEval.getExpression();
062 Class<?> invocationTargetType = getFieldType(primaryExpr);
063
064 // Find the evaluator to use and invoke it
065 MethodEvaluator eval = createMethodEvaluatorForMethodOfClass(invocationTargetType, getExpression().getOperation());
066 if (eval.requiresComparisonArgument()) {
067 eval.setCompareToArgument(getCompareToArgument());
068 }
069 return eval.evaluate(getQueryEvaluator(), this, primaryExpr, resultDescriptor);
070 }
071 else if (this.getLeft() instanceof VariableExpressionEvaluator) {
072 VariableExpressionEvaluator variableExprEval = (VariableExpressionEvaluator) this.getLeft();
073 VariableExpression variableExpr = variableExprEval.getExpression();
074 Symbol classSymbol = variableExprEval.getExpression().getSymbol();
075 if (classSymbol == null)
076 throw new IllegalStateException("((VariableExpressionEvaluator)this.getLeft()).getExpression().getSymbol() returned null!");
077
078 // Evaluate the left-hand expression on which we perform the method invocation
079 Class<?> invocationTargetType = getQueryEvaluator().getValueType(classSymbol);
080
081 // Find the evaluator to use and invoke it
082 MethodEvaluator eval = createMethodEvaluatorForMethodOfClass(invocationTargetType, getExpression().getOperation());
083 if (eval.requiresComparisonArgument()) {
084 eval.setCompareToArgument(getCompareToArgument());
085 }
086 return eval.evaluate(getQueryEvaluator(), this, variableExpr, resultDescriptor);
087 }
088 else if (this.getLeft() instanceof ParameterExpressionEvaluator) {
089 ParameterExpressionEvaluator paramExprEval = (ParameterExpressionEvaluator) this.getLeft();
090 Symbol classSymbol = paramExprEval.getExpression().getSymbol();
091 if (classSymbol == null)
092 throw new IllegalStateException("((ParameterExpressionEvaluator)this.getLeft()).getExpression().getSymbol() returned null!");
093
094 // Evaluate the left-hand expression on which we perform the method invocation
095 Class<?> invocationTargetType = getQueryEvaluator().getValueType(classSymbol);
096
097 // Find the evaluator to use and invoke it
098 MethodEvaluator eval = createMethodEvaluatorForMethodOfClass(invocationTargetType, getExpression().getOperation());
099 if (eval.requiresComparisonArgument()) {
100 eval.setCompareToArgument(getCompareToArgument());
101 }
102 return eval.evaluate(getQueryEvaluator(), this, paramExprEval.getExpression(), resultDescriptor);
103 }
104 else if (this.getLeft() instanceof InvokeExpressionEvaluator) {
105 // TODO support this.getLeft() instanceof InvokeExpressionEvaluator
106 throw new UnsupportedOperationException("NYI: this.getLeft() instanceof InvokeExpressionEvaluator");
107 }
108 else if (this.getLeft() instanceof SubqueryExpressionEvaluator) {
109 // TODO support this.getLeft() instanceof SubqueryExpressionEvaluator
110 throw new UnsupportedOperationException("NYI: this.getLeft() instanceof SubqueryExpressionEvaluator");
111 }
112 else if (this.getLeft() == null) {
113 // TODO support this.getLeft() == null (static method call)
114 throw new UnsupportedOperationException("NYI: this.getLeft() == null");
115 }
116
117 throw new UnsupportedOperationException("NYI");
118 }
119
120 private MethodEvaluator createMethodEvaluatorForMethodOfClass(Class cls, String method) {
121 String className = cls.getName();
122 if (Collection.class.isAssignableFrom(cls)) {
123 className = Collection.class.getName(); // Sub-types go through Collection
124 }
125 else if (Map.class.isAssignableFrom(cls)) {
126 className = Map.class.getName(); // Sub-types go through Map
127 }
128 else if (Date.class.isAssignableFrom(cls)) {
129 className = Date.class.getName(); // Sub-types go through Date
130 }
131 return ExpressionHelper.createMethodEvaluatorForMethodOfClass(getQueryEvaluator().getStoreManager(),
132 getQueryEvaluator().getClassLoaderResolver(), className, method);
133 }
134
135 private Object getCompareToArgument() {
136 if (!(getParent() instanceof ComparisonExpressionEvaluator))
137 throw new UnsupportedOperationException(this.getExpression().toString() + " needs to be compared to something as it does not have a boolean result! this.getParent() is thus expected to be a ComparisonExpressionEvaluator, but is: " + getParent());
138
139 ComparisonExpressionEvaluator comparisonExpressionEvaluator = (ComparisonExpressionEvaluator) getParent();
140
141 Object compareToArgument;
142 if (this == comparisonExpressionEvaluator.getLeft())
143 compareToArgument = comparisonExpressionEvaluator.getRightCompareToArgument();
144 else if (this == comparisonExpressionEvaluator.getRight())
145 compareToArgument = comparisonExpressionEvaluator.getLeftCompareToArgument();
146 else
147 throw new UnsupportedOperationException("this is neither parent.left nor parent.right!");
148
149 return compareToArgument;
150 }
151 }