001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/fields/CmsSearchFieldMapping.java,v $
003: * Date : $Date: 2008-02-27 12:05:31 $
004: * Version: $Revision: 1.6 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.search.fields;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.file.CmsResource;
036: import org.opencms.i18n.CmsMessageContainer;
037: import org.opencms.main.CmsException;
038: import org.opencms.main.CmsRuntimeException;
039: import org.opencms.search.Messages;
040: import org.opencms.search.extractors.I_CmsExtractionResult;
041: import org.opencms.util.CmsStringUtil;
042:
043: /**
044: * Describes a mapping of a piece of content from an OpenCms VFS resource to a field of a search index.<p>
045: *
046: * @author Alexander Kandzior
047: *
048: * @version $Revision: 1.6 $
049: *
050: * @since 7.0.0
051: */
052: public class CmsSearchFieldMapping {
053:
054: /** The configured default value. */
055: private String m_defaultValue;
056:
057: /** Pre-calculated hash value. */
058: private int m_hashCode;
059:
060: /** The parameter for the mapping type. */
061: private String m_param;
062:
063: /** The mapping type. */
064: private CmsSearchFieldMappingType m_type;
065:
066: /**
067: * Public constructor for a new search field mapping.<p>
068: */
069: public CmsSearchFieldMapping() {
070:
071: // no initialization required
072: }
073:
074: /**
075: * Public constructor for a new search field mapping.<p>
076: *
077: * @param type the type to use, see {@link #setType(CmsSearchFieldMappingType)}
078: * @param param the mapping parameter, see {@link #setParam(String)}
079: */
080: public CmsSearchFieldMapping(CmsSearchFieldMappingType type,
081: String param) {
082:
083: this ();
084: setType(type);
085: setParam(param);
086: }
087:
088: /**
089: * Two mappings are equal if the type and the parameter is equal.<p>
090: *
091: * @see java.lang.Object#equals(java.lang.Object)
092: */
093: public boolean equals(Object obj) {
094:
095: if (obj == this ) {
096: return true;
097: }
098: if (obj instanceof CmsSearchFieldMapping) {
099: CmsSearchFieldMapping other = (CmsSearchFieldMapping) obj;
100: return CmsStringUtil.isEqual(m_type, other.m_type)
101: && CmsStringUtil.isEqual(m_param, other.m_param);
102: }
103: return false;
104: }
105:
106: /**
107: * Returns the default value used for this field mapping in case no content is available.<p>
108: *
109: * @return the default value used for this field mapping in case no content is available
110: */
111: public String getDefaultValue() {
112:
113: return m_defaultValue;
114: }
115:
116: /**
117: * Returns the mapping parameter.<p>
118: *
119: * The parameter is used depending on the implementation of the rules of
120: * the selected {@link CmsSearchFieldMappingType}.<p>
121: *
122: * @return the mapping parameter
123: */
124: public String getParam() {
125:
126: return m_param;
127: }
128:
129: /**
130: * Returns the String value extracted form the provided data according to the rules of this mapping type.<p>
131: *
132: * @param cms the OpenCms context used for building the search index
133: * @param res the resource that is indexed
134: * @param extractionResult the plain text extraction result from the resource
135: *
136: * @return the String value extracted form the provided data according to the rules of this mapping type
137: */
138: public String getStringValue(CmsObject cms, CmsResource res,
139: I_CmsExtractionResult extractionResult) {
140:
141: String content = null;
142: switch (getType().getMode()) {
143: case 0: // content
144: if (extractionResult != null) {
145: content = extractionResult.getContent();
146: }
147: break;
148: case 1: // property
149: case 2: // property-search
150: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParam())) {
151: boolean search = (getType() == CmsSearchFieldMappingType.PROPERTY_SEARCH);
152: try {
153: content = cms.readPropertyObject(res, getParam(),
154: search).getValue();
155: } catch (CmsException e) {
156: // ignore, continue without content
157: }
158: }
159: break;
160: case 3: // item
161: if (extractionResult != null
162: && CmsStringUtil
163: .isNotEmptyOrWhitespaceOnly(getParam())) {
164: content = (String) extractionResult.getContentItems()
165: .get(getParam());
166: }
167: break;
168: default:
169: // noop, content is already null
170: }
171: if (content == null) {
172: // in case the content is not available, use the default value for this mapping
173: content = getDefaultValue();
174: }
175: return content;
176: }
177:
178: /**
179: * Returns the mapping type.<p>
180: *
181: * @return the mapping type
182: */
183: public CmsSearchFieldMappingType getType() {
184:
185: return m_type;
186: }
187:
188: /**
189: * The hash code depends on the type and the parameter.<p>
190: *
191: * @see java.lang.Object#hashCode()
192: */
193: public int hashCode() {
194:
195: if (m_hashCode == 0) {
196: int hashCode = 73 * ((m_type == null) ? 29 : m_type
197: .hashCode());
198: if (m_param != null) {
199: hashCode += m_param.hashCode();
200: }
201: m_hashCode = hashCode;
202: }
203: return m_hashCode;
204: }
205:
206: /**
207: * Sets the default value for this field mapping in case no content is available.<p>
208: *
209: * @param defaultValue the default value to set
210: */
211: public void setDefaultValue(String defaultValue) {
212:
213: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(defaultValue)) {
214: m_defaultValue = defaultValue.trim();
215: } else {
216: m_defaultValue = null;
217: }
218: }
219:
220: /**
221: * Sets the mapping parameter.<p>
222: *
223: * The parameter is used depending on the implementation of the rules of
224: * the selected {@link CmsSearchFieldMappingType}.<p>
225: *
226: * @param param the parameter to set
227: */
228: public void setParam(String param) {
229:
230: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(param)) {
231: m_param = param.trim();
232: } else {
233: m_param = null;
234: }
235: }
236:
237: /**
238: * Sets the mapping type.<p>
239: *
240: * @param type the type to set
241: */
242: public void setType(CmsSearchFieldMappingType type) {
243:
244: m_type = type;
245: }
246:
247: /**
248: * Sets the mapping type as a String.<p>
249: *
250: * @param type the name of the type to set
251: */
252: public void setType(String type) {
253:
254: CmsSearchFieldMappingType mappingType = CmsSearchFieldMappingType
255: .valueOf(type);
256: if (mappingType == null) {
257: // invalid mapping type has been used, throw an exception
258: throw new CmsRuntimeException(new CmsMessageContainer(
259: Messages.get(), Messages.ERR_FIELD_TYPE_UNKNOWN_1,
260: new Object[] { type }));
261: }
262: setType(mappingType);
263: }
264: }
|