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