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.CmsStringUtil;
041
042import javax.servlet.http.HttpServletRequest;
043import javax.servlet.http.HttpServletResponse;
044import javax.servlet.jsp.PageContext;
045
046import org.apache.commons.logging.Log;
047
048/**
049 * Sitemap action used to generate the sitemap editor.<p>
050 *
051 * See jsp file <tt>/system/workplace/commons/sitemap.jsp</tt>.<p>
052 *
053 * @since 8.0.0
054 */
055public class CmsSitemapActionElement extends CmsGwtActionElement {
056
057    /** The OpenCms module name. */
058    public static final String CMS_MODULE_NAME = "org.opencms.ade.sitemap";
059
060    /** The GWT module name. */
061    public static final String GWT_MODULE_NAME = "sitemap";
062
063    /** The static log object for this class. */
064    private static final Log LOG = CmsLog.getLog(CmsSitemapActionElement.class);
065
066    /** The current sitemap data. */
067    private CmsSitemapData m_sitemapData;
068
069    /**
070     * Constructor.<p>
071     *
072     * @param context the JSP page context object
073     * @param req the JSP request
074     * @param res the JSP response
075     */
076    public CmsSitemapActionElement(PageContext context, HttpServletRequest req, HttpServletResponse res) {
077
078        super(context, req, res);
079    }
080
081    /**
082     * @see org.opencms.gwt.CmsGwtActionElement#export()
083     */
084    @Override
085    public String export() throws Exception {
086
087        return "";
088    }
089
090    /**
091     * @see org.opencms.gwt.CmsGwtActionElement#exportAll()
092     */
093    @Override
094    public String exportAll() throws Exception {
095
096        StringBuffer sb = new StringBuffer();
097        sb.append(export(false));
098        sb.append(
099            exportDictionary(
100                CmsSitemapData.DICT_NAME,
101                I_CmsSitemapService.class.getMethod("prefetch", String.class),
102                getSitemapData()));
103        sb.append(exportModuleScriptTag(GWT_MODULE_NAME));
104        String vaadinBootstrap = CmsStringUtil.joinPaths(
105            OpenCms.getSystemInfo().getContextPath(),
106            "VAADIN/vaadinBootstrap.js");
107        sb.append("  <script type=\"text/javascript\"\n" + "          src=\"" + vaadinBootstrap + "\"></script>");
108        sb.append(
109            "<script type='text/javascript'>    \n"
110                + "function initVaadin() { "
111                + CmsVaadinUtils.getBootstrapScript(getCmsObject(), "sitemap-ui", "workplace/sitemap/")
112                + " } "
113                + "</script>");
114        return sb.toString();
115    }
116
117    /**
118     * Returns the needed server data for client-side usage.<p>
119     *
120     * @return the needed server data for client-side usage
121     */
122    public CmsSitemapData getSitemapData() {
123
124        if (m_sitemapData == null) {
125            try {
126                m_sitemapData = CmsVfsSitemapService.prefetch(
127                    getRequest(),
128                    getCmsObject().getRequestContext().getUri());
129            } catch (CmsRpcException e) {
130                LOG.error(e.getLocalizedMessage(), e);
131            }
132        }
133        return m_sitemapData;
134    }
135
136    /**
137     * Returns the editor title.<p>
138     *
139     * @return the editor title
140     */
141    public String getTitle() {
142
143        CmsSitemapData data = getSitemapData();
144        String folderTitle = "";
145        if (data != null) {
146            folderTitle = getSitemapData().getOpenPath();
147            CmsClientSitemapEntry root = getSitemapData().getRoot();
148            if (root != null) {
149                CmsClientProperty titleProp = root.getOwnProperties().get(CmsPropertyDefinition.PROPERTY_TITLE);
150                if ((titleProp != null) && !titleProp.isEmpty()) {
151                    folderTitle = root.getOwnProperties().get(CmsPropertyDefinition.PROPERTY_TITLE).getEffectiveValue();
152                } else {
153                    folderTitle = root.getName();
154                }
155            }
156        }
157        return Messages.get().getBundle(getWorkplaceLocale()).key(Messages.GUI_EDITOR_TITLE_1, folderTitle);
158    }
159}