bugfixx: When a user selects topics in an open posting, the applicable topic pages...
[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",HTMLTemplateProcessor.makeSimpleHash(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           try {
105               SimpleHash specialHash = HTMLTemplateProcessor.makeSimpleHash(currentContent);
106
107               currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
108               if (currentMediaList!=null && currentMediaList.getCount()>=1) {
109                 SimpleList mediaListAudio = new SimpleList();
110                 SimpleList mediaListImages = new SimpleList();
111                 SimpleList mediaListVideo = new SimpleList();
112                 SimpleList mediaListOther = new SimpleList();
113                 for (int n=0; n < currentMediaList.size();n++) {
114                   upMedia = currentMediaList.elementAt(n);
115                   upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
116                   mediaType = ((EntityMedia)upMedia).getMediaType();
117                   //must be a non-existant to_media_type entry..
118                   if (mediaType != null) {
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                     upMediaSimpleHash.put("url", mediaHandler.getListView(upMedia, mediaType));
129                     if (upMedia.getValue("is_published") == "1") {
130                       if (mediaHandler.isImage()) {
131                         mediaListImages.add(upMediaSimpleHash);
132                       } else if (mediaHandler.isAudio()) {
133                         mediaListAudio.add(upMediaSimpleHash);
134                       } else if (mediaHandler.isVideo()) {
135                         mediaListVideo.add(upMediaSimpleHash);
136                       } else {
137                         mediaListOther.add(upMediaSimpleHash);
138                       }
139                     } //end if is_published
140                   } //end if media_type != null
141                 } //end for
142                 specialHash.put("to_media_audio", mediaListAudio);
143                 specialHash.put("to_media_images", mediaListImages);
144                 specialHash.put("to_media_video", mediaListVideo);
145                 specialHash.put("to_media_other", mediaListOther);
146               } //end if currentMediaList != null
147                                                         //convert to html
148                                                         if ( ((SimpleScalar)specialHash.get("is_html")).getAsString().equals("0") ) {
149                                                                 SimpleScalar temp = (SimpleScalar)specialHash.get("description");
150                                                                 specialHash.put("description",StringUtil.createHTML(temp.getAsString()));
151                                                         }
152               setAdditional("special",specialHash);
153           } catch (Exception e) {
154             theLog.printError("ProducerTopics: problem with start special: "+currentContent.getId()+" "+e.toString());
155             logHTML(htmlout,"ProducerTopics: problem with start special: "+currentContent.getId()+" "+e.toString());
156           }
157         }
158         
159         //set the list of topics
160         setAdditional("topicslist",topicsList);
161
162         handleIt(htmlout,user,force);
163       }
164     }
165   }
166
167   public static void main(String argv[]){
168     try {
169       new ProducerOpenPosting().handle(new PrintWriter(System.out), null,false, false);
170     } catch(Exception e) {
171       System.err.println(e.toString());
172     }
173   }
174 }