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.ui.sitemap; 029 030import org.opencms.ade.sitemap.shared.CmsClientSitemapEntry; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsProperty; 033import org.opencms.file.CmsPropertyDefinition; 034import org.opencms.file.CmsResource; 035import org.opencms.file.CmsResourceFilter; 036import org.opencms.file.types.CmsResourceTypeXmlContainerPage; 037import org.opencms.i18n.CmsLocaleGroup; 038import org.opencms.i18n.CmsLocaleManager; 039import org.opencms.main.CmsException; 040import org.opencms.main.CmsLog; 041import org.opencms.util.CmsUUID; 042 043import java.util.Collection; 044import java.util.Locale; 045 046import org.apache.commons.logging.Log; 047 048/** 049 * Represents the data of a sitemap tree node. 050 */ 051public class CmsSitemapTreeNodeData { 052 053 /** The log instance for this class. */ 054 @SuppressWarnings("unused") 055 private static final Log LOG = CmsLog.getLog(CmsSitemapTreeNodeData.class); 056 057 /** The client sitemap entry. */ 058 private CmsClientSitemapEntry m_entry; 059 060 /** Indicates whether we have definitely no children. */ 061 private boolean m_hasNoChildren; 062 063 /** Sitmap entry is copyable (ie has the 'Copy page' option). */ 064 private boolean m_isCopyable; 065 066 /** Flag indicating whether the linked resource is directly linked. */ 067 private boolean m_isDirectLink; 068 069 /** If m_linkedResource is a default file, then m_linkedEntry is its parent folder, else it's m_linkedResource itself. */ 070 private CmsResource m_linkedEntryResource; 071 072 /** The linked resource. */ 073 private CmsResource m_linkedResource; 074 075 /** String containing locales for which no translations should be provided, read from locale.notranslation property. */ 076 private String m_noTranslation; 077 078 /** The other locale. */ 079 private Locale m_otherLocale; 080 081 /** The entry resource. */ 082 private CmsResource m_resource; 083 084 /** 085 * Creates a new instance.<p> 086 * 087 * @param mainLocale the main locale 088 * @param otherLocale the other locale 089 */ 090 public CmsSitemapTreeNodeData(Locale mainLocale, Locale otherLocale) { 091 092 m_otherLocale = otherLocale; 093 } 094 095 /** 096 * Gets the client sitemap entry.<p> 097 * 098 * @return the client sitemap entry 099 */ 100 public CmsClientSitemapEntry getClientEntry() { 101 102 return m_entry; 103 } 104 105 /** 106 * Gets the linked resource or its parent folder, whichever corresponds to the sitemap entry. 107 * 108 * @return the linked entry resource 109 */ 110 public CmsResource getLinkedEntryResource() { 111 112 return m_linkedEntryResource; 113 } 114 115 /** 116 * Gets the linked resource.<p> 117 * 118 * @return the linked resource 119 */ 120 public CmsResource getLinkedResource() { 121 122 return m_linkedResource; 123 } 124 125 /** 126 * Gets the sitemap entry resource.<p> 127 * 128 * @return the resource 129 */ 130 public CmsResource getResource() { 131 132 return m_resource; 133 134 } 135 136 /** 137 * Returns true if the node definitely has no children to load.<p> 138 * 139 * @return true if there are definitely no children to load 140 */ 141 public boolean hasNoChildren() { 142 143 return m_hasNoChildren; 144 } 145 146 /** 147 * Initializes the bean.<p> 148 * 149 * @param cms the CMS context to use 150 * 151 * @throws CmsException if something goes wrong 152 */ 153 public void initialize(CmsObject cms) throws CmsException { 154 155 CmsUUID id = m_entry.getId(); 156 CmsResource resource = cms.readResource(id, CmsResourceFilter.IGNORE_EXPIRATION); 157 m_resource = resource; 158 CmsResource defaultFile = resource; 159 if (resource.isFolder()) { 160 defaultFile = cms.readDefaultFile(resource, CmsResourceFilter.IGNORE_EXPIRATION); 161 } 162 CmsLocaleGroup localeGroup = cms.getLocaleGroupService().readLocaleGroup(defaultFile); 163 CmsResource primary = localeGroup.getPrimaryResource(); 164 CmsProperty noTranslationProp = cms.readPropertyObject( 165 primary, 166 CmsPropertyDefinition.PROPERTY_LOCALE_NOTRANSLATION, 167 false); 168 m_noTranslation = noTranslationProp.getValue(); 169 CmsUUID defaultFileId = (defaultFile != null) ? defaultFile.getStructureId() : null; 170 m_isCopyable = (defaultFile != null) && CmsResourceTypeXmlContainerPage.isContainerPage(defaultFile); 171 Collection<CmsResource> resourcesForTargetLocale = localeGroup.getResourcesForLocale(m_otherLocale); 172 if (!resourcesForTargetLocale.isEmpty()) { 173 m_linkedResource = resourcesForTargetLocale.iterator().next(); 174 m_linkedEntryResource = CmsSitemapTreeController.readSitemapEntryFolderIfPossible(m_linkedResource); 175 if (primary.getStructureId().equals(m_resource.getStructureId()) 176 || primary.getStructureId().equals(defaultFileId) 177 || primary.getStructureId().equals(m_linkedResource.getStructureId())) { 178 m_isDirectLink = true; 179 } 180 } 181 } 182 183 /** 184 * Returns true if the 'Copy page' function should be offered for this entry.<p> 185 * 186 * @return true if the 'copy pgae' function should be available 187 */ 188 public boolean isCopyable() { 189 190 return m_isCopyable; 191 } 192 193 /** 194 * Returns true if the linked resource is directly linked.<p> 195 * 196 * @return true if the linked resource is directly linked 197 */ 198 public boolean isDirectLink() { 199 200 return m_isDirectLink; 201 202 } 203 204 /** 205 * Returns true if this sitemap entry has a linked resource.<p> 206 * 207 * @return true if there is a linked resource 208 */ 209 public boolean isLinked() { 210 211 return m_linkedResource != null; 212 } 213 214 /** 215 * Checks if this entry is marked as 'do not translate' for the given locale .<p> 216 * 217 * @param locale the locale 218 * @return true if the 'do not translate' mark for the given locale is set 219 */ 220 public boolean isMarkedNoTranslation(Locale locale) { 221 222 if (m_noTranslation != null) { 223 return CmsLocaleManager.getLocales(m_noTranslation).contains(locale); 224 } 225 return false; 226 } 227 228 /** 229 * Sets the client entry.<p> 230 * 231 * @param entry the client entry 232 */ 233 public void setClientEntry(CmsClientSitemapEntry entry) { 234 235 m_entry = entry; 236 } 237 238 /** 239 * Sets the 'has no children' flag.<p> 240 * 241 * @param b the new value 242 */ 243 public void setHasNoChildren(boolean b) { 244 245 m_hasNoChildren = b; 246 247 } 248 249}