c430f210b112fb188712cd7fe4fd2e8f27050c21
[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   public static void main(String argv[]){
21     /**
22      * Why are we reloading the config here?
23      * Can someone please explain this?
24      * Hope I didn't break anything
25      * -mh. <heckmann@hbe.ca>
26      */
27     //Configuration.initConfig("config");
28     System.out.println(MirConfig.getProp("Producer.DocRoot"));
29
30     try {
31       new ProducerContent().handle(new PrintWriter(System.out), null, false,false);
32     } catch(Exception e) {
33       System.err.println(e.toString());
34     }
35   }
36
37
38
39   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)
40     throws StorageObjectException, ModuleException {
41
42     handle(htmlout,user,force,sync,null);
43   }
44
45
46   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
47     throws StorageObjectException, ModuleException {
48
49     String contentTemplate = MirConfig.getProp("Producer.Content.Template");
50     int contentBatchsize = Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
51
52     long                sessionConnectTime = 0;
53     long                startTime = (new java.util.Date()).getTime();
54     String              whereClause = " ";
55     String              orderBy = " ";
56     String              htmlFileName = null;
57     EntityContent       currentContent;
58     EntityList          batchEntityList;
59     EntityUsers         userEntity=null;
60
61
62     // production of the content-pages
63     orderBy="date desc, webdb_lastchange desc";
64     if(force==true){
65       whereClause="is_published='1'";
66       // if true: produces a single content item
67       if(id !=null){
68         whereClause += " AND id="+id;
69       }
70       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
71     } else {
72       whereClause="is_produced='0' AND is_published='1'";
73       //if true produces a single contentitem
74       if(id !=null){
75         whereClause += " AND id="+id;
76       }
77       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
78     }
79
80     while (batchEntityList != null) {
81       for(int i=0;i<batchEntityList.size();i++) {
82         currentContent = (EntityContent)batchEntityList.elementAt(i);
83         try {
84             String date = currentContent.getValue("date");
85             String year = date.substring(0,4);
86             String month = date.substring(4,6);
87             htmlFileName =  producerDocRoot
88               + "/" + year + "/" + month + "/" +  currentContent.getValue("id") + ".shtml";
89             //produce html
90             boolean retVal = produce(contentTemplate, htmlFileName, currentContent, htmlout);
91             sessionConnectTime = new java.util.Date().getTime() - startTime;
92             if (retVal == true && !"1".equals(currentContent.getValue("is_produced")))
93                 currentContent.setProduced(true);
94         } catch(Exception e) {
95           logHTML(htmlout, "Producer.Content ERROR while producing content ID: " + currentContent.getId()+",skipping it :: "+e.toString());
96           theLog.printError("Producer.Content ERROR while producing content ID: " + currentContent.getId() +",skipping it :: "+e.toString());
97         }
98
99       }//for
100
101       if (batchEntityList.hasNextBatch()){
102         batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);
103       } else {
104         batchEntityList=null;
105       }
106     }
107
108     // timing and message to browser
109     sessionConnectTime = new java.util.Date().getTime() - startTime;
110     logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");
111   }
112
113 }
114