e9fe8ebb64832776b6ef52922ab8241b238b62bb
[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     SimpleHash          imageHash = new SimpleHash();
70     EntityUsers         userEntity=null;
71     Entity              mediaType;
72     Entity              upMedia;
73     SimpleHash          upMediaSimpleHash;
74     Class               mediaHandlerClass;
75     Class               mediaStorageClass;
76     String              mediaStorageName;
77     String              mediaHandlerName=null;
78     MirMedia            mediaHandler=null;
79     Database            mediaStorage=null;
80
81
82
83     // production of the content-pages
84     orderBy="date desc, webdb_lastchange desc";
85     if(force==true){
86       whereClause="is_published='1'";
87       // if true: produces a single content item
88       if(id !=null){
89         whereClause += " AND id="+id;
90       }
91       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
92     } else {
93       whereClause="is_produced='0' AND is_published='1'";
94       //if true produces a single contentitem
95       if(id !=null){
96         whereClause += " AND id="+id;
97       }
98       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
99     }
100
101     while (batchEntityList != null) {
102       for(int i=0;i<batchEntityList.size();i++) {
103         currentContent = (EntityContent)batchEntityList.elementAt(i);
104         HashMap currentContentValues = currentContent.getValues();
105         String date = (String)currentContent.getValue("date");
106         String year = date.substring(0,4);
107         String month = date.substring(4,6);
108
109         htmlFileName =  producerDocRoot
110           + "/" + year + "/" + month + "/" +  currentContent.getValue("id") + ".shtml";
111
112         currentContentValues.put("content_data",StringUtil.deleteForbiddenTags((String)currentContentValues.get("content_data")));
113         currentContentValues.put("description",StringUtil.deleteForbiddenTags((String)currentContentValues.get("description")));
114
115
116         //create http-links and email-links
117         if (currentContentValues.get("is_html").equals("0")) {
118           String temp = (String)currentContentValues.get("content_data");
119           if(temp!=null && temp.length()>0){
120             temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
121             temp = StringUtil.decodeHTMLinTags(temp);
122             currentContentValues.put("content_data",temp);
123           }
124           temp = (String)currentContentValues.get("description");
125           if(temp!=null && temp.length()>0){
126             temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
127             temp = StringUtil.decodeHTMLinTags(temp);
128             currentContentValues.put("description",temp);
129           }
130         } else {
131           String temp = (String)currentContentValues.get("content_data");
132           if(temp!=null && temp.length()>0){
133             temp = StringUtil.decodeHTMLinTags(temp);
134             currentContentValues.put("content_data",temp);
135           }
136           temp = (String)currentContentValues.get("description");
137           if(temp!=null && temp.length()>0){
138             temp = StringUtil.decodeHTMLinTags(temp);
139             currentContentValues.put("description",temp);
140           }
141         }
142
143         //create the freemarker-model
144         SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(currentContentValues);
145
146         // get the uploaded media
147         EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
148         if (currentMediaList!=null && currentMediaList.getCount()>=1) {
149           SimpleList mediaList = new SimpleList();
150           for (int n=0; n < currentMediaList.size();n++) {
151             upMedia = currentMediaList.elementAt(n);
152             upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
153             mediaType = ((EntityMedia)upMedia).getMediaType();
154             //in case it's a non-existant to_media_type entry..
155             if (mediaType != null) {
156               try {
157                 mediaHandlerName = mediaType.getValue("classname");
158                 mediaStorageName = mediaType.getValue("tablename");
159                 mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
160                 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
161                 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
162                 Method m = mediaStorageClass.getMethod("getInstance", null);
163                 mediaStorage = (Database)m.invoke(null, null);
164                 //we most likely need further info
165                 upMedia = mediaStorage.selectById(upMedia.getId());
166               } catch (Exception e) {
167                 theLog.printError("ProducerStartpage:problem in reflection: "+mediaHandlerName);
168               } //end catch
169               upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
170               upMediaSimpleHash.put("type",mediaType.getValue("classname"));
171               mediaList.add(upMediaSimpleHash);
172             } //end if media_type != null
173           } //end for
174           mergeData.put("to_media", mediaList);
175         } //end if currentMediaList != null
176
177         // get the comments for the article
178         // and html-ize them
179         SimpleList commentList = currentContent.getComments();
180         try{
181           if(commentList.isEmpty()==false){
182             while(commentList.hasNext()){
183               SimpleHash comment = (SimpleHash)commentList.next();
184               SimpleScalar commentText = (SimpleScalar)comment.get("description");
185               comment.put("description",new SimpleScalar(StringUtil.createHTML(commentText.getAsString(),imageRoot,mailLinkName,extLinkName,intLinkName)));
186             }
187           }
188         } catch(Exception e){}
189         mergeData.put("comments", commentList);
190
191         // get the topics of this article
192         mergeData.put("topics",HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(currentContent)));
193
194         //produce html
195         boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
196         sessionConnectTime = new java.util.Date().getTime() - startTime;
197         if (retVal == true && !"1".equals(currentContent.getValue("is_produced")))
198             currentContent.setProduced(true);
199       }//while
200
201       if (batchEntityList.hasNextBatch()){
202         batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);
203       } else {
204         batchEntityList=null;
205       }
206     }
207
208     // timing an message to browser
209     sessionConnectTime = new java.util.Date().getTime() - startTime;
210     logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");
211   }
212
213 }
214