producer topic debug
[mir.git] / source / mircoders / producer / ProducerContent.java
index c7cf7bc..0e3953f 100755 (executable)
@@ -2,13 +2,11 @@ package mircoders.producer;
 
 import java.io.*;
 import java.lang.*;
-import java.lang.reflect.*;
 import java.util.*;
 
 import freemarker.template.*;
 
 import mir.misc.*;
-import mir.media.*;
 import mir.storage.*;
 import mir.module.*;
 import mir.entity.*;
@@ -19,18 +17,27 @@ import mircoders.storage.*;
 
 public class ProducerContent extends Producer {
 
+  private String      contentTemplate = MirConfig.getProp("Producer.Content.Template");
+
   public static void main(String argv[]){
     /**
      * Why are we reloading the config here?
      * Can someone please explain this?
      * Hope I didn't break anything
      * -mh. <heckmann@hbe.ca>
+     *
+     * This is the entry point for standalone production. The code
+     * is then running in a new virtual machine, e.g. when called
+     * from cron-script. In that case the config has to be initliased.
+     * Don't know if is ok that way //rk
+     *
      */
     //Configuration.initConfig("config");
     System.out.println(MirConfig.getProp("Producer.DocRoot"));
 
     try {
-      new ProducerContent().handle(new PrintWriter(System.out), null, false,false);
+      new ProducerContent().handle(new PrintWriter(System.out), null,
+                                    false,false);
     } catch(Exception e) {
       System.err.println(e.toString());
     }
@@ -38,50 +45,30 @@ public class ProducerContent extends Producer {
 
 
 
-  public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)
+  public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
+                    boolean sync)
     throws StorageObjectException, ModuleException {
 
     handle(htmlout,user,force,sync,null);
   }
 
+  public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
+                     boolean sync, String id) throws StorageObjectException, ModuleException
+  {
 
+    long                startTime = System.currentTimeMillis();
+    int                 pageCount=0;
 
-
-
-  public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
-    throws StorageObjectException, ModuleException {
-
-    String contentTemplate = MirConfig.getProp("Producer.Content.Template");
-    int contentBatchsize = Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
-    String extLinkName = MirConfig.getProp("Producer.ExtLinkName");
-    String intLinkName = MirConfig.getProp("Producer.IntLinkName");
-    String mailLinkName = MirConfig.getProp("Producer.MailLinkName");
-    String imageRoot = MirConfig.getProp("Producer.ImageRoot");
-
-    long                sessionConnectTime = 0;
-    long                startTime = (new java.util.Date()).getTime();
     String              whereClause = " ";
     String              orderBy = " ";
     String              htmlFileName = null;
-    String              currentMediaId;
     EntityContent       currentContent;
     EntityList          batchEntityList;
-    HashMap             currentContentValues;
-    SimpleHash          imageHash = new SimpleHash();
     EntityUsers         userEntity=null;
-    Entity              mediaType;
-    Entity              upMedia;
-    SimpleHash          upMediaSimpleHash;
-    Class               mediaHandlerClass;
-    Class               mediaStorageClass;
-    String              mediaStorageName;
-    String              mediaHandlerName=null;
-    MirMedia            mediaHandler=null;
-    Database            mediaStorage=null;
-
-
 
     // production of the content-pages
+
+    /** @todo this should be moved to ModuleContent */
     orderBy="date desc, webdb_lastchange desc";
     if(force==true){
       whereClause="is_published='1'";
@@ -89,130 +76,58 @@ public class ProducerContent extends Producer {
       if(id !=null){
         whereClause += " AND id="+id;
       }
-      batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
+      batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
+                                                -1, userEntity);
     } else {
       whereClause="is_produced='0' AND is_published='1'";
       //if true produces a single contentitem
       if(id !=null){
         whereClause += " AND id="+id;
       }
-      batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
+      batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
+                                                -1, userEntity);
     }
 
-    while (batchEntityList != null) {
+    if (batchEntityList!=null) {
       for(int i=0;i<batchEntityList.size();i++) {
         currentContent = (EntityContent)batchEntityList.elementAt(i);
+
         try {
-            currentContentValues = currentContent.getValues();
-
-            //because of postgres 7.1.* not needed anymore
-            //currentContentValues.put("content_data",currentContent.getContentData());
-            String date = (String)currentContentValues.get("date");
-            String year = date.substring(0,4);
-            String month = date.substring(4,6);
-
-            htmlFileName =  producerDocRoot
-              + "/" + year + "/" + month + "/" +  currentContentValues.get("id") + ".shtml";
-
-            currentContentValues.put("content_data",StringUtil.deleteForbiddenTags((String)currentContentValues.get("content_data")));
-            currentContentValues.put("description",StringUtil.deleteForbiddenTags((String)currentContentValues.get("description")));
-
-
-            //create http-links and email-links
-            if (currentContentValues.get("is_html").equals("0")) {
-              String temp = (String)currentContentValues.get("content_data");
-              if(temp!=null && temp.length()>0){
-                temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
-                temp = StringUtil.decodeHTMLinTags(temp);
-                currentContentValues.put("content_data",temp);
-              }
-              temp = (String)currentContentValues.get("description");
-              if(temp!=null && temp.length()>0){
-                temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
-                temp = StringUtil.decodeHTMLinTags(temp);
-                currentContentValues.put("description",temp);
-              }
-            } else {
-              String temp = (String)currentContentValues.get("content_data");
-              if(temp!=null && temp.length()>0){
-                temp = StringUtil.decodeHTMLinTags(temp);
-                currentContentValues.put("content_data",temp);
-              }
-              temp = (String)currentContentValues.get("description");
-              if(temp!=null && temp.length()>0){
-                temp = StringUtil.decodeHTMLinTags(temp);
-                currentContentValues.put("description",temp);
-              }
-            }
-
-            //create the freemarker-model
-            SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(currentContentValues);
-
-            // get the uploaded media
-            EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
-            if (currentMediaList!=null && currentMediaList.getCount()>=1) {
-              SimpleList mediaList = new SimpleList();
-              for (int n=0; n < currentMediaList.size();n++) {
-                upMedia = currentMediaList.elementAt(n);
-                upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
-                mediaType = ((EntityMedia)upMedia).getMediaType();
-                //must be a non-existant to_media_type entry..
-                if (mediaType != null) {
-                  mediaHandlerName = mediaType.getValue("classname");
-                  mediaStorageName = mediaType.getValue("tablename");
-                  mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
-                  mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
-                  mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
-                  Method m = mediaStorageClass.getMethod("getInstance", null);
-                  mediaStorage = (Database)m.invoke(null, null);
-                  //we most likely need further info
-                  upMedia = mediaStorage.selectById(upMedia.getId());
-                  upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
-                  upMediaSimpleHash.put("type",mediaType.getValue("classname"));
-                  mediaList.add(upMediaSimpleHash);
-                } //end if media_type != null
-              } //end for
-              mergeData.put("to_media", mediaList);
-            } //end if currentMediaList != null
-
-            // get the comments for the article
-            // and html-ize them
-            SimpleList commentList = currentContent.getComments();
-            if(commentList.isEmpty()==false){
-              while(commentList.hasNext()){
-                SimpleHash comment = (SimpleHash)commentList.next();
-                SimpleScalar commentText = (SimpleScalar)comment.get("description");
-                comment.put("description",new SimpleScalar(StringUtil.createHTML(commentText.getAsString(),imageRoot,mailLinkName,extLinkName,intLinkName)));
-              }
-            }
-            mergeData.put("comments", commentList);
-
-            // get the topics of this article
-            mergeData.put("topics",HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(currentContent)));
-
-            //produce html
-            boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
-            sessionConnectTime = new java.util.Date().getTime() - startTime;
-            if (retVal == true && !"1".equals(currentContent.getValue("is_produced")))
-                currentContent.setProduced(true);
-        } catch(Exception e) {
-          logHTML(htmlout, "Producer.Content ERROR while producing content ID: " + currentContent.getId()+",skipping it :: "+e.toString());
-          theLog.printError("Producer.Content ERROR while producing content ID: " + currentContent.getId() +",skipping it :: "+e.toString());
+
+          SimpleHash mergeData=new SimpleHash();
+          mergeData.put("content", currentContent);
+          String date = currentContent.getValue("date");
+          String year = date.substring(0,4);
+          String month = date.substring(4,6);
+          htmlFileName =  producerDocRoot + "/" + year + "/" + month + "/" +
+                          currentContent.getValue("id") + ".shtml";
+
+          //produce html
+          boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
+          if ( retVal ) currentContent.setProduced(true);
+
         }
-          
-          
+        catch(Exception e)
+        {
+          String errorText = "Producer.Content ERROR while producing content ID:"
+                    + currentContent.getId()+", skipping it :: "+e.toString();
+          logHTML(htmlout, errorText);
+          theLog.printError(errorText);
+        }
+        pageCount++;
       }//for
-
-      if (batchEntityList.hasNextBatch()){
-        batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);
-      } else {
-        batchEntityList=null;
-      }
     }
-
     // timing and message to browser
-    sessionConnectTime = new java.util.Date().getTime() - startTime;
-    logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");
+    long overall = System.currentTimeMillis() - startTime;
+    long pagesPerMinute=0; float perMinute = overall/60000;
+    if (perMinute >0) pagesPerMinute = (long) (pageCount / perMinute);
+
+    logHTML(htmlout, "Producer.Content finished producing: " +
+            overall + " ms for "+ pageCount+" Pages = " +pagesPerMinute + " pages/min");
+    logHTML(htmlout, "Back to <a href=\""+actionRoot+"\">Admin-Startage</a>");
+
+    /** @todo why no syncing here? */
+
   }
 
 }