001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.ade.galleries;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.ade.galleries.shared.CmsSiteSelectorOption;
032import org.opencms.ade.galleries.shared.CmsSiteSelectorOption.Type;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsResourceFilter;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.main.OpenCms;
038import org.opencms.site.CmsSite;
039import org.opencms.site.CmsSiteManagerImpl;
040import org.opencms.util.CmsMacroResolver;
041import org.opencms.util.CmsStringUtil;
042
043import java.util.ArrayList;
044import java.util.HashSet;
045import java.util.List;
046import java.util.Locale;
047import java.util.Set;
048
049import org.apache.commons.logging.Log;
050
051/**
052 * Helper class for building the options for the site selector in the gallery dialog.<p>
053 */
054public class CmsSiteSelectorOptionBuilder {
055
056    /** The logger instance for this class. */
057    private static final Log LOG = CmsLog.getLog(CmsSiteSelectorOptionBuilder.class.getName());
058
059    /** The CMS context used by this object. */
060    private CmsObject m_cms;
061
062    /** The option list. */
063    private List<CmsSiteSelectorOption> m_options = new ArrayList<CmsSiteSelectorOption>();
064
065    /** The macro resolver to use. */
066    private CmsMacroResolver m_resolver;
067
068    /** The current site root. */
069    private String m_siteRoot;
070
071    /** Site roots of sites which have already been added. */
072    private Set<String> m_usedSiteRoots = new HashSet<String>();
073
074    /**
075     * Creates a new builder instance.<p>
076     *
077     * @param cms the CMS context to use
078     */
079    public CmsSiteSelectorOptionBuilder(CmsObject cms) {
080
081        m_cms = cms;
082        m_siteRoot = m_cms.getRequestContext().getSiteRoot();
083        m_resolver = new CmsMacroResolver();
084        m_resolver.setCmsObject(cms);
085        m_resolver.setMessages(
086            OpenCms.getWorkplaceManager().getMessages(OpenCms.getWorkplaceManager().getWorkplaceLocale(m_cms)));
087    }
088
089    /**
090     * Adds the current subsite.<p>
091     *
092     * @param referencePath the reference path
093     */
094    public void addCurrentSubsite(String referencePath) {
095
096        CmsADEConfigData configData = OpenCms.getADEManager().lookupConfiguration(m_cms, referencePath);
097        String basePath = configData.getBasePath();
098        if (basePath != null) {
099            addOption(
100                Type.currentSubsite,
101                basePath,
102                Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(m_cms)).key(
103                    Messages.GUI_SITESELECTOR_CURRENT_SUBSITE_0));
104        }
105    }
106
107    /**
108     * Adds 'normal' sites.<p>
109     *
110     * @param includeRoot if true, also adds the root site
111     * @param startFolder the users configured start folder
112     */
113    public void addNormalSites(boolean includeRoot, String startFolder) {
114
115        CmsSiteManagerImpl siteManager = OpenCms.getSiteManager();
116        List<CmsSite> sites = siteManager.getAvailableSites(m_cms, true, false, m_cms.getRequestContext().getOuFqn());
117        try {
118            CmsObject rootCms = OpenCms.initCmsObject(m_cms);
119            rootCms.getRequestContext().setSiteRoot("/");
120            if (sites.isEmpty()) {
121                String siteRoot = m_cms.getRequestContext().getSiteRoot();
122                if (!rootCms.existsResource(siteRoot, CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) {
123                    if (startFolder != null) {
124                        siteRoot = CmsStringUtil.joinPaths(siteRoot, startFolder);
125                    }
126                    if ((startFolder == null)
127                        || !rootCms.existsResource(siteRoot, CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) {
128                        siteRoot = null;
129                    }
130                }
131                if (siteRoot != null) {
132                    Type type = Type.site;
133                    String message = siteRoot;
134                    addOption(type, siteRoot, message);
135                }
136            }
137            for (CmsSite site : sites) {
138                // Do not add the shared site - it will be among sites if you are on the shared site at the moment.
139                if (site.isSharedSite()) {
140                    continue;
141                }
142                String siteRoot = site.getSiteRoot();
143                if (!rootCms.existsResource(siteRoot, CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) {
144                    if (startFolder != null) {
145                        siteRoot = CmsStringUtil.joinPaths(siteRoot, startFolder);
146                    }
147                    if ((startFolder == null)
148                        || !rootCms.existsResource(siteRoot, CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) {
149                        siteRoot = null;
150                    }
151                }
152                if (siteRoot != null) {
153                    Type type = Type.site;
154                    String message = null;
155                    String title = site.getTitle();
156                    if (!CmsStringUtil.isEmptyOrWhitespaceOnly(title)) {
157                        message = title;
158                    } else {
159                        message = siteRoot;
160                    }
161                    if (siteRoot.equals("")) {
162                        type = Type.root;
163                        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(m_cms);
164                        message = Messages.get().getBundle(locale).key(Messages.GUI_ROOT_SITE_0);
165                        if (!includeRoot) {
166                            continue;
167                        }
168                    }
169                    addOption(type, siteRoot, message);
170                }
171            }
172        } catch (CmsException e) {
173            LOG.error(e.getLocalizedMessage(), e);
174        }
175    }
176
177    /**
178     * Adds the shared folder.<p>
179     */
180    public void addSharedSite() {
181
182        String shared = OpenCms.getSiteManager().getSharedFolder();
183        if (shared.endsWith("/")) {
184            // IMPORTANT: remove last "/".
185            // Otherwise the version without slash will be added as well when you are in the shared folder currently.
186            shared = shared.substring(0, shared.length() - 1);
187        }
188        if ((shared != null) && m_cms.existsResource(shared, CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) {
189            addOption(
190                Type.shared,
191                shared,
192                org.opencms.workplace.Messages.get().getBundle(
193                    OpenCms.getWorkplaceManager().getWorkplaceLocale(m_cms)).key(
194                        org.opencms.workplace.Messages.GUI_SHARED_TITLE_0));
195        }
196    }
197
198    /**
199     * Adds the system folder.
200     */
201    public void addSystemFolder() {
202
203        addOption(Type.site, "/system/", "/system/");
204    }
205
206    /**
207     * Gets the site selector options.<p>
208     *
209     * @return the site selector options
210     */
211    public List<CmsSiteSelectorOption> getOptions() {
212
213        return m_options;
214    }
215
216    /**
217     * Internal helper method for adding an option.<p>
218     *
219     * @param type the option type
220     * @param siteRoot the site root
221     * @param message the message for the option
222     */
223    private void addOption(Type type, String siteRoot, String message) {
224
225        if (m_usedSiteRoots.contains(CmsStringUtil.joinPaths(siteRoot, "/"))) {
226            return;
227        }
228        message = m_resolver.resolveMacros(message);
229
230        CmsSiteSelectorOption option = new CmsSiteSelectorOption(type, siteRoot, m_siteRoot.equals(siteRoot), message);
231
232        // make sure to insert the root site is first and the shared site as second entry
233        if (Type.root.equals(type)) {
234            m_options.add(0, option);
235        } else if (Type.shared.equals(type)) {
236            if (!m_options.isEmpty() && Type.root.equals(m_options.get(0).getType())) {
237                m_options.add(1, option);
238            } else {
239                m_options.add(0, option);
240            }
241        } else {
242            m_options.add(option);
243        }
244        m_usedSiteRoots.add(CmsStringUtil.joinPaths(siteRoot, "/"));
245    }
246}