001/*
002 *  Copyright 2020 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.Objects;
019
020import org.ametys.cms.content.indexing.solr.SolrFieldNames;
021import org.ametys.cms.content.indexing.solr.SolrResourceGroupedMimeTypes;
022
023/**
024 * MIME-Type group Query
025 * <br>See {@link SolrResourceGroupedMimeTypes}
026 */
027public class MimeTypeGroupQuery implements Query
028{
029    /** The group MIME-Type to test */
030    protected String _groupMimeType;
031    
032    /**
033     * Creates a MIME-Type group Query
034     * @param groupMimeType The group MIME-Type to test
035     */
036    public MimeTypeGroupQuery(String groupMimeType)
037    {
038        if (!SolrResourceGroupedMimeTypes.isValidGroup(groupMimeType))
039        {
040            throw new IllegalArgumentException(groupMimeType + " is not a valid group of MIME-Type.");
041        }
042        _groupMimeType = groupMimeType;
043    }
044    
045    @Override
046    public String build() throws QuerySyntaxException
047    {
048        return new StringBuilder()
049                .append(SolrFieldNames.RESOURCE_MIME_TYPE_GROUP)
050                .append(':').append(_groupMimeType)
051                .toString();
052    }
053
054    @Override
055    public int hashCode()
056    {
057        return Objects.hash(_groupMimeType);
058    }
059
060    @Override
061    public boolean equals(Object obj)
062    {
063        if (this == obj)
064        {
065            return true;
066        }
067        if (obj == null)
068        {
069            return false;
070        }
071        if (getClass() != obj.getClass())
072        {
073            return false;
074        }
075        MimeTypeGroupQuery other = (MimeTypeGroupQuery) obj;
076        return Objects.equals(_groupMimeType, other._groupMimeType);
077    }
078}