-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.js
234 lines (210 loc) · 6.56 KB
/
model.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
// Dataspecer data specification endpoint
const API_ENDPOINT = "https://demo-ds-backend.penduline.mercadia.cz/data-specification";
// MM-evocat schema categories endpoint
const MM_API_ENDPOINT = "https://demo.mmcatdb.com/api/v1/public/schema-categories";
// Change this to use multiple independent instances under the same DS backend
// Used as a key to store MM project ID in DS
const DATASPECER_MM_METADATA = "https://demo.mmcatdb.com/mmcat/dataspecer-self-metadata";
// Default tag for new specifications and for filtering of projects
const DEFAULT_SPECIFICATION_TAG = "Atlas demo";
const CREATE_SPECIFICATION_PAYLOAD = {
"type": "http://dataspecer.com/vocabularies/data-specification/documentation",
"tags": []
};
export function getCurrentTagFilter() {
let filter = localStorage.getItem("tag-filter") ?? DEFAULT_SPECIFICATION_TAG;
if (filter == "") {
filter = null;
}
return filter;
}
export function setCurrentTagFilter(tag) {
if (tag !== DEFAULT_SPECIFICATION_TAG) {
localStorage.setItem("tag-filter", tag);
} else {
localStorage.removeItem("tag-filter");
}
}
export function initTagFilter() {
const tagFilter = document.getElementById("tag-filter");
tagFilter.addEventListener("input", () => {
setCurrentTagFilter(tagFilter.value);
});
tagFilter.value = getCurrentTagFilter();
}
export async function fetchProjects(filter = (entry) => true) {
const response = await fetchAsJson(API_ENDPOINT);
const result = [];
const requests = response.map(async item => {
if (!filter(item)) {
return;
}
const store = selectWritablePimStore(item);
if (store == null) {
console.warning("Missing store.", item);
return;
}
const storeData = await fetchAsJson(store.url);
const schemaResource = storeData?.resources?.[item.pim];
// We have the right resource.
result.push({
...item,
'iri': item.iri,
'mmId': item.artefactConfiguration?.[DATASPECER_MM_METADATA]?.projectId ?? null,
"tags": item.tags,
"label": schemaResource?.pimHumanLabel,
'pimStore': store.url
});
});
await Promise.all(requests);
return result;
}
async function fetchAsJson(url) {
return await (await fetch(url)).json();
}
function selectWritablePimStore(entry) {
for (const storeEntry of entry.pimStores) {
// We can ignore read only stores.
if (storeEntry.isReadOnly) {
continue;
}
return storeEntry;
}
console.error("Missing writable store for project.", entry);
return null;
}
export function selectLangString(value) {
return nonEmpty(value?.["en"]) ?? nonEmpty(value?.["cs"]) ?? "Project without name";
}
function nonEmpty(value) {
if (value === undefined || value === null || value === "") {
return undefined;
} else {
return value;
}
}
export async function createStandardSpecification(label, tag) {
const payload = {...CREATE_SPECIFICATION_PAYLOAD, tags: tag ? [tag] : []};
let project =
await fetchPostJson(API_ENDPOINT, payload);
const store = selectWritablePimStore(project);
if (store == null) {
throw new Error("Can't create project.");
}
await initializeProject(store, project.pim, label);
return project;
}
async function fetchPostJson(url, content, method = "POST") {
return await (await fetch(url, {
"method": method,
"headers": {
"Content-Type": "application/json",
},
"body": JSON.stringify(content),
})).json();
}
async function initializeProject(store, pimResourceIri, label) {
const createId = uuid();
const setLabelId = uuid();
const setDescriptionId = uuid();
const requestContent = {
"operations": [
{
"types": [
"core-operation",
"https://ofn.gov.cz/slovník/pim/CreateSchema"
],
"iri": `https://ofn.gov.cz/operation/${createId}`,
"parent": null,
"pimNewIri": null,
"pimHumanLabel": null,
"pimHumanDescription": null
}, {
"types": [
"core-operation",
"https://ofn.gov.cz/slovník/pim/SetHumanLabel"
],
"iri": `https://ofn.gov.cz/operation/${setLabelId}`,
"parent": `https://ofn.gov.cz/operation/${createId}`,
"pimResource": pimResourceIri,
"pimHumanLabel": { "en": label }
}, {
"types": [
"core-operation",
"https://ofn.gov.cz/slovník/pim/SetHumanDescription"
],
"iri": `https://ofn.gov.cz/operation/${setDescriptionId}`,
"parent": `https://ofn.gov.cz/operation/${setLabelId}`,
"pimResource": pimResourceIri,
"pimHumanDescription": {}
}],
"resources": {
[pimResourceIri]: {
"types": [
"https://ofn.gov.cz/slovník/pim/Schema"
],
"iri": pimResourceIri,
"pimHumanLabel": { "en": label },
"pimHumanDescription": {},
"pimParts": []
}
}
};
return await await fetch(store.url, {
"method": "PUT",
"headers": {
"Content-Type": "application/json",
},
"body": JSON.stringify(requestContent),
});
}
/**
* Time based identifier.
*/
function uuid() {
const head = Date.now();
const tail = ([1e3]+-4e3+-8e3).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
return `${head}-${tail}`;
};
export async function createAndSetMmProject(dataSpecification) {
// Create new MM project
const {id: mmToolId} = await fetchPostJson(MM_API_ENDPOINT, {
label: dataSpecification.label,
});
// Update the data specification locally
dataSpecification.artefactConfiguration = {
...dataSpecification.artefactConfiguration,
[DATASPECER_MM_METADATA]: {
...dataSpecification.artefactConfiguration?.[DATASPECER_MM_METADATA],
projectId: mmToolId
}
}
dataSpecification.mmId = mmToolId;
// Modify the data specification on the server
await fetchPostJson(API_ENDPOINT, {
dataSpecificationIri: dataSpecification.iri,
update: {
artefactConfiguration: dataSpecification.artefactConfiguration,
}
}, "PUT");
}
export async function unlinkMmProject(dataSpecification) {
// Update the data specification locally
dataSpecification.artefactConfiguration = {
...dataSpecification.artefactConfiguration,
[DATASPECER_MM_METADATA]: {
...dataSpecification.artefactConfiguration?.[DATASPECER_MM_METADATA],
projectId: null
}
}
dataSpecification.mmId = null;
// Modify the data specification on the server
await fetchPostJson(API_ENDPOINT, {
dataSpecificationIri: dataSpecification.iri,
update: {
artefactConfiguration: dataSpecification.artefactConfiguration,
}
}, "PUT");
}