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 new Runnable() {
091
092            public void run() {
093
094                openPageEditor();
095            }
096        };
097    }
098
099    /**
100     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getHelpText(java.util.Locale)
101     */
102    @Override
103    public String getHelpText(Locale locale) {
104
105        return Messages.get().getBundle(locale).key(Messages.GUI_PAGEEDITOR_HELP_0);
106    }
107
108    /**
109     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getIcon()
110     */
111    public Resource getIcon() {
112
113        return ICON;
114    }
115
116    /**
117     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getId()
118     */
119    public String getId() {
120
121        return APP_ID;
122    }
123
124    /**
125     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getName(java.util.Locale)
126     */
127    @Override
128    public String getName(Locale locale) {
129
130        return Messages.get().getBundle(locale).key(Messages.GUI_PAGEEDITOR_TITLE_0);
131    }
132
133    /**
134     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getOrder()
135     */
136    @Override
137    public int getOrder() {
138
139        return 1;
140    }
141
142    /**
143     * @see org.opencms.ui.apps.A_CmsWorkplaceAppConfiguration#getRequiredRole()
144     */
145    @Override
146    public CmsRole getRequiredRole() {
147
148        return CmsRole.ELEMENT_AUTHOR;
149
150    }
151
152    /**
153     * @see org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration#getVisibility(org.opencms.file.CmsObject)
154     */
155    @Override
156    public CmsAppVisibilityStatus getVisibility(CmsObject cms) {
157
158        boolean active = !cms.getRequestContext().getCurrentProject().isOnlineProject()
159            && CmsStringUtil.isNotEmptyOrWhitespaceOnly(cms.getRequestContext().getSiteRoot());
160        HttpServletRequest req = CmsVaadinUtils.getRequest();
161        String message = null;
162        if (active) {
163            if (req != null) {
164                // this is a VAADIN UI request
165                active = getPath(cms, req.getSession()) != null;
166                if (!active) {
167                    message = Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms)).key(
168                        Messages.GUI_PAGE_EDITOR_PLEASE_SELECT_PAGE_0);
169                }
170            }
171        } else {
172            message = Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms)).key(
173                Messages.GUI_PAGE_EDITOR_NOT_AVAILABLE_0);
174        }
175        return new CmsAppVisibilityStatus(true, active, message);
176    }
177
178    /**
179     * Opens the page editor for the current site.<p>
180     */
181    void openPageEditor() {
182
183        CmsAppWorkplaceUi ui = CmsAppWorkplaceUi.get();
184        if (ui.beforeViewChange(new ViewChangeEvent(ui.getNavigator(), ui.getCurrentView(), null, APP_ID, null))) {
185            CmsObject cms = A_CmsUI.getCmsObject();
186            HttpServletRequest req = CmsVaadinUtils.getRequest();
187            if (req == null) {
188                // called from outside the VAADIN UI, not allowed
189                throw new RuntimeException("Wrong usage, this can not be called from outside a VAADIN UI.");
190            }
191            CmsJspTagEnableAde.removeDirectEditFlagFromSession(req.getSession());
192            String page = getPath(cms, req.getSession());
193            if (page != null) {
194                A_CmsUI.get().getPage().setLocation(OpenCms.getLinkManager().substituteLink(cms, page));
195
196            } else {
197                String message = CmsVaadinUtils.getMessageText(Messages.GUI_PAGE_EDITOR_NOT_AVAILABLE_0);
198                Notification.show(message, Type.WARNING_MESSAGE);
199            }
200        }
201    }
202
203    /**
204     * Returns the page editor path to open.<p>
205     *
206     * @param cms the cms context
207     * @param session the user session
208     *
209     * @return the path or <code>null</code>
210     */
211    private String getPath(CmsObject cms, HttpSession session) {
212
213        CmsQuickLaunchLocationCache locationCache = CmsQuickLaunchLocationCache.getLocationCache(session);
214        String page = locationCache.getPageEditorLocation(cms.getRequestContext().getSiteRoot());
215        if (page == null) {
216            try {
217                CmsResource mainDefaultFile = cms.readDefaultFile("/");
218                if (mainDefaultFile != null) {
219                    page = cms.getSitePath(mainDefaultFile);
220                }
221            } catch (CmsException e) {
222                LOG.error(e.getLocalizedMessage(), e);
223            }
224        }
225        return page;
226    }
227
228}