001    /*
002     * 
003     * $Revision: 15422 $ $Date: 2009-07-01 10:48:51 +0200 (Wed, 01 Jul 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.servlets;
025    
026    import org.mycore.datamodel.classifications.MCRClassificationBrowserData;
027    import org.mycore.frontend.servlets.MCRServlet;
028    import org.apache.log4j.Logger;
029    import org.mycore.common.*;
030    import org.jdom.*;
031    import org.jdom.input.SAXBuilder;
032    import javax.servlet.*;
033    import javax.servlet.http.*;
034    
035    import java.io.File;
036    
037    /**
038     * This servlet provides a way to visually navigate through the tree of
039     * categories of a classification, provides a link to show the documents in a
040     * category and shows to number of documents per category.
041     * 
042     * @author Anja Schaar
043     * 
044     * @see org.mycore.datamodel.classifications.MCRClassificationBrowserData
045     */
046    public class MCRClassificationBrowser extends MCRServlet {
047        /**
048         * 
049         */
050        private static final long serialVersionUID = 1L;
051    
052        private static Logger LOGGER = Logger.getLogger(MCRClassificationBrowser.class);
053    
054        private static String lang = null;
055    
056        public void doGetPost(MCRServletJob job) throws ServletException, Exception {
057            /*
058             * default classification
059             */
060            LOGGER.debug("Start browsing in classifications");
061            MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
062            lang = mcrSession.getCurrentLanguage();
063    
064            /*
065             * the urn with information about classification-property and category
066             */
067            String uri = "";
068            if (job.getRequest().getPathInfo() != null)
069                uri = job.getRequest().getPathInfo();
070    
071            if ("/".equals(uri))
072                uri = "";
073    
074            String mode = "";
075            if (job.getRequest().getParameter("mode") != null)
076                mode = job.getRequest().getParameter("mode");
077            String actclid = "";
078            if (job.getRequest().getParameter("clid") != null)
079                actclid = job.getRequest().getParameter("clid");
080            String actcateg = "";
081            if (job.getRequest().getParameter("categid") != null)
082                actcateg = job.getRequest().getParameter("categid");
083    
084            LOGGER.debug("Browsing Path = " + uri);
085            LOGGER.debug("Browsing  Mode = " + mode);
086    
087            MCRClassificationBrowserData bData;
088            try {
089                LOGGER.debug("Creation of BData.");
090                LOGGER.debug("URI: " + uri);
091                LOGGER.debug("MODE: " + mode);
092                LOGGER.debug("ACTCLID: " + actclid);
093                LOGGER.debug("ACTCATEG: " + actcateg);
094                bData = new MCRClassificationBrowserData(uri, mode, actclid, actcateg);
095            } catch (MCRConfigurationException cErr) {
096                generateErrorPage(job.getRequest(), job.getResponse(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR, cErr.getMessage(), cErr,
097                        false);
098                return;
099            }
100    
101            Document jdomFile = getEmbeddingPage(bData.getPageName());
102            Document jdom = null;
103    
104            if (mode.equalsIgnoreCase("edit") && bData.getClassification() == null) {
105                // alle Klassifikationen auflisten (auch die nicht eingebundenen)
106                jdom = bData.createXmlTreeforAllClassifications();
107    
108            } else {
109                jdom = bData.createXmlTree(lang);
110            }
111            jdom = bData.loadTreeIntoSite(jdomFile, jdom);
112            doLayout(job, bData.getXslStyle(), jdom); // use the stylesheet-postfix from properties
113        }
114    
115        private org.jdom.Document getEmbeddingPage(String coverPage) throws Exception {
116            String path = getServletContext().getRealPath(coverPage);
117            File file = new File(path);
118            if (!file.exists()) {
119                LOGGER.debug("Did not find the CoverPage " + path);
120                return null;
121            }
122            SAXBuilder sxbuild = new SAXBuilder();
123            LOGGER.debug("Found CoverPage " + path);
124            return sxbuild.build(file);
125    
126        }
127    
128        /**
129         * Gather information about the XML document to be shown and the
130         * corresponding XSLT stylesheet and redirect the request to the
131         * LayoutService
132         * 
133         * @param job
134         *            The MCRServletJob instance
135         * @param styleBase
136         *            String value to select the correct XSL stylesheet
137         * @param jdomDoc
138         *            The XML representation to be presented by the LayoutService
139         * @throws ServletException
140         *             for errors from the servlet engine.
141         * @throws Exception
142         */
143        protected void doLayout(MCRServletJob job, String styleBase, Document jdomDoc) throws Exception {
144            if (getProperty(job.getRequest(), "XSL.Style") == null) {
145                LOGGER.info("Set XSL.Style to: " + styleBase);
146                job.getRequest().setAttribute("XSL.Style", styleBase);
147            }
148            getLayoutService().doLayout(job.getRequest(), job.getResponse(), jdomDoc);
149        }
150    
151    }