001: package org.nemesis.forum.search;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.Iterator;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009: import org.apache.lucene.analysis.Analyzer;
010: import org.apache.lucene.analysis.StopAnalyzer;
011: import org.apache.lucene.index.IndexReader;
012: import org.apache.lucene.index.IndexWriter;
013: import org.apache.lucene.index.Term;
014: import org.nemesis.forum.Authorization;
015: import org.nemesis.forum.Forum;
016: import org.nemesis.forum.ForumFactory;
017: import org.nemesis.forum.ForumThread;
018: import org.nemesis.forum.Message;
019: import org.nemesis.forum.TreeWalker;
020: import org.nemesis.forum.impl.DbForumMessage;
021: import org.nemesis.forum.impl.DbForumThread;
022: import org.nemesis.forum.config.ConfigLoader;
023: import org.nemesis.forum.config.Constants;
024: import org.nemesis.forum.event.ForumEvent;
025: import org.nemesis.forum.event.ForumListener;
026: import org.nemesis.forum.exception.UnauthorizedException;
027:
028: /**
029: * @author dlaurent
030: *
031: *
032: */
033: public class Indexer implements ForumListener {
034:
035: static protected Log log = LogFactory.getLog(Indexer.class);
036: private static boolean autoIndex = ConfigLoader.getInstance()
037: .getConfig().isAutoIndex();
038:
039: final private static String PATH = ConfigLoader.getInstance()
040: .getConfig().getDataDir()
041: + File.separator + "index";
042:
043: private static Object indexLock = new Object();
044:
045: private static Analyzer analyzer = new StopAnalyzer();
046:
047: static {
048: Indexer i = new Indexer();
049: DbForumMessage.addListener(i);
050: DbForumThread.addListener(i);
051: }
052:
053: public void objectCreated(ForumEvent event) {
054: log.debug("Indexer.objectCreated()");
055: if (autoIndex)
056: index((Message) event.getSource());
057: }
058:
059: public void objectModified(ForumEvent event) {
060: log.debug("Indexer.objectModified()");
061: if (!autoIndex)
062: return;
063: remove((Message) event.getSource());
064: index((Message) event.getSource());
065: }
066:
067: public void objectDeleted(ForumEvent event) {
068: log.debug("Indexer.objectDeleted()");
069: if (autoIndex)
070: remove((Message) event.getSource());
071: }
072:
073: private static void remove(Message m) {
074: //remove recursive from index if doc not approved
075: TreeWalker walker = m.getForumThread().treeWalker();
076: removeFromIndex(m, walker);
077: }
078:
079: private static void removeFromIndex(Message m, TreeWalker walker) {
080:
081: synchronized (indexLock) {
082: log.debug("Indexer.removeFromIndex()" + m.getID());
083: Term term = new Term("messageID", m.getID() + "");
084: try {
085: IndexReader reader = IndexReader.open(new File(PATH));
086: reader.delete(term);
087: reader.close();
088: } catch (IOException e) {
089: log.error("impossible de supprimer le message "
090: + m.getID() + " de l'index ", e);
091: }
092: }
093:
094: for (int i = 0; i < walker.getChildCount(m); i++) {
095: removeFromIndex(walker.getChild(m, i), walker);
096: }
097:
098: }
099:
100: private static void index(Message m) {
101: TreeWalker walker = m.getForumThread().treeWalker();
102: addToIndex(m, walker);
103:
104: }
105:
106: private static void addToIndex(Message m, TreeWalker walker) {
107:
108: if (!m.isApproved())
109: return;
110:
111: synchronized (indexLock) {
112: log.debug("Indexer.addFromIndex()" + m.getID());
113: IndexWriter writer = null;
114: try {
115:
116: if (IndexReader.indexExists(PATH)) {
117: writer = new IndexWriter(new File(PATH),
118: new StopAnalyzer(), false);
119: } else {
120: writer = new IndexWriter(new File(PATH),
121: new StopAnalyzer(), true);
122: }
123: writer.addDocument(MessageDocument.getDocument(m));
124:
125: } catch (Exception e) {
126: log.error("addToIndex", e);
127: } finally {
128: try {
129: writer.close();
130: } catch (Exception e) {
131: }
132: }
133:
134: }
135:
136: for (int i = 0; i < walker.getChildCount(m); i++) {
137: addToIndex(walker.getChild(m, i), walker);
138: }
139:
140: }
141:
142: public static void flush(Authorization auth)
143: throws UnauthorizedException {
144: ForumFactory forumFactory = ForumFactory.getInstance(auth);
145: if (!forumFactory.getPermissions(auth).get(
146: Constants.SYSTEM_ADMIN))
147: throw new UnauthorizedException();
148: try {
149: IndexWriter writer = new IndexWriter(new File(PATH),
150: new StopAnalyzer(), true);
151: writer.close();
152: } catch (IOException e) {
153: log.error("impossible de flusher l'index ", e);
154: }
155: }
156:
157: public static void reindex(Authorization auth)
158: throws UnauthorizedException {
159: ForumFactory forumFactory = ForumFactory.getInstance(auth);
160: if (!forumFactory.getPermissions(auth).get(
161: Constants.SYSTEM_ADMIN))
162: throw new UnauthorizedException();
163: //ok system admin
164: flush(auth);
165: Forum f = null;
166: ForumThread ft = null;
167: for (Iterator it = forumFactory.forums(); it.hasNext();) {
168: f = (Forum) it.next();
169: for (Iterator it2 = f.threads(); it2.hasNext();) {
170: ft = (ForumThread) it2.next();
171: index(ft.getRootMessage());
172: }
173: }
174:
175: }
176:
177: /**
178: * @return
179: */
180: public static boolean isAutoIndex() {
181: return autoIndex;
182: }
183:
184: /**
185: * @param b
186: */
187: public static void setAutoIndex(boolean b, Authorization auth)
188: throws UnauthorizedException {
189: ForumFactory forumFactory = ForumFactory.getInstance(auth);
190: if (!forumFactory.getPermissions(auth).get(
191: Constants.SYSTEM_ADMIN))
192: throw new UnauthorizedException();
193: autoIndex = b;
194: }
195:
196: }
|