-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmdjson.js
247 lines (197 loc) · 5.94 KB
/
mdjson.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
import Service, {
inject as service
} from '@ember/service';
import {
isArray
} from '@ember/array';
import EmberObject, {
getWithDefault,
get,
set
} from '@ember/object';
import Ember from 'ember';
import Ajv from 'ajv';
import Schemas from 'mdjson-schemas/resources/js/schemas';
import {
formatCitation
} from 'mdeditor/pods/components/object/md-citation/component';
import * as draft4 from 'ajv/lib/refs/json-schema-draft-04';
Ember.libraries.register('mdJson-schemas', Schemas.schema.version);
const validator = new Ajv({
verbose: true,
allErrors: true,
jsonPointers: true,
removeAdditional: false,
meta: false,
schemaId: 'id'
});
//support draft-04
validator.addMetaSchema(draft4);
Object.keys(Schemas)
.forEach(function (key) {
if(key === 'default') {
return;
}
let val = Schemas[key];
validator.addSchema(val, key);
});
const unImplemented = [
'metadata.metadataInfo.otherMetadataLocale',
'metadata.resourceInfo.spatialRepresentation', [
'metadata.resourceInfo.extent',
'verticalExtent'
],
['metadata.resourceInfo.extent',
'temporalExtent'
],
'metadata.resourceInfo.coverageDescription',
//'metadata.resourceInfo.taxonomy',
'metadata.resourceInfo.otherResourceLocale',
//'metadata.resourceInfo.resourceMaintenance'
];
export default Service.extend({
cleaner: service(),
contacts: service(),
store: service(),
injectCitations(json) {
let assoc = json.metadata.associatedResource;
if(assoc) {
let refs = assoc.reduce((acc, itm) => {
if(itm.mdRecordId) {
acc.push(itm);
}
return acc;
}, []);
let records = this.store.peekAll('record').filterBy('recordId');
refs.forEach((ref) => {
let record = records.findBy('recordId', ref.mdRecordId);
if(record) {
let info = get(record, 'json.metadata.metadataInfo') || {};
let metadata = {
'title': `Metadata for ${get(record,'title')}`,
'responsibleParty': getWithDefault(info,
'metadataContact', []),
'date': getWithDefault(info, 'metadataDate', []),
'onlineResource': getWithDefault(info,
'metadataOnlineResource', []),
'identifier': [getWithDefault(info, 'metadataIdentifier', {})],
};
let citation = get(record,
'json.metadata.resourceInfo.citation') || {};
let resourceType = get(record,
'json.metadata.resourceInfo.resourceType') || [];
set(ref, 'resourceCitation', EmberObject.create(
formatCitation(
citation)));
set(ref, 'metadataCitation', EmberObject.create(
formatCitation(
metadata)));
set(ref, 'resourceType', resourceType);
set(ref, 'mdRecordId', null);
return;
}
set(ref, 'mdRecordId', null);
});
}
},
injectDictionaries(rec, json) {
let ids = rec.get('json.mdDictionary') || [];
let arr = [];
if(ids.length) {
let dicts = this.store.peekAll('dictionary').filterBy(
'dictionaryId');
ids.forEach((id) => {
let record = dicts.findBy('dictionaryId', id);
if(record) {
arr.pushObject(record.get('json.dataDictionary'));
}
});
}
set(json, 'dataDictionary', arr);
},
formatRecord(rec, asText) {
let _contacts = [];
let conts = this.contacts;
const _replacer = function (key, value) {
let check = {
contactId: true,
sourceId: true,
recipientId: true
};
if(key === 'sourceId' && !('amount' in this || 'currency' in this)) {
//console.log(this);
return value;
}
if(check[key] && !_contacts.includes(value)) {
let contact = conts.get('contacts').findBy('contactId', value);
if(!contact) {
return null;
}
let orgs = isArray(contact.get('json.memberOfOrganization')) ?
contact.get('json.memberOfOrganization').slice(0) : null;
_contacts.push(value);
if(orgs && orgs.length) {
orgs.forEach(itm => {
let org = conts.get('contacts').findBy('contactId', itm);
if(!org) {
return;
}
if(!_contacts.includes(itm) && org) {
_contacts.push(itm);
}
let iOrgs = org.get('json.memberOfOrganization');
if(iOrgs.length) {
iOrgs.forEach(iOrg => {
if(!_contacts.includes(iOrg)) {
orgs.push(iOrg);
}
});
}
});
}
}
return value;
};
let cleaner = this.cleaner;
let clean = cleaner.clean(get(rec, 'json'));
this.injectCitations(clean);
this.injectDictionaries(rec, clean);
let json = JSON.parse(JSON.stringify(cleaner.clean(clean), _replacer));
let contacts = this.store
.peekAll('contact')
.mapBy('json');
json.contact = contacts.filter((item) => {
return _contacts.includes(get(item, 'contactId'));
});
if(unImplemented) {
unImplemented.forEach((path) => {
let array = isArray(path);
let target = array ? get(json, path[0]) : get(json, path);
if(target) {
if(array) {
target.forEach((item) => {
set(item, path[1], undefined);
});
return;
}
set(json, path, undefined);
}
});
}
return asText ? JSON.stringify(cleaner.clean(json)) : cleaner.clean(
json);
},
validateRecord(record) {
validator.validate('schema', this.formatRecord(record));
return validator;
},
validateContact(contact) {
validator.validate('contact', contact.get('cleanJson'));
return validator;
},
validateDictionary(dictionary) {
validator.validate('dataDictionary', dictionary.get('cleanJson').dataDictionary);
console.log(validator)
return validator;
}
});