-
Notifications
You must be signed in to change notification settings - Fork 0
/
EF2.js
429 lines (365 loc) · 15.5 KB
/
EF2.js
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
110
111
112
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
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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
var fs = require('fs');
var myArgs = process.argv.slice(2); //put all the arguments given to the program from the thrid one
var fileToParse = myArgs[1];
var fileToWrite = (fileToParse.substring(0, fileToParse.length - 3) + "ics");
var nbCellules = 0; //Va compter le nombre de cellules parsées
var heureActuellementRegardee = 0;
var compteCellules = 0;
var launchCSVToICalendar = function () {
//Ouverture et lecture d'un fichier
try {
var data = fs.readFileSync(myArgs[1], 'utf8'); //use synchrone call to prevent code execution with errors of loading files
analyzer = new CSVToICalendar;
analyzer.transform(data);
console.log(fileToParse + " has been created as " + fileToParse.substring(0, fileToParse.length - 3) + "ics");
} catch (e) {
console.log(e);
process.exit(1);
}
//console.log(analyzer.parsedPOI);
}
// POI : RDV au format Pivot
var POI = function (date, duration, manager, location)
{
this.date = date;
this.duration = duration;
this.manager = manager;
this.location = location;
}
// CSVToICalendar
var CSVToICalendar = function () {
// The list of POI parsed from the input file.
this.parsedPOI = []
this.symb = ["lundi;mardi;mercredi;jeudi;vendredi;samedi;dimanche"];
}
// Transformation procedure
// tokenize : tranform the data input into a list
// <eol> = CRLF
CSVToICalendar.prototype.tokenize = function (data) {
//return data.split(/(\r\n|: )/);
var separator = /(\r\n)/;
data = data.split(separator);
data = data.filter(function (val, idx) {
return !val.match(separator);
});
return data;
}
CSVToICalendar.prototype.transform = function (data) {
fs.appendFile(fileToWrite, "BEGIN:VCALENDAR\r\n" +
"VERSION:2.0\r\n" +
"PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n", function (err) {
if (err) {
return console.log(err);
}
});
var tData = this.tokenize(data);
//console.log(tData);
this.header(tData);
fs.appendFile(fileToWrite, "END:VCALENDAR", function (err) {
if (err) {
return console.log(err);
}
});
}
CSVToICalendar.prototype.err = function (msg, input) {
console.log("Transformation Error ! -- msg : " + msg);
process.exit(0);
}
// Read and return a symbol from input
CSVToICalendar.prototype.next = function (input) {
var curS = input.shift();
//console.log(curS);
return curS
}
// accept : verify if the arg s is part of the language symbols.
CSVToICalendar.prototype.accept = function (s) {
var idx = this.symb.indexOf(s);
// index 0 exists
if (idx === -1) {
this.err("symbol " + s + " unknown", [" "]);
return false;
}
return idx;
}
// check : check whether the arg elt is on the head of the list
CSVToICalendar.prototype.check = function (s, input) {
if (this.accept(input[0]) == this.accept(s)) {
return true;
}
return false;
}
// expect : expect the next symbol to be s.
CSVToICalendar.prototype.expect = function (s, input) {
if (s == this.next(input)) {
//console.log("Reckognized! "+s)
return true;
} else {
this.err("symbol " + s + " doesn't match", input);
}
return false;
}
// Parser rules
// <header> = "lundi;mardi;mercredi;jeudi;vendredi;samedi;dimanche" <eol> <listPoi>
CSVToICalendar.prototype.header = function (input) {
if (this.check("lundi;mardi;mercredi;jeudi;vendredi;samedi;dimanche", input)) {
this.expect("lundi;mardi;mercredi;jeudi;vendredi;samedi;dimanche", input);
this.listPoi(input);
return true;
} else {
return false;
}
}
// <listPoi> = *(RDV";"RDV";"RDV";"RDV";"RDV";"RDV";"RDV";"<eol>)
CSVToICalendar.prototype.listPoi = function (input) {
var separator = /(;)/;
//Reconcaténer la chaîne de caractère afin de la split
var inputAsString = "";
for (var i = 0; i < input.length; i++) {
inputAsString += input[i];
}
//Split selon separator <;>
var listRdv = inputAsString.split(separator);
listRdv = listRdv.filter(function (val, idx) {
return !val.match(separator);
});
//Si le nombre de rendez-vous n'est pas égal à 7x, renvoyer une erreur
if (listRdv.length % 7 != 0)
this.err("We don't have 7 events per week", new Object());
//console.log(listRdv);
this.rdv(listRdv);
return;
}
// <rdv> = "vide"
// ou <rdv> = FONCTIONINTERVENANT.'-'.NOMINTERVENANT.'-'.PRENOMINTERVENANT.'-'.TELINTERVENANT.'-'.SOCIETEINTERVENANT.'-'.NOMPATIENT.'-'.PRENOMPATIENT.'-'.TELPATIENT.'-'.VILLEPATIENT.'-'.NUMRUEPATIENT RUEPATIENT.'-'.CODEPOSTALPATIENT
CSVToICalendar.prototype.rdv = function (input)
{
//console.log(compteCellules);
if (compteCellules == 7)
compteCellules = 0;
if ((compteCellules == 0) && (heureActuellementRegardee % 100 == 0)) {
heureActuellementRegardee += 30;
} else if ((compteCellules == 0) && (heureActuellementRegardee % 100 == 30)) {
heureActuellementRegardee += 70;
}
//Si le rdv = "vide"
if (input[0] == "vide") {
//console.log(input[0]);
nbCellules++;
compteCellules++;
this.next(input);
this.rdv(input);
return true;
//Sinon c'est soit un rdv correct, soit un rdv avec une/des erreur(s)
} else {
//Si input n'est pas vide, on vérifie le prochain rdv
if (input.length > 0) {
var rdv = input[0];
var separator = /(-)/;
rdvInfos = rdv.split(separator);
rdvInfos = rdvInfos.filter(function (val, idx) {
return !val.match(separator);
});
this.isTextApostrophe(rdvInfos[0]);
this.isTextApostrophe(rdvInfos[1]);
this.isText(rdvInfos[2]);
this.isTel(rdvInfos[3]);
this.isSociety(rdvInfos[4]);
this.isTextApostrophe(rdvInfos[5]);
this.isText(rdvInfos[6]);
this.isTel(rdvInfos[7]);
this.isTextApostrophe(rdvInfos[8]);
this.isStreet(rdvInfos[9]);
this.isPostalCode(rdvInfos[10]);
/* Création de l'objet rdv et ajout dans le tableau parsedPOI */
var dateDuPlanning = "";
var jourDuPlanning = 0;
var moisDuPlanning = 0;
var anneeDuPlanning = 0;
var heureDuPlanning = "";
if (myArgs[0].substring(0, 8).toLowerCase() == "domicile") {
switch (myArgs[0].substring(12, 14)) {
case "01":
case "03":
case "05":
case "07":
case "08":
case "10":
case "12":
if (parseInt(myArgs[0].substring(9, 11)) + (nbCellules % 7) > 31) {
jourDuPlanning = (parseInt(myArgs[0].substring(9, 11)) + (nbCellules % 7)) % 31;
moisDuPlanning = parseInt(myArgs[0].substring(12, 14)) + 1;
if (moisDuPlanning > 12) {
moisDuPlanning = moisDuPlanning - 12;
anneeDuPlanning = parseInt(myArgs[0].substring(15, 19)) + 1;
} else {
anneeDuPlanning = parseInt(myArgs[0].substring(15, 19));
}
} else {
jourDuPlanning = parseInt(myArgs[0].substring(9, 11)) + (nbCellules % 7);
moisDuPlanning = parseInt(myArgs[0].substring(12, 14));
anneeDuPlanning = parseInt(myArgs[0].substring(15, 19));
}
break;
case "02":
case "04":
case "06":
case "09":
case "11":
if (parseInt(myArgs[0].substring(9, 11)) + (nbCellules % 7) > 31) {
jourDuPlanning = (parseInt(myArgs[0].substring(9, 11)) + (nbCellules % 7)) % 31;
moisDuPlanning = parseInt(myArgs[0].substring(12, 14)) + 1;
anneeDuPlanning = parseInt(myArgs[0].substring(15, 19));
} else {
jourDuPlanning = parseInt(myArgs[0].substring(9, 11)) + (nbCellules % 7);
moisDuPlanning = parseInt(myArgs[0].substring(12, 14));
anneeDuPlanning = parseInt(myArgs[0].substring(15, 19));
}
break;
}
}
if (myArgs[0].substring(0, 11).toLowerCase() == "intervenant") {
switch (myArgs[0].substring(12, 14)) {
case "01":
case "03":
case "05":
case "07":
case "08":
case "10":
case "12":
if (parseInt(myArgs[0].substring(12, 14)) + (nbCellules % 7) > 31) {
jourDuPlanning = (parseInt(myArgs[0].substring(12, 14)) + (nbCellules % 7)) % 31;
moisDuPlanning = parseInt(myArgs[0].substring(15, 17)) + 1;
if (moisDuPlanning > 12) {
moisDuPlanning = moisDuPlanning - 12;
anneeDuPlanning = parseInt(myArgs[0].substring(18, 22)) + 1;
} else {
anneeDuPlanning = parseInt(myArgs[0].substring(18, 22))
}
} else {
jourDuPlanning = parseInt(myArgs[0].substring(12, 14)) + (nbCellules % 7);
moisDuPlanning = parseInt(myArgs[0].substring(15, 17));
anneeDuPlanning = parseInt(myArgs[0].substring(18, 22));
}
break;
case "02":
case "04":
case "06":
case "09":
case "11":
if (parseInt(myArgs[0].substring(12, 14)) + (nbCellules % 7) > 31) {
jourDuPlanning = (parseInt(myArgs[0].substring(12, 14)) + (nbCellules % 7)) % 31;
moisDuPlanning = parseInt(myArgs[0].substring(15, 17)) + 1;
anneeDuPlanning = parseInt(myArgs[0].substring(18, 22));
} else {
jourDuPlanning = parseInt(myArgs[0].substring(12, 14)) + (nbCellules % 7);
moisDuPlanning = parseInt(myArgs[0].substring(15, 17));
anneeDuPlanning = parseInt(myArgs[0].substring(18, 22));
}
break;
}
}
if (heureActuellementRegardee < 1000)
dateDuPlanning = anneeDuPlanning.toString() + moisDuPlanning.toString() + jourDuPlanning.toString() + "T0" + heureActuellementRegardee.toString() + "00";
else
dateDuPlanning = anneeDuPlanning.toString() + moisDuPlanning.toString() + jourDuPlanning.toString() + "T" + heureActuellementRegardee.toString() + "00";
var p = new POI(dateDuPlanning, "PT0H30M0S", rdvInfos[4], (rdvInfos[8] + "," + rdvInfos[9] + "," + rdvInfos[10]));
this.parsedPOI.push(p);
/* Fin création de l'objet rdv */
nbCellules++;
compteCellules++;
fs.appendFile(fileToWrite, "BEGIN:VEVENT\r\n" +
"DTSTART:" + p.date + "\r\n" +
"DURATION:" + p.duration + "\r\n" +
"ORGANIZER:" + p.manager + "\r\n" +
"LOCATION:" + p.location + "\r\n" +
"CONTACT:" + rdvInfos[6] + " " + rdvInfos[5] + "\\," + rdvInfos[7] + "\r\n" +
"END:VEVENT\r\n", function (err) {
if (err) {
return console.log(err);
}
});
this.next(input);
this.rdv(input);
return true;
//Sinon on vérifie une ligne vide
} else {
//console.log(input);
nbCellules++;
compteCellules++
return true;
}
}
}
// <textApostrophe> = 1*(VCHAR WSP VCHAR/VCHAR/VCHAR.'''.VCHAR)
CSVToICalendar.prototype.isTextApostrophe = function (input) {
if (matched = input.match(/((?:[A-Z])+(?:\s[A-Z])*|(?:\'[A-Z])*|(?:[A-Z])*)*/i)) {
//console.log(matched[0]);
if (matched[0].length == input.length)
return matched[0];
else
this.err("Invalid input next to " + matched[0], input);
} else {
this.err("Invalid input" + input, input);
}
}
// <text> = 1*(VCHAR WSP VCHAR/VCHAR)
CSVToICalendar.prototype.isText = function (input) {
if (matched = input.match(/((?:[A-Z])+(?:\s[A-Z])*|(?:[A-Z])*)*/i)) {
//console.log(matched[0]);
if (matched[0].length == input.length)
return matched[0];
else
this.err("Invalid input next to " + matched[0], input);
} else {
this.err("Invalid input" + input, input);
}
}
// <tel> = 10DIGIT
CSVToICalendar.prototype.isTel = function (input) {
if (matched = input.match(/(0[0-9]{9})/)) {
//console.log(matched[0]);
if (matched[0].length == input.length)
return matched[0];
else
this.err("Invalid input next to " + matched[0], input);
} else {
this.err("Invalid input" + input, input);
}
}
// <society> = 1*(VCHAR WSP VCHAR/VCHAR/VCHAR.'''.VCHAR/DIGIT/DIGIT WSP VCHAR/VCHAR WSP DIGIT)
CSVToICalendar.prototype.isSociety = function (input) {
if (matched = input.match(/((?:[A-Z]|[0-9])+(?:\s[A-Z])*|(?:\'[A-Z])*|(?:\s[0-9])*|(?:[A-Z]|[0-9])*)*/i)) {
//console.log(matched[0]);
if (matched[0].length == input.length)
return matched[0];
else
this.err("Invalid input next to " + matched[0], input);
} else {
this.err("Invalid input" + input, input);
}
}
// <street> = 1*DIGIT WSP 1*(VCHAR/VCHAR WSP VCHAR/VCHAR.'''.VCHAR)
CSVToICalendar.prototype.isStreet = function (input) {
if (matched = input.match(/((?:[0-9])+(?:(?:\s[A-Z])+(?:\s[A-Z])*|(?:\'[A-Z])*|(?:[A-Z])*)*)/i)) {
//console.log(matched[0]);
if (matched[0].length === input.length)
return matched[0];
else
this.err("Invalid input next to " + matched[0], input);
} else {
this.err("Invalid input" + input, input);
}
};
// <postalCode> = 5DIGIT
CSVToICalendar.prototype.isPostalCode = function (input) {
if (matched = input.match(/([0-9]{5})/i)) {
//console.log(matched[0]);
if (matched[0].length === input.length)
return matched[0];
else
this.err("Invalid input next to " + matched[0], input);
} else {
this.err("Invalid input" + input, input);
}
};
exports.launchCSVToICalendar = launchCSVToICalendar;