*** empty log message ***
[mir.git] / source / mircoders / producer / ProducerStartPage.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.module.*;
17 import mircoders.storage.*;
18 import mircoders.entity.*;
19
20
21 public class ProducerStartPage extends Producer {
22
23   private static String startPageTemplate = MirConfig.getProp("Producer.StartPage.Template");
24   private static int itemsPerPage = Integer.parseInt(MirConfig.getProp("Producer.StartPage.Items"));
25   private static int newsPerPage = Integer.parseInt(MirConfig.getProp("Producer.StartPage.Newswire"));
26   Entity              mediaType;
27   Entity              upMedia;
28   SimpleHash          upMediaSimpleHash;
29   Class               mediaHandlerClass;
30   Class               mediaStorageClass;
31   String              mediaStorageName;
32
33
34
35   public static void main(String argv[]){
36     try {
37       // Why are we reloading the configuration here?
38       // is there something I'm missing?
39       // mh. <heckmann@hbe.ca>
40       // Configuration.initConfig(argv[0]);
41       new ProducerStartPage().handle(new PrintWriter(System.out), null);
42     } catch(Exception e) {
43       System.err.println(e.toString());
44     }
45   }
46
47   public void handle(PrintWriter htmlout, EntityUsers user, boolean force,boolean sync)
48     throws StorageObjectException, ModuleException
49   {
50     printHTML(htmlout, "Producer.StartPage: started");
51
52     String extLinkName = MirConfig.getProp("Producer.ExtLinkName");
53     String intLinkName = MirConfig.getProp("Producer.IntLinkName");
54     String mailLinkName = MirConfig.getProp("Producer.MailLinkName");
55     String imageRoot = MirConfig.getProp("Producer.ImageRoot");
56
57     long                sessionConnectTime = 0;
58     long                startTime = (new java.util.Date()).getTime();
59     String              nowWebdbDate = StringUtil.date2webdbDate(new GregorianCalendar());
60     String              whereClause;
61     String              orderBy;
62     FileWriter          outputFile;
63     String              htmlFileName;
64     EntityContent       currentContent;
65     EntityList          entityList;
66     SimpleHash          startPageModel;
67     SimpleList          contentList;
68     String              currentMediaId;
69     EntityList          upMediaEntityList;
70     EntityList          imageEntityList;
71     EntityList          currentMediaList;
72     Entity              mediaType;
73     EntityMedia         uploadedMedia;
74     Class               mediaHandlerClass=null;
75     MirMedia            mediaHandler=null;
76     String              mediaHandlerName=null;
77     Database            mediaStorage=null;
78     String              tinyIcon;
79     String              iconAlt;
80     Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home") + "/" +MirConfig.getProp("Producer.Logfile"));
81
82     SimpleList mediaList;
83     SimpleHash contentHash;
84
85     // get the imclinks
86     /** @todo don't we want to preproduce and include the imcs? */
87     entityList = linksImcsModule.getByWhereClause("", "sortpriority, title", -1);
88     SimpleList imcList = HTMLTemplateProcessor.makeSimpleList(entityList);
89     EntityList theParentList = linksImcsModule.getByWhereClause("to_parent_id=NULL", "sortpriority, title", -1);
90     SimpleList parentList = HTMLTemplateProcessor.makeSimpleList(theParentList);
91
92     // get the newswire
93     /** @todo same with newswire, should be fetched via moduleContent.getNewswire() */
94     whereClause="is_published=true AND to_article_type = 1";
95     entityList = contentModule.getContent(whereClause,"date desc, webdb_create desc",0,newsPerPage);
96     SimpleList newsWireList = HTMLTemplateProcessor.makeSimpleList(entityList);
97     for (int i=0; i < entityList.size();i++) {
98       currentContent = (EntityContent)entityList.elementAt(i);
99       try {
100           /** @todo all this should be done inside EntityContent and on demand */
101           //fetching/setting the images
102           upMediaEntityList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
103           if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
104             tinyIcon = null;
105             iconAlt = null;
106             mediaHandler = null;
107             mediaHandlerName = null;
108             for (int n=0; n < upMediaEntityList.size();n++) {
109               uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
110               mediaType = uploadedMedia.getMediaType();
111
112               //must of had a non-existant to_media_type entry..
113               //let's save our ass.
114               if (mediaType != null) {
115                   mediaHandlerName = mediaType.getValue("classname");
116                   mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
117                   mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
118
119                   //the "best" media type to show
120                   if (mediaHandler.isVideo()) {
121                     tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
122                     iconAlt = "Video";
123                     break;
124                   } else if (mediaHandler.isAudio()) {
125                     tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
126                     iconAlt = "Audio";
127                   } else if (tinyIcon == null && !mediaHandler.isImage()) {
128                     tinyIcon = mediaHandler.getTinyIcon();
129                     iconAlt = mediaHandler.getIconAlt();
130                   }
131               }
132             }
133             //it only has image(s)
134             if (tinyIcon == null) {
135               tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
136               iconAlt = "Image";
137             }
138
139           // uploadedMedia Entity list is empty.
140           // we only have text
141           } else {
142             tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
143             iconAlt = "Text";
144           }
145
146           //mediaList = HTMLTemplateProcessor.makeSimpleList(upMediaEntityList);
147           contentHash = (SimpleHash)newsWireList.get(i);
148           contentHash.put("tiny_icon", imageRoot+"/"+tinyIcon);
149           contentHash.put("icon_alt", iconAlt);
150       } catch (Exception e) {
151         logHTML(htmlout, "Producer.StartPage error id: " + currentContent.getId() + ", skipping");
152         theLog.printError("Producer.StartPage error id: " + currentContent.getId() + ", skipping"+e.toString());
153       }
154     }
155     theLog.printDebugInfo("############### got newswire");
156
157     // get the startarticle and the related images
158     /** @todo this should move to moduleContent.getStartArticle */
159     whereClause="is_published=true AND to_article_type=4";
160     entityList = contentModule.getContent(whereClause,"date desc, webdb_create desc",0,1);
161     //if no startspecial exists
162     if (entityList==null || entityList.size()==0){
163       whereClause="is_published=true AND to_article_type=3";
164       entityList = contentModule.getContent(whereClause,"date desc, webdb_create desc",0,1);
165     }
166     SimpleList startItemList = HTMLTemplateProcessor.makeSimpleList(entityList);
167     for (int k=0; k < entityList.size();k++) {
168       currentContent = (EntityContent)entityList.elementAt(k);
169       try {
170           //media to content
171           currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
172           contentHash = (SimpleHash)startItemList.get(k);
173           /** @todo this should move to Entity */
174                                         if (currentMediaList!=null && currentMediaList.getCount()>=1) {
175             SimpleList mediaListAudio = new SimpleList();
176             SimpleList mediaListImages = new SimpleList();
177             SimpleList mediaListVideo = new SimpleList();
178             SimpleList mediaListOther = new SimpleList();
179             for (int n=0; n < currentMediaList.size();n++) {
180               upMedia = currentMediaList.elementAt(n);
181               upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
182               mediaType = ((EntityMedia)upMedia).getMediaType();
183               //must be a non-existant to_media_type entry..
184               if (mediaType != null) {
185                 mediaHandlerName = mediaType.getValue("classname");
186                 mediaStorageName = mediaType.getValue("tablename");
187                 mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
188                 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
189                 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
190                 Method m = mediaStorageClass.getMethod("getInstance", null);
191                 mediaStorage = (Database)m.invoke(null, null);
192                 //we most likely need further info
193                 upMedia = mediaStorage.selectById(upMedia.getId());
194                 upMediaSimpleHash.put("url", mediaHandler.getListView(upMedia, mediaType));
195                 if (upMedia.getValue("is_published") == "1") {
196                   if (mediaHandler.isImage()) {
197                     mediaListImages.add(upMediaSimpleHash);
198                   } else if (mediaHandler.isAudio()) {
199                     mediaListAudio.add(upMediaSimpleHash);
200                   } else if (mediaHandler.isVideo()) {
201                     mediaListVideo.add(upMediaSimpleHash);
202                   } else {
203                     mediaListOther.add(upMediaSimpleHash);
204                   }
205                 } //end if is_published
206               } //end if media_type != null
207             } //end for
208             contentHash.put("to_media_audio", mediaListAudio);
209             contentHash.put("to_media_images", mediaListImages);
210             contentHash.put("to_media_video", mediaListVideo);
211             contentHash.put("to_media_other", mediaListOther);
212           } //end if currentMediaList != null
213                                         //convert to html
214                                         if ( ((SimpleScalar)contentHash.get("is_html")).getAsString().equals("0") ) {
215                                                 SimpleScalar tempScalar = (SimpleScalar)contentHash.get("description");
216                                                 String temp = StringUtil.createHTML(tempScalar.getAsString(),imageRoot,mailLinkName,extLinkName,intLinkName);
217                                                 temp = StringUtil.decodeHTMLinTags(temp);
218                                                 contentHash.put("description",temp);
219                                         }
220
221                         } catch (Exception e) {
222         logHTML(htmlout, "Producer.StartPage error id: " + currentContent.getId() + ", skipping");
223         theLog.printError("Producer.StartPage error id: " + currentContent.getId() + ", skipping"+e.toString());
224       }
225     } //enf for featurueList.size..
226
227     theLog.printDebugInfo("############### got startitems");
228
229     // get the breaking news
230     // only the first 5
231     // todo: the number of breaking_news items have to be configurable
232     ModuleBreaking breakingModule = new ModuleBreaking(DatabaseBreaking.getInstance());
233     entityList = breakingModule.getByWhereClause(null,"webdb_create desc",0,5);
234     SimpleList breakingList = HTMLTemplateProcessor.makeSimpleList(entityList);
235     theLog.printDebugInfo("############### got breaking");
236
237     // get the articles
238     /** @todo moduleContent.getArticles() */
239     whereClause="is_published=true AND to_article_type=2";
240     orderBy="date desc, webdb_create desc";
241     entityList = contentModule.getContent(whereClause, orderBy, 0, itemsPerPage);
242     SimpleList featureList = HTMLTemplateProcessor.makeSimpleList(entityList);
243     for (int k=0; k < entityList.size();k++) {
244       currentContent = (EntityContent)entityList.elementAt(k);
245       try {
246           //media to content
247           currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
248           theLog.printDebugInfo("############### getting contenthash");
249           contentHash = (SimpleHash)featureList.get(k);
250           theLog.printDebugInfo("############### building media lists");
251                                         if (currentMediaList!=null && currentMediaList.getCount()>=1) {
252             SimpleList mediaListAudio = new SimpleList();
253             SimpleList mediaListImages = new SimpleList();
254             SimpleList mediaListVideo = new SimpleList();
255             SimpleList mediaListOther = new SimpleList();
256             for (int n=0; n < currentMediaList.size();n++) {
257               upMedia = currentMediaList.elementAt(n);
258               upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
259               mediaType = ((EntityMedia)upMedia).getMediaType();
260               //must be a non-existant to_media_type entry..
261               if (mediaType != null) {
262                 mediaHandlerName = mediaType.getValue("classname");
263                 mediaStorageName = mediaType.getValue("tablename");
264                 mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
265                 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
266                 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
267                 Method m = mediaStorageClass.getMethod("getInstance", null);
268                 mediaStorage = (Database)m.invoke(null, null);
269                 //we most likely need further info
270                 upMedia = mediaStorage.selectById(upMedia.getId());
271                 upMediaSimpleHash.put("url", mediaHandler.getListView(upMedia, mediaType));
272                 /** @todo String comparison here should never work .equals()  !!!! */
273                 if (upMedia.getValue("is_published") == "1") {
274                   if (mediaHandler.isImage()) {
275                     mediaListImages.add(upMediaSimpleHash);
276                   } else if (mediaHandler.isAudio()) {
277                     mediaListAudio.add(upMediaSimpleHash);
278                   } else if (mediaHandler.isVideo()) {
279                     mediaListVideo.add(upMediaSimpleHash);
280                   } else {
281                     mediaListOther.add(upMediaSimpleHash);
282                   }
283                 } //end if is_published
284               } //end if media_type != null
285             } //end for
286             contentHash.put("to_media_audio", mediaListAudio);
287             contentHash.put("to_media_images", mediaListImages);
288             contentHash.put("to_media_video", mediaListVideo);
289             contentHash.put("to_media_other", mediaListOther);
290           } //end if currentMediaList != null
291
292                                         //convert to html
293           theLog.printDebugInfo("############### html conversion");
294                                         if ( ((SimpleScalar)contentHash.get("is_html")).getAsString().equals("0") ) {
295                                                 SimpleScalar tempScalar = (SimpleScalar)contentHash.get("description");
296                                                 String temp = StringUtil.createHTML(tempScalar.getAsString(),imageRoot,mailLinkName,extLinkName,intLinkName);
297                                                 temp = StringUtil.decodeHTMLinTags(temp);
298                                                 contentHash.put("description",temp);
299                                         }
300                         } catch (Exception e) {
301         logHTML(htmlout, "Producer.StartPage error id: " + currentContent.getId() + ", skipping");
302         theLog.printError("Producer.StartPage error id: " + currentContent.getId() + ", skipping"+e.toString());
303       }
304     } //enf for featurueList.size..
305     theLog.printDebugInfo("############### got featurelist");
306
307     /** @todo the following should be the only thing left done here. a lot of redundancies are gone then,
308      *  code is more readable, and caching stuff, mass changes, etc. can be done in one spot */
309
310     // Additional Information
311     startPageModel = new SimpleHash();
312     startPageModel.put("breakingnews", breakingList);
313     startPageModel.put("topics", topicsModule.getTopicsList());
314     startPageModel.put("imclist", imcList);
315     startPageModel.put("parentlist", parentList);
316     startPageModel.put("newswire", newsWireList);
317     startPageModel.put("startspecial", startItemList);
318     startPageModel.put("features", featureList);
319
320     htmlFileName = producerDocRoot + "/index.shtml";
321
322     produce(startPageTemplate, htmlFileName, startPageModel, htmlout);
323
324     // Finish
325     sessionConnectTime = new java.util.Date().getTime() - startTime;
326     logHTML(htmlout, "Producer.Startseite finished: " + sessionConnectTime + " ms.");
327     if(sync==true){
328       Helper.rsync();
329       logHTML(htmlout, "Producer.Startseite: rsync done");
330     }
331   }
332 }
333