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