-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.js
248 lines (229 loc) · 8.39 KB
/
import.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
import { uuid, query, update, sparqlEscapeString, sparqlEscapeUri } from 'mu';
import { RESOURCE_BASE_URI, WORKSPACE_URI, KIMAI_ACCOUNT_SERVICE_HOMEPAGE, SPARQL_PREFIXES } from './constants';
export function upsertResource(type, kimaiResource) {
if (type == 'customers') {
return upsertCustomer(kimaiResource);
} else if (type == 'projects') {
return upsertProject(kimaiResource);
} else if (type == 'activities') {
return upsertActivity(kimaiResource);
} else if (type == 'users') {
return upsertUser(kimaiResource);
} else {
throw new Error(`Unsupported resource type ${type}`);
}
}
async function upsertUser(kimaiUser) {
const account = await ensureKimaiResource('foaf:OnlineAccount', 'accounts', kimaiUser.id);
if (account.isNew) {
console.log(`Account with Kimai ID ${account.kimaiId} not found in triplestore. Going to create a new one.`);
await update(`
${SPARQL_PREFIXES}
INSERT DATA {
${sparqlEscapeUri(account.uri)} a foaf:OnlineAccount ;
mu:uuid ${sparqlEscapeString(account.uuid)} ;
dct:identifier ${sparqlEscapeString(account.kimaiId)} ;
foaf:accountName ${sparqlEscapeString(kimaiUser.username)} ;
foaf:accountServiceHomepage ${sparqlEscapeUri(KIMAI_ACCOUNT_SERVICE_HOMEPAGE)} .
}
`);
} else {
console.log(`Account with Kimai ID ${account.kimaiId} already exists.`);
}
}
async function upsertActivity(kimaiActivity) {
const activity = await ensureKimaiResource('ext:KimaiActivity', 'tasks', kimaiActivity.id)
if (activity.isNew) {
console.log(`Activity with Kimai ID ${activity.kimaiId} not found in triplestore. Going to create a new one.`);
await update(`
${SPARQL_PREFIXES}
INSERT DATA {
${sparqlEscapeUri(activity.uri)} a wf:Task, ext:KimaiActivity ;
mu:uuid ${sparqlEscapeString(activity.uuid)} ;
rdfs:label ${sparqlEscapeString(kimaiActivity.name)} ;
dct:identifier ${sparqlEscapeString(activity.kimaiId)} ;
skos:inScheme ${sparqlEscapeUri(WORKSPACE_URI)} .
${colorStatement(activity.uri, kimaiActivity.color)}
}
`);
// Relate activity to project
if (kimaiActivity.project) {
const project = await findKimaiResource('wf:Task', kimaiActivity.project);
if (project) {
await update(`
${SPARQL_PREFIXES}
INSERT DATA {
${sparqlEscapeUri(activity.uri)} skos:broader ${sparqlEscapeUri(project.uri)} .
}
`);
} else {
console.log(`Unable to link activity ${activity.kimaiId} to project ${kimaiActivity.project} since project is not available in triplestore.`);
}
}
} else {
console.log(`Activity with Kimai ID ${activity.kimaiId} already exists. Going to update name and color.`);
await update(`
${SPARQL_PREFIXES}
DELETE {
${sparqlEscapeUri(activity.uri)} rdfs:label ?name ;
ui:color ?color .
} INSERT {
${sparqlEscapeUri(activity.uri)} rdfs:label ${sparqlEscapeString(kimaiActivity.name)} .
${colorStatement(activity.uri, kimaiActivity.color)}
} WHERE {
${sparqlEscapeUri(activity.uri)} a wf:Task ;
rdfs:label ?name .
OPTIONAL { ${sparqlEscapeUri(activity.uri)} ui:color ?color . }
}
`);
}
// Inherit color from parent if not set in Kimai
await update(`
${SPARQL_PREFIXES}
INSERT {
${sparqlEscapeUri(activity.uri)} ui:color ?parentColor .
} WHERE {
${sparqlEscapeUri(activity.uri)} a wf:Task .
FILTER NOT EXISTS { ${sparqlEscapeUri(activity.uri)} ui:color ?color . }
${sparqlEscapeUri(activity.uri)} skos:broader ?parent .
?parent ui:color ?parentColor .
}
`);
}
async function upsertProject(kimaiProject) {
const project = await ensureKimaiResource('doap:Project', 'tasks', kimaiProject.id)
if (project.isNew) {
console.log(`Project with Kimai ID ${project.kimaiId} not found in triplestore. Going to create a new one.`);
await update(`
${SPARQL_PREFIXES}
INSERT DATA {
${sparqlEscapeUri(project.uri)} a wf:Task, doap:Project ;
mu:uuid ${sparqlEscapeString(project.uuid)} ;
rdfs:label ${sparqlEscapeString(kimaiProject.name)} ;
dct:identifier ${sparqlEscapeString(project.kimaiId)} ;
skos:topConceptOf ${sparqlEscapeUri(WORKSPACE_URI)} ;
skos:inScheme ${sparqlEscapeUri(WORKSPACE_URI)} .
${colorStatement(project.uri, kimaiProject.color)}
}
`);
// Relate project to customer
if (kimaiProject.customer) {
const customer = await findKimaiResource('prov:Organization', kimaiProject.customer);
if (customer) {
await update(`
${SPARQL_PREFIXES}
INSERT DATA {
${sparqlEscapeUri(project.uri)} prov:wasAttributedTo ${sparqlEscapeUri(customer.uri)} .
}
`);
} else {
console.log(`Unable to link project ${project.kimaiId} to customer ${kimaiProject.customer} since customer is not available in triplestore.`);
}
}
} else {
console.log(`Project with Kimai ID ${project.kimaiId} already exists. Going to update name and color.`);
await update(`
${SPARQL_PREFIXES}
DELETE {
${sparqlEscapeUri(project.uri)} rdfs:label ?name ;
ui:color ?color .
} INSERT {
${sparqlEscapeUri(project.uri)} rdfs:label ${sparqlEscapeString(kimaiProject.name)} .
${colorStatement(project.uri, kimaiProject.color)}
} WHERE {
${sparqlEscapeUri(project.uri)} a wf:Task ;
rdfs:label ?name .
OPTIONAL { ${sparqlEscapeUri(project.uri)} ui:color ?color . }
}
`);
}
// Inherit color from customer if not set in Kimai
await update(`
${SPARQL_PREFIXES}
INSERT {
${sparqlEscapeUri(project.uri)} ui:color ?customerColor .
} WHERE {
${sparqlEscapeUri(project.uri)} a wf:Task .
FILTER NOT EXISTS { ${sparqlEscapeUri(project.uri)} ui:color ?projectColor . }
${sparqlEscapeUri(project.uri)} prov:wasAttributedTo ?customer .
?customer ui:color ?customerColor .
}
`);
}
async function upsertCustomer(kimaiCustomer) {
const customer = await ensureKimaiResource('prov:Organization', 'customers', kimaiCustomer.id)
if (customer.isNew) {
console.log(`Customer with Kimai ID ${customer.kimaiId} not found in triplestore. Going to create a new one.`);
await update(`
${SPARQL_PREFIXES}
INSERT DATA {
${sparqlEscapeUri(customer.uri)} a prov:Organization ;
mu:uuid ${sparqlEscapeString(customer.uuid)} ;
rdfs:label ${sparqlEscapeString(kimaiCustomer.name)} ;
dct:identifier ${sparqlEscapeString(customer.kimaiId)} .
${colorStatement(customer.uri, kimaiCustomer.color)}
}
`);
} else {
console.log(`Customer with Kimai ID ${customer.kimaiId} already exists. Going to update name and color.`);
await update(`
${SPARQL_PREFIXES}
DELETE {
${sparqlEscapeUri(customer.uri)} rdfs:label ?name ;
ui:color ?color .
} INSERT {
${sparqlEscapeUri(customer.uri)} rdfs:label ${sparqlEscapeString(kimaiCustomer.name)} .
${colorStatement(customer.uri, kimaiCustomer.color)}
} WHERE {
${sparqlEscapeUri(customer.uri)} a prov:Organization ;
rdfs:label ?name .
OPTIONAL { ${sparqlEscapeUri(customer.uri)} ui:color ?color . }
}
`);
}
}
async function findKimaiResource(rdfType, kimaiId) {
const kimaiIdStr = `${kimaiId}`;
const result = await query(`
${SPARQL_PREFIXES}
SELECT ?uri ?uuid
WHERE {
?uri a ${rdfType} ;
mu:uuid ?uuid ;
dct:identifier ${sparqlEscapeString(kimaiIdStr)} .
} LIMIT 1
`);
if (result.results.bindings.length) {
return {
uri: result.results.bindings[0]['uri'].value,
uuid: result.results.bindings[0]['uuid'].value,
kimaiId: kimaiIdStr,
};
} else {
return null;
}
}
async function ensureKimaiResource(rdfType, resourceType, kimaiId) {
const resource = await findKimaiResource(rdfType, kimaiId);
if (resource) {
return {
...resource,
isNew: false
};
} else {
const id = uuid();
return {
uri: `${RESOURCE_BASE_URI}/${resourceType}/${id}`,
uuid: id,
kimaiId: `${kimaiId}`,
isNew: true
};
}
}
function colorStatement(subject, color) {
if (color) {
return `${sparqlEscapeUri(subject)} ui:color ${sparqlEscapeString(color)} .`
} else {
return '';
}
}