001    /*
002     * 
003     * $Revision: 15105 $ $Date: 2009-04-23 11:23:28 +0200 (Do, 23. Apr 2009) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.frontend.indexbrowser.lucene;
025    
026    import java.util.List;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    import org.jdom.Document;
031    import org.jdom.Element;
032    
033    /**
034     * contains  utility functions for indexbrowser
035     * 
036     * @author Anja Schaar, Andreas Trappe, Matthias Eichner, Robert Stephan
037     */
038    public class MCRIndexBrowserUtils{
039    
040        private static final long serialVersionUID = 4963472470316616461L;
041    
042        protected MCRIndexBrowserIncomingData incomingBrowserData;
043    
044        protected MCRIndexBrowserConfig config;
045        
046      
047    
048        /**
049         * Creates a xml document with the results of the index browser.
050         * @return 
051         */
052        public static Document createResultListDocument(MCRIndexBrowserIncomingData incomingBrowserData,
053                    MCRIndexBrowserConfig config) {
054            List<MCRIndexBrowserEntry> resultList = null;
055            String index = config.getIndex();
056            if(MCRIndexBrowserCache.isCached(index, incomingBrowserData)) {
057                resultList = MCRIndexBrowserCache.getFromCache(index, incomingBrowserData);
058            } else {
059                MCRIndexBrowserSearcher searcher = new MCRIndexBrowserSearcher(incomingBrowserData, config);
060                resultList = searcher.doSearch();
061                MCRIndexBrowserCache.addToCache(incomingBrowserData, index, resultList);
062            }
063            MCRIndexBrowserXmlGenerator xmlGen = new MCRIndexBrowserXmlGenerator(resultList, incomingBrowserData, config);
064            return xmlGen.getXMLContent();
065        }
066    
067        /**
068         * Creates an empty xml index browser document.
069         * @return a new empty document
070         */
071        public static Document createEmptyDocument(MCRIndexBrowserIncomingData incomingBrowserData) {
072            Element rootElement = MCRIndexBrowserXmlGenerator.buildPageElement(incomingBrowserData);
073            MCRIndexBrowserXmlGenerator.buildResultsElement(rootElement, incomingBrowserData);
074            return new Document(rootElement);
075        }
076    
077        public static MCRIndexBrowserIncomingData getIncomingBrowserData(HttpServletRequest request) {
078            String search = request.getParameter("search");
079            String mode = getMode(request);
080            String searchclass = request.getParameter("searchclass");
081            String fromTo = request.getParameter("fromTo");
082            String init = request.getParameter("init");
083    
084            return new MCRIndexBrowserIncomingData(search, mode, searchclass, fromTo, init);
085        }
086    
087        private static String getMode(HttpServletRequest request) {
088            if (request.getParameter("mode") != null && !request.getParameter("mode").trim().equals("")) {
089                return request.getParameter("mode").toLowerCase().trim();
090            } else
091                return "prefix";
092        }
093    }