don't sort by the "date" field, use webdb_create instead
[mir.git] / source / mircoders / producer / ProducerContent.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.producer;
33
34 import java.io.*;
35 import java.lang.*;
36 import java.util.*;
37
38 import freemarker.template.*;
39
40 import mir.misc.*;
41 import mir.storage.*;
42 import mir.module.*;
43 import mir.entity.*;
44
45 import mircoders.entity.*;
46 import mircoders.storage.*;
47
48 //for pdf production
49 import org.apache.fop.apps.* ;
50 import org.xml.sax.InputSource;
51 import org.xml.sax.XMLReader;
52 import org.apache.log.*;
53
54 public class ProducerContent extends Producer {
55
56         private String contentTemplate=MirConfig.getProp("Producer.Content.Template");
57         private String contentPrintableTemplate=MirConfig.getProp("Producer.PrintableContent.Template");
58         private String html2foStyleSheetName=MirConfig.getProp("Producer.PrintableContent.html2foStyleSheetName");
59         private String generateFO=MirConfig.getProp("GenerateFO");
60         private String generatePDF=MirConfig.getProp("GeneratePDF");
61         private String producerStorageRoot=MirConfig.getProp("Producer.StorageRoot");
62         private Logger fopLog=null;
63         private String templateDir = MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir");
64         
65         public static void main(String argv[]){
66                 //Configuration.initConfig("config");
67                 System.out.println(MirConfig.getProp("Producer.DocRoot"));
68
69                 try {
70                         new ProducerContent().handle(new PrintWriter(System.out), null,
71                                                                                                                                                 false,false);
72                 } catch(Exception e) {
73                         System.err.println(e.toString());
74                 }
75         }
76
77         public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
78                                                                                 boolean sync)
79                 throws StorageObjectException, ModuleException {
80
81                 handle(htmlout,user,force,sync,null);
82         }
83
84         public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
85                                                                                  boolean sync, String id) throws StorageObjectException,
86                                                                                  ModuleException
87         {
88
89                 long                startTime = System.currentTimeMillis();
90                 int                 pageCount=0;
91
92                 String              whereClause = " ";
93                 String              orderBy = " ";
94                 String              htmlFileName = null;
95                 String              foFileName = null;
96                 String              pdfFileName = null;
97                 EntityContent       currentContent;
98                 EntityList          batchEntityList;
99                 EntityUsers         userEntity=null;
100
101                 int                 contentBatchsize =
102                                                 Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
103                 // production of the content-pages
104
105                 /** @todo this should be moved to ModuleContent */
106                 orderBy="webdb_lastchange desc";
107                 if(force==true){
108                         whereClause="is_published='1'";
109                         // if true: produces a single content item
110                         if(id !=null){
111                                 whereClause += " AND id="+id;
112                                 // I think this avoids a select count(*)...
113                                 contentBatchsize=-1;
114                         }
115                         batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
116                                                                                                                                                                                                 contentBatchsize, userEntity);
117                 } else {
118                         whereClause="is_produced='0' AND is_published='1'";
119                         //if true produces a single contentitem
120                         if(id !=null){
121                                 whereClause += " AND id="+id;
122                                 // this avoids a select count(*)...
123                                 contentBatchsize=-1;
124                         }
125                         batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
126                                                                                                                                                                                                 contentBatchsize, userEntity);
127                 }
128
129                 while (batchEntityList!=null) {
130                         for(int i=0;i<batchEntityList.size();i++) {
131                                 currentContent = (EntityContent)batchEntityList.elementAt(i);
132
133                                 try {
134
135                                         SimpleHash mergeData=new SimpleHash();
136                                         mergeData.put("content", currentContent);
137
138                                         /** @todo this should be assembled in entity */
139                                         String date = currentContent.getValue("date");
140                                         String year = date.substring(0,4);
141                                         String month = date.substring(4,6);
142                                         htmlFileName =  "/" + year + "/" + month + "/" +
143                                                                                                         currentContent.getValue("id") + ".shtml";
144                                         
145                                         //produce html
146                                         boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
147                                         if ( retVal ) currentContent.setProduced(true);
148                                         
149                                         //produce xsl:fo and pdf version(if desired)
150                                         if (generateFO.toLowerCase().equals("yes")){
151             foFileName =  "/" + year + "/" + month + "/"
152                           + currentContent.getValue("id") + ".fo";
153             boolean foRetVal = produce(contentPrintableTemplate, foFileName,
154                                         mergeData, htmlout, "UTF8");
155                                         
156             if (generatePDF.toLowerCase().equals("yes")){
157               pdfFileName =  producerStorageRoot + "/" + year
158                               + "/" + month + "/"
159                               + currentContent.getValue("id") + ".pdf";
160               Driver driver = new Driver();
161                                                 
162               Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
163               fopLog = hierarchy.getLoggerFor("fop");
164               fopLog.setPriority(Priority.WARN);
165                                                 
166               driver.setLogger(fopLog);
167               driver.setRenderer(Driver.RENDER_PDF);
168               File foFile=new File(producerStorageRoot + foFileName);
169               File html2foStyleSheet=new File(templateDir+"/"
170                                               +html2foStyleSheetName);
171               InputHandler inputHandler =
172                 new XSLTInputHandler(foFile, html2foStyleSheet);
173               XMLReader parser = inputHandler.getParser();
174               driver.setOutputStream(new FileOutputStream(pdfFileName));
175               driver.render(parser, inputHandler.getInputSource());
176             }
177                                         }
178                                 } catch(Exception e) {
179                                         String errorText = "Producer.Content <font color=red>ERROR</font> while producing content ID:"
180                                                                                 + currentContent.getId()+", skipping it :: "+e.toString();
181                                         logHTML(htmlout, errorText);
182                                         theLog.printError(errorText);
183                                         e.printStackTrace();
184                                 }
185                                 pageCount++;
186                         }//for
187                         // if next batch get it...
188                         if (batchEntityList.hasNextBatch()){
189                                 batchEntityList = contentModule.getContent(whereClause, orderBy,
190                                                                                                                                                                 batchEntityList.getNextBatch(),
191                                                                                                                                                                 contentBatchsize, userEntity);
192                         } else {
193                                 batchEntityList=null;
194                         }
195                 }
196                 logHTMLFinish(htmlout, "Content", pageCount, startTime, System.currentTimeMillis());
197                 /** @todo why no syncing here? */
198         }
199 }
200