From 8b89a8c95046f52a89eb29b8f679bcc0060d4d5b Mon Sep 17 00:00:00 2001
From: "S.Listl" <S.Listl@SLISTL.aditosoftware.local>
Date: Fri, 7 Aug 2020 11:36:14 +0200
Subject: [PATCH] #1063319 Address fields for lat and lon, location added to
 Organisation index

---
 .../basic/2020.1.3/add_Address_LatLon.xml     | 10 +++
 .../Data_alias/basic/2020.1.3/changelog.xml   |  1 +
 .../Data_alias/basic/_demoData/changelog.xml  |  2 +-
 .liquibase/Data_alias/changelog.xml           |  1 +
 aliasDefinition/Data_alias/Data_alias.aod     | 28 ++++++++
 entity/Address_entity/Address_entity.aod      | 18 +++++
 .../Organisation_entity.aod                   |  6 ++
 .../recordcontainers/index/query.js           | 66 +++++++++----------
 8 files changed, 98 insertions(+), 34 deletions(-)
 create mode 100644 .liquibase/Data_alias/basic/2020.1.3/add_Address_LatLon.xml

diff --git a/.liquibase/Data_alias/basic/2020.1.3/add_Address_LatLon.xml b/.liquibase/Data_alias/basic/2020.1.3/add_Address_LatLon.xml
new file mode 100644
index 0000000000..cb352481af
--- /dev/null
+++ b/.liquibase/Data_alias/basic/2020.1.3/add_Address_LatLon.xml
@@ -0,0 +1,10 @@
+<?xml version="1.1" encoding="UTF-8" standalone="no"?>
+<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
+                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
+    <changeSet author="s.listl" id="2f2d806c-8aab-42f0-91c2-c7d15322d89f">
+        <addColumn tableName="ADDRESS">
+            <column name="LAT" type="DECIMAL(10,8)"/>
+            <column name="LON" type="DECIMAL(11,8)"/>
+        </addColumn>
+    </changeSet>
+</databaseChangeLog>
diff --git a/.liquibase/Data_alias/basic/2020.1.3/changelog.xml b/.liquibase/Data_alias/basic/2020.1.3/changelog.xml
index ac6898c7f1..bd8e2232fe 100644
--- a/.liquibase/Data_alias/basic/2020.1.3/changelog.xml
+++ b/.liquibase/Data_alias/basic/2020.1.3/changelog.xml
@@ -2,6 +2,7 @@
 <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
     <include file="AlterButtonLabelTitles/AlterButtonLabelTitles.xml" relativeToChangelogFile="true" />
+    <include file="add_Address_LatLon.xml" relativeToChangelogFile="true"/>
     <!--    disabled the following script since it is WIP-->
     <!--<include file="VisitPlanEntry/rename_VisitPlanEntry_ORGANISATION_ID.xml" relativeToChangelogFile="true" />-->
 </databaseChangeLog>
diff --git a/.liquibase/Data_alias/basic/_demoData/changelog.xml b/.liquibase/Data_alias/basic/_demoData/changelog.xml
index e6850dd5b6..e0d3d57558 100644
--- a/.liquibase/Data_alias/basic/_demoData/changelog.xml
+++ b/.liquibase/Data_alias/basic/_demoData/changelog.xml
@@ -50,5 +50,5 @@
   <include file="generatedData/exporttemplatefield.xml" relativeToChangelogFile="true"/>
   <include file="generatedData/exporttemplateplaceofuse.xml" relativeToChangelogFile="true"/>
   <include file="generatedData/documenttemplateplaceofuse.xml" relativeToChangelogFile="true"/>
-  <include file="generatedData/visitplan.xml" relativeToChangelogFile="true"/>
+  <!--<include file="generatedData/visitplan.xml" relativeToChangelogFile="true"/>-->
 </databaseChangeLog>
\ No newline at end of file
diff --git a/.liquibase/Data_alias/changelog.xml b/.liquibase/Data_alias/changelog.xml
index 415dcce416..b3fee691bd 100644
--- a/.liquibase/Data_alias/changelog.xml
+++ b/.liquibase/Data_alias/changelog.xml
@@ -14,6 +14,7 @@
     <include relativeToChangelogFile="true" file="basic/2020.1.0/changelog.xml"/>
     <include relativeToChangelogFile="true" file="basic/2020.1.1/changelog.xml"/>
     <include relativeToChangelogFile="true" file="basic/2020.1.2/changelog.xml"/>
+    <include relativeToChangelogFile="true" file="basic/2020.1.3/changelog.xml"/>
 
     <!--enable this only when you definetly want to overwrite the existing data with demo records:--> 
     <!--<include relativeToChangelogFile="true" file="basic/_demoData/changelog.xml" context="example"/>-->
diff --git a/aliasDefinition/Data_alias/Data_alias.aod b/aliasDefinition/Data_alias/Data_alias.aod
index 6e685b98b7..b42801cfda 100644
--- a/aliasDefinition/Data_alias/Data_alias.aod
+++ b/aliasDefinition/Data_alias/Data_alias.aod
@@ -1143,6 +1143,34 @@
                 <title>Creation user</title>
                 <description></description>
               </entityFieldDb>
+              <entityFieldDb>
+                <name>LON</name>
+                <dbName></dbName>
+                <primaryKey v="false" />
+                <columnType v="3" />
+                <size v="11" />
+                <scale v="8" />
+                <notNull v="false" />
+                <isUnique v="false" />
+                <index v="false" />
+                <documentation></documentation>
+                <title></title>
+                <description></description>
+              </entityFieldDb>
+              <entityFieldDb>
+                <name>LAT</name>
+                <dbName></dbName>
+                <primaryKey v="false" />
+                <columnType v="3" />
+                <size v="10" />
+                <scale v="8" />
+                <notNull v="false" />
+                <isUnique v="false" />
+                <index v="false" />
+                <documentation></documentation>
+                <title></title>
+                <description></description>
+              </entityFieldDb>
             </entityFields>
           </entityDb>
           <entityDb>
diff --git a/entity/Address_entity/Address_entity.aod b/entity/Address_entity/Address_entity.aod
index 0b6f95c995..e0515671c3 100644
--- a/entity/Address_entity/Address_entity.aod
+++ b/entity/Address_entity/Address_entity.aod
@@ -469,6 +469,16 @@
       <name>ReplaceStandardAddress_param</name>
       <expose v="true" />
     </entityParameter>
+    <entityField>
+      <name>LAT</name>
+      <title>Latitude</title>
+      <contentType>NUMBER</contentType>
+    </entityField>
+    <entityField>
+      <name>LON</name>
+      <title>Longitude</title>
+      <contentType>NUMBER</contentType>
+    </entityField>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
@@ -577,6 +587,14 @@
           <name>COUNTRY.displayValue</name>
           <expression>%aditoprj%/entity/Address_entity/recordcontainers/db/recordfieldmappings/country.displayvalue/expression.js</expression>
         </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>LAT.value</name>
+          <recordfield>ADDRESS.LAT</recordfield>
+        </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>LON.value</name>
+          <recordfield>ADDRESS.LON</recordfield>
+        </dbRecordFieldMapping>
       </recordFieldMappings>
     </dbRecordContainer>
   </recordContainers>
diff --git a/entity/Organisation_entity/Organisation_entity.aod b/entity/Organisation_entity/Organisation_entity.aod
index cbd7cb44ef..2586777683 100644
--- a/entity/Organisation_entity/Organisation_entity.aod
+++ b/entity/Organisation_entity/Organisation_entity.aod
@@ -1353,6 +1353,12 @@
       </affectedTables>
       <affectedIds>%aditoprj%/entity/Organisation_entity/recordcontainers/index/affectedIds.js</affectedIds>
       <indexFieldMappings>
+        <indexRecordFieldMapping>
+          <name>#LOCATION</name>
+          <systemField>LOCATION</systemField>
+          <isFilterable v="false" />
+          <isLookupFilter v="false" />
+        </indexRecordFieldMapping>
         <indexRecordFieldMapping>
           <name>NAME.value</name>
           <indexFieldType>PROPER_NAME</indexFieldType>
diff --git a/entity/Organisation_entity/recordcontainers/index/query.js b/entity/Organisation_entity/recordcontainers/index/query.js
index eeb55badd1..278616e222 100644
--- a/entity/Organisation_entity/recordcontainers/index/query.js
+++ b/entity/Organisation_entity/recordcontainers/index/query.js
@@ -5,6 +5,7 @@ import("system.calendars");
 import("system.db");
 import("Sql_lib");
 import("Communication_lib");
+import("system.SQLTYPES");
 
 /*
 Before changing the results that are returned here:
@@ -12,40 +13,39 @@ Please keep in mind that some of the indexfields are used in other modules (like
 You may want to check out if your change affects other modules. However adding more fields should not be a problem therefor.
  */
 
-var sqlQuery, sqlHelper, queryCondition, affectedIds;
-var CommMediumPhoneIds = db.array( db.COLUMN, "select KEYID from AB_KEYWORD_ENTRY join ab_keyword_attributerelation on AB_KEYWORD_ENTRYID = AB_KEYWORD_ENTRY_ID "
-        + " join ab_keyword_attribute on AB_KEYWORD_ATTRIBUTEID = AB_KEYWORD_ATTRIBUTE_ID and CHAR_VALUE = 'TELEPHONE' and AB_KEYWORD_ATTRIBUTE.CONTAINER = 'CommunicationMedium'");
-sqlHelper = new SqlMaskingUtils();
-var querySelect = newSelect("CONTACT.CONTACTID "
-                        + ", " + sqlHelper.concat(["ORGANISATION.NAME", "'|'", "ORGANISATION.CUSTOMERCODE"])
-                        + ", " + sqlHelper.concat([
-                             sqlHelper.concat(["defaultAddress.ADDRESS", "defaultAddress.BUILDINGNO", "'-'"
-                                ,"defaultAddress.COUNTRY", "defaultAddress.ZIP", "defaultAddress.CITY"])
-                            ,sqlHelper.concat(["'" + translate.text("Phone") + ":'", "(" + CommUtil.getStandardSubSqlPhone() + ")"])
-                            ,sqlHelper.concat(["'" + translate.text("Email") + ":'", "(" + CommUtil.getStandardSubSqlMail() + ")"])
-                            ], " | ") 
-                           //additional indexed fields
-                           + ", ORGANISATION.NAME"
-                           + ", ORGANISATION.ORGANISATIONID"
-                           + ", CONTACT.CONTACTID"
-                           + ", ORGANISATION.CUSTOMERCODE"
-                           + ", ADDRESS.ADDRESS " 
-                           + ", ADDRESS.COUNTRY " 
-                           + ", ADDRESS.ZIP " 
-                           + ", ADDRESS.CITY " 
-                           + ", COMMUNICATION.ADDR " 
-                           + ", PHONE.ADDR " 
-                       )
-                    .from("ORGANISATION")
-                    .join("CONTACT", "CONTACT.ORGANISATION_ID = ORGANISATION.ORGANISATIONID and CONTACT.PERSON_ID is null")
-                    .leftJoin("ADDRESS", "defaultAddress.ADDRESSID = CONTACT.ADDRESS_ID", "defaultAddress")
-                    .leftJoin("ADDRESS", "ADDRESS.CONTACT_ID = CONTACT.CONTACTID")
-                    .leftJoin("COMMUNICATION", "COMMUNICATION.CONTACT_ID = CONTACT.CONTACTID")
-                    .leftJoin("COMMUNICATION", "PHONE.CONTACT_ID = CONTACT.CONTACTID and PHONE.MEDIUM_ID in ('" + CommMediumPhoneIds.join("', '") + "')", "PHONE")//TODO: refactor to new SqlBuilder
+var commMediumPhoneIds = CommUtil.getMediumIdsByCategory("PHONE");
+var sqlHelper = new SqlMaskingUtils();
+var querySelect = newSelect([
+        "CONTACT.CONTACTID",
+        sqlHelper.concat(["ORGANISATION.NAME", "'|'", "ORGANISATION.CUSTOMERCODE"]),
+        sqlHelper.concat([
+            sqlHelper.concat(["defaultAddress.ADDRESS", "defaultAddress.BUILDINGNO", "'-'"
+                ,"defaultAddress.COUNTRY", "defaultAddress.ZIP", "defaultAddress.CITY"]),
+            sqlHelper.concat(["'" + translate.text("Phone") + ":'", "(" + CommUtil.getStandardSubSqlPhone() + ")"]),
+            sqlHelper.concat(["'" + translate.text("Email") + ":'", "(" + CommUtil.getStandardSubSqlMail() + ")"])
+        ], " | "),
+        sqlHelper.concat([sqlHelper.cast("ADDRESS.LAT", SQLTYPES.VARCHAR, 16), sqlHelper.cast("ADDRESS.LON", SQLTYPES.VARCHAR, 16)], ","),
+        //additional indexed fields
+        "ORGANISATION.NAME",
+        "ORGANISATION.ORGANISATIONID",
+        "CONTACT.CONTACTID",
+        "ORGANISATION.CUSTOMERCODE",
+        "ADDRESS.ADDRESS",
+        "ADDRESS.COUNTRY",
+        "ADDRESS.ZIP",
+        "ADDRESS.CITY",
+        "COMMUNICATION.ADDR",
+        "PHONE.ADDR" 
+    ])
+    .from("ORGANISATION")
+    .join("CONTACT", "CONTACT.ORGANISATION_ID = ORGANISATION.ORGANISATIONID and CONTACT.PERSON_ID is null")
+    .leftJoin("ADDRESS", "defaultAddress.ADDRESSID = CONTACT.ADDRESS_ID", "defaultAddress")
+    .leftJoin("ADDRESS", "ADDRESS.CONTACT_ID = CONTACT.CONTACTID")
+    .leftJoin("COMMUNICATION", "COMMUNICATION.CONTACT_ID = CONTACT.CONTACTID")
+    .leftJoin("COMMUNICATION", newWhere("phone.CONTACT_ID = CONTACT.CONTACTID")
+        .and(["COMMUNICATION", "MEDIUM_ID", "phone"], commMediumPhoneIds, SqlBuilder.IN()), "phone");
 
 if (vars.exists("$local.idvalue") && vars.get("$local.idvalue") && vars.get("$local.idvalue").length > 0) 
-{
-    querySelect.where("CONTACT.CONTACTID", vars.get("$local.idvalue"), SqlBuilder.IN());
-}
+    querySelect.where("CONTACT.CONTACTID", "$local.idvalue", SqlBuilder.IN());
 
 result.string(querySelect.toString());
-- 
GitLab