generate email on the fly....and cache it for later use.
authorjohn <john>
Sun, 4 May 2003 12:56:33 +0000 (12:56 +0000)
committerjohn <john>
Sun, 4 May 2003 12:56:33 +0000 (12:56 +0000)
source/mircoders/servlet/ServletModuleOpenIndy.java

index 2c7ddc7..4072d74 100755 (executable)
@@ -123,7 +123,7 @@ import mircoders.storage.DatabaseTopics;
  *    open-postings to the newswire
  *
  * @author mir-coders group
- * @version $Id: ServletModuleOpenIndy.java,v 1.85 2003/05/04 11:42:07 john Exp $
+ * @version $Id: ServletModuleOpenIndy.java,v 1.86 2003/05/04 12:56:33 john Exp $
  *
  */
 
@@ -133,7 +133,7 @@ public class ServletModuleOpenIndy extends ServletModule
   private String        commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate;
   private String        postingFormTemplate, postingFormDoneTemplate, postingFormDupeTemplate;
   private String        searchResultsTemplate;
-  private String        prepareMailTemplate,sentMailTemplate;
+  private String        prepareMailTemplate,sentMailTemplate,emailAnArticleTemplate;
   private ModuleContent contentModule;
   private ModuleComment commentModule;
   private ModuleImages  imageModule;
@@ -158,6 +158,7 @@ public class ServletModuleOpenIndy extends ServletModule
 
       searchResultsTemplate = configuration.getString("ServletModule.OpenIndy.SearchResultsTemplate");
       prepareMailTemplate = configuration.getString("ServletModule.OpenIndy.PrepareMailTemplate");
+      emailAnArticleTemplate = configuration.getString("ServletModule.OpenIndy.MailableArticleTemplate");
       sentMailTemplate = configuration.getString("ServletModule.OpenIndy.SentMailTemplate");
       directOp = configuration.getString("DirectOpenposting").toLowerCase();
       commentModule = new ModuleComment(DatabaseComment.getInstance());
@@ -615,44 +616,35 @@ public class ServletModuleOpenIndy extends ServletModule
       if (from.indexOf('\n') != -1 || from.indexOf('\r') != -1 || from.indexOf(',') != -1 ) {
         throw new ServletModuleUserExc("email.error.invalidfromaddress", new String[] {from});
       }
-
-
-      EntityContent contentEnt;
-      try{
-        contentEnt = (EntityContent)contentModule.getById(aid);
+      
+      CacheKey theCacheKey=new CacheKey("email",aid+mail_language);
+      String theEmailText;
+      
+      if (MirGlobal.mruCache().hasObject(theCacheKey)){
+       logger.info("fetching email text for article "+aid+" from cache");
+       theEmailText = (String) MirGlobal.mruCache().getObject(theCacheKey);
       }
-      catch (Throwable e){
-        throw new ServletModuleFailure("Couldn't get content for article "+aid + ": " + e.getMessage(), e);
+      else {
+       EntityContent contentEnt;
+       try{
+         contentEnt = (EntityContent)contentModule.getById(aid);
+         StringWriter theEMailTextWriter=new StringWriter();
+         PrintWriter dest = new PrintWriter(theEMailTextWriter);
+         Map articleData = new HashMap();
+         articleData.put("article",mir.generator.FreemarkerGenerator.makeAdapter(contentEnt));
+         articleData.put("languagecode",mail_language);
+         deliver(dest,req, res, articleData, null, emailAnArticleTemplate,mail_language);
+         theEmailText=theEMailTextWriter.toString();
+         MirGlobal.mruCache().storeObject(theCacheKey,theEmailText);
+       }
+       catch (Throwable e){
+         throw new ServletModuleFailure("Couldn't get content for article "+aid + mail_language + ": " + e.getMessage(), e);
+       }
       }
+            
+      String content = theEmailText;
 
 
-
-
-      String producerStorageRoot=configuration.getString("Producer.StorageRoot");
-      String producerDocRoot=configuration.getString("Producer.DocRoot");
-      String publishPath = contentEnt.getValue("publish_path");
-      String txtFilePath = producerStorageRoot + producerDocRoot + "/" + mail_language +
-                                                                                                         publishPath + "/" + aid + ".txt";
-
-
-      File inputFile = new File(txtFilePath);
-      String content;
-
-      try{
-        FileReader in = new FileReader(inputFile);
-        StringWriter out = new StringWriter();
-        int c;
-        while ((c = in.read()) != -1)
-          out.write(c);
-        in.close();
-        content= out.toString();
-      }
-      catch (FileNotFoundException e){
-        throw new ServletModuleFailure("No text file found in " + txtFilePath, e);
-      }
-      catch (IOException e){
-        throw new ServletModuleFailure("Problem reading file in " + txtFilePath, e);
-      }
       // add some headers
       content = "To: " + to + "\nReply-To: "+ from + "\n" + content;
       // put in the comment where it should go
@@ -1119,6 +1111,27 @@ public class ServletModuleOpenIndy extends ServletModule
     }
   }
 
+  public void deliver(PrintWriter anOutputWriter, HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator,String aLocaleString)
+      throws ServletModuleFailure {
+    try {
+      Map responseData = ServletHelper.makeGenerationData(new Locale[] { new Locale(aLocaleString,""), getFallbackLocale(aRequest)}, "bundles.open");
+      responseData.put("data", aData);
+      responseData.put("extra", anExtra);
+
+
+      Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator);
+      generator.generate(anOutputWriter, responseData, logger);
+
+      anOutputWriter.close();
+    }
+    catch (Throwable e) {
+      logger.error("Error while generating " + aGenerator + ": " + e.getMessage());
+
+      throw new ServletModuleFailure(e);
+    }
+  }
+
+
   public void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse,PrintWriter out, Throwable anException) {
     try {
       logger.error("error: " + anException);