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.ArrayList; 019import java.util.Collection; 020import java.util.Collections; 021import java.util.HashSet; 022import java.util.LinkedHashMap; 023import java.util.List; 024import java.util.Map; 025import java.util.Set; 026 027import org.ametys.runtime.model.View; 028import org.ametys.runtime.model.ViewHelper; 029import org.ametys.runtime.model.ViewItem; 030import org.ametys.runtime.model.ViewItemContainer; 031 032/** 033 * Default implementation of a {@link SearchModel}. 034 */ 035public class DefaultSearchModel implements SearchModel 036{ 037 /** The content types of this search model. */ 038 protected Set<String> _cTypes; 039 /** The content types excluded from this search model. */ 040 protected Set<String> _excludedCTypes; 041 042 /** The simple search criteria, indexed by ID. */ 043 protected Map<String, SearchCriterion> _criteria; 044 /** The faceted search criteria, indexed by ID. */ 045 protected Map<String, SearchCriterion> _facetedCriteria; 046 /** The result items */ 047 protected ViewItemContainer _resultItems; 048 /** the workspace */ 049 protected String _workspace; 050 051 /** 052 * Default constructor. 053 */ 054 public DefaultSearchModel() 055 { 056 _cTypes = new HashSet<>(); 057 _excludedCTypes = new HashSet<>(); 058 059 _criteria = new LinkedHashMap<>(); 060 _facetedCriteria = new LinkedHashMap<>(); 061 } 062 063 /** 064 * Constructor by copying an existing {@link SearchModel}. 065 * @param searchModelToCopy The {@link SearchModel} to copy 066 * @param contextualParameters The contextual parameters 067 */ 068 public DefaultSearchModel(SearchModel searchModelToCopy, Map<String, Object> contextualParameters) 069 { 070 setContentTypes(new HashSet<>(searchModelToCopy.getContentTypes(contextualParameters))); 071 setExcludedContentTypes(new HashSet<>(searchModelToCopy.getExcludedContentTypes(contextualParameters))); 072 073 setCriteria(new ArrayList<>(searchModelToCopy.getCriteria(contextualParameters).values())); 074 setFacetedCriteria(new ArrayList<>(searchModelToCopy.getFacetedCriteria(contextualParameters).values())); 075 076 View resultItems = new View(); 077 ViewItemContainer resultItemsToCopy = searchModelToCopy.getResultItems(contextualParameters); 078 List<ViewItem> viewItems = ViewHelper.copyViewItems(resultItemsToCopy.getViewItems()); 079 resultItems.addViewItems(viewItems); 080 setResulItems(resultItems); 081 082 setWorkspace(searchModelToCopy.getWorkspace(contextualParameters)); 083 } 084 085 public Set<String> getContentTypes(Map<String, Object> contextualParameters) 086 { 087 return Collections.unmodifiableSet(_cTypes); 088 } 089 090 /** 091 * Set the content types of the models 092 * @param cTypes The content types. 093 */ 094 public void setContentTypes(Set<String> cTypes) 095 { 096 _cTypes = new HashSet<>(cTypes); 097 } 098 099 public Set<String> getExcludedContentTypes(Map<String, Object> contextualParameters) 100 { 101 return Collections.unmodifiableSet(_excludedCTypes); 102 } 103 104 /** 105 * Set the content types to exclude 106 * @param cTypes The content types to exclude 107 */ 108 public void setExcludedContentTypes(Set<String> cTypes) 109 { 110 _excludedCTypes = new HashSet<>(cTypes); 111 } 112 113 public Map<String, ? extends SearchCriterion> getCriteria(Map<String, Object> contextualParameters) 114 { 115 return Collections.unmodifiableMap(_criteria); 116 } 117 118 /** 119 * Add a criterion 120 * @param criterion the criterion to add 121 */ 122 public void addCriterion(SearchCriterion criterion) 123 { 124 _criteria.put(criterion.getId(), criterion); 125 } 126 127 /** 128 * Set the criteria 129 * @param criteria The criteria list 130 */ 131 public void setCriteria(Collection<? extends SearchCriterion> criteria) 132 { 133 _criteria = new LinkedHashMap<>(); 134 for (SearchCriterion criterion : criteria) 135 { 136 _criteria.put(criterion.getId(), criterion); 137 } 138 } 139 140 public Map<String, ? extends SearchCriterion> getFacetedCriteria(Map<String, Object> contextualParameters) 141 { 142 return Collections.unmodifiableMap(_facetedCriteria); 143 } 144 145 /** 146 * Add a faceted criterion 147 * @param criterion the faceted criterion to add 148 */ 149 public void addFacetedCriterion(SearchCriterion criterion) 150 { 151 _facetedCriteria.put(criterion.getId(), criterion); 152 } 153 154 /** 155 * Set the faceted criteria 156 * @param criteria The list of faceted criteria 157 */ 158 public void setFacetedCriteria(Collection<? extends SearchCriterion> criteria) 159 { 160 _facetedCriteria = new LinkedHashMap<>(); 161 for (SearchCriterion criterion : criteria) 162 { 163 _facetedCriteria.put(criterion.getId(), criterion); 164 } 165 } 166 167 public ViewItemContainer getResultItems(Map<String, Object> contextualParameters) 168 { 169 return _resultItems; 170 } 171 172 /** 173 * Set the result items 174 * @param resultItems The result items to set 175 */ 176 public void setResulItems(ViewItemContainer resultItems) 177 { 178 _resultItems = resultItems; 179 } 180 181 public String getWorkspace(Map<String, Object> contextualParameters) 182 { 183 return _workspace; 184 } 185 186 /** 187 * Set the workspace 188 * @param workspace The workspace to set 189 */ 190 public void setWorkspace(String workspace) 191 { 192 _workspace = workspace; 193 } 194}