c3d134c1f4eb8a3fbb40d35bfd713daee940762c
[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
31
32
33   public void handle(PrintWriter htmlout, EntityUsers user, boolean sync, boolean force)
34     throws StorageObjectException, ModuleException {
35     handleIt(htmlout,user,force);
36   }
37
38   /** @todo this should return the number of pages produced! */
39   public void handleIt(PrintWriter htmlout, EntityUsers user, boolean force)
40     throws StorageObjectException, ModuleException {
41
42     logHTML(htmlout, "Producer.List: started");
43     int newsPerPage = Integer.parseInt(MirConfig.getProp("Producer.StartPage.Newswire"));
44     long                sessionConnectTime = 0;
45     long                startTime = (new java.util.Date()).getTime();
46     String              htmlFileName = "";
47     String              currentMediaId;
48     EntityContent       currentContent;
49     EntityList          list;
50     EntityUsers         userEntity=null;
51     SimpleHash          imageHash = new SimpleHash();
52     int size = 0;
53     int listSize = 0;
54
55     int maxItemsOnPage = Integer.parseInt(MirConfig.getProp("Lists.Max.Items"));
56
57     try {
58       listSize = contentModule.getSize(whereClause);
59     } catch (Exception e) {
60       logHTML(htmlout,e.toString());
61     }
62
63     int modRest = listSize % maxItemsOnPage;
64     int numberOfPages = (listSize - modRest) / maxItemsOnPage;
65     boolean first=true;
66     for (int i = 0;i < numberOfPages+1;i ++) {
67       //break the loop, if only athe actuell pages should be produced
68       if (force == false && i==3) {
69         break;
70       }
71       //break, if only the first page has to be produced
72       if (force == false && modRest != 0 && first==false){
73         break;
74       }
75
76
77       if (first==true) {
78         //get the data for the first page
79         size=maxItemsOnPage + modRest;
80         list = contentModule.getContent(whereClause, orderBy, 0, size, userEntity);
81         first=false;
82       } else {
83         //get the data for the other pages
84         list = contentModule.getContent(whereClause, orderBy, size, maxItemsOnPage, userEntity);
85         size = size + maxItemsOnPage;
86       }
87
88       //now produce the pages
89       if (list!=null || force==true) {
90         SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(list);
91         //process hashmap additional and add to mergedata
92         if (additional != null) {
93           Set set = additional.keySet();
94           for (Iterator it = set.iterator();it.hasNext();) {
95             String key = (String)it.next();
96             mergeData.put(key,(TemplateModel)additional.get(key));
97           }
98         }
99
100         if (i==0){
101           htmlFileName = "/" + fileDesc + ".shtml";
102           mergeData.put("filename",fileDesc + ".shtml");
103           mergeData.put("previousPage","");
104           if(numberOfPages<=1){
105             mergeData.put("nextPage","");
106           } else {
107             mergeData.put("nextPage",fileDesc + (numberOfPages-1) + ".shtml");
108           }
109         } else {
110           if (i==1 && numberOfPages > 2){
111             mergeData.put("previousPage",fileDesc + ".shtml");
112             mergeData.put("nextPage",fileDesc + (numberOfPages-2) + ".shtml");
113           } else {
114             if (i==(numberOfPages-1)){
115               mergeData.put("previousPage",fileDesc + (numberOfPages-i+1) + ".shtml");
116               mergeData.put("nextPage","");
117             } else {
118               mergeData.put("previousPage",fileDesc + (numberOfPages-(i-1)) + ".shtml");
119               mergeData.put("nextPage",fileDesc + (numberOfPages-(i+1)) + ".shtml");
120             }
121           }
122           htmlFileName = "/" + fileDesc + (numberOfPages-i) + ".shtml";
123           mergeData.put("filename",fileDesc + (numberOfPages-i) + ".shtml");
124         }
125
126         //producing the html-files
127         boolean retVal = produce(listTemplate, htmlFileName, mergeData, htmlout);
128       } //end if
129     }//end for
130
131     sessionConnectTime = new java.util.Date().getTime() - startTime;
132     logHTML(htmlout, "Producer.List finished: " + sessionConnectTime + " ms.");
133   } //end handle
134
135   public void setAdditional(String key, TemplateModel value) {
136     additional.put(key,value);
137   }
138
139 }