1-n-content-media, tomcat-session-tracking without cookies, and more
[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 mir.misc.*;
11 import mir.storage.*;
12 import mir.module.*;
13 import mir.entity.*;
14
15 import mircoders.module.*;
16 import mircoders.entity.*;
17 import mircoders.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                                                 EntityList currentMedia = DatabaseContentToMedia.getInstance().getMedia(currentContent);
93                                                 if (currentMedia!=null && currentMedia.getCount()>=1) {
94                                                         SimpleList mediaList = HTMLTemplateProcessor.makeSimpleList(currentMedia);
95                                                         mergeData.put("to_media", mediaList);
96                                                 }
97             //content to html
98             if(currentContent.getValue("is_html").equals("0")){
99               String temp = (String)currentContent.getValue("description");
100               if(temp!=null && temp.length()>0){
101                 temp = StringUtil.createHTML(temp);
102                 temp = StringUtil.decodeHTMLinTags(temp);
103                 currentContent.setValueForProperty("description",temp);
104               }
105             } else {
106               String temp = (String)currentContent.getValue("description");
107               if(temp!=null && temp.length()>0){
108                 temp = StringUtil.decodeHTMLinTags(temp);
109                 currentContent.setValueForProperty("description",temp);
110               }
111             }
112                                         }
113                                         mergeData.put("images", imageHash);
114                                 }
115
116                                 //process hashmap additional and add to mergedata
117                                 if (additional != null) {
118                                         Set set = additional.keySet();
119                                         for (Iterator it = set.iterator();it.hasNext();) {
120                                                 String key = (String)it.next();
121                                                 mergeData.put(key,(TemplateModel)additional.get(key));
122                                         }
123                                 }
124
125                                 if (i==0){
126                                         htmlFileName = producerDocRoot + "/" + fileDesc + ".shtml";
127                                         mergeData.put("filename",fileDesc + ".shtml");
128                                         mergeData.put("previousPage","");
129                                         if(numberOfPages<=1){
130                                                 mergeData.put("nextPage","");
131                                         } else {
132                                                 mergeData.put("nextPage",fileDesc + (numberOfPages-1) + ".shtml");
133                                         }
134                                 } else {
135                                         if (i==1 && numberOfPages > 2){
136                                                 mergeData.put("previousPage",fileDesc + ".shtml");
137                                                 mergeData.put("nextPage",fileDesc + (numberOfPages-2) + ".shtml");
138                                         } else {
139                                                 if (i==(numberOfPages-1)){
140                                                         mergeData.put("previousPage",fileDesc + (numberOfPages-i+1) + ".shtml");
141                                                         mergeData.put("nextPage","");
142                                                 } else {
143                                                         mergeData.put("previousPage",fileDesc + (numberOfPages-(i-1)) + ".shtml");
144                                                         mergeData.put("nextPage",fileDesc + (numberOfPages-(i+1)) + ".shtml");
145                                                 }
146                                         }
147                                         htmlFileName = producerDocRoot + "/" + fileDesc + (numberOfPages-i) + ".shtml";
148                                         mergeData.put("filename",fileDesc + (numberOfPages-i) + ".shtml");
149                                 }
150
151                                 //producing the html-files
152                                 boolean retVal = produce(listTemplate, htmlFileName, mergeData, htmlout);
153                         } //end if
154                 }//end for
155
156                 sessionConnectTime = new java.util.Date().getTime() - startTime;
157                 logHTML(htmlout, "Producer.List finished: " + sessionConnectTime + " ms.");
158         } //end handle
159
160         abstract public void setAdditional(String key, TemplateModel value);
161 }