KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > search > CmsSearchResult


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/CmsSearchResult.java,v $
3  * Date : $Date: 2006/03/27 14:52:54 $
4  * Version: $Revision: 1.20 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.search;
33
34 import org.opencms.monitor.CmsMemoryMonitor;
35 import org.opencms.monitor.I_CmsMemoryMonitorable;
36 import org.opencms.search.documents.I_CmsDocumentFactory;
37
38 import java.text.ParseException JavaDoc;
39 import java.util.Date JavaDoc;
40
41 import org.apache.lucene.document.DateTools;
42 import org.apache.lucene.document.Document;
43 import org.apache.lucene.document.Field;
44
45 /**
46  * Contains the data of a single item in a search result.<p>
47  *
48  * @author Thomas Weckert
49  *
50  * @version $Revision: 1.20 $
51  *
52  * @since 6.0.0
53  */

54 public class CmsSearchResult implements I_CmsMemoryMonitorable, Comparable JavaDoc {
55
56     /** The creation date of this search result. */
57     protected Date JavaDoc m_dateCreated;
58
59     /** The last modification date of this search result. */
60     protected Date JavaDoc m_dateLastModified;
61
62     /** The description of this search result. */
63     protected String JavaDoc m_description;
64
65     /** The excerpt of this search result. */
66     protected String JavaDoc m_excerpt;
67
68     /** The key words of this search result. */
69     protected String JavaDoc m_keyWords;
70
71     /** The resource path of this search result. */
72     protected String JavaDoc m_path;
73
74     /** The score of this search result. */
75     protected int m_score;
76
77     /** The title of this search result. */
78     protected String JavaDoc m_title;
79
80     /**
81      * Creates a new search result.<p>
82      *
83      * @param score the score of this search result
84      * @param luceneDocument the Lucene document to extract fields from such as description, title, key words etc. pp.
85      * @param excerpt the excerpt of the search result's content
86      */

87     protected CmsSearchResult(int score, Document luceneDocument, String JavaDoc excerpt) {
88
89         Field f;
90
91         m_score = score;
92         m_excerpt = excerpt;
93
94         f = luceneDocument.getField(I_CmsDocumentFactory.DOC_DESCRIPTION);
95         if (f != null) {
96             m_description = f.stringValue();
97         } else {
98             m_description = null;
99         }
100
101         f = luceneDocument.getField(I_CmsDocumentFactory.DOC_KEYWORDS);
102         if (f != null) {
103             m_keyWords = f.stringValue();
104         } else {
105             m_keyWords = null;
106         }
107
108         f = luceneDocument.getField(I_CmsDocumentFactory.DOC_TITLE_KEY);
109         if (f != null) {
110             m_title = f.stringValue();
111         } else {
112             m_title = null;
113         }
114
115         f = luceneDocument.getField(I_CmsDocumentFactory.DOC_PATH);
116         if (f != null) {
117             m_path = f.stringValue();
118         } else {
119             m_path = null;
120         }
121
122         f = luceneDocument.getField(I_CmsDocumentFactory.DOC_DATE_CREATED);
123         if (f != null) {
124             try {
125                 m_dateCreated = DateTools.stringToDate(f.stringValue());
126             } catch (ParseException JavaDoc exc) {
127                 m_dateCreated = null;
128             }
129         } else {
130             m_dateCreated = null;
131         }
132
133         f = luceneDocument.getField(I_CmsDocumentFactory.DOC_DATE_LASTMODIFIED);
134         if (f != null) {
135             try {
136                 m_dateLastModified = DateTools.stringToDate(f.stringValue());
137             } catch (ParseException JavaDoc exc) {
138                 m_dateLastModified = null;
139             }
140         } else {
141             m_dateLastModified = null;
142         }
143     }
144
145     /**
146      * @see java.lang.Comparable#compareTo(java.lang.Object)
147      */

148     public int compareTo(Object JavaDoc obj) {
149
150         if (obj == this) {
151             return 0;
152         }
153         if (obj instanceof CmsSearchResult) {
154             return ((CmsSearchResult)obj).m_score - m_score;
155         }
156         return 0;
157     }
158
159     /**
160      * Returns the date created.<p>
161      *
162      * @return the date created
163      */

164     public Date JavaDoc getDateCreated() {
165
166         return m_dateCreated;
167     }
168
169     /**
170      * Returns the date last modified.<p>
171      *
172      * @return the date last modified
173      */

174     public Date JavaDoc getDateLastModified() {
175
176         return m_dateLastModified;
177     }
178
179     /**
180      * Returns the description.<p>
181      *
182      * @return the description
183      */

184     public String JavaDoc getDescription() {
185
186         return m_description;
187     }
188
189     /**
190      * Returns the excerpt.<p>
191      *
192      * @return the excerpt
193      */

194     public String JavaDoc getExcerpt() {
195
196         return m_excerpt;
197     }
198
199     /**
200      * Returns the key words.<p>
201      *
202      * @return the key words
203      */

204     public String JavaDoc getKeywords() {
205
206         return m_keyWords;
207     }
208
209     /**
210      * @see org.opencms.monitor.I_CmsMemoryMonitorable#getMemorySize()
211      */

212     public int getMemorySize() {
213
214         int result = 8;
215
216         if (m_dateCreated != null) {
217             result += CmsMemoryMonitor.getMemorySize(m_dateCreated);
218         }
219
220         if (m_dateLastModified != null) {
221             result += CmsMemoryMonitor.getMemorySize(m_dateLastModified);
222         }
223
224         if (m_path != null) {
225             result += CmsMemoryMonitor.getMemorySize(m_path);
226         }
227
228         if (m_title != null) {
229             result += CmsMemoryMonitor.getMemorySize(m_title);
230         }
231
232         if (m_description != null) {
233             result += CmsMemoryMonitor.getMemorySize(m_description);
234         }
235
236         if (m_keyWords != null) {
237             result += CmsMemoryMonitor.getMemorySize(m_keyWords);
238         }
239
240         if (m_excerpt != null) {
241             result += CmsMemoryMonitor.getMemorySize(m_excerpt);
242         }
243
244         return result;
245     }
246
247     /**
248      * Returns the path.<p>
249      *
250      * @return the path
251      */

252     public String JavaDoc getPath() {
253
254         return m_path;
255     }
256
257     /**
258      * Returns the score.<p>
259      *
260      * @return the score
261      */

262     public int getScore() {
263
264         return m_score;
265     }
266
267     /**
268      * Returns the title.<p>
269      *
270      * @return the title
271      */

272     public String JavaDoc getTitle() {
273
274         return m_title;
275     }
276
277 }
Popular Tags