KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > search > LuceneHitTracker


1 /*
2  * Created on May 2, 2004
3  */

4 package com.openedit.modules.search;
5
6 import java.io.IOException JavaDoc;
7 import java.text.ParseException JavaDoc;
8 import java.text.SimpleDateFormat JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 import org.apache.lucene.document.DateTools;
13 import org.apache.lucene.document.Document;
14 import org.apache.lucene.search.Hit;
15 import org.apache.lucene.search.Hits;
16
17 import com.openedit.OpenEditRuntimeException;
18 import com.openedit.hittracker.HitTracker;
19 import com.openedit.hittracker.SearchQuery;
20
21
22 /**
23  * @author cburkey
24  *
25  */

26 public class LuceneHitTracker extends HitTracker
27 {
28     protected Hits fieldHits;
29     
30     public LuceneHitTracker()
31     {
32         
33     }
34     
35     public LuceneHitTracker(Hits inHits)
36     {
37         setHits(inHits);
38     }
39     
40     public Hits getHits()
41     {
42         return fieldHits;
43     }
44     
45     public void setHits(Hits inHits)
46     {
47         fieldHits = inHits;
48     }
49     
50     public int getTotal()
51     {
52         if ( getHits() == null )
53         {
54             return 0;
55         }
56         else
57         {
58             return getHits().length();
59         }
60     }
61     
62     public Object JavaDoc get(int count)
63     {
64         try
65         {
66             return getHits().doc(count);
67         }
68         catch ( IOException JavaDoc ex)
69         {
70             throw new OpenEditRuntimeException(ex);
71         }
72     }
73     
74     public Iterator JavaDoc getAllHits()
75     {
76         return new HitIterator( getHits());
77     }
78     
79     public String JavaDoc toDate(String JavaDoc inValue)
80     {
81         if ( inValue == null)
82         {
83             return null;
84         }
85         Date JavaDoc date = null;
86         try
87         {
88             date = DateTools.stringToDate(inValue);
89         }
90         catch (ParseException JavaDoc ex)
91         {
92             throw new OpenEditRuntimeException(ex);
93         }
94         return SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT).format(date);
95     }
96     
97     public Integer JavaDoc findSelf( String JavaDoc inId) throws Exception JavaDoc
98     {
99         if( inId == null)
100         {
101             return null;
102         }
103         for (int i = 0; i < getTotal(); i++)
104         {
105             Document hit = (Document)get(i);
106             if ( inId.equals( hit.get("id")))
107             {
108                 return new Integer JavaDoc(i);
109             }
110         }
111         return null;
112     }
113     
114     public String JavaDoc previousId(String JavaDoc inId) throws Exception JavaDoc
115     {
116         Integer JavaDoc row = findSelf(inId);
117         if ( row != null && row.intValue() - 1 >= 0)
118         {
119             Document hit = (Document)get( row.intValue() - 1);
120             return hit.get("id");
121         }
122         return null;
123     }
124     public String JavaDoc nextId(String JavaDoc inId) throws Exception JavaDoc
125     {
126         Integer JavaDoc row = findSelf(inId);
127         if ( row != null && row.intValue() + 1 < getTotal())
128         {
129             Document hit = (Document)get( row.intValue() + 1);
130             return hit.get("id");
131         }
132         return null;
133     }
134
135     public boolean contains(Object JavaDoc inHit)
136     {
137         for (int i = 0; i < getTotal(); i++)
138         {
139             Document hit = (Document)get(i);
140             if ( hit.equals( inHit ) )
141             {
142                 return true;
143             }
144         }
145         return false;
146     }
147
148     
149 }
150
Popular Tags