language fix
[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                 //Configuration.initConfig("config");
36                 System.out.println(MirConfig.getProp("Producer.DocRoot"));
37
38                 try {
39                         new ProducerContent().handle(new PrintWriter(System.out), null,
40                                                                                                                                                 false,false);
41                 } catch(Exception e) {
42                         System.err.println(e.toString());
43                 }
44         }
45
46         public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
47                                                                                 boolean sync)
48                 throws StorageObjectException, ModuleException {
49
50                 handle(htmlout,user,force,sync,null);
51         }
52
53         public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
54                                                                                  boolean sync, String id) throws StorageObjectException,
55                                                                                  ModuleException
56         {
57
58                 long                startTime = System.currentTimeMillis();
59                 int                 pageCount=0;
60
61                 String              whereClause = " ";
62                 String              orderBy = " ";
63                 String              htmlFileName = null;
64                 String              foFileName = null;
65                 String              pdfFileName = null;
66                 EntityContent       currentContent;
67                 EntityList          batchEntityList;
68                 EntityUsers         userEntity=null;
69
70                 int                 contentBatchsize =
71                                                 Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
72                 // production of the content-pages
73
74                 /** @todo this should be moved to ModuleContent */
75                 orderBy="date desc, webdb_lastchange desc";
76                 if(force==true){
77                         whereClause="is_published='1'";
78                         // if true: produces a single content item
79                         if(id !=null){
80                                 whereClause += " AND id="+id;
81                                 // I think this avoids a select count(*)...
82                                 contentBatchsize=-1;
83                         }
84                         batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
85                                                                                                                                                                                                 contentBatchsize, userEntity);
86                 } else {
87                         whereClause="is_produced='0' AND is_published='1'";
88                         //if true produces a single contentitem
89                         if(id !=null){
90                                 whereClause += " AND id="+id;
91                                 // this avoids a select count(*)...
92                                 contentBatchsize=-1;
93                         }
94                         batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
95                                                                                                                                                                                                 contentBatchsize, userEntity);
96                 }
97
98                 while (batchEntityList!=null) {
99                         for(int i=0;i<batchEntityList.size();i++) {
100                                 currentContent = (EntityContent)batchEntityList.elementAt(i);
101
102                                 try {
103
104                                         SimpleHash mergeData=new SimpleHash();
105                                         mergeData.put("content", currentContent);
106
107                                         /** @todo this should be assembled in entity */
108                                         String date = currentContent.getValue("date");
109                                         String year = date.substring(0,4);
110                                         String month = date.substring(4,6);
111                                         htmlFileName =  "/" + year + "/" + month + "/" +
112                                                                                                         currentContent.getValue("id") + ".shtml";
113                                         
114                                         //produce html
115                                         boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
116                                         if ( retVal ) currentContent.setProduced(true);
117                                         
118                                         //produce xsl:fo and pdf version(if desired)
119                                         if (generateFO.toLowerCase().equals("yes")){
120             foFileName =  "/" + year + "/" + month + "/"
121                           + currentContent.getValue("id") + ".fo";
122             boolean foRetVal = produce(contentPrintableTemplate, foFileName,
123                                         mergeData, htmlout, "UTF8");
124                                         
125             if (generatePDF.toLowerCase().equals("yes")){
126               pdfFileName =  producerStorageRoot + "/" + year
127                               + "/" + month + "/"
128                               + currentContent.getValue("id") + ".pdf";
129               Driver driver = new Driver();
130                                                 
131               Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
132               fopLog = hierarchy.getLoggerFor("fop");
133               fopLog.setPriority(Priority.WARN);
134                                                 
135               driver.setLogger(fopLog);
136               driver.setRenderer(Driver.RENDER_PDF);
137               File foFile=new File(producerStorageRoot + foFileName);
138               File html2foStyleSheet=new File(templateDir+"/"
139                                               +html2foStyleSheetName);
140               InputHandler inputHandler =
141                 new XSLTInputHandler(foFile, html2foStyleSheet);
142               XMLReader parser = inputHandler.getParser();
143               driver.setOutputStream(new FileOutputStream(pdfFileName));
144               driver.render(parser, inputHandler.getInputSource());
145             }
146                                         }
147                                 } catch(Exception e) {
148                                         String errorText = "Producer.Content <font color=red>ERROR</font> while producing content ID:"
149                                                                                 + currentContent.getId()+", skipping it :: "+e.toString();
150                                         logHTML(htmlout, errorText);
151                                         theLog.printError(errorText);
152                                         e.printStackTrace();
153                                 }
154                                 pageCount++;
155                         }//for
156                         // if next batch get it...
157                         if (batchEntityList.hasNextBatch()){
158                                 batchEntityList = contentModule.getContent(whereClause, orderBy,
159                                                                                                                                                                 batchEntityList.getNextBatch(),
160                                                                                                                                                                 contentBatchsize, userEntity);
161                         } else {
162                                 batchEntityList=null;
163                         }
164                 }
165                 logHTMLFinish(htmlout, "Content", pageCount, startTime, System.currentTimeMillis());
166                 /** @todo why no syncing here? */
167         }
168 }
169