first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[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
18 public class ProducerContent extends Producer {
19
20         private String contentTemplate=MirConfig.getProp("Producer.Content.Template");
21
22         public static void main(String argv[]){
23                 /**
24                  * Why are we reloading the config here?
25                  * Can someone please explain this?
26                  * Hope I didn't break anything
27                  * -mh. <heckmann@hbe.ca>
28                  *
29                  * This is the entry point for standalone production. The code
30                  * is then running in a new virtual machine, e.g. when called
31                  * from cron-script. In that case the config has to be initliased.
32                  * Don't know if is ok that way //rk
33                  *
34                  * ok, i figured that out a few months ago.. -mh
35                  *
36                  */
37                 //Configuration.initConfig("config");
38                 System.out.println(MirConfig.getProp("Producer.DocRoot"));
39
40                 try {
41                         new ProducerContent().handle(new PrintWriter(System.out), null,
42                                                                                                                                                 false,false);
43                 } catch(Exception e) {
44                         System.err.println(e.toString());
45                 }
46         }
47
48
49
50         public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
51                                                                                 boolean sync)
52                 throws StorageObjectException, ModuleException {
53
54                 handle(htmlout,user,force,sync,null);
55         }
56
57         public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
58                                                                                  boolean sync, String id) throws StorageObjectException,
59                                                                                  ModuleException
60         {
61
62                 long                startTime = System.currentTimeMillis();
63                 int                 pageCount=0;
64
65                 String              whereClause = " ";
66                 String              orderBy = " ";
67                 String              htmlFileName = null;
68                 EntityContent       currentContent;
69                 EntityList          batchEntityList;
70                 EntityUsers         userEntity=null;
71
72                 int                 contentBatchsize =
73                                                 Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
74                 // production of the content-pages
75
76                 /** @todo this should be moved to ModuleContent */
77                 orderBy="date desc, webdb_lastchange desc";
78                 if(force==true){
79                         whereClause="is_published='1'";
80                         // if true: produces a single content item
81                         if(id !=null){
82                                 whereClause += " AND id="+id;
83                                 // I think this avoids a select count(*)...
84                                 contentBatchsize=-1;
85                         }
86                         batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
87                                                                                                                                                                                                 contentBatchsize, userEntity);
88                 } else {
89                         whereClause="is_produced='0' AND is_published='1'";
90                         //if true produces a single contentitem
91                         if(id !=null){
92                                 whereClause += " AND id="+id;
93                                 // this avoids a select count(*)...
94                                 contentBatchsize=-1;
95                         }
96                         batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
97                                                                                                                                                                                                 contentBatchsize, userEntity);
98                 }
99
100                 while (batchEntityList!=null) {
101                         for(int i=0;i<batchEntityList.size();i++) {
102                                 currentContent = (EntityContent)batchEntityList.elementAt(i);
103
104                                 try {
105
106                                         SimpleHash mergeData=new SimpleHash();
107                                         mergeData.put("content", currentContent);
108
109                                         /** @todo this should be assembled in entity */
110                                         String date = currentContent.getValue("date");
111                                         String year = date.substring(0,4);
112                                         String month = date.substring(4,6);
113                                         htmlFileName =  producerDocRoot + "/" + year + "/" + month + "/" +
114                                                                                                         currentContent.getValue("id") + ".shtml";
115
116                                         //produce html
117                                         boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
118                                         if ( retVal ) currentContent.setProduced(true);
119
120                                 }
121                                 catch(Exception e)
122                                 {
123                                         String errorText = "Producer.Content <font color=red>ERROR</font> while producing content ID:"
124                                                                                 + currentContent.getId()+", skipping it :: "+e.toString();
125                                         logHTML(htmlout, errorText);
126                                         theLog.printError(errorText);
127                                 }
128                                 pageCount++;
129                         }//for
130                         // if next batch get it...
131                         if (batchEntityList.hasNextBatch()){
132                                 //batchEntityList = contentModule.getByWhereClause(whereClause,
133                                 //                                                                                                              null, batchEntityList.getNextBatch(),
134                                 //                                                                                                              contentBatchsize);
135                batchEntityList = contentModule.getContent(whereClause, orderBy,
136                                                 batchEntityList.getNextBatch(),
137                                                 contentBatchsize, userEntity);
138                         } else {
139                                 batchEntityList=null;
140                         }
141
142                 }
143
144
145
146                 logHTMLFinish(htmlout, "Content", pageCount, startTime, System.currentTimeMillis());
147
148                 /** @todo why no syncing here? */
149
150         }
151
152 }
153