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.db.CmsUserSettings;
032import org.opencms.file.CmsObject;
033import org.opencms.file.CmsResource;
034import org.opencms.file.CmsResourceFilter;
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.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.CmsTraditionalWorkplaceConfiguration;
048import org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration;
049import org.opencms.workplace.CmsWorkplace;
050
051import java.util.ArrayList;
052import java.util.List;
053import java.util.Locale;
054
055import javax.servlet.http.HttpSession;
056
057import org.apache.commons.logging.Log;
058
059import com.google.common.collect.Lists;
060import com.vaadin.server.ExternalResource;
061import com.vaadin.server.FontIcon;
062import com.vaadin.server.Resource;
063
064/**
065 * Provides the data for the buttons in the quick launch menu.<p>
066 */
067public class CmsQuickLaunchProvider {
068
069    /** The font icon HTML format String. */
070    private static final String FONT_ICON_HTML = "fonticon:<span class=\"v-icon %1$s\" style=\"font-family: %2$s;\">&#x%3$x;</span>";
071
072    /** Log instance for this class. */
073    private static final Log LOG = CmsLog.getLog(CmsQuickLaunchProvider.class);
074
075    /**
076     * Gets the quick launch data for the current user and context.<p>
077     *
078     * The context is a string which identifies where the quick launch menu is located
079     * @param cms the cms context
080     * @param session  the user session
081     *
082     * @param params the quick launch parameters
083     *
084     * @return the list of available quick launch items
085     */
086    public static List<CmsQuickLaunchData> getQuickLaunchData(
087        CmsObject cms,
088        HttpSession session,
089        CmsQuickLaunchParams params) {
090
091        List<CmsQuickLaunchData> result = Lists.newArrayList();
092        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
093        CmsUserSettings userSettings = new CmsUserSettings(cms);
094        boolean usesNewWorkplace = userSettings.usesNewWorkplace();
095        List<I_CmsWorkplaceAppConfiguration> appConfigs = new ArrayList<I_CmsWorkplaceAppConfiguration>(
096            OpenCms.getWorkplaceAppManager().getQuickLaunchConfigurations(cms));
097        if (!usesNewWorkplace) {
098            // the workplace app is only available within ADE in case of the traditional workplace user setting
099            I_CmsWorkplaceAppConfiguration config = OpenCms.getWorkplaceAppManager().getAppConfiguration(
100                CmsTraditionalWorkplaceConfiguration.APP_ID);
101            if ((config != null) && config.getVisibility(cms).isActive()) {
102                appConfigs.add(config);
103            }
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                    if (!usesNewWorkplace) {
123                        continue;
124                    }
125                    String page = locationCache.getFileExplorerLocation(cms.getRequestContext().getSiteRoot());
126                    if (page != null) {
127                        link = CmsCoreService.getVaadinWorkplaceLink(cms, cms.getRequestContext().addSiteRoot(page));
128                    } else {
129                        if (cms.existsResource("/", CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) {
130                            link = CmsCoreService.getVaadinWorkplaceLink(cms, cms.getRequestContext().getSiteRoot());
131                        } else if (currentPage != null) {
132                            link = CmsCoreService.getVaadinWorkplaceLink(cms, params.getPageId());
133                        } else {
134                            errorTitle = config.getName(locale);
135                            errorMessage = Messages.get().getBundle(locale).key(
136                                Messages.GUI_QUICKLAUNCH_EXPLORER_NOT_ALLOWED_0);
137                        }
138                    }
139                } else if (CmsPageEditorConfiguration.APP_ID.equals(config.getId())) {
140                    if (params.isPageContext()) {
141                        if ((currentPage != null)
142                            && CmsResourceTypeXmlContainerPage.MODEL_GROUP_TYPE_NAME.equals(
143                                OpenCms.getResourceManager().getResourceType(currentPage).getTypeName())) {
144                            String page = locationCache.getPageEditorLocation(cms.getRequestContext().getSiteRoot());
145                            if (page != null) {
146                                link = OpenCms.getLinkManager().substituteLink(cms, page);
147                            } else {
148                                reload = true;
149                            }
150                        } else {
151                            reload = true;
152                        }
153                    } else if (params.isSitemapContext()) {
154                        String page = locationCache.getPageEditorLocation(cms.getRequestContext().getSiteRoot());
155                        if (page == null) {
156                            page = locationCache.getSitemapEditorLocation(cms.getRequestContext().getSiteRoot());
157                        }
158                        if (page != null) {
159                            link = OpenCms.getLinkManager().substituteLink(cms, page);
160                        }
161                    }
162                } else if (CmsSitemapEditorConfiguration.APP_ID.equals(config.getId())) {
163                    if (params.isSitemapContext()) {
164                        reload = true;
165                    } else if (params.isPageContext()) {
166                        String sitemapLink = OpenCms.getLinkManager().substituteLinkForUnknownTarget(
167                            cms,
168                            CmsADEManager.PATH_SITEMAP_EDITOR_JSP);
169                        String page = locationCache.getPageEditorLocation(cms.getRequestContext().getSiteRoot());
170                        link = sitemapLink + "?path=" + page;
171                    }
172                } else if (CmsTraditionalWorkplaceConfiguration.APP_ID.equals(config.getId())) {
173                    String resourceRootFolder = currentPage != null
174                    ? CmsResource.getFolderPath(currentPage.getRootPath())
175                    : cms.getRequestContext().getSiteRoot();
176                    link = CmsWorkplace.getWorkplaceExplorerLink(cms, resourceRootFolder);
177                    useLegacyButtonStyle = true;
178                } else {
179                    if (!usesNewWorkplace) {
180                        continue;
181                    }
182                    link = OpenCms.getSystemInfo().getWorkplaceContext() + "#!" + config.getId();
183                }
184                Resource icon = config.getIcon();
185                String imageLink = "";
186                if (icon instanceof ExternalResource) {
187                    imageLink = ((ExternalResource)icon).getURL();
188                    // no icon if not an external resource
189                } else if (icon instanceof FontIcon) {
190                    imageLink = String.format(
191                        FONT_ICON_HTML,
192                        config.getButtonStyle(),
193                        ((FontIcon)icon).getFontFamily(),
194                        Integer.valueOf(((FontIcon)icon).getCodepoint()));
195                }
196                String name = config.getName(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms));
197                CmsAppVisibilityStatus visibility = config.getVisibility(cms);
198                if (!visibility.isActive()) {
199                    errorTitle = name;
200                    errorMessage = visibility.getHelpText();
201                }
202                CmsQuickLaunchData data = new CmsQuickLaunchData(
203                    link,
204                    name,
205                    imageLink,
206                    errorTitle,
207                    errorMessage,
208                    useLegacyButtonStyle,
209                    reload);
210                result.add(data);
211            } catch (Exception e) {
212                LOG.error(e.getLocalizedMessage(), e);
213            }
214
215        }
216        return result;
217    }
218
219}