001/* 002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others. 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 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.directory.memory; 020 021import static java.lang.Boolean.TRUE; 022 023import java.util.List; 024import java.util.Map; 025 026import org.nuxeo.ecm.core.query.QueryParseException; 027import org.nuxeo.ecm.core.query.sql.NXQL; 028import org.nuxeo.ecm.core.query.sql.model.Expression; 029import org.nuxeo.ecm.core.query.sql.model.Operand; 030import org.nuxeo.ecm.core.query.sql.model.Operator; 031import org.nuxeo.ecm.core.query.sql.model.Reference; 032import org.nuxeo.ecm.core.storage.ExpressionEvaluator; 033 034/** 035 * Evaluates an expression on a map. 036 * 037 * @since 10.3 038 */ 039public class MapExpressionEvaluator extends ExpressionEvaluator { 040 041 protected static final Long ZERO = Long.valueOf(0); 042 043 protected static final Long ONE = Long.valueOf(1); 044 045 protected Map<String, Object> map; 046 047 public MapExpressionEvaluator() { 048 super(null, null, true); 049 } 050 051 public boolean matchesEntry(Expression expression, Map<String, Object> map) { 052 this.map = map; 053 Object result = walkExpression(expression); 054 return TRUE.equals(result); 055 } 056 057 protected QueryParseException unknownProperty(String name) { 058 return new QueryParseException("No column: " + name); 059 } 060 061 @Override 062 public Boolean walkMixinTypes(List<String> mixins, boolean include) { 063 throw unknownProperty(NXQL.ECM_MIXINTYPE); 064 } 065 066 @Override 067 protected Boolean walkEcmFulltext(String name, Operator op, Operand rvalue) { 068 throw unknownProperty(NXQL.ECM_FULLTEXT); 069 } 070 071 @Override 072 protected Boolean walkEcmPath(Operator op, Operand rvalue) { 073 throw unknownProperty(NXQL.ECM_PATH); 074 } 075 076 @Override 077 public Boolean walkStartsWith(Operand lvalue, Operand rvalue) { 078 throw new QueryParseException("Cannot use operator: " + Operator.STARTSWITH); 079 } 080 081 @Override 082 protected Boolean walkAncestorId(Operator op, Operand rvalue) { 083 throw unknownProperty(NXQL.ECM_ANCESTORID); 084 } 085 086 @Override 087 protected Boolean walkIsTrashed(Operator op, Operand rvalue) { 088 throw unknownProperty(NXQL.ECM_ISTRASHED); 089 } 090 091 @Override 092 public Object walkReference(Reference ref) { 093 if (ref.cast != null) { 094 throw new QueryParseException("Cannot use cast: " + ref); 095 } 096 Object value = map.get(ref.name); 097 if (value instanceof Boolean) { 098 value = (((Boolean) value).booleanValue() ? ONE : ZERO); 099 } 100 return value; 101 } 102 103}