001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH (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.gwt; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsProperty; 032import org.opencms.file.CmsPropertyDefinition; 033import org.opencms.file.CmsResource; 034import org.opencms.file.CmsResourceFilter; 035import org.opencms.file.types.CmsResourceTypeJsp; 036import org.opencms.gwt.shared.property.CmsClientTemplateBean; 037import org.opencms.main.CmsException; 038import org.opencms.main.OpenCms; 039import org.opencms.util.CmsMacroResolver; 040import org.opencms.util.CmsStringUtil; 041import org.opencms.workplace.CmsWorkplace; 042 043import java.util.HashMap; 044import java.util.List; 045import java.util.Map; 046 047/** 048 * Utility class for getting information about available templates.<p> 049 * 050 * @since 8.0.0 051 */ 052public class CmsTemplateFinder { 053 054 /** Macro which is used in template.provider property to be substituted with the template path. */ 055 public static final String MACRO_TEMPLATEPATH = "templatepath"; 056 057 /** The cms context. */ 058 protected CmsObject m_cms; 059 060 /** 061 * Creates a new instance.<p> 062 * 063 * @param cms the cms context to use 064 */ 065 066 public CmsTemplateFinder(CmsObject cms) { 067 068 m_cms = cms; 069 } 070 071 /** 072 * Returns the available templates.<p> 073 * 074 * @return the available templates 075 * 076 * @throws CmsException if something goes wrong 077 */ 078 public Map<String, CmsClientTemplateBean> getTemplates() throws CmsException { 079 080 Map<String, CmsClientTemplateBean> result = new HashMap<String, CmsClientTemplateBean>(); 081 CmsObject cms = getCmsObject(); 082 083 // find current site templates 084 int templateId = OpenCms.getResourceManager().getResourceType( 085 CmsResourceTypeJsp.getContainerPageTemplateTypeName()).getTypeId(); 086 List<CmsResource> templates = cms.readResources( 087 "/", 088 CmsResourceFilter.ONLY_VISIBLE_NO_DELETED.addRequireType(templateId), 089 true); 090 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(cms.getRequestContext().getSiteRoot())) { 091 // if not in the root site, also add template under /system/ 092 templates.addAll(cms.readResources( 093 CmsWorkplace.VFS_PATH_SYSTEM, 094 CmsResourceFilter.ONLY_VISIBLE_NO_DELETED.addRequireType(templateId), 095 true)); 096 } 097 // convert resources to template beans 098 for (CmsResource template : templates) { 099 CmsClientTemplateBean templateBean = getTemplateBean(cms, template); 100 result.put(templateBean.getSitePath(), templateBean); 101 } 102 return result; 103 } 104 105 /** 106 * Gets the CMS context to use.<p> 107 * 108 * @return the CMS context to use 109 */ 110 protected CmsObject getCmsObject() { 111 112 return m_cms; 113 } 114 115 /** 116 * Returns a bean representing the given template resource.<p> 117 * 118 * @param cms the cms context to use for VFS operations 119 * @param resource the template resource 120 * 121 * @return bean representing the given template resource 122 * 123 * @throws CmsException if something goes wrong 124 */ 125 private CmsClientTemplateBean getTemplateBean(CmsObject cms, CmsResource resource) throws CmsException { 126 127 CmsProperty titleProp = cms.readPropertyObject(resource, CmsPropertyDefinition.PROPERTY_TITLE, false); 128 CmsProperty descProp = cms.readPropertyObject(resource, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false); 129 CmsProperty imageProp = cms.readPropertyObject(resource, CmsPropertyDefinition.PROPERTY_TEMPLATE_IMAGE, false); 130 CmsProperty selectValueProp = cms.readPropertyObject( 131 resource, 132 CmsPropertyDefinition.PROPERTY_TEMPLATE_PROVIDER, 133 false); 134 String sitePath = cms.getSitePath(resource); 135 String templateValue = sitePath; 136 if (!selectValueProp.isNullProperty() && !CmsStringUtil.isEmptyOrWhitespaceOnly(selectValueProp.getValue())) { 137 String selectValue = selectValueProp.getValue(); 138 CmsMacroResolver resolver = new CmsMacroResolver(); 139 resolver.addMacro(MACRO_TEMPLATEPATH, sitePath); 140 templateValue = resolver.resolveMacros(selectValue); 141 } 142 return new CmsClientTemplateBean(titleProp.getValue(), descProp.getValue(), templateValue, imageProp.getValue()); 143 } 144 145}