ProducerList is now converted to the new way to handle media.
[mir.git] / source / mircoders / producer / ProducerList.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.module.*;
18 import mircoders.entity.*;
19 import mircoders.storage.*;
20
21
22
23 abstract public class ProducerList extends Producer {
24
25   public String listTemplate;
26   public String whereClause;
27   public String orderBy;
28   public String fileDesc;
29   protected HashMap additional = new HashMap();
30   Entity              mediaType;
31   Entity              upMedia;
32   SimpleHash          upMediaSimpleHash;
33   Class               mediaHandlerClass;
34   Class               mediaStorageClass;
35   String              mediaStorageName;
36   String              mediaHandlerName=null;
37   MirMedia            mediaHandler=null;
38   Database            mediaStorage=null;
39
40
41
42   public void handle(PrintWriter htmlout, EntityUsers user, boolean sync, boolean force)
43     throws StorageObjectException, ModuleException {
44     handleIt(htmlout,user,force);
45   }
46
47   public void handleIt(PrintWriter htmlout, EntityUsers user, boolean force)
48     throws StorageObjectException, ModuleException {
49
50     logHTML(htmlout, "Producer.List: started");
51     int newsPerPage = Integer.parseInt(MirConfig.getProp("Producer.StartPage.Newswire"));
52     long                sessionConnectTime = 0;
53     long                startTime = (new java.util.Date()).getTime();
54     String              htmlFileName = "";
55     String              currentMediaId;
56     EntityContent       currentContent;
57     EntityList          list;
58     EntityUsers         userEntity=null;
59     SimpleHash          imageHash = new SimpleHash();
60     int size = 0;
61     int listSize = 0;
62
63     int maxItemsOnPage = Integer.parseInt(MirConfig.getProp("Lists.Max.Items"));
64
65     try {
66       listSize = contentModule.getSize(whereClause);
67     } catch (Exception e) {
68       logHTML(htmlout,e.toString());
69     }
70
71     int modRest = listSize % maxItemsOnPage;
72     int numberOfPages = (listSize - modRest) / maxItemsOnPage;
73     boolean first=true;
74     for (int i = 0;i < numberOfPages+1;i ++) {
75       //break the loop, if only athe actuell pages should be produced
76       if (force == false && i==3) {
77         break;
78       }
79       //break, if only the first page has to be produced
80       if (force == false && modRest != 0 && first==false){
81         break;
82       }
83
84
85       if (first==true) {
86         //get the data for the first page
87         size=maxItemsOnPage + modRest;
88         list = contentModule.getContent(whereClause, orderBy, 0, size, userEntity);
89         first=false;
90       } else {
91         //get the data for the other pages
92         list = contentModule.getContent(whereClause, orderBy, size, maxItemsOnPage, userEntity);
93         size = size + maxItemsOnPage;
94       }
95
96       //now produce the pages
97       if (list!=null || force==true) {
98         SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(list);
99         //first we try to get the images
100
101         if(list!=null){
102           for (int k=0; k < list.size();k++) {
103             currentContent = (EntityContent)list.elementAt(k);
104             //images to content
105             EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
106             if (currentMediaList!=null && currentMediaList.getCount()>=1) {
107               SimpleList mediaListAudio = new SimpleList();
108               SimpleList mediaListImages = new SimpleList();
109               SimpleList mediaListVideo = new SimpleList();
110               SimpleList mediaListOther = new SimpleList();
111               //SimpleHash allMediaSimpleHash = new SimpleHash();
112               for (int n=0; n < currentMediaList.size();n++) {
113                 upMedia = currentMediaList.elementAt(n);
114                 upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
115                 mediaType = ((EntityUploadedMedia)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                     if(!mediaStorageName.equals(new String("UploadedMedia"))) {
125                       Method m = mediaStorageClass.getMethod("getInstance", null);
126                       mediaStorage = (Database)m.invoke(null, null);
127                       //we most likely need further info
128                       upMedia = mediaStorage.selectById(upMedia.getId());
129                     }
130                   } catch (Exception e) {
131                     theLog.printError("ProducerList: problem in reflection: "+mediaHandlerName);
132                   } //end catch
133                   upMediaSimpleHash.put("url", mediaHandler.getListView(upMedia, mediaType));
134                   if (upMedia.getValue("is_published") == "1") {
135                       if (mediaHandler.isImage()) {
136                         mediaListImages.add(upMediaSimpleHash);
137                       } else if (mediaHandler.isAudio()) {
138                         mediaListAudio.add(upMediaSimpleHash);
139                       } else if (mediaHandler.isVideo()) {
140                         mediaListVideo.add(upMediaSimpleHash);
141                       } else {
142                         mediaListOther.add(upMediaSimpleHash);
143                       }
144                   } //end if is_published
145                 } //end if media_type != null
146               } //end for
147               try{
148                 SimpleList contentList = (SimpleList)mergeData.get("contentlist");
149                 SimpleHash contentHash = (SimpleHash)contentList.get(k);
150                 contentHash.put("to_media_audio", mediaListAudio);
151                 contentHash.put("to_media_images", mediaListImages);
152                 contentHash.put("to_media_video", mediaListVideo);
153                 contentHash.put("to_media_other", mediaListOther);
154               } catch (Exception e){}
155             } //end if currentMediaList != null
156
157             //content to html
158             if(currentContent.getValue("is_html").equals("0")){
159               String temp = (String)currentContent.getValue("description");
160               if(temp!=null && temp.length()>0){
161                 temp = StringUtil.createHTML(temp);
162                 temp = StringUtil.decodeHTMLinTags(temp);
163                 currentContent.setValueForProperty("description",temp);
164               }
165             } else {
166               String temp = (String)currentContent.getValue("description");
167               if(temp!=null && temp.length()>0){
168                 temp = StringUtil.decodeHTMLinTags(temp);
169                 currentContent.setValueForProperty("description",temp);
170               }
171             }
172           }
173         }
174         SimpleList itemList = HTMLTemplateProcessor.makeSimpleList(list);
175         //process hashmap additional and add to mergedata
176         if (additional != null) {
177           Set set = additional.keySet();
178           for (Iterator it = set.iterator();it.hasNext();) {
179             String key = (String)it.next();
180             mergeData.put(key,(TemplateModel)additional.get(key));
181           }
182         }
183
184         if (i==0){
185           htmlFileName = producerDocRoot + "/" + fileDesc + ".shtml";
186           mergeData.put("filename",fileDesc + ".shtml");
187           mergeData.put("previousPage","");
188           if(numberOfPages<=1){
189             mergeData.put("nextPage","");
190           } else {
191             mergeData.put("nextPage",fileDesc + (numberOfPages-1) + ".shtml");
192           }
193         } else {
194           if (i==1 && numberOfPages > 2){
195             mergeData.put("previousPage",fileDesc + ".shtml");
196             mergeData.put("nextPage",fileDesc + (numberOfPages-2) + ".shtml");
197           } else {
198             if (i==(numberOfPages-1)){
199               mergeData.put("previousPage",fileDesc + (numberOfPages-i+1) + ".shtml");
200               mergeData.put("nextPage","");
201             } else {
202               mergeData.put("previousPage",fileDesc + (numberOfPages-(i-1)) + ".shtml");
203               mergeData.put("nextPage",fileDesc + (numberOfPages-(i+1)) + ".shtml");
204             }
205           }
206           htmlFileName = producerDocRoot + "/" + fileDesc + (numberOfPages-i) + ".shtml";
207           mergeData.put("filename",fileDesc + (numberOfPages-i) + ".shtml");
208         }
209
210         //producing the html-files
211         boolean retVal = produce(listTemplate, htmlFileName, mergeData, htmlout);
212       } //end if
213     }//end for
214
215     sessionConnectTime = new java.util.Date().getTime() - startTime;
216     logHTML(htmlout, "Producer.List finished: " + sessionConnectTime + " ms.");
217   } //end handle
218
219   abstract public void setAdditional(String key, TemplateModel value);
220 }