added back proper batching of content EntityLists with optimization for
[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         // I think 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           String date = currentContent.getValue("date");
109           String year = date.substring(0,4);
110           String month = date.substring(4,6);
111           htmlFileName =  producerDocRoot + "/" + 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         }
119         catch(Exception e)
120         {
121           String errorText = "Producer.Content <font color=red>ERROR</font> while producing content ID:"
122                     + currentContent.getId()+", skipping it :: "+e.toString();
123           logHTML(htmlout, errorText);
124           theLog.printError(errorText);
125         }
126         pageCount++;
127       }//for
128       // if next batch get it...
129       if (batchEntityList.hasNextBatch()){
130         batchEntityList = contentModule.getByWhereClause(whereClause, 
131                                     null, batchEntityList.getNextBatch(),
132                                     contentBatchsize);
133       } else {
134         batchEntityList=null;
135       }
136
137     }
138
139
140
141     logHTMLFinish(htmlout, "Content", pageCount, startTime, System.currentTimeMillis());
142
143     /** @todo why no syncing here? */
144
145   }
146
147 }
148