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