1 4 package com.openedit.modules.search; 5 6 import java.io.IOException ; 7 import java.text.ParseException ; 8 import java.text.SimpleDateFormat ; 9 import java.util.Date ; 10 import java.util.Iterator ; 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 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 get(int count) 63 { 64 try 65 { 66 return getHits().doc(count); 67 } 68 catch ( IOException ex) 69 { 70 throw new OpenEditRuntimeException(ex); 71 } 72 } 73 74 public Iterator getAllHits() 75 { 76 return new HitIterator( getHits()); 77 } 78 79 public String toDate(String inValue) 80 { 81 if ( inValue == null) 82 { 83 return null; 84 } 85 Date date = null; 86 try 87 { 88 date = DateTools.stringToDate(inValue); 89 } 90 catch (ParseException ex) 91 { 92 throw new OpenEditRuntimeException(ex); 93 } 94 return SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT).format(date); 95 } 96 97 public Integer findSelf( String inId) throws Exception 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 (i); 109 } 110 } 111 return null; 112 } 113 114 public String previousId(String inId) throws Exception 115 { 116 Integer 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 nextId(String inId) throws Exception 125 { 126 Integer 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 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 |