9651787850897a8896404ffb25b680cfcbda61f0
[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     Configuration.initConfig("config");
22     System.out.println(Configuration.getProperty("Producer.DocRoot"));
23
24     try {
25       new ProducerContent().handle(new PrintWriter(System.out), null, false,false);
26     } catch(Exception e) {
27       System.err.println(e.toString());
28     }
29   }
30
31
32
33   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)
34     throws StorageObjectException, ModuleException {
35
36     handle(htmlout,user,force,sync,null);
37   }
38
39
40
41
42
43   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
44     throws StorageObjectException, ModuleException {
45
46     String contentTemplate = Configuration.getProperty("Producer.Content.Template");
47     int contentBatchsize = Integer.parseInt(Configuration.getProperty("Producer.Content.Batchsize"));
48     String extLinkName = Configuration.getProperty("Producer.ExtLinkName");
49     String intLinkName = Configuration.getProperty("Producer.IntLinkName");
50     String mailLinkName = Configuration.getProperty("Producer.MailLinkName");
51     String imageRoot = Configuration.getProperty("Producer.ImageRoot");
52
53     long                sessionConnectTime = 0;
54     long                startTime = (new java.util.Date()).getTime();
55     String              whereClause = " ";
56     String              orderBy = " ";
57     String              htmlFileName = null;
58     String              currentMediaId;
59     EntityContent       currentContent;
60     EntityList          batchEntityList;
61     HashMap             currentContentValues;
62     SimpleHash          imageHash = new SimpleHash();
63     EntityGruppen       userEntity=null;
64
65
66
67     // production of the content-pages
68     orderBy="date desc, webdb_lastchange desc";
69     if(force==true){
70       whereClause="is_published='1'";
71       // if true: produces a single content item
72       if(id !=null){
73         whereClause += " AND id="+id;
74       }
75       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
76     } else {
77       whereClause="is_produced='0' AND is_published='1'";
78       //if true produces a single contentitem
79       if(id !=null){
80         whereClause += " AND id="+id;
81       }
82       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
83     }
84
85     while (batchEntityList != null) {
86       for(int i=0;i<batchEntityList.size();i++) {
87         currentContent = (EntityContent)batchEntityList.elementAt(i);
88         currentContentValues = currentContent.getValues();
89
90         //because of postgres 7.1.* not needed anymore
91         //currentContentValues.put("content_data",currentContent.getContentData());
92         String date = (String)currentContentValues.get("date");
93         String year = date.substring(0,4);
94         String month = date.substring(4,6);
95
96         htmlFileName =  producerDocRoot
97           + "/" + year + "/" + month + "/" +  currentContentValues.get("id") + ".shtml";
98
99         currentContentValues.put("content_data",StringUtil.deleteForbiddenTags((String)currentContentValues.get("content_data")));
100         currentContentValues.put("description",StringUtil.deleteForbiddenTags((String)currentContentValues.get("description")));
101
102
103         //create http-links and email-links
104         if (currentContentValues.get("is_html").equals("0")) {
105           String temp = (String)currentContentValues.get("content_data");
106           if(temp!=null && temp.length()>0){
107             temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
108             temp = StringUtil.decodeHTMLinTags(temp);
109             currentContentValues.put("content_data",temp);
110           }
111           temp = (String)currentContentValues.get("description");
112           if(temp!=null && temp.length()>0){
113             temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
114             temp = StringUtil.decodeHTMLinTags(temp);
115             currentContentValues.put("description",temp);
116           }
117         } else {
118           String temp = (String)currentContentValues.get("content_data");
119           if(temp!=null && temp.length()>0){
120             temp = StringUtil.decodeHTMLinTags(temp);
121             currentContentValues.put("content_data",temp);
122           }
123           temp = (String)currentContentValues.get("description");
124           if(temp!=null && temp.length()>0){
125             temp = StringUtil.decodeHTMLinTags(temp);
126             currentContentValues.put("description",temp);
127           }
128         }
129
130         //create the freemarker-model
131         SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(currentContentValues);
132
133         // get the images
134                                 EntityList currentMedia = DatabaseContentToMedia.getInstance().getMedia(currentContent);
135                                 if (currentMedia!=null && currentMedia.getCount()>=1) {
136                                         SimpleList mediaList = HTMLTemplateProcessor.makeSimpleList(currentMedia);
137                                         mergeData.put("to_media", mediaList);
138                                 }
139                                 /**
140         currentMediaId = currentContent.getValue("to_media");
141         if (currentMediaId!=null && !currentMediaId.equals("")) {
142           imageHash.put(currentMediaId, HTMLTemplateProcessor.makeSimpleHash(imageModule.getById(currentMediaId)));
143         }
144         mergeData.put("images", imageHash);
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 }