Initial revision
[mir.git] / source / mircoders / producer / Producer.java
diff --git a/source/mircoders/producer/Producer.java b/source/mircoders/producer/Producer.java
new file mode 100755 (executable)
index 0000000..d480796
--- /dev/null
@@ -0,0 +1,157 @@
+package mircoders.producer;
+
+import java.io.*;
+
+import freemarker.template.*;
+
+import webdb.misc.*;
+import webdb.storage.*;
+import webdb.module.*;
+
+import mir.module.*;
+import mir.entity.*;
+import mir.storage.*;
+
+abstract public class Producer {
+
+       protected static String producerDocRoot = Configuration.getProperty("Producer.DocRoot");
+  protected static String producerStorageRoot = Configuration.getProperty("Producer.StorageRoot");
+       protected static String producerProductionHost = Configuration.getProperty("Producer.ProductionHost");
+       protected static String producerOpenAction = Configuration.getProperty("Producer.OpenAction");;
+       protected static Logfile theLog = Logfile.getInstance(Configuration.getProperty("Home") + Configuration.getProperty("Producer.Logfile"));
+       protected static ModuleTopics         topicsModule;
+       protected static ModuleSchwerpunkt    schwerpunktModule;
+       protected static ModuleFeature        featureModule;
+       protected static ModuleGruppen        gruppenModule;
+       protected static ModuleContent        contentModule;
+       protected static ModuleImages         imageModule;
+
+  static {
+               // init
+    try {
+                       contentModule = new ModuleContent(DatabaseContent.getInstance());
+                       gruppenModule = new ModuleGruppen(DatabaseGroups.getInstance());
+                       topicsModule = new ModuleTopics(DatabaseTopics.getInstance());
+                       schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
+                       featureModule = new ModuleFeature(DatabaseFeature.getInstance());
+                       imageModule = new ModuleImages(DatabaseImages.getInstance());
+               } catch(StorageObjectException e) {;}
+  }
+
+       public void handle(PrintWriter htmlout, EntityUsers user)
+               throws StorageObjectException, ModuleException {
+               handle(htmlout,user,false,false);
+       }
+
+       abstract public void handle(PrintWriter htmlout, EntityUsers user, boolean forced, boolean sync)
+               throws StorageObjectException, ModuleException;
+
+//
+// Produktionsroutine
+
+       public boolean produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
+               return _produce(template, filename, model, htmlout, false);
+       }
+
+       public boolean produce_compressed(String template, String filename, TemplateModelRoot model, PrintWriter htmlout) {
+               return _produce(template, filename, model, htmlout, true);
+       }
+
+       private boolean _produce(String template, String filename, TemplateModelRoot model, PrintWriter htmlout, boolean compressed) {
+               try {
+                       File f = new File(producerStorageRoot + filename);
+                       File dir = new File(f.getParent());
+                       dir.mkdirs();
+                       FileWriter outputFile = new FileWriter(f);
+                       PrintWriter outStream;
+                       if (compressed==true) {
+                               outStream = new LineFilterWriter(outputFile);
+                       } else {
+                               outStream = new PrintWriter(outputFile);
+                       }
+
+                       HTMLTemplateProcessor.process(template, model, outStream);
+                       outputFile.close();
+                       outStream.close();
+
+                       printHTML(htmlout, "Produced <a href=\"" + producerProductionHost+  filename + "\">" + filename + "</a>");
+                       //theLog.printInfo("Produced: " + producerStorageRoot + filename);
+      //theLog.printDebugInfo("free mem:" + java.lang.Runtime.getRuntime().freeMemory());
+      //theLog.printDebugInfo("total mem:" + java.lang.Runtime.getRuntime().totalMemory());
+                       return true;
+
+               } catch(IOException exception){
+                       logHTML(htmlout, "Producer: File konnte nicht zum schreiben geoeffnet werden: " + filename);
+                       return false;
+               } catch(HTMLParseException exception){
+                       logHTML(htmlout,"Producer: File konnte nicht hatemelisiert  werden: " + filename);
+                       return false;
+               }
+       }
+
+       public boolean produceFile(String filename, byte[] in, PrintWriter htmlout, boolean icon) {
+
+               boolean retVal = false;
+
+               if (in!=null) {
+                       try {
+        File f = null;
+        if(icon==false){
+          f = new File(filename);
+          theLog.printDebugInfo("image: " + filename);
+        } else {
+          f = new File(producerStorageRoot + filename);
+          theLog.printDebugInfo("icon: " + filename);
+        }
+                               File dir = new File(f.getParent());
+                               dir.mkdirs();
+
+                               FileOutputStream outStream;
+                               outStream = new FileOutputStream(f);
+                               outStream.write(in);
+                               outStream.close();
+                               retVal = true;
+                       } catch(IOException exception) {
+        logHTML(htmlout, "Producer: File could not be written: " + filename);
+      }
+               }
+               return retVal;
+       }
+
+
+
+       //
+       // filename routinen
+
+       public String indexFileNameForPageCount(int pc) {
+               return fileNameForPageCount("/index", pc);
+       }
+
+       public String fileNameForPageCount(String stub, int pc) {
+               String fileName = producerDocRoot + stub;
+               if (pc>1) {
+                       fileName+=pc;
+               }
+               fileName += ".html";
+               return fileName;
+       }
+
+       /**
+        * logging
+        */
+       public void logHTML(PrintWriter out, String s) {
+               _print(out, s, true);
+       }
+
+       public void printHTML(PrintWriter out, String s) {
+               _print(out, s, false);
+       }
+
+       private void _print(PrintWriter out, String s, boolean log) {
+               if (out != null) { out.println(s+"<br>");out.flush(); }
+               if (log == true) {
+                       theLog.printInfo(s);
+               }
+       }
+
+}