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.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.jsp.CmsJspTagEnableAde;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.security.CmsRole;
037import org.opencms.ui.A_CmsUI;
038import org.opencms.ui.CmsCssIcon;
039import org.opencms.ui.CmsVaadinUtils;
040import org.opencms.util.CmsStringUtil;
041
042import java.util.Locale;
043
044import javax.servlet.http.HttpServletRequest;
045import javax.servlet.http.HttpSession;
046
047import org.apache.commons.logging.Log;
048
049import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
050import com.vaadin.server.Resource;
051import com.vaadin.ui.Notification;
052import com.vaadin.ui.Notification.Type;
053
054/**
055 * The page editor app configuration.<p>
056 */
057public class CmsPageEditorConfiguration extends A_CmsWorkplaceAppConfiguration implements I_CmsHasAppLaunchCommand {
058
059    /** The app id. */
060    public static final String APP_ID = "pageeditor";
061
062    /** The app icon resource (size 32x32). */
063    public static final CmsCssIcon ICON = new CmsCssIcon("oc-icon-32-edit-point");
064
065    /** The logger for this class. */
066    private static final Log LOG = CmsLog.getLog(CmsPageEditorConfiguration.class);
067
068    /**
069     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getAppCategory()
070     */
071    @Override
072    public String getAppCategory() {
073
074        return CmsWorkplaceAppManager.MAIN_CATEGORY_ID;
075    }
076
077    /**
078     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getAppInstance()
079     */
080    public I_CmsWorkplaceApp getAppInstance() {
081
082        throw new IllegalStateException("The editor app should be launched through the app launch command only.");
083    }
084
085    /**
086     * @see org.opencms.ui.apps.I_CmsHasAppLaunchCommand#getAppLaunchCommand()
087     */
088    public Runnable getAppLaunchCommand() {
089
090        return this::openPageEditor;
091    }
092
093    /**
094     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getHelpText(java.util.Locale)
095     */
096    @Override
097    public String getHelpText(Locale locale) {
098
099        return Messages.get().getBundle(locale).key(Messages.GUI_PAGEEDITOR_HELP_0);
100    }
101
102    /**
103     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getIcon()
104     */
105    public Resource getIcon() {
106
107        return ICON;
108    }
109
110    /**
111     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getId()
112     */
113    public String getId() {
114
115        return APP_ID;
116    }
117
118    /**
119     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getName(java.util.Locale)
120     */
121    @Override
122    public String getName(Locale locale) {
123
124        return Messages.get().getBundle(locale).key(Messages.GUI_PAGEEDITOR_TITLE_0);
125    }
126
127    /**
128     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getOrder()
129     */
130    @Override
131    public int getOrder() {
132
133        return 1;
134    }
135
136    /**
137     * @see org.opencms.ui.apps.A_CmsWorkplaceAppConfiguration#getRequiredRole()
138     */
139    @Override
140    public CmsRole getRequiredRole() {
141
142        return CmsRole.ELEMENT_AUTHOR;
143
144    }
145
146    /**
147     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getVisibility(org.opencms.file.CmsObject)
148     */
149    @Override
150    public CmsAppVisibilityStatus getVisibility(CmsObject cms) {
151
152        boolean active = !cms.getRequestContext().getCurrentProject().isOnlineProject()
153            && CmsStringUtil.isNotEmptyOrWhitespaceOnly(cms.getRequestContext().getSiteRoot());
154        HttpServletRequest req = CmsVaadinUtils.getRequest();
155        String message = null;
156        if (active) {
157            if (req != null) {
158                // this is a VAADIN UI request
159                active = getPath(cms, req.getSession()) != null;
160                if (!active) {
161                    message = Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms)).key(
162                        Messages.GUI_PAGE_EDITOR_PLEASE_SELECT_PAGE_0);
163                }
164            }
165        } else {
166            message = Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms)).key(
167                Messages.GUI_PAGE_EDITOR_NOT_AVAILABLE_0);
168        }
169        return new CmsAppVisibilityStatus(true, active, message);
170    }
171
172    /**
173     * Opens the page editor for the current site.<p>
174     */
175    void openPageEditor() {
176
177        CmsAppWorkplaceUi ui = CmsAppWorkplaceUi.get();
178        if (ui.beforeViewChange(new ViewChangeEvent(ui.getNavigator(), ui.getCurrentView(), null, APP_ID, null))) {
179            CmsObject cms = A_CmsUI.getCmsObject();
180            HttpServletRequest req = CmsVaadinUtils.getRequest();
181            if (req == null) {
182                // called from outside the VAADIN UI, not allowed
183                throw new RuntimeException("Wrong usage, this can not be called from outside a VAADIN UI.");
184            }
185            CmsJspTagEnableAde.removeDirectEditFlagFromSession(req.getSession());
186            String page = getPath(cms, req.getSession());
187            if (page != null) {
188                A_CmsUI.get().getPage().setLocation(OpenCms.getLinkManager().substituteLink(cms, page));
189
190            } else {
191                String message = CmsVaadinUtils.getMessageText(Messages.GUI_PAGE_EDITOR_NOT_AVAILABLE_0);
192                Notification.show(message, Type.WARNING_MESSAGE);
193            }
194        }
195    }
196
197    /**
198     * Returns the page editor path to open.<p>
199     *
200     * @param cms the cms context
201     * @param session the user session
202     *
203     * @return the path or <code>null</code>
204     */
205    private String getPath(CmsObject cms, HttpSession session) {
206
207        CmsQuickLaunchLocationCache locationCache = CmsQuickLaunchLocationCache.getLocationCache(session);
208        String page = locationCache.getPageEditorLocation(cms, cms.getRequestContext().getSiteRoot());
209        if (page == null) {
210            try {
211                CmsResource mainDefaultFile = cms.readDefaultFile("/");
212                if (mainDefaultFile != null) {
213                    page = cms.getSitePath(mainDefaultFile);
214                }
215            } catch (CmsException e) {
216                LOG.error(e.getLocalizedMessage(), e);
217            }
218        }
219        return page;
220    }
221
222}