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[]){
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>
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
46 * ok, i figured that out a few months ago.. -mh
49 //Configuration.initConfig("config");
50 System.out.println(MirConfig.getProp("Producer.DocRoot"));
53 new ProducerContent().handle(new PrintWriter(System.out), null,
55 } catch(Exception e) {
56 System.err.println(e.toString());
62 public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
64 throws StorageObjectException, ModuleException {
66 handle(htmlout,user,force,sync,null);
69 public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
70 boolean sync, String id) throws StorageObjectException,
74 long startTime = System.currentTimeMillis();
77 String whereClause = " ";
79 String htmlFileName = null;
80 String foFileName = null;
81 String pdfFileName = null;
82 EntityContent currentContent;
83 EntityList batchEntityList;
84 EntityUsers userEntity=null;
86 int contentBatchsize =
87 Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
88 // production of the content-pages
90 /** @todo this should be moved to ModuleContent */
91 orderBy="date desc, webdb_lastchange desc";
93 whereClause="is_published='1'";
94 // if true: produces a single content item
96 whereClause += " AND id="+id;
97 // I think this avoids a select count(*)...
100 batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
101 contentBatchsize, userEntity);
103 whereClause="is_produced='0' AND is_published='1'";
104 //if true produces a single contentitem
106 whereClause += " AND id="+id;
107 // this avoids a select count(*)...
110 batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
111 contentBatchsize, userEntity);
114 while (batchEntityList!=null) {
115 for(int i=0;i<batchEntityList.size();i++) {
116 currentContent = (EntityContent)batchEntityList.elementAt(i);
120 SimpleHash mergeData=new SimpleHash();
121 mergeData.put("content", currentContent);
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";
131 boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
132 if ( retVal ) currentContent.setProduced(true);
134 //produce xsl:fo and pdf version(if desired)
135 if (generateFO.toLowerCase().equals("yes")){
136 foFileName = producerDocRoot + "/" + year + "/" + month + "/"
137 + currentContent.getValue("id") + ".fo";
138 boolean foRetVal = produce(contentPrintableTemplate, foFileName,
141 if (generatePDF.toLowerCase().equals("yes")){
142 pdfFileName = producerStorageRoot + producerDocRoot +"/" + year
144 + currentContent.getValue("id") + ".pdf";
145 Driver driver = new Driver();
147 Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
148 fopLog = hierarchy.getLoggerFor("fop");
149 fopLog.setPriority(Priority.WARN);
151 driver.setLogger(fopLog);
152 driver.setRenderer(Driver.RENDER_PDF);
153 File foFile=new File(producerStorageRoot + foFileName);
154 File html2foStyleSheet=new File(templateDir+"/"
155 +html2foStyleSheetName);
156 InputHandler inputHandler =
157 new XSLTInputHandler(foFile, html2foStyleSheet);
158 XMLReader parser = inputHandler.getParser();
159 driver.setOutputStream(new FileOutputStream(pdfFileName));
160 driver.render(parser, inputHandler.getInputSource());
163 } catch(Exception e) {
164 String errorText = "Producer.Content <font color=red>ERROR</font> while producing content ID:"
165 + currentContent.getId()+", skipping it :: "+e.toString();
166 logHTML(htmlout, errorText);
167 theLog.printError(errorText);
172 // if next batch get it...
173 if (batchEntityList.hasNextBatch()){
174 batchEntityList = contentModule.getContent(whereClause, orderBy,
175 batchEntityList.getNextBatch(),
176 contentBatchsize, userEntity);
178 batchEntityList=null;
181 logHTMLFinish(htmlout, "Content", pageCount, startTime, System.currentTimeMillis());
182 /** @todo why no syncing here? */