d3e05535cbde93e8f9df658e0f0a4e14a479b64b
[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         protected static Logfile theLog = Logfile.getInstance("Producer");
22         protected static ModuleTopics         topicsModule;
23   protected static ModuleLinksImcs      linksImcsModule;
24         protected static ModuleSchwerpunkt    schwerpunktModule;
25         protected static ModuleFeature        featureModule;
26         protected static ModuleContent        contentModule;
27         protected static ModuleImages         imageModule;
28         protected static ModuleUploadedMedia  uploadedMediaModule;
29
30   static {
31                 // init
32     try {
33                         contentModule = new ModuleContent(DatabaseContent.getInstance());
34                         topicsModule = new ModuleTopics(DatabaseTopics.getInstance());
35       linksImcsModule = new ModuleLinksImcs(DatabaseLinksImcs.getInstance());
36                         schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
37                         featureModule = new ModuleFeature(DatabaseFeature.getInstance());
38                         imageModule = new ModuleImages(DatabaseImages.getInstance());
39                         uploadedMediaModule = new ModuleUploadedMedia(DatabaseImages.getInstance());
40                 } catch(StorageObjectException e) {;}
41   }
42
43         public void handle(PrintWriter htmlout, EntityUsers user)
44                 throws StorageObjectException, ModuleException {
45                 handle(htmlout,user,false,false);
46         }
47
48         abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
49                 throws StorageObjectException, ModuleException;
50
51 //
52 // Methods for producing files
53
54         public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
55                 return _produce(template, filename, model, htmlout, false);
56         }
57
58         public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
59                 return _produce(template, filename, model, htmlout, true);
60         }
61
62         private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed) {
63                 try {
64                         File f = new File(producerStorageRoot + filename);
65                         File dir = new File(f.getParent());
66                         dir.mkdirs();
67                         FileWriter outputFile = new FileWriter(f);
68                         PrintWriter outStream;
69                         if (compressed==true) {
70                                 outStream = new LineFilterWriter(outputFile);
71                         } else {
72                                 outStream = new PrintWriter(outputFile);
73                         }
74
75                         HTMLTemplateProcessor.process(null,template, model, outStream);
76                         outputFile.close();
77                         outStream.close();
78
79                         printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+  filename + "\">" + filename + "</a>");
80                         //theLog.printInfo("Produced: " + producerStorageRoot + filename);
81       //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
82       //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
83                         return true;
84
85                 } catch(IOException exception){
86                         logHTML(htmlout, "Producer: File could not be written " + filename);
87                         return false;
88                 } catch(HTMLParseException exception){
89                         logHTML(htmlout,"Producer: Error in HTML-parsing: " + filename);
90                         return false;
91                 }
92         }
93
94         public boolean produceFile(String filename, byte[] in, PrintWriter htmlout, boolean icon) {
95
96                 boolean retVal = false;
97
98                 if (in!=null) {
99                         try {
100         File f = null;
101         if(icon==false){
102           f = new File(filename);
103           theLog.printDebugInfo("image: " + filename);
104         } else {
105           f = new File(producerStorageRoot + filename);
106           theLog.printDebugInfo("icon: " + filename);
107         }
108                                 File dir = new File(f.getParent());
109                                 dir.mkdirs();
110
111                                 FileOutputStream outStream;
112                                 outStream = new FileOutputStream(f);
113                                 outStream.write(in);
114                                 outStream.close();
115                                 retVal = true;
116                         } catch(IOException exception) {
117         logHTML(htmlout, "Producer: File could not be written: " + filename);
118       }
119                 }
120                 return retVal;
121         }
122
123
124
125         //
126         // filename methods
127
128         public String indexFileNameForPageCount(int pc) {
129                 return fileNameForPageCount("/index", pc);
130         }
131
132         public String fileNameForPageCount(String stub, int pc) {
133                 String fileName = producerDocRoot + stub;
134                 if (pc>1) {
135                         fileName+=pc;
136                 }
137                 fileName += ".html";
138                 return fileName;
139         }
140
141         /**
142          * logging
143          */
144         public void logHTML(PrintWriter out, String s) {
145                 _print(out, s, true);
146         }
147
148         public void printHTML(PrintWriter out, String s) {
149                 _print(out, s, false);
150         }
151
152         private void _print(PrintWriter out, String s, boolean log) {
153                 if (out != null) { out.println(s+"<br>");out.flush(); }
154                 if (log == true) {
155                         theLog.printInfo(s);
156                 }
157         }
158
159 }