-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-user-controller.js
143 lines (119 loc) · 5.92 KB
/
ng-user-controller.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
/*eslint-env browser, node*/
/*global coreFactory:false, serializerFactory:false, parserFactory:false */
/*global graph:false */
/*global rdfNode:false */
/*global angular:false */
//noinspection Eslint
function UserController($scope) {
"use strict";
$scope.triples = []; // Here it's a JSON-LD
$scope.submit = function() {
if (($scope.newSubject) && ($scope.newPredicate) && ($scope.newObject)) {
console.log($scope.newSubject);
console.log($scope.newPredicate);
console.log($scope.newObject);
var ressource = coreFactory.getCore("http://localhost:8080/api/user/54d357ea2f6af8e974000001");
ressource.edit(function(graph) {
return graph.addTriple(rdfNode.iri($scope.newSubject), rdfNode.iri($scope.newPredicate), $scope.newObject)
.then(function() {
var ressource2 = coreFactory.getCore("http://localhost:8080/api/user/54d357ea2f6af8e974000001");
ressource2.getState()
.then(function (g) {
// Here we have an in-memory graph representation
var strJson = "";
// Creating an instance of the serialiser from the factory
var serialiser = serializerFactory.getSerializer({
contentType: "application/ld+json",
graph: g
});
// Serialising the Graph to JSON-LD
return serialiser(function (line) {
strJson += line; // Chunks of JSON-LD here !
}).then(function () {
console.log("JSONLD REPRESENTATION:");
// strJson contains the full JSON-LD
// we hope it's the same as the original one
console.log(strJson);
angular.fromJson(strJson).forEach(function (e) {
var json = {};
json.subject = e["@id"];
for (var key in e) {
if (e.hasOwnProperty(key) && key !== "@id") {
json.predicate = key;
if (e[key]["@value"]) {
json.object = e[key]["@value"];
} else {
json.object = e[key]["@id"];
}
}
}
$scope.triples.push(json);
});
$scope.$apply(); // ugly
});
});
}).catch(function(reason) {
console.log(reason);
});
});
}
};
var bc = coreFactory.getCore("http://localhost:8080/api/user/54d357ea2f6af8e974000001");
bc.getState()
.then(function(g) {
// Here we have an in-memory graph representation
var strJson = "";
// Creating an instance of the serialiser from the factory
var serialiser = serializerFactory.getSerializer({
contentType: "application/ld+json",
graph: g
});
// Serialising the Graph to JSON-LD
return serialiser(function(line) {
strJson += line; // Chunks of JSON-LD here !
}).then(function() {
console.log("JSONLD REPRESENTATION:");
// strJson contains the full JSON-LD
// we hope it's the same as the original one
console.log(strJson);
angular.fromJson(strJson).forEach(function(e) {
var json = {};
json.subject = e["@id"];
for (var key in e) {
if (e.hasOwnProperty(key) && key !== "@id") {
json.predicate = key;
if (e[key]["@value"]) {
json.object = e[key]["@value"];
} else {
json.object = e[key]["@id"];
}
}
}
$scope.triples.push(json);
});
$scope.$apply(); // ugly
// OPTIONAL, juste because i want ntriples
// We can use the g that you have before,
// but we can do a round trip:
// the new JSON-LD -> GRAPH -> NTRIPLES
var parser = parserFactory.getParser({
contentType: "application/ld+json", // it take Json-LD
graph: graph.graph() // to construct a graph()
});
parser.addChunk(strJson); // Fill it with the whole Json-LD
return parser.finalize();
}).then(function(parsedGraph) {
// Creating an instance of the serialiser from the factory
var nTriplesSerialiser = serializerFactory.getSerializer({
contentType: "application/n-triples",
graph: parsedGraph
});
console.log("NTRIPLE REPRESENTATION:");
return nTriplesSerialiser(function(line) {
console.log(line);
});
});
}).catch(function(reason) {
console.log(reason);
});
}