001/*
002 *  Copyright 2015 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.HashMap;
019import java.util.Map;
020
021import org.ametys.cms.content.indexing.solr.SolrFieldNames;
022
023/**
024 * Query on a Dublin Core string metadata (all, except date).
025 */
026public class DublinCoreQuery extends AbstractOperatorQuery<String>
027{
028    private static final Map<String, String> NAME_TO_FIELD = new HashMap<>();
029    
030    static
031    {
032        NAME_TO_FIELD.put("title", SolrFieldNames.DC_TITLE);
033        NAME_TO_FIELD.put("creator", SolrFieldNames.DC_CREATOR);
034        NAME_TO_FIELD.put("subject", SolrFieldNames.DC_SUBJECT);
035        NAME_TO_FIELD.put("description", SolrFieldNames.DC_DESCRIPTION);
036        NAME_TO_FIELD.put("publisher", SolrFieldNames.DC_PUBLISHER);
037        NAME_TO_FIELD.put("contributor", SolrFieldNames.DC_CONTRIBUTOR);
038        NAME_TO_FIELD.put("type", SolrFieldNames.DC_TYPE);
039        NAME_TO_FIELD.put("format", SolrFieldNames.DC_FORMAT);
040        NAME_TO_FIELD.put("identifier", SolrFieldNames.DC_IDENTIFIER);
041        NAME_TO_FIELD.put("source", SolrFieldNames.DC_SOURCE);
042        NAME_TO_FIELD.put("language", SolrFieldNames.DC_LANGUAGE);
043        NAME_TO_FIELD.put("relation", SolrFieldNames.DC_RELATION);
044        NAME_TO_FIELD.put("coverage", SolrFieldNames.DC_COVERAGE);
045        NAME_TO_FIELD.put("rights", SolrFieldNames.DC_RIGHTS);
046    }
047    
048    /**
049     * Create a Dublin Core query.
050     * @param metadata The metadata name, lower-cased and wihout 'dc:' prefix, such as "title" or "date".
051     * @param value The value to test.
052     */
053    public DublinCoreQuery(String metadata, String value)
054    {
055        super(NAME_TO_FIELD.get(metadata), Operator.EQ, value);
056        
057        if (getFieldName() == null)
058        {
059            throw new IllegalArgumentException(metadata + " is not a valid Dublin Core metadata.");
060        }
061    }
062}