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