i18n-feature based on kellans proposal implemented. the resource-bundles are located...
[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         }
67
68         public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
69                 return _produce(template, filename, model, htmlout, true);
70         }
71
72         private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed) {
73                 try {
74                         File f = new File(producerStorageRoot + filename);
75                         File dir = new File(f.getParent());
76                         dir.mkdirs();
77                         FileWriter outputFile = new FileWriter(f);
78                         PrintWriter outStream;
79                         if (compressed==true) {
80                                 outStream = new LineFilterWriter(outputFile);
81                         } else {
82                                 outStream = new PrintWriter(outputFile);
83                         }
84
85                         HTMLTemplateProcessor.process(null,template, model, outStream,null);
86                         outputFile.close();
87                         outStream.close();
88
89                         printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+
90                         filename + "\">" + filename + "</a>");
91                         //theLog.printInfo("Produced: " + producerStorageRoot + filename);
92                         //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
93       //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
94                         return true;
95
96                 } catch(IOException exception){
97                         logHTML(htmlout, "Producer: File could not be written " + filename);
98                         return false;
99                 } catch(HTMLParseException exception){
100                         logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
101                         return false;
102                 }
103         }
104
105         public boolean produceFile(String filename, byte[] in, PrintWriter htmlout, boolean icon) {
106
107                 boolean retVal = false;
108
109                 if (in!=null) {
110                         try {
111         File f = null;
112         if(icon==false){
113           f = new File(filename);
114           theLog.printDebugInfo("image: " + filename);
115         } else {
116           f = new File(producerStorageRoot + filename);
117           theLog.printDebugInfo("icon: " + filename);
118         }
119                                 File dir = new File(f.getParent());
120                                 dir.mkdirs();
121
122                                 FileOutputStream outStream;
123                                 outStream = new FileOutputStream(f);
124                                 outStream.write(in);
125                                 outStream.close();
126                                 retVal = true;
127                         } catch(IOException exception) {
128         logHTML(htmlout, "Producer: File could not be written: " + filename);
129       }
130                 }
131                 return retVal;
132         }
133
134
135
136         //
137         // filename methods
138
139         public String indexFileNameForPageCount(int pc) {
140                 return fileNameForPageCount("/index", pc);
141         }
142
143         public String fileNameForPageCount(String stub, int pc) {
144                 String fileName = producerDocRoot + stub;
145                 if (pc>1) {
146                         fileName+=pc;
147                 }
148                 fileName += ".html";
149                 return fileName;
150         }
151
152         /**
153          * logging
154          */
155
156   public void logHTMLFinish(PrintWriter htmlout,String moduleName, int pageCount, long startTime, long endTime) {
157     // timing and message to browser
158     long overall = endTime - startTime;
159     int pagesPerMinute=0; float perMinute = (float)overall/(float)60000;
160     if (perMinute >0) pagesPerMinute = (int) ((float)pageCount / perMinute);
161
162     logHTML(htmlout, "Producer."+moduleName+" finished producing: " +
163             overall + " ms for "+ pageCount+" Pages = " +pagesPerMinute + " pages/min");
164     logHTML(htmlout, "Back to <a href=\""+actionRoot+"\">Admin-Startage</a>");
165   }
166
167         public void logHTML(PrintWriter out, String s) {
168                 _print(out, s, true);
169         }
170
171         public void printHTML(PrintWriter out, String s) {
172                 _print(out, s, false);
173         }
174
175         private void _print(PrintWriter out, String s, boolean log) {
176                 if (out != null) { out.println(s+"<br>");out.flush(); }
177                 if (log == true) {
178                         theLog.printInfo(s);
179                 }
180         }
181
182 }