Mir goes GPL
[mir.git] / source / mircoders / producer / ProducerList.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.producer;
33
34 import java.io.*;
35 import java.lang.*;
36 import java.lang.reflect.*;
37 import java.util.*;
38 import java.sql.*;
39
40 import freemarker.template.*;
41
42 import mir.misc.*;
43 import mir.media.*;
44 import mir.storage.*;
45 import mir.module.*;
46 import mir.entity.*;
47
48 import mircoders.module.*;
49 import mircoders.entity.*;
50 import mircoders.storage.*;
51
52
53
54 abstract public class ProducerList extends Producer {
55
56   public String listTemplate;
57   public String whereClause;
58   public String orderBy;
59   public String fileDesc;
60   protected HashMap additional = new HashMap();
61
62
63
64   public void handle(PrintWriter htmlout, EntityUsers user, boolean sync, boolean force)
65     throws StorageObjectException, ModuleException {
66     handleIt(htmlout,user,force);
67   }
68
69   /** @todo this should return the number of pages produced! */
70   public void handleIt(PrintWriter htmlout, EntityUsers user, boolean force)
71     throws StorageObjectException, ModuleException {
72
73     logHTML(htmlout, "Producer.List: started");
74     int newsPerPage = Integer.parseInt(MirConfig.getProp("Producer.StartPage.Newswire"));
75     long                sessionConnectTime = 0;
76     long                startTime = (new java.util.Date()).getTime();
77     String              htmlFileName = "";
78     String              currentMediaId;
79     EntityContent       currentContent;
80     EntityList          list;
81     EntityUsers         userEntity=null;
82     SimpleHash          imageHash = new SimpleHash();
83     int size = 0;
84     int listSize = 0;
85
86     int maxItemsOnPage = Integer.parseInt(MirConfig.getProp("Lists.Max.Items"));
87
88     try {
89       listSize = contentModule.getSize(whereClause);
90     } catch (Exception e) {
91       logHTML(htmlout,e.toString());
92     }
93
94     int modRest = listSize % maxItemsOnPage;
95     int numberOfPages = (listSize - modRest) / maxItemsOnPage;
96     boolean first=true;
97     for (int i = 0;i < numberOfPages+1;i ++) {
98       //break the loop, if only athe actuell pages should be produced
99       if (force == false && i==3) {
100         break;
101       }
102       //break, if only the first page has to be produced
103       if (force == false && modRest != 0 && first==false){
104         break;
105       }
106
107
108       if (first==true) {
109         //get the data for the first page
110         size=maxItemsOnPage + modRest;
111         list = contentModule.getContent(whereClause, orderBy, 0, size, userEntity);
112         first=false;
113       } else {
114         //get the data for the other pages
115         list = contentModule.getContent(whereClause, orderBy, size, maxItemsOnPage, userEntity);
116         size = size + maxItemsOnPage;
117       }
118
119       //now produce the pages
120       if (list!=null || force==true) {
121         SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(list);
122         //process hashmap additional and add to mergedata
123         if (additional != null) {
124           Set set = additional.keySet();
125           for (Iterator it = set.iterator();it.hasNext();) {
126             String key = (String)it.next();
127             mergeData.put(key,(TemplateModel)additional.get(key));
128           }
129         }
130
131         if (i==0){
132           htmlFileName = "/" + fileDesc + ".shtml";
133           mergeData.put("filename",fileDesc + ".shtml");
134           mergeData.put("previousPage","");
135           if(numberOfPages<=1){
136             mergeData.put("nextPage","");
137           } else {
138             mergeData.put("nextPage",fileDesc + (numberOfPages-1) + ".shtml");
139           }
140         } else {
141           if (i==1 && numberOfPages > 2){
142             mergeData.put("previousPage",fileDesc + ".shtml");
143             mergeData.put("nextPage",fileDesc + (numberOfPages-2) + ".shtml");
144           } else {
145             if (i==(numberOfPages-1)){
146               mergeData.put("previousPage",fileDesc + (numberOfPages-i+1) + ".shtml");
147               mergeData.put("nextPage","");
148             } else {
149               mergeData.put("previousPage",fileDesc + (numberOfPages-(i-1)) + ".shtml");
150               mergeData.put("nextPage",fileDesc + (numberOfPages-(i+1)) + ".shtml");
151             }
152           }
153           htmlFileName = "/" + fileDesc + (numberOfPages-i) + ".shtml";
154           mergeData.put("filename",fileDesc + (numberOfPages-i) + ".shtml");
155         }
156
157         //producing the html-files
158         boolean retVal = produce(listTemplate, htmlFileName, mergeData, htmlout);
159       } //end if
160     }//end for
161
162     sessionConnectTime = new java.util.Date().getTime() - startTime;
163     logHTML(htmlout, "Producer.List finished: " + sessionConnectTime + " ms.");
164   } //end handle
165
166   public void setAdditional(String key, TemplateModel value) {
167     additional.put(key,value);
168   }
169
170 }