proper handling of exceptions all around. especially for the media stuff.
[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 media
100
101         if(list!=null){
102           for (int k=0; k < list.size();k++) {
103             currentContent = (EntityContent)list.elementAt(k);
104             try {
105                 //media to content
106                 EntityList 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                   //SimpleHash allMediaSimpleHash = new SimpleHash();
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                   SimpleList contentList = (SimpleList)mergeData.get("contentlist");
143                   SimpleHash contentHash = (SimpleHash)contentList.get(k);
144                   contentHash.put("to_media_audio", mediaListAudio);
145                   contentHash.put("to_media_images", mediaListImages);
146                   contentHash.put("to_media_video", mediaListVideo);
147                   contentHash.put("to_media_other", mediaListOther);
148                 } //end if currentMediaList != null
149
150                 //content to html
151                 if(currentContent.getValue("is_html").equals("0")){
152                   String temp = (String)currentContent.getValue("description");
153                   if(temp!=null && temp.length()>0){
154                     temp = StringUtil.createHTML(temp);
155                     temp = StringUtil.decodeHTMLinTags(temp);
156                     currentContent.setValueForProperty("description",temp);
157                   }
158                 } else {
159                   String temp = (String)currentContent.getValue("description");
160                   if(temp!=null && temp.length()>0){
161                     temp = StringUtil.decodeHTMLinTags(temp);
162                     currentContent.setValueForProperty("description",temp);
163                   }
164                 }
165             } catch (Exception e) {
166                 logHTML(htmlout, "Producer.List id " +currentContent.getId()+ " seems corrupt, skipping it :: "+e.toString());
167                 theLog.printError("Producer.List id " +currentContent.getId()+ " seems corrupt, skipping it :: "+e.toString());
168             }
169           } //end for over each article
170         } //end if list != null
171         SimpleList itemList = HTMLTemplateProcessor.makeSimpleList(list);
172         //process hashmap additional and add to mergedata
173         if (additional != null) {
174           Set set = additional.keySet();
175           for (Iterator it = set.iterator();it.hasNext();) {
176             String key = (String)it.next();
177             mergeData.put(key,(TemplateModel)additional.get(key));
178           }
179         }
180
181         if (i==0){
182           htmlFileName = producerDocRoot + "/" + fileDesc + ".shtml";
183           mergeData.put("filename",fileDesc + ".shtml");
184           mergeData.put("previousPage","");
185           if(numberOfPages<=1){
186             mergeData.put("nextPage","");
187           } else {
188             mergeData.put("nextPage",fileDesc + (numberOfPages-1) + ".shtml");
189           }
190         } else {
191           if (i==1 && numberOfPages > 2){
192             mergeData.put("previousPage",fileDesc + ".shtml");
193             mergeData.put("nextPage",fileDesc + (numberOfPages-2) + ".shtml");
194           } else {
195             if (i==(numberOfPages-1)){
196               mergeData.put("previousPage",fileDesc + (numberOfPages-i+1) + ".shtml");
197               mergeData.put("nextPage","");
198             } else {
199               mergeData.put("previousPage",fileDesc + (numberOfPages-(i-1)) + ".shtml");
200               mergeData.put("nextPage",fileDesc + (numberOfPages-(i+1)) + ".shtml");
201             }
202           }
203           htmlFileName = producerDocRoot + "/" + fileDesc + (numberOfPages-i) + ".shtml";
204           mergeData.put("filename",fileDesc + (numberOfPages-i) + ".shtml");
205         }
206
207         //producing the html-files
208         boolean retVal = produce(listTemplate, htmlFileName, mergeData, htmlout);
209       } //end if
210     }//end for
211
212     sessionConnectTime = new java.util.Date().getTime() - startTime;
213     logHTML(htmlout, "Producer.List finished: " + sessionConnectTime + " ms.");
214   } //end handle
215
216   abstract public void setAdditional(String key, TemplateModel value);
217 }