Skip to content
Snippets Groups Projects
Commit f4721f34 authored by Johannes Hörmann's avatar Johannes Hörmann
Browse files

refactor lib and comments

parent b8847b46
No related branches found
No related tags found
No related merge requests found
import("system.logging");
import("system.vars"); import("system.vars");
import("system.result"); import("system.result");
import("system.db"); import("system.db");
import("Sql_lib"); import("Sql_lib");
import("Context_lib");
/** /**
* a static Utility class for relations * a static Utility class for relations
...@@ -23,10 +25,7 @@ function RelationUtils() {} ...@@ -23,10 +25,7 @@ function RelationUtils() {}
*/ */
RelationUtils.getRelationTypeByRelation = function(pRelationId) RelationUtils.getRelationTypeByRelation = function(pRelationId)
{ {
var relationData = db.array(db.ROW, var relationData = RelationUtils.getContextByRelationId(pRelationId);
SqlCondition.begin()
.andPrepare("RELATION.RELATIONID", pRelationId)
.buildSelect("select RELATIONID, PERS_ID, ORG_ID from RELATION", "1=0"));
if (relationData[0]) if (relationData[0])
{ {
...@@ -55,7 +54,8 @@ RelationUtils.getRelationTypeByRelation = function(pRelationId) ...@@ -55,7 +54,8 @@ RelationUtils.getRelationTypeByRelation = function(pRelationId)
* @param {String} pPersId selected from the RELATION table * @param {String} pPersId selected from the RELATION table
* @param {String} pOrgId selected from the RELATION table * @param {String} pOrgId selected from the RELATION table
* <br> * <br>
* @return {Integer} <br>1 if organisation <br> * @return {Integer} <br>0 if both ids are empty <br>
* 1 if organisation <br>
* 2 if privat person <br> * 2 if privat person <br>
* 3 if person of an organisation <br> * 3 if person of an organisation <br>
*/ */
...@@ -63,6 +63,9 @@ RelationUtils.getRelationTypeByPersOrg = function(pPersId, pOrgId) ...@@ -63,6 +63,9 @@ RelationUtils.getRelationTypeByPersOrg = function(pPersId, pOrgId)
{ {
if (!pPersId) if (!pPersId)
{ {
if (!pOrgId) {
return 0; // both are empty
}
return 1; // Organisation da PERS_ID leer return 1; // Organisation da PERS_ID leer
} }
else else
...@@ -78,88 +81,131 @@ RelationUtils.getRelationTypeByPersOrg = function(pPersId, pOrgId) ...@@ -78,88 +81,131 @@ RelationUtils.getRelationTypeByPersOrg = function(pPersId, pOrgId)
} }
} }
RelationUtils.getContextByPersOrg = function(pPersIdField, pOrgIdField) /**
* return the corresponding context of the relation
*
* It only checks if the parameters are not empty. <br>
* Based on which parameter is empty / not empty it return s the type of the relation. <br>
* <br>
* !!It does not check if the pers / org ids really exist!! <br>
* !!And it does not check if really any relation with this pers / org ids exist!! <br>
* <br>
* This function is more performant than getContextByRelationId, <br>
* because it doesn't load something from the db. <br>
* <br>
* It is meant to be used by entitys, where you can load pers and org with the DataRecord. <br>
* This saves an extra select from RELATION. <br>
* <br>
*
* @param {String} pPersId selected from the RELATION table
* @param {String} pOrgId selected from the RELATION table
*
* @return {String} contextname or "" if both ids are empty
*/
RelationUtils.getContextByPersOrg = function(pPersId, pOrgId)
{ {
if(!vars.get(pPersIdField) && !vars.get(pOrgIdField)) switch (RelationUtils.getRelationTypeByPersOrg(pPersId, pOrgId))
return "";
else
{ {
switch (RelationUtils.getRelationTypeByPersOrg(vars.get(pPersIdField), vars.get(pOrgIdField))) case 1: // Org
{ return ContextUtils.getContextName("Org_context");
case 1: // Org case 2: // private Pers
return "Org_context"; case 3: // Pers
case 2: // private Pers return ContextUtils.getContextName("Pers_context");
case 3: // Pers default:
return "Pers_context"; return "";
default:
return "";
}
} }
} }
/**
* return the corresponding context of the relation <br>
* If you already have persId and orgId from the RELATION table, use getContextByPersOrg() <br>
*
* @param {String} pRelationId
* @return {String} contextname or "" if relation not found
*/
RelationUtils.getContextByRelationId = function(pRelationId) RelationUtils.getContextByRelationId = function(pRelationId)
{ {
if(!pRelationId) var relationData = RelationUtils.getPersOrgIds(pRelationId);
return ""; return RelationUtils.getContextByPersOrg(relationData[0], relationData[1])
else }
{
switch (RelationUtils.getRelationTypeByRelation(pRelationId)) /**
{ * get the pers- and org-id from a relation as array
case 1: // Org *
return "Org_context"; * @param {String} pRelationId
case 2: // private Pers * @return {String[]} result as [persid, orgid] if one of them is null in the db, "" will be returned as the id.
case 3: // Pers */
return "Pers_context"; RelationUtils.getPersOrgIds = function(pRelationId)
default: {
return ""; if (pRelationId) {
} return db.array(db.ROW,
SqlCondition.begin()
.andPrepare("RELATION.RELATIONID", pRelationId)
.buildSelect("select RELATIONID, PERS_ID, ORG_ID from RELATION", "1=0"));
} }
return ["", ""];
} }
RelationUtils.getNameByPersOrg = function(pPersIdField, pOrgIdField, pPersFirstnameField, /**
pPersLastnameField, pOrgnameField) * get the name of the person or organisation
* The parameters have to be selected from the same relation.
*
* @param {String} pPersId value of the pers_id selected from RELATION
* @param {String} pOrgId value of the org_id selected from RELATION
* @param {String} pPersFirstname value of the firstname selected from PERS
* @param {String} pPersLastname value of the lastname selected from PERS
* @param {String} pOrgname value of the name selected from ORG
* @param {String} [pTitle=undefined] value of the title selected from PERS. You can ommit this parameter if you do not want to append the title
*
* @return {String} the name or ""
*/
RelationUtils.getNameByPersOrg = function(pPersId, pOrgId, pPersFirstname,
pPersLastname, pOrgname, pTitle)
{ {
if(!vars.get(pPersIdField) && !vars.get(pOrgIdField)) if(!pPersId && !pOrgId)
return ""; return "";
else else
{ {
switch (RelationUtils.getRelationTypeByPersOrg(vars.get(pPersIdField), vars.get(pOrgIdField))) switch (RelationUtils.getRelationTypeByPersOrg(pPersId, pOrgId))
{ {
case 1: // Org case 1: // Org
return vars.get(pOrgnameField); return pOrgname;
case 2: // private Pers case 2: // private Pers
case 3: // Pers case 3: // Pers
return vars.getString(pPersFirstnameField) var name = "";
.concat(" " , vars.getString(pPersLastnameField));
if (pTitle != undefined && pTitle)
name = pTitle + " ";
return name + pPersFirstname.concat(" " , pPersLastname);
default: default:
return ""; return "";
} }
} }
} }
RelationUtils.getNameByPersOrgWithRelationId = function(pRelationId) /**
* get the name of the person or organisation
*
* @param {String} pRelationId the relation id
* @param {Boolean} [pTitle=false] also add the title for persons
*
* @return {String} the name or ""
*/
RelationUtils.getNameByPersOrgWithRelationId = function(pRelationId, pTitle)
{ {
logging.log(vars.get("$sys.currententityname") + pRelationId + " ")
var data = db.array(db.ROW, SqlCondition.begin() if (pRelationId) {
var data = db.array(db.ROW, SqlCondition.begin()
.andPrepare("RELATION.RELATIONID", pRelationId) .andPrepare("RELATION.RELATIONID", pRelationId)
.buildSelect("select RELATION.PERS_ID, RELATION.ORG_ID, PERS.FIRSTNAME, PERS.LASTNAME, ORG.NAME from RELATION RELATION join ORG on ORG.ORGID = RELATION.ORG_ID left join PERS on PERS.PERSID = RELATION.PERS_ID", "1 = 2")); .buildSelect("select RELATION.PERS_ID, RELATION.ORG_ID, PERS.FIRSTNAME, PERS.LASTNAME, PERS.TITLE, ORG.NAME from RELATION RELATION join ORG on ORG.ORGID = RELATION.ORG_ID left join PERS on PERS.PERSID = RELATION.PERS_ID", "1 = 2"));
if(!data[0] && !data[1]) return RelationUtils.getNameByPersOrg(data[0], data[1], data[2], data[3], data[5], (pTitle ? data[4] : undefined));
return "";
else
{
switch (RelationUtils.getRelationTypeByPersOrg(data[0], data[1]))
{
case 1: // Org
return data[4];
case 2: // private Pers
case 3: // Pers
return data[2]
.concat(" " , data[3]);
default:
return "";
}
} }
return "";
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment