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.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(
093                cms.readResources(
094                    CmsWorkplace.VFS_PATH_SYSTEM,
095                    CmsResourceFilter.ONLY_VISIBLE_NO_DELETED.addRequireType(templateId),
096                    true));
097        }
098        // convert resources to template beans
099        for (CmsResource template : templates) {
100            CmsClientTemplateBean templateBean = getTemplateBean(cms, template);
101            result.put(templateBean.getSitePath(), templateBean);
102        }
103        return result;
104    }
105
106    /**
107     * Gets the CMS context to use.<p>
108     *
109     * @return the CMS context to use
110     */
111    protected CmsObject getCmsObject() {
112
113        return m_cms;
114    }
115
116    /**
117     * Returns a bean representing the given template resource.<p>
118     *
119     * @param cms the cms context to use for VFS operations
120     * @param resource the template resource
121     *
122     * @return bean representing the given template resource
123     *
124     * @throws CmsException if something goes wrong
125     */
126    private CmsClientTemplateBean getTemplateBean(CmsObject cms, CmsResource resource) throws CmsException {
127
128        CmsProperty titleProp = cms.readPropertyObject(resource, CmsPropertyDefinition.PROPERTY_TITLE, false);
129        CmsProperty descProp = cms.readPropertyObject(resource, CmsPropertyDefinition.PROPERTY_DESCRIPTION, false);
130        CmsProperty imageProp = cms.readPropertyObject(resource, CmsPropertyDefinition.PROPERTY_TEMPLATE_IMAGE, false);
131        CmsProperty selectValueProp = cms.readPropertyObject(
132            resource,
133            CmsPropertyDefinition.PROPERTY_TEMPLATE_PROVIDER,
134            false);
135        String sitePath = cms.getSitePath(resource);
136        String templateValue = sitePath;
137        if (!selectValueProp.isNullProperty() && !CmsStringUtil.isEmptyOrWhitespaceOnly(selectValueProp.getValue())) {
138            String selectValue = selectValueProp.getValue();
139            CmsMacroResolver resolver = new CmsMacroResolver();
140            resolver.addMacro(MACRO_TEMPLATEPATH, sitePath);
141            templateValue = resolver.resolveMacros(selectValue);
142        }
143        return new CmsClientTemplateBean(
144            titleProp.getValue(),
145            descProp.getValue(),
146            templateValue,
147            imageProp.getValue());
148    }
149
150}