At last the EntityBatchingProducerNode is working. This will replace the old
[mir.git] / source / mircoders / producer / ProducerContent.java
1 package mircoders.producer;
2
3 import java.io.*;
4 import java.lang.*;
5 import java.util.*;
6
7 import freemarker.template.*;
8
9 import mir.misc.*;
10 import mir.storage.*;
11 import mir.module.*;
12 import mir.entity.*;
13
14 import mircoders.entity.*;
15 import mircoders.storage.*;
16
17 //for pdf production
18 import org.apache.fop.apps.* ;
19 import org.xml.sax.InputSource;
20 import org.xml.sax.XMLReader;
21 import org.apache.log.*;
22
23 public class ProducerContent extends Producer {
24
25         private String contentTemplate=MirConfig.getProp("Producer.Content.Template");
26         private String contentPrintableTemplate=MirConfig.getProp("Producer.PrintableContent.Template");
27         private String html2foStyleSheetName=MirConfig.getProp("Producer.PrintableContent.html2foStyleSheetName");
28         private String generateFO=MirConfig.getProp("GenerateFO");
29         private String generatePDF=MirConfig.getProp("GeneratePDF");
30         private String producerStorageRoot=MirConfig.getProp("Producer.StorageRoot");
31         private Logger fopLog=null;
32         private String templateDir = MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir");
33         
34         public static void main(String argv[]){
35                 /**
36                  * Why are we reloading the config here?
37                  * Can someone please explain this?
38                  * Hope I didn't break anything
39                  * -mh. <heckmann@hbe.ca>
40                  *
41                  * This is the entry point for standalone production. The code
42                  * is then running in a new virtual machine, e.g. when called
43                  * from cron-script. In that case the config has to be initliased.
44                  * Don't know if is ok that way //rk
45                  *
46                  * ok, i figured that out a few months ago.. -mh
47                  *
48                  */
49                 //Configuration.initConfig("config");
50                 System.out.println(MirConfig.getProp("Producer.DocRoot"));
51
52                 try {
53                         new ProducerContent().handle(new PrintWriter(System.out), null,
54                                                                                                                                                 false,false);
55                 } catch(Exception e) {
56                         System.err.println(e.toString());
57                 }
58         }
59
60
61
62         public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
63                                                                                 boolean sync)
64                 throws StorageObjectException, ModuleException {
65
66                 handle(htmlout,user,force,sync,null);
67         }
68
69         public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
70                                                                                  boolean sync, String id) throws StorageObjectException,
71                                                                                  ModuleException
72         {
73
74                 long                startTime = System.currentTimeMillis();
75                 int                 pageCount=0;
76
77                 String              whereClause = " ";
78                 String              orderBy = " ";
79                 String              htmlFileName = null;
80                 String              foFileName = null;
81                 String              pdfFileName = null;
82                 EntityContent       currentContent;
83                 EntityList          batchEntityList;
84                 EntityUsers         userEntity=null;
85
86                 int                 contentBatchsize =
87                                                 Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
88                 // production of the content-pages
89
90                 /** @todo this should be moved to ModuleContent */
91                 orderBy="date desc, webdb_lastchange desc";
92                 if(force==true){
93                         whereClause="is_published='1'";
94                         // if true: produces a single content item
95                         if(id !=null){
96                                 whereClause += " AND id="+id;
97                                 // I think this avoids a select count(*)...
98                                 contentBatchsize=-1;
99                         }
100                         batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
101                                                                                                                                                                                                 contentBatchsize, userEntity);
102                 } else {
103                         whereClause="is_produced='0' AND is_published='1'";
104                         //if true produces a single contentitem
105                         if(id !=null){
106                                 whereClause += " AND id="+id;
107                                 // this avoids a select count(*)...
108                                 contentBatchsize=-1;
109                         }
110                         batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
111                                                                                                                                                                                                 contentBatchsize, userEntity);
112                 }
113
114                 while (batchEntityList!=null) {
115                         for(int i=0;i<batchEntityList.size();i++) {
116                                 currentContent = (EntityContent)batchEntityList.elementAt(i);
117
118                                 try {
119
120                                         SimpleHash mergeData=new SimpleHash();
121                                         mergeData.put("content", currentContent);
122
123                                         /** @todo this should be assembled in entity */
124                                         String date = currentContent.getValue("date");
125                                         String year = date.substring(0,4);
126                                         String month = date.substring(4,6);
127                                         htmlFileName =  producerDocRoot + "/" + year + "/" + month + "/" +
128                                                                                                         currentContent.getValue("id") + ".shtml";
129                                         
130                                         //produce html
131                                         boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
132                                         if ( retVal ) currentContent.setProduced(true);
133                                         
134                                         //produce xsl:fo and pdf version(if desired)
135                                         if (generateFO.toLowerCase().equals("yes")){
136                                         foFileName =  producerDocRoot + "/" + year + "/" + month + "/" + currentContent.getValue("id") + ".fo";
137                                         boolean foRetVal = produce(contentPrintableTemplate, foFileName, mergeData, htmlout);
138                                         
139                                         if (generatePDF.toLowerCase().equals("yes")){
140                                                 pdfFileName =  producerStorageRoot + producerDocRoot +"/" + year + "/" + month + "/" + currentContent.getValue("id") + ".pdf";
141                                                 Driver driver = new Driver();
142                                                 
143                                                 Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
144                                                 fopLog = hierarchy.getLoggerFor("fop");
145                                                 fopLog.setPriority(Priority.WARN);
146                                                 
147                                                 driver.setLogger(fopLog);
148                                                 driver.setRenderer(Driver.RENDER_PDF);
149                                                 File foFile=new File(producerStorageRoot + foFileName);
150                                                 File html2foStyleSheet=new File(templateDir+"/"+html2foStyleSheetName);
151                                                 InputHandler inputHandler = new XSLTInputHandler(foFile, html2foStyleSheet);
152                                                 XMLReader parser = inputHandler.getParser();
153                                                 driver.setOutputStream(new FileOutputStream(pdfFileName));
154                                                 driver.render(parser, inputHandler.getInputSource());
155                                                 }
156                                         }
157                                 } catch(Exception e) {
158                                         String errorText = "Producer.Content <font color=red>ERROR</font> while producing content ID:"
159                                                                                 + currentContent.getId()+", skipping it :: "+e.toString();
160                                         logHTML(htmlout, errorText);
161                                         theLog.printError(errorText);
162                                         e.printStackTrace();
163                                 }
164                                 pageCount++;
165                         }//for
166                         // if next batch get it...
167                         if (batchEntityList.hasNextBatch()){
168                                 batchEntityList = contentModule.getContent(whereClause, orderBy,
169                                                                                                                                                                 batchEntityList.getNextBatch(),
170                                                                                                                                                                 contentBatchsize, userEntity);
171                         } else {
172                                 batchEntityList=null;
173                         }
174                 }
175                 logHTMLFinish(htmlout, "Content", pageCount, startTime, System.currentTimeMillis());
176                 /** @todo why no syncing here? */
177         }
178 }
179