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.search.fields;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsProperty;
033import org.opencms.file.CmsResource;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.search.extractors.I_CmsExtractionResult;
038import org.opencms.search.galleries.CmsGalleryNameMacroResolver;
039import org.opencms.util.CmsMacroResolver;
040import org.opencms.xml.content.CmsXmlContent;
041import org.opencms.xml.content.CmsXmlContentFactory;
042
043import java.util.List;
044
045import org.apache.commons.logging.Log;
046
047/**
048 * Adopted version of the default {@link org.opencms.search.fields.CmsSearchFieldMapping}
049 * that resolves macros via the {@link org.opencms.search.galleries.CmsGalleryNameMacroResolver} in the mapped value before returning it.
050 */
051public class CmsMacroResolvingSearchFieldMapping extends CmsSearchFieldMapping {
052
053    /** Serial version UID. */
054    private static final long serialVersionUID = 7690286960084342440L;
055
056    /** Logger for the class */
057    protected static final Log LOG = CmsLog.getLog(CmsMacroResolvingSearchFieldMapping.class);
058
059    /**
060     * Default constructor.
061     */
062    public CmsMacroResolvingSearchFieldMapping() {
063
064        super();
065    }
066
067    /**
068     * Calls the super method and resolves macros in the returned value.
069     *
070     * @see org.opencms.search.fields.CmsSearchFieldMapping#getStringValue(org.opencms.file.CmsObject, org.opencms.file.CmsResource, org.opencms.search.extractors.I_CmsExtractionResult, java.util.List, java.util.List)
071     */
072    @Override
073    public String getStringValue(
074        CmsObject cms,
075        CmsResource res,
076        I_CmsExtractionResult extractionResult,
077        List<CmsProperty> properties,
078        List<CmsProperty> propertiesSearched) {
079
080        String result = super.getStringValue(cms, res, extractionResult, properties, propertiesSearched);
081        if ((result != null) && !result.isEmpty()) {
082            try {
083                CmsObject cmsClone = OpenCms.initCmsObject(cms);
084                if (null != m_locale) {
085                    cmsClone.getRequestContext().setLocale(m_locale);
086                }
087                CmsFile file = cmsClone.readFile(res);
088                CmsXmlContent content = CmsXmlContentFactory.unmarshal(cmsClone, file);
089                CmsMacroResolver resolver = new CmsGalleryNameMacroResolver(cms, content, m_locale);
090                return resolver.resolveMacros(result);
091            } catch (CmsException e) {
092                LOG.error(
093                    "Failed to resolve macros in a search field mapping for content "
094                        + (res != null ? res.getRootPath() : "null")
095                        + ". Returning unresolved value.",
096                    e);
097            }
098        }
099        return result;
100    }
101
102}