001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.search;
020
021import java.io.IOException;
022
023import lucee.commons.lang.Md5;
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.type.util.ArrayUtil;
026import lucee.runtime.type.util.ListUtil;
027
028
029
030
031/**
032 */
033public final class SearchIndex {
034    
035    
036    /**
037     * Field <code>TYPE_FILE</code>
038     */
039    public static final short TYPE_FILE = 0;
040    /**
041     * Field <code>TYPE_PATH</code>
042     */
043    public static final short TYPE_PATH = 1;
044    /**
045     * Field <code>TYPE_CUSTOM</code>
046     */
047    public static final short TYPE_CUSTOM = 2;
048    /**
049     * Field <code>TYPE_URL</code>
050     */
051    public static final short TYPE_URL = 3;
052    
053    private String id;
054    private String title;
055    private String key;
056    private short type;
057    private String[] extensions;
058    private String language;
059    private String urlpath;
060    private String custom1;
061    private String custom2;
062    private String query;
063    private String custom3;
064    private String custom4;
065        private String categoryTree;
066        private String[] categories;
067    
068    
069
070    /**
071     * @param title
072     * @param id
073     * @param key
074     * @param type
075     * @param query
076     * @param extensions
077     * @param language
078     * @param urlpath
079     * @param custom1
080     * @param custom2
081     * @param custom3 
082     * @param custom4 
083     */
084    protected SearchIndex(String id, String title, String key, short type, String query, String[] extensions,
085            String language, String urlpath,String categoryTree, String[] categories, String custom1, String custom2, String custom3, String custom4) {
086        super();
087        this.title = title;
088        this.id = id;
089        this.key = key;
090        this.type = type;
091        this.query = query;
092        this.extensions = extensions;
093        this.language = SearchUtil.translateLanguage(language);
094        this.urlpath = urlpath;
095        this.categoryTree = categoryTree;
096        this.categories = ArrayUtil.trim(categories);
097        this.custom1 = custom1;
098        this.custom2 = custom2;
099        this.custom3 = custom3;
100        this.custom4 = custom4;
101    }
102
103    /**
104     * @param title
105     * @param key
106     * @param type
107     * @param query
108     * @param extensions
109     * @param language
110     * @param urlpath
111     * @param custom1
112     * @param custom2
113     * @param custom3 
114     * @param custom4 
115     */
116    protected SearchIndex(String title, String key, short type, String query, String[] extensions,
117            String language, String urlpath,String categoryTree, String[] categories, String custom1, String custom2, String custom3, String custom4) {
118        super();
119        
120        this.title = title;
121        this.key = key;
122        this.type = type;
123        this.query = query;
124        this.extensions = extensions;
125        this.language = SearchUtil.translateLanguage(language);
126        this.urlpath = urlpath;
127        this.categoryTree = categoryTree;
128        this.categories = categories;
129        this.custom1 = custom1;
130        this.custom2 = custom2;
131        this.custom3 = custom3;
132        this.custom4 = custom4;
133        this.id=toId(type,key,query);
134    }
135
136    /**
137     * cast string type to short
138     * @param type type to cast
139     * @return casted type
140     * @throws SearchException
141     */
142    public static short toType(String type) throws SearchException {
143        type=type.toLowerCase().trim();
144        if(type.equals("custom"))return SearchIndex.TYPE_CUSTOM; 
145        else if(type.equals("query"))return SearchIndex.TYPE_CUSTOM; 
146            else if(type.equals("file"))return SearchIndex.TYPE_FILE; 
147            else if(type.equals("path"))return SearchIndex.TYPE_PATH; 
148            else if(type.equals("url"))return SearchIndex.TYPE_URL; 
149            else throw new SearchException("invalid value for attribute type ["+type+"]");
150    }
151
152    /**
153     * cast short type to string
154     * @param type type to cast
155     * @return casted type
156     * @throws SearchException
157     */
158    public static String toStringType(short type) throws SearchException {
159        if(type==SearchIndex.TYPE_CUSTOM) return "custom";
160        else if(type==SearchIndex.TYPE_FILE) return "file";
161        else if(type==SearchIndex.TYPE_PATH) return "path";
162        else if(type==SearchIndex.TYPE_URL) return "url";
163        else throw new SearchException("invalid value for attribute type ["+type+"]");
164        
165    }
166
167    /**
168     * cast short type to string
169     * @param type type to cast
170     * @return casted type
171     * @throws SearchException
172     */
173    public static String toStringTypeEL(short type) {
174        if(type==SearchIndex.TYPE_CUSTOM) return "custom";
175        else if(type==SearchIndex.TYPE_FILE) return "file";
176        else if(type==SearchIndex.TYPE_PATH) return "path";
177        else if(type==SearchIndex.TYPE_URL) return "url";
178        else return "custom";
179        
180    }
181    
182    @Override
183    public boolean equals(Object o) {
184        if(!(o instanceof SearchIndex)) return false;
185        SearchIndex other=(SearchIndex) o;
186        
187        return (other.key.equals(key) && other.type==type);
188    }
189    
190    
191    /**
192     * @return Returns the custom1.
193     */
194    public String getCustom1() {
195        return custom1;
196    }
197    /**
198     * @return Returns the custom2.
199     */
200    public String getCustom2() {
201        return custom2;
202    }
203
204    /**
205     * @return Returns the custom3.
206     */
207    public String getCustom3() {
208        return custom3;
209    }
210
211    /**
212     * @return Returns the custom4.
213     */
214    public String getCustom4() {
215        return custom4;
216    }
217    
218    /**
219     * @return Returns the extensions.
220     */
221    public String[] getExtensions() {
222        return extensions;
223    }
224    /**
225     * @return Returns the key.
226     */
227    public String getKey() {
228        return key;
229    }
230    /**
231     * @return Returns the language.
232     */
233    public String getLanguage() {
234        return language;
235    }
236    /**
237     * @return Returns the title.
238     */
239    public String getTitle() {
240        return title;
241    }
242    /**
243     * @return Returns the type.
244     */
245    public short getType() {
246        return type;
247    }
248    /**
249     * @return Returns the id.
250     */
251    public String getId() {
252        return id;
253    }
254    
255    /**
256     * @param id The id to set.
257     * /
258    public void setId(String id) {
259        this.id = id;
260    }*/
261    
262    /**
263     * @return Returns the urlpath.
264     */
265    public String getUrlpath() {
266        return urlpath;
267    }
268
269    /**
270     * @return Returns the query.
271     */
272    public String getQuery() {
273        return query;
274    }
275
276    @Override
277    public String toString() {
278        return "lucee.runtime.search.SearchIndex(id:"+id+";title:"+title+";key:"+key+";type:"+toStringTypeEL(type)+
279        ";language:"+language+";urlpath:"+urlpath+";query:"+query+";categoryTree:"+categoryTree+";categories:"+ListUtil.arrayToList(categories,",")+";custom1:"+custom1+";custom2:"+custom2+";custom3:"+custom3+";custom4:"+custom4+";)";
280    }
281
282    /**
283     * @param type
284     * @param key
285     * @param queryName
286     * @return id from given data
287     */
288    public static String toId(short type, String key, String queryName) {
289        if(type==SearchIndex.TYPE_CUSTOM) return "custom";
290        //if(type==SearchIndex.TYPE_FILE) return "file";//P504
291        //if(type==SearchIndex.TYPE_PATH) return "file";//P504
292        
293        try {
294                        return SearchIndex.toStringTypeEL(type)+"-"+Md5.getDigestAsString(key+null);// null is for backward compatibility to older collections
295                } catch (IOException e) {
296                        
297                        return SearchIndex.toStringTypeEL(type)+"-"+StringUtil.toVariableName(key+null);// null is for backward compatibility to older collections
298                }
299        //return SearchIndex.toStringTypeEL(type)+"-"+HexCoder.encode((key+queryName).getBytes());
300    }
301
302        /**
303         * @return the categories
304         */
305        public String[] getCategories() {
306                return categories;
307        }
308
309        /**
310         * @return the categoryTree
311         */
312        public String getCategoryTree() {
313                return categoryTree;
314        }
315}