1 package mircoders.producer;
5 import java.lang.reflect.*;
8 import freemarker.template.*;
16 import mircoders.module.*;
17 import mircoders.storage.*;
18 import mircoders.entity.*;
21 public class ProducerStartPage extends Producer {
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"));
28 SimpleHash upMediaSimpleHash;
29 Class mediaHandlerClass;
30 Class mediaStorageClass;
31 String mediaStorageName;
35 public static void main(String argv[]){
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());
47 public void handle(PrintWriter htmlout, EntityUsers user, boolean force,boolean sync)
48 throws StorageObjectException, ModuleException
50 printHTML(htmlout, "Producer.StartPage: started");
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");
57 long sessionConnectTime = 0;
58 long startTime = (new java.util.Date()).getTime();
59 String nowWebdbDate = StringUtil.date2webdbDate(new GregorianCalendar());
62 FileWriter outputFile;
64 EntityContent currentContent;
65 EntityList entityList;
66 SimpleHash startPageModel;
67 SimpleList contentList;
68 String currentMediaId;
69 EntityList upMediaEntityList;
70 EntityList imageEntityList;
71 EntityList currentMediaList;
73 EntityMedia uploadedMedia;
74 Class mediaHandlerClass=null;
75 MirMedia mediaHandler=null;
76 String mediaHandlerName=null;
77 Database mediaStorage=null;
80 Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home") + "/" +MirConfig.getProp("Producer.Logfile"));
83 SimpleHash contentHash;
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);
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);
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) {
107 mediaHandlerName = null;
108 for (int n=0; n < upMediaEntityList.size();n++) {
109 uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
110 mediaType = uploadedMedia.getMediaType();
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();
119 //the "best" media type to show
120 if (mediaHandler.isVideo()) {
121 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
124 } else if (mediaHandler.isAudio()) {
125 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
127 } else if (tinyIcon == null && !mediaHandler.isImage()) {
128 tinyIcon = mediaHandler.getTinyIcon();
129 iconAlt = mediaHandler.getIconAlt();
133 //it only has image(s)
134 if (tinyIcon == null) {
135 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
139 // uploadedMedia Entity list is empty.
142 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
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());
155 theLog.printDebugInfo("############### got newswire");
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);
166 SimpleList startItemList = HTMLTemplateProcessor.makeSimpleList(entityList);
167 for (int k=0; k < entityList.size();k++) {
168 currentContent = (EntityContent)entityList.elementAt(k);
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);
203 mediaListOther.add(upMediaSimpleHash);
205 } //end if is_published
206 } //end if media_type != null
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
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);
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());
225 } //enf for featurueList.size..
227 theLog.printDebugInfo("############### got startitems");
229 // get the breaking news
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");
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);
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);
281 mediaListOther.add(upMediaSimpleHash);
283 } //end if is_published
284 } //end if media_type != null
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
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);
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());
304 } //enf for featurueList.size..
305 theLog.printDebugInfo("############### got featurelist");
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 */
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);
320 htmlFileName = producerDocRoot + "/index.shtml";
322 produce(startPageTemplate, htmlFileName, startPageModel, htmlout);
325 sessionConnectTime = new java.util.Date().getTime() - startTime;
326 logHTML(htmlout, "Producer.Startseite finished: " + sessionConnectTime + " ms.");
329 logHTML(htmlout, "Producer.Startseite: rsync done");