introduced some customization for the content list in admin
[mir.git] / source / mircoders / servlet / ServletModuleOpenIndy.java
index 6c7895c..d6e9e58 100755 (executable)
@@ -66,6 +66,7 @@ import mir.misc.*;
 import mir.entity.*;
 import mir.storage.*;
 import mir.media.*;
+import mir.log.*;
 
 import mircoders.entity.*;
 import mircoders.storage.*;
@@ -74,6 +75,7 @@ import mircoders.producer.*;
 import mircoders.media.MediaRequest;
 import mircoders.global.*;
 import mircoders.localizer.*;
+import mircoders.search.*;
 
 /*
  *  ServletModuleOpenIndy -
@@ -82,7 +84,7 @@ import mircoders.localizer.*;
  *    open-postings to the newswire
  *
  * @author mir-coders group
- * @version $Id: ServletModuleOpenIndy.java,v 1.43 2002/11/21 23:51:07 john Exp $
+ * @version $Id: ServletModuleOpenIndy.java,v 1.48 2002/11/30 19:45:57 zapata Exp $
  *
  */
 
@@ -106,7 +108,8 @@ public class ServletModuleOpenIndy extends ServletModule
 
   private ServletModuleOpenIndy() {
     try {
-      theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.OpenIndy.Logfile"));
+      logger = new LoggerWrapper("ServletModule.OpenIndy");
+
       commentFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentTemplate");
       commentFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDoneTemplate");
       commentFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDupeTemplate");
@@ -124,7 +127,7 @@ public class ServletModuleOpenIndy extends ServletModule
 
     }
     catch (StorageObjectException e) {
-      theLog.printError("servletmoduleopenindy could not be initialized");
+      logger.error("servletmoduleopenindy could not be initialized: " + e.getMessage());
     }
   }
 
@@ -203,7 +206,7 @@ public class ServletModuleOpenIndy extends ServletModule
 
          // inserting into database
          String id = mainModule.add(withValues);
-         theLog.printDebugInfo("id: "+id);
+         logger.debug("id: "+id);
          //insert was not successfull
          if(id==null){
            deliver(req, res, new SimpleHash(), commentFormDupeTemplate);
@@ -278,9 +281,9 @@ public class ServletModuleOpenIndy extends ServletModule
 
       extraInfo.put("topics", themenModule.getTopicsList());
 
-    } catch (Exception e) {
-      theLog.printError("languagePopUpData or getTopicslist failed "
-                        +e.toString());
+    } 
+    catch (Exception e) {
+      logger.error("languagePopUpData or getTopicslist failed "+e.toString());
       throw new ServletModuleException("OpenIndy -- failed getting language or topics: "+e.toString());
     }
 
@@ -369,7 +372,7 @@ public class ServletModuleOpenIndy extends ServletModule
 
       // inserting  content into database
       String cid = contentModule.add(withValues);
-      theLog.printDebugInfo("id: "+cid);
+      logger.debug("id: "+cid);
       //insert was not successfull
       if(cid==null){
         //How do we know that it was not succesful cause of a
@@ -387,8 +390,9 @@ public class ServletModuleOpenIndy extends ServletModule
         try{
           DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
           setTopic = true;
-        } catch (Exception e) {
-          theLog.printError("setting content_x_topic failed");
+        } 
+       catch (Exception e) {
+          logger.error("setting content_x_topic failed");
           contentModule.deleteById(cid);
           throw new ServletModuleException("smod - openindy :: insposting: setting content_x_topic failed: "+e.toString());
         } //end try
@@ -421,73 +425,265 @@ public class ServletModuleOpenIndy extends ServletModule
    */
   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!");
+    try {
+      int increment=10;
+
+      HttpSession session = req.getSession(false);
+
+      String queryString="";
+    
+      SimpleHash mergeData = new SimpleHash();
+    
+      KeywordSearchTerm dateTerm = new KeywordSearchTerm("date_formatted","search_date","webdb_create_formatted","webdb_create_formatted","webdb_create_formatted");
+      UnIndexedSearchTerm whereTerm = new UnIndexedSearchTerm("","","","where","where");
+      TextSearchTerm creatorTerm = new TextSearchTerm("creator","search_creator","creator","creator","creator");
+      TextSearchTerm titleTerm = new TextSearchTerm("title","search_content","title","title","title");
+      TextSearchTerm descriptionTerm =  new TextSearchTerm("description","search_content","description","description","description");
+      ContentSearchTerm contentTerm = new ContentSearchTerm("content_data","search_content","content","","");
+      TopicSearchTerm topicTerm = new TopicSearchTerm();
+      ImagesSearchTerm imagesTerm = new ImagesSearchTerm();
+      AudioSearchTerm audioTerm = new AudioSearchTerm();
+      VideoSearchTerm videoTerm = new VideoSearchTerm();
+    
+      //make the query available to subsequent iterations
+
+      for (Enumeration theParams = req.getParameterNames(); theParams.hasMoreElements() ;) {
+       String pName=(String)theParams.nextElement();
+       if (pName.startsWith("search_")){
+         mergeData.put(pName,new SimpleScalar(req.getParameter(pName)));
+       }
       }
-      
-      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!");
+    
+      try{
+       mergeData.put("topics", themenModule.getTopicsAsSimpleList());
+      }
+      catch(ModuleException e) {
+       logger.debug("Can't get topics: " + e.toString());
       }
+    
+      String searchSubmitValue = req.getParameter("search_submit");
+    
+      if (searchSubmitValue != null && searchSubmitValue.equals("Back")){
+       int totalHits = ((Integer) session.getAttribute("numberOfHits")).intValue();
+       int newPosition=((Integer)session.getAttribute("positionInResults")).intValue()-increment;
+       if (newPosition < 0 || newPosition >= totalHits){
+         throw new ServletModuleUserException("newPosition: index out bounds, value was:"+(new Integer(newPosition)).toString());
+       }
+       session.setAttribute("positionInResults",new Integer(newPosition));
       
-      // iterate over the results
-      // the results are an array of document
+      }
+      else {
+       if (searchSubmitValue != null && searchSubmitValue.equals("Forward")){
+         int totalHits = ((Integer) session.getAttribute("numberOfHits")).intValue();
+         int newPosition=((Integer)session.getAttribute("positionInResults")).intValue()+increment;
+         if (newPosition < 0 || newPosition >= totalHits){
+           throw new ServletModuleUserException("newPosition: index out bounds, value was:"+(new Integer(newPosition)).toString());
+         }
+         session.setAttribute("positionInResults",new Integer(newPosition));
+       
+       }
+       else {
+         String indexPath=MirConfig.getProp("IndexPath");
+               
+       
+         String creatorFragment = creatorTerm.makeTerm(req);
+         if (creatorFragment != null){
+           queryString = queryString + " +" + creatorFragment;
+         }
+       
+         // search title, description, and content for something
+         // the contentTerm uses param "search_boolean" to combine its terms
+         String contentFragment = contentTerm.makeTerm(req);
+         if (contentFragment != null){
+           logger.debug("contentFragment: " + contentFragment);
+           queryString = queryString + " +" + contentFragment;
+         }
+       
+         String topicFragment = topicTerm.makeTerm(req);
+         if (topicFragment != null){
+           queryString = queryString + " +" + topicFragment;
+         }
+       
+         String imagesFragment = imagesTerm.makeTerm(req);
+         if (imagesFragment != null){
+           queryString = queryString + " +" + imagesFragment;
+         }
+       
+         String audioFragment = audioTerm.makeTerm(req);
+         if (audioFragment != null){
+           queryString = queryString + " +" + audioFragment;
+         }
+       
+         String videoFragment = videoTerm.makeTerm(req);
+         if (videoFragment != null){
+           queryString = queryString + " +" + videoFragment;
+         }
+       
+         if (queryString == null || queryString == ""){
+           queryString = "";
+         }
+         else{
+           try{
+             Searcher searcher = null;
+             try {
+               searcher = new IndexSearcher(indexPath);
+             } 
+             catch(IOException e) {
+               logger.debug("Can't open indexPath: " + indexPath);
+               throw new ServletModuleUserException("Problem with Search Index! : "+ e.toString());
+             }
+           
+             Query query = null;
+             try {
+               query = QueryParser.parse(queryString, "content", new StandardAnalyzer());
+             } 
+             catch(Exception e) {
+               searcher.close();
+               logger.debug("Query don't parse: " + queryString);
+               throw new ServletModuleUserException("Problem with Query String! (was '"+queryString+"')");
+             }
+           
+             Hits hits = null;
+             try {
+               hits = searcher.search(query);
+             } 
+             catch(IOException e) {
+               searcher.close();
+               logger.debug("Can't get hits: " + e.toString());
+               throw new ServletModuleUserException("Problem getting hits!");
+             }
+         
+             int start = 0;
+             int end = hits.length();
+         
+             String sortBy=req.getParameter("search_sort");
+             if (sortBy == null || sortBy.equals("")){
+               throw new ServletModuleUserException("Please let me sort by something!(missing search_sort)");
+             }
+         
+             // here is where the documents will go for storage across sessions
+             ArrayList theDocumentsSorted = new ArrayList();
+         
+             if (sortBy.equals("score")){
+               for(int i = start; i < end; i++) {
+                 theDocumentsSorted.add(hits.doc(i));
+               }
+             }
+             else{
+               // then we'll sort by date!
+               HashMap dateToPosition = new HashMap(end,1.0F); //we know how big it will be
+               for(int i = start; i < end; i++) {
+                 String creationDate=(hits.doc(i)).get("creationDate");
+                 // do a little dance in case two contents created at the same second!
+                 if (dateToPosition.containsKey(creationDate)){
+                   ((ArrayList) (dateToPosition.get(creationDate))).add(new Integer(i));
+                 }
+                 else{ 
+                   ArrayList thePositions = new ArrayList();
+                   thePositions.add(new Integer(i));
+                   dateToPosition.put(creationDate,thePositions);
+                 }
+               }
+               Set keys = dateToPosition.keySet();
+               ArrayList keyList= new ArrayList(keys);
+               Collections.sort(keyList);
+               if (sortBy.equals("date_desc")){
+                 Collections.reverse(keyList);     
+               }
+               else{
+                 if (!sortBy.equals("date_asc")){
+                   throw new ServletModuleUserException("don't know how to sort by: "+ sortBy);
+                 }
+               }
+               ListIterator keyTraverser = keyList.listIterator();
+               while (keyTraverser.hasNext()){
+                 ArrayList positions = (ArrayList)dateToPosition.get((keyTraverser.next()));
+                 ListIterator positionsTraverser=positions.listIterator();
+                 while (positionsTraverser.hasNext()){
+                   theDocumentsSorted.add(hits.doc(((Integer)(positionsTraverser.next())).intValue()));
+                 }
+               }
+             }
+             
+             try{
+               searcher.close();
+             }
+             catch (IOException e){
+               logger.debug("Can't close searcher: " + e.toString());
+               throw new ServletModuleUserException("Problem closing searcher(normal)!");
+             }
+                     
+             
+             session.removeAttribute("numberOfHits");
+             session.removeAttribute("theDocumentsSorted");
+             session.removeAttribute("positionInResults");
+         
+             session.setAttribute("numberOfHits",new Integer(end));
+             session.setAttribute("theDocumentsSorted",theDocumentsSorted);
+             session.setAttribute("positionInResults",new Integer(0));
+         
+           }
+           catch (IOException e){
+             logger.debug("Can't close searcher: " + e.toString());
+             throw new ServletModuleUserException("Problem closing searcher!");
+           }
+         }
+       }
+      }
+    
       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++) {
+       ArrayList theDocs = (ArrayList)session.getAttribute("theDocumentsSorted"); 
+       if (theDocs != null){
+       
+         mergeData.put("numberOfHits", ((Integer)session.getAttribute("numberOfHits")).toString());
+         SimpleList theHits = new SimpleList();
+         int pIR=((Integer)session.getAttribute("positionInResults")).intValue();
+         int terminus;
+         int numHits=((Integer)session.getAttribute("numberOfHits")).intValue();
+       
+         if (!(pIR+increment>=numHits)){
+           mergeData.put("hasNext","y");
+         }
+         if (pIR-increment>=0){
+           mergeData.put("hasPrevious","y");
+         }
+       
+         if ((pIR+increment)>numHits){
+           terminus=numHits;
+         }
+         else {
+           terminus=pIR+increment;
+         }
+         for(int i = pIR; i < terminus; 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"));
+           Document theHit = (Document)theDocs.get(i);
+           whereTerm.returnMeta(h,theHit);
+           creatorTerm.returnMeta(h,theHit);
+           titleTerm.returnMeta(h,theHit);
+           descriptionTerm.returnMeta(h,theHit);
+           dateTerm.returnMeta(h,theHit);
+           imagesTerm.returnMeta(h,theHit);
+           audioTerm.returnMeta(h,theHit);
+           videoTerm.returnMeta(h,theHit);  
            theHits.add(h);
+         }
+         mergeData.put("hits",theHits);
        }
-       mergeData.put("hits",theHits);
       }
       catch (Exception e) {
-       searcher.close();
-       theLog.printDebugInfo("Can't iterate over hits: " + e.toString());
+       logger.debug("Can't iterate over hits: " + e.toString());
        throw new ServletModuleUserException("Problem getting hits!");
       }
-      searcher.close();
+    
+      mergeData.put("queryString",queryString);
+      deliver(req,res,mergeData,searchResultsTemplate);
     }
-    catch (IOException e){
-      theLog.printDebugInfo("Can't close searcher: " + e.toString());
-      throw new ServletModuleUserException("Problem closing searcher!");
+    catch (NullPointerException n){
+      n.printStackTrace();
+      throw new ServletModuleUserException("Null Pointer"+n.toString());
     }
-    deliver(req,res,mergeData,searchResultsTemplate);
   }
-    
+
   /*
    * Method for dynamically generating a pdf from a fo file
    */
@@ -502,7 +698,7 @@ public class ServletModuleOpenIndy extends ServletModule
     //pregenerating PDF's
     if (generateFO.equals("yes") && generatePDF.equals("no")){
       //fop complains unless you do the logging this way
-      Logger log = null;
+      org.apache.log.Logger log = null;
       Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
       log = hierarchy.getLoggerFor("fop");
       log.setPriority(Priority.WARN);
@@ -550,7 +746,7 @@ public class ServletModuleOpenIndy extends ServletModule
   private void _throwBadContentType (String fileName, String contentType)
     throws ServletModuleUserException {
 
-    theLog.printDebugInfo("Wrong file type uploaded!: " + fileName+" "
+    logger.error("Wrong file type uploaded!: " + fileName+" "
                           +contentType);
     throw new ServletModuleUserException("The file you uploaded is of the "
                                         +"following mime-type: "+contentType