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.Map;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.solr.common.SolrInputDocument;
024
025import org.ametys.cms.contenttype.MetadataType;
026import org.ametys.cms.repository.Content;
027import org.ametys.cms.search.SearchField;
028import org.ametys.cms.search.query.Query;
029import org.ametys.cms.search.query.Query.Operator;
030import org.ametys.cms.search.solr.schema.SchemaDefinition;
031import org.ametys.runtime.i18n.I18nizableText;
032import org.ametys.runtime.parameter.Enumerator;
033
034/**
035 * Represents a universal property of a {@link Content} (content types, language, current workflow step, ...)
036 */
037public interface SystemProperty
038{
039    
040    /**
041     * Get the system property ID.
042     * @return The property ID.
043     */
044    String getId();
045    
046    /**
047     * Get the system property label.
048     * @return The property label.
049     */
050    I18nizableText getLabel();
051    
052    /**
053     * Get the system property description.
054     * @return The property description.
055     */
056    I18nizableText getDescription();
057    
058    /**
059     * Get the property type.
060     * @return the property type.
061     */
062    MetadataType getType();
063    
064    /**
065     * Get the multiple status of the property.
066     * @return <code>true</code> if the property can have multiple values, <code>false</code> otherwise.
067     */
068    boolean isMultiple();
069    
070    /**
071     * Get the definition of an enumerator.
072     * @param contentTypes The model's content types.
073     * @param configuration The enumerator configuration.
074     * @return The enumerator definition or null if the property is not enumerated.
075     */
076    EnumeratorDefinition getEnumeratorDefinition(Collection<String> contentTypes, Configuration configuration);
077    
078    /**
079     * In case of a Content reference field, the content type ID.
080     * @return the content type ID.
081     */
082    String getContentTypeId();
083    
084    /**
085     * Get if the property can be searched on (i.e. used in a SearchCriterion).
086     * @return <code>true</code> if the property can be searched on, <code>false</code> otherwise.
087     */
088    boolean isSearchable();
089    
090    /**
091     * Get if the property can be displayed (i.e. used in a ResultField).
092     * @return <code>true</code> if the property can be displayed, <code>false</code> otherwise.
093     */
094    boolean isDisplayable();
095    
096    /**
097     * Get if the property can be sorted on.
098     * @return <code>true</code> if the property can be sorted on, <code>false</code> otherwise.
099     */
100    boolean isSortable();
101    
102    /**
103     * Get the {@link Query} associated to the given value.
104     * @param value the user-submitted value for this property.
105     * @param operator In advanced search mode, the operator chosen by the user. <code>null</code> to use the criterion-defined operator (simple search mode).
106     * @param language The current search language.
107     * @param contextualParameters the search contextual parameters.
108     * @return The {@link Query} associated to the given value.
109     */
110    Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters);
111    
112    /**
113     * Get the default widget to use when rendering this property as a criterion.
114     * @return The default widget to use, or <code>null</code> if no specific widget is necessary.
115     */
116    String getWidget();
117    
118    /**
119     * Get the widget parameters.
120     * @return The widget parameters as a Map.
121     */
122    Map<String, I18nizableText> getWidgetParameters();
123    
124    /**
125     * Get the renderer.
126     * @return The column renderer.
127     */
128    String getRenderer();
129    
130    /**
131     * Get the property column converter.
132     * @return The property column converter.
133     */
134    String getConverter();
135    
136    /**
137     * Get the column width.
138     * @return the default column width, can be null.
139     */
140    Integer getColumnWidth();
141    
142    /**
143     * Index the system property in a solr document.
144     * @param content The content to index.
145     * @param document The solr document to index into.
146     */
147    void index(Content content, SolrInputDocument document);
148    
149//    /**
150//     * Get the name of the field to use when indexing this property.
151//     * @return The field name.
152//     */
153//    String getField();
154    
155    /**
156     * Get the {@link SearchField} representing this system property.
157     * @return The search field representing this system property.
158     */
159    SearchField getSearchField();
160    
161    /**
162     * Get the value represented by this field in the given result content.
163     * @param content the result content.
164     * @return the content field value (cast to the appropriate object).
165     */
166    Object getValue(Content content);
167    
168    /**
169     * Get the value represented by this field in the given result content.
170     * @param content the result content.
171     * @return the content field value (cast to the appropriate object).
172     */
173    Object getFullValue(Content content);
174    
175    /**
176     * Get the sort value represented by this field in the given result content.
177     * @param content the result content.
178     * @return the content sort value, must be scalar (String, long, double, Date).
179     */
180    Object getSortValue(Content content);
181    
182    /**
183     * Get the schema definitions brought by this property.
184     * @return The schema definitions used by this property.
185     */
186    Collection<SchemaDefinition> getSchemaDefinitions();
187    
188    /**
189     * Representation of an Enumerator, used to generate an Enumerator on the fly.
190     */
191    class EnumeratorDefinition
192    {
193        
194        private boolean _isStatic;
195        
196        private Class<? extends Enumerator> _enumeratorClass;
197        
198        private Configuration _configuration;
199        
200        private Map<String, I18nizableText> _staticEntries;
201        
202        /**
203         * Build a definition representing a dynamic Enumerator.
204         * @param enumeratorClass The enumerator class.
205         * @param configuration The enumerator configuration.
206         */
207        public EnumeratorDefinition(Class<? extends Enumerator> enumeratorClass, Configuration configuration)
208        {
209            this._enumeratorClass = enumeratorClass;
210            this._configuration = configuration;
211            this._isStatic = false;
212        }
213        
214        /**
215         * Build a definition representing a static Enumerator.
216         * @param staticEntries the enumerator entries.
217         */
218        public EnumeratorDefinition(Map<String, I18nizableText> staticEntries)
219        {
220            this._staticEntries = staticEntries;
221            this._isStatic = true;
222        }
223        
224        public boolean isStatic()
225        {
226            return _isStatic;
227        }
228        
229        public Class<? extends Enumerator> getEnumeratorClass()
230        {
231            return _enumeratorClass;
232        }
233        
234        public Configuration getConfiguration()
235        {
236            return _configuration;
237        }
238        
239        public Map<String, I18nizableText> getStaticEntries()
240        {
241            return Collections.unmodifiableMap(_staticEntries);
242        }
243        
244    }
245    
246}