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