small fixx
[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         currentContentValues = currentContent.getValues();
106
107         //because of postgres 7.1.* not needed anymore
108         //currentContentValues.put("content_data",currentContent.getContentData());
109         String date = (String)currentContentValues.get("date");
110         String year = date.substring(0,4);
111         String month = date.substring(4,6);
112
113         htmlFileName =  producerDocRoot
114           + "/" + year + "/" + month + "/" +  currentContentValues.get("id") + ".shtml";
115
116         currentContentValues.put("content_data",StringUtil.deleteForbiddenTags((String)currentContentValues.get("content_data")));
117         currentContentValues.put("description",StringUtil.deleteForbiddenTags((String)currentContentValues.get("description")));
118
119
120         //create http-links and email-links
121         if (currentContentValues.get("is_html").equals("0")) {
122           String temp = (String)currentContentValues.get("content_data");
123           if(temp!=null && temp.length()>0){
124             temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
125             temp = StringUtil.decodeHTMLinTags(temp);
126             currentContentValues.put("content_data",temp);
127           }
128           temp = (String)currentContentValues.get("description");
129           if(temp!=null && temp.length()>0){
130             temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
131             temp = StringUtil.decodeHTMLinTags(temp);
132             currentContentValues.put("description",temp);
133           }
134         } else {
135           String temp = (String)currentContentValues.get("content_data");
136           if(temp!=null && temp.length()>0){
137             temp = StringUtil.decodeHTMLinTags(temp);
138             currentContentValues.put("content_data",temp);
139           }
140           temp = (String)currentContentValues.get("description");
141           if(temp!=null && temp.length()>0){
142             temp = StringUtil.decodeHTMLinTags(temp);
143             currentContentValues.put("description",temp);
144           }
145         }
146
147         //create the freemarker-model
148         SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(currentContentValues);
149
150         // get the uploaded media
151         EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
152         if (currentMediaList!=null && currentMediaList.getCount()>=1) {
153           SimpleList mediaList = new SimpleList();
154           for (int n=0; n < currentMediaList.size();n++) {
155             upMedia = currentMediaList.elementAt(n);
156             upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
157             mediaType = ((EntityUploadedMedia)upMedia).getMediaType();
158             //must be a non-existant to_media_type entry..
159             if (mediaType != null) {
160               try {
161                 mediaHandlerName = mediaType.getValue("classname");
162                 mediaStorageName = mediaType.getValue("tablename");
163                 mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
164                 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
165                 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
166                 if(!mediaStorageName.equals(new String("UploadedMedia"))) {
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                 }
172               } catch (Exception e) {
173                 theLog.printError("ProducerStartpage:problem in reflection: "+mediaHandlerName);
174               } //end catch
175               upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
176               mediaList.add(upMediaSimpleHash);
177             } //end if media_type != null
178           } //end for
179           mergeData.put("to_media", mediaList);
180         } //end if currentMediaList != null
181
182         // get the comments for the article
183         // and html-ize them
184         SimpleList commentList = currentContent.getComments();
185         try{
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         } catch(Exception e){}
194         mergeData.put("comments", commentList);
195
196         // get the topics of this article
197         mergeData.put("topics",HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(currentContent)));
198
199         //produce html
200         boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
201         sessionConnectTime = new java.util.Date().getTime() - startTime;
202         if (retVal == true && !"1".equals(currentContent.getValue("is_produced")))
203             currentContent.setProduced(true);
204       }//while
205
206       if (batchEntityList.hasNextBatch()){
207         batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);
208       } else {
209         batchEntityList=null;
210       }
211     }
212
213     // timing an message to browser
214     sessionConnectTime = new java.util.Date().getTime() - startTime;
215     logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");
216   }
217
218 }
219