001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.serverimpl.query;
017:
018: import org.outerj.daisy.repository.query.ValueExpression;
019: import org.outerj.daisy.repository.query.QueryException;
020: import org.outerj.daisy.repository.query.EvaluationContext;
021: import org.outerj.daisy.repository.Document;
022: import org.outerj.daisy.repository.Version;
023: import org.outerj.daisy.repository.ValueType;
024: import org.outerj.daisy.repository.Repository;
025: import org.outerj.daisy.query.model.ValueExpr;
026: import org.outerj.daisy.query.model.PredicateExpr;
027: import org.outerj.daisy.query.model.QValueType;
028: import org.outerj.daisy.query.model.ExprDocData;
029: import org.outerj.daisy.query.EvaluationInfo;
030: import org.outerj.daisy.query.QueryContext;
031: import org.outerj.daisy.query.ExtQueryContext;
032:
033: public class ValueExpressionImpl implements ValueExpression {
034: private ValueExpr valueExpr;
035: private PredicateExpr predicateExpr;
036: private QValueType qValueType;
037: private ValueType valueType;
038: private QueryContext queryContext;
039:
040: protected ValueExpressionImpl(ValueExpr expr,
041: QueryContext queryContext) {
042: this .queryContext = queryContext;
043: this .valueExpr = expr;
044: qValueType = valueExpr.getValueType();
045: if (qValueType == QValueType.UNDEFINED) {
046: qValueType = QValueType.STRING;
047: }
048: valueType = ValueTypeHelper.queryToFieldValueType(qValueType);
049: }
050:
051: protected ValueExpressionImpl(PredicateExpr expr,
052: QueryContext queryContext) {
053: this .queryContext = queryContext;
054: this .predicateExpr = expr;
055: this .valueType = ValueType.BOOLEAN;
056: }
057:
058: public ValueType getValueType() {
059: return valueType;
060: }
061:
062: public Object evaluate(Document document, Version version)
063: throws QueryException {
064: return evaluate(document, version, version == null,
065: queryContext, null);
066: }
067:
068: public Object evaluate(Document document, Version version,
069: boolean searchLastVersion) throws QueryException {
070: return evaluate(document, version, searchLastVersion,
071: queryContext, null);
072: }
073:
074: public Object evaluate(Document document, Version version,
075: boolean searchLastVersion, Repository repository)
076: throws QueryException {
077: return evaluate(document, version, searchLastVersion,
078: new ExtQueryContext(repository), null);
079: }
080:
081: public Object evaluate(Document document, Version version,
082: boolean searchLastVersion,
083: EvaluationContext evaluationContext) throws QueryException {
084: return evaluate(document, version, searchLastVersion,
085: queryContext, evaluationContext);
086: }
087:
088: public Object evaluate(Document document, Version version,
089: boolean searchLastVersion,
090: EvaluationContext evaluationContext, Repository repository)
091: throws QueryException {
092: return evaluate(document, version, searchLastVersion,
093: new ExtQueryContext(repository), evaluationContext);
094: }
095:
096: private Object evaluate(Document document, Version version,
097: boolean searchLastVersion, QueryContext queryContext,
098: EvaluationContext evaluationContext) throws QueryException {
099: if (evaluationContext == null)
100: evaluationContext = new EvaluationContext();
101: EvaluationInfo evaluationInfo = new EvaluationInfo(
102: queryContext, evaluationContext);
103: evaluationInfo.setSearchLastVersion(searchLastVersion);
104:
105: ExprDocData data = document != null ? new ExprDocData(document,
106: version) : null;
107: if (valueExpr != null) {
108: Object result = valueExpr.evaluate(qValueType, data,
109: evaluationInfo);
110: result = ValueTypeHelper.queryValueToFieldValueType(result,
111: qValueType);
112: return result;
113: } else if (predicateExpr != null) {
114: return predicateExpr.evaluate(data, evaluationInfo);
115: } else {
116: throw new RuntimeException("Impossible situation.");
117: }
118: }
119: }
|