language fix
[mir.git] / source / mircoders / producer / ProducerContent.java
index e9fe8eb..a4e14a5 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.*;
@@ -16,199 +14,156 @@ import mir.entity.*;
 import mircoders.entity.*;
 import mircoders.storage.*;
 
+//for pdf production
+import org.apache.fop.apps.* ;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+import org.apache.log.*;
 
 public class ProducerContent extends Producer {
 
-  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>
-     */
-    //Configuration.initConfig("config");
-    System.out.println(MirConfig.getProp("Producer.DocRoot"));
-
-    try {
-      new ProducerContent().handle(new PrintWriter(System.out), null, false,false);
-    } catch(Exception e) {
-      System.err.println(e.toString());
-    }
-  }
-
-
-
-  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 {
-
-    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;
-    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
-    orderBy="date desc, webdb_lastchange desc";
-    if(force==true){
-      whereClause="is_published='1'";
-      // if true: produces a single content item
-      if(id !=null){
-        whereClause += " AND id="+id;
-      }
-      batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, 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);
-    }
-
-    while (batchEntityList != null) {
-      for(int i=0;i<batchEntityList.size();i++) {
-        currentContent = (EntityContent)batchEntityList.elementAt(i);
-        HashMap currentContentValues = currentContent.getValues();
-        String date = (String)currentContent.getValue("date");
-        String year = date.substring(0,4);
-        String month = date.substring(4,6);
-
-        htmlFileName =  producerDocRoot
-          + "/" + year + "/" + month + "/" +  currentContent.getValue("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();
-            //in case it's a non-existant to_media_type entry..
-            if (mediaType != null) {
-              try {
-                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());
-              } catch (Exception e) {
-                theLog.printError("ProducerStartpage:problem in reflection: "+mediaHandlerName);
-              } //end catch
-              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();
-        try{
-          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)));
+       private String contentTemplate=MirConfig.getProp("Producer.Content.Template");
+       private String contentPrintableTemplate=MirConfig.getProp("Producer.PrintableContent.Template");
+       private String html2foStyleSheetName=MirConfig.getProp("Producer.PrintableContent.html2foStyleSheetName");
+       private String generateFO=MirConfig.getProp("GenerateFO");
+       private String generatePDF=MirConfig.getProp("GeneratePDF");
+       private String producerStorageRoot=MirConfig.getProp("Producer.StorageRoot");
+       private Logger fopLog=null;
+       private String templateDir = MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir");
+       
+       public static void main(String argv[]){
+               //Configuration.initConfig("config");
+               System.out.println(MirConfig.getProp("Producer.DocRoot"));
+
+               try {
+                       new ProducerContent().handle(new PrintWriter(System.out), null,
+                                                                                                                                               false,false);
+               } catch(Exception e) {
+                       System.err.println(e.toString());
+               }
+       }
+
+       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;
+
+               String              whereClause = " ";
+               String              orderBy = " ";
+               String              htmlFileName = null;
+               String              foFileName = null;
+               String              pdfFileName = null;
+               EntityContent       currentContent;
+               EntityList          batchEntityList;
+               EntityUsers         userEntity=null;
+
+               int                 contentBatchsize =
+                                               Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
+               // 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'";
+                       // if true: produces a single content item
+                       if(id !=null){
+                               whereClause += " AND id="+id;
+                               // I think this avoids a select count(*)...
+                               contentBatchsize=-1;
+                       }
+                       batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
+                                                                                                                                                                                               contentBatchsize, userEntity);
+               } else {
+                       whereClause="is_produced='0' AND is_published='1'";
+                       //if true produces a single contentitem
+                       if(id !=null){
+                               whereClause += " AND id="+id;
+                               // this avoids a select count(*)...
+                               contentBatchsize=-1;
+                       }
+                       batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
+                                                                                                                                                                                               contentBatchsize, userEntity);
+               }
+
+               while (batchEntityList!=null) {
+                       for(int i=0;i<batchEntityList.size();i++) {
+                               currentContent = (EntityContent)batchEntityList.elementAt(i);
+
+                               try {
+
+                                       SimpleHash mergeData=new SimpleHash();
+                                       mergeData.put("content", currentContent);
+
+                                       /** @todo this should be assembled in entity */
+                                       String date = currentContent.getValue("date");
+                                       String year = date.substring(0,4);
+                                       String month = date.substring(4,6);
+                                       htmlFileName =  "/" + year + "/" + month + "/" +
+                                                                                                       currentContent.getValue("id") + ".shtml";
+                                       
+                                       //produce html
+                                       boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
+                                       if ( retVal ) currentContent.setProduced(true);
+                                       
+                                       //produce xsl:fo and pdf version(if desired)
+                                       if (generateFO.toLowerCase().equals("yes")){
+            foFileName =  "/" + year + "/" + month + "/"
+                          + currentContent.getValue("id") + ".fo";
+            boolean foRetVal = produce(contentPrintableTemplate, foFileName,
+                                        mergeData, htmlout, "UTF8");
+                                       
+            if (generatePDF.toLowerCase().equals("yes")){
+              pdfFileName =  producerStorageRoot + "/" + year
+                              + "/" + month + "/"
+                              + currentContent.getValue("id") + ".pdf";
+              Driver driver = new Driver();
+                                               
+              Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
+              fopLog = hierarchy.getLoggerFor("fop");
+              fopLog.setPriority(Priority.WARN);
+                                               
+              driver.setLogger(fopLog);
+              driver.setRenderer(Driver.RENDER_PDF);
+              File foFile=new File(producerStorageRoot + foFileName);
+              File html2foStyleSheet=new File(templateDir+"/"
+                                              +html2foStyleSheetName);
+              InputHandler inputHandler =
+                new XSLTInputHandler(foFile, html2foStyleSheet);
+              XMLReader parser = inputHandler.getParser();
+              driver.setOutputStream(new FileOutputStream(pdfFileName));
+              driver.render(parser, inputHandler.getInputSource());
             }
-          }
-        } catch(Exception e){}
-        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);
-      }//while
-
-      if (batchEntityList.hasNextBatch()){
-        batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);
-      } else {
-        batchEntityList=null;
-      }
-    }
-
-    // timing an message to browser
-    sessionConnectTime = new java.util.Date().getTime() - startTime;
-    logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");
-  }
-
+                                       }
+                               } catch(Exception e) {
+                                       String errorText = "Producer.Content <font color=red>ERROR</font> while producing content ID:"
+                                                                               + currentContent.getId()+", skipping it :: "+e.toString();
+                                       logHTML(htmlout, errorText);
+                                       theLog.printError(errorText);
+                                       e.printStackTrace();
+                               }
+                               pageCount++;
+                       }//for
+                       // if next batch get it...
+                       if (batchEntityList.hasNextBatch()){
+                               batchEntityList = contentModule.getContent(whereClause, orderBy,
+                                                                                                                                                               batchEntityList.getNextBatch(),
+                                                                                                                                                               contentBatchsize, userEntity);
+                       } else {
+                               batchEntityList=null;
+                       }
+               }
+               logHTMLFinish(htmlout, "Content", pageCount, startTime, System.currentTimeMillis());
+               /** @todo why no syncing here? */
+       }
 }