001/*
002 *  Copyright 2013 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.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.runtime.model.ElementDefinition;
026import org.ametys.runtime.model.Model;
027import org.ametys.runtime.model.ModelItem;
028import org.ametys.runtime.model.ModelViewItem;
029import org.ametys.runtime.model.ViewElement;
030import org.ametys.runtime.model.ViewHelper;
031import org.ametys.runtime.model.ViewItemContainer;
032
033/**
034 * This interface represents a search model.
035 */
036public interface SearchModel extends Model
037{
038    /**
039     * Get the list of content types.
040     * @param contextualParameters the contextual parameters.
041     * @return The list of content types.
042     */
043    Set<String> getContentTypes(Map<String, Object> contextualParameters);
044    
045    /**
046     * Get the list of excluded content types.
047     * @param contextualParameters the contextual parameters
048     * @return The list of excluded content types.
049     */
050    Set<String> getExcludedContentTypes(Map<String, Object> contextualParameters);
051    
052    /**
053     * Retrieves the criteria in simple mode
054     * @param contextualParameters the contextual parameters
055     * @return the criteria in simple mode
056     */
057    ViewItemContainer getCriteria(Map<String, Object> contextualParameters);
058    
059    /**
060     * Retrieves the simple criterion with the given name
061     * @param name The name of the criterion to retrieve
062     * @param contextualParameters the contextual parameters
063     * @return the criterion or <code>null</code> if not found
064     */
065    default ModelViewItem getCriterion(String name, Map<String, Object> contextualParameters)
066    {
067        ViewItemContainer criteria = getCriteria(contextualParameters);
068        return SearchModelHelper.getCriterion(criteria, name);
069    }
070    
071    /**
072     * Add the given criterion to the simple criteria
073     * @param criterion the criterion to add
074     * @param contextualParameters the contextual parameters
075     */
076    default void addCriterion(SearchModelCriterionDefinition criterion, Map<String, Object> contextualParameters)
077    {
078        ModelViewItem<ElementDefinition> viewItem = new ViewElement();
079        viewItem.setDefinition(criterion);
080        
081        ViewItemContainer criteria = getCriteria(contextualParameters);
082        criteria.addViewItem(viewItem);
083    }
084    
085    /**
086     * Retrieves the faceted criteria.
087     * @param contextualParameters the contextual parameters
088     * @return the faceted criteria.
089     */
090    ViewItemContainer getFacetedCriteria(Map<String, Object> contextualParameters);
091    
092    /**
093     * Retrieves the faceted criterion with the given name
094     * @param name The name of the faceted criterion to retrieve
095     * @param contextualParameters the contextual parameters
096     * @return the faceted criterion or <code>null</code> if not found
097     */
098    default ModelViewItem getFacetedCriterion(String name, Map<String, Object> contextualParameters)
099    {
100        ViewItemContainer criteria = getFacetedCriteria(contextualParameters);
101        return SearchModelHelper.getCriterion(criteria, name);
102    }
103    
104    /**
105     * Add the given criterion to the faceted criteria
106     * @param criterion the criterion to add
107     * @param contextualParameters the contextual parameters
108     */
109    default void addFacetedCriterion(SearchModelCriterionDefinition criterion, Map<String, Object> contextualParameters)
110    {
111        ModelViewItem<ElementDefinition> viewItem = new ViewElement();
112        viewItem.setDefinition(criterion);
113        
114        ViewItemContainer criteria = getFacetedCriteria(contextualParameters);
115        criteria.addViewItem(viewItem);
116    }
117    
118    /**
119     * Retrieves the search result items 
120     * @param contextualParameters the contextual parameters
121     * @return the search result items
122     */
123    ViewItemContainer getResultItems(Map<String, Object> contextualParameters);
124    
125    /**
126     * Retrieves the search result item with the given path
127     * @param itemPath the path of the result item to retrieve
128     * @param contextualParameters the contextual parameters
129     * @return the search result item with
130     */
131    default ModelViewItem getResultItem(String itemPath, Map<String, Object> contextualParameters)
132    {
133        ViewItemContainer resultItems = getResultItems(contextualParameters);
134        return ViewHelper.getModelViewItem(resultItems, itemPath);
135    }
136    
137    /**
138     * Get the specific workspace to use.
139     * @param contextualParameters the contextual parameters.
140     * @return the workspace to use when searching, or null to use the default workspace.
141     */
142    String getWorkspace(Map<String, Object> contextualParameters);
143    
144    /**
145     * Converts the search model in a JSON map
146     * @param contextualParameters The contextual parameters
147     * @return the search model as a JSON map
148     */
149    public Map<String, Object> toJSON(Map<String, Object> contextualParameters);
150    
151    /**
152     * Converts the search model's result items on a JSON list
153     * @param contextualParameters The contextual parameters
154     * @return the result items as a JSON list
155     */
156    public List<Object> resultItemsToJSON(Map<String, Object> contextualParameters);
157    
158    default String getId()
159    {
160        return StringUtils.EMPTY;
161    }
162    
163    default String getFamilyId()
164    {
165        return this.getClass().getName();
166    }
167    
168    default Collection<? extends ModelItem> getModelItems()
169    {
170        throw new UnsupportedOperationException("Criteria of the search model must be accessed by the getCriteria methods");
171    }
172}