1 /***
2 *
3 */
4 package hu.elte.tribus.services;
5
6 import hu.elte.tribus.model.QA;
7 import hu.elte.tribus.model.Topic;
8
9 import java.io.ByteArrayOutputStream;
10 import java.io.IOException;
11 import java.io.ObjectOutputStream;
12 import java.io.Serializable;
13 import java.io.StringReader;
14 import java.util.Iterator;
15
16 import org.apache.log4j.Logger;
17 import org.apache.lucene.analysis.Analyzer;
18 import org.apache.lucene.document.Document;
19 import org.apache.lucene.document.Field;
20 import org.apache.lucene.document.Field.Index;
21 import org.apache.lucene.document.Field.Store;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.IndexWriter;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.search.Hit;
26 import org.apache.lucene.search.Hits;
27 import org.apache.lucene.search.IndexSearcher;
28 import org.apache.lucene.search.TermQuery;
29 import org.apache.lucene.store.Directory;
30 import org.hibernate.EmptyInterceptor;
31 import org.hibernate.type.Type;
32
33 /***
34 * Cos life of the smurfs is not just fun.
35 *
36 * @author kocka
37 *
38 */
39 @SuppressWarnings("serial")
40 public class IndexingInterceptor extends EmptyInterceptor {
41
42 Directory directory = null;
43
44 Analyzer analyzer = null;
45
46 private final static Logger logger = Logger.getLogger(IndexingInterceptor.class);
47
48 void deleteDocument(String type, Serializable key) {
49 IndexSearcher indexSearcher = null;
50 IndexReader indexReader = null;
51 try {
52 indexSearcher = new IndexSearcher(directory);
53 indexReader = IndexReader.open(directory);
54 Hits hits = indexSearcher.search(new TermQuery(new Term("id", key.toString())));
55 Iterator it = hits.iterator();
56 if(hits.length() == 0) {
57 logger.warn("There was no document like that in index, consider full reindexing!");
58 }
59 while(it.hasNext()) {
60
61 Hit hit = (Hit) it.next();
62 logger.info("deleting from index:"+hit.getId());
63 indexReader.deleteDocument(hit.getId());
64 }
65 } catch (IOException e) {
66 logger.fatal("Indexing error at deleting document!", e);
67 } finally {
68 try {
69 if(indexSearcher != null) {
70 indexSearcher.close();
71 }
72 if(indexReader != null) {
73 indexReader.close();
74 }
75 } catch (IOException e) {
76 logger.warn("Hopszare", e);
77 }
78 }
79 }
80
81 void saveDocument(Document document) {
82 if(document == null) {
83 return;
84 }
85 IndexWriter indexWriter = null;
86 try {
87 indexWriter = new IndexWriter(directory, analyzer, false);
88 indexWriter.addDocument(document);
89 indexWriter.optimize();
90 } catch (IOException e) {
91 logger.fatal("Could not save document to index.", e);
92 } finally {
93 try {
94 if(indexWriter != null)
95 indexWriter.close();
96 } catch (IOException e) {
97 logger.warn("Could not close indexwriter.");
98 }
99 }
100
101 }
102
103 public static byte[] serialize(Serializable data) {
104 try {
105 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
106 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
107 objectOutputStream.writeObject(data);
108 return byteArrayOutputStream.toByteArray();
109 } catch (IOException e) {
110 logger.error("Errorn serializing",e);
111 return null;
112 }
113
114 }
115
116 Document createDocument(Serializable key, Object[] data, String[] properties) {
117
118 String question = null;
119 String answer = null;
120 Topic topic = null;
121 for (int i = 0; i < data.length; i++) {
122 if ("question".equals(properties[i]))
123 question = (String) data[i];
124 if ("answer".equals(properties[i]))
125 answer = (String) data[i];
126 if ("topic".equals(properties[i]))
127 topic = (Topic) data[i];
128 }
129 if(topic == null){
130 return null;
131 }
132
133 Document document = new Document();
134 document.add(new Field("type", new StringReader("qa")));
135 document.add(new Field("id", serialize(key), Store.YES));
136 document.add(new Field("topic",topic.getId().toString(), Store.YES, Index.UN_TOKENIZED));
137 document.add(new Field("question", new StringReader(question == null ? "" : question)));
138 document.add(new Field("answer", new StringReader(answer == null ? "" : answer)));
139 return document;
140 }
141
142 public void onDelete(Object entity, Serializable key, Object[] data,
143 String[] properties, Type[] types) {
144 if (entity instanceof QA) {
145 saveDocument(createDocument(key, data, properties));
146 }
147 }
148
149 public boolean onFlushDirty(Object entity, Serializable key, Object[] data,
150 Object[] previousState, String[] properties, Type[] types) {
151 if(entity instanceof QA) {
152 deleteDocument("qa", key);
153 saveDocument(createDocument(key, data, properties));
154 }
155 return false;
156 }
157
158 public boolean onSave(Object entity, Serializable key, Object[] data, String[] properties, Type[] types) {
159 if(entity instanceof QA) {
160 saveDocument(createDocument(key, data, properties));
161 }
162 return false;
163 }
164
165 public Analyzer getAnalyzer() {
166 return analyzer;
167 }
168
169 public void setAnalyzer(Analyzer analyzer) {
170 this.analyzer = analyzer;
171 }
172
173 public Directory getDirectory() {
174 return directory;
175 }
176
177 public void setDirectory(Directory directory) {
178 this.directory = directory;
179 }
180
181 }