960dac833e72bc47e35705fe2e297efb53580c74
[mir.git] / source / mircoders / producer / Producer.java
1 package mircoders.producer;
2
3 import java.io.*;
4
5 import freemarker.template.*;
6
7 import mir.misc.*;
8 import mir.storage.*;
9 import mir.module.*;
10
11 import mircoders.module.*;
12 import mircoders.entity.*;
13 import mircoders.storage.*;
14
15 abstract public class Producer {
16
17   protected static String   producerDocRoot = MirConfig.getProp("Producer.DocRoot");
18   protected static String   producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
19   protected static String   producerProductionHost = MirConfig.getProp("Producer.ProductionHost");
20   protected static String   producerOpenAction = MirConfig.getProp("Producer.OpenAction");;
21
22   /** @todo same as in HTMLTemplateProcessor, this should be dynamically set */
23   protected static String   actionRoot = MirConfig.getProp("RootUri") + "/servlet/Mir";
24
25   protected static Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home") + "/" + MirConfig.getProp("Producer.Logfile"));
26   protected static ModuleTopics         topicsModule;
27   protected static ModuleLinksImcs      linksImcsModule;
28   protected static ModuleSchwerpunkt    schwerpunktModule;
29   protected static ModuleFeature        featureModule;
30   protected static ModuleContent        contentModule;
31   protected static ModuleImages         imageModule;
32   protected static ModuleUploadedMedia  uploadedMediaModule;
33
34   static {
35                 // init
36     try {
37
38       contentModule = new ModuleContent(DatabaseContent.getInstance());
39       topicsModule = new ModuleTopics(DatabaseTopics.getInstance());
40       linksImcsModule = new ModuleLinksImcs(DatabaseLinksImcs.getInstance());
41       schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
42       featureModule = new ModuleFeature(DatabaseFeature.getInstance());
43       imageModule = new ModuleImages(DatabaseImages.getInstance());
44       uploadedMediaModule = new ModuleUploadedMedia(DatabaseImages.getInstance());
45
46     }
47     catch(StorageObjectException e)
48     {
49       System.err.println("*** failed to initialize Producer " + e.toString());
50     }
51   }
52
53         public void handle(PrintWriter htmlout, EntityUsers user)
54                 throws StorageObjectException, ModuleException {
55                 handle(htmlout,user,false,false);
56         }
57
58         abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
59                 throws StorageObjectException, ModuleException;
60
61 //
62 // Methods for producing files
63
64         public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
65                 return _produce(template, filename, model, htmlout, false,
66                     MirConfig.getProp("Mir.DefaultEncoding"));
67         }
68
69         public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, String encoding) {
70                 return _produce(template, filename, model, htmlout, false, encoding);
71         }
72
73         public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
74                 return _produce(template, filename, model, htmlout, true,
75                     MirConfig.getProp("Mir.DefaultEncoding"));
76         }
77
78         private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed, String encoding) {
79                 try {
80                         File f = new File(producerStorageRoot + filename);
81                         File dir = new File(f.getParent());
82                         dir.mkdirs();
83                         // it's important that we set the desired encoding. It should be UTF8
84       // not the platform default.
85       OutputStreamWriter outputFileStream =
86         new OutputStreamWriter(new FileOutputStream(f), encoding);
87                         PrintWriter outStream;
88                         if (compressed==true) {
89                                 outStream = new LineFilterWriter(outputFileStream);
90                         } else {
91                                 outStream = new PrintWriter(outputFileStream);
92                         }
93
94                         HTMLTemplateProcessor.process(null,template, model, outStream,null);
95                         outputFileStream.close();
96                         outStream.close();
97
98                         printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+producerDocRoot +
99                         filename + "\">" + filename + "</a>");
100                         //theLog.printInfo("Produced: " + producerStorageRoot + filename);
101         //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
102       //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
103                         return true;
104
105                 } catch(IOException exception){
106                         logHTML(htmlout, "Producer: File could not be written " + filename);
107       System.out.println(exception.toString());
108                         return false;
109                 } catch(HTMLParseException exception){
110                         logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
111                         return false;
112                 }
113         }
114
115         //
116         // filename methods
117
118         public String indexFileNameForPageCount(int pc) {
119                 return fileNameForPageCount("/index", pc);
120         }
121
122         public String fileNameForPageCount(String stub, int pc) {
123                 String fileName = producerDocRoot + stub;
124                 if (pc>1) {
125                         fileName+=pc;
126                 }
127                 fileName += ".html";
128                 return fileName;
129         }
130
131         /**
132          * logging
133          */
134
135   public void logHTMLFinish(PrintWriter htmlout,String moduleName, int pageCount, long startTime, long endTime) {
136     // timing and message to browser
137     long overall = endTime - startTime;
138     int pagesPerMinute=0; float perMinute = (float)overall/(float)60000;
139     if (perMinute >0) pagesPerMinute = (int) ((float)pageCount / perMinute);
140
141     logHTML(htmlout, "Producer."+moduleName+" finished producing: " +
142             overall + " ms for "+ pageCount+" Pages = " +pagesPerMinute + " pages/min");
143     printHTML(htmlout, "Back to <a href=\""+actionRoot+"\">Admin-Startage</a>");
144   }
145
146         public void logHTML(PrintWriter out, String s) {
147                 _print(out, s, true);
148         }
149
150         public void printHTML(PrintWriter out, String s) {
151                 _print(out, s, false);
152         }
153
154         private void _print(PrintWriter out, String s, boolean log) {
155                 if (out != null) { out.println(s+"<br />");out.flush(); }
156                 if (log == true) {
157                         theLog.printInfo(s);
158                 }
159         }
160
161 }