e007aad15cacaa86f4c0d1fe7a52d3090cb14773
[mir.git] / source / mircoders / producer / ProducerTopics.java
1 package mircoders.producer;
2
3 import java.io.*;
4 import java.lang.*;
5 import java.lang.reflect.*;
6 import java.util.*;
7 import java.sql.*;
8
9 import freemarker.template.*;
10
11 import mir.misc.*;
12 import mir.media.*;
13 import mir.storage.*;
14 import mir.module.*;
15 import mir.entity.*;
16
17 import mircoders.entity.*;
18 import mircoders.storage.*;
19
20
21
22 public class ProducerTopics extends ProducerList {
23
24   public String where;
25   String              currentMediaId;
26   EntityList          upMediaEntityList;
27   EntityList          imageEntityList;
28   EntityList          currentMediaList;
29   Entity              mediaType;
30   EntityMedia         uploadedMedia;
31   Class               mediaHandlerClass=null;
32   MirMedia            mediaHandler=null;
33   String              mediaHandlerName=null;
34   Database            mediaStorage=null;
35   String              tinyIcon;
36   String              iconAlt;
37
38   public void setAdditional(String key, TemplateModel value) {
39     additional.put(key,value);
40   }
41
42   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
43     throws StorageObjectException, ModuleException {
44     where=id;
45     handle(htmlout,user,force,sync);
46   }
47
48   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)
49     throws StorageObjectException, ModuleException {
50
51     orderBy="date desc, webdb_create desc";
52     listTemplate = MirConfig.getProp("Producer.TopicList.Template");
53
54     EntityList topicsEntityList;
55     if(where==null){
56       topicsEntityList = topicsModule.getByWhereClause("","title", -1);
57     } else {
58       topicsEntityList = topicsModule.getByWhereClause(where,"title", -1);
59     }
60     SimpleList topicsList = HTMLTemplateProcessor.makeSimpleList(topicsEntityList);
61
62     for(int i=0; i < topicsEntityList.size(); i++){
63
64       EntityTopics currentTopic = (EntityTopics)topicsEntityList.elementAt(i);
65       EntityList contentEntityList = DatabaseContentToTopics.getInstance().getContent(currentTopic);
66       String whereClauseSpecial=null;
67
68       if (contentEntityList!=null || force==true) {
69         if (contentEntityList!=null){
70           boolean first=true;
71           whereClause="is_published='1' AND to_article_type >= 1 AND to_article_type <=2 AND id IN (";
72           whereClauseSpecial="is_published='1' AND to_article_type=3 AND id IN (";
73           for(int j=0; j < contentEntityList.size(); j++){
74             if(first==false) {
75               whereClause += ",";
76               whereClauseSpecial += ",";
77             }
78             EntityContent currentContent = (EntityContent)contentEntityList.elementAt(j);
79             whereClause += currentContent.getId();
80             whereClauseSpecial += currentContent.getId();
81
82             setAdditional("topic",currentTopic);
83
84             first = false;
85           }
86           whereClause += ")";
87           whereClauseSpecial += ")";
88         }
89
90         if(contentEntityList==null && force==true){
91           //hihi, das ist eigentlich boese
92           whereClause="is_published='1' AND to_article_type>=1 AND id IN (0)";
93         }
94
95         fileDesc = currentTopic.getValue("filename");
96
97         // get the startarticle
98         EntityList entityList = contentModule.getContent(whereClauseSpecial,"date desc, webdb_create desc",0,1);
99         String currentMediaId = null;
100         SimpleHash imageHash = new SimpleHash();
101         EntityContent currentContent;
102         if(entityList != null && entityList.size()==1){
103           currentContent = (EntityContent)entityList.elementAt(0);
104           SimpleHash specialHash = HTMLTemplateProcessor.makeSimpleHash(currentContent);
105
106           currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
107           if (currentMediaList!=null && currentMediaList.getCount()>=1) {
108             SimpleList mediaListAudio = new SimpleList();
109             SimpleList mediaListImages = new SimpleList();
110             SimpleList mediaListVideo = new SimpleList();
111             SimpleList mediaListOther = new SimpleList();
112             for (int n=0; n < currentMediaList.size();n++) {
113               upMedia = currentMediaList.elementAt(n);
114               upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
115               mediaType = ((EntityMedia)upMedia).getMediaType();
116               //must be a non-existant to_media_type entry..
117               if (mediaType != null) {
118                 try {
119                   mediaHandlerName = mediaType.getValue("classname");
120                   mediaStorageName = mediaType.getValue("tablename");
121                   mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
122                   mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
123                   mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
124                   Method m = mediaStorageClass.getMethod("getInstance", null);
125                   mediaStorage = (Database)m.invoke(null, null);
126                   //we most likely need further info
127                   upMedia = mediaStorage.selectById(upMedia.getId());
128                 } catch (Exception e) {
129                   theLog.printError("ProducerList: problem in reflection: "+mediaHandlerName);
130                 } //end catch
131                 upMediaSimpleHash.put("url", mediaHandler.getListView(upMedia, mediaType));
132                 if (upMedia.getValue("is_published") == "1") {
133                   if (mediaHandler.isImage()) {
134                     mediaListImages.add(upMediaSimpleHash);
135                   } else if (mediaHandler.isAudio()) {
136                     mediaListAudio.add(upMediaSimpleHash);
137                   } else if (mediaHandler.isVideo()) {
138                     mediaListVideo.add(upMediaSimpleHash);
139                   } else {
140                     mediaListOther.add(upMediaSimpleHash);
141                   }
142                 } //end if is_published
143               } //end if media_type != null
144             } //end for
145             try{
146               specialHash.put("to_media_audio", mediaListAudio);
147               specialHash.put("to_media_images", mediaListImages);
148               specialHash.put("to_media_video", mediaListVideo);
149               specialHash.put("to_media_other", mediaListOther);
150             } catch (Exception e){}
151           } //end if currentMediaList != null
152           setAdditional("special",specialHash);
153         }
154
155         //set the list of topics
156         setAdditional("topicslist",topicsList);
157
158         handleIt(htmlout,user,force);
159       }
160     }
161   }
162
163   public static void main(String argv[]){
164     try {
165       new ProducerOpenPosting().handle(new PrintWriter(System.out), null,false, false);
166     } catch(Exception e) {
167       System.err.println(e.toString());
168     }
169   }
170 }