1 package mircoders.producer;
7 import freemarker.template.*;
14 import mircoders.entity.*;
15 import mircoders.storage.*;
18 import org.apache.fop.apps.* ;
19 import org.xml.sax.InputSource;
20 import org.xml.sax.XMLReader;
21 import org.apache.log.*;
23 public class ProducerContent extends Producer {
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");
34 public static void main(String argv[]){
35 //Configuration.initConfig("config");
36 System.out.println(MirConfig.getProp("Producer.DocRoot"));
39 new ProducerContent().handle(new PrintWriter(System.out), null,
41 } catch(Exception e) {
42 System.err.println(e.toString());
46 public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
48 throws StorageObjectException, ModuleException {
50 handle(htmlout,user,force,sync,null);
53 public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
54 boolean sync, String id) throws StorageObjectException,
58 long startTime = System.currentTimeMillis();
61 String whereClause = " ";
63 String htmlFileName = null;
64 String foFileName = null;
65 String pdfFileName = null;
66 EntityContent currentContent;
67 EntityList batchEntityList;
68 EntityUsers userEntity=null;
70 int contentBatchsize =
71 Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
72 // production of the content-pages
74 /** @todo this should be moved to ModuleContent */
75 orderBy="date desc, webdb_lastchange desc";
77 whereClause="is_published='1'";
78 // if true: produces a single content item
80 whereClause += " AND id="+id;
81 // I think this avoids a select count(*)...
84 batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
85 contentBatchsize, userEntity);
87 whereClause="is_produced='0' AND is_published='1'";
88 //if true produces a single contentitem
90 whereClause += " AND id="+id;
91 // this avoids a select count(*)...
94 batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
95 contentBatchsize, userEntity);
98 while (batchEntityList!=null) {
99 for(int i=0;i<batchEntityList.size();i++) {
100 currentContent = (EntityContent)batchEntityList.elementAt(i);
104 SimpleHash mergeData=new SimpleHash();
105 mergeData.put("content", currentContent);
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";
115 boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
116 if ( retVal ) currentContent.setProduced(true);
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");
125 if (generatePDF.toLowerCase().equals("yes")){
126 pdfFileName = producerStorageRoot + "/" + year
128 + currentContent.getValue("id") + ".pdf";
129 Driver driver = new Driver();
131 Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
132 fopLog = hierarchy.getLoggerFor("fop");
133 fopLog.setPriority(Priority.WARN);
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());
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);
156 // if next batch get it...
157 if (batchEntityList.hasNextBatch()){
158 batchEntityList = contentModule.getContent(whereClause, orderBy,
159 batchEntityList.getNextBatch(),
160 contentBatchsize, userEntity);
162 batchEntityList=null;
165 logHTMLFinish(htmlout, "Content", pageCount, startTime, System.currentTimeMillis());
166 /** @todo why no syncing here? */