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.cms.search.query;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.Collection;
021import java.util.List;
022
023import org.ametys.cms.content.indexing.solr.SolrFieldNames;
024
025/**
026 * Query testing the contentId of a resource (content attachment).
027 */
028public class ContentAttachmentQuery implements Query
029{
030    /** The content ids */
031    protected List<String> _contentIds;
032    
033    /**
034     * Create an ContentAttachmentQuery which searches on resources attached to the given content.
035     * @param contentIds the content ids
036     */
037    public ContentAttachmentQuery(String... contentIds)
038    {
039        this(Arrays.asList(contentIds));
040    }
041    
042    /**
043     * Create an ContentAttachmentQuery which searches on resources attached to the given content.
044     * @param contentIds the content ids
045     */
046    public ContentAttachmentQuery(Collection<String> contentIds)
047    {
048        _contentIds = new ArrayList<>(contentIds);
049    }
050    
051    @Override
052    public String build() throws QuerySyntaxException
053    {
054        int count = _contentIds.size();
055        
056        StringBuilder query = new StringBuilder();
057        
058        if (count > 1)
059        {
060            query.append('(');
061        }
062        
063        boolean first = true;
064        for (String contentId : _contentIds)
065        {
066            if (!first)
067            {
068                query.append(" OR ");
069            }
070            query.append(SolrFieldNames.ATTACHMENT_CONTENT_ID).append(":\"").append(contentId).append('"');
071            first = false;
072        }
073        
074        if (count > 1)
075        {
076            query.append(')');
077        }
078        
079        return query.toString();
080    }
081
082    @Override
083    public int hashCode()
084    {
085        final int prime = 31;
086        int result = 1;
087        result = prime * result + ((_contentIds == null) ? 0 : _contentIds.hashCode());
088        return result;
089    }
090
091    @Override
092    public boolean equals(Object obj)
093    {
094        if (this == obj)
095        {
096            return true;
097        }
098        if (obj == null)
099        {
100            return false;
101        }
102        if (getClass() != obj.getClass())
103        {
104            return false;
105        }
106        ContentAttachmentQuery other = (ContentAttachmentQuery) obj;
107        if (_contentIds == null)
108        {
109            if (other._contentIds != null)
110            {
111                return false;
112            }
113        }
114        else if (!_contentIds.equals(other._contentIds))
115        {
116            return false;
117        }
118        return true;
119    }
120}