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.content.indexing.solr;
017
018import org.ametys.cms.contenttype.MetadataType;
019
020/**
021 * Helper for getting Solr field names.
022 */
023public final class SolrFieldHelper
024{
025    
026//    private static final int __SORT_MAX_CHARS = 50;
027    
028    private SolrFieldHelper()
029    {
030        // Hide the default constructor.
031    }
032    
033    /**
034     * Get the Solr field name (without analyzing)
035     * @param fieldPath The path to metadata
036     * @param language The query language.
037     * @param fullText <code>true</code> for a full-text query, false otherwise.
038     * @return the Solr field name
039     */
040    public static String getStringIndexingFieldName(String fieldPath, String language, boolean fullText)
041    {
042        return getIndexingFieldName(MetadataType.STRING, fieldPath, language, fullText);
043    }
044    
045    /**
046     * Get the Solr field name (without analyzing)
047     * @param type The type
048     * @param fieldPath The path to metadata
049     * @return the Solr field name
050     */
051    public static String getIndexingFieldName(MetadataType type, String fieldPath)
052    {
053        return getIndexingFieldName(type, fieldPath, null, false);
054    }
055    
056    /**
057     * Get the Solr field's name (without analyzing)
058     * @param type The type
059     * @param fieldPath The path to metadata
060     * @param language The query language.
061     * @return the Solr field's name
062     */
063    public static String getIndexingFieldName(MetadataType type, String fieldPath, String language)
064    {
065        return getIndexingFieldName(type, fieldPath, language, false);
066    }
067    
068    /**
069     * Get the Solr field name
070     * @param type The type
071     * @param fieldPath The path to metadata
072     * @param language The query language.
073     * @param fullText true if field should be analyzed.
074     * @return the Solr field name
075     */
076    public static String getIndexingFieldName(MetadataType type, String fieldPath, String language, boolean fullText)
077    {
078        return fieldPath + getFieldSuffix(type, language, fullText);
079    }
080    
081    /**
082     * Get a field suffix from its type.
083     * @param language The query language.
084     * @param fullText <code>true</code> for a full-text query, false otherwise.
085     * @return the field name suffix.
086     */
087    public static String getStringFieldSuffix(String language, boolean fullText)
088    {
089        return getFieldSuffix(MetadataType.STRING, language, fullText);
090    }
091    
092    /**
093     * Get the wildcard field suffix
094     * @return the field name suffix.
095     */
096    public static String getWildcardFieldSuffix()
097    {
098        return "_s_lower";
099    }
100    
101    /**
102     * Get the wildcard field suffix
103     * @param language The query language.
104     * @return the field name suffix.
105     */
106    public static String getFulltextFieldSuffix(String language)
107    {
108        return "_txt_" + language;
109    }
110    
111    /**
112     * Get a field suffix from its type.
113     * @param type the field type.
114     * @return the field name suffix.
115     */
116    public static String getFieldSuffix(MetadataType type)
117    {
118        return getFieldSuffix(type, null, false);
119    }
120    
121    /**
122     * Get a field suffix from its type.
123     * @param type the field type.
124     * @param language The search language.
125     * @param fullText true if the field is indexed full-text, false otherwise.
126     * @return the field name suffix.
127     */
128    public static String getFieldSuffix(MetadataType type, String language, boolean fullText)
129    {
130        switch (type)
131        {
132            case STRING:
133            case MULTILINGUAL_STRING:
134            case REFERENCE:
135            case CONTENT:
136            case SUB_CONTENT:
137            case USER:
138                return fullText ? ("_txt_stemmed_" + language) : "_s";
139            case RICH_TEXT:
140                return "_txt_stemmed_" + language;
141            case LONG:
142                return "_l";
143            case DOUBLE:
144                return "_d";
145            case BOOLEAN:
146                return "_b";
147            case DATE:
148            case DATETIME:
149                return "_dt";
150            case GEOCODE:
151                return "_geo";
152            default:
153                // TODO
154                break;
155        }
156        
157        return "";
158    }
159    
160    /**
161     * Get the Solr field name for sorting
162     * @param type The type
163     * @param metadataPath The path to metadata
164     * @return the Solr field name
165     */
166    public static String getMetadataSortFieldName(MetadataType type, String metadataPath)
167    {
168        return metadataPath + getSortFieldSuffix(type);
169    }
170    
171    
172    /**
173     * Get the suffix for sorting
174     * @param metaType The type
175     * @return The sort field suffix.
176     */
177    public static String getSortFieldSuffix(MetadataType metaType)
178    {
179        switch (metaType)
180        {
181            case STRING:
182            case MULTILINGUAL_STRING:
183            case REFERENCE:
184            case CONTENT:
185            case SUB_CONTENT:
186            case USER:
187                return "_s_sort";
188            case LONG:
189                return "_l_sort";
190            case DOUBLE:
191                return "_d_sort";
192            case BOOLEAN:
193                return "_b_sort";
194            case DATE:
195            case DATETIME:
196                return "_dt_sort";
197            case GEOCODE:
198                // TODO?
199                break;
200            default:
201                // TODO
202                break;
203        }
204        
205        return "";
206    }
207    
208    /**
209     * Get the field name for join
210     * @param metadataPath The metadata path, separated by '/'
211     * @return the join field name
212     */
213    public static String getJoinFieldName(String metadataPath)
214    {
215        return metadataPath;
216    }
217    
218    /**
219     * Get the sort value for a string.
220     * @param string the string to index in a sort field.
221     * @return the sort value.
222     */
223    public static String getSortValue(String string)
224    {
225        return string == null ? "" : string;
226//        if (string != null)
227//        {
228//            int endIndex = Math.min(string.length(), __SORT_MAX_CHARS);
229//            return string.substring(0, endIndex);
230//        }
231//        else
232//        {
233//            return "";
234//        }
235    }
236    
237}