001/* 002 * Copyright 2016 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.contentio.synchronize.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.contentio.synchronize.SynchronizableContentsCollection; 026import org.ametys.plugins.contentio.synchronize.search.field.CollectionSearchField; 027 028/** 029 * {@link Query} testing {@link SynchronizableContentsCollection}s of contents. 030 */ 031public class CollectionsQuery implements Query 032{ 033 private Operator _operator; 034 private List<String> _collectionIds; 035 036 /** 037 * Build a CollectionsQuery. 038 * @param ids The ids of the {@link SynchronizableContentsCollection}s to test 039 */ 040 public CollectionsQuery(String... ids) 041 { 042 this(Operator.EQ, ids); 043 } 044 045 /** 046 * Build a CollectionsQuery. 047 * @param ids The ids of the {@link SynchronizableContentsCollection}s to test 048 */ 049 public CollectionsQuery(Collection<String> ids) 050 { 051 this(Operator.EQ, ids); 052 } 053 054 /** 055 * Build a CollectionsQuery. 056 * @param operator The query operator (can be EQ or NE). 057 * @param ids The ids of the {@link SynchronizableContentsCollection}s to test 058 */ 059 public CollectionsQuery(Operator operator, String... ids) 060 { 061 this(operator, Arrays.asList(ids)); 062 } 063 064 /** 065 * Build a CollectionsQuery. 066 * @param operator The query operator (can be EQ or NE). 067 * @param ids The ids of the {@link SynchronizableContentsCollection}s to test 068 */ 069 public CollectionsQuery(Operator operator, Collection<String> ids) 070 { 071 if (Operator.EQ != operator && Operator.NE != operator) 072 { 073 throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression."); 074 } 075 076 _operator = operator; 077 _collectionIds = new ArrayList<>(ids); 078 } 079 080 @Override 081 public String build() throws QuerySyntaxException 082 { 083 int count = _collectionIds.size(); 084 StringBuilder query = new StringBuilder(); 085 086 if (_operator == Operator.NE) 087 { 088 query.append('-'); 089 } 090 if (count > 1) 091 { 092 query.append('('); 093 } 094 095 boolean first = true; 096 for (String collectionId : _collectionIds) 097 { 098 if (!first) 099 { 100 query.append(" OR "); 101 } 102 query.append(CollectionSearchField.NAME).append(':').append(collectionId); 103 first = false; 104 } 105 106 if (count > 1) 107 { 108 query.append(')'); 109 } 110 111 return query.toString(); 112 } 113}