bee57b8f98341c36184c0d3cfbd42c7519a47a87
[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
47
48
49   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
50     throws StorageObjectException, ModuleException {
51
52     String contentTemplate = MirConfig.getProp("Producer.Content.Template");
53     int contentBatchsize = Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
54     String extLinkName = MirConfig.getProp("Producer.ExtLinkName");
55     String intLinkName = MirConfig.getProp("Producer.IntLinkName");
56     String mailLinkName = MirConfig.getProp("Producer.MailLinkName");
57     String imageRoot = MirConfig.getProp("Producer.ImageRoot");
58
59     long                sessionConnectTime = 0;
60     long                startTime = (new java.util.Date()).getTime();
61     String              whereClause = " ";
62     String              orderBy = " ";
63     String              htmlFileName = null;
64     String              currentMediaId;
65     EntityContent       currentContent;
66     EntityList          batchEntityList;
67     HashMap             currentContentValues;
68     SimpleHash          imageHash = new SimpleHash();
69     EntityUsers         userEntity=null;
70
71
72
73     // production of the content-pages
74     orderBy="date desc, webdb_lastchange desc";
75     if(force==true){
76       whereClause="is_published='1'";
77       // if true: produces a single content item
78       if(id !=null){
79         whereClause += " AND id="+id;
80       }
81       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
82     } else {
83       whereClause="is_produced='0' AND is_published='1'";
84       //if true produces a single contentitem
85       if(id !=null){
86         whereClause += " AND id="+id;
87       }
88       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
89     }
90
91     while (batchEntityList != null) {
92       for(int i=0;i<batchEntityList.size();i++) {
93         currentContent = (EntityContent)batchEntityList.elementAt(i);
94         currentContentValues = currentContent.getValues();
95
96         //because of postgres 7.1.* not needed anymore
97         //currentContentValues.put("content_data",currentContent.getContentData());
98         String date = (String)currentContentValues.get("date");
99         String year = date.substring(0,4);
100         String month = date.substring(4,6);
101
102         htmlFileName =  producerDocRoot
103           + "/" + year + "/" + month + "/" +  currentContentValues.get("id") + ".shtml";
104
105         currentContentValues.put("content_data",StringUtil.deleteForbiddenTags((String)currentContentValues.get("content_data")));
106         currentContentValues.put("description",StringUtil.deleteForbiddenTags((String)currentContentValues.get("description")));
107
108
109         //create http-links and email-links
110         if (currentContentValues.get("is_html").equals("0")) {
111           String temp = (String)currentContentValues.get("content_data");
112           if(temp!=null && temp.length()>0){
113             temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
114             temp = StringUtil.decodeHTMLinTags(temp);
115             currentContentValues.put("content_data",temp);
116           }
117           temp = (String)currentContentValues.get("description");
118           if(temp!=null && temp.length()>0){
119             temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
120             temp = StringUtil.decodeHTMLinTags(temp);
121             currentContentValues.put("description",temp);
122           }
123         } else {
124           String temp = (String)currentContentValues.get("content_data");
125           if(temp!=null && temp.length()>0){
126             temp = StringUtil.decodeHTMLinTags(temp);
127             currentContentValues.put("content_data",temp);
128           }
129           temp = (String)currentContentValues.get("description");
130           if(temp!=null && temp.length()>0){
131             temp = StringUtil.decodeHTMLinTags(temp);
132             currentContentValues.put("description",temp);
133           }
134         }
135
136         //create the freemarker-model
137         SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(currentContentValues);
138
139         // get the images
140         EntityList currentMedia = DatabaseContentToMedia.getInstance().getImages(currentContent);
141         if (currentMedia!=null && currentMedia.getCount()>=1) {
142           SimpleList mediaList = HTMLTemplateProcessor.makeSimpleList(currentMedia);
143           mergeData.put("to_media", mediaList);
144         }
145
146         // get the comments for the article
147         mergeData.put("comments", currentContent.getComments());
148
149         // get the topics of this article
150         mergeData.put("topics",HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(currentContent)));
151
152         //produce html
153         boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
154         sessionConnectTime = new java.util.Date().getTime() - startTime;
155         if (retVal == true && !"1".equals(currentContent.getValue("is_produced")))
156             currentContent.setProduced(true);
157       }
158
159       if (batchEntityList.hasNextBatch()){
160         batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);
161       } else {
162         batchEntityList=null;
163       }
164     }
165
166     // timing an message to browser
167     sessionConnectTime = new java.util.Date().getTime() - startTime;
168     logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");
169   }
170
171 }