001/* 002 * Copyright 2018 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.web.frontoffice.search.metamodel.impl; 017 018import java.util.Collection; 019import java.util.Map; 020import java.util.Optional; 021import java.util.stream.Stream; 022 023import org.ametys.cms.contenttype.MetadataType; 024import org.ametys.cms.search.query.DublinCoreQuery; 025import org.ametys.cms.search.query.OrQuery; 026import org.ametys.cms.search.query.Query; 027import org.ametys.cms.search.query.Query.Operator; 028import org.ametys.runtime.i18n.I18nizableText; 029import org.ametys.runtime.parameter.Enumerator; 030import org.ametys.runtime.parameter.StaticEnumerator; 031import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition; 032import org.ametys.web.frontoffice.search.metamodel.Searchable; 033 034/** 035 * {@link SearchCriterionDefinition} for {@link ResourceSearchable} proposing a search criterion on a DublinCore metadata of the resource. 036 */ 037public class DublinCoreSearchCriterionDefinition extends AbstractDefaultSearchCriterionDefinition 038{ 039 private String _metadataName; 040 041 /** 042 * Default constructor 043 * @param id The id 044 * @param pluginName The plugin name 045 * @param label The label 046 * @param type The type 047 * @param enumeratorEntries The enumerator entries 048 * @param searchable the {@link Searchable} 049 * @param metadataName The DC metadata name 050 */ 051 public DublinCoreSearchCriterionDefinition(String id, String pluginName, I18nizableText label, MetadataType type, Optional<Map<String, I18nizableText>> enumeratorEntries, Optional<Searchable> searchable, String metadataName) 052 { 053 super(id, pluginName, label, type, Optional.empty(), Optional.empty(), _getEnumerator(enumeratorEntries), Optional.empty(), Optional.empty(), searchable); 054 _metadataName = metadataName; 055 } 056 057 private static Optional<Enumerator> _getEnumerator(Optional<Map<String, I18nizableText>> entries) 058 { 059 return entries.map(map -> 060 { 061 StaticEnumerator enumerator = new StaticEnumerator(); 062 for (String value : map.keySet()) 063 { 064 enumerator.add(map.get(value), value); 065 } 066 return enumerator; 067 068 }); 069 } 070 071 /** 072 * Potentially retrieve multiple values (as a stream) from an untyped client object 073 * @param value The untyped client object 074 * @return The values 075 */ 076 @SuppressWarnings("unchecked") 077 protected Stream<String> _retrieveValues(Object value) 078 { 079 if (value instanceof String[]) 080 { 081 return Stream.of((String[]) value); 082 } 083 else if (value instanceof Collection< ? >) 084 { 085 return ((Collection<String>) value).stream(); 086 } 087 else if (value instanceof String) 088 { 089 return Stream.of((String) value); 090 } 091 else 092 { 093 throw new IllegalArgumentException("Wrong type for object '" + value + "' : " + value.getClass().getTypeName()); 094 } 095 } 096 097 @Override 098 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 099 { 100 return _retrieveValues(value) 101// .map(ClientUtils::escapeQueryChars) 102 .map(val -> new DublinCoreQuery(_metadataName, val)) 103 .collect(OrQuery.collector()); 104 } 105}