ff8aa708a671e6e7c964a3d5d6fe2212a8932317
[mir.git] / source / mircoders / producer / ProducerList.java
1 package mircoders.producer;
2
3 import java.io.*;
4 import java.lang.*;
5 import java.util.*;
6 import java.sql.*;
7
8 import freemarker.template.*;
9
10 import webdb.misc.*;
11 import webdb.storage.*;
12 import webdb.module.*;
13 import webdb.entity.*;
14
15 import mir.module.*;
16 import mir.entity.*;
17 import mir.storage.*;
18
19
20
21 abstract public class ProducerList extends Producer {
22
23         public String listTemplate;
24         public String whereClause;
25         public String orderBy;
26         public String fileDesc;
27         protected HashMap additional = new HashMap();
28
29         public void handle(PrintWriter htmlout, EntityUsers user, boolean sync, boolean force)
30                 throws StorageObjectException, ModuleException {
31                 handleIt(htmlout,user,force);
32         }
33
34         public void handleIt(PrintWriter htmlout, EntityUsers user, boolean force)
35                 throws StorageObjectException, ModuleException {
36
37                 logHTML(htmlout, "Producer.List: started");
38     int newsPerPage = Integer.parseInt(Configuration.getProperty("Producer.StartPage.Newswire"));
39                 long                sessionConnectTime = 0;
40                 long                startTime = (new java.util.Date()).getTime();
41                 String              htmlFileName = "";
42                 String              currentMediaId;
43                 EntityContent       currentContent;
44                 EntityList          list;
45                 EntityGruppen       userEntity=null;
46                 SimpleHash          imageHash = new SimpleHash();
47                 int size = 0;
48                 int listSize = 0;
49
50                 int maxItemsOnPage = Integer.parseInt(Configuration.getProperty("Lists.Max.Items"));
51
52                 try {
53                         listSize = contentModule.getSize(whereClause);
54                 } catch (Exception e) {
55                         logHTML(htmlout,e.toString());
56                 }
57
58                 int modRest = listSize % maxItemsOnPage;
59                 int numberOfPages = (listSize - modRest) / maxItemsOnPage;
60                 boolean first=true;
61                 for (int i = 0;i < numberOfPages+1;i ++) {
62                         //break the loop, if only athe actuell pages should be produced
63                         if (force == false && i==3) {
64                                 break;
65                         }
66                         //break, if only the first page has to be produced
67                         if (force == false && modRest != 0 && first==false){
68                                 break;
69                         }
70
71
72                         if (first==true) {
73                                 //get the data for the first page
74                                 size=maxItemsOnPage + modRest;
75                                 list = contentModule.getContent(whereClause, orderBy, 0, size, userEntity);
76                                 first=false;
77                         } else {
78                                 //get the data for the other pages
79                                 list = contentModule.getContent(whereClause, orderBy, size, maxItemsOnPage, userEntity);
80                                 size = size + maxItemsOnPage;
81                         }
82
83                         //now produce the pages
84                         if (list!=null || force==true) {
85                                 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(list);
86                                 //first we try to get the images
87                                 SimpleList itemList = HTMLTemplateProcessor.makeSimpleList(list);
88                                 if(list!=null){
89                                         for (int k=0; k < list.size();k++) {
90                                                 currentContent = (EntityContent)list.elementAt(k);
91             //images to content
92                                                 currentMediaId = currentContent.getValue("to_media");
93                                                 if (currentMediaId!=null && !currentMediaId.equals("")) {
94                                                         imageHash.put(currentMediaId, HTMLTemplateProcessor.makeSimpleHash(imageModule.getById(currentMediaId)));
95                                                 }
96             //content to html
97             if(currentContent.getValue("is_html").equals("0")){
98               String temp = (String)currentContent.getValue("description");
99               if(temp!=null && temp.length()>0){
100                 temp = StringUtil.createHTML(temp);
101                 temp = StringUtil.decodeHTMLinTags(temp);
102                 currentContent.setValueForProperty("description",temp);
103               }
104             } else {
105               String temp = (String)currentContent.getValue("description");
106               if(temp!=null && temp.length()>0){
107                 temp = StringUtil.decodeHTMLinTags(temp);
108                 currentContent.setValueForProperty("description",temp);
109               }
110             }
111                                         }
112                                         mergeData.put("images", imageHash);
113                                 }
114
115                                 //process hashmap additional and add to mergedata
116                                 if (additional != null) {
117                                         Set set = additional.keySet();
118                                         for (Iterator it = set.iterator();it.hasNext();) {
119                                                 String key = (String)it.next();
120                                                 mergeData.put(key,(TemplateModel)additional.get(key));
121                                         }
122                                 }
123
124                                 if (i==0){
125                                         htmlFileName = producerDocRoot + "/" + fileDesc + ".shtml";
126                                         mergeData.put("filename",fileDesc + ".shtml");
127                                         mergeData.put("previousPage","");
128                                         if(numberOfPages<=1){
129                                                 mergeData.put("nextPage","");
130                                         } else {
131                                                 mergeData.put("nextPage",fileDesc + (numberOfPages-1) + ".shtml");
132                                         }
133                                 } else {
134                                         if (i==1 && numberOfPages > 2){
135                                                 mergeData.put("previousPage",fileDesc + ".shtml");
136                                                 mergeData.put("nextPage",fileDesc + (numberOfPages-2) + ".shtml");
137                                         } else {
138                                                 if (i==(numberOfPages-1)){
139                                                         mergeData.put("previousPage",fileDesc + (numberOfPages-i+1) + ".shtml");
140                                                         mergeData.put("nextPage","");
141                                                 } else {
142                                                         mergeData.put("previousPage",fileDesc + (numberOfPages-(i-1)) + ".shtml");
143                                                         mergeData.put("nextPage",fileDesc + (numberOfPages-(i+1)) + ".shtml");
144                                                 }
145                                         }
146                                         htmlFileName = producerDocRoot + "/" + fileDesc + (numberOfPages-i) + ".shtml";
147                                         mergeData.put("filename",fileDesc + (numberOfPages-i) + ".shtml");
148                                 }
149
150                                 //producing the html-files
151                                 boolean retVal = produce(listTemplate, htmlFileName, mergeData, htmlout);
152                         } //end if
153                 }//end for
154
155                 sessionConnectTime = new java.util.Date().getTime() - startTime;
156                 logHTML(htmlout, "Producer.List finished: " + sessionConnectTime + " ms.");
157         } //end handle
158
159         abstract public void setAdditional(String key, TemplateModel value);
160 }