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.gwt;
029
030import org.opencms.ade.configuration.CmsADEManager;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsResource;
033import org.opencms.file.CmsResourceFilter;
034import org.opencms.file.types.CmsResourceTypeXmlContainerPage;
035import org.opencms.gwt.shared.CmsQuickLaunchData;
036import org.opencms.gwt.shared.CmsQuickLaunchParams;
037import org.opencms.main.CmsException;
038import org.opencms.main.CmsLog;
039import org.opencms.main.OpenCms;
040import org.opencms.ui.CmsVaadinUtils;
041import org.opencms.ui.apps.CmsAppVisibilityStatus;
042import org.opencms.ui.apps.CmsFileExplorerConfiguration;
043import org.opencms.ui.apps.CmsLegacyAppConfiguration;
044import org.opencms.ui.apps.CmsPageEditorConfiguration;
045import org.opencms.ui.apps.CmsQuickLaunchLocationCache;
046import org.opencms.ui.apps.CmsSitemapEditorConfiguration;
047import org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration;
048
049import java.util.ArrayList;
050import java.util.List;
051import java.util.Locale;
052
053import javax.servlet.http.HttpSession;
054
055import org.apache.commons.logging.Log;
056
057import com.google.common.collect.Lists;
058import com.vaadin.server.ExternalResource;
059import com.vaadin.server.FontIcon;
060import com.vaadin.server.Resource;
061
062/**
063 * Provides the data for the buttons in the quick launch menu.<p>
064 */
065public final class CmsQuickLaunchProvider {
066
067    /** The font icon HTML format String. */
068    private static final String FONT_ICON_PREFIX = "fonticon:";
069
070    /** Log instance for this class. */
071    private static final Log LOG = CmsLog.getLog(CmsQuickLaunchProvider.class);
072
073    /**
074     * Hiding default constructor.<p>
075     */
076    private CmsQuickLaunchProvider() {
077
078        // nothing to do
079    }
080
081    /**
082     * Gets the quick launch data for the current user and context.<p>
083     *
084     * The context is a string which identifies where the quick launch menu is located
085     * @param cms the cms context
086     * @param session  the user session
087     *
088     * @param params the quick launch parameters
089     *
090     * @return the list of available quick launch items
091     */
092    public static List<CmsQuickLaunchData> getQuickLaunchData(
093        CmsObject cms,
094        HttpSession session,
095        CmsQuickLaunchParams params) {
096
097        List<CmsQuickLaunchData> result = Lists.newArrayList();
098        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
099        List<I_CmsWorkplaceAppConfiguration> appConfigs = new ArrayList<I_CmsWorkplaceAppConfiguration>(
100            OpenCms.getWorkplaceAppManager().getQuickLaunchConfigurations(cms));
101
102        CmsResource currentPage = null;
103        if (params.getPageId() != null) {
104            try {
105                currentPage = cms.readResource(params.getPageId(), CmsResourceFilter.ONLY_VISIBLE_NO_DELETED);
106            } catch (CmsException e) {
107                LOG.warn(e.getLocalizedMessage(), e);
108            }
109        }
110        CmsQuickLaunchLocationCache locationCache = CmsQuickLaunchLocationCache.getLocationCache(session);
111        for (I_CmsWorkplaceAppConfiguration config : appConfigs) {
112            try {
113                boolean reload = false;
114                String link = null;
115                String errorTitle = null;
116                String errorMessage = null;
117                boolean useLegacyButtonStyle = config instanceof CmsLegacyAppConfiguration;
118                if (CmsFileExplorerConfiguration.APP_ID.equals(config.getId())) {
119                    String page = locationCache.getFileExplorerLocation(cms.getRequestContext().getSiteRoot());
120                    if (page != null) {
121                        link = CmsCoreService.getVaadinWorkplaceLink(cms, cms.getRequestContext().addSiteRoot(page));
122                    } else {
123                        if (cms.existsResource("/", CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) {
124                            link = CmsCoreService.getVaadinWorkplaceLink(cms, cms.getRequestContext().getSiteRoot());
125                        } else if (currentPage != null) {
126                            link = CmsCoreService.getVaadinWorkplaceLink(cms, params.getPageId());
127                        } else {
128                            errorTitle = config.getName(locale);
129                            errorMessage = Messages.get().getBundle(locale).key(
130                                Messages.GUI_QUICKLAUNCH_EXPLORER_NOT_ALLOWED_0);
131                        }
132                    }
133                } else if (CmsPageEditorConfiguration.APP_ID.equals(config.getId())) {
134                    if (params.isPageContext()) {
135                        if ((currentPage != null)
136                            && CmsResourceTypeXmlContainerPage.MODEL_GROUP_TYPE_NAME.equals(
137                                OpenCms.getResourceManager().getResourceType(currentPage).getTypeName())) {
138                            String page = locationCache.getPageEditorLocation(
139                                cms,
140                                cms.getRequestContext().getSiteRoot());
141                            if (page != null) {
142                                link = OpenCms.getLinkManager().substituteLink(cms, page);
143                            } else {
144                                reload = true;
145                            }
146                        } else {
147                            reload = true;
148                        }
149                    } else if (params.isSitemapContext()) {
150                        String page = locationCache.getPageEditorLocation(cms, cms.getRequestContext().getSiteRoot());
151                        if (page == null) {
152                            page = locationCache.getSitemapEditorLocation(cms.getRequestContext().getSiteRoot());
153                        }
154                        if (page != null) {
155                            link = OpenCms.getLinkManager().substituteLink(cms, page);
156                        }
157                    }
158                } else if (CmsSitemapEditorConfiguration.APP_ID.equals(config.getId())) {
159                    if (params.isSitemapContext()) {
160                        reload = true;
161                    } else if (params.isPageContext()) {
162                        String sitemapLink = OpenCms.getLinkManager().substituteLinkForUnknownTarget(
163                            cms,
164                            CmsADEManager.PATH_SITEMAP_EDITOR_JSP);
165                        String page = locationCache.getPageEditorLocation(cms, cms.getRequestContext().getSiteRoot());
166                        link = sitemapLink + "?path=" + page;
167                    }
168                } else {
169                    link = CmsVaadinUtils.getWorkplaceLink(config.getId());
170                }
171                Resource icon = config.getIcon();
172                String imageLink = "";
173                if (icon instanceof ExternalResource) {
174                    imageLink = ((ExternalResource)icon).getURL();
175                } else if (icon instanceof FontIcon) {
176                    imageLink = FONT_ICON_PREFIX + ((FontIcon)icon).getHtml();
177                }
178
179                String name = config.getName(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms));
180                CmsAppVisibilityStatus visibility = config.getVisibility(cms);
181                if (!visibility.isActive()) {
182                    errorTitle = name;
183                    errorMessage = visibility.getHelpText();
184                }
185                CmsQuickLaunchData data = new CmsQuickLaunchData(
186                    link,
187                    name,
188                    imageLink,
189                    config.getButtonStyle(),
190                    errorTitle,
191                    errorMessage,
192                    useLegacyButtonStyle,
193                    reload);
194                result.add(data);
195            } catch (Exception e) {
196                LOG.error(e.getLocalizedMessage(), e);
197            }
198
199        }
200        return result;
201    }
202
203}