1 package mircoders.producer;
5 import java.lang.reflect.*;
8 import freemarker.template.*;
16 import mircoders.entity.*;
17 import mircoders.storage.*;
20 public class ProducerContent extends Producer {
22 public static void main(String argv[]){
24 * Why are we reloading the config here?
25 * Can someone please explain this?
26 * Hope I didn't break anything
27 * -mh. <heckmann@hbe.ca>
29 //Configuration.initConfig("config");
30 System.out.println(MirConfig.getProp("Producer.DocRoot"));
33 new ProducerContent().handle(new PrintWriter(System.out), null, false,false);
34 } catch(Exception e) {
35 System.err.println(e.toString());
41 public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)
42 throws StorageObjectException, ModuleException {
44 handle(htmlout,user,force,sync,null);
51 public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
52 throws StorageObjectException, ModuleException {
54 String contentTemplate = MirConfig.getProp("Producer.Content.Template");
55 int contentBatchsize = Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
56 String extLinkName = MirConfig.getProp("Producer.ExtLinkName");
57 String intLinkName = MirConfig.getProp("Producer.IntLinkName");
58 String mailLinkName = MirConfig.getProp("Producer.MailLinkName");
59 String imageRoot = MirConfig.getProp("Producer.ImageRoot");
61 long sessionConnectTime = 0;
62 long startTime = (new java.util.Date()).getTime();
63 String whereClause = " ";
65 String htmlFileName = null;
66 String currentMediaId;
67 EntityContent currentContent;
68 EntityList batchEntityList;
69 HashMap currentContentValues;
70 SimpleHash imageHash = new SimpleHash();
71 EntityUsers userEntity=null;
74 SimpleHash upMediaSimpleHash;
75 Class mediaHandlerClass;
76 Class mediaStorageClass;
77 String mediaStorageName;
78 String mediaHandlerName=null;
79 MirMedia mediaHandler=null;
80 Database mediaStorage=null;
84 // production of the content-pages
85 orderBy="date desc, webdb_lastchange desc";
87 whereClause="is_published='1'";
88 // if true: produces a single content item
90 whereClause += " AND id="+id;
92 batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
94 whereClause="is_produced='0' AND is_published='1'";
95 //if true produces a single contentitem
97 whereClause += " AND id="+id;
99 batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
102 while (batchEntityList != null) {
103 for(int i=0;i<batchEntityList.size();i++) {
104 currentContent = (EntityContent)batchEntityList.elementAt(i);
106 currentContentValues = currentContent.getValues();
108 //because of postgres 7.1.* not needed anymore
109 //currentContentValues.put("content_data",currentContent.getContentData());
110 String date = (String)currentContentValues.get("date");
111 String year = date.substring(0,4);
112 String month = date.substring(4,6);
114 htmlFileName = producerDocRoot
115 + "/" + year + "/" + month + "/" + currentContentValues.get("id") + ".shtml";
117 currentContentValues.put("content_data",StringUtil.deleteForbiddenTags((String)currentContentValues.get("content_data")));
118 currentContentValues.put("description",StringUtil.deleteForbiddenTags((String)currentContentValues.get("description")));
121 //create http-links and email-links
122 if (currentContentValues.get("is_html").equals("0")) {
123 String temp = (String)currentContentValues.get("content_data");
124 if(temp!=null && temp.length()>0){
125 temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
126 temp = StringUtil.decodeHTMLinTags(temp);
127 currentContentValues.put("content_data",temp);
129 temp = (String)currentContentValues.get("description");
130 if(temp!=null && temp.length()>0){
131 temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
132 temp = StringUtil.decodeHTMLinTags(temp);
133 currentContentValues.put("description",temp);
136 String temp = (String)currentContentValues.get("content_data");
137 if(temp!=null && temp.length()>0){
138 temp = StringUtil.decodeHTMLinTags(temp);
139 currentContentValues.put("content_data",temp);
141 temp = (String)currentContentValues.get("description");
142 if(temp!=null && temp.length()>0){
143 temp = StringUtil.decodeHTMLinTags(temp);
144 currentContentValues.put("description",temp);
148 //create the freemarker-model
149 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(currentContentValues);
151 /** @todo all the following should take place in EntityContent */
152 // get the uploaded media
153 EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
154 if (currentMediaList!=null && currentMediaList.getCount()>=1) {
155 SimpleList mediaList = new SimpleList();
156 for (int n=0; n < currentMediaList.size();n++) {
157 upMedia = currentMediaList.elementAt(n);
158 upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
159 mediaType = ((EntityMedia)upMedia).getMediaType();
160 //must be a non-existant to_media_type entry..
161 if (mediaType != null) {
162 mediaHandlerName = mediaType.getValue("classname");
163 mediaStorageName = mediaType.getValue("tablename");
164 mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
165 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
166 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
167 Method m = mediaStorageClass.getMethod("getInstance", null);
168 mediaStorage = (Database)m.invoke(null, null);
169 //we most likely need further info
170 upMedia = mediaStorage.selectById(upMedia.getId());
171 upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
172 upMediaSimpleHash.put("type",mediaType.getValue("classname"));
173 mediaList.add(upMediaSimpleHash);
174 } //end if media_type != null
176 mergeData.put("to_media", mediaList);
177 } //end if currentMediaList != null
179 // get the comments for the article
182 /** @todo currently this doesn't work
183 * all this should be moved to Entity, and the parsed description should
184 * be delivered via entity.get("description_parsed") */
185 SimpleList commentList = currentContent.getComments();
186 if(commentList.isEmpty()==false){
187 while(commentList.hasNext()){
188 SimpleHash comment = (SimpleHash)commentList.next();
189 SimpleScalar commentText = (SimpleScalar)comment.get("description");
190 comment.put("description",new SimpleScalar(StringUtil.createHTML(commentText.getAsString(),imageRoot,mailLinkName,extLinkName,intLinkName)));
193 mergeData.put("comments", commentList);
195 // get the topics of this article
196 mergeData.put("topics",HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(currentContent)));
199 boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
200 sessionConnectTime = new java.util.Date().getTime() - startTime;
201 if (retVal == true && !"1".equals(currentContent.getValue("is_produced")))
202 currentContent.setProduced(true);
203 } catch(Exception e) {
204 logHTML(htmlout, "Producer.Content ERROR while producing content ID: " + currentContent.getId()+",skipping it :: "+e.toString());
205 theLog.printError("Producer.Content ERROR while producing content ID: " + currentContent.getId() +",skipping it :: "+e.toString());
211 if (batchEntityList.hasNextBatch()){
212 batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);
214 batchEntityList=null;
218 // timing and message to browser
219 sessionConnectTime = new java.util.Date().getTime() - startTime;
220 logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");