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 implements Query 027{ 028 029 private static final Map<String, String> NAME_TO_FIELD = new HashMap<>(); 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 /** The Dublin Core metadata name. */ 049 protected String _metadata; 050 051 /** The value to test. */ 052 protected String _value; 053 054 /** 055 * Create a Dublin Core query. 056 * @param metadata The metadata name, lower-cased and wihout 'dc:' prefix, such as "title" or "date". 057 * @param value The value to test. 058 */ 059 public DublinCoreQuery(String metadata, String value) 060 { 061 if (!NAME_TO_FIELD.containsKey(metadata)) 062 { 063 throw new IllegalArgumentException(metadata + " is not a valid Dublin Core metadata."); 064 } 065 066 _metadata = metadata; 067 _value = value; 068 } 069 070 @Override 071 public String build() throws QuerySyntaxException 072 { 073 // TODO Params? 074// String escapedValue = escapeStringValue(_value); 075 076 StringBuilder query = new StringBuilder(); 077 078 query.append(NAME_TO_FIELD.get(_metadata)).append(":(").append(_value).append(')'); 079 080 return query.toString(); 081 } 082 083 @Override 084 public int hashCode() 085 { 086 final int prime = 31; 087 int result = 1; 088 result = prime * result + ((_metadata == null) ? 0 : _metadata.hashCode()); 089 result = prime * result + ((_value == null) ? 0 : _value.hashCode()); 090 return result; 091 } 092 093 @Override 094 public boolean equals(Object obj) 095 { 096 if (this == obj) 097 { 098 return true; 099 } 100 if (obj == null) 101 { 102 return false; 103 } 104 if (getClass() != obj.getClass()) 105 { 106 return false; 107 } 108 DublinCoreQuery other = (DublinCoreQuery) obj; 109 if (_metadata == null) 110 { 111 if (other._metadata != null) 112 { 113 return false; 114 } 115 } 116 else if (!_metadata.equals(other._metadata)) 117 { 118 return false; 119 } 120 if (_value == null) 121 { 122 if (other._value != null) 123 { 124 return false; 125 } 126 } 127 else if (!_value.equals(other._value)) 128 { 129 return false; 130 } 131 return true; 132 } 133}