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.ui.model;
017
018import java.util.Map;
019import java.util.Set;
020
021import org.ametys.cms.search.model.SearchModel;
022import org.ametys.cms.search.model.SearchModelCriterionDefinition;
023import org.ametys.cms.search.model.SearchModelHelper;
024import org.ametys.cms.search.ui.model.impl.DefaultSearchModelCriterionViewItem;
025import org.ametys.runtime.model.ElementDefinition;
026import org.ametys.runtime.model.ModelViewItem;
027import org.ametys.runtime.model.ViewItem;
028import org.ametys.runtime.model.ViewItemContainer;
029import org.ametys.runtime.util.Labelable;
030
031/**
032 * Search tool model.
033 */
034public interface SearchUIModel extends SearchModel, Labelable
035{
036    /** Tag to indicate that a search model is public */
037    public static final String TAG_PUBLIC = "public";
038    
039    default void addCriterion(SearchModelCriterionDefinition criterion, Map<String, Object> contextualParameters)
040    {
041        ModelViewItem<ElementDefinition> viewItem = new DefaultSearchModelCriterionViewItem();
042        viewItem.setDefinition(criterion);
043        
044        addCriterion(viewItem, contextualParameters);
045    }
046    
047    /**
048     * Add the given criterion to the simple criteria
049     * @param criterion the criterion to add
050     * @param contextualParameters the contextual parameters
051     */
052    default void addCriterion(ViewItem criterion, Map<String, Object> contextualParameters)
053    {
054        ViewItemContainer criteria = getCriteria(contextualParameters);
055        criteria.addViewItem(criterion);
056    }
057    
058    default void addFacetedCriterion(SearchModelCriterionDefinition criterion, Map<String, Object> contextualParameters)
059    {
060        SearchModelCriterionViewItem<ElementDefinition> viewItem = new DefaultSearchModelCriterionViewItem();
061        viewItem.setDefinition(criterion);
062        
063        addFacetedCriterion(viewItem, contextualParameters);
064    }
065    
066    /**
067     * Add the given criterion to the faceted criteria
068     * @param criterion the criterion to add
069     * @param contextualParameters the contextual parameters
070     */
071    default void addFacetedCriterion(SearchModelCriterionViewItem criterion, Map<String, Object> contextualParameters)
072    {
073        ViewItemContainer criteria = getFacetedCriteria(contextualParameters);
074        criteria.addViewItem(criterion);
075    }
076    
077    /**
078     * Retrieves the criteria in advanced mode
079     * @param contextualParameters the contextual parameters
080     * @return the criteria in advanced mode
081     */
082    ViewItemContainer getAdvancedCriteria(Map<String, Object> contextualParameters);
083    
084    /**
085     * Retrieves the advanced criterion with the given name
086     * @param name The name of the faceted criterion to retrieve
087     * @param contextualParameters the contextual parameters
088     * @return the advanced criterion or <code>null</code> if not found
089     */
090    default ModelViewItem getAdvancedCriterion(String name, Map<String, Object> contextualParameters)
091    {
092        ViewItemContainer criteria = getAdvancedCriteria(contextualParameters);
093        return SearchModelHelper.getCriterion(criteria, name);
094    }
095    
096    /**
097     * Add the given criterion to the advanced criteria
098     * @param criterion the criterion to add
099     * @param contextualParameters the contextual parameters
100     */
101    default void addAdvancedCriterion(SearchModelCriterionDefinition criterion, Map<String, Object> contextualParameters)
102    {
103        SearchModelCriterionViewItem<ElementDefinition> viewItem = new DefaultSearchModelCriterionViewItem();
104        viewItem.setDefinition(criterion);
105        
106        addAdvancedCriterion(viewItem, contextualParameters);
107    }
108    
109    /**
110     * Add the given criterion to the advanced criteria
111     * @param criterion the criterion to add
112     * @param contextualParameters the contextual parameters
113     */
114    default void addAdvancedCriterion(SearchModelCriterionViewItem criterion, Map<String, Object> contextualParameters)
115    {
116        ViewItemContainer criteria = getAdvancedCriteria(contextualParameters);
117        criteria.addViewItem(criterion);
118    }
119    
120    /**
121     * Get the page size.
122     * @param contextualParameters the contextual parameters.
123     * @return The page size, unlimited or default used when negative or 0.
124     */
125    int getPageSize(Map<String, Object> contextualParameters);
126    
127    /**
128     * Get the URL for search
129     * @param contextualParameters the contextual parameters
130     * @return the URL for search
131     */
132    String getSearchUrl(Map<String, Object> contextualParameters);
133    
134    /**
135     * Get the plugin name for search
136     * @param contextualParameters the contextual parameters
137     * @return the plugin name for search
138     */
139    String getSearchUrlPlugin(Map<String, Object> contextualParameters);
140    
141    /**
142     * Get the URL for CSV export of results
143     * @param contextualParameters the contextual parameters
144     * @return the URL for CSV export
145     */
146    String getExportCSVUrl(Map<String, Object> contextualParameters);
147    
148    /**
149     * Get the plugin name for CSV export of results
150     * @param contextualParameters the contextual parameters
151     * @return the plugin name for CSV export
152     */
153    String getExportCSVUrlPlugin(Map<String, Object> contextualParameters);
154    
155    /**
156     * Get the URL for print results
157     * @param contextualParameters the contextual parameters
158     * @return the URL for print results
159     */
160    String getPrintUrl(Map<String, Object> contextualParameters);
161    
162    /**
163     * Get the plugin name for print results
164     * @param contextualParameters the contextual parameters
165     * @return the plugin name for print results
166     */
167    String getPrintUrlPlugin(Map<String, Object> contextualParameters);
168    
169    /**
170     * Get the name of the view to use for summary of the content.
171     * @return the name of the view to use for summary of the content. Can be null.
172     */
173    String getSummaryView();
174    
175    /**
176     * Determines if the search model has the given tag
177     * @param tagName The tag name
178     * @return true if the search model is tagged with the given tag name
179     */
180    boolean hasTag (String tagName);
181    
182    /**
183     * Get the registered tags for this search model
184     * @return the tags
185     */
186    Set<String> getTags ();
187    
188    /**
189     * Get whether the search model is public
190     * @return true if the model is public, false if it is private.
191     */
192    default boolean isPublic()
193    {
194        return hasTag(TAG_PUBLIC);
195    }
196
197}