tuning full production of content
[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      */
35     //Configuration.initConfig("config");
36     System.out.println(MirConfig.getProp("Producer.DocRoot"));
37
38     try {
39       new ProducerContent().handle(new PrintWriter(System.out), null,
40                                     false,false);
41     } catch(Exception e) {
42       System.err.println(e.toString());
43     }
44   }
45
46
47
48   public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
49                     boolean sync)
50     throws StorageObjectException, ModuleException {
51
52     handle(htmlout,user,force,sync,null);
53   }
54
55   public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
56                      boolean sync, String id) throws StorageObjectException, ModuleException
57   {
58
59     long                startTime = System.currentTimeMillis();
60     int                 pageCount=0;
61
62     String              whereClause = " ";
63     String              orderBy = " ";
64     String              htmlFileName = null;
65     EntityContent       currentContent;
66     EntityList          batchEntityList;
67     EntityUsers         userEntity=null;
68
69     // production of the content-pages
70
71     /** @todo this should be moved to ModuleContent */
72     orderBy="date desc, webdb_lastchange desc";
73     if(force==true){
74       whereClause="is_published='1'";
75       // if true: produces a single content item
76       if(id !=null){
77         whereClause += " AND id="+id;
78       }
79       batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
80                                                 -1, userEntity);
81     } else {
82       whereClause="is_produced='0' AND is_published='1'";
83       //if true produces a single contentitem
84       if(id !=null){
85         whereClause += " AND id="+id;
86       }
87       batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
88                                                 -1, userEntity);
89     }
90
91     if (batchEntityList!=null) {
92       for(int i=0;i<batchEntityList.size();i++) {
93         currentContent = (EntityContent)batchEntityList.elementAt(i);
94
95         try {
96
97           SimpleHash mergeData=new SimpleHash();
98           mergeData.put("content", currentContent);
99           String date = currentContent.getValue("date");
100           String year = date.substring(0,4);
101           String month = date.substring(4,6);
102           htmlFileName =  producerDocRoot + "/" + year + "/" + month + "/" +
103                           currentContent.getValue("id") + ".shtml";
104
105           //produce html
106           boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
107           if ( retVal ) currentContent.setProduced(true);
108
109         }
110         catch(Exception e)
111         {
112           String errorText = "Producer.Content ERROR while producing content ID:"
113                     + currentContent.getId()+", skipping it :: "+e.toString();
114           logHTML(htmlout, errorText);
115           theLog.printError(errorText);
116         }
117         pageCount++;
118       }//for
119     }
120     // timing and message to browser
121     long overall = System.currentTimeMillis() - startTime;
122     long ppm = pageCount / (overall/60000);
123     logHTML(htmlout, "Producer.Content finished producing: " +
124             overall + " ms for "+ pageCount+" Pages = " +ppm + " pages/min");
125     logHTML(htmlout, "Back to <a href=\""+actionRoot+"\">Admin-Startage</a>");
126
127     /** @todo why no syncing here? */
128
129   }
130
131 }
132