001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.sitemap;
029
030import org.opencms.ade.sitemap.shared.CmsClientSitemapEntry;
031import org.opencms.ade.sitemap.shared.CmsSitemapData;
032import org.opencms.ade.sitemap.shared.rpc.I_CmsSitemapService;
033import org.opencms.file.CmsPropertyDefinition;
034import org.opencms.gwt.CmsGwtActionElement;
035import org.opencms.gwt.CmsRpcException;
036import org.opencms.gwt.shared.property.CmsClientProperty;
037import org.opencms.main.CmsLog;
038import org.opencms.main.OpenCms;
039import org.opencms.ui.CmsVaadinUtils;
040import org.opencms.util.CmsRequestUtil;
041import org.opencms.util.CmsStringUtil;
042
043import javax.servlet.http.HttpServletRequest;
044import javax.servlet.http.HttpServletResponse;
045import javax.servlet.jsp.PageContext;
046
047import org.apache.commons.logging.Log;
048
049/**
050 * Sitemap action used to generate the sitemap editor.<p>
051 *
052 * See jsp file <tt>/system/workplace/commons/sitemap.jsp</tt>.<p>
053 *
054 * @since 8.0.0
055 */
056public class CmsSitemapActionElement extends CmsGwtActionElement {
057
058    /** The OpenCms module name. */
059    public static final String CMS_MODULE_NAME = "org.opencms.ade.sitemap";
060
061    /** The GWT module name. */
062    public static final String GWT_MODULE_NAME = "sitemap";
063
064    /** The static log object for this class. */
065    private static final Log LOG = CmsLog.getLog(CmsSitemapActionElement.class);
066
067    /** The current sitemap data. */
068    private CmsSitemapData m_sitemapData;
069
070    /**
071     * Constructor.<p>
072     *
073     * @param context the JSP page context object
074     * @param req the JSP request
075     * @param res the JSP response
076     */
077    public CmsSitemapActionElement(PageContext context, HttpServletRequest req, HttpServletResponse res) {
078
079        super(context, req, res);
080    }
081
082    /**
083     * @see org.opencms.gwt.CmsGwtActionElement#export()
084     */
085    @Override
086    public String export() throws Exception {
087
088        return "";
089    }
090
091    /**
092     * @see org.opencms.gwt.CmsGwtActionElement#exportAll()
093     */
094    @Override
095    public String exportAll() throws Exception {
096
097        CmsRequestUtil.disableCrossSiteFrameEmbedding(getResponse());
098        StringBuffer sb = new StringBuffer();
099        sb.append(export(false));
100        sb.append(
101            exportDictionary(
102                CmsSitemapData.DICT_NAME,
103                I_CmsSitemapService.class.getMethod("prefetch", String.class),
104                getSitemapData()));
105        sb.append(exportModuleScriptTag(GWT_MODULE_NAME));
106        String vaadinBootstrap = CmsStringUtil.joinPaths(
107            OpenCms.getSystemInfo().getContextPath(),
108            "VAADIN/vaadinBootstrap.js");
109        sb.append("  <script \n" + "          src=\"" + vaadinBootstrap + "\"></script>");
110        sb.append(
111            "<script>    \n"
112                + "function initVaadin() { "
113                + CmsVaadinUtils.getBootstrapScript(getCmsObject(), "sitemap-ui", "workplace/sitemap/")
114                + " } "
115                + "</script>");
116        return sb.toString();
117    }
118
119    /**
120     * Returns the needed server data for client-side usage.<p>
121     *
122     * @return the needed server data for client-side usage
123     */
124    public CmsSitemapData getSitemapData() {
125
126        if (m_sitemapData == null) {
127            try {
128                m_sitemapData = CmsVfsSitemapService.prefetch(
129                    getRequest(),
130                    getCmsObject().getRequestContext().getUri());
131            } catch (CmsRpcException e) {
132                LOG.error(e.getLocalizedMessage(), e);
133            }
134        }
135        return m_sitemapData;
136    }
137
138    /**
139     * Returns the editor title.<p>
140     *
141     * @return the editor title
142     */
143    public String getTitle() {
144
145        CmsSitemapData data = getSitemapData();
146        String folderTitle = "";
147        if (data != null) {
148            folderTitle = getSitemapData().getOpenPath();
149            CmsClientSitemapEntry root = getSitemapData().getRoot();
150            if (root != null) {
151                CmsClientProperty titleProp = root.getOwnProperties().get(CmsPropertyDefinition.PROPERTY_TITLE);
152                if ((titleProp != null) && !titleProp.isEmpty()) {
153                    folderTitle = root.getOwnProperties().get(CmsPropertyDefinition.PROPERTY_TITLE).getEffectiveValue();
154                } else {
155                    folderTitle = root.getName();
156                }
157            }
158        }
159        return Messages.get().getBundle(getWorkplaceLocale()).key(Messages.GUI_EDITOR_TITLE_1, folderTitle);
160    }
161}