001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/CmsSearchResult.java,v $
003: * Date : $Date: 2008-02-27 12:05:39 $
004: * Version: $Revision: 1.25 $
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 GmbH, 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;
033:
034: import org.opencms.monitor.CmsMemoryMonitor;
035: import org.opencms.monitor.I_CmsMemoryMonitorable;
036: import org.opencms.search.fields.CmsSearchField;
037: import org.opencms.util.CmsStringUtil;
038:
039: import java.text.ParseException;
040: import java.util.Date;
041: import java.util.HashMap;
042: import java.util.Iterator;
043: import java.util.Map;
044:
045: import org.apache.lucene.document.DateTools;
046: import org.apache.lucene.document.Document;
047: import org.apache.lucene.document.Field;
048:
049: /**
050: * Contains the data of a single item in a search result.<p>
051: *
052: * @author Alexander Kandzior
053: * @author Thomas Weckert
054: *
055: * @version $Revision: 1.25 $
056: *
057: * @since 6.0.0
058: */
059: public class CmsSearchResult implements I_CmsMemoryMonitorable,
060: Comparable {
061:
062: /** The creation date of this search result. */
063: protected Date m_dateCreated;
064:
065: /** The last modification date of this search result. */
066: protected Date m_dateLastModified;
067:
068: /** The document type of the search result. */
069: protected String m_documentType;
070:
071: /** The excerpt of this search result. */
072: protected String m_excerpt;
073:
074: /** The resource path of this search result. */
075: protected String m_path;
076:
077: /** The score of this search result. */
078: protected int m_score;
079:
080: /** Holds the values of the search result fields. */
081: Map m_fields;
082:
083: /** Contains the pre-calculated memory size. */
084: private int m_memorySize;
085:
086: /**
087: * Creates a new search result.<p>
088: *
089: * @param score the score of this search result
090: * @param doc the Lucene document to extract fields from such as description, title, key words etc. pp.
091: * @param excerpt the excerpt of the search result's content
092: */
093: protected CmsSearchResult(int score, Document doc, String excerpt) {
094:
095: m_score = score;
096: m_excerpt = excerpt;
097: m_fields = new HashMap();
098:
099: Iterator i = doc.getFields().iterator();
100: while (i.hasNext()) {
101: Field field = (Field) i.next();
102: if ((field != null) && field.isStored()) {
103: // content can be displayed only if it has been stored in the field
104: String name = field.name();
105: String value = field.stringValue();
106: if (CmsStringUtil.isNotEmpty(value)
107: && !CmsSearchField.FIELD_PATH.equals(name)
108: && !CmsSearchField.FIELD_DATE_CREATED
109: .equals(name)
110: && !CmsSearchField.FIELD_DATE_LASTMODIFIED
111: .equals(name)) {
112: // these "hard coded" fields are treated differently
113: m_fields.put(name, value);
114: }
115: }
116: }
117:
118: Field f = doc.getField(CmsSearchField.FIELD_PATH);
119: if (f != null) {
120: m_path = f.stringValue();
121: } else {
122: m_path = null;
123: }
124:
125: f = doc.getField(CmsSearchField.FIELD_DATE_CREATED);
126: if (f != null) {
127: try {
128: m_dateCreated = DateTools.stringToDate(f.stringValue());
129: } catch (ParseException exc) {
130: m_dateCreated = null;
131: }
132: } else {
133: m_dateCreated = null;
134: }
135:
136: f = doc.getField(CmsSearchField.FIELD_DATE_LASTMODIFIED);
137: if (f != null) {
138: try {
139: m_dateLastModified = DateTools.stringToDate(f
140: .stringValue());
141: } catch (ParseException exc) {
142: m_dateLastModified = null;
143: }
144: } else {
145: m_dateLastModified = null;
146: }
147:
148: f = doc.getField(CmsSearchField.FIELD_TYPE);
149: if (f != null) {
150: m_documentType = f.stringValue();
151: } else {
152: m_documentType = null;
153: }
154: }
155:
156: /**
157: * @see java.lang.Comparable#compareTo(java.lang.Object)
158: */
159: public int compareTo(Object obj) {
160:
161: if (obj == this ) {
162: return 0;
163: }
164: if (obj instanceof CmsSearchResult) {
165: return ((CmsSearchResult) obj).m_score - m_score;
166: }
167: return 0;
168: }
169:
170: /**
171: * @see java.lang.Object#equals(java.lang.Object)
172: */
173: public boolean equals(Object obj) {
174:
175: if (obj == this ) {
176: return true;
177: }
178: if (obj instanceof CmsSearchResult) {
179: CmsSearchResult other = (CmsSearchResult) obj;
180: return m_documentType.equals(other.m_documentType)
181: && m_path.equals(other.m_path);
182: }
183: return false;
184: }
185:
186: /**
187: * Returns the date created.<p>
188: *
189: * @return the date created
190: */
191: public Date getDateCreated() {
192:
193: return (Date) m_dateCreated.clone();
194: }
195:
196: /**
197: * Returns the date last modified.<p>
198: *
199: * @return the date last modified
200: */
201: public Date getDateLastModified() {
202:
203: return (Date) m_dateLastModified.clone();
204: }
205:
206: /**
207: * Returns the description.<p>
208: *
209: * @return the description
210: *
211: * @deprecated use {@link #getField(String)} instead with the name of the field,
212: * for example use {@link CmsSearchField#FIELD_DESCRIPTION} to get the description (if available)
213: */
214: public String getDescription() {
215:
216: return getField(CmsSearchField.FIELD_DESCRIPTION);
217: }
218:
219: /**
220: * Returns the document type of the search result document.<p>
221: *
222: * Usually this will be a VFS resource type String that can be used in the
223: * resource type manager with {@link org.opencms.loader.CmsResourceManager#getResourceType(String)}.
224: * However, what is stored in the document type field depends only on the indexer used, and therefore it
225: * may also be some String not referring a VFS resource type but some external type or application.
226: * It may also be <code>null</code> in case it has not been set by a non-standard indexer.<p>
227: *
228: * @return the document type of the search result document
229: *
230: * @see org.opencms.loader.CmsResourceManager#getResourceType(String)
231: */
232: public String getDocumentType() {
233:
234: return m_documentType;
235: }
236:
237: /**
238: * Returns the excerpt.<p>
239: *
240: * @return the excerpt
241: */
242: public String getExcerpt() {
243:
244: return m_excerpt;
245: }
246:
247: /**
248: * Returns the text stored in the search index field with the given name.<p>
249: *
250: * @param fieldName the name of the field to get the stored text for
251: *
252: * @return the text stored in the search index field with the given name
253: */
254: public String getField(String fieldName) {
255:
256: return (String) m_fields.get(fieldName);
257: }
258:
259: /**
260: * Returns the key words.<p>
261: *
262: * @return the key words
263: *
264: * @deprecated use {@link #getField(String)} instead with the name of the field,
265: * for example use {@link CmsSearchField#FIELD_KEYWORDS} to get the keywords (if available)
266: */
267: public String getKeywords() {
268:
269: return getField(CmsSearchField.FIELD_KEYWORDS);
270: }
271:
272: /**
273: * @see org.opencms.monitor.I_CmsMemoryMonitorable#getMemorySize()
274: */
275: public int getMemorySize() {
276:
277: if (m_memorySize == 0) {
278: int result = 8;
279: if (m_dateCreated != null) {
280: result += CmsMemoryMonitor.getMemorySize(m_dateCreated);
281: }
282: if (m_dateLastModified != null) {
283: result += CmsMemoryMonitor
284: .getMemorySize(m_dateLastModified);
285: }
286: if (m_path != null) {
287: result += CmsMemoryMonitor.getMemorySize(m_path);
288: }
289: if (m_fields != null) {
290: Iterator entries = m_fields.entrySet().iterator();
291: while (entries.hasNext()) {
292: Map.Entry entry = (Map.Entry) entries.next();
293: result += CmsMemoryMonitor.getMemorySize(entry
294: .getKey());
295: result += CmsMemoryMonitor.getMemorySize(entry
296: .getValue());
297: }
298: }
299: if (m_excerpt != null) {
300: result += CmsMemoryMonitor.getMemorySize(m_excerpt);
301: }
302: m_memorySize = result;
303: }
304: return m_memorySize;
305: }
306:
307: /**
308: * Returns the path.<p>
309: *
310: * @return the path
311: */
312: public String getPath() {
313:
314: return m_path;
315: }
316:
317: /**
318: * Returns the score.<p>
319: *
320: * @return the score
321: */
322: public int getScore() {
323:
324: return m_score;
325: }
326:
327: /**
328: * Returns the title.<p>
329: *
330: * @return the title
331: *
332: * @deprecated use {@link #getField(String)} instead with the name of the field,
333: * for example use {@link CmsSearchField#FIELD_TITLE} to get the title (if available)
334: */
335: public String getTitle() {
336:
337: return getField(CmsSearchField.FIELD_TITLE);
338: }
339:
340: /**
341: * @see java.lang.Object#hashCode()
342: */
343: public int hashCode() {
344:
345: return m_documentType.hashCode() * 1109 + m_path.hashCode();
346: }
347: }
|