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