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