001    package railo.runtime.tag;
002    
003    import railo.commons.io.res.Resource;
004    import railo.commons.io.res.util.ResourceUtil;
005    import railo.commons.lang.StringUtil;
006    import railo.runtime.exp.ApplicationException;
007    import railo.runtime.exp.PageException;
008    import railo.runtime.ext.tag.TagImpl;
009    import railo.runtime.op.Caster;
010    import railo.runtime.search.SearchCollection;
011    import railo.runtime.search.SearchEngine;
012    import railo.runtime.search.SearchException;
013    
014    /**
015    * Allows you to create and administer Collections.
016    **/
017    public final class Collection extends TagImpl {
018    
019            /** Specifies the action to perform. */
020            private String action="list";
021    
022            /**  */
023            private Resource path;
024    
025            /** Specifies a collection name or an alias if action = "map" */
026            private String collection;
027    
028            /** Name of the output variable (action=list) */
029            private String name;
030    
031            /** language of the collection (operators,stopwords) */
032            private String language="english";
033            
034            //private boolean categories=false;
035    
036    
037            /**
038            * @see javax.servlet.jsp.tagext.Tag#release()
039            */
040            public void release()   {
041                    super.release();
042                    action="list";
043                    path=null;
044                    collection=null;
045                    name=null;
046                    language="english";
047                    //categories=false;
048            }
049            
050    
051            /**
052             * @param categories the categories to set
053             * @throws ApplicationException 
054             */
055            public void setCategories(boolean categories) {
056                    // Railo always support categories
057                    //this.categories = categories;
058            }
059            
060            /** set the value action
061            *  Specifies the action to perform.
062            * @param action value to set
063            **/
064            public void setAction(String action)    {
065                    if(action==null) return;
066                    this.action=action.toLowerCase().trim();
067            }
068            
069    
070            public void setEngine(String engine)    {
071                    // This setter only exists for compatibility reasions to other CFML engines, the attribute is completely ignored by Railo.
072            }
073    
074            /** set the value path
075            *  
076            * @param path value to set
077             * @throws PageException 
078            **/
079            public void setPath(String strPath) throws PageException        {
080                    if(strPath==null) return;
081                this.path=ResourceUtil.toResourceNotExisting(pageContext,strPath.trim() );
082                    //this.path=new File(path.toLowerCase().trim());
083    
084                    pageContext.getConfig().getSecurityManager().checkFileLocation(this.path);
085                    
086                if(!this.path.exists()) {
087                    Resource parent=this.path.getParentResource();
088                    if(parent!=null && parent.exists())this.path.mkdirs();
089                    else {
090                        throw new ApplicationException("attribute path of the tag collection must be a existing directory");
091                    }
092                }
093                    else if(!this.path.isDirectory())
094                        throw new ApplicationException("attribute path of the tag collection must be a existing directory");
095            }
096    
097            /** set the value collection
098            *  Specifies a collection name or an alias if action = "map"
099            * @param collection value to set
100            **/
101            public void setCollection(String collection)    {
102                    if(collection==null) return;
103                    this.collection=collection.toLowerCase().trim();
104            }
105    
106            /** set the value name
107            *  
108            * @param name value to set
109            **/
110            public void setName(String name)        {
111                    if(name==null) return;
112                    this.name=name.toLowerCase().trim();
113            }
114    
115            /** set the value language
116            *  
117            * @param language value to set
118            **/
119            public void setLanguage(String language)        {
120                    if(language==null) return;
121                    this.language=validateLanguage(language);
122            }
123    
124            public static String validateLanguage(String language) {
125                    if(StringUtil.isEmpty(language,true)) 
126                            return "english"; 
127                    language=language.toLowerCase().trim();
128                    if("standard".equals(language))
129                            return "english";
130                    return language;
131            }
132    
133    
134            /**
135             * calls the specified methods depending on the value of the property action
136            * @see javax.servlet.jsp.tagext.Tag#doStartTag()
137            */
138            public int doStartTag() throws PageException    {
139                //SerialNumber sn = pageContext.getConfig().getSerialNumber();
140                //if(sn.getVersion()==SerialNumber.VERSION_COMMUNITY)
141                //    throw new SecurityException("no access to this functionality with the "+sn.getStringVersion()+" version of railo");
142                
143                try {
144                            if(action.equals("create"))                     doCreate();
145                            else if(action.equals("repair"))        doRepair();
146                            else if(action.equals("delete"))        doDelete();
147                            else if(action.equals("optimize"))      doOptimize();
148                            else if(action.equals("list"))          doList();
149                            else if(action.equals("map"))           doMap();
150                            else if(action.equals("categorylist"))doCategoryList();
151                            
152                            else throw new ApplicationException("Invalid value [" + action + "] for attribute action.", "allowed values are [create,repair,map,delete,optimize,list ]");
153                    } catch (SearchException e) {
154                            throw Caster.toPageException(e);
155                    }
156                    return SKIP_BODY;
157            }
158    
159            /**
160             * @throws SearchException
161             * @throws PageException 
162         * 
163         */
164        private void doMap() throws SearchException, PageException {
165            required("collection",action,"collection",collection);
166            required("collection",action,"path",path);
167                    getCollection().map(path);
168        }
169    
170            /**
171             * Creates a query in the PageContext containing all available Collections of the current searchStorage
172             * @throws ApplicationException
173             * @throws PageException
174             * 
175             */
176            private void doList() throws ApplicationException, PageException {
177                    required("collection",action,"name",name);
178            //if(StringUtil.isEmpty(name))throw new ApplicationException("for action list attribute name is required");
179            pageContext.setVariable(name,getSearchEngine().getCollectionsAsQuery());
180            }
181            
182    
183            private void doCategoryList() throws PageException, SearchException {
184                    // check attributes
185                    required("collection",action,"collection",collection);
186                    required("collection",action,"name",name);
187                    pageContext.setVariable(name,getCollection().getCategoryInfo());
188            }
189    
190            /**
191             * Optimizes the Collection
192             * @throws SearchException
193             * @throws PageException 
194             * 
195             */
196            private void doOptimize() throws SearchException, PageException {
197                required("collection",action,"collection",collection);
198                getCollection().optimize();
199            }
200    
201            /**
202             * Deletes a Collection
203             * @throws SearchException
204             * @throws PageException 
205             * 
206             */
207            private void doDelete() throws SearchException, PageException {
208                required("collection",action,"collection",collection);
209                getCollection().delete();
210            }
211    
212            /**
213             * 
214             * @throws SearchException
215             * @throws PageException 
216             * 
217             */
218            private void doRepair() throws SearchException, PageException {
219                required("collection",action,"collection",collection);
220                getCollection().repair();
221            }
222    
223            /**
224             * Creates a new collection
225             * @throws SearchException
226             * @throws PageException 
227             * 
228             */
229            private void doCreate() throws SearchException, PageException {
230                required("collection",action,"collection",collection);
231                required("collection",action,"path",path);
232                    getSearchEngine().createCollection(collection,path,language,SearchEngine.DENY_OVERWRITE);
233            }
234    
235        /**
236             * Returns the Searchstorage defined in the Environment
237             * @return searchStorage
238             */
239            private SearchEngine getSearchEngine() {
240                return pageContext.getConfig().getSearchEngine();
241            }
242    
243        /**
244         * the collection matching the collection name
245         * @return collection
246         * @throws SearchException
247         */
248        private SearchCollection getCollection() throws SearchException {
249            return getSearchEngine().getCollectionByName(collection);
250        }
251    
252        /**
253            * @see javax.servlet.jsp.tagext.Tag#doEndTag()
254            */
255            public int doEndTag()   {
256                    return EVAL_PAGE;
257            }
258    }