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