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.configuration; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.file.CmsResourceFilter; 033import org.opencms.main.CmsException; 034import org.opencms.main.CmsLog; 035import org.opencms.main.OpenCms; 036import org.opencms.util.CmsStringUtil; 037 038import org.apache.commons.logging.Log; 039 040/** 041 * A bean which represents the location configured for content elements of a specific type in a sitemap configuration.<p> 042 */ 043public class CmsContentFolderDescriptor { 044 045 /** Name of the folder for elements stored with container pages. */ 046 public static final String ELEMENTS_FOLDER_NAME = ".elements"; 047 048 /** The logger instance for this class. */ 049 private static final Log LOG = CmsLog.getLog(CmsContentFolderDescriptor.class); 050 051 /** The base path which the folder name is relative to. */ 052 private String m_basePath; 053 054 /** The folder resource's structure id. */ 055 private CmsResource m_folder; 056 057 /** The folder name. */ 058 private String m_folderName; 059 060 /** The 'isPageRelative' flag. If true, elements of this type will be stored with the pages on which they were created. */ 061 private boolean m_isPageRelative; 062 063 /** 064 * Creates an instance based on an existing folder.<p> 065 * 066 * @param folder the folder 067 */ 068 public CmsContentFolderDescriptor(CmsResource folder) { 069 070 m_folder = folder; 071 } 072 073 /** 074 * Creates an instance based on a relative folder name.<p> 075 * 076 * @param basePath the base path which the folder name is relative to 077 * @param name the relative folder name 078 */ 079 public CmsContentFolderDescriptor(String basePath, String name) { 080 081 m_basePath = basePath; 082 083 m_folderName = name; 084 } 085 086 /** 087 * Private constructor which does nothing.<p> 088 */ 089 private CmsContentFolderDescriptor() { 090 091 } 092 093 /** 094 * Creates folder descriptor which represents the 'page relative' setting.<p> 095 * 096 * @return the folder descriptor for the 'page relative' setting 097 */ 098 public static CmsContentFolderDescriptor createPageRelativeFolderDescriptor() { 099 100 CmsContentFolderDescriptor result = new CmsContentFolderDescriptor(); 101 result.m_isPageRelative = true; 102 return result; 103 } 104 105 /** 106 * Gets the base path.<p> 107 * 108 * @return the base path 109 */ 110 public String getBasePath() { 111 112 return m_basePath; 113 } 114 115 /** 116 * Gets the folder.<p> 117 * 118 * @return the folder 119 */ 120 public CmsResource getFolder() { 121 122 return m_folder; 123 } 124 125 /** 126 * Gets the relative folder name if available, else null.<p> 127 * 128 * @return the relative folder name null 129 */ 130 public String getFolderName() { 131 132 return m_folderName; 133 } 134 135 /** 136 * Computes the folder root path.<p> 137 * 138 * @param cms the CMS context to use 139 * @param pageFolderPath the root path of the folder containing the current container page 140 * @return the folder root path 141 */ 142 public String getFolderPath(CmsObject cms, String pageFolderPath) { 143 144 if (m_folder != null) { 145 try { 146 return OpenCms.getADEManager().getRootPath( 147 m_folder.getStructureId(), 148 cms.getRequestContext().getCurrentProject().isOnlineProject()); 149 } catch (CmsException e) { 150 LOG.error(e.getLocalizedMessage(), e); 151 return m_folder.getRootPath(); 152 } 153 } else if (m_basePath != null) { 154 return CmsStringUtil.joinPaths(m_basePath, m_folderName); 155 } else if (m_isPageRelative) { 156 if (pageFolderPath == null) { 157 throw new IllegalArgumentException( 158 "getFolderPath called without page folder, but pageRelative is enabled!"); 159 } 160 try { 161 CmsResource folder = cms.readResource( 162 cms.getRequestContext().removeSiteRoot(pageFolderPath), 163 CmsResourceFilter.IGNORE_EXPIRATION); 164 if (folder.isFile()) { 165 // in case this is not a folder, use the parent 166 pageFolderPath = CmsResource.getParentFolder(pageFolderPath); 167 } 168 } catch (CmsException e) { 169 // ignore 170 } 171 return CmsStringUtil.joinPaths(pageFolderPath, ELEMENTS_FOLDER_NAME); 172 } else { 173 return CmsStringUtil.joinPaths( 174 cms.getRequestContext().getSiteRoot(), 175 CmsADEManager.CONTENT_FOLDER_NAME, 176 m_folderName); 177 } 178 179 } 180 181 /** 182 * Returns true if the current instance was created with a folder structure id parameter.<p> 183 * 184 * @return true if this instance was created with a folder structure id parameter 185 */ 186 public boolean isFolder() { 187 188 return m_folder != null; 189 } 190 191 /** 192 * Returns true if this instance was created with a folder name parameter.<p> 193 * 194 * @return true if this instance was created with a folder name parameter 195 */ 196 public boolean isName() { 197 198 return m_folderName != null; 199 } 200 201 /** 202 * Returns true if this page descriptor represents the 'page relative' setting.<p> 203 * 204 * @return true if this is page relative 205 */ 206 public boolean isPageRelative() { 207 208 return m_isPageRelative; 209 } 210 211}