restructuring producer
[mir.git] / source / mircoders / producer / ProducerContent.java
1 package mircoders.producer;
2
3 import java.io.*;
4 import java.lang.*;
5 import java.lang.reflect.*;
6 import java.util.*;
7
8 import freemarker.template.*;
9
10 import mir.misc.*;
11 import mir.media.*;
12 import mir.storage.*;
13 import mir.module.*;
14 import mir.entity.*;
15
16 import mircoders.entity.*;
17 import mircoders.storage.*;
18
19
20 public class ProducerContent extends Producer {
21
22   public static void main(String argv[]){
23     /**
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>
28      */
29     //Configuration.initConfig("config");
30     System.out.println(MirConfig.getProp("Producer.DocRoot"));
31
32     try {
33       new ProducerContent().handle(new PrintWriter(System.out), null, false,false);
34     } catch(Exception e) {
35       System.err.println(e.toString());
36     }
37   }
38
39
40
41   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)
42     throws StorageObjectException, ModuleException {
43
44     handle(htmlout,user,force,sync,null);
45   }
46
47
48
49
50
51   public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
52     throws StorageObjectException, ModuleException {
53
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");
60
61     long                sessionConnectTime = 0;
62     long                startTime = (new java.util.Date()).getTime();
63     String              whereClause = " ";
64     String              orderBy = " ";
65     String              htmlFileName = null;
66     String              currentMediaId;
67     EntityContent       currentContent;
68     EntityList          batchEntityList;
69     SimpleHash          imageHash = new SimpleHash();
70     EntityUsers         userEntity=null;
71     Entity              mediaType;
72     Entity              upMedia;
73     SimpleHash          upMediaSimpleHash;
74     Class               mediaHandlerClass;
75     Class               mediaStorageClass;
76     String              mediaStorageName;
77     String              mediaHandlerName=null;
78     MirMedia            mediaHandler=null;
79     Database            mediaStorage=null;
80
81
82
83     // production of the content-pages
84     orderBy="date desc, webdb_lastchange desc";
85     if(force==true){
86       whereClause="is_published='1'";
87       // if true: produces a single content item
88       if(id !=null){
89         whereClause += " AND id="+id;
90       }
91       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
92     } else {
93       whereClause="is_produced='0' AND is_published='1'";
94       //if true produces a single contentitem
95       if(id !=null){
96         whereClause += " AND id="+id;
97       }
98       batchEntityList = contentModule.getContent(whereClause, orderBy, 0, contentBatchsize, userEntity);
99     }
100
101     while (batchEntityList != null) {
102       for(int i=0;i<batchEntityList.size();i++) {
103         currentContent = (EntityContent)batchEntityList.elementAt(i);
104         try {
105             String date = currentContent.getValue("date");
106             String year = date.substring(0,4);
107             String month = date.substring(4,6);
108
109             htmlFileName =  producerDocRoot
110               + "/" + year + "/" + month + "/" +  currentContent.getValue("id") + ".shtml";
111
112             /** @todo all the following should take place in EntityContent */
113             // get the uploaded media
114             EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
115             if (currentMediaList!=null && currentMediaList.getCount()>=1) {
116               SimpleList mediaList = new SimpleList();
117               for (int n=0; n < currentMediaList.size();n++) {
118                 upMedia = currentMediaList.elementAt(n);
119                 upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
120                 mediaType = ((EntityMedia)upMedia).getMediaType();
121                 //must be a non-existant to_media_type entry..
122                 if (mediaType != null) {
123                   mediaHandlerName = mediaType.getValue("classname");
124                   mediaStorageName = mediaType.getValue("tablename");
125                   mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
126                   mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
127                   mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
128                   Method m = mediaStorageClass.getMethod("getInstance", null);
129                   mediaStorage = (Database)m.invoke(null, null);
130                   //we most likely need further info
131                   upMedia = mediaStorage.selectById(upMedia.getId());
132                   upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
133                   upMediaSimpleHash.put("type",mediaType.getValue("classname"));
134                   mediaList.add(upMediaSimpleHash);
135                 } //end if media_type != null
136               } //end for
137               mergeData.put("to_media", mediaList);
138             } //end if currentMediaList != null
139
140             // get the comments for the article
141             SimpleList commentList = currentContent.getComments();
142             mergeData.put("comments", commentList);
143
144             // get the topics of this article
145             mergeData.put("topics",HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(currentContent)));
146
147             //produce html
148             boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
149             sessionConnectTime = new java.util.Date().getTime() - startTime;
150             if (retVal == true && !"1".equals(currentContent.getValue("is_produced")))
151                 currentContent.setProduced(true);
152         } catch(Exception e) {
153           logHTML(htmlout, "Producer.Content ERROR while producing content ID: " + currentContent.getId()+",skipping it :: "+e.toString());
154           theLog.printError("Producer.Content ERROR while producing content ID: " + currentContent.getId() +",skipping it :: "+e.toString());
155         }
156
157
158       }//for
159
160       if (batchEntityList.hasNextBatch()){
161         batchEntityList = contentModule.getContent(whereClause, orderBy, batchEntityList.getNextBatch(),contentBatchsize, userEntity);
162       } else {
163         batchEntityList=null;
164       }
165     }
166
167     // timing and message to browser
168     sessionConnectTime = new java.util.Date().getTime() - startTime;
169     logHTML(htmlout, "Producer.Content finished: " + sessionConnectTime + " ms.");
170   }
171
172 }
173