150cf4ee04c0dd5ace80e5d76c93a7d6ef76d387
[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(MirConfig.getProp("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(MirConfig.getProp("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         
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().getImages(currentContent);
93             if (currentMedia!=null && currentMedia.getCount()>=1) {
94               SimpleList mediaList = HTMLTemplateProcessor.makeSimpleList(currentMedia);
95               try{
96                 SimpleList contentList = (SimpleList)mergeData.get("contentlist");
97                 SimpleHash contentHash = (SimpleHash)contentList.get(k);
98                 contentHash.put("to_media", mediaList);
99               } catch (Exception e){}
100               
101             }
102             //content to html
103             if(currentContent.getValue("is_html").equals("0")){
104               String temp = (String)currentContent.getValue("description");
105               if(temp!=null && temp.length()>0){
106                 temp = StringUtil.createHTML(temp);
107                 temp = StringUtil.decodeHTMLinTags(temp);
108                 currentContent.setValueForProperty("description",temp);
109               }
110             } else {
111               String temp = (String)currentContent.getValue("description");
112               if(temp!=null && temp.length()>0){
113                 temp = StringUtil.decodeHTMLinTags(temp);
114                 currentContent.setValueForProperty("description",temp);
115               }
116             }
117           }
118         }
119         SimpleList itemList = HTMLTemplateProcessor.makeSimpleList(list);
120         //process hashmap additional and add to mergedata
121         if (additional != null) {
122           Set set = additional.keySet();
123           for (Iterator it = set.iterator();it.hasNext();) {
124             String key = (String)it.next();
125             mergeData.put(key,(TemplateModel)additional.get(key));
126           }
127         }
128
129         if (i==0){
130           htmlFileName = producerDocRoot + "/" + fileDesc + ".shtml";
131           mergeData.put("filename",fileDesc + ".shtml");
132           mergeData.put("previousPage","");
133           if(numberOfPages<=1){
134             mergeData.put("nextPage","");
135           } else {
136             mergeData.put("nextPage",fileDesc + (numberOfPages-1) + ".shtml");
137           }
138         } else {
139           if (i==1 && numberOfPages > 2){
140             mergeData.put("previousPage",fileDesc + ".shtml");
141             mergeData.put("nextPage",fileDesc + (numberOfPages-2) + ".shtml");
142           } else {
143             if (i==(numberOfPages-1)){
144               mergeData.put("previousPage",fileDesc + (numberOfPages-i+1) + ".shtml");
145               mergeData.put("nextPage","");
146             } else {
147               mergeData.put("previousPage",fileDesc + (numberOfPages-(i-1)) + ".shtml");
148               mergeData.put("nextPage",fileDesc + (numberOfPages-(i+1)) + ".shtml");
149             }
150           }
151           htmlFileName = producerDocRoot + "/" + fileDesc + (numberOfPages-i) + ".shtml";
152           mergeData.put("filename",fileDesc + (numberOfPages-i) + ".shtml");
153         }
154
155         //producing the html-files
156         boolean retVal = produce(listTemplate, htmlFileName, mergeData, htmlout);
157       } //end if
158     }//end for
159
160     sessionConnectTime = new java.util.Date().getTime() - startTime;
161     logHTML(htmlout, "Producer.List finished: " + sessionConnectTime + " ms.");
162   } //end handle
163
164   abstract public void setAdditional(String key, TemplateModel value);
165 }