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 SimpleHash imageHash = new SimpleHash();
70 EntityUsers userEntity=null;
73 SimpleHash upMediaSimpleHash;
74 Class mediaHandlerClass;
75 Class mediaStorageClass;
76 String mediaStorageName;
77 String mediaHandlerName=null;
78 MirMedia mediaHandler=null;
79 Database mediaStorage=null;
83 // production of the content-pages
84 orderBy="date desc, webdb_lastchange desc";
86 whereClause="is_published='1'";
87 // if true: produces a single content item
89 whereClause += " AND id="+id;
91 batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
93 whereClause="is_produced='0' AND is_published='1'";
94 //if true produces a single contentitem
96 whereClause += " AND id="+id;
98 batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
101 while (batchEntityList != null) {
102 for(int i=0;i<batchEntityList.size();i++) {
103 currentContent = (EntityContent)batchEntityList.elementAt(i);
104 HashMap currentContentValues = currentContent.getValues();
105 String date = (String)currentContent.getValue("date");
106 String year = date.substring(0,4);
107 String month = date.substring(4,6);
109 htmlFileName = producerDocRoot
110 + "/" + year + "/" + month + "/" + currentContent.getValue("id") + ".shtml";
112 currentContentValues.put("content_data",StringUtil.deleteForbiddenTags((String)currentContentValues.get("content_data")));
113 currentContentValues.put("description",StringUtil.deleteForbiddenTags((String)currentContentValues.get("description")));
116 //create http-links and email-links
117 if (currentContentValues.get("is_html").equals("0")) {
118 String temp = (String)currentContentValues.get("content_data");
119 if(temp!=null && temp.length()>0){
120 temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
121 temp = StringUtil.decodeHTMLinTags(temp);
122 currentContentValues.put("content_data",temp);
124 temp = (String)currentContentValues.get("description");
125 if(temp!=null && temp.length()>0){
126 temp = StringUtil.createHTML(temp,imageRoot,mailLinkName,extLinkName,intLinkName);
127 temp = StringUtil.decodeHTMLinTags(temp);
128 currentContentValues.put("description",temp);
131 String temp = (String)currentContentValues.get("content_data");
132 if(temp!=null && temp.length()>0){
133 temp = StringUtil.decodeHTMLinTags(temp);
134 currentContentValues.put("content_data",temp);
136 temp = (String)currentContentValues.get("description");
137 if(temp!=null && temp.length()>0){
138 temp = StringUtil.decodeHTMLinTags(temp);
139 currentContentValues.put("description",temp);
143 //create the freemarker-model
144 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(currentContentValues);
146 // get the uploaded media
147 EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
148 if (currentMediaList!=null && currentMediaList.getCount()>=1) {
149 SimpleList mediaList = new SimpleList();
150 for (int n=0; n < currentMediaList.size();n++) {
151 upMedia = currentMediaList.elementAt(n);
152 upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
153 mediaType = ((EntityMedia)upMedia).getMediaType();
154 //in case it's a non-existant to_media_type entry..
155 if (mediaType != null) {
157 mediaHandlerName = mediaType.getValue("classname");
158 mediaStorageName = mediaType.getValue("tablename");
159 mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
160 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
161 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
162 Method m = mediaStorageClass.getMethod("getInstance", null);
163 mediaStorage = (Database)m.invoke(null, null);
164 //we most likely need further info
165 upMedia = mediaStorage.selectById(upMedia.getId());
166 } catch (Exception e) {
167 theLog.printError("ProducerStartpage:problem in reflection: "+mediaHandlerName);
169 upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
170 upMediaSimpleHash.put("type",mediaType.getValue("classname"));
171 mediaList.add(upMediaSimpleHash);
172 } //end if media_type != null
174 mergeData.put("to_media", mediaList);
175 } //end if currentMediaList != null
177 // get the comments for the article
179 SimpleList commentList = currentContent.getComments();
181 if(commentList.isEmpty()==false){
182 while(commentList.hasNext()){
183 SimpleHash comment = (SimpleHash)commentList.next();
184 SimpleScalar commentText = (SimpleScalar)comment.get("description");
185 comment.put("description",new SimpleScalar(StringUtil.createHTML(commentText.getAsString(),imageRoot,mailLinkName,extLinkName,intLinkName)));
188 } catch(Exception e){}
189 mergeData.put("comments", commentList);
191 // get the topics of this article
192 mergeData.put("topics",HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(currentContent)));
195 boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
196 sessionConnectTime = new java.util.Date().getTime() - startTime;
197 if (retVal == true && !"1".equals(currentContent.getValue("is_produced")))
198 currentContent.setProduced(true);
201 if (batchEntityList.hasNextBatch()){
202 batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);
204 batchEntityList=null;
208 // timing an message to browser
209 sessionConnectTime = new java.util.Date().getTime() - startTime;
210 logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");