From 2e4ba254fe0fa82884539b66284afcf9988e0056 Mon Sep 17 00:00:00 2001
From: "S.Listl" <S.Listl@SLISTL.aditosoftware.local>
Date: Thu, 1 Aug 2019 15:04:44 +0200
Subject: [PATCH] Refactoring

---
 entity/BulkMail_entity/BulkMail_entity.aod    |  42 +++---
 .../addtobulkmailfromtable/tooltipProcess.js  |   2 +-
 .../addtoserialletter/tooltipProcess.js       |   2 +-
 .../SerialLetter_entity.aod                   |   4 +
 .../_____LANGUAGE_EXTRA.aod                   |  12 ++
 .../_____LANGUAGE_de/_____LANGUAGE_de.aod     |  20 +++
 .../_____LANGUAGE_en/_____LANGUAGE_en.aod     |  12 ++
 process/DocumentTemplate_lib/process.js       | 123 ++++++++++--------
 process/JditoFilter_lib/process.js            |  11 +-
 9 files changed, 149 insertions(+), 79 deletions(-)

diff --git a/entity/BulkMail_entity/BulkMail_entity.aod b/entity/BulkMail_entity/BulkMail_entity.aod
index f2ff471f468..3263111aa59 100644
--- a/entity/BulkMail_entity/BulkMail_entity.aod
+++ b/entity/BulkMail_entity/BulkMail_entity.aod
@@ -88,14 +88,6 @@
       <mandatory v="true" />
       <onValidation>%aditoprj%/entity/BulkMail_entity/entityfields/sender/onValidation.js</onValidation>
     </entityField>
-    <entityActionField>
-      <name>sendMail</name>
-      <title>Send</title>
-      <onActionProcess>%aditoprj%/entity/BulkMail_entity/entityfields/sendmail/onActionProcess.js</onActionProcess>
-      <iconId>VAADIN:PAPERPLANE</iconId>
-      <stateProcess>%aditoprj%/entity/BulkMail_entity/entityfields/sendmail/stateProcess.js</stateProcess>
-      <tooltipProcess>%aditoprj%/entity/BulkMail_entity/entityfields/sendmail/tooltipProcess.js</tooltipProcess>
-    </entityActionField>
     <entityField>
       <name>ICON</name>
       <contentType>IMAGE</contentType>
@@ -127,13 +119,6 @@
         <element>SUBJECT</element>
       </fields>
     </entityFieldGroup>
-    <entityActionField>
-      <name>openAdminView</name>
-      <title>Open admin view</title>
-      <onActionProcess>%aditoprj%/entity/BulkMail_entity/entityfields/openadminview/onActionProcess.js</onActionProcess>
-      <iconId>VAADIN:CURLY_BRACKETS</iconId>
-      <stateProcess>%aditoprj%/entity/BulkMail_entity/entityfields/openadminview/stateProcess.js</stateProcess>
-    </entityActionField>
     <entityParameter>
       <name>PresetRecipients_param</name>
       <expose v="true" />
@@ -180,12 +165,6 @@
         </entityParameter>
       </children>
     </entityConsumer>
-    <entityActionField>
-      <name>testMail</name>
-      <title>Test email</title>
-      <onActionProcess>%aditoprj%/entity/BulkMail_entity/entityfields/testmail/onActionProcess.js</onActionProcess>
-      <iconId>VAADIN:CLIPBOARD_CHECK</iconId>
-    </entityActionField>
     <entityField>
       <name>TESTING_CONTACT_ID</name>
       <title>Testing contact</title>
@@ -208,6 +187,27 @@
         <fieldName>#PROVIDER</fieldName>
       </dependency>
     </entityConsumer>
+    <entityActionField>
+      <name>sendMail</name>
+      <title>Send</title>
+      <onActionProcess>%aditoprj%/entity/BulkMail_entity/entityfields/sendmail/onActionProcess.js</onActionProcess>
+      <iconId>VAADIN:PAPERPLANE</iconId>
+      <stateProcess>%aditoprj%/entity/BulkMail_entity/entityfields/sendmail/stateProcess.js</stateProcess>
+      <tooltipProcess>%aditoprj%/entity/BulkMail_entity/entityfields/sendmail/tooltipProcess.js</tooltipProcess>
+    </entityActionField>
+    <entityActionField>
+      <name>testMail</name>
+      <title>Test email</title>
+      <onActionProcess>%aditoprj%/entity/BulkMail_entity/entityfields/testmail/onActionProcess.js</onActionProcess>
+      <iconId>VAADIN:CLIPBOARD_CHECK</iconId>
+    </entityActionField>
+    <entityActionField>
+      <name>openAdminView</name>
+      <title>Open admin view</title>
+      <onActionProcess>%aditoprj%/entity/BulkMail_entity/entityfields/openadminview/onActionProcess.js</onActionProcess>
+      <iconId>VAADIN:CURLY_BRACKETS</iconId>
+      <stateProcess>%aditoprj%/entity/BulkMail_entity/entityfields/openadminview/stateProcess.js</stateProcess>
+    </entityActionField>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
diff --git a/entity/Person_entity/entityfields/campaignactiongroup/children/addtobulkmailfromtable/tooltipProcess.js b/entity/Person_entity/entityfields/campaignactiongroup/children/addtobulkmailfromtable/tooltipProcess.js
index 81d6fafaa07..76c0ea1a0d6 100644
--- a/entity/Person_entity/entityfields/campaignactiongroup/children/addtobulkmailfromtable/tooltipProcess.js
+++ b/entity/Person_entity/entityfields/campaignactiongroup/children/addtobulkmailfromtable/tooltipProcess.js
@@ -1,4 +1,4 @@
 import("system.translate");
 import("system.result");
 
-result.string(translate.text("Adds the selection to a bulk mail"));
\ No newline at end of file
+result.string(translate.text("Add the selection to a bulk mail"));
\ No newline at end of file
diff --git a/entity/Person_entity/entityfields/campaignactiongroup/children/addtoserialletter/tooltipProcess.js b/entity/Person_entity/entityfields/campaignactiongroup/children/addtoserialletter/tooltipProcess.js
index a8ad9436e61..d00822c123e 100644
--- a/entity/Person_entity/entityfields/campaignactiongroup/children/addtoserialletter/tooltipProcess.js
+++ b/entity/Person_entity/entityfields/campaignactiongroup/children/addtoserialletter/tooltipProcess.js
@@ -1,4 +1,4 @@
 import("system.translate");
 import("system.result");
 
-result.string(translate.text("Adds the selection to a serial letter"));
\ No newline at end of file
+result.string(translate.text("Add the selection to a serial letter"));
\ No newline at end of file
diff --git a/entity/SerialLetter_entity/SerialLetter_entity.aod b/entity/SerialLetter_entity/SerialLetter_entity.aod
index e291842dda6..0e103f8692b 100644
--- a/entity/SerialLetter_entity/SerialLetter_entity.aod
+++ b/entity/SerialLetter_entity/SerialLetter_entity.aod
@@ -93,6 +93,10 @@
       <iconId>VAADIN:CURLY_BRACKETS</iconId>
       <stateProcess>%aditoprj%/entity/SerialLetter_entity/entityfields/openadminview/stateProcess.js</stateProcess>
     </entityActionField>
+    <entityField>
+      <name>BINDATA</name>
+      <contentType>FILE</contentType>
+    </entityField>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
diff --git a/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod b/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
index 035f3b18e6d..cd1d779266c 100644
--- a/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
+++ b/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
@@ -4854,6 +4854,18 @@
     <entry>
       <key>Add the selection to a serial letter</key>
     </entry>
+    <entry>
+      <key>Create activities</key>
+    </entry>
+    <entry>
+      <key>Testing contact</key>
+    </entry>
+    <entry>
+      <key>Recipient address</key>
+    </entry>
+    <entry>
+      <key>Testing</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 289f383904f..5e0010607c7 100644
--- a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
+++ b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
@@ -10,6 +10,10 @@
       <key>DSGVO Information</key>
       <value>DSGVO Informationen</value>
     </entry>
+    <entry>
+      <key>Recipient address</key>
+      <value>Empfängeradresse</value>
+    </entry>
     <entry>
       <key>Data Privacy</key>
       <value>Datenschutz</value>
@@ -18,6 +22,10 @@
       <key>Add the selection to a bulk mail</key>
       <value>Fügt die Auswahl einer Serienmail hinzu</value>
     </entry>
+    <entry>
+      <key>Testing</key>
+      <value>Testen</value>
+    </entry>
     <entry>
       <key>Number of activities</key>
       <value>Anzahl Aktivitäten</value>
@@ -4793,6 +4801,10 @@
       <key>${HIGHER_THAN_MAX} max: %0</key>
       <value>Der eingegebene Wert ist über dem Maximalwert %0.</value>
     </entry>
+    <entry>
+      <key>Test email</key>
+      <value>E-Mail testen</value>
+    </entry>
     <entry>
       <key>Rating</key>
       <value>Bewertung</value>
@@ -5411,10 +5423,18 @@
       <key>Treat</key>
       <value>Genuss</value>
     </entry>
+    <entry>
+      <key>Testing contact</key>
+      <value>Testkontakt</value>
+    </entry>
     <entry>
       <key>Motor cycle</key>
       <value>Motorrad</value>
     </entry>
+    <entry>
+      <key>Save settings</key>
+      <value>Einstellung speichern</value>
+    </entry>
     <entry>
       <key>Revolution speed (rpm)</key>
       <value>Drehzahl (rpm)</value>
diff --git a/language/_____LANGUAGE_en/_____LANGUAGE_en.aod b/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
index b8f78581254..bc01304f1d2 100644
--- a/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
+++ b/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
@@ -4903,6 +4903,18 @@
     <entry>
       <key>Add the selection to a serial letter</key>
     </entry>
+    <entry>
+      <key>Create activities</key>
+    </entry>
+    <entry>
+      <key>Testing contact</key>
+    </entry>
+    <entry>
+      <key>Recipient address</key>
+    </entry>
+    <entry>
+      <key>Testing</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 6327fbf8a15..6761dfbbd2d 100644
--- a/process/DocumentTemplate_lib/process.js
+++ b/process/DocumentTemplate_lib/process.js
@@ -26,6 +26,9 @@ import("Email_lib");
  * 
  * @class
  */
+var DocumentTemplate = (function ()
+{
+
 function DocumentTemplate (pTemplateContent, pType, pFilename)
 {
     this.content = pTemplateContent;
@@ -114,37 +117,6 @@ DocumentTemplate.loadTemplate = function (pAssignmentRowId, pAssignmentTable)
     return new DocumentTemplate(db.getBinaryContent(binaryId, alias), type, filename);
 }
 
-/**
- * Replace function that works with strings instead of regular expressions
- * so that control characters (for example '{', '}') don't have to be escaped.
- * 
- * @private
- */
-DocumentTemplate._replaceText = function (pText, pReplacements)
-{
-    for (let placeholder in pReplacements)
-        pText = pText.replace(placeholder, pReplacements[placeholder], "ig");
-    return pText;
-}
-
-/**
- * returns the 'simpleName' of all placeholders that are used in the template
- * 
- * @private
- */
-DocumentTemplate.prototype._getRequiredPlaceholders = function ()
-{
-    var allPlaceholders = PlaceholderUtils.getPlaceholders();
-    var plainText = this.toString();
-    var usedPlaceholders = [];
-    for (let i = 0, l = allPlaceholders.length; i < l; i++)
-    {
-        if (plainText.indexOf(allPlaceholders[i]) !== -1)
-            usedPlaceholders.push(allPlaceholders[i]);
-    }
-    return usedPlaceholders; 
-}
-
 /** 
  * Returns the template content with replaced placeholders by choosing the right
  * replace function for the type.
@@ -164,21 +136,21 @@ DocumentTemplate.prototype.getReplacedContent = function (pReplacements, pEncode
                 pReplacements[i] = text.text2html(pReplacements[i], false);
         case DocumentTemplate.types.TXT:
             let decodedContent = util.decodeBase64String(this.content);
-            let encodedContent = DocumentTemplate._replaceText(decodedContent, pReplacements);
+            let encodedContent = TemplateHelper._replaceText(decodedContent, pReplacements);
             if (pEncoded)
                 encodedContent = util.encodeBase64String(encodedContent);
             return encodedContent;
         case DocumentTemplate.types.EML:
-            let content = this._getReplacedEML(pReplacements);
+            let content = TemplateHelper._getReplacedEML(this, pReplacements);
             if (pEncoded)
                 content = util.encodeBase64String(content);
             return content;
         case DocumentTemplate.types.ODT:
-            return this._getReplacedODT(pReplacements);
+            return TemplateHelper._getReplacedODT(this, pReplacements);
         case DocumentTemplate.types.DOCX:
-            return this._getReplacedDOCX(pReplacements);
+            return TemplateHelper._getReplacedDOCX(this, pReplacements);
         case DocumentTemplate.types.PLAIN:
-            let plainText = DocumentTemplate._replaceText(this.content, pReplacements);
+            let plainText = TemplateHelper._replaceText(this.content, pReplacements);
             if (pEncoded)
                 plainText = util.encodeBase64String(plainText);
             return plainText;
@@ -192,7 +164,7 @@ DocumentTemplate.prototype.getReplacedContent = function (pReplacements, pEncode
  */
 DocumentTemplate.prototype.getReplacedContentByContactId = function (pContactId, pEncoded) 
 {
-    var replacements = this._getReplacementsByContactIds([pContactId]); 
+    var replacements = TemplateHelper._getReplacementsByContactIds(this, [pContactId]); 
     var content = this.getReplacedContent(replacements[pContactId], pEncoded);
     
     return content;
@@ -208,7 +180,7 @@ DocumentTemplate.prototype.getReplacedContentByContactId = function (pContactId,
  */
 DocumentTemplate.prototype.getReplacedContentByContactIds = function (pContactIds, pEncoded) 
 {
-    var replacements = this._getReplacementsByContactIds(pContactIds);
+    var replacements = TemplateHelper._getReplacementsByContactIds(this, pContactIds);
     var contents = {};
     for (let contactId in replacements)
     {
@@ -253,11 +225,11 @@ DocumentTemplate.prototype.getSerialLetterByContactIds = function (pContactIds,
 {
     if (this.type == DocumentTemplate.types.ODT)
     {
-        let replacements = this._getReplacementsByContactIds(pContactIds);
+        let replacements = TemplateHelper._getReplacementsByContactIds(this, pContactIds);
         let replaceArray = [];
         for (let i = 0, l = pContactIds.length; i < l; i++)
             replaceArray.push(replacements[pContactIds[i]]);
-        return this._getReplacedODT(replaceArray, pTableData);
+        return TemplateHelper._getReplacedODT(this, replaceArray, pTableData);
     }
     return null;
 }
@@ -272,14 +244,14 @@ DocumentTemplate.prototype.getSerialLetterByContactIds = function (pContactIds,
  */
 DocumentTemplate.prototype.getReplacedEmailsByContactIds = function (pContactIds) 
 {
-    var replacements = this._getReplacementsByContactIds(pContactIds);
+    var replacements = TemplateHelper._getReplacementsByContactIds(this, pContactIds);
     var emailObj = {};
     for (let contactId in replacements)
     {
         if (this.type == DocumentTemplate.types.EML)
         {
             //use the special function for EML to also fill subject and sender
-            emailObj[contactId] = this._getReplacedEML(replacements[contactId], true);
+            emailObj[contactId] = TemplateHelper._getReplacedEML(this, replacements[contactId], true);
         }
         else
         {
@@ -292,18 +264,52 @@ DocumentTemplate.prototype.getReplacedEmailsByContactIds = function (pContactIds
     return emailObj;
 }
 
+/**
+ * Provides functions for the DocumentTemplate object.
+ */
+function TemplateHelper () {}
+/**
+ * Replace function that works with strings instead of regular expressions
+ * so that control characters (for example '{', '}') don't have to be escaped.
+ */
+TemplateHelper._replaceText = function (pText, pReplacements)
+{
+    for (let placeholder in pReplacements)
+        pText = pText.replace(placeholder, pReplacements[placeholder], "ig");
+    return pText;
+}
+
+/**
+ * returns the 'simpleName' of all placeholders that are used in the template
+ * 
+ * @private
+ */
+TemplateHelper._getRequiredPlaceholders = function (pTemplate)
+{
+    var allPlaceholders = PlaceholderUtils.getPlaceholders();
+    var plainText = pTemplate.toString();
+    var usedPlaceholders = [];
+    for (let i = 0, l = allPlaceholders.length; i < l; i++)
+    {
+        if (plainText.indexOf(allPlaceholders[i]) !== -1)
+            usedPlaceholders.push(allPlaceholders[i]);
+    }
+    return usedPlaceholders; 
+}
+
 /**
  * Builds an object with the placeholder data for multiple contacts
  * 
+ * @param {DocumentTemplate} pTemplate document template
  * @param {Array} pContactIds contact ids
  * 
  * @return {Object} Object containing the data. The structure is like {contactId : {placeholderName : replacementValue, ...}, ...}
  * 
  * @private
  */
-DocumentTemplate.prototype._getReplacementsByContactIds = function (pContactIds)
+TemplateHelper._getReplacementsByContactIds = function (pTemplate, pContactIds)
 {
-    var config = this._getRequiredPlaceholders();
+    var config = TemplateHelper._getRequiredPlaceholders(pTemplate);
     var contactIdPlaceholder = new Placeholder("contactId", Placeholder.types.SQLPART, "CONTACT.CONTACTID");
     config = [contactIdPlaceholder].concat(config);
     var addressData = getAddressesData(pContactIds, config, EmployeeUtils.getCurrentContactId()); //TODO: add sender selection
@@ -326,6 +332,7 @@ DocumentTemplate.prototype._getReplacementsByContactIds = function (pContactIds)
 /**
  * Replaces placeholders for EML
  * 
+ * @param {DocumentTemplate} pTemplate document template
  * @param {Object} pReplacements mapping with replacements for every placeholder
  * @param {boolean} [pGetEmail] if true, return an Email object (use this if the sender and subject are required)
  * 
@@ -333,15 +340,15 @@ DocumentTemplate.prototype._getReplacementsByContactIds = function (pContactIds)
  * 
  * @private
  */
-DocumentTemplate.prototype._getReplacedEML = function (pReplacements, pGetEmail)
+TemplateHelper._getReplacedEML = function (pTemplate, pReplacements, pGetEmail)
 {
-    var mailData = mail.parseRFC(util.decodeBase64String(this.content));
+    var mailData = mail.parseRFC(util.decodeBase64String(pTemplate.content));
     var email;
-    var body = DocumentTemplate._replaceText(mailData[mail.MAIL_HTMLTEXT], pReplacements);
+    var body = TemplateHelper._replaceText(mailData[mail.MAIL_HTMLTEXT], pReplacements);
     if (pGetEmail)
     {
-        var sender = DocumentTemplate._replaceText(mailData[mail.MAIL_SENDER], pReplacements);
-        var subject = DocumentTemplate._replaceText(mailData[mail.MAIL_SUBJECT], pReplacements);
+        var sender = TemplateHelper._replaceText(mailData[mail.MAIL_SENDER], pReplacements);
+        var subject = TemplateHelper._replaceText(mailData[mail.MAIL_SUBJECT], pReplacements);
         email = new Email(null, sender, subject, body);
     }
     else
@@ -352,6 +359,7 @@ DocumentTemplate.prototype._getReplacedEML = function (pReplacements, pGetEmail)
 /*
  * replaces a given Odt-File on the server and returns the replaced base64-file
  *
+ * @param {DocumentTemplate} pTemplate document template
  * @param {Object} pReplacements map of placeholders and replacements
  * @param {Array} pTableData
  *
@@ -359,13 +367,13 @@ DocumentTemplate.prototype._getReplacedEML = function (pReplacements, pGetEmail)
  * 
  * @private
  */
-DocumentTemplate.prototype._getReplacedODT = function (pReplacements, pTableData)
+TemplateHelper._getReplacedODT = function (pTemplate, pReplacements, pTableData)
 {
     //save the file on the server so it can be unzipped via pack.getFromZip
     var serverFilePath = vars.get("$sys.servertemp") + "/clientid_" + vars.get("$sys.clientid")
-        + "/" + util.getNewUUID() + "/" + this.filename.replace(/\\/g, "/");
+        + "/" + util.getNewUUID() + "/" + pTemplate.filename.replace(/\\/g, "/");
     
-    fileIO.storeData(serverFilePath, this.content, util.DATA_BINARY, false);
+    fileIO.storeData(serverFilePath, pTemplate.content, util.DATA_BINARY, false);
     if (!_replaceODTFile(pReplacements, serverFilePath, pTableData))
         return null;
 
@@ -442,7 +450,7 @@ DocumentTemplate.prototype._getReplacedODT = function (pReplacements, pTableData
                         for (let rowIndex = 0; rowIndex < tableData.length; rowIndex++)
                         {
                             let tableRowData = tableData[rowIndex];
-                            currentBody += DocumentTemplate._replaceText(tableRow, tableRowData);
+                            currentBody += TemplateHelper._replaceText(tableRow, tableRowData);
                         }
                         currentBody += afterTable;
                     }
@@ -469,13 +477,14 @@ DocumentTemplate.prototype._getReplacedODT = function (pReplacements, pTableData
 /*
  * This function is used to replace placeholders via DocXTemplater
  * 
+ * @param {DocumentTemplate} pTemplate document template
  * @param {Object} pReplacements - Must contain an object, which holds the placeholders
  * 
  * @return {String} returns the modified document in a BASE64 coded string
  * 
  * @private
  */
-DocumentTemplate.prototype._getReplacedDOCX = function (pReplacements)
+TemplateHelper._getReplacedDOCX = function (pTemplate, pReplacements)
 {
     var replacements = {};
     for (let placeholder in pReplacements)  //removes the prefix and postfix, the process needs it like this
@@ -483,13 +492,17 @@ DocumentTemplate.prototype._getReplacedDOCX = function (pReplacements)
 
     //this is executed as a process because of better performance
     var documentData = process.execute("getDocxDocument_serverProcess", {
-        templateb64: this.content,
+        templateb64: pTemplate.content,
         placeholderConfig: JSON.stringify(replacements) //process.execute is only able to handle strings
     });
 
     return documentData;
 }
 
+    return DocumentTemplate;
+
+})();
+
 /**
  * functions for working with letters (mails)
  */
diff --git a/process/JditoFilter_lib/process.js b/process/JditoFilter_lib/process.js
index f60e36bcc81..077e6c10ae5 100644
--- a/process/JditoFilter_lib/process.js
+++ b/process/JditoFilter_lib/process.js
@@ -1,10 +1,16 @@
 import("Sql_lib");
 
+//private scope to make only JditoFilterUtils public
+var JditoFilterUtils = (function ()
+{
+
 /**
  * object for filtering records
  * 
  * @param {Array} pColumns the column names
  * @param {Object} pFilter the filter object
+ * 
+ * @private
  */
 function JditoFilterHelper (pColumns, pFilter) 
 {
@@ -230,4 +236,7 @@ JditoFilterUtils.getSqlCondition = function (pFilter, pTable, pTableAlias, pColu
                 return "# is not null";
         }
     }
-}
\ No newline at end of file
+}
+    return JditoFilterUtils; //return only functions that should be public
+    
+})();
\ No newline at end of file
-- 
GitLab