added ?do=search
[mir.git] / source / mircoders / servlet / ServletModuleOpenIndy.java
index 6c375cf..6c7895c 100755 (executable)
@@ -51,6 +51,13 @@ import org.apache.fop.apps.Driver;
 import org.apache.fop.apps.Version;
 import org.apache.fop.apps.XSLTInputHandler;
 
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.search.*;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.analysis.standard.*;
+import org.apache.lucene.queryParser.*;
+
 import org.apache.log.*;
 
 import mir.servlet.*;
@@ -75,7 +82,7 @@ import mircoders.localizer.*;
  *    open-postings to the newswire
  *
  * @author mir-coders group
- * @version $Id: ServletModuleOpenIndy.java,v 1.42 2002/11/04 04:35:22 mh Exp $
+ * @version $Id: ServletModuleOpenIndy.java,v 1.43 2002/11/21 23:51:07 john Exp $
  *
  */
 
@@ -83,9 +90,10 @@ public class ServletModuleOpenIndy extends ServletModule
 {
 
   private String        commentFormTemplate, commentFormDoneTemplate,
-                        commentFormDupeTemplate;
+    commentFormDupeTemplate;
   private String        postingFormTemplate, postingFormDoneTemplate,
-                        postingFormDupeTemplate;
+    postingFormDupeTemplate;
+  private String        searchResultsTemplate;
   private ModuleContent contentModule;
   private ModuleComment commentModule;
   private ModuleImages  imageModule;
@@ -105,6 +113,7 @@ public class ServletModuleOpenIndy extends ServletModule
       postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate");
       postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");
       postingFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDupeTemplate");
+      searchResultsTemplate = MirConfig.getProp("ServletModule.OpenIndy.SearchResultsTemplate");
       directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();
       passwdProtection = MirConfig.getProp("PasswdProtection").toLowerCase();
       mainModule = new ModuleComment(DatabaseComment.getInstance());
@@ -115,7 +124,7 @@ public class ServletModuleOpenIndy extends ServletModule
 
     }
     catch (StorageObjectException e) {
-        theLog.printError("servletmoduleopenindy could not be initialized");
+      theLog.printError("servletmoduleopenindy could not be initialized");
     }
   }
 
@@ -130,27 +139,27 @@ public class ServletModuleOpenIndy extends ServletModule
     String language = req.getParameter("language");
 
     if (aid!=null && !aid.equals(""))
-    {
-      SimpleHash mergeData = new SimpleHash();
+      {
+       SimpleHash mergeData = new SimpleHash();
+
+       // onetimepasswd
+       if(passwdProtection.equals("yes")){
+         String passwd = this.createOneTimePasswd();
+         System.out.println(passwd);
+         HttpSession session = req.getSession(false);
+         session.setAttribute("passwd",passwd);
+         mergeData.put("passwd", passwd);
+       }
 
-      // onetimepasswd
-      if(passwdProtection.equals("yes")){
-        String passwd = this.createOneTimePasswd();
-        System.out.println(passwd);
-        HttpSession session = req.getSession(false);
-        session.setAttribute("passwd",passwd);
-        mergeData.put("passwd", passwd);
-      }
+       if (language!=null) {
+         HttpSession session = req.getSession(false);
+         session.setAttribute("Locale", new Locale(language, ""));
+         session.setAttribute("passwd",language);
+       }
 
-      if (language!=null) {
-        HttpSession session = req.getSession(false);
-        session.setAttribute("Locale", new Locale(language, ""));
-        session.setAttribute("passwd",language);
+       mergeData.put("aid", aid);
+       deliver(req, res, mergeData, commentFormTemplate);
       }
-
-      mergeData.put("aid", aid);
-      deliver(req, res, mergeData, commentFormTemplate);
-    }
     else throw new ServletModuleException("aid not set!");
   }
 
@@ -160,68 +169,68 @@ public class ServletModuleOpenIndy extends ServletModule
    */
 
   public void inscomment(HttpServletRequest req, HttpServletResponse res)
-  throws ServletModuleException,ServletModuleUserException
+    throws ServletModuleException,ServletModuleUserException
   {
     String aid = req.getParameter("to_media"); // the article id the comment will belong to
     if (aid!=null && !aid.equals(""))
-    {
-      // ok, collecting data from form
-      try {
-        HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
+      {
+       // ok, collecting data from form
+       try {
+         HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
 
-        //no html in comments(for now)
-        for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
+         //no html in comments(for now)
+         for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
             String k=(String)i.next();
             String v=(String)withValues.get(k);
 
             withValues.put(k,StringUtil.removeHTMLTags(v));
-        }
-        withValues.put("is_published","1");
-
-        //checking the onetimepasswd
-        if(passwdProtection.equals("yes")){
-          HttpSession session = req.getSession(false);
-          String sessionPasswd = (String)session.getAttribute("passwd");
-          if ( sessionPasswd == null){
-            throw new ServletModuleUserException("Lost password");
-          }
-          String passwd = req.getParameter("passwd");
-          if ( passwd == null || (!sessionPasswd.equals(passwd))) {
-            throw new ServletModuleUserException("Missing password");
-          }
-          session.invalidate();
-        }
-
-        // inserting into database
-        String id = mainModule.add(withValues);
-        theLog.printDebugInfo("id: "+id);
-        //insert was not successfull
-        if(id==null){
-          deliver(req, res, new SimpleHash(), commentFormDupeTemplate);
-        } else {
-          DatabaseContent.getInstance().setUnproduced("id="+aid);
-
-          try {
-            EntityComment comment = (EntityComment) DatabaseComment.getInstance().selectById(id);
-            MirGlobal.localizer().openPostings().afterCommentPosting(comment);
-          }
-          catch (Throwable t) {
-            throw new ServletModuleException(t.getMessage());
-          }
+         }
+         withValues.put("is_published","1");
+
+         //checking the onetimepasswd
+         if(passwdProtection.equals("yes")){
+           HttpSession session = req.getSession(false);
+           String sessionPasswd = (String)session.getAttribute("passwd");
+           if ( sessionPasswd == null){
+             throw new ServletModuleUserException("Lost password");
+           }
+           String passwd = req.getParameter("passwd");
+           if ( passwd == null || (!sessionPasswd.equals(passwd))) {
+             throw new ServletModuleUserException("Missing password");
+           }
+           session.invalidate();
+         }
+
+         // inserting into database
+         String id = mainModule.add(withValues);
+         theLog.printDebugInfo("id: "+id);
+         //insert was not successfull
+         if(id==null){
+           deliver(req, res, new SimpleHash(), commentFormDupeTemplate);
+         } else {
+           DatabaseContent.getInstance().setUnproduced("id="+aid);
+
+           try {
+             EntityComment comment = (EntityComment) DatabaseComment.getInstance().selectById(id);
+             MirGlobal.localizer().openPostings().afterCommentPosting(comment);
+           }
+           catch (Throwable t) {
+             throw new ServletModuleException(t.getMessage());
+           }
           
           
           
-        }
+         }
 
-        // redirecting to url
-        // should implement back to article
-        SimpleHash mergeData = new SimpleHash();
-        deliver(req, res, mergeData, commentFormDoneTemplate);
-      }
-      catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
-      catch (ModuleException e) { throw new ServletModuleException(e.toString());}
+         // redirecting to url
+         // should implement back to article
+         SimpleHash mergeData = new SimpleHash();
+         deliver(req, res, mergeData, commentFormDoneTemplate);
+       }
+       catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
+       catch (ModuleException e) { throw new ServletModuleException(e.toString());}
 
-    }
+      }
     else throw new ServletModuleException("aid not set!");
 
   }
@@ -324,7 +333,7 @@ public class ServletModuleOpenIndy extends ServletModule
       if ((((String)withValues.get("title")).length() == 0) ||
           (((String)withValues.get("description")).length() == 0) ||
           (((String)withValues.get("content_data")).length() == 0))
-            throw new ServletModuleUserException("Missing field");
+       throw new ServletModuleUserException("Missing field");
 
       // call the routines that escape html
 
@@ -354,9 +363,9 @@ public class ServletModuleOpenIndy extends ServletModule
       withValues.put("to_publisher","1");
 
       // owner is openposting user
-//      ML: this is not multi-language friendly and this can be done in a template
-//      if (withValues.get("creator").toString().equals(""))
-//        withValues.put("creator","Anonym");
+      //      ML: this is not multi-language friendly and this can be done in a template
+      //      if (withValues.get("creator").toString().equals(""))
+      //        withValues.put("creator","Anonym");
 
       // inserting  content into database
       String cid = contentModule.add(withValues);
@@ -393,7 +402,7 @@ public class ServletModuleOpenIndy extends ServletModule
 
       try {
         MirGlobal.localizer().openPostings().afterContentPosting(
-            (EntityContent)contentModule.getById(cid));
+                                                                (EntityContent)contentModule.getById(cid));
       }
       catch (Throwable t) {
         throw new ServletModuleException(t.getMessage());
@@ -408,6 +417,78 @@ public class ServletModuleOpenIndy extends ServletModule
   }
 
   /*
+   * Method for querying a lucene index
+   */
+  public void search(HttpServletRequest req, HttpServletResponse res)
+    throws ServletModuleException, ServletModuleUserException {
+    String QUERYSTRING_REQUEST_PARAM = "query";
+    String queryString = req.getParameter(QUERYSTRING_REQUEST_PARAM);
+    SimpleHash mergeData = new SimpleHash();
+    //String indexPath=MirConfig.getProp("IndexPath");
+    String indexPath="/tmp/index";
+    try{
+      Searcher searcher = null;
+      try {
+       searcher = new IndexSearcher(indexPath);
+      } 
+      catch(IOException e) {
+       theLog.printDebugInfo("Can't open indexPath: " + indexPath);
+       throw new ServletModuleUserException("Problem with Search Index!");
+    }
+      
+      // parse the query String.
+      Query query = null;
+      try {
+       query = QueryParser.parse(queryString, "content_and_description", new StandardAnalyzer());
+      } 
+      catch(Exception e) {
+       searcher.close();
+       theLog.printDebugInfo("Query don't parse: " + queryString);
+       throw new ServletModuleUserException("Problem with Query String!");
+      }
+      
+      Hits hits = null;
+      try {
+       hits = searcher.search(query);
+      } catch(IOException e) {
+      searcher.close();
+      theLog.printDebugInfo("Can't get hits: " + e.toString());
+      throw new ServletModuleUserException("Problem getting hits!");
+      }
+      
+      // iterate over the results
+      // the results are an array of document
+      try {
+       int start = 0;
+       int end = hits.length();
+       mergeData.put("numberOfHits", (new Integer(end)).toString());
+       SimpleList theHits = new SimpleList();
+       for(int i = start; i < end; i++) {
+           SimpleHash h = new SimpleHash();
+           h.put("id",hits.doc(i).get("id"));
+           h.put("where",hits.doc(i).get("where"));
+            h.put("creator",hits.doc(i).get("creator"));
+           h.put("title",hits.doc(i).get("title"));
+           h.put("webdb_create_formatted",hits.doc(i).get("webdb_create_formatted"));
+           theHits.add(h);
+       }
+       mergeData.put("hits",theHits);
+      }
+      catch (Exception e) {
+       searcher.close();
+       theLog.printDebugInfo("Can't iterate over hits: " + e.toString());
+       throw new ServletModuleUserException("Problem getting hits!");
+      }
+      searcher.close();
+    }
+    catch (IOException e){
+      theLog.printDebugInfo("Can't close searcher: " + e.toString());
+      throw new ServletModuleUserException("Problem closing searcher!");
+    }
+    deliver(req,res,mergeData,searchResultsTemplate);
+  }
+    
+  /*
    * Method for dynamically generating a pdf from a fo file
    */
   public void getpdf(HttpServletRequest req, HttpServletResponse res)
@@ -438,9 +519,9 @@ public class ServletModuleOpenIndy extends ServletModule
             (EntityContent)contentModule.getById(idParam);
           String publishPath = contentEnt.getValue("publish_path");
           String foFile = producerStorageRoot + producerDocRoot + "/" 
-                          + publishPath + "/" + idParam + ".fo";
+           + publishPath + "/" + idParam + ".fo";
           XSLTInputHandler input = new XSLTInputHandler(new File(foFile), 
-                                                       new File(xslSheet));
+                                                       new File(xslSheet));
           
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           res.setContentType("application/pdf");
@@ -472,9 +553,9 @@ public class ServletModuleOpenIndy extends ServletModule
     theLog.printDebugInfo("Wrong file type uploaded!: " + fileName+" "
                           +contentType);
     throw new ServletModuleUserException("The file you uploaded is of the "
-        +"following mime-type: "+contentType
-        +", we do not support this mime-type. "
-        +"Error One or more files of unrecognized type. Sorry");
+                                        +"following mime-type: "+contentType
+                                        +", we do not support this mime-type. "
+                                        +"Error One or more files of unrecognized type. Sorry");
   }
 
   protected String createOneTimePasswd(){
@@ -488,24 +569,24 @@ public class ServletModuleOpenIndy extends ServletModule
   }
 
 
-    /* this is an overwritten method of ServletModule in order
-    to use different bundles for open and admin */
-    public void deliver(HttpServletRequest req, HttpServletResponse res,
-                                                                                       TemplateModelRoot rtm, TemplateModelRoot popups,
-                                                                                       String templateFilename)
-       throws ServletModuleException {
-               if (rtm == null) rtm = new SimpleHash();
-               try {
-                       PrintWriter out = res.getWriter();
-                       HTMLTemplateProcessor.process(res, templateFilename, rtm, popups, out,
-                                                                                                                                               getLocale(req), "bundles.open");
-                       out.close();
-               }       catch (HTMLParseException e) {
-                       throw new ServletModuleException(e.toString());
-               } catch (IOException e) {
-                       throw new ServletModuleException(e.toString());
-               }
-       }
+  /* this is an overwritten method of ServletModule in order
+     to use different bundles for open and admin */
+  public void deliver(HttpServletRequest req, HttpServletResponse res,
+                     TemplateModelRoot rtm, TemplateModelRoot popups,
+                     String templateFilename)
+    throws ServletModuleException {
+    if (rtm == null) rtm = new SimpleHash();
+    try {
+      PrintWriter out = res.getWriter();
+      HTMLTemplateProcessor.process(res, templateFilename, rtm, popups, out,
+                                   getLocale(req), "bundles.open");
+      out.close();
+    }  catch (HTMLParseException e) {
+      throw new ServletModuleException(e.toString());
+    } catch (IOException e) {
+      throw new ServletModuleException(e.toString());
+    }
+  }
 }