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 GmbH & Co. KG, 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.workplace.explorer; 029 030import org.opencms.file.CmsFile; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsPropertyDefinition; 033import org.opencms.file.CmsResource; 034import org.opencms.file.CmsResourceFilter; 035import org.opencms.main.CmsException; 036import org.opencms.main.CmsLog; 037import org.opencms.util.CmsStringUtil; 038import org.opencms.workplace.CmsWorkplace; 039 040import java.util.ArrayList; 041import java.util.List; 042import java.util.TreeMap; 043 044import org.apache.commons.logging.Log; 045 046/** 047 * The new resource page dialog handles the creation of an xml page.<p> 048 * 049 * The following files use this class: 050 * <ul> 051 * <li>/commons/newresource_xmlpage.jsp 052 * </ul> 053 * <p> 054 * 055 * @since 6.0.0 056 */ 057public class CmsNewResourceXmlPage { 058 059 /** Request parameter name for the selected body. */ 060 public static final String PARAM_BODYFILE = "bodyfile"; 061 062 /** Request parameter name for the suffix check. */ 063 public static final String PARAM_SUFFIXCHECK = "suffixcheck"; 064 065 /** Request parameter name for the selected template. */ 066 public static final String PARAM_TEMPLATE = "template"; 067 068 /** The log object for this class. */ 069 private static final Log LOG = CmsLog.getLog(CmsNewResourceXmlPage.class); 070 071 /** 072 * Returns a sorted Map of all available body files of the OpenCms modules.<p> 073 * 074 * @param cms the current cms object 075 * @param currWpPath the current path in the OpenCms workplace 076 * 077 * @return a sorted map with the body file title as key and absolute path to the body file as value 078 * 079 * @throws CmsException if reading a folder or file fails 080 */ 081 public static TreeMap<String, String> getBodies(CmsObject cms, String currWpPath) throws CmsException { 082 083 return getElements(cms, CmsWorkplace.VFS_DIR_DEFAULTBODIES, currWpPath, true); 084 } 085 086 /** 087 * Returns a sorted Map of all available body files of the OpenCms modules.<p> 088 * 089 * @param cms the current cms object 090 * @param currWpPath the current path in the OpenCms workplace 091 * @param emptyMap flag indicating if it is OK to return a filtered empty Map 092 * 093 * @return a sorted map with the body file title as key and absolute path to the body file as value 094 * 095 * @throws CmsException if reading a folder or file fails 096 */ 097 public static TreeMap<String, String> getBodies(CmsObject cms, String currWpPath, boolean emptyMap) 098 throws CmsException { 099 100 return getElements(cms, CmsWorkplace.VFS_DIR_DEFAULTBODIES, currWpPath, emptyMap); 101 } 102 103 /** 104 * Returns a sorted Map of all available templates of the OpenCms modules.<p> 105 * 106 * @param cms the current cms object 107 * @param currWpPath the current path in the OpenCms workplace 108 * 109 * @return a sorted map with the template title as key and absolute path to the template as value 110 * 111 * @throws CmsException if reading a folder or file fails 112 */ 113 public static TreeMap<String, String> getTemplates(CmsObject cms, String currWpPath) throws CmsException { 114 115 return getElements(cms, CmsWorkplace.VFS_DIR_TEMPLATES, currWpPath, true); 116 } 117 118 /** 119 * Returns a sorted Map of all available templates of the OpenCms modules.<p> 120 * 121 * @param cms the current cms object 122 * @param currWpPath the current path in the OpenCms workplace 123 * @param emptyMap flag indicating if it is OK to return a filtered empty Map 124 * 125 * @return a sorted map with the template title as key and absolute path to the template as value 126 * 127 * @throws CmsException if reading a folder or file fails 128 */ 129 public static TreeMap<String, String> getTemplates(CmsObject cms, String currWpPath, boolean emptyMap) 130 throws CmsException { 131 132 return getElements(cms, CmsWorkplace.VFS_DIR_TEMPLATES, currWpPath, emptyMap); 133 } 134 135 /** 136 * Returns a sorted Map of all available elements in the specified subfolder of the OpenCms modules.<p> 137 * 138 * @param cms the current cms object 139 * @param elementFolder the module subfolder to search for elements 140 * @param currWpPath the current path in the OpenCms workplace 141 * @param emptyMap flag indicating if it is OK to return a filtered empty Map 142 * 143 * @return a sorted map with the element title as key and absolute path to the element as value 144 * 145 * @throws CmsException if reading a folder or file fails 146 */ 147 protected static TreeMap<String, String> getElements( 148 CmsObject cms, 149 String elementFolder, 150 String currWpPath, 151 boolean emptyMap) 152 throws CmsException { 153 154 TreeMap<String, String> elements = new TreeMap<String, String>(); 155 TreeMap<String, String> allElements = new TreeMap<String, String>(); 156 157 if (CmsStringUtil.isNotEmpty(currWpPath)) { 158 // add site root to current workplace path 159 currWpPath = cms.getRequestContext().addSiteRoot(currWpPath); 160 } 161 162 // get all visible template elements in the module folders 163 List<CmsResource> modules = cms.getSubFolders( 164 CmsWorkplace.VFS_PATH_MODULES, 165 CmsResourceFilter.IGNORE_EXPIRATION); 166 for (int i = 0; i < modules.size(); i++) { 167 List<CmsResource> moduleTemplateFiles = new ArrayList<CmsResource>(); 168 String folder = cms.getSitePath(modules.get(i)); 169 try { 170 moduleTemplateFiles = cms.getFilesInFolder( 171 folder + elementFolder, 172 CmsResourceFilter.DEFAULT.addRequireVisible()); 173 } catch (CmsException e) { 174 // folder not available, list will be empty 175 if (LOG.isDebugEnabled()) { 176 LOG.debug(e.getMessage(), e); 177 } 178 } 179 for (int j = 0; j < moduleTemplateFiles.size(); j++) { 180 // get the current template file 181 CmsFile templateFile = (CmsFile)moduleTemplateFiles.get(j); 182 String title = null; 183 String folderProp = null; 184 try { 185 title = cms.readPropertyObject( 186 cms.getSitePath(templateFile), 187 CmsPropertyDefinition.PROPERTY_TITLE, 188 false).getValue(); 189 folderProp = cms.readPropertyObject( 190 templateFile, 191 CmsPropertyDefinition.PROPERTY_FOLDERS_AVAILABLE, 192 false).getValue(); 193 } catch (CmsException e) { 194 // property not available, will be null 195 if (LOG.isInfoEnabled()) { 196 LOG.info(e.getLocalizedMessage(), e); 197 } 198 } 199 200 boolean isInFolder = false; 201 // check template folders property value 202 if (CmsStringUtil.isNotEmpty(currWpPath) && CmsStringUtil.isNotEmpty(folderProp)) { 203 // property value set on template, check if current workplace path fits 204 List<String> folders = CmsStringUtil.splitAsList(folderProp, CmsNewResource.DELIM_PROPERTYVALUES); 205 for (int k = 0; k < folders.size(); k++) { 206 String checkFolder = folders.get(k); 207 if (currWpPath.startsWith(checkFolder)) { 208 isInFolder = true; 209 break; 210 } 211 } 212 } else { 213 isInFolder = true; 214 } 215 216 if (title == null) { 217 // no title property found, display the file name 218 title = templateFile.getName(); 219 } 220 String path = cms.getSitePath(templateFile); 221 if (isInFolder) { 222 // element is valid, add it to result 223 elements.put(title, path); 224 } 225 // also put element to overall result 226 allElements.put(title, path); 227 } 228 } 229 if (!emptyMap && (elements.size() < 1)) { 230 // empty Map should not be returned, return all collected elements 231 return allElements; 232 } 233 // return the filtered elements sorted by title 234 return elements; 235 } 236}