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