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.util.ArrayUtil;
008    import railo.runtime.type.util.ListUtil;
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        @Override
165        public boolean equals(Object o) {
166            if(!(o instanceof SearchIndex)) return false;
167            SearchIndex other=(SearchIndex) o;
168            
169            return (other.key.equals(key) && other.type==type);
170        }
171        
172        
173        /**
174         * @return Returns the custom1.
175         */
176        public String getCustom1() {
177            return custom1;
178        }
179        /**
180         * @return Returns the custom2.
181         */
182        public String getCustom2() {
183            return custom2;
184        }
185    
186        /**
187         * @return Returns the custom3.
188         */
189        public String getCustom3() {
190            return custom3;
191        }
192    
193        /**
194         * @return Returns the custom4.
195         */
196        public String getCustom4() {
197            return custom4;
198        }
199        
200        /**
201         * @return Returns the extensions.
202         */
203        public String[] getExtensions() {
204            return extensions;
205        }
206        /**
207         * @return Returns the key.
208         */
209        public String getKey() {
210            return key;
211        }
212        /**
213         * @return Returns the language.
214         */
215        public String getLanguage() {
216            return language;
217        }
218        /**
219         * @return Returns the title.
220         */
221        public String getTitle() {
222            return title;
223        }
224        /**
225         * @return Returns the type.
226         */
227        public short getType() {
228            return type;
229        }
230        /**
231         * @return Returns the id.
232         */
233        public String getId() {
234            return id;
235        }
236        
237        /**
238         * @param id The id to set.
239         * /
240        public void setId(String id) {
241            this.id = id;
242        }*/
243        
244        /**
245         * @return Returns the urlpath.
246         */
247        public String getUrlpath() {
248            return urlpath;
249        }
250    
251        /**
252         * @return Returns the query.
253         */
254        public String getQuery() {
255            return query;
256        }
257    
258        @Override
259        public String toString() {
260            return "railo.runtime.search.SearchIndex(id:"+id+";title:"+title+";key:"+key+";type:"+toStringTypeEL(type)+
261            ";language:"+language+";urlpath:"+urlpath+";query:"+query+";categoryTree:"+categoryTree+";categories:"+ListUtil.arrayToList(categories,",")+";custom1:"+custom1+";custom2:"+custom2+";custom3:"+custom3+";custom4:"+custom4+";)";
262        }
263    
264        /**
265         * @param type
266         * @param key
267         * @param queryName
268         * @return id from given data
269         */
270        public static String toId(short type, String key, String queryName) {
271            if(type==SearchIndex.TYPE_CUSTOM) return "custom";
272            //if(type==SearchIndex.TYPE_FILE) return "file";//P504
273            //if(type==SearchIndex.TYPE_PATH) return "file";//P504
274            
275            try {
276                            return SearchIndex.toStringTypeEL(type)+"-"+Md5.getDigestAsString(key+null);// null is for backward compatibility to older collections
277                    } catch (IOException e) {
278                            
279                            return SearchIndex.toStringTypeEL(type)+"-"+StringUtil.toVariableName(key+null);// null is for backward compatibility to older collections
280                    }
281            //return SearchIndex.toStringTypeEL(type)+"-"+HexCoder.encode((key+queryName).getBytes());
282        }
283    
284            /**
285             * @return the categories
286             */
287            public String[] getCategories() {
288                    return categories;
289            }
290    
291            /**
292             * @return the categoryTree
293             */
294            public String getCategoryTree() {
295                    return categoryTree;
296            }
297    }