Newer
Older
import("KeywordRegistry_basic");
import("Keyword_lib");
Johannes Goderbauer
committed
import("system.translate");
import("system.neon");
import("system.vars");
import("system.result");
import("system.db");
import("Sql_lib");
import("Util_lib");
import("Context_lib");
/**
* a static Utility class for organisations
*
* Do not create an instance of this!
* @class
*/
function OrganisationUtils() {}//TODO: there exsits a OrgUtils and OrganisationUtils, this is inconvenient
Johannes Goderbauer
committed
/*
* retrieves the name of an organisation with a select statement
*
* @param {String} pOrganisationId the ID of the organisation that shall be searched in the database;
*
* @return {String} the name of the organisation
*/
OrganisationUtils.getNameByOrganisationId = function(pOrganisationId)
{
var orgname = newSelect("ORGANISATION.NAME")
.from("ORGANISATION")
.whereIfSet("ORGANISATION.ORGANISATIONID", pOrganisationId)
.cell(true);
Johannes Goderbauer
committed
return orgname;
};
/*
* retrieves the name of an organisation with a select statement
*
* @param {String} pContactId the ID of the corresponding org-contact of the organisation that shall be searched in the database;
*
* @return {String} the name of the organisation
*/
OrganisationUtils.getNameByContactId = function(pContactId)
{
var orgname = newSelect("ORGANISATION.NAME")
.from("ORGANISATION")
.join("CONTACT", "CONTACT.ORGANISATION_ID = ORGANISATION.ORGANISATIONID and CONTACT.PERSON_ID is null")
.whereIfSet("CONTACT.CONTACTID", pContactId)
.cell(true);
Johannes Goderbauer
committed
return orgname;
};
/**
* a static Utility class for contacts
*
* Do not create an instance of this!
* @class
*/
function ContactUtils() {}
/**
* creates an subSql for resolving a person/organisation-contact into one string of text (for example the name of a person)
* useful for example in an displayValue-expression to resolave a references (which is chosen by Lookups)
* does not validate if pRelationIdField exists or is a valid or harmful value
*
* @param {String} pContactIdField fieldname for the CONTACTID-condition as TABLEALIAS.COLUMNALIAS; e.g. TASK.EDITOR_CONTACT_ID
Johannes Goderbauer
committed
* @param {Boolean} [pIncludeOrganisation=true] false if the organisation shall not be included in the result string
* @return {String} a subsql (without bracets) that can be played within an SQL
*/
Johannes Goderbauer
committed
ContactUtils.getResolvingDisplaySubSql = function(pContactIdField, pIncludeOrganisation)
{
var contact = Contact.createWithColumnPreset();
Johannes Goderbauer
committed
var rendererOptions;
if (pIncludeOrganisation === false)
rendererOptions = ContactTitleRenderer.OPTIONS.NoOption;
else
rendererOptions = ContactTitleRenderer.OPTIONS.IncludeOrganisation;
var renderer = new ContactTitleRenderer(contact, rendererOptions);
var selectExpression = renderer.asSql();
//TODO: verify if there is a better solution for the usage of this as a displayValueExpression --> automatic use of #TITLE | waiting vor implementation
return "select " + selectExpression + "from CONTACT "
+ " left join PERSON on (PERSON.PERSONID = CONTACT.PERSON_ID) "
+ " left join ORGANISATION on (ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID) "
+ " where CONTACT.CONTACTID = " + pContactIdField;
Johannes Goderbauer
committed
/*
* validates if a ORGANISATION_ID in a person-contact is correct [=>does not already exist] or not [=>does already exist]
* this is done by checking the database for entires that do already exist with this combination of ORGANISATIONID and PERSONID
* gives different messages for private persons and contacts that do already exist
*
* @param {String} pPersonId the ID of the person that shall be searched in the database
* @param {String} pOrganisationId the ID of the organisation that shall be searched in the database;
* if this is an empty string it will be treated as private-dummy-organisation
* @param {String} [pOwnContactId] the CONTACTID of your current record; this is only needed when in EDIT-mode since you don't want to get a message
* for your own CONTACT;
* (if you do a lookup if a organisation-person-combination does already exist you'l get your own contact which you want to exclude)
*
* @return {String} translated text that describes whats the problem or null if there was no problem and everything is fine
*
*/
ContactUtils.validateIfAlreadyExists = function(pPersonId, pOrganisationId, pOwnContactId)
{
if (!pPersonId)
return null;
if (pOrganisationId == "")
pOrganisationId = "0";
var alreadyExistantContactId = newSelect("CONTACT.CONTACTID")
.from("CONTACT")
.where("CONTACT.PERSON_ID", pPersonId)
.and("CONTACT.ORGANISATION_ID", pOrganisationId)
//exclude the own since we do not want a "is not valid"-message for our own entry (on EDIT-mode)
.andIfSet("CONTACT.CONTACTID", pOwnContactId, SqlBuilder.NOT_EQUAL())
Johannes Goderbauer
committed
if (alreadyExistantContactId)
if (pOrganisationId.trim() == "0")
return translate.text("This private person doeas already exist and can not be created once more.");
else
return translate.text("This combination of person and organisation does already exist and can not be created once more.");
return null;
};
/**
* Get the type of contact. <br>
* In recordstate NEW or EDIT it loads the person- / orgid from the db.<br>

Benjamin Ulrich
committed
* But in the other states it uses the values pPersonId, pOrganisationId directly (for performance).<br>
Johannes Goderbauer
committed
* <br>

Benjamin Ulrich
committed
* It only checks if pPersonId / pOrganisationId are not empty. <br>
Johannes Goderbauer
committed
* Based on which parameter is empty / not empty it return s the type of the contact. <br>
* <br>
* !!It does not check if the person / org ids really exist!! <br>
* !!And it does not check if really any contact with this person / org ids exist!! <br>
* <br>
* <br>
* It is meant to be used by entitys, where you can load person and org with the DataRecord. <br>
* This saves an extra select from CONTACT. <br>
* <br>
* <br>
* @param {String} pContactId

Benjamin Ulrich
committed
* @param {String} pPersonId selected from the CONTACT table
* @param {String} pOrganisationId selected from the CONTACT table
Johannes Goderbauer
committed
* <br>
* @return {Integer} <br>0 if both ids are empty <br>
* 1 if organisation <br>
* 2 if privat person <br>
* 3 if person of an organisation <br>
*/

Benjamin Ulrich
committed
ContactUtils.getContactType = function(pContactId, pPersonId, pOrganisationId)
Johannes Goderbauer
committed
{
if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW || vars.get("$sys.recordstate") == neon.OPERATINGSTATE_EDIT)
{
return ContactUtils.getContactTypeByContactId(pContactId);
}
else
{

Benjamin Ulrich
committed
return ContactUtils.getContactTypeByPersOrg(pPersonId, pOrganisationId);
Johannes Goderbauer
committed
}
}

Benjamin Ulrich
committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Check if the contact is of the contactType "1": organisation <br>
* This function uses "ContactUtils.getContactType" to check the type.<br>
* So for more information about when to use whcih parameters for better perfrmance see it's documentation.<br>
* <br>
* <br>
* @param {String} pContactId
* @param {String} pPersonId selected from the CONTACT table
* @param {String} pOrganisationId selected from the CONTACT table
* <br>
* @return {Boolean} <br>true if the contact is a organisationType<br>
* else: false <br>
*/
ContactUtils.isOrganisation = function(pContactId, pPersonId, pOrganisationId)
{
var contactType = ContactUtils.getContactType(pContactId, pPersonId, pOrganisationId);
return contactType == 1;
}
/**
* Check if the contact is of the contactType "2": private person <br>
* This function uses "ContactUtils.getContactType" to check the type.<br>
* So for more information about when to use whcih parameters for better perfrmance see it's documentation.<br>
* <br>
* <br>
* @param {String} pContactId
* @param {String} pPersonId selected from the CONTACT table
* @param {String} pOrganisationId selected from the CONTACT table
* <br>
* @return {Boolean} <br>true if the contact is a private person<br>
* else: false <br>
*/
ContactUtils.isPrivatePerson = function(pContactId, pPersonId, pOrganisationId)
{
var contactType = ContactUtils.getContactType(pContactId, pPersonId, pOrganisationId);
return contactType == 2;
}
/**
* Check if the contact is of the contactType "3": person of an organisation <br>
* This function uses "ContactUtils.getContactType" to check the type.<br>
* So for more information about when to use whcih parameters for better perfrmance see it's documentation.<br>
* <br>
* <br>
* @param {String} pContactId
* @param {String} pPersonId selected from the CONTACT table
* @param {String} pOrganisationId selected from the CONTACT table
* <br>
* @return {Boolean} <br>true if the contact is a person of an organisation<br>
* else: false <br>
*/
ContactUtils.isPersonOfOrganisation = function(pContactId, pPersonId, pOrganisationId)
{
var contactType = ContactUtils.getContactType(pContactId, pPersonId, pOrganisationId);
return contactType == 3;
}
Johannes Goderbauer
committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* get the type of contact for a relationId <br>
* If you already have persId and orgId from the CONTACT table, use getContactTypeByPersOrg() <br>
* @param {String} pContactId
* <br>
* @return {Integer} 0 if relationId not found <br>
* 1 if organisation <br>
* 2 if privat person <br>
* 3 if person of an organisation <br>
*/
ContactUtils.getContactTypeByContactId = function(pContactId)
{
var relationData = ContactUtils.getPersOrgIds(pContactId);
if (relationData[0])
{
return this.getContactTypeByPersOrg(relationData[1], relationData[2]);
}
else
{
return 0;
}
}
/**
* Get the type of contact. <br>
* 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 contact. <br>
* <br>
* !!It does not check if the person / org ids really exist!! <br>
* !!And it does not check if really any contact with this person / org ids exist!! <br>
* <br>
* This function is more performant than getContactTypeByContactId, <br>
* because it doesn't load something from the db. <br>
* <br>
* It is meant to be used by entitys, where you can load person and org with the DataRecord. <br>
* This saves an extra select from CONTACT. <br>
* <br>

Benjamin Ulrich
committed
* @param {String} pPersonId selected from the CONTACT table
* @param {String} pOrganisationId selected from the CONTACT table
Johannes Goderbauer
committed
* <br>
* @return {Integer} <br>0 if both ids are empty <br>
* 1 if organisation <br>
* 2 if privat person <br>
* 3 if person of an organisation <br>
*/

Benjamin Ulrich
committed
ContactUtils.getContactTypeByPersOrg = function(pPersonId, pOrganisationId)
Johannes Goderbauer
committed
{

Benjamin Ulrich
committed
if (!pPersonId)
Johannes Goderbauer
committed
{

Benjamin Ulrich
committed
if (!pOrganisationId) {
Johannes Goderbauer
committed
return 0; // both are empty
}
return 1; // Organisation da PERSON_ID leer
}
else
{

Benjamin Ulrich
committed
if (pOrganisationId.replace(/\s/g,"") == "0" )
Johannes Goderbauer
committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
{
return 2; // Privatperson da PERSON_ID nicht leer und ORGANISATION_ID.trim() = '0'
}
else
{
return 3; // Person einer Organisation da PERSON_ID nicht leer und ORGANISATION_ID nicht '0'
}
}
}
/**
* return the corresponding context of the contact
*
* 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 contact. <br>
* <br>
* !!It does not check if the person / org ids really exist!! <br>
* !!And it does not check if really any contact with this person / org ids exist!! <br>
* <br>
* This function is more performant than getContextByContactId, <br>
* because it doesn't load something from the db. <br>
* <br>
* It is meant to be used by entitys, where you can load person and org with the DataRecord. <br>
* This saves an extra select from CONTACT. <br>
* <br>
*

Benjamin Ulrich
committed
* @param {String} pPersonId selected from the CONTACT table
* @param {String} pOrganisationId selected from the CONTACT table
Johannes Goderbauer
committed
*
* @return {String} contextname or "" if both ids are empty
*/

Benjamin Ulrich
committed
ContactUtils.getContextByPersOrg = function(pPersonId, pOrganisationId)
Johannes Goderbauer
committed
{

Benjamin Ulrich
committed
switch (ContactUtils.getContactTypeByPersOrg(pPersonId, pOrganisationId))
Johannes Goderbauer
committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
{
case 1: // Org
return ContextUtils.getContextName("Organisation");
case 2: // private Person
case 3: // Person
return ContextUtils.getContextName("Person");
default:
return "";
}
}
/**
* return the corresponding context of the contact <br>
* If you already have persId and orgId from the CONTACT table, use getContextByPersOrg() <br>
*
* @param {String} pContactId
* @return {String} contextname or "" if contact not found
*/
ContactUtils.getContextByContactId = function(pContactId)
{
var relationData = ContactUtils.getPersOrgIds(pContactId);
return ContactUtils.getContextByPersOrg(relationData[1], relationData[2])
}
/**
* get the person- and org-id from a contact as array
*
* @param {String} pContactId
* @return {String[]} result as [contactid, persid, orgid] if one of them is null in the db, "" will be returned as the id. if Contactid is empty -> [] is returned
Johannes Goderbauer
committed
*/
ContactUtils.getPersOrgIds = function(pContactId)
{
var persOrgIds = newSelect("CONTACTID, PERSON_ID, ORGANISATION_ID")
.from("CONTACT")
.whereIfSet("CONTACT.CONTACTID", pContactId)
.arrayRow(true);
Johannes Goderbauer
committed
Johannes Goderbauer
committed
}
/**
* get the person- and org-id from a contact as array
*
* @param {String} pContactId
* @return {String[]} result as [contactid, persid, orgid] if one of them is null in the db, "" will be returned as the id. if Contactid is empty -> [] is returned
*/
ContactUtils.getPersOrgIds = function(pContactId)
return newSelect("CONTACTID, PERSON_ID, ORGANISATION_ID")
.from("CONTACT")
.whereIfSet("CONTACT.CONTACTID", pContactId)
.arrayRow(true);
}
/**
* automatically determines the organisations contactid by specifying a person-contactid (or organisation-contactid)
* this is done with a sql statement, so this will only affect to the db commited data
*
* @param {String} pAnyContactId a person-contactid or organisation-contactid
* @return {String} the contactId of the organisation or ""
*/
ContactUtils.getOrganisationContactId = function(pAnyContactId)
{
return newSelect("CONTACT.CONTACTID")
.from("CONTACT")
.join("CONTACT", "anyContact.ORGANISATION_ID = CONTACT.ORGANISATION_ID and CONTACT.PERSON_ID is null", "anyContact")
.whereIfSet(["CONTACT", "CONTACTID", "anyContact"], pAnyContactId)
.cell(true);
/**
* get the contactId from the OrganisationId
*

Benjamin Ulrich
committed
* @param {String} pOrganisationId
* @return {String} the contactId or ""
*/

Benjamin Ulrich
committed
ContactUtils.getOrgContactId = function(pOrganisationId)

Benjamin Ulrich
committed
if (pOrganisationId) {

Benjamin Ulrich
committed
.where("CONTACT.ORGANISATION_ID", pOrganisationId)
}
return "";
}
Johannes Goderbauer
committed
/**
* get the name of the person or organisation
*
* @param {String} pContactId the contact id where pers-name or orgname shall be loaded
Johannes Goderbauer
committed
* @param {Boolean} [pIncludeOrganisation=true] false if the organisation shall not be included in the result string
Johannes Goderbauer
committed
*
* @return {String} the name or ""
*/
Johannes Goderbauer
committed
ContactUtils.getFullTitleByContactId = function(pContactId, pIncludeOrganisation)
Johannes Goderbauer
committed
{
var data = newSelect("ORGANISATION.NAME, PERSON.SALUTATION, PERSON.TITLE, PERSON.FIRSTNAME, PERSON.MIDDLENAME, PERSON.LASTNAME")
.from("CONTACT")
.join("ORGANISATION", "ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID")
.leftJoin("PERSON", "PERSON.PERSONID = CONTACT.PERSON_ID")
.whereIfSet("CONTACT.CONTACTID", pContactId)
.arrayRow(true);
if (data.length == 0)
return "";
var contact = new Contact();
[contact.organisationName, contact.salutation, contact.title, contact.firstname, contact.middlename, contact.lastname] = data;
Johannes Goderbauer
committed
var rendererOptions;
if (pIncludeOrganisation === false)
rendererOptions = ContactTitleRenderer.OPTIONS.NoOption;
else
rendererOptions = ContactTitleRenderer.OPTIONS.IncludeOrganisation;
var renderer = new ContactTitleRenderer(contact, rendererOptions);
Johannes Goderbauer
committed
}
/**
* get the name of the person
* do not use this for a mass of data (e.g. in a loop) since this will be slow due to select-time
*
* @param {String} pPersonId the id of the person where the data shall be loaded
Johannes Goderbauer
committed
* @param {Boolean} [pIncludeOrganisation=true] false if the organisation shall not be included in the result string
Johannes Goderbauer
committed
*
* @return {String} the name or ""
*/
Johannes Goderbauer
committed
ContactUtils.getTitleByPersonId = function(pPersonId, pIncludeOrganisation)
Johannes Goderbauer
committed
{
var data = newSelect("PERSON.SALUTATION, PERSON.TITLE, PERSON.FIRSTNAME, PERSON.MIDDLENAME, PERSON.LASTNAME")
.from("PERSON")
.whereIfSet("PERSON.PERSONID", pPersonId)
.arrayRow(true);
if (data.length == 0)
return "";
var contact = new Contact();
[contact.salutation, contact.title, contact.firstname, contact.middlename, contact.lastname] = data;
Johannes Goderbauer
committed
var rendererOptions;
if (pIncludeOrganisation === false)
rendererOptions = ContactTitleRenderer.OPTIONS.NoOption;
else
rendererOptions = ContactTitleRenderer.OPTIONS.IncludeOrganisation;
var renderer = new ContactTitleRenderer(contact, rendererOptions);
Johannes Goderbauer
committed
}
/**
* get the name of the person
* do not use this for a mass of data (e.g. in a loop) since this will be slow due to select-time
*
* @param {String} pContactId the id of the contact entry with the person where the data shall be loaded
*
* @return {String} the name or ""
*/
ContactUtils.getTitleByContactId = function(pContactId)
{
var personId = newSelect("CONTACT.PERSON_ID")
.from("CONTACT")
.whereIfSet("CONTACT.CONTACTID", pContactId)
.cell(true);
Johannes Goderbauer
committed
Johannes Goderbauer
committed
}
/**
* returns the from string for the contact joined with org, person, address
*
* @return {String}
*/
ContactUtils.getFullContactString = function()
{
return " CONTACT "
+ " join ORGANISATION on ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID "
+ " left join PERSON on PERSON.PERSONID = CONTACT.PERSON_ID "
+ " left join ADDRESS on ADDRESS.ADDRESSID = CONTACT.ADDRESS_ID";
Johannes Goderbauer
committed
}
/**
* Returns a SqlBuilder for selecting a contact with organisation, person and address.
*
* @param {String|Array|SqlBuilder} [pSelectFields] The select-part to set for the query, if it is omitted, no select will be set.
* @return {SqlBuilder}
*/
ContactUtils.getFullContactSqlBuilder = function (pSelectFields)
{
var sql = new SqlBuilder()
.from("CONTACT")
.join("ORGANISATION", "ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID")
.leftJoin("PERSON", "PERSON.PERSONID = CONTACT.PERSON_ID")
.leftJoin("ADDRESS", "ADDRESS.ADDRESSID = CONTACT.ADDRESS_ID");
if (pSelectFields)
sql.select(pSelectFields);
return sql;
}
Johannes Goderbauer
committed
/**
* get the addressid of the default address
*
* @return {String} the ID or "" if no default address exists
*/
ContactUtils.getDefaultAddressId = function(pContactId)
{
return newSelect("ADDRESS_ID")
.from("CONTACT")
.whereIfSet("CONTACT.CONTACTID", pContactId)
.cell(true);
Johannes Goderbauer
committed
}
* makes a SqlBuilder that checks if there's (not) a commrestriction for a contact
* @param {String} [pMedium=undefined] medium to check if undefined, don't check it
* @param {boolean} [pNoRestriction=false] if true, the condition gets every contact that has no
* a commrestriction, otherwise every contact that has a commrestriction
* @param {String|Number} [pStartDate=current date] date to check against the start date of the commrestriction
*/
ContactUtils.getCommRestrictionCondition = function (pMedium, pNoRestriction, pStartDate)
{
if (!pStartDate && pStartDate !== 0)
pStartDate = datetime.date();
var existsQuery = new SqlBuilder()
.whereIfSet("COMMRESTRICTION.MEDIUM", pMedium)
.and("COMMRESTRICTION.STARTDATE", pStartDate, SqlBuilder.LESS_OR_EQUAL())
.and(newWhere()
// check contactid
.or("COMMRESTRICTION.CONTACT_ID = CONTACT.CONTACTID")
// or the contact id of the organisation
.or("COMMRESTRICTION.CONTACT_ID", newSelect("orgContact.CONTACTID")
.from("CONTACT anyContact")
.join("CONTACT orgContact", newWhere("anyContact.ORGANISATION_ID = orgContact.ORGANISATION_ID")
.and("orgContact.PERSON_ID is null")
)
.where("anyContact.CONTACTID = CONTACT.CONTACTID")
)
var cond = pNoRestriction ? SqlBuilder.NOT_EXISTS() : SqlBuilder.EXISTS();
/**
* checks if a contact has a commrestriction
*
* @param {String} pContactId contact id
* @param {String} pMedium medium to check
* @param {String|Number} [pStartDate=current date] the start date of the commrestriction
*
* @return {Boolean} true, if the contact has a commrestriction, otherwise false
*/
ContactUtils.hasCommRestriction = function(pContactId, pMedium, pStartDate)
return newSelect("CONTACTID")
.from("CONTACT")
.where("CONTACT.CONTACTID", pContactId)
.and(ContactUtils.getCommRestrictionCondition(pMedium, false, pStartDate))
.cell() != "";
/**
* returns a sql subselect which concatenates all commrestriction displayvalues (translated).
* The select needs the CONTACT.CONTACTID column.
*
* @return {String} the resulting subselect
*/
ContactUtils.getActiveCommRestrictionsSubselect = function()
{
var mediumList = KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.communicationMediumCampaign(), "COMMRESTRICTION.MEDIUM");
var mask = new SqlMaskingUtils()
var orgContactSubselect = newSelect("orgContact.CONTACTID")
.from("CONTACT anyContact")
.join("CONTACT orgContact", newWhere("anyContact.ORGANISATION_ID = orgContact.ORGANISATION_ID")
.and("orgContact.PERSON_ID IS NULL"))
.where("anyContact.CONTACTID = CONTACT.CONTACTID")
var group = mask.getGroupConcat("DISTINCT "+mediumList, "', '");
var subselect = "''";
if(group)
var res = newSelect(group).from("COMMRESTRICTION")
.where("COMMRESTRICTION.CONTACT_ID = CONTACT.CONTACTID")
var subres = newSelect(group).from("COMMRESTRICTION")
.where("COMMRESTRICTION.CONTACT_ID in ( "+orgContactSubselect.toString()+")")
subselect = mask.concatWithSeparator(["("+res.toString()+")", "("+subres.toString()+")"], ", ", false);
}
return subselect;
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/**
* returns a sql subselect which counts all Commrestrictions from a Person and his Organisation.
* The select needs the CONTACT.CONTACTID column.
*
* @return {String} the resulting subselect
*/
ContactUtils.getCommRestrictionCount = function(pMedium, pStartDate)
{
if (!pStartDate && pStartDate !== 0)
pStartDate = datetime.date();
var firstLevel = new SqlBuilder()
.select("count(*)")
.from("COMMRESTRICTION")
.whereIfSet("COMMRESTRICTION.MEDIUM", pMedium)
.and("COMMRESTRICTION.STARTDATE", pStartDate, SqlBuilder.LESS_OR_EQUAL())
.and("COMMRESTRICTION.CONTACT_ID = CONTACT.CONTACTID");
var secondLevel = new SqlBuilder()
.select("count(*)")
.from("COMMRESTRICTION")
.whereIfSet("COMMRESTRICTION.MEDIUM", pMedium)
.and("COMMRESTRICTION.STARTDATE", pStartDate, SqlBuilder.LESS_OR_EQUAL())
.and("COMMRESTRICTION.CONTACT_ID", newSelect("c1.CONTACTID").from("CONTACT", "c1")
.join("CONTACT", "c2.ORGANISATION_ID = c1.ORGANISATION_ID and c1.PERSON_ID is null", "c2")
.where("c2.CONTACTID = CONTACT.CONTACTID")
, SqlBuilder.IN());
return firstLevel.toString() +") + ("+secondLevel.toString();
}
/**
* returns sql for filtering links, mail addresses or telephone numbers
*
* @param {String} pOperator filter operator
* @param {String} pRawvalue value that is filtered for
* @param {String} pFilterOperatorName name of the filter operator
* @param {String} pComparison comparison operator
* @param {String} pType type of communication
*
* @return {String} the resulting select
*/
ContactUtils.getCommFilter = function(pOperator, pRawvalue, pFilterOperatorName, pComparison, pType)
{
let resultSqlCond;
//SqlBuilder not implemented as the statement needs to be finished in the switch case
let commpart = "(select COMMUNICATION.CONTACT_ID from COMMUNICATION "
+ " join AB_KEYWORD_ENTRY on AB_KEYWORD_ENTRY.KEYID = COMMUNICATION.MEDIUM_ID "
+ " join AB_KEYWORD_ATTRIBUTERELATION on AB_KEYWORD_ENTRY.AB_KEYWORD_ENTRYID = AB_KEYWORD_ATTRIBUTERELATION.AB_KEYWORD_ENTRY_ID"
+ " where AB_KEYWORD_ATTRIBUTERELATION.CHAR_VALUE = '" + pType + "'";
resultSqlCond = "CONTACTID in " + commpart + " and COMMUNICATION.ADDR " + pFilterOperatorName;
switch (pComparison)
{
case "EQUAL":
case "NOT_EQUAL":
resultSqlCond = resultSqlCond + " '" + pRawvalue + "')";
break;
case "CONTAINS":
case "CONTAINSNOT":
resultSqlCond = resultSqlCond + " '%" + pRawvalue + "%')";
break;
case "STARTSWITH":
resultSqlCond = resultSqlCond + " '" + pRawvalue + "%')";
break;
case "ENDSWITH":
resultSqlCond = resultSqlCond + " '%" + pRawvalue + "')";
break;
case "ISNULL":
resultSqlCond = "CONTACTID not in " + commpart + ")";
break;
case "ISNOTNULL":
resultSqlCond = resultSqlCond + ")";
break;
return resultSqlCond;
}
Johannes Goderbauer
committed
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/**
* object for handling of a single contact
* provides static- and instance-functions
*
* @class
*
*/
function Contact()
{
//storage for information
this.salutation = "";
this.title = "";
this.firstname= "";
this.middlename = "";
this.lastname = "";
this.suffix = "";
this.customercode = "";
this.organisationName = "";
this._contactType = Contact.TYPES.Auto;
}
/**
* sets the contactType of a contact
* reserved for future implementation
*
* @param {String} pContactType contactType that shall be set; value of Contact.TYPES.
*
* @return void
*/
Contact.prototype.setContactType = function (pContactType)
{
if (! ObjectUtils.existsValue(Contact.TYPES, pContactType))
throw new TypeError("the given contact type is not a valid value and not a contact type");
this._contactType = pContactType;
};
/**
* returns the contactType of a contact
* reserved for future implementation
*
* @return {String} the contactType is a value of Contact.TYPES.
*/
Contact.prototype.getContactType = function ()
{
return this._contactType;
};
/**
* constants for types of Contacts
* use only within functions
* reserved for future implementation
*
* @static
*/
Contact.TYPES = {
Organisation: "organisation",
Contact: "contact",
Private: "private",
Auto: null
};
/**
* creates a new Contact-object with a preset of DB-columns
*
* @static
*/
Contact.createWithColumnPreset = function()
{
var contact = new Contact();
contact.salutation = "PERSON.SALUTATION";
contact.title = "PERSON.TITLE";
contact.firstname= "PERSON.FIRSTNAME";
contact.middlename = "PERSON.MIDDLENAME";
contact.lastname = "PERSON.LASTNAME";
contact.suffix = "PERSON.TITLESUFFIX";
contact.customercode = "ORGANISATION.CUSTOMERCODE";
contact.organisationName = "ORGANISATION.NAME";
return contact;
};
/**
* creates a new Contact-object with a preset of DB-columns
*
* @static
*/
Contact.createWithColumnPresetForPrivatePerson = function()
{
var contact = new Contact();
contact.salutation = "PERSON.SALUTATION";
contact.title = "PERSON.TITLE";
contact.firstname= "PERSON.FIRSTNAME";
contact.middlename = "PERSON.MIDDLENAME";
contact.lastname = "PERSON.LASTNAME";
contact.suffix = "PERSON.TITLESUFFIX";
return contact;
};
Johannes Goderbauer
committed
/**
* object for rendering a Contact-object into text representation or an sql that represents the text
* provides static- and instance-functions
*
* @param {Contact} pContact The Contact-object to render
* @param {Number} pOptions additional options for rendering; use values of ContactTitleRenderer.OPTIONS and pass them by bitwise OR concatination; e.g.:
* OPTION_1 | OPTION_2 | OPTION_5
*
* @class
*
* //TODO: far too complex to understand for the very easy job it does. Refactor for better understanding what this (and related) function do, and how.
*
Johannes Goderbauer
committed
*/
function ContactTitleRenderer(pContact, pOptions)
{
this.contact = pContact;
if (pOptions !== undefined)//null means null which is "no option"; so check exactly for undefined to check if default option has to be set
this._options = pOptions;
else
this._options = ContactTitleRenderer.OPTIONS.IncludeOrganisation;
Johannes Goderbauer
committed
//function that renders the contact into a sql expression (e.g. for a subselect)
this._asSqlFn = function(){
var maskingUtil = new SqlMaskingUtils();
var res = maskingUtil.concatWithSeparator([this.contact.salutation, this.contact.title, this.contact.firstname, this.contact.middlename, this.contact.lastname].filter(function (e){
Johannes Goderbauer
committed
return e != "";
Johannes Goderbauer
committed
//binary AND check for possibility to check serveral options
Johannes Goderbauer
committed
if (this._options & ContactTitleRenderer.OPTIONS.IncludeOrganisation && this.contact.organisationName)
res = maskingUtil.concatWithSeparator([res, this.contact.organisationName], " | ");
Johannes Goderbauer
committed
return res;
};
//function that renders the contact into a text (e.g. for a displayValue)
this._asStringFn = function (){
var res = StringUtils.concat(" ", [this.contact.salutation, this.contact.title, this.contact.firstname, this.contact.middlename, this.contact.lastname]);
Johannes Goderbauer
committed
//binary AND check for possibility to check serveral options
Johannes Goderbauer
committed
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
if (this._options & ContactTitleRenderer.OPTIONS.IncludeOrganisation && this.contact.organisationName)
res = StringUtils.concat(" | ", [res, this.contact.organisationName]);
return res;
}
}
/**
* constants for options for the ContactTitleRenderer
* use only within functions
*
* @static
*/
ContactTitleRenderer.OPTIONS = {
NoOption: 0,
IncludeOrganisation: 1
};
/**
* function that renders the contact into a sql expression (e.g. for a subselect)
* what this function exactly does depends on specified values and options specified in the Renderer-object
*
* @return {String} sql-expression that can be placed inside a select statement
*/
ContactTitleRenderer.prototype.asSql = function ()
{
return this._asSqlFn.apply(this, arguments);
};
/**
* function that renders the contact into text
* what this function exactly does depends on specified values and options specified in the Renderer-object
*
* @return {String} rendered values as text
*/
ContactTitleRenderer.prototype.asString = function ()
{
return this._asStringFn.apply(this, arguments);
};