KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > actions > admin > searchengine > IndexAction


1 /*
2  * ChangePasswordAction.java
3  *
4  * Created on 16. Dezember 2003, 22:25
5  */

6
7 package org.contineo.actions.admin.searchengine;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import javax.servlet.http.HttpSession JavaDoc;
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 /**
37  *
38  * @author Michael Scholz
39  * @version 1.0
40  */

41 public class IndexAction extends Action {
42
43     /**
44      * @uml.property name="logger"
45      * @uml.associationEnd
46      */

47     private Logger logger;
48
49     
50     /** Creates a new instance of ChangePasswordAction */
51     public IndexAction() {
52         logger = LoggingManager.getLogger(this.getClass());
53     }
54     
55     public ActionForward execute(ActionMapping mapping,
56     ActionForm form, HttpServletRequest JavaDoc request,
57     HttpServletResponse JavaDoc response) {
58         ActionForward actionForward = new ActionForward();
59         HttpSession JavaDoc session = request.getSession();
60         if (SessionManagement.isValid(session)) {
61             try {
62                 SettingConfigurator conf = new SettingConfigurator();
63                 String JavaDoc 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 JavaDoc 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 JavaDoc documents = documentDao.findAll();
83                 Iterator JavaDoc 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 JavaDoc dir = conf.getValue("docdir") + "/";
89                     dir += menu.getMenuPath() + "/" + menu.getMenuId();
90                     indexer.addDirectory(new File JavaDoc(dir), document);
91                 }
92                 actionForward.setPath("/Searchengine.do");
93             } catch (Exception JavaDoc 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