producer topic debug
[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);
86                         outputFile.close();
87                         outStream.close();
88
89                         printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+  filename + "\">" + filename + "</a>");
90                         //theLog.printInfo("Produced: " + producerStorageRoot + filename);
91                         //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
92       //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
93                         return true;
94
95                 } catch(IOException exception){
96                         logHTML(htmlout, "Producer: File could not be written " + filename);
97                         return false;
98                 } catch(HTMLParseException exception){
99                         logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
100                         return false;
101                 }
102         }
103
104         public boolean produceFile(String filename, byte[] in, PrintWriter htmlout, boolean icon) {
105
106                 boolean retVal = false;
107
108                 if (in!=null) {
109                         try {
110         File f = null;
111         if(icon==false){
112           f = new File(filename);
113           theLog.printDebugInfo("image: " + filename);
114         } else {
115           f = new File(producerStorageRoot + filename);
116           theLog.printDebugInfo("icon: " + filename);
117         }
118                                 File dir = new File(f.getParent());
119                                 dir.mkdirs();
120
121                                 FileOutputStream outStream;
122                                 outStream = new FileOutputStream(f);
123                                 outStream.write(in);
124                                 outStream.close();
125                                 retVal = true;
126                         } catch(IOException exception) {
127         logHTML(htmlout, "Producer: File could not be written: " + filename);
128       }
129                 }
130                 return retVal;
131         }
132
133
134
135         //
136         // filename methods
137
138         public String indexFileNameForPageCount(int pc) {
139                 return fileNameForPageCount("/index", pc);
140         }
141
142         public String fileNameForPageCount(String stub, int pc) {
143                 String fileName = producerDocRoot + stub;
144                 if (pc>1) {
145                         fileName+=pc;
146                 }
147                 fileName += ".html";
148                 return fileName;
149         }
150
151         /**
152          * logging
153          */
154         public void logHTML(PrintWriter out, String s) {
155                 _print(out, s, true);
156         }
157
158         public void printHTML(PrintWriter out, String s) {
159                 _print(out, s, false);
160         }
161
162         private void _print(PrintWriter out, String s, boolean log) {
163                 if (out != null) { out.println(s+"<br>");out.flush(); }
164                 if (log == true) {
165                         theLog.printInfo(s);
166                 }
167         }
168
169 }