-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsystem.class.js
1911 lines (1818 loc) · 49.1 KB
/
system.class.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* __ __ _
* | \/ | | |
* | \ / | _ _ ___ ___ | | ___
* | |\/| || | | |/ __| / __|| | / - \
* | | | || |_| |\__ \( (__ | |( __/
* |_| |_| \__,_||___/ \___||_| \___|
*
* Mock-up of the System class from VCF Automation.
* This is a general set of functions and it is always available in the
* JavaScript scripting environment.
*
* @author Stefan Schnell <[email protected]>
* @license MIT
* @version 0.3.3
*
* Hint: This mock-up works only with the Mozilla Rhino JavaScript
* engine.
*
* Checked with Rhino engines version 1.7R4, 1.7.14, 1.7.15 and 1.8.0
*/
/**
* URL class,
* mock-up of VCF Automation URL class, only attributes.
*/
function URL() {
// this.contentType = "application/x-www-form-urlencoded";
this.contentType = "text/plain";
this.datas = null;
this.host = "localhost";
this.port = 443;
this.requestType = "GET";
this.result = "This is a mock-up of URL";
this.url = "https://localhost/";
}
/**
* Workflow class,
* mock-up of VCF Automation Workflow class, only attributes.
*/
function Workflow() {
this.attributes = {};
this.description = "This is a mock-up";
this.executions = {};
this.firstItem = {};
this.id = "";
this.inParameters = {};
this.items = {};
this.logEvents = {};
this.name = "Mock-up of Workflow class";
this.numberOfItems = 0;
this.outParameters = {};
this.parameterInfos = {};
this.version = "1.0.0.0";
this.versionHistoryItems = {};
this.workflowCategory = {};
}
/**
* WorkflowItemInfo class,
* mock-up of VCF Automation WorkflowItemInfo class.
*/
function WorkflowItemInfo(name, displayName) {
this._displayName = displayName;
this._name = name;
this.getDisplayName = function() {
return this._displayName;
};
this.getName = function() {
return this._name;
};
}
// System class
var _context = new org.mozilla.javascript.Context();
var _logMarker = null;
var _SystemNS = function() {
};
_SystemNS.prototype = {
/**
* Private method to log text for debug, error, log and warn methods.
*
* @function _println
* @param {string} prefix - Log level.
* @param {string} text - Text to log.
*/
_println : function(prefix, text) {
var outText = null;
if (typeof text === "undefined" || text === null) {
outText = "";
} else {
var rhinoVersion = this.getRhinoVersion();
if (this.compareVersionNumber(rhinoVersion, "1.7.5") === 1) {
// trimRight added to release 1.7.6 of Rhino
outText = String(text).trimRight();
} else {
outText = String(text).replace(/[\s]+$/g, "");
}
}
if (_logMarker) {
java.lang.System.out.println(
prefix + " {{" + _logMarker + "}} " + outText
);
} else {
java.lang.System.out.println(
prefix + " " + outText
);
}
},
/**
* Displays a dialog with an optional message and waits until the
* user dismisses the dialog.
*
* @function alert
* @param {string} message - A string to display in the alert dialog
*
* @example
* System.alert("Hello World");
*/
alert : function(message) {
if (
typeof message === "undefined" ||
message === null ||
String(message).trim() === ""
) {
javax.swing.JOptionPane.showMessageDialog(
null,
null,
"Alert",
javax.swing.JOptionPane.WARNING_MESSAGE
);
} else {
javax.swing.JOptionPane.showMessageDialog(
null,
String(message),
"Alert",
javax.swing.JOptionPane.WARNING_MESSAGE
);
}
},
/**
* Appends a path fragment to another.
*
* @function appendToPath
* @param {string} root - Path to add to.
* @param {string} toAdd - Part to add.
* @returns {string}
*
* @example
* // Delivers on Windows \sys\vco
* var result = System.appendToPath("/sys/", "vco");
* System.log(result);
*/
appendToPath : function(root, toAdd) {
if (
typeof root === "undefined" ||
root === null ||
String(root).trim() === ""
) {
throw new Error("root argument can not be undefined or null");
}
if (
typeof toAdd === "undefined" ||
toAdd === null ||
String(toAdd).trim() === ""
) {
throw new Error("toAdd argument can not be undefined or null");
}
return String(
java.nio.file.Paths.get(root.toString(), toAdd.toString())
);
},
/**
* Decodes a base64 encoded string.<br>
* Hint: This method is not available in the standard.
*
* @function base64ToString
* @param {string} value - Text to decode from base64.
* @returns {string}
*/
base64ToString : function(value) {
if (typeof value === "undefined" || value === null) {
throw new Error("value argument can not be undefined or null");
}
return String(java.lang.String(
java.util.Base64.getDecoder().decode(
java.lang.String(value).getBytes()
)
));
},
/**
* Clones an object.<br>
* Hint: This method is not available in the standard.
*
* @param {object} object - Object to clone.
* @returns {object}
*/
clone : function(object) {
if (typeof object === "undefined" || object === null) {
throw new Error("object argument can not be undefined or null");
}
if (typeof object === "object") {
function func() {}
func.prototype = object;
return new func();
}
},
/**
* Compares two version numbers.<br>
* Hint: Arguments should be string and if a one-digit number is
* compared with a multi-digit number, leading zeros must be passed.
*
* @function compareVersionNumber
* @param {string} version1 - First version.
* @param {string} version2 - Second version.
* @returns {number}
* Returns 0 if they are equivalent<br>
* Returns 1 version1 is greater than version2<br>
* Returns -1 version2 a is greater than version1
*
* @example
* // Delivers -1
* var result = System.compareVersionNumber("1.0.0", "2.0.0");
* System.log(result);
*/
compareVersionNumber : function(version1, version2) {
if (
typeof version1 === "undefined" ||
version1 === null ||
String(version1).trim() === ""
) {
throw new Error("version1 argument can not be undefined or null");
}
if (
typeof version2 === "undefined" ||
version2 === null ||
String(version2).trim() === ""
) {
throw new Error("version2 argument can not be undefined or null");
}
return version1.toString().localeCompare(version2.toString(),
undefined, { numeric: true, sensitivity: "base" });
},
/**
* Creates an empty file in the temporary directory, using the given
* suffix to generate its name.
*
* @function createTempFile
* @param {string} suffix - The suffix string to be used in generating
* the file's name.
* @returns {object.<java.io.File>}
*
* @example
* // Delivers on Windows
* // C:\Users\[name]\AppData\Local\Temp\vco-[number].tmp
* var result = System.createTempFile(".tmp");
* System.log(result);
*/
createTempFile : function(suffix) {
if (
typeof suffix === "undefined" ||
String(suffix).trim() === ""
) {
suffix = null;
}
return java.io.File.createTempFile("vco-", suffix);
},
/**
* Information for currently executed workflow item.
*
* @function currentWorkflowItem
* @returns {object.<WorkflowItemInfo>}
*
* @example
* var result = System.currentWorkflowItem();
*/
currentWorkflowItem : function() {
return new WorkflowItemInfo("System Class Mock-up",
"Mock-up of System Class");
},
/**
* Returns URL to generate HTTP custom event.
*
* @function customEventUrl
* @param {string} eventName - Custom event name.
* @param {boolean} secure, default false - https if true, http otherwise.
* @returns {object.<URL>}
*
* @example
* var result = System.customEventUrl("click", false);
*/
customEventUrl : function(eventName, secure) {
if (
typeof eventName === "undefined" ||
eventName === null ||
String(eventName).trim() === ""
) {
throw new Error("eventName argument can not be undefined or null");
}
var url = new URL(); // Mock-up URL object
url.port = 8280;
url.contentType = "application/x-www-form-urlencoded";
return url;
},
/**
* Returns URL to generate HTTP custom event.
*
* @function customEventUrlforServer
* @param {string} eventName - Custom event name.
* @param {string} host - Host to use in URL.
* @param {string} port - Port to use in URL.
* @param {boolean} secure - Use https if true, http otherwise.
* @returns {object.<URL>}
*
* @example
* var result =
* System.customEventUrlforServer("click", "localhost", "8080", false);
*/
customEventUrlforServer : function(eventName, host, port, secure) {
if (
typeof eventName === "undefined" ||
eventName === null ||
String(eventName).trim() === ""
) {
throw new Error("eventName argument can not be undefined or null");
}
if (
typeof host === "undefined" ||
host === null ||
String(host).trim() === ""
) {
throw new Error("host argument can not be undefined or null");
}
if (
typeof port === "undefined" ||
port === null ||
String(port).trim() === ""
) {
throw new Error("port argument can not be undefined or null");
}
var url = new URL(); // Mock-up URL object
url.host = host.toString();
url.port = port.toString();
url.contentType = "application/x-www-form-urlencoded";
return url;
},
/**
* Logs a text in the system with DEBUG.
*
* @function debug
* @param {string} text - Text to log.
*
* @example
* System.debug("This is a test");
*/
debug : function(text) {
this._println("\u001B[32mDEBUG\u001B[0m", text);
},
/**
* Converts decimal number to hexadecimal string.
*
* @function decimalToHex
* @param {number} value - Decimal value.
* @returns {string}
*
* @example
* // Delivers 1267
* var result = System.decimalToHex(4711);
* System.log(result);
*/
decimalToHex : function(value) {
if (typeof value !== "undefined" && value !== null) {
var newValue = parseFloat(value.toString());
if (isNaN(newValue)) {
return "";
}
return newValue.toString(16).toUpperCase();
} else {
return "";
}
},
/**
* Logs a text in the system with ERROR.
*
* @function error
* @param {string} text - Text to log.
*
* @example
* System.error("This is a test");
*/
error : function(text) {
this._println("\u001B[31mERROR\u001B[0m", text);
},
/**
* Evaluates a JavaScript source string.<br>
* Hint: This method is not documented in the standard.
*
* @function exec
* @param {string} command
* @returns {object.<any>}
*
* @example
* // Delivers Hello World as object of type java.lang.String
* var result =
* System.exec("function test() { return \"Hello World\"; } test();");
* System.log(result);
*
* @example
* // Delivers "Hello World" as string
* var result = JSON.stringify(
* System.exec("function test() { return \"Hello World\"; } test();")
* );
* System.log(result);
*/
exec : function(command) {
if (
typeof command === "undefined" ||
command === null ||
String(command).trim() === ""
) {
throw new Error("command argument can not be undefined or null");
}
var result = null;
try {
var cx = _context.enter();
var scope = cx.initStandardObjects();
result = cx.evaluateString(scope, command.toString(),
"SystemMockupScript", 1, null);
} catch (exception) {
result = exception;
} finally {
_context.exit();
}
return result;
},
/**
* Extracts the directory part from the file path.
*
* @function extractDirectory
* @param {string} fullPath - Path from which something is to be extracted.
* @returns {string}
*
* @example
* // Delivers C:/temp/
* var result = System.extractDirectory("C:\\temp\\myfile.txt");
* System.log(result);
*
* @example
* // Delivers /home/temp/
* var result = System.extractDirectory("/home/temp/myfile.txt");
* System.log(result);
*/
extractDirectory : function(fullPath) {
if (
typeof fullPath === "undefined" ||
fullPath === null ||
String(fullPath).trim() === ""
) {
throw new Error("fullPath argument can not be undefined or null");
}
var separatorFullPath = fullPath.toString().replace(/\\/g, "/");
return separatorFullPath.substring(
0,
separatorFullPath.lastIndexOf("/") + 1
);
},
/**
* Extracts the file name part from the file path.
*
* @function extractFileName
* @param {string} fullPath - Path from which something is to be extracted.
* @returns {string}
*
* @example
* // Delivers myfile.txt
* var result = System.extractFileName("C:/temp/myfile.txt");
* System.log(result);
*/
extractFileName : function(fullPath) {
if (
typeof fullPath === "undefined" ||
fullPath === null ||
String(fullPath).trim() === ""
) {
throw new Error("fullPath argument can not be undefined or null");
}
var separatorFullPath = fullPath.toString().replace(/\\/g, "/");
return separatorFullPath.substring(
separatorFullPath.lastIndexOf("/") + 1
);
},
/**
* Extracts the extension part from the file path.
*
* @function extractFileNameExtension
* @param {string} fullPath - Path from which something is to be extracted.
* @returns {string}
*
* @example
* // Delivers txt
* var result = System.extractFileNameExtension("C:/temp/myfile.txt");
* System.log(result);
*/
extractFileNameExtension : function(fullPath) {
if (
typeof fullPath === "undefined" ||
fullPath === null ||
String(fullPath).trim() === ""
) {
throw new Error("fullPath argument can not be undefined or null");
}
var fileName = this.extractFileName(fullPath);
if (fileName.indexOf(".") !== -1) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
} else {
return "";
}
},
/**
* Extracts the file name part without extension from the file path.
*
* @function extractFileNameWithoutExtension
* @param {string} fullPath - Path from which something is to be extracted.
* @returns {string}
*
* @example
* // Delivers C:/temp/myfile
* var result = System.extractFileNameWithoutExtension("C:/temp/myfile.txt");
* System.log(result);
*/
extractFileNameWithoutExtension : function(fullPath) {
if (
typeof fullPath === "undefined" ||
fullPath === null ||
String(fullPath).trim() === ""
) {
throw new Error("fullPath argument can not be undefined or null");
}
var fileName = this.extractFileName(fullPath.toString());
if (fileName.indexOf(".") !== -1) {
fileName = fileName.substring(0, fileName.lastIndexOf("."));
}
return this.extractDirectory(fullPath) + fileName;
},
/**
* Filters non authorized objects for the current user.<br>
* Returns null if the user is not authorized and the input object
* if the authorization is correct.<br>
* Hint: The argument authorized is an additional flag, to mock the
* desired result.
*
* @function filterAuthorized
* @param {object} source - Object to check.
* @param {boolean} authorized - Flag to mock result.
* @returns {object}
*
* @example
* var obj = {"id": "Mock-up object"};
* var result = System.filterAuthorized(obj);
* System.log(JSON.stringify(result));
*
* @example
* var obj = {"id": "Mock-up object"};
* var result = System.filterAuthorized(obj, false);
* if (result === null) {
* System.log("null");
* }
*/
filterAuthorized : function(source, authorized) {
if (typeof source === "undefined" || source === null) {
throw new Error("source argument can not be undefined or null");
}
if (
typeof authorized === "undefined" ||
authorized === null ||
authorized === true
) {
return source;
} else {
return null;
}
},
/**
* Formats a number in a readable size format.
*
* @function formatBinaryValue
* @param {number} value - Number to format.
* @returns {string}
*
* @example
* // Delivers 2K
* var result = System.formatBinaryValue(2048);
* System.log(result);
*
* @example
* // Delivers 4.6005859375K
* var result = System.formatBinaryValue(4711);
* System.log(result);
*
* @example
* // Delivers 1M
* var result = System.formatBinaryValue(1048576);
* System.log(result);
*
* @example
* // Delivers 1G
* var result = System.formatBinaryValue(1073741824);
* System.log(result);
*
* @example
* // Delivers 1T
* var result = System.formatBinaryValue(1099511627776);
* System.log(result);
*/
formatBinaryValue : function(value) {
if (typeof value === "undefined" || value === null) {
return null;
}
var newValue = parseFloat(value.toString());
if (isNaN(newValue)) {
return null;
}
if (newValue < 1024) {
return newValue.toString();
} else if (newValue < 1048576) {
return (newValue / 1024 * 100) / 100 + "K";
} else if (newValue < 1073741824) {
return (newValue / 1048576 * 100) / 100 + "M";
} else if (newValue < 1099511627776) {
return (newValue / 1073741824 * 100) / 100 + "G";
} else {
return (newValue / 1099511627776 * 100) / 100 + "T";
}
},
/**
* Formats a date in the given format.
*
* @function formatDate
* @param {Date} date - Date to format.
* @param {string} pattern - Format pattern.
* @returns {string}
*
* @example
* // Delivers 2023.04.04 n. Chr. at 05:37:19 MESZ
* var result =
* System.formatDate(new Date(), "yyyy.MM.dd G 'at' HH:mm:ss z");
* System.log(result);
*
* @example
* // Delivers 04.07.2023, 05:33:59
* var result = System.formatDate(new Date());
* System.log(result);
*/
formatDate : function(date, pattern) {
if (
!(date instanceof Date) ||
isNaN(date) ||
typeof date === "undefined" ||
date === null
) {
throw new Error(
"date argument must be Date and can not be undefined or null"
);
}
try {
var format = java.text.DateFormat.getDateTimeInstance();
if (pattern != null) {
format = new java.text.SimpleDateFormat(pattern);
}
return String(format.format(date));
} catch (exception) {
throw new Error("Unable to format date");
}
},
/**
* Formats the milliseconds in a readable format.
*
* @function formatDuration
* @param {number} milliseconds - Millisecond number to format.
* @param {boolean} showMilli - Show millisecond if true, default false.
* @param {boolean} showZero - Show leading zero values if true,
* default false.
* @returns {string}
* @see getCurrentTime
*
* @example
* // Delivers 19452d 02h 44m 11.986s
* var result = System.formatDuration(System.getCurrentTime(), true, true)
* System.log(result);
*
* @example
* // Delivers Wed Apr 05 2023 04:44:11 GMT+0200 (MESZ)
* var result = new Date();
* System.log(result);
*
* @example
* // Delivers 19452d 2h 44m 11s
* var result = System.formatDuration(System.getCurrentTime(), false, false);
* System.log(result);
*/
formatDuration : function(milliseconds, showMilli, showZero) {
var showMilliseconds = false;
var showLeadingZeros = false;
var retValue = "";
if (
typeof milliseconds === "undefined" ||
milliseconds === null ||
typeof milliseconds !== "number"
) {
throw new Error(
"milliseconds argument must be number and can not be undefined or null"
);
}
if (typeof showMilli === "boolean") {
showMilliseconds = showMilli;
}
if (typeof showZero === "boolean") {
showLeadingZeros = showZero;
}
if (milliseconds < 0) {
return "N/A";
}
// Milliseconds
if (showMilliseconds) {
var milli = String(parseInt(milliseconds % 1000));
while (milli.length < 3) {
milli = "0" + milli;
}
retValue = "." + milli + "s";
} else {
retValue = "s";
}
// Seconds
var time = milliseconds / 1000;
var seconds = String(parseInt(time % 60));
if (showZero && seconds < 10) {
seconds = "0" + seconds;
}
retValue = seconds + retValue;
// Subtract seconds from time
time -= parseInt(time % 60);
time /= 60;
// Minutes
var minutes = String(parseInt(time % 60));
if (showZero && minutes < 10) {
minutes = "0" + minutes;
}
retValue = minutes + "m " + retValue;
// Subtract minutes from time
time -= parseInt(time % 60);
time /= 60;
// Hours
var hours = String(parseInt(time % 24));
if (showZero && hours < 10) {
hours = "0" + hours;
}
retValue = hours + "h " + retValue;
// Subtract hours from time
time -= parseInt(time % 24);
time /= 24;
// Days
var days = String(parseInt(time));
retValue = days + "d " + retValue;
return retValue;
},
/**
* Formats a number in the given format.<br>
* Creates a DecimalFormat using the given pattern and the symbols
* for the default locale.
*
* @function formatNumber
* @param {number} aNumber - Number to format.
* @param {string} pattern - Format pattern.
* @returns {string}
*
* @example
* var myNumber = -1234.56;
* // Delivers -01234,560
* var result = System.formatNumber(myNumber, "00000.000")
* System.log(result);
* // Delivers -1234,6
* var result = System.formatNumber(myNumber, "#.#");
* System.log(result);
* // Delivers -1.234,6
* var result = System.formatNumber(myNumber, "#,###.#");
* System.log(result);
*/
formatNumber : function(aNumber, pattern) {
if (typeof aNumber === "undefined" || aNumber === null) {
throw new Error(
"aNumber argument must be Number and can not be undefined or null"
);
}
var format = new java.text.DecimalFormat();
if (typeof pattern !== "undefined" || pattern !== null) {
try {
format = new java.text.DecimalFormat(pattern);
} catch (exception) {
throw new Error("Unable to create a formatter from pattern");
}
}
try {
return String(format.format(parseFloat(aNumber)));
} catch (exception) {
throw new Error("Unable to format aNumber with the pattern");
}
},
/**
* Returns all action modules.
*
* @function getAllModules
* @returns {object[]}
*
* @example
* var result = System.getAllModules();
*/
getAllModules : function() {
var Module = [
{
name: "com.vmware.basic",
description: "",
id: "8a7480b5796abea101796ac0e5630169",
type: "ScriptModuleCategory"
},
{
name: "com.mock-up",
description: "",
id: "1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d",
type: "ScriptModuleCategory"
}
];
return Module;
},
/**
* Returns the class name.<br>
* Hint: This method is a standard.
*
* @function getClassName
* @returns {string}
*
* @example
* var result = System.getClassName();
* System.log(result);
*/
getClassName : function() {
return "System";
},
/**
* Gets the context for the current workflow run.<br>
* Hint: This method delivers not the ExecutionContext like the
* original, it delivers the object org.mozilla.javascript.Context.
*
* @function getContext
* @returns {object}
*
* @example
* var cx = System.getContext();
*/
getContext : function() {
return _context;
},
/**
* Gets the current time.<br>
* Delivers the number of milliseconds elapsed since midnight at the
* beginning of January 1, 1970, UTC.
*
* @function getCurrentTime
* @returns {number}
*
* @example
* // Delivers e.g. 1688273422309
* var result = System.getCurrentTime();
* System.log(result);
*/
getCurrentTime : function() {
return parseInt(Date.now());
},
/**
* Creates a new date from the input.<br>
* Hint: The reference date is not considered.
*
* @function getDate
* @param {string} input - Input date.
* @param {Date} refDate - Reference date, now if not set.
* @returns {object}
*
* @example
* // Delivers Tue Jan 01 2019 01:00:00 GMT+0100 (MEZ)
* var result = System.getDate("2019-01-01");
* System.log(result);
*
* @example
* // Delivers Tue Jun 16 1964 00:00:00 GMT+0200 (MESZ)
* var result = System.getDate("Jun 16, 1964");
* System.log(result);
*
* @example
* // Delivers Sat Dec 24 2022 12:12:00 GMT+0100 (MEZ)
* var result = System.getDate("2022/12/24 12:12:00");
* System.log(result);
*/
getDate : function(input, refDate) {
if (
typeof input === "undefined" ||
input === null ||
String(input).trim() === ""
) {
throw new Error("input argument can not be undefined or null");
}
var referenceDate = Date.now();
if (
typeof refDate === "object" &&
refDate !== null &&
Object.prototype.toString.call(refDate) === "[object Date]"
) {
referenceDate = refDate;
}
var retDate = new Date(Date.parse(input.toString()));
return retDate;
},
/**
* Parses the given date as string to creates a date.<br>
* Hint: The date to be parsed must have the same format as the
* format pattern.
*
* @function getDateFromFormat
* @param {string} date - String to parse.
* @param {string} pattern - Format pattern, default yyyy-MM-dd HH:mm:ss.
* @returns {object.<java.util.Date>}
*
* @example
* // Delivers Tue Jun 16 15:54:00 CET 1964
* var result = System.getDateFromFormat("1964-6-16 15:54:00");
* System.log(result);
*
* @example
* // Delivers Thu Jan 01 00:00:00 CET 1970
* var result = System.getDateFromFormat("1970-1-1", "yyyy-MM-dd");
* System.log(result);
*/
getDateFromFormat : function(date, pattern) {
if (
typeof date === "undefined" ||
date === null ||
String(date).trim() === ""
) {
throw new Error("date argument can not be undefined or null");
}
var formatPattern = "yyyy-MM-dd HH:mm:ss";
if (typeof pattern !== "undefined" && pattern !== null) {
formatPattern = pattern.toString();
}
var format;
try {