001/* 002 * Copyright 2015 Anyware Services 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 */ 016package org.ametys.cms.search.query; 017 018import java.util.HashMap; 019import java.util.Map; 020 021/** 022 * Represents a search predicate. 023 */ 024public interface Query 025{ 026 027 /** Enumeration of available operators in {@link Query} */ 028 public enum Operator 029 { 030 /** Operator testing the existence of a property. */ 031 EXISTS("exists"), 032 /** Constant of test's operator for textual search, on unstemmed terms. Works only for type String */ 033 SEARCH("search"), 034 /** Constant of test's operator for textual search, on stemmed terms. Works only for type String */ 035 SEARCH_STEMMED("searchStemmed"), 036 /** Constant of test's operator for 'like' comparison. Works only for type String */ 037 LIKE("like"), 038 /** Constant of test's operator for 'less than' comparison */ 039 LT("lt"), 040 /** Constant of test's operator for 'less than or equals to' comparison */ 041 LE("le"), 042 /** Constant of test's operator for 'greater than' comparison */ 043 GT("gt"), 044 /** Constant of test's operator for 'greater than or equals to' comparison */ 045 GE("ge"), 046 /** Constant of test's operator for 'equals to' comparison */ 047 EQ("eq"), 048 /** Constant of test's operator for 'not equals to' comparison */ 049 NE("ne"); 050 051 private static Map<String, Operator> _OP_NAMES = new HashMap<>(); 052 static 053 { 054 for (Operator op : values()) 055 { 056 _OP_NAMES.put(op.getName(), op); 057 } 058 } 059 060 private final String _name; 061 062 Operator(String name) 063 { 064 _name = name; 065 } 066 067 /** 068 * Get the operator name. 069 * @return the operator name. 070 */ 071 public String getName() 072 { 073 return _name; 074 } 075 076 /** 077 * Get an Operator object from its name. 078 * @param name the operator name. 079 * @return the operator object, or null if not found. 080 */ 081 public static Operator fromName(String name) 082 { 083 if (_OP_NAMES.containsKey(name)) 084 { 085 return _OP_NAMES.get(name); 086 } 087 throw new IllegalArgumentException("No operator with code '" + name + "'"); 088 } 089 } 090 091 /** Enumeration of available logical operators in {@link Query} */ 092 public enum LogicalOperator 093 { 094 /** Logical operator AND */ 095 AND, 096 /** Logical operator OR */ 097 OR 098 } 099 100 /** 101 * Build the solr query string representing the Query object. 102 * @return the solr query string representing the Query object. 103 * @throws QuerySyntaxException if the query can't be built because of a syntax error. 104 */ 105 String build() throws QuerySyntaxException; 106 107}