moving breaking / imclist to modules
[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 newswire
86     /** @todo same with newswire, should be fetched via moduleContent.getNewswire() */
87     whereClause="is_published=true AND to_article_type = 1";
88     entityList = contentModule.getContent(whereClause,"date desc, webdb_create desc",0,newsPerPage);
89     SimpleList newsWireList = HTMLTemplateProcessor.makeSimpleList(entityList);
90     for (int i=0; i < entityList.size();i++) {
91       currentContent = (EntityContent)entityList.elementAt(i);
92       try {
93           /** @todo all this should be done inside EntityContent and on demand */
94           //fetching/setting the images
95           upMediaEntityList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
96           if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
97             tinyIcon = null;
98             iconAlt = null;
99             mediaHandler = null;
100             mediaHandlerName = null;
101             for (int n=0; n < upMediaEntityList.size();n++) {
102               uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
103               mediaType = uploadedMedia.getMediaType();
104
105               //must of had a non-existant to_media_type entry..
106               //let's save our ass.
107               if (mediaType != null) {
108                   mediaHandlerName = mediaType.getValue("classname");
109                   mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
110                   mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
111
112                   //the "best" media type to show
113                   if (mediaHandler.isVideo()) {
114                     tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
115                     iconAlt = "Video";
116                     break;
117                   } else if (mediaHandler.isAudio()) {
118                     tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
119                     iconAlt = "Audio";
120                   } else if (tinyIcon == null && !mediaHandler.isImage()) {
121                     tinyIcon = mediaHandler.getTinyIcon();
122                     iconAlt = mediaHandler.getIconAlt();
123                   }
124               }
125             }
126             //it only has image(s)
127             if (tinyIcon == null) {
128               tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
129               iconAlt = "Image";
130             }
131
132           // uploadedMedia Entity list is empty.
133           // we only have text
134           } else {
135             tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
136             iconAlt = "Text";
137           }
138
139           //mediaList = HTMLTemplateProcessor.makeSimpleList(upMediaEntityList);
140           contentHash = (SimpleHash)newsWireList.get(i);
141           contentHash.put("tiny_icon", imageRoot+"/"+tinyIcon);
142           contentHash.put("icon_alt", iconAlt);
143       } catch (Exception e) {
144         logHTML(htmlout, "Producer.StartPage error id: " + currentContent.getId() + ", skipping");
145         theLog.printError("Producer.StartPage error id: " + currentContent.getId() + ", skipping"+e.toString());
146       }
147     }
148     theLog.printDebugInfo("############### got newswire");
149
150     // get the startarticle and the related images
151     /** @todo this should move to moduleContent.getStartArticle */
152     whereClause="is_published=true AND to_article_type=4";
153     entityList = contentModule.getContent(whereClause,"date desc, webdb_create desc",0,1);
154     //if no startspecial exists
155     if (entityList==null || entityList.size()==0){
156       whereClause="is_published=true AND to_article_type=3";
157       entityList = contentModule.getContent(whereClause,"date desc, webdb_create desc",0,1);
158     }
159     SimpleList startItemList = HTMLTemplateProcessor.makeSimpleList(entityList);
160     for (int k=0; k < entityList.size();k++) {
161       currentContent = (EntityContent)entityList.elementAt(k);
162       try {
163           //media to content
164           currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
165           contentHash = (SimpleHash)startItemList.get(k);
166           /** @todo this should move to Entity */
167                                         if (currentMediaList!=null && currentMediaList.getCount()>=1) {
168             SimpleList mediaListAudio = new SimpleList();
169             SimpleList mediaListImages = new SimpleList();
170             SimpleList mediaListVideo = new SimpleList();
171             SimpleList mediaListOther = new SimpleList();
172             for (int n=0; n < currentMediaList.size();n++) {
173               upMedia = currentMediaList.elementAt(n);
174               upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
175               mediaType = ((EntityMedia)upMedia).getMediaType();
176               //must be a non-existant to_media_type entry..
177               if (mediaType != null) {
178                 mediaHandlerName = mediaType.getValue("classname");
179                 mediaStorageName = mediaType.getValue("tablename");
180                 mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
181                 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
182                 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
183                 Method m = mediaStorageClass.getMethod("getInstance", null);
184                 mediaStorage = (Database)m.invoke(null, null);
185                 //we most likely need further info
186                 upMedia = mediaStorage.selectById(upMedia.getId());
187                 upMediaSimpleHash.put("url", mediaHandler.getListView(upMedia, mediaType));
188                 if (upMedia.getValue("is_published").equals("1")) {
189                   if (mediaHandler.isImage()) {
190                     mediaListImages.add(upMediaSimpleHash);
191                   } else if (mediaHandler.isAudio()) {
192                     mediaListAudio.add(upMediaSimpleHash);
193                   } else if (mediaHandler.isVideo()) {
194                     mediaListVideo.add(upMediaSimpleHash);
195                   } else {
196                     mediaListOther.add(upMediaSimpleHash);
197                   }
198                 } //end if is_published
199               } //end if media_type != null
200             } //end for
201             contentHash.put("to_media_audio", mediaListAudio);
202             contentHash.put("to_media_images", mediaListImages);
203             contentHash.put("to_media_video", mediaListVideo);
204             contentHash.put("to_media_other", mediaListOther);
205           } //end if currentMediaList != null
206                                         //convert to html
207                                         if ( ((SimpleScalar)contentHash.get("is_html")).getAsString().equals("0") ) {
208                                                 SimpleScalar tempScalar = (SimpleScalar)contentHash.get("description");
209                                                 String temp = StringUtil.createHTML(tempScalar.getAsString(),imageRoot,mailLinkName,extLinkName,intLinkName);
210                                                 temp = StringUtil.decodeHTMLinTags(temp);
211                                                 contentHash.put("description",temp);
212                                         }
213
214                         } catch (Exception e) {
215         logHTML(htmlout, "Producer.StartPage error id: " + currentContent.getId() + ", skipping");
216         theLog.printError("Producer.StartPage error id: " + currentContent.getId() + ", skipping"+e.toString());
217       }
218     } //enf for featurueList.size..
219
220     theLog.printDebugInfo("############### got startitems");
221
222
223
224     // get the articles
225     /** @todo moduleContent.getArticles() */
226     whereClause="is_published=true AND to_article_type=2";
227     orderBy="date desc, webdb_create desc";
228     entityList = contentModule.getContent(whereClause, orderBy, 0, itemsPerPage);
229     SimpleList featureList = HTMLTemplateProcessor.makeSimpleList(entityList);
230     for (int k=0; k < entityList.size();k++) {
231       currentContent = (EntityContent)entityList.elementAt(k);
232       try {
233           //media to content
234           currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
235           theLog.printDebugInfo("############### getting contenthash");
236           contentHash = (SimpleHash)featureList.get(k);
237           theLog.printDebugInfo("############### building media lists");
238                                         if (currentMediaList!=null && currentMediaList.getCount()>=1) {
239             SimpleList mediaListAudio = new SimpleList();
240             SimpleList mediaListImages = new SimpleList();
241             SimpleList mediaListVideo = new SimpleList();
242             SimpleList mediaListOther = new SimpleList();
243             for (int n=0; n < currentMediaList.size();n++) {
244               upMedia = currentMediaList.elementAt(n);
245               upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
246               mediaType = ((EntityMedia)upMedia).getMediaType();
247               //must be a non-existant to_media_type entry..
248               if (mediaType != null) {
249                 mediaHandlerName = mediaType.getValue("classname");
250                 mediaStorageName = mediaType.getValue("tablename");
251                 mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
252                 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
253                 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
254                 Method m = mediaStorageClass.getMethod("getInstance", null);
255                 mediaStorage = (Database)m.invoke(null, null);
256                 //we most likely need further info
257                 upMedia = mediaStorage.selectById(upMedia.getId());
258                 upMediaSimpleHash.put("url", mediaHandler.getListView(upMedia, mediaType));
259
260                 if (upMedia.getValue("is_published").equals("1")) {
261                   if (mediaHandler.isImage()) {
262                     mediaListImages.add(upMediaSimpleHash);
263                   } else if (mediaHandler.isAudio()) {
264                     mediaListAudio.add(upMediaSimpleHash);
265                   } else if (mediaHandler.isVideo()) {
266                     mediaListVideo.add(upMediaSimpleHash);
267                   } else {
268                     mediaListOther.add(upMediaSimpleHash);
269                   }
270                 } //end if is_published
271               } //end if media_type != null
272             } //end for
273             contentHash.put("to_media_audio", mediaListAudio);
274             contentHash.put("to_media_images", mediaListImages);
275             contentHash.put("to_media_video", mediaListVideo);
276             contentHash.put("to_media_other", mediaListOther);
277           } //end if currentMediaList != null
278
279                                         //convert to html
280           theLog.printDebugInfo("############### html conversion");
281                                         if ( ((SimpleScalar)contentHash.get("is_html")).getAsString().equals("0") ) {
282                                                 SimpleScalar tempScalar = (SimpleScalar)contentHash.get("description");
283                                                 String temp = StringUtil.createHTML(tempScalar.getAsString(),imageRoot,mailLinkName,extLinkName,intLinkName);
284                                                 temp = StringUtil.decodeHTMLinTags(temp);
285                                                 contentHash.put("description",temp);
286                                         }
287                         } catch (Exception e) {
288         logHTML(htmlout, "Producer.StartPage error id: " + currentContent.getId() + ", skipping");
289         theLog.printError("Producer.StartPage error id: " + currentContent.getId() + ", skipping"+e.toString());
290       }
291     } //enf for featurueList.size..
292     theLog.printDebugInfo("############### got featurelist");
293
294     /** @todo the following should be the only thing left done here. a lot of redundancies are gone then,
295      *  code is more readable, and caching stuff, mass changes, etc. can be done in one spot */
296
297     // Additional Information
298     startPageModel = new SimpleHash();
299
300     // breaking news
301     ModuleBreaking breakingModule = new ModuleBreaking(DatabaseBreaking.getInstance());
302     startPageModel.put("breakingnews", breakingModule.getBreakingNews());
303
304     startPageModel.put("topics", topicsModule.getTopicsList());
305     startPageModel.put("newswire", newsWireList);
306     startPageModel.put("startspecial", startItemList);
307     startPageModel.put("features", featureList);
308
309     htmlFileName = producerDocRoot + "/index.shtml";
310
311     produce(startPageTemplate, htmlFileName, startPageModel, htmlout);
312
313     // Finish
314     sessionConnectTime = new java.util.Date().getTime() - startTime;
315     logHTML(htmlout, "Producer.Startseite finished: " + sessionConnectTime + " ms.");
316     if(sync==true){
317       Helper.rsync();
318       logHTML(htmlout, "Producer.Startseite: rsync done");
319     }
320   }
321 }
322