001/* 002 * Copyright 2017 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.plugins.workspaces.search.query; 017 018import java.util.ArrayList; 019import java.util.Arrays; 020import java.util.Collection; 021import java.util.List; 022 023import org.ametys.cms.search.query.Query; 024import org.ametys.cms.search.query.QuerySyntaxException; 025import org.ametys.plugins.workspaces.indexing.solr.SolrWorkspacesConstants; 026 027/** 028 * Represents a {@link Query} testing the keywords property of a project resource. 029 */ 030public class KeywordQuery implements Query 031{ 032 private Operator _operator; 033 private List<String> _keywords; 034 035 /** 036 * Build a KeywordQuery to test if the keywords property is equal to one of the given keywords 037 * @param keywords the keywords. 038 */ 039 public KeywordQuery(String... keywords) 040 { 041 this(Operator.EQ, keywords); 042 } 043 044 /** 045 * Build a KeywordQuery to test if the keywords property is equal to one of the given keywords 046 * @param keywords the keywords. 047 */ 048 public KeywordQuery(Collection<String> keywords) 049 { 050 this(Operator.EQ, keywords); 051 } 052 053 /** 054 * Build a KeywordQuery to test if the keywords property is equal or different to one of the given keywords 055 * @param operator the operator (equal ot not-equal) 056 * @param keywords the keywords. 057 */ 058 public KeywordQuery(Operator operator, String... keywords) 059 { 060 this(operator, Arrays.asList(keywords)); 061 } 062 063 /** 064 * Build a KeywordQuery. 065 * @param operator the operator. 066 * @param keywords the keywords. 067 */ 068 public KeywordQuery(Operator operator, Collection<String> keywords) 069 { 070 if (Operator.EQ != operator && Operator.NE != operator) 071 { 072 throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression."); 073 } 074 075 _operator = operator; 076 _keywords = new ArrayList<>(keywords); 077 } 078 079 @Override 080 public String build() throws QuerySyntaxException 081 { 082 int count = _keywords.size(); 083 084 StringBuilder sb = new StringBuilder(); 085 086 if (_operator == Operator.NE) 087 { 088 sb.append('-'); 089 } 090 if (count > 1) 091 { 092 sb.append('('); 093 } 094 095 boolean first = true; 096 for (String keyword : _keywords) 097 { 098 if (!first) 099 { 100 sb.append(" OR "); 101 } 102 sb.append(SolrWorkspacesConstants.KEYWORDS).append(':').append(keyword); 103 first = false; 104 } 105 106 if (count > 1) 107 { 108 sb.append(')'); 109 } 110 111 return sb.toString(); 112 } 113 114}