Newer
Older
Alexander Riedl
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import("system.neon");
import("Util_lib");
import("system.translate");
import("Communication_lib");
import("Placeholder_lib");
import("system.SQLTYPES");
import("system.vars");
import("system.datetime");
import("system.util");
import("system.tools");
import("system.favorite");
import("system.logging");
import("Sql_lib");
import("system.db");
import("system.entities")
/**
* Provides methods for handling and interacting with the EWS Plugin
*
* @class
*/
function EwsClientSyncUtils() {}
/*
* Constant which contains the ewsSync Tag
* if the tag has to be changed a constant is the better way
*/
EwsClientSyncUtils.EWSSYNCTAG = function(){
return "ewssync";
}
/*
* prepares the synctable, insert new entries and mark for deletion
*
*/
EwsClientSyncUtils.prepareContactSyncTable = function(){
var favoriteDataArray = EwsClientSyncUtils.getEwsFavorites();
var ewsSyncData = newSelect("SYNCCONTACTID, ASYS_FAVORITEID, USER_ID, CONTACT_ID")
.from("ab_synccontact")
.table();
var dataIndex = {
index: new Map(),
add: function (pA, pB)
{
var idxMap = this.index;
if (!idxMap.has(pA))
idxMap.set(pA, new Set());
idxMap.get(pA).add(pB);
},
has: function (pA, pB)
{
return this.index.has(pA) && this.index.get(pA).has(pB);
}
};
//preparing the value for inserting in ewsSync Table
var toInsertFavs = [];
for (let i = 0, l = ewsSyncData.length; i < l; i++)
{
//3 - contactID 2 userID
dataIndex.add(ewsSyncData[i][3], ewsSyncData[i][2]);
}
//we want to insert those which aren't in the sync table yet but which are tagged for sync
toInsertFavs = favoriteDataArray.filter(function (row) {
return !dataIndex.has(row[1], row[2]);
});
//preparing values for updating entrys in the ewsSync Table
dataIndex.index.clear();
var toUpdate = [];
for (let i = 0, l = favoriteDataArray.length; i < l; i++)
{
dataIndex.add(favoriteDataArray[i][1], favoriteDataArray[i][2]);
}
//dataset which are in the synctable but aren't tagged for sync (favorites ewssync) has to be updated for deletion
toUpdate = ewsSyncData.filter(function (row){
return !dataIndex.has(row[3], row[2]);
}
);
let statements = [];
let toInsert = [];
let cols = ["SYNCCONTACTID", "ASYS_FAVORITEID", "CONTACT_ID", "USER_ID"];
let vals = [];
let table = "AB_SYNCCONTACT";
let updStatements = [];
for(let i = 0; i < toInsertFavs.length; i++)
{
vals = [];
vals = [util.getNewUUID(), toInsertFavs[i][0], toInsertFavs[i][1], toInsertFavs[i][2]]
statements.push([table, cols, null, vals]);
}
for(let i = 0; i < toUpdate.length; i++)
{
updStatements.push([table, ["DATE_DEL"], null, [vars.get("$sys.date")], "ASYS_FAVORITEID = '" + toUpdate[i][1] + "'" ]);
}
try{

Andreas Schindlbeck
committed
let alias = db.getCurrentAlias();
let count = db.inserts(statements, alias);
let updCount = db.updates(updStatements, alias);
Alexander Riedl
committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
}
catch(exc){
logging.log(exc)
}
}
/*
* get the contact which are marked with the ewssync favorite Tag
*
* @return [Array] favorite Data [[]]
*/
EwsClientSyncUtils.getEwsFavorites = function(){
//get Favorites which are tagged with the Sync Tag
// var conf = favorite.createGetFavoritesConfig();
// conf.setFavoriteGroupTitle(EwsClientSyncUtils.EWSSYNCTAG());
// conf.setObjectType("Person");
//
// var favoriteData = favorite.getFavorites(conf);
// var favoriteDataArray = [];
//
// for (var temp in favoriteData)
// {
// let favId = favoriteData[temp]["id"];
// let rowId = favoriteData[temp]["rowid"];
// let userID = favoriteData[temp]["group"]["groupuser"];
//
// favoriteDataArray.push([favId, rowId, userID]);
// }
//in the current version it isn't possible to call the favorite API within a Serverprocess this will be implemented in the next RC
var favoriteDataArray = new SqlBuilder("_____SYSTEMALIAS")
.selectDistinct("ASYS_RECORD.ID, ASYS_RECORD.ROW_ID, ASYS_RECORDGROUP.USER_ID")
.from("ASYS_RECORD")
.join("ASYS_RECORDGROUP", "ASYS_RECORDGROUP.ID = ASYS_RECORD.RECORDGROUP_ID")
.where("ASYS_RECORD.OBJECT_TYPE", "Person")
.and("ASYS_RECORDGROUP.TITLE", EwsClientSyncUtils.EWSSYNCTAG(), "LOWER(#) = ?")
.groupBy("ASYS_RECORDGROUP.USER_ID, ASYS_RECORD.ROW_ID, ASYS_RECORD.ID")
.table();
return favoriteDataArray;
}
/*
* sets the editdate for the entrys in the synctable, in reason to get synced with exchange
*
* @param {String} pTableName
* @param {String} pDataSetID req
* @param {String} pDate req
*
* @return {void}
*/
EwsClientSyncUtils.setContactToSync = function(pTableName, pDataSetID, pDate){
var affectedTables = ["ORGANISATION" , "PERSON", "CONTACT", "ADDRESS", "COMMUNICATION"];
var affectedIDs = [];
if (affectedTables.indexOf(pTableName) != -1)
{
switch(pTableName)
{
case "ORGANISATION":
affectedIDs = newSelect("CONTACT.CONTACTID").from("CONTACT").where("CONTACT.ORGANISATION_ID", pDataSetID).arrayColumn();
break;
case "PERSON":
affectedIDs = newSelect("CONTACT.CONTACTID").from("CONTACT").where("CONTACT.PERSON_ID", pDataSetID).arrayColumn();
break;
case "CONTACT":
affectedIDs = [pDataSetID];
break;
case "ADDRESS":
affectedIDs = newSelect("ADDRESS.CONTACT_ID").from("ADDRESS")
.where("ADDRESS.ADDRESSID", pDataSetID)

Sebastian Pongratz
committed
.union(newSelect("CONTACT.CONTACTID").from("CONTACT").where("CONTACT.ADDRESS_ID", pDataSetID)).arrayColumn();
Alexander Riedl
committed
break;
case "COMMUNICATION":

Sebastian Pongratz
committed
affectedIDs = newSelect("COMMUNICATION.OBJECT_ROWID").from("COMMUNICATION").where("COMMUNICATION.COMMUNICATIONID", pDataSetID).and("COMMUNICATION.OBJECT_TYPE", "Contact").arrayColumn();
Alexander Riedl
committed
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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
310
311
312
313
314
315
316
317
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
346
347
348
349
350
351
352
353
354
355
356
357
358
break;
}
if (affectedIDs.length > 0)
{
var upd = db.updateData("AB_SYNCCONTACT", ["DATE_EDIT"], null, [pDate],
"EXCHANGEID is not null and CONTACT_ID in ('" + affectedIDs.join("','") + "')");
}
}
}
/*
* prepare Placholder for Exchange contacts
*
* @param {String} pUserId
*/
EwsClientSyncUtils.getPlaceholders = function(pUserId){
var ewsPlaceholders = [];
//EWS Sync Placeholder
//See EWS API Doc for other placeholder
//new Placeholder that has to be synced probably has also to be added in addDataToValueObjects() in the serverprocess
_addSqlPart("CONTACTID", "CONTACT.CONTACTID");
_addSqlPart("EXCHANGEID", "select EXCHANGEID from ab_synccontact where Ab_synccontact.contact_id = contact.contactid and user_id = '"+ pUserId +"'", null, null)
_addSqlPart("Givenname", "PERSON.FIRSTNAME");
_addSqlPart("Surname", "PERSON.LASTNAME");
_addSqlPart("Department", "CONTACT.DEPARTMENT");
_addSqlPart("JobTitle", "CONTACT.CONTACTROLE");
_addSqlPart("CompanyName", "ORGANISATION.NAME");
_addAddressFormat("Business_street", "{street} {buildingno}");
_addAddressFormat("Business_postalcode", "{zip}");
_addAddressFormat("Business_city", "{city}");
_addAddressFormat("Business_state", "{district}");
_addAddressFormat("Business_countryorregion", "{country}");
_addSqlPart("EmailAddress1", CommUtil.getStandardSubSqlMail());
_addSqlPart("BusinessPhone", CommUtil.getStandardSubSqlPhone());
_addSqlPart("BusinessHomepage", CommUtil.getMediumAddrSubSqlByKey("COMMINTERNET"));
_addSqlPart("OtherFax", CommUtil.getMediumAddrSubSqlByKey("COMMFAX"));
_addSqlPart("MobilePhone", CommUtil.getMediumAddrSubSqlByKey("COMMMOBIL"));
_addSqlPart("FileAsMapping","case when PERSON_ID is not null then 'SurnameCommaGivenName' else 'Company' end" , null, null);
// _addSqlPart("HomePhone", "''");
// _addSqlPart("HomeFax", "''");
// _addSqlPart("EmailAddress2", "''");
return ewsPlaceholders;
function _addSqlPart (pName, pSqlPart, pTarget, pTitle)
{
ewsPlaceholders.push(new Placeholder(pName, Placeholder.types.SQLPART, "(" + pSqlPart + ")", pTarget, pTitle));
}
/**
* add an address format placeholder to placeholders
*/
function _addAddressFormat (pName, pFormat, pTarget, pTitle)
{
ewsPlaceholders.push(new Placeholder(pName, Placeholder.types.ADDRESSFORMAT, pFormat, pTarget, pTitle));
}
}
/*
* removes Person favorites with the EWS Tag
*
* @param {Array} pToDelete contains contact_ids for which the favorite should be removed
* @param {String} pUserId - User for which the favorites should be removed
*
* @return {Boolean} true if successfull
*
*/
EwsClientSyncUtils.removeFromFavorite = function(pToDelete, pUserId ){
//maybe in a future version there will be a way to remove favorites on a better way
//all ews related Favorites for the user
var config = favorite.createGetFavoritesConfig()
.setFavoriteGroupTitle(EwsClientSyncUtils.EWSSYNCTAG())
.setGroupType(favorite.FAVORITE_GROUP).setObjectType("Person")
.setUserId(pUserId);
var ewsFavorite = favorite.getFavorites(config);
var favsToDelete = [];
var dataIndex = {
index: new Map(),
add: function (pA)
{
var idxMap = this.index;
if (!idxMap.has(pA))
idxMap.set(pA, new Set());
},
has: function (pA)
{
return this.index.has(pA);
}
};
for each(let row in pToDelete)
{
dataIndex.add(row)
}
// delete those which are in the selection and are also in ewsFavorite
for each (let row in ewsFavorite)
{
if(dataIndex.has(row["rowid"]))
favsToDelete.push(row["id"]);
}
var delConfig = favorite.createRemoveMultipleByIdConfig().setFavoriteRecordIds(favsToDelete);
return favorite.remove(delConfig);
}
/*
* Add contacts to favorites as ewssync group
*
* @param {Array} pToInsert Array with contactIds which should be added to the sync
*/
EwsClientSyncUtils.addToEwsFavorite = function(pToInsert){
var userID = tools.getCurrentUser()[tools.NAME]
var config = favorite.createAddFavoriteConfig();
config.setFavoriteGroupTitle(EwsClientSyncUtils.EWSSYNCTAG());
config.setObjectType("Person");
config.setUserId(userID);
config.setGroupType(favorite.FAVORITE_GROUP)
for(let i = 0, l = pToInsert.length; i < l; i++){
config.setRowId(pToInsert[i]);
favorite.add(config);
}
}
/**
* Opens a context to add or remove contacts from the ewssync<br>
*
* @param {String[]} pContactIds Contacts that should be added.<br>
* @param {String|Object} pFilter the filter for the contacts that should be used if no contact is selected
* @param {String} pMode the mode which should be used ("ADD" or "REMOVE") default ADD
*/
EwsClientSyncUtils.openEwsSyncAddContactView = function(pContactIds, pFilter, pMode)
{
if (!Utils.isString(pContactIds))
pContactIds = JSON.stringify(pContactIds);
if (!Utils.isString(pFilter))
pFilter = JSON.stringify(pFilter);
var params = {
"ContactIds_param": pContactIds,
"ContactFilter_param": pFilter,
"Mode_param": pMode
}
neon.openContext("EwsSyncAddContacts", "EwsSyncAddContactsEdit_view", null, neon.OPERATINGSTATE_VIEW, params);
}