1 package mircoders.producer;
5 import java.lang.reflect.*;
9 import freemarker.template.*;
17 import mircoders.module.*;
18 import mircoders.entity.*;
19 import mircoders.storage.*;
23 abstract public class ProducerList extends Producer {
25 public String listTemplate;
26 public String whereClause;
27 public String orderBy;
28 public String fileDesc;
29 protected HashMap additional = new HashMap();
32 SimpleHash upMediaSimpleHash;
33 Class mediaHandlerClass;
34 Class mediaStorageClass;
35 String mediaStorageName;
36 String mediaHandlerName=null;
37 MirMedia mediaHandler=null;
38 Database mediaStorage=null;
42 public void handle(PrintWriter htmlout, EntityUsers user, boolean sync, boolean force)
43 throws StorageObjectException, ModuleException {
44 handleIt(htmlout,user,force);
47 public void handleIt(PrintWriter htmlout, EntityUsers user, boolean force)
48 throws StorageObjectException, ModuleException {
50 logHTML(htmlout, "Producer.List: started");
51 int newsPerPage = Integer.parseInt(MirConfig.getProp("Producer.StartPage.Newswire"));
52 long sessionConnectTime = 0;
53 long startTime = (new java.util.Date()).getTime();
54 String htmlFileName = "";
55 String currentMediaId;
56 EntityContent currentContent;
58 EntityUsers userEntity=null;
59 SimpleHash imageHash = new SimpleHash();
63 int maxItemsOnPage = Integer.parseInt(MirConfig.getProp("Lists.Max.Items"));
66 listSize = contentModule.getSize(whereClause);
67 } catch (Exception e) {
68 logHTML(htmlout,e.toString());
71 int modRest = listSize % maxItemsOnPage;
72 int numberOfPages = (listSize - modRest) / maxItemsOnPage;
74 for (int i = 0;i < numberOfPages+1;i ++) {
75 //break the loop, if only athe actuell pages should be produced
76 if (force == false && i==3) {
79 //break, if only the first page has to be produced
80 if (force == false && modRest != 0 && first==false){
86 //get the data for the first page
87 size=maxItemsOnPage + modRest;
88 list = contentModule.getContent(whereClause, orderBy, 0, size, userEntity);
91 //get the data for the other pages
92 list = contentModule.getContent(whereClause, orderBy, size, maxItemsOnPage, userEntity);
93 size = size + maxItemsOnPage;
96 //now produce the pages
97 if (list!=null || force==true) {
98 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(list);
99 //first we try to get the media
102 for (int k=0; k < list.size();k++) {
103 currentContent = (EntityContent)list.elementAt(k);
106 EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
107 SimpleList contentList = (SimpleList)mergeData.get("contentlist");
108 SimpleHash contentHash = (SimpleHash)contentList.get(k);
109 if (currentMediaList!=null && currentMediaList.getCount()>=1) {
110 SimpleList mediaListAudio = new SimpleList();
111 SimpleList mediaListImages = new SimpleList();
112 SimpleList mediaListVideo = new SimpleList();
113 SimpleList mediaListOther = new SimpleList();
114 //SimpleHash allMediaSimpleHash = new SimpleHash();
115 for (int n=0; n < currentMediaList.size();n++) {
116 upMedia = currentMediaList.elementAt(n);
117 upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
118 mediaType = ((EntityMedia)upMedia).getMediaType();
119 //must be a non-existant to_media_type entry..
120 if (mediaType != null) {
121 mediaHandlerName = mediaType.getValue("classname");
122 mediaStorageName = mediaType.getValue("tablename");
123 mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
124 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
125 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
126 Method m = mediaStorageClass.getMethod("getInstance", null);
127 mediaStorage = (Database)m.invoke(null, null);
128 //we most likely need further info
129 upMedia = mediaStorage.selectById(upMedia.getId());
130 upMediaSimpleHash.put("url", mediaHandler.getListView(upMedia, mediaType));
131 if (upMedia.getValue("is_published").equals("1")) {
132 if (mediaHandler.isImage()) {
133 mediaListImages.add(upMediaSimpleHash);
134 } else if (mediaHandler.isAudio()) {
135 mediaListAudio.add(upMediaSimpleHash);
136 } else if (mediaHandler.isVideo()) {
137 mediaListVideo.add(upMediaSimpleHash);
139 mediaListOther.add(upMediaSimpleHash);
141 } //end if is_published
142 } //end if media_type != null
146 contentHash.put("to_media_audio", mediaListAudio);
147 contentHash.put("to_media_images", mediaListImages);
148 contentHash.put("to_media_video", mediaListVideo);
149 contentHash.put("to_media_other", mediaListOther);
150 } //end if currentMediaList != null
153 if(((SimpleScalar)contentHash.get("is_html")).getAsString().equals("0") ){
154 SimpleScalar tempScalar = (SimpleScalar)contentHash.get("description");
155 String temp = tempScalar.getAsString();
156 if(temp!=null && temp.length()>0){
157 temp = StringUtil.createHTML(temp);
158 temp = StringUtil.decodeHTMLinTags(temp);
159 contentHash.put("description",temp);
162 SimpleScalar tempScalar = (SimpleScalar)contentHash.get("description");
163 String temp = tempScalar.getAsString();
164 if(temp!=null && temp.length()>0){
165 temp = StringUtil.decodeHTMLinTags(temp);
166 contentHash.put("description",temp);
170 } catch (Exception e) {
171 logHTML(htmlout, "Producer.List id " +currentContent.getId()+ " seems corrupt, skipping it :: "+e.toString());
172 theLog.printError("Producer.List id " +currentContent.getId()+ " seems corrupt, skipping it :: "+e.toString());
174 } //end for over each article
175 } //end if list != null
176 SimpleList itemList = HTMLTemplateProcessor.makeSimpleList(list);
177 //process hashmap additional and add to mergedata
178 if (additional != null) {
179 Set set = additional.keySet();
180 for (Iterator it = set.iterator();it.hasNext();) {
181 String key = (String)it.next();
182 mergeData.put(key,(TemplateModel)additional.get(key));
187 htmlFileName = producerDocRoot + "/" + fileDesc + ".shtml";
188 mergeData.put("filename",fileDesc + ".shtml");
189 mergeData.put("previousPage","");
190 if(numberOfPages<=1){
191 mergeData.put("nextPage","");
193 mergeData.put("nextPage",fileDesc + (numberOfPages-1) + ".shtml");
196 if (i==1 && numberOfPages > 2){
197 mergeData.put("previousPage",fileDesc + ".shtml");
198 mergeData.put("nextPage",fileDesc + (numberOfPages-2) + ".shtml");
200 if (i==(numberOfPages-1)){
201 mergeData.put("previousPage",fileDesc + (numberOfPages-i+1) + ".shtml");
202 mergeData.put("nextPage","");
204 mergeData.put("previousPage",fileDesc + (numberOfPages-(i-1)) + ".shtml");
205 mergeData.put("nextPage",fileDesc + (numberOfPages-(i+1)) + ".shtml");
208 htmlFileName = producerDocRoot + "/" + fileDesc + (numberOfPages-i) + ".shtml";
209 mergeData.put("filename",fileDesc + (numberOfPages-i) + ".shtml");
212 //producing the html-files
213 boolean retVal = produce(listTemplate, htmlFileName, mergeData, htmlout);
217 sessionConnectTime = new java.util.Date().getTime() - startTime;
218 logHTML(htmlout, "Producer.List finished: " + sessionConnectTime + " ms.");
221 abstract public void setAdditional(String key, TemplateModel value);