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