001    /**
002     * 
003     */
004    package org.mycore.frontend.servlets;
005    
006    import javax.servlet.http.HttpServletResponse;
007    
008    import org.jdom.Document;
009    import org.mycore.common.MCRException;
010    import org.mycore.datamodel.classifications2.MCRCategory;
011    import org.mycore.datamodel.classifications2.MCRCategoryDAO;
012    import org.mycore.datamodel.classifications2.MCRCategoryDAOFactory;
013    import org.mycore.datamodel.classifications2.MCRCategoryID;
014    import org.mycore.datamodel.classifications2.utils.MCRCategoryTransformer;
015    
016    /** Handles getting a classification from the database and converting it to xml view so it can be shown in the browser
017     *  for saving. URL for this servlet must be: /servlerts/MCRClassExportServlet?id=...
018     * @author Radi Radichev
019     * 
020     */
021    public class MCRClassExportServlet extends MCRServlet {
022    
023        private static final long serialVersionUID = 1L;
024    
025        private static MCRCategoryDAO DAO = MCRCategoryDAOFactory.getInstance();
026    
027        public void doGetPost(MCRServletJob job) throws Exception, MCRException {
028            try {
029                String id = getProperty(job.getRequest(), "id");
030                if (id == null)
031                    throw new MCRException("Classification ID is null!");
032                MCRCategoryID catId = MCRCategoryID.rootID(id);
033                MCRCategory category = DAO.getCategory(catId, -1);
034                if (category == null)
035                    throw new MCRException("Cannot find classification with id: " + id);
036                Document jdom = MCRCategoryTransformer.getMetaDataDocument(category, true);
037                getLayoutService().sendXML(job.getRequest(), job.getResponse(), jdom);
038            } catch (Exception e) {
039                generateErrorPage(job.getRequest(), job.getResponse(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e, false);
040            }
041        }
042    }