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; 044import java.util.Locale; 045 046import org.apache.commons.logging.Log; 047 048/** 049 * Field mapping to resolve macros as for gallery names. 050 * 051 * The main purpose is to use stringtemplates for special mappings to Solr fields. 052 * 053 * For this use case, define a parameter (via <code>xsd:annotation/xsd:appinfo/parameters/param</code> and use a stringtemplate as value. 054 * In the solr mapping, you just place <code>%(stringtemplate:paramName)</code>. 055 * 056 * Example (there is some element "Type" and in the parameters section of the schema, there's a param "eventKind"): 057 * <pre> 058 * <searchsetting element="Type"> 059 * <solrfield targetfield="event-kind" sourcefield="*_s"> 060 * <mapping type="dynamic" class="org.opencms.search.fields.CmsSchemaParameterSearchFieldMapping">%(stringtemplate:eventKind)</mapping> 061 * </solrfield> 062 * </searchsetting> 063 * </pre> 064 */ 065public class CmsMacroSearchFieldMapping implements I_CmsSearchFieldMapping { 066 067 /** Serialization id */ 068 private static final long serialVersionUID = 1L; 069 070 /** Logger for the class */ 071 protected static final Log LOG = CmsLog.getLog(CmsMacroSearchFieldMapping.class); 072 073 /** The configuration parameter as handed over to the mapping. */ 074 private String m_param; 075 076 /** The mapping type. */ 077 private CmsSearchFieldMappingType m_type; 078 079 /** The default value set via the interface method. */ 080 private String m_defaultValue = null; 081 082 /** The content locale to index for. */ 083 private Locale m_locale = null; 084 085 /** 086 * Public constructor for a new search field mapping. 087 * <p> 088 */ 089 public CmsMacroSearchFieldMapping() { 090 091 m_param = null; 092 setType(CmsSearchFieldMappingType.DYNAMIC); 093 } 094 095 /** 096 * Public constructor for a new search field mapping. 097 * <p> 098 * 099 * @param type 100 * the type to use, see 101 * {@link #setType(CmsSearchFieldMappingType)} 102 * @param param 103 * the mapping parameter, see {@link #setParam(String)} 104 */ 105 public CmsMacroSearchFieldMapping(CmsSearchFieldMappingType type, String param) { 106 107 this(); 108 setParam(param); 109 setType(type); 110 } 111 112 /** 113 * @see org.opencms.search.fields.I_CmsSearchFieldMapping#getDefaultValue() 114 */ 115 public String getDefaultValue() { 116 117 return m_defaultValue; 118 } 119 120 /** 121 * @see org.opencms.search.fields.I_CmsSearchFieldMapping#getParam() 122 */ 123 public String getParam() { 124 125 return m_param; 126 } 127 128 /** 129 * @see org.opencms.search.fields.I_CmsSearchFieldMapping#getStringValue(org.opencms.file.CmsObject, org.opencms.file.CmsResource, org.opencms.search.extractors.I_CmsExtractionResult, java.util.List, java.util.List) 130 */ 131 public String getStringValue( 132 CmsObject cms, 133 CmsResource res, 134 I_CmsExtractionResult extractionResult, 135 List<CmsProperty> properties, 136 List<CmsProperty> propertiesSearched) { 137 138 if (m_param != null) { 139 try { 140 CmsObject cmsClone = OpenCms.initCmsObject(cms); 141 if (null != m_locale) { 142 cmsClone.getRequestContext().setLocale(m_locale); 143 } 144 CmsFile file = cmsClone.readFile(res); 145 CmsXmlContent content = CmsXmlContentFactory.unmarshal(cmsClone, file); 146 CmsMacroResolver resolver = new CmsGalleryNameMacroResolver(cms, content, m_locale); 147 return resolver.resolveMacros(m_param); 148 } catch (CmsException e) { 149 LOG.error("Failed to resolve search field mapping value. Returning null.", e); 150 } 151 } 152 return null; 153 } 154 155 /** 156 * @see org.opencms.search.fields.I_CmsSearchFieldMapping#getType() 157 */ 158 @Override 159 public CmsSearchFieldMappingType getType() { 160 161 return m_type; 162 } 163 164 /** 165 * @see org.opencms.search.fields.I_CmsSearchFieldMapping#setDefaultValue(java.lang.String) 166 */ 167 @Override 168 public void setDefaultValue(String defaultValue) { 169 170 m_defaultValue = defaultValue; 171 } 172 173 /** 174 * @see org.opencms.search.fields.I_CmsSearchFieldMapping#setLocale(java.util.Locale) 175 */ 176 @Override 177 public void setLocale(Locale locale) { 178 179 m_locale = locale; 180 } 181 182 /** 183 * @see org.opencms.search.fields.I_CmsSearchFieldMapping#setParam(java.lang.String) 184 */ 185 @Override 186 public void setParam(String param) { 187 188 m_param = param; 189 190 } 191 192 /** 193 * @see org.opencms.search.fields.I_CmsSearchFieldMapping#setType(org.opencms.search.fields.CmsSearchFieldMappingType) 194 */ 195 @Override 196 public void setType(CmsSearchFieldMappingType type) { 197 198 m_type = type; 199 200 } 201 202 /** 203 * @see org.opencms.search.fields.I_CmsSearchFieldMapping#setType(java.lang.String) 204 */ 205 @Override 206 public void setType(String type) { 207 208 m_type = CmsSearchFieldMappingType.valueOf(type); 209 210 } 211 212}