1 6 7 package org.contineo.actions.admin.searchengine; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.util.Collection ; 12 import java.util.Iterator ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 import javax.servlet.http.HttpSession ; 16 import org.apache.log4j.Level; 17 import org.apache.log4j.Logger; 18 import org.apache.lucene.analysis.de.GermanAnalyzer; 19 import org.apache.lucene.analysis.fr.FrenchAnalyzer; 20 import org.apache.lucene.analysis.standard.StandardAnalyzer; 21 import org.apache.lucene.index.IndexWriter; 22 import org.apache.struts.action.Action; 23 import org.apache.struts.action.ActionForm; 24 import org.apache.struts.action.ActionForward; 25 import org.apache.struts.action.ActionMapping; 26 import org.contineo.admin.Menu; 27 import org.contineo.admin.dao.MenuDAO; 28 import org.contineo.core.FileBean; 29 import org.contineo.core.LoggingManager; 30 import org.contineo.core.SessionManagement; 31 import org.contineo.core.config.SettingConfigurator; 32 import org.contineo.documan.Document; 33 import org.contineo.documan.dao.DocumentDAO; 34 import org.contineo.searchengine.crawler.Indexer; 35 36 41 public class IndexAction extends Action { 42 43 47 private Logger logger; 48 49 50 51 public IndexAction() { 52 logger = LoggingManager.getLogger(this.getClass()); 53 } 54 55 public ActionForward execute(ActionMapping mapping, 56 ActionForm form, HttpServletRequest request, 57 HttpServletResponse response) { 58 ActionForward actionForward = new ActionForward(); 59 HttpSession session = request.getSession(); 60 if (SessionManagement.isValid(session)) { 61 try { 62 SettingConfigurator conf = new SettingConfigurator(); 63 String path = conf.getValue("indexdir"); 64 if (!path.endsWith(File.pathSeparator)) 65 path += "/"; 66 try { 67 FileBean.deleteDir(path + "english/"); 68 FileBean.deleteDir(path + "french/"); 69 FileBean.deleteDir(path + "german/"); 70 FileBean.createDir(path + "english/"); 71 FileBean.createDir(path + "french/"); 72 FileBean.createDir(path + "german/"); 73 new IndexWriter(path + "english/" ,new StandardAnalyzer(), true); 74 new IndexWriter(path + "french/" ,new FrenchAnalyzer(), true); 75 new IndexWriter(path + "german/" ,new GermanAnalyzer(), true); 76 } catch (IOException ioe) { 77 if (logger.isEnabledFor(Level.ERROR)) 78 logger.error(ioe.getMessage()); 79 } 80 MenuDAO menuDao = new MenuDAO(); 81 DocumentDAO documentDao = new DocumentDAO(); 82 Collection documents = documentDao.findAll(); 83 Iterator iter = documents.iterator(); 84 Indexer indexer = new Indexer(); 85 while (iter.hasNext()) { 86 Document document = (Document)iter.next(); 87 Menu menu = menuDao.findByPrimaryKey(document.getMenuId()); 88 String dir = conf.getValue("docdir") + "/"; 89 dir += menu.getMenuPath() + "/" + menu.getMenuId(); 90 indexer.addDirectory(new File (dir), document); 91 } 92 actionForward.setPath("/Searchengine.do"); 93 } catch (Exception e) { 94 if (logger.isEnabledFor(Level.ERROR)) 95 logger.error(e.getMessage()); 96 actionForward = mapping.findForward("error"); 97 } 98 } else 99 actionForward = mapping.findForward("invalid"); 100 return actionForward; 101 } 102 } 103 | Popular Tags |