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 project property. 029 */ 030public class ProjectQuery implements Query 031{ 032 private Operator _operator; 033 private List<String> _ids; 034 035 /** 036 * Build a ProjectQuery to test if the project property exits 037 */ 038 public ProjectQuery() 039 { 040 this(Operator.EQ, "*"); 041 } 042 043 /** 044 * Build a ProjectQuery to test if the project property is equal to one of the given project ids 045 * @param ids the project ids. 046 */ 047 public ProjectQuery(String... ids) 048 { 049 this(Operator.EQ, ids); 050 } 051 052 /** 053 * Build a ProjectQuery to test if the project property is equal to one of the given project ids 054 * @param ids the project ids. 055 */ 056 public ProjectQuery(Collection<String> ids) 057 { 058 this(Operator.EQ, ids); 059 } 060 061 /** 062 * Build a ProjectQuery to test if the project property is equal or different to one of the given project ids 063 * @param operator the operator (equal ot not-equal) 064 * @param ids the project ids. 065 */ 066 public ProjectQuery(Operator operator, String... ids) 067 { 068 this(operator, Arrays.asList(ids)); 069 } 070 071 /** 072 * Build a ProjectQuery. 073 * @param operator the operator. 074 * @param ids the project ids. 075 */ 076 public ProjectQuery(Operator operator, Collection<String> ids) 077 { 078 if (Operator.EQ != operator && Operator.NE != operator) 079 { 080 throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression."); 081 } 082 083 _operator = operator; 084 _ids = new ArrayList<>(ids); 085 } 086 087 @Override 088 public String build() throws QuerySyntaxException 089 { 090 int count = _ids.size(); 091 092 StringBuilder sb = new StringBuilder(); 093 094 if (_operator == Operator.NE) 095 { 096 sb.append('-'); 097 } 098 if (count > 1) 099 { 100 sb.append('('); 101 } 102 103 boolean first = true; 104 for (String id : _ids) 105 { 106 if (!first) 107 { 108 sb.append(" OR "); 109 } 110 sb.append(SolrWorkspacesConstants.PROJECT_ID).append(":\"").append(id).append("\""); 111 first = false; 112 } 113 114 if (count > 1) 115 { 116 sb.append(')'); 117 } 118 119 return sb.toString(); 120 } 121}