Initial revision
[mir.git] / source / mircoders / producer / ProducerContent.java
1 package mircoders.producer;\r
2 \r
3 import java.io.*;\r
4 import java.lang.*;\r
5 import java.util.*;\r
6 \r
7 import freemarker.template.*;\r
8 \r
9 import com.icl.saxon.trax.Transformer;\r
10 \r
11 import webdb.misc.*;\r
12 import webdb.storage.*;\r
13 import webdb.module.*;\r
14 import webdb.entity.*;\r
15 \r
16 import mir.module.*;\r
17 import mir.entity.*;\r
18 import mir.storage.*;\r
19 \r
20 \r
21 public class ProducerContent extends Producer {\r
22 \r
23         public static void main(String argv[]){\r
24                 Configuration.initConfig("config");\r
25                 System.out.println(Configuration.getProperty("Producer.DocRoot"));\r
26                 try {\r
27                         new ProducerContent().handle(new PrintWriter(System.out), null, false,false);\r
28                 } catch(Exception e) { System.err.println(e.toString()); }\r
29         }\r
30 \r
31         public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)\r
32                 throws StorageObjectException, ModuleException {\r
33                 handle(htmlout,user,force,sync,null);\r
34         }\r
35 \r
36 \r
37         public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)\r
38                 throws StorageObjectException, ModuleException\r
39         {\r
40 \r
41                 String contentTemplate = Configuration.getProperty("Producer.Content.Template");\r
42                 int contentBatchsize = Integer.parseInt(Configuration.getProperty("Producer.Content.Batchsize"));\r
43                 String extLinkName = Configuration.getProperty("Producer.ExtLinkName");\r
44                 String intLinkName = Configuration.getProperty("Producer.IntLinkName");\r
45                 String mailLinkName = Configuration.getProperty("Producer.MailLinkName");\r
46                 String imageRoot = Configuration.getProperty("Producer.ImageRoot");\r
47 \r
48                 long                sessionConnectTime = 0;\r
49                 long                startTime = (new java.util.Date()).getTime();\r
50                 String              whereClause = " ";\r
51                 String              orderBy = " ";\r
52                 String              htmlFileName = null;\r
53                 String              currentMediaId;\r
54                 EntityContent       currentContent;\r
55                 EntityList          batchEntityList;\r
56                 HashMap             currentContentValues;\r
57                 SimpleHash          imageHash = new SimpleHash();\r
58                 EntityGruppen       userEntity=null;\r
59 \r
60                 // production of the content-pages\r
61                 orderBy="date desc, webdb_lastchange desc";\r
62                 if(force==true){\r
63                         whereClause="is_published='1'";\r
64 \r
65                         // if true: produces a single content item\r
66                         if(id !=null){\r
67                                 whereClause += " AND id="+id;\r
68                         }\r
69                         batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);\r
70                 } else {\r
71                         whereClause="is_produced='0' AND is_published='1'";\r
72 \r
73                         //if true produces a single contentitem\r
74                         if(id !=null){\r
75                                 whereClause += " AND id="+id;\r
76                         }\r
77                         batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);\r
78                 }\r
79 \r
80                 while (batchEntityList != null) {\r
81                         for(int i=0;i<batchEntityList.size();i++) {\r
82                                 currentContent = (EntityContent)batchEntityList.elementAt(i);\r
83                                 currentContentValues = currentContent.getValues();\r
84                                 //currentContentValues.put("content_data",currentContent.getContentData());\r
85 \r
86 \r
87                                 String date = (String)currentContentValues.get("date");\r
88                                 String year = date.substring(0,4);\r
89                                 String month = date.substring(4,6);\r
90 \r
91                                 htmlFileName =  producerDocRoot\r
92                                         + "/" + year + "/" + month + "/" +  currentContentValues.get("id") + ".shtml";\r
93 \r
94                                 currentContentValues.put("content_data",StringUtil.deleteForbiddenTags((String)currentContentValues.get("content_data")));\r
95                                 currentContentValues.put("description",StringUtil.deleteForbiddenTags((String)currentContentValues.get("description")));\r
96 \r
97         if (currentContentValues.get("is_html").equals("0")) {\r
98           String temp = (String)currentContentValues.get("content_data");\r
99           theLog.printDebugInfo("länge:"+temp.length());\r
100           if(temp!=null && temp.length()>0){\r
101             temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);\r
102             temp = StringUtil.decodeHTMLinTags(temp);\r
103             currentContentValues.put("content_data",temp);\r
104           }\r
105           temp = (String)currentContentValues.get("description");\r
106           if(temp!=null && temp.length()>0){\r
107             temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);\r
108             temp = StringUtil.decodeHTMLinTags(temp);\r
109             currentContentValues.put("description",temp);\r
110           }\r
111                                 } else {\r
112           String temp = (String)currentContentValues.get("content_data");\r
113           if(temp!=null && temp.length()>0){\r
114             temp = StringUtil.decodeHTMLinTags(temp);\r
115             currentContentValues.put("content_data",temp);\r
116           }\r
117           temp = (String)currentContentValues.get("description");\r
118           if(temp!=null && temp.length()>0){\r
119             temp = StringUtil.decodeHTMLinTags(temp);\r
120             currentContentValues.put("description",temp);\r
121           }\r
122         }\r
123 \r
124                                 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(currentContentValues);\r
125 \r
126                                 // get the images\r
127                                 currentMediaId = currentContent.getValue("to_media");\r
128                                 if (currentMediaId!=null && !currentMediaId.equals("")) {\r
129                                         imageHash.put(currentMediaId, HTMLTemplateProcessor.makeSimpleHash(imageModule.getById(currentMediaId)));\r
130                                 }\r
131                                 mergeData.put("images", imageHash);\r
132 \r
133                                 // get the comments for the article\r
134                                 mergeData.put("comments", currentContent.getComments());\r
135 \r
136                                 // get the topics of this article\r
137                                 mergeData.put("topics",HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(currentContent)));\r
138 \r
139                                 boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);\r
140                                 sessionConnectTime = new java.util.Date().getTime() - startTime;\r
141                                 if (retVal == true && !"1".equals(currentContent.getValue("is_produced")))\r
142                                                 currentContent.setProduced(true);\r
143                         }\r
144                         if (batchEntityList.hasNextBatch()){\r
145                                 batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);\r
146                                 //theLog.printDebugInfo("producing next batch");\r
147                         } else {\r
148                                 batchEntityList=null;\r
149                         }\r
150                 }\r
151                 // Finish\r
152                 sessionConnectTime = new java.util.Date().getTime() - startTime;\r
153                 logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");\r
154 \r
155         }\r
156 \r
157 }