-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyjsonixschema.js
134 lines (119 loc) · 3.85 KB
/
copyjsonixschema.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
var copydir = require('copy-dir');
var fs = require('fs');
copydir.sync('./node_modules/jsonix/jsonschemas', './resources/app/standards/jsonschemas');
//extraction of schema to .json file
//var GHSTSJsonSchema = JSON.parse(fs.readFileSync('./resources/app/standards/01_00_00/GHSTS.jsonschema').toString());
var GHSTS = require('./resources/app/standards/01_04_00/GHSTS').GHSTS;
var count = 0;
var defName_arr = [];
var transf2vm = function (doc) {
let ret = { vrole: undefined, fields: undefined };
let fields = {}, vrole = {};
// console.log(doc['propertyInfos']);
doc.propertyInfos.map(prop => {
let roles = { empty: true };
if (prop.collection)
fields[prop.name] = [];
else if (prop['typeInfo'] && !prop['type']) {
let lTypeInfo = prop['typeInfo'];
lTypeInfo = lTypeInfo.slice(lTypeInfo.lastIndexOf('.') + 1).toLowerCase();
if (!lTypeInfo.startsWith('type') &&
!lTypeInfo.endsWith('struct') &&
!lTypeInfo.endsWith('type') &&
lTypeInfo !== 'country' &&
lTypeInfo !== 'unit' &&
lTypeInfo !== 'dataprotection' &&
lTypeInfo !== 'datarequirement')
if (lTypeInfo === 'decimal' || lTypeInfo === 'positiveinteger')
fields[prop.name] = '';
else if (lTypeInfo === 'boolean')
fields[prop.name] = false;
else if (lTypeInfo === 'date')
fields[prop.name] = 'Date';
else
fields[prop.name] = { ref: prop['typeInfo'].slice(prop['typeInfo'].lastIndexOf('.') + 1).toLowerCase() };
else
fields[prop.name] = ''; //Picklist Items
}
else
fields[prop.name] = '';
if (!fields.hasOwnProperty('has'))
fields.has = true;
if (prop.required) {
roles.required = true;
delete roles.empty;
}
if (prop.minOccurs > 0) {
roles.min = prop.minOccurs;
delete roles.empty;
}
if (prop.maxOccurs > 0) {
roles.max = prop.maxOccurs;
delete roles.empty;
}
if (!roles.hasOwnProperty('empty')) {
vrole[prop.name] = roles;
vrole.has = true;
}
});
if (fields.has) {
delete fields.has;
ret.fields = fields;
}
if (vrole.has) {
delete vrole.has;
ret.vrole = vrole;
}
return JSON.stringify(ret, undefined, '\t');
};
// We reuse GHSTSMappings variable declared in file GHSTSMappings.js which are generated by jsonix
// to count number of GHSTS definitions
GHSTS.typeInfos.forEach(ghstsDefn => {
defName_arr.push(ghstsDefn.localName);
});
GHSTS.typeInfos.forEach(ghstsDefn => {
//defName_arr.push(ghstsDefn.localName);
//console.log(count + ": " + JSON.stringify(ghstsDefn));
count++;
var defLocalName = ghstsDefn.localName;
var jsondef = JSON.stringify(ghstsDefn, undefined, '\t');
var curPath = './resources/app/standards/01_04_00/jsondefinitions';
try {
fs.mkdirSync(curPath);
} catch (err) {
if (err.code !== 'EEXIST')
throw err;
}
curPath = curPath + '/' + defLocalName + '.json';
var vmfilename = defLocalName.slice(defLocalName.lastIndexOf('.') + 1).toLowerCase();
var vmPath;
if (vmfilename.startsWith('type') ||
vmfilename.startsWith('extension') ||
vmfilename.endsWith('type') ||
vmfilename === 'country' ||
vmfilename === 'unit') {
vmPath = undefined;
} else {
vmPath = './src/renderer/view-models/gen/' + vmfilename + '.json';
}
try {
fs.writeFileSync(curPath, jsondef);
console.log(curPath + ' was saved!');
if (vmPath) {
try {
fs.mkdirSync('./src/renderer/view-models/gen');
} catch (err) {
if (err.code !== 'EEXIST')
throw err;
}
try {
fs.writeFileSync(vmPath, transf2vm(ghstsDefn));
console.log(vmPath + ' was saved!');
} catch (err) {
if (err) console.log(vmPath + ' failed as ' + err);
}
}
} catch (err) {
if (err) console.log(curPath + ' failed as ' + err);
}
});