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.model; 017 018import java.util.Collection; 019import java.util.Collections; 020import java.util.HashSet; 021import java.util.LinkedHashMap; 022import java.util.Map; 023import java.util.Set; 024 025/** 026 * Default implementation of a {@link SearchModel}. 027 */ 028public class DefaultSearchModel implements SearchModel 029{ 030 031 /** The content types of this search model. */ 032 protected Set<String> _cTypes; 033 /** The content types excluded from this search model. */ 034 protected Set<String> _excludedCTypes; 035 036 /** The simple search criteria, indexed by ID. */ 037 protected Map<String, SearchCriterion> _searchCriteria; 038 /** The faceted search criteria, indexed by ID. */ 039 protected Map<String, SearchCriterion> _facetedCriteria; 040// /** The advanced search criteria, indexed by ID. */ 041// protected Map<String, SearchCriterion> _advancedSearchCriteria; 042 /** The simple search criteria, indexed by ID. */ 043 protected Map<String, ResultField> _resultFields; 044 045 public Set<String> getContentTypes(Map<String, Object> contextualParameters) 046 { 047 return Collections.unmodifiableSet(_cTypes); 048 } 049 050 /** 051 * Set the content types of the models 052 * @param cTypes The content types. 053 */ 054 public void setContentTypes(Set<String> cTypes) 055 { 056 _cTypes = new HashSet<>(cTypes); 057 } 058 059 public Set<String> getExcludedContentTypes(Map<String, Object> contextualParameters) 060 { 061 return Collections.unmodifiableSet(_excludedCTypes); 062 } 063 064 /** 065 * Set the content types to exclude 066 * @param cTypes The content types to exclude 067 */ 068 public void setExcludedContentTypes(Set<String> cTypes) 069 { 070 _excludedCTypes = new HashSet<>(cTypes); 071 } 072 073 public Map<String, ? extends SearchCriterion> getCriteria(Map<String, Object> contextualParameters) 074 { 075 return Collections.unmodifiableMap(_searchCriteria); 076 } 077 078// public void setCriteria(Collection<SearchCriterion> criteria) 079 /** 080 * Set the criteria 081 * @param criteria The criterion list 082 */ 083 public void setCriteria(Collection<? extends SearchCriterion> criteria) 084 { 085 _searchCriteria = new LinkedHashMap<>(); 086 for (SearchCriterion criterion : criteria) 087 { 088 _searchCriteria.put(criterion.getId(), criterion); 089 } 090// criteria.stream().collect(Collectors.toMap(SearchCriterion::getId, Function.identity())); 091 } 092 093// public Map<String, ? extends SearchCriterion> getAdvancedCriteria(Map<String, Object> contextualParameters) 094// { 095// return Collections.unmodifiableMap(_advancedSearchCriteria); 096// } 097 098 public Map<String, ? extends SearchCriterion> getFacetedCriteria(Map<String, Object> contextualParameters) 099 { 100 return Collections.unmodifiableMap(_facetedCriteria); 101 } 102 103 /** 104 * Set the faceted criteria 105 * @param criteria The list of faceted criterion 106 */ 107 public void setFacetedCriteria(Collection<? extends SearchCriterion> criteria) 108 { 109 _facetedCriteria = new LinkedHashMap<>(); 110 for (SearchCriterion criterion : criteria) 111 { 112 _facetedCriteria.put(criterion.getId(), criterion); 113 } 114 } 115 116 public Map<String, ? extends ResultField> getResultFields(Map<String, Object> contextualParameters) 117 { 118 return Collections.unmodifiableMap(_resultFields); 119 } 120 121 /** 122 * Set the result fields 123 * @param fields The list of result field 124 */ 125 public void setResultFields(Collection<? extends ResultField> fields) 126 { 127 _resultFields = new LinkedHashMap<>(); 128 for (ResultField field : fields) 129 { 130 _resultFields.put(field.getId(), field); 131 } 132 } 133 134// public boolean isValidCriterionSystemProperty(String propertyName) 135// { 136// // TODO Auto-generated method stub 137// return false; 138// } 139// 140// public boolean isValidResultSystemProperty(String propertyName) 141// { 142// // TODO Auto-generated method stub 143// return false; 144// } 145 146}