producerContent cleaned & bugfix for contentn.template
[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
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     for(int i=0;i<batchEntityList.size();i++) {
92       currentContent = (EntityContent)batchEntityList.elementAt(i);
93
94       try {
95
96         SimpleHash mergeData=new SimpleHash();
97         mergeData.put("content", currentContent);
98         String date = currentContent.getValue("date");
99         String year = date.substring(0,4);
100         String month = date.substring(4,6);
101         htmlFileName =  producerDocRoot + "/" + year + "/" + month + "/" +
102                         currentContent.getValue("id") + ".shtml";
103
104         //produce html
105         boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
106         if ( retVal ) currentContent.setProduced(true);
107
108       }
109       catch(Exception e)
110       {
111         String errorText = "Producer.Content ERROR while producing content ID:"
112                   + currentContent.getId()+", skipping it :: "+e.toString();
113         logHTML(htmlout, errorText);
114         theLog.printError(errorText);
115       }
116
117     }//for
118
119     // timing and message to browser
120     logHTML(htmlout, "Producer.Content finished: " +
121             (System.currentTimeMillis() - startTime) + " ms.");
122
123     /** @todo here we should have a link back to admin logged */
124     /** @todo why no syncing here? */
125
126   }
127
128 }
129