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, 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.ui.apps;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.ade.configuration.CmsADEManager;
032import org.opencms.file.CmsObject;
033import org.opencms.file.CmsResource;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.security.CmsRole;
038import org.opencms.ui.A_CmsUI;
039import org.opencms.ui.CmsCssIcon;
040import org.opencms.ui.CmsVaadinUtils;
041import org.opencms.util.CmsStringUtil;
042
043import java.util.Locale;
044
045import javax.servlet.http.HttpServletRequest;
046import javax.servlet.http.HttpSession;
047
048import org.apache.commons.logging.Log;
049
050import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
051import com.vaadin.server.Resource;
052import com.vaadin.ui.Notification;
053import com.vaadin.ui.Notification.Type;
054
055/**
056 * The sitemap editor app configuration.<p>
057 */
058public class CmsSitemapEditorConfiguration extends A_CmsWorkplaceAppConfiguration implements I_CmsHasAppLaunchCommand {
059
060    /** The app id. */
061    public static final String APP_ID = "sitemapeditor";
062
063    /** The app icon resource (size 32x32). */
064    public static final CmsCssIcon ICON = new CmsCssIcon("oc-icon-32-sitemap");
065
066    /** Logger instance for this class. */
067    private static final Log LOG = CmsLog.getLog(CmsSitemapEditorConfiguration.class);
068
069    /**
070     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getAppCategory()
071     */
072    @Override
073    public String getAppCategory() {
074
075        return CmsWorkplaceAppManager.MAIN_CATEGORY_ID;
076    }
077
078    /**
079     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getAppInstance()
080     */
081    public I_CmsWorkplaceApp getAppInstance() {
082
083        throw new IllegalStateException("The sitemap app should be launched through the app launch command only.");
084    }
085
086    /**
087     * @see org.opencms.ui.apps.I_CmsHasAppLaunchCommand#getAppLaunchCommand()
088     */
089    public Runnable getAppLaunchCommand() {
090
091        return new Runnable() {
092
093            public void run() {
094
095                openSitemapEditor();
096            }
097        };
098
099    }
100
101    /**
102     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getHelpText(java.util.Locale)
103     */
104    @Override
105    public String getHelpText(Locale locale) {
106
107        return Messages.get().getBundle(locale).key(Messages.GUI_SITEMAP_HELP_0);
108    }
109
110    /**
111     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getIcon()
112     */
113    public Resource getIcon() {
114
115        return ICON;
116    }
117
118    /**
119     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getId()
120     */
121    public String getId() {
122
123        return APP_ID;
124    }
125
126    /**
127     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getName(java.util.Locale)
128     */
129    @Override
130    public String getName(Locale locale) {
131
132        return Messages.get().getBundle(locale).key(Messages.GUI_SITEMAP_TITLE_0);
133    }
134
135    /**
136     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getOrder()
137     */
138    @Override
139    public int getOrder() {
140
141        return 2;
142    }
143
144    /**
145     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getPriority()
146     */
147    @Override
148    public int getPriority() {
149
150        return I_CmsWorkplaceAppConfiguration.DEFAULT_PRIORIY;
151    }
152
153    /**
154     * @see org.opencms.ui.apps.A_CmsWorkplaceAppConfiguration#getRequiredRole()
155     */
156    @Override
157    public CmsRole getRequiredRole() {
158
159        return CmsRole.EDITOR;
160
161    }
162
163    /**
164     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getVisibility(org.opencms.file.CmsObject)
165     */
166    @Override
167    public CmsAppVisibilityStatus getVisibility(CmsObject cms) {
168
169        if (OpenCms.getRoleManager().hasRole(cms, CmsRole.EDITOR)) {
170            String siteRoot = cms.getRequestContext().getSiteRoot();
171            boolean active = CmsStringUtil.isNotEmptyOrWhitespaceOnly(siteRoot);
172            HttpServletRequest req = CmsVaadinUtils.getRequest();
173            String message = null;
174            if (active) {
175                if (req != null) {
176                    // this is a VAADIN UI request
177                    active = getPath(cms, req.getSession()) != null;
178                    if (!active) {
179                        message = Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms)).key(
180                            Messages.GUI_SITEMAP_COULD_NOT_BE_DETERMINED_0);
181                    }
182                }
183            } else {
184                message = Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms)).key(
185                    Messages.GUI_SITEMAP_NOT_AVAILABLE_0);
186            }
187            return new CmsAppVisibilityStatus(true, active, message);
188        } else {
189            return CmsAppVisibilityStatus.INVISIBLE;
190        }
191    }
192
193    /**
194     * Opens the sitemap editor for the current site.<p>
195     */
196    void openSitemapEditor() {
197
198        CmsObject cms = A_CmsUI.getCmsObject();
199        String siteRoot = cms.getRequestContext().getSiteRoot();
200        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(siteRoot)) {
201            String path = getPath(cms, A_CmsUI.get().getHttpSession());
202            if (path != null) {
203                try {
204                    CmsAppWorkplaceUi ui = CmsAppWorkplaceUi.get();
205                    if (ui.beforeViewChange(
206                        new ViewChangeEvent(ui.getNavigator(), ui.getCurrentView(), null, APP_ID, null))) {
207                        CmsResource res = cms.readResource(CmsADEManager.PATH_SITEMAP_EDITOR_JSP);
208                        String link = OpenCms.getLinkManager().substituteLink(cms, res);
209                        A_CmsUI.get().getPage().setLocation(link + "?path=" + path);
210                    }
211                    return;
212                } catch (CmsException e) {
213                    LOG.debug("Unable to open sitemap editor.", e);
214                }
215            }
216        }
217        Notification.show(CmsVaadinUtils.getMessageText(Messages.GUI_SITEMAP_NOT_AVAILABLE_0), Type.WARNING_MESSAGE);
218    }
219
220    /**
221     * Returns the page editor path to open.<p>
222     *
223     * @param cms the cms context
224     * @param session the user session
225     *
226     * @return the path or <code>null</code>
227     */
228    private String getPath(CmsObject cms, HttpSession session) {
229
230        CmsQuickLaunchLocationCache locationCache = CmsQuickLaunchLocationCache.getLocationCache(session);
231        String page = locationCache.getFileExplorerLocation(cms.getRequestContext().getSiteRoot());
232        if (page != null) {
233            CmsADEConfigData conf = OpenCms.getADEManager().lookupConfiguration(
234                cms,
235                cms.getRequestContext().addSiteRoot(page));
236            if ((conf == null) || (conf.getBasePath() == null)) {
237                page = null;
238            } else {
239                page = cms.getRequestContext().removeSiteRoot(conf.getBasePath());
240            }
241        }
242        if (page == null) {
243            page = locationCache.getSitemapEditorLocation(cms.getRequestContext().getSiteRoot());
244        }
245        return page;
246    }
247
248}