*** empty log message ***
[mir.git] / source / mircoders / producer / ProducerContent.java
1 package mircoders.producer;
2
3 import java.io.*;
4 import java.lang.*;
5 import java.lang.reflect.*;
6 import java.util.*;
7
8 import freemarker.template.*;
9
10 import mir.misc.*;
11 import mir.media.*;
12 import mir.storage.*;
13 import mir.module.*;
14 import mir.entity.*;
15
16 import mircoders.entity.*;
17 import mircoders.storage.*;
18
19
20 public class ProducerContent extends Producer {
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     //Configuration.initConfig("config");
30     System.out.println(MirConfig.getProp("Producer.DocRoot"));
31
32     try {
33       new ProducerContent().handle(new PrintWriter(System.out), null, false,false);
34     } catch(Exception e) {
35       System.err.println(e.toString());
36     }
37   }
38
39
40
41   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)
42     throws StorageObjectException, ModuleException {
43
44     handle(htmlout,user,force,sync,null);
45   }
46
47
48
49
50
51   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
52     throws StorageObjectException, ModuleException {
53
54     String contentTemplate = MirConfig.getProp("Producer.Content.Template");
55     int contentBatchsize = Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
56     String extLinkName = MirConfig.getProp("Producer.ExtLinkName");
57     String intLinkName = MirConfig.getProp("Producer.IntLinkName");
58     String mailLinkName = MirConfig.getProp("Producer.MailLinkName");
59     String imageRoot = MirConfig.getProp("Producer.ImageRoot");
60
61     long                sessionConnectTime = 0;
62     long                startTime = (new java.util.Date()).getTime();
63     String              whereClause = " ";
64     String              orderBy = " ";
65     String              htmlFileName = null;
66     String              currentMediaId;
67     EntityContent       currentContent;
68     EntityList          batchEntityList;
69     HashMap             currentContentValues;
70     SimpleHash          imageHash = new SimpleHash();
71     EntityUsers         userEntity=null;
72     Entity              mediaType;
73     Entity              upMedia;
74     SimpleHash          upMediaSimpleHash;
75     Class               mediaHandlerClass;
76     Class               mediaStorageClass;
77     String              mediaStorageName;
78     String              mediaHandlerName=null;
79     MirMedia            mediaHandler=null;
80     Database            mediaStorage=null;
81
82
83
84     // production of the content-pages
85     orderBy="date desc, webdb_lastchange desc";
86     if(force==true){
87       whereClause="is_published='1'";
88       // if true: produces a single content item
89       if(id !=null){
90         whereClause += " AND id="+id;
91       }
92       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
93     } else {
94       whereClause="is_produced='0' AND is_published='1'";
95       //if true produces a single contentitem
96       if(id !=null){
97         whereClause += " AND id="+id;
98       }
99       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
100     }
101
102     while (batchEntityList != null) {
103       for(int i=0;i<batchEntityList.size();i++) {
104         currentContent = (EntityContent)batchEntityList.elementAt(i);
105         try {
106             currentContentValues = currentContent.getValues();
107
108             //because of postgres 7.1.* not needed anymore
109             //currentContentValues.put("content_data",currentContent.getContentData());
110             String date = (String)currentContentValues.get("date");
111             String year = date.substring(0,4);
112             String month = date.substring(4,6);
113
114             htmlFileName =  producerDocRoot
115               + "/" + year + "/" + month + "/" +  currentContentValues.get("id") + ".shtml";
116
117             currentContentValues.put("content_data",StringUtil.deleteForbiddenTags((String)currentContentValues.get("content_data")));
118             currentContentValues.put("description",StringUtil.deleteForbiddenTags((String)currentContentValues.get("description")));
119
120
121             //create http-links and email-links
122             if (currentContentValues.get("is_html").equals("0")) {
123               String temp = (String)currentContentValues.get("content_data");
124               if(temp!=null && temp.length()>0){
125                 temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
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.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
132                 temp = StringUtil.decodeHTMLinTags(temp);
133                 currentContentValues.put("description",temp);
134               }
135             } else {
136               String temp = (String)currentContentValues.get("content_data");
137               if(temp!=null && temp.length()>0){
138                 temp = StringUtil.decodeHTMLinTags(temp);
139                 currentContentValues.put("content_data",temp);
140               }
141               temp = (String)currentContentValues.get("description");
142               if(temp!=null && temp.length()>0){
143                 temp = StringUtil.decodeHTMLinTags(temp);
144                 currentContentValues.put("description",temp);
145               }
146             }
147
148             //create the freemarker-model
149             SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(currentContentValues);
150
151             /** @todo all the following should take place in EntityContent */
152             // get the uploaded media
153             EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
154             if (currentMediaList!=null && currentMediaList.getCount()>=1) {
155               SimpleList mediaList = new SimpleList();
156               for (int n=0; n < currentMediaList.size();n++) {
157                 upMedia = currentMediaList.elementAt(n);
158                 upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
159                 mediaType = ((EntityMedia)upMedia).getMediaType();
160                 //must be a non-existant to_media_type entry..
161                 if (mediaType != null) {
162                   mediaHandlerName = mediaType.getValue("classname");
163                   mediaStorageName = mediaType.getValue("tablename");
164                   mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
165                   mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
166                   mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
167                   Method m = mediaStorageClass.getMethod("getInstance", null);
168                   mediaStorage = (Database)m.invoke(null, null);
169                   //we most likely need further info
170                   upMedia = mediaStorage.selectById(upMedia.getId());
171                   upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
172                   upMediaSimpleHash.put("type",mediaType.getValue("classname"));
173                   mediaList.add(upMediaSimpleHash);
174                 } //end if media_type != null
175               } //end for
176               mergeData.put("to_media", mediaList);
177             } //end if currentMediaList != null
178
179             // get the comments for the article
180             // and html-ize them
181
182             /** @todo currently this doesn't work
183              *  all this should be moved to Entity, and the parsed description should
184              *  be delivered via entity.get("description_parsed") */
185             SimpleList commentList = currentContent.getComments();
186             if(commentList.isEmpty()==false){
187               while(commentList.hasNext()){
188                 SimpleHash comment = (SimpleHash)commentList.next();
189                 SimpleScalar commentText = (SimpleScalar)comment.get("description");
190                 comment.put("description",new SimpleScalar(StringUtil.createHTML(commentText.getAsString(),imageRoot,mailLinkName,extLinkName,intLinkName)));
191               }
192             }
193             mergeData.put("comments", commentList);
194
195             // get the topics of this article
196             mergeData.put("topics",HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(currentContent)));
197
198             //produce html
199             boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
200             sessionConnectTime = new java.util.Date().getTime() - startTime;
201             if (retVal == true && !"1".equals(currentContent.getValue("is_produced")))
202                 currentContent.setProduced(true);
203         } catch(Exception e) {
204           logHTML(htmlout, "Producer.Content ERROR while producing content ID: " + currentContent.getId()+",skipping it :: "+e.toString());
205           theLog.printError("Producer.Content ERROR while producing content ID: " + currentContent.getId() +",skipping it :: "+e.toString());
206         }
207
208
209       }//for
210
211       if (batchEntityList.hasNextBatch()){
212         batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);
213       } else {
214         batchEntityList=null;
215       }
216     }
217
218     // timing and message to browser
219     sessionConnectTime = new java.util.Date().getTime() - startTime;
220     logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");
221   }
222
223 }
224