From 6c8c4f22a1504f4794c6676e2e8f40053996b93d Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Mon, 25 Nov 2019 15:23:19 +0100
Subject: [PATCH] add error message if template file is missing in asys
 binaries, fix update of template file

---
 .../documenttemplate_id/onValueChange.js      |  2 +-
 .../recordcontainers/db/onDBInsert.js         |  2 +-
 .../recordcontainers/db/onDBUpdate.js         |  2 +-
 .../downloadtemplate/onActionProcess.js       |  2 +-
 .../downloadtemplate/onActionProcess.js       |  2 +-
 .../recordcontainers/jdito/onInsert.js        | 25 +++++++++++--------
 .../downloadtemplate/onActionProcess.js       |  2 +-
 .../_____LANGUAGE_EXTRA.aod                   |  6 +++++
 .../_____LANGUAGE_de/_____LANGUAGE_de.aod     |  8 ++++++
 .../_____LANGUAGE_en/_____LANGUAGE_en.aod     |  6 +++++
 process/DocumentTemplate_lib/process.js       | 15 ++++++++---
 process/Email_lib/process.js                  | 10 ++++++++
 12 files changed, 61 insertions(+), 21 deletions(-)

diff --git a/entity/BulkMail_entity/entityfields/documenttemplate_id/onValueChange.js b/entity/BulkMail_entity/entityfields/documenttemplate_id/onValueChange.js
index 29a05d9cfb..6673df6b95 100644
--- a/entity/BulkMail_entity/entityfields/documenttemplate_id/onValueChange.js
+++ b/entity/BulkMail_entity/entityfields/documenttemplate_id/onValueChange.js
@@ -16,7 +16,7 @@ if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW || vars.get("$sys.re
     // TODO: load subject without loadTemplate again... maybe return with getTemplateContent() above?
     var templateId = vars.get("local.value");
     var template = DocumentTemplate.loadTemplate(templateId, undefined, false);
-    if (template.type == DocumentTemplate.types.EML)
+    if (template && template.type == DocumentTemplate.types.EML)
     {
         let subject = mail.parseRFC(util.decodeBase64String(template.content))[mail.MAIL_SUBJECT];
         neon.setFieldValue("$field.SUBJECT", subject);
diff --git a/entity/BulkMail_entity/recordcontainers/db/onDBInsert.js b/entity/BulkMail_entity/recordcontainers/db/onDBInsert.js
index 93eba9d8b9..06720b44b7 100644
--- a/entity/BulkMail_entity/recordcontainers/db/onDBInsert.js
+++ b/entity/BulkMail_entity/recordcontainers/db/onDBInsert.js
@@ -19,7 +19,7 @@ var bulkMailName = rowdata["BULKMAIL.NAME"];
 
 var template = DocumentTemplate.getSelectedTemplate(templateId, new FileUpload(bindata), content, DocumentTemplate.types.HTML);
 
-if (template.content)
+if (template && template.content)
 {
     if (!template.filename)
         template.filename = bulkMailName + ".html";
diff --git a/entity/BulkMail_entity/recordcontainers/db/onDBUpdate.js b/entity/BulkMail_entity/recordcontainers/db/onDBUpdate.js
index daf4625956..a22303d714 100644
--- a/entity/BulkMail_entity/recordcontainers/db/onDBUpdate.js
+++ b/entity/BulkMail_entity/recordcontainers/db/onDBUpdate.js
@@ -19,7 +19,7 @@ var bulkMailName = rowdata["BULKMAIL.NAME"];
 
 var template = DocumentTemplate.getSelectedTemplate(templateId, new FileUpload(bindata), content, DocumentTemplate.types.HTML);
 
-if (template.content)
+if (template && template.content)
 {
     if (!template.filename)
         template.filename = bulkMailName + ".html";
diff --git a/entity/Email_entity/entityfields/downloadtemplate/onActionProcess.js b/entity/Email_entity/entityfields/downloadtemplate/onActionProcess.js
index c3e5346cbc..29a0e6a4ef 100644
--- a/entity/Email_entity/entityfields/downloadtemplate/onActionProcess.js
+++ b/entity/Email_entity/entityfields/downloadtemplate/onActionProcess.js
@@ -4,5 +4,5 @@ import("system.vars");
 
 var templateId = vars.get("$field.DOCUMENT_TEMPLATE");
 var template = DocumentTemplate.loadTemplate(templateId, undefined, false);
-if (template.content)
+if (template && template.content)
     neon.download(template.content, template.filename);
\ No newline at end of file
diff --git a/entity/Letter_entity/entityfields/downloadtemplate/onActionProcess.js b/entity/Letter_entity/entityfields/downloadtemplate/onActionProcess.js
index c3e5346cbc..29a0e6a4ef 100644
--- a/entity/Letter_entity/entityfields/downloadtemplate/onActionProcess.js
+++ b/entity/Letter_entity/entityfields/downloadtemplate/onActionProcess.js
@@ -4,5 +4,5 @@ import("system.vars");
 
 var templateId = vars.get("$field.DOCUMENT_TEMPLATE");
 var template = DocumentTemplate.loadTemplate(templateId, undefined, false);
-if (template.content)
+if (template && template.content)
     neon.download(template.content, template.filename);
\ No newline at end of file
diff --git a/entity/Letter_entity/recordcontainers/jdito/onInsert.js b/entity/Letter_entity/recordcontainers/jdito/onInsert.js
index 1f265afe9a..f82590e6fc 100644
--- a/entity/Letter_entity/recordcontainers/jdito/onInsert.js
+++ b/entity/Letter_entity/recordcontainers/jdito/onInsert.js
@@ -8,16 +8,19 @@ import("DocumentTemplate_lib");
 import("KeywordRegistry_basic");
 
 var template = DocumentTemplate.getSelectedTemplate(vars.get("$field.DOCUMENT_TEMPLATE"), new FileUpload(vars.get("$field.bindata")));
-var contactId = vars.get("$param.ContactId_param");
-var content = template.getReplacedContentByContactId(contactId, true);
-if (template.type)
-    neon.download(content, template.filename);
-
-var links = [];
-if (contactId)
+if (template)
 {
-    let context = ContactUtils.getContextByContactId(contactId);
-    links.push([context, contactId]);
-}
+    var contactId = vars.get("$param.ContactId_param");
+    var content = template.getReplacedContentByContactId(contactId, true);
+    if (template.type)
+        neon.download(content, template.filename);
 
-ActivityUtils.createNewActivity(null, links, null, null, translate.text("Letter"), text.parseDocument(content), $KeywordRegistry.activityDirection$outgoing(), [[template.filename, content, false]]);
+    var links = [];
+    if (contactId)
+    {
+        let context = ContactUtils.getContextByContactId(contactId);
+        links.push([context, contactId]);
+    }
+
+    ActivityUtils.createNewActivity(null, links, null, null, translate.text("Letter"), text.parseDocument(content), $KeywordRegistry.activityDirection$outgoing(), [[template.filename, content, false]]);
+}
diff --git a/entity/SerialLetter_entity/entityfields/downloadtemplate/onActionProcess.js b/entity/SerialLetter_entity/entityfields/downloadtemplate/onActionProcess.js
index 518d384f32..c7cef2b830 100644
--- a/entity/SerialLetter_entity/entityfields/downloadtemplate/onActionProcess.js
+++ b/entity/SerialLetter_entity/entityfields/downloadtemplate/onActionProcess.js
@@ -3,5 +3,5 @@ import("system.vars");
 import("DocumentTemplate_lib");
 
 var template = DocumentTemplate.loadTemplate(vars.get("$field.DOCUMENTTEMPLATE_ID"), undefined, false);
-if (template.type)
+if (template && template.type)
     neon.download(template.content, template.filename);
\ No newline at end of file
diff --git a/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod b/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
index cce8452878..2482b08216 100644
--- a/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
+++ b/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
@@ -5909,6 +5909,12 @@
     <entry>
       <key>SqlBuilder: invalid value-type for pFieldOrCond. It can be a fully qualified SqlBuilder (e.g. select, from, ... have to be set) or an jdito-prepared-statement array</key>
     </entry>
+    <entry>
+      <key>Error</key>
+    </entry>
+    <entry>
+      <key>File from this template could not be found anymore. Please go to the template and upload a new file.</key>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
   <sqlModels>
diff --git a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
index b11ee95e61..3ae5c657ab 100644
--- a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
+++ b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
@@ -7423,6 +7423,14 @@
       <key>Dataset</key>
       <value>Datensatz</value>
     </entry>
+    <entry>
+      <key>Error</key>
+      <value>Fehler</value>
+    </entry>
+    <entry>
+      <key>File from this template could not be found anymore. Please go to the template and upload a new file.</key>
+      <value>Die Datei dieser Vorlage konnte nicht mehr gefunden werde. Bitte laden Sie bei der Dokumentvorlage eine neue Datei hoch.</value>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
 </language>
diff --git a/language/_____LANGUAGE_en/_____LANGUAGE_en.aod b/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
index 85f45888ae..27e36d62c2 100644
--- a/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
+++ b/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
@@ -5959,6 +5959,12 @@
     <entry>
       <key>SqlBuilder: invalid value-type for pFieldOrCond. It can be a fully qualified SqlBuilder (e.g. select, from, ... have to be set) or an jdito-prepared-statement array</key>
     </entry>
+    <entry>
+      <key>Error</key>
+    </entry>
+    <entry>
+      <key>File from this template could not be found anymore. Please go to the template and upload a new file.</key>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
 </language>
diff --git a/process/DocumentTemplate_lib/process.js b/process/DocumentTemplate_lib/process.js
index 6a72035ce5..c03ac5eba7 100644
--- a/process/DocumentTemplate_lib/process.js
+++ b/process/DocumentTemplate_lib/process.js
@@ -253,7 +253,12 @@ DocumentTemplate.loadTemplate = function (pAssignmentRowId, pAssignmentTable, pR
     
     if (templateId)
     {
-        return DocumentTemplateUtils.getTemplate(templateId, pResolveSubtemplates)
+        var template = DocumentTemplateUtils.getTemplate(templateId, pResolveSubtemplates);
+        if (template == null)
+        {
+            question.showMessage(translate.text("File from this template could not be found anymore. Please go to the template and upload a new file."), question.ERROR, translate.text("Error"));
+            return null;
+        }
     }
     
     var templateDocument = db.getBinaryMetadata(pAssignmentTable, "DOCUMENT", pAssignmentRowId, false, alias, null);
@@ -305,7 +310,11 @@ DocumentTemplate.getSelectedTemplate = function (pTemplateId, pDocumentUpload, p
     if (pDocumentUpload.isFilled())
         template = DocumentTemplate.fromUpload(pDocumentUpload);
     else if (pTemplateId)
+    {
         template = DocumentTemplate.loadTemplate(pTemplateId);
+        if (!template)
+            return null;
+    }
     else
         template = new DocumentTemplate(null, pDefaultType || DocumentTemplate.types.TXT, null, true);
     
@@ -826,9 +835,8 @@ DocumentTemplateUtils.updateTemplateData = function (pTemplateId, pFileUpload, p
         var assignmentName= "DOCUMENT";
         var keyword = "TEMPLATE";
         var binMeta = db.getBinaryMetadata(assignmentTable, assignmentName, pTemplateId, false, SqlUtils.getBinariesAlias(), keyword);
-        
         if (binMeta.length == 0)
-            SingleBinaryUtils.insertMainDocument(assignmentTable, assignmentName, pTemplateId, pFileUpload.bindata, pFileUpload.filename, null, SqlUtils.getBinariesAlias());
+            SingleBinaryUtils.insert(assignmentTable, assignmentName, pTemplateId, pFileUpload.bindata, pFileUpload.filename, null, "TEMPLATE", SqlUtils.getBinariesAlias());
         else
             db.updateBinary(binMeta[0][db.BINARY_ID], "", pFileUpload.bindata, pFileUpload.filename, "", keyword, SqlUtils.getBinariesAlias());
     }
@@ -884,7 +892,6 @@ DocumentTemplateUtils.getTemplate = function (pTemplateId, pResolveSubtemplates)
     var keyword = "TEMPLATE";
 
     var binMeta = db.getBinaryMetadata(assignmentTable, assignmentName, pTemplateId, false, SqlUtils.getBinariesAlias(), keyword);
-
     if (binMeta.length != 0)
     {                   
         return new DocumentTemplate(
diff --git a/process/Email_lib/process.js b/process/Email_lib/process.js
index 64162b4cdb..ac0b91e769 100644
--- a/process/Email_lib/process.js
+++ b/process/Email_lib/process.js
@@ -33,9 +33,14 @@ EmailWritingUtils.openMailTemplate = function (pToRecipients, pSenderContactId,
     
     var email;
     if (pTemplateId || pBindata)
+    {
         email = Email.fromTemplate(pTemplateId, pRecipientContactId, pBindata)
+        if (!email)
+            email = new Email();
+    }    
     else
         email = new Email();
+    
     email.setSender(pSenderContactId);
 
     if (pToRecipients)
@@ -110,7 +115,12 @@ Email.fromTemplate = function (pTemplateId, pContactId, pBindata)
     if (pBindata && pBindata.isFilled() && BulkMailUtils.isValidMimeType(pBindata.mimeType))
         template = new DocumentTemplate(pBindata.bindata, DocumentTemplate.types.fromMimeType(pBindata.mimeType), pBindata.filename, true);
     else
+    {
         template = DocumentTemplate.loadTemplate(pTemplateId);
+        if (!template)
+            return null;
+    }
+        
         
     return Email.getReplacedBulkEmails(template, [pContactId])[pContactId];
 }
-- 
GitLab