This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdf-manager.ts
124 lines (116 loc) · 3.53 KB
/
rdf-manager.ts
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
/* istanbul ignore file */
import * as rdflib from 'rdflib';
const LPA = rdflib.Namespace('https://w3id.org/def/lpapps#');
/* tslint:disable */
/**
* The RdfManager class encapsulating some LPA specific logic and general
* rdflib library
*/
class RdfManager {
static getInstance() {
if (!RdfManager.instance) {
RdfManager.instance = new RdfManager();
// ... any one time initialization goes here ...
}
return RdfManager.instance;
}
private static instance: RdfManager;
store: any;
fetcher: rdflib.Fetcher;
updater: rdflib.UpdateManager;
/* tslint:disable */
public requiresForceReload = false;
private constructor() {
this.requiresForceReload = false;
this.store = rdflib.graph();
this.fetcher = new rdflib.Fetcher(this.store);
this.updater = new rdflib.UpdateManager(this.store);
}
/**
* A method to update an current rdflib graph instance
* @param deletions rdflib statements to delete
* @param insertions rdflib statements to insert
*/
public async update(
deletions: rdflib.Statement[],
insertions: rdflib.Statement[]
): Promise<any> {
try {
return this.updater.update(deletions, insertions, (uri, ok, message) => {
console.log(
'uri:' + uri + '\n' + 'status:' + ok + '\n' + 'message:' + message
);
return Promise.resolve(message);
});
} catch (err) {
return Promise.reject(new Error('Could not update the document.'));
}
}
/**
* A method to load and parse an RDF lib into rdflib graph
* @param document NamedNode to load
*/
public async load(document: rdflib.NamedNode): Promise<any> {
const reloadRequired = this.requiresForceReload;
this.requiresForceReload = false;
try {
return this.fetcher.load(document, {
force: reloadRequired,
clearPreviousData: reloadRequired
});
} catch (err) {
return Promise.reject(new Error('Could not fetch the document.'));
}
}
/**
* Updates rdf file
* @param resourceUrl Resource to url
* @param insertions Rdflib insertions to perform
* @param deletions Rdflib deletions to perform
*/
public async updateResource(
resourceUrl: string,
insertions: rdflib.Statement[],
deletions: rdflib.Statement[]
): Promise<any> {
const resource = rdflib.sym(resourceUrl);
try {
let response1 = await this.load(resource);
let response2 = await this.update(deletions, insertions);
return Promise.resolve('Resource updated!' + response1 + response2);
} catch (err) {
return Promise.reject(err);
}
}
/**
* Method sets reference to folder with linkedpipes applications
* configurations for particlar webId
* @param webId WebId of the user
* @param folderUrl Path to the lpa folder in pod
*/
public async updateAppFolder(
webId: string,
folderUrl: string
): Promise<boolean> {
try {
const user = rdflib.sym(webId);
const predicate = rdflib.sym(LPA('lpStorage'));
const folder = rdflib.sym(folderUrl);
const profile = user.doc();
try {
await this.load(profile);
} catch (err) {
return false;
}
const ins = [rdflib.st(user, predicate, folder, profile)];
const del = this.store.statementsMatching(user, predicate, null, profile);
await this.updateResource(profile.value, ins, del);
return true;
} catch (err) {
return false;
}
}
}
// Usage
let StorageRdfManager = RdfManager.getInstance(); // do something with the instance...
export { rdflib, StorageRdfManager };