1
|
import { Injectable } from "@angular/core";
|
2
|
import { JsonldDocument, Identifier, Person, License, Citation, Dataset, Organization } from "../model/jsonld-document";
|
3
|
import * as _ from "lodash";
|
4
|
|
5
|
@Injectable()
|
6
|
export class JsonldDocumentSerializerService {
|
7
|
constructor() { }
|
8
|
serializeOrganization(doc: Organization): any {
|
9
|
const buffer = {};
|
10
|
buffer["@type"] = "Organization";
|
11
|
this.serializeDocument(doc, buffer);
|
12
|
if(doc.areaServed){
|
13
|
buffer["areaServed"] = doc.areaServed;
|
14
|
}
|
15
|
if(doc.legalName){
|
16
|
buffer["legalName"] = doc.legalName;
|
17
|
}
|
18
|
if(doc.funder ){
|
19
|
buffer["funder"] = this.serializeOrganization(doc.funder );
|
20
|
}
|
21
|
return buffer;
|
22
|
}
|
23
|
|
24
|
|
25
|
serializeDocument(doc:JsonldDocument, buffer):any{
|
26
|
buffer["@context"] = "http://schema.org";
|
27
|
buffer["@id"] = doc.id;
|
28
|
this.serializeName(doc, buffer);
|
29
|
this.serializeDescription(doc, buffer);
|
30
|
this.serializeIdentifier(doc, buffer);
|
31
|
this.serializeURL(doc, buffer);
|
32
|
this.serializeSameAs(doc, buffer);
|
33
|
}
|
34
|
serializeDataset(doc: Dataset): any {
|
35
|
var buffer = {};
|
36
|
buffer["@context"] = "http://schema.org";
|
37
|
buffer["@type"] = "Dataset";
|
38
|
this.serializeDocument(doc, buffer);
|
39
|
if(doc.headline){
|
40
|
buffer["headline"] = doc.headline;
|
41
|
}
|
42
|
if(doc.alternativeHeadline){
|
43
|
buffer["alternativeHeadline"] = doc.alternativeHeadline;
|
44
|
}
|
45
|
if(doc.issn){
|
46
|
buffer["issn"] = doc.issn;
|
47
|
}
|
48
|
|
49
|
if (doc.creator && doc.creator.length == 1) {
|
50
|
buffer["creator"] = this.buildCreator(doc.creator[0]);
|
51
|
}
|
52
|
else if (doc.creator && doc.creator.length > 1) {
|
53
|
const array = new Array<any>();
|
54
|
for (var i = 0; i < doc.creator.length; i += 1) {
|
55
|
array.push(this.buildCreator(doc.creator[i]));
|
56
|
}
|
57
|
buffer["creator"] = array;
|
58
|
}
|
59
|
|
60
|
if (doc.dateCreated && doc.dateCreated.length == 1) {
|
61
|
buffer["dateCreated"] = doc.dateCreated[0];
|
62
|
}
|
63
|
else if (doc.dateCreated && doc.dateCreated.length > 1) {
|
64
|
buffer["dateCreated"] = doc.dateCreated;
|
65
|
}
|
66
|
|
67
|
if (doc.dateModified && doc.dateModified.length == 1) {
|
68
|
buffer["dateModified"] = doc.dateModified[0];
|
69
|
}
|
70
|
else if (doc.dateModified && doc.dateModified.length > 1) {
|
71
|
buffer["dateModified"] = doc.dateModified;
|
72
|
}
|
73
|
|
74
|
if (doc.license && doc.license.length == 1) {
|
75
|
buffer["license"] = this.buildLicense(doc.license[0]);
|
76
|
}
|
77
|
else if (doc.license && doc.license.length > 1) {
|
78
|
const array = new Array<any>();
|
79
|
for (var i = 0; i < doc.license.length; i += 1) {
|
80
|
array.push(this.buildLicense(doc.license[i]));
|
81
|
}
|
82
|
buffer["license"] = array;
|
83
|
}
|
84
|
|
85
|
if (doc.keyword && doc.keyword.length == 1) {
|
86
|
buffer["keywords"] = doc.keyword[0];
|
87
|
}
|
88
|
else if (doc.keyword && doc.keyword.length > 1) {
|
89
|
buffer["keywords"] = _.join(doc.keyword, ", ");
|
90
|
}
|
91
|
|
92
|
if (doc.citation && doc.citation.length == 1) {
|
93
|
buffer["citation"] = this.buildCitation(doc.citation[0]);
|
94
|
}
|
95
|
else if (doc.citation && doc.citation.length > 1) {
|
96
|
const array = new Array<any>();
|
97
|
for (var i = 0; i < doc.citation.length; i += 1) {
|
98
|
array.push(this.buildCitation(doc.citation[i]));
|
99
|
}
|
100
|
buffer["citation"] = array;
|
101
|
}
|
102
|
return buffer;
|
103
|
// return JSON.stringify(buffer, (key, value) => {
|
104
|
// if (value !== null) return value
|
105
|
// }, 4);
|
106
|
}
|
107
|
|
108
|
serializeName(doc, buffer){
|
109
|
if (doc.title && doc.title.length == 1) {
|
110
|
buffer["name"] = doc.title[0];
|
111
|
}
|
112
|
else if (doc.title && doc.title.length > 1) {
|
113
|
buffer["name"] = doc.title;
|
114
|
}
|
115
|
}
|
116
|
|
117
|
serializeDescription(doc, buffer){
|
118
|
if (doc.description && doc.description.length == 1) {
|
119
|
buffer["description"] = doc.description[0];
|
120
|
}
|
121
|
else if (doc.description && doc.description.length > 1) {
|
122
|
buffer["description"] = doc.description;
|
123
|
}
|
124
|
}
|
125
|
serializeIdentifier(doc, buffer){
|
126
|
if (doc.identifier && doc.identifier.length == 1) {
|
127
|
buffer["identifier"] = this.buildIdentifier(doc.identifier[0]);
|
128
|
}
|
129
|
else if (doc.identifier && doc.identifier.length > 1) {
|
130
|
const array = new Array<any>();
|
131
|
for (var i = 0; i < doc.identifier.length; i += 1) {
|
132
|
array.push(this.buildIdentifier(doc.identifier[i]));
|
133
|
}
|
134
|
buffer["identifier"] = array;
|
135
|
}
|
136
|
}
|
137
|
serializeURL(doc, buffer){
|
138
|
if (doc.url && doc.url.length == 1) {
|
139
|
buffer["URL"] = doc.url[0];
|
140
|
}
|
141
|
else if (doc.url && doc.url.length > 1) {
|
142
|
buffer["URL"] = doc.url;
|
143
|
}
|
144
|
}
|
145
|
serializeSameAs(doc, buffer){
|
146
|
if (doc.sameAs && doc.sameAs.length == 1) {
|
147
|
buffer["sameAs"] = doc.sameAs[0];
|
148
|
}
|
149
|
else if (doc.sameAs && doc.sameAs.length > 1) {
|
150
|
buffer["sameAs"] = doc.sameAs;
|
151
|
}
|
152
|
}
|
153
|
|
154
|
buildIdentifier(item: Identifier): any {
|
155
|
return {
|
156
|
"@type": "PropertyValue",
|
157
|
"propertyID": item.schema,
|
158
|
"value": item.id
|
159
|
};
|
160
|
}
|
161
|
|
162
|
buildCreator(item: Person): any {
|
163
|
var person ={};
|
164
|
person["@type"] = "Person";
|
165
|
if(item.givenName){
|
166
|
person["givenName"] = item.givenName;
|
167
|
}
|
168
|
if(item.familyName){
|
169
|
person["familyName"] = item.familyName;
|
170
|
}
|
171
|
person["name"] = item.name;
|
172
|
return person;
|
173
|
// return {
|
174
|
// "@type": "Person",
|
175
|
// "givenName": item.givenName,
|
176
|
// "familyName": item.familyName,
|
177
|
// "name": item.name
|
178
|
// };
|
179
|
}
|
180
|
|
181
|
buildLicense(item: License): any {
|
182
|
const licenseBuffer = {
|
183
|
"@type": "CreativeWork"
|
184
|
};
|
185
|
|
186
|
if (item.title && item.title.length == 1) {
|
187
|
licenseBuffer["name"] = item.title[0];
|
188
|
}
|
189
|
else if (item.title && item.title.length > 1) {
|
190
|
licenseBuffer["name"] = item.title;
|
191
|
}
|
192
|
|
193
|
if (item.identifier && item.identifier.length == 1) {
|
194
|
licenseBuffer["identifier"] = this.buildIdentifier(item.identifier[0]);
|
195
|
}
|
196
|
else if (item.identifier && item.identifier.length > 1) {
|
197
|
const array = new Array<any>();
|
198
|
for (var i = 0; i < item.identifier.length; i += 1) {
|
199
|
array.push(this.buildIdentifier(item.identifier[i]));
|
200
|
}
|
201
|
licenseBuffer["identifier"] = array;
|
202
|
}
|
203
|
|
204
|
return licenseBuffer;
|
205
|
}
|
206
|
|
207
|
buildCitation(item: Citation): any {
|
208
|
const citationBuffer = {
|
209
|
"@type": "CreativeWork"
|
210
|
};
|
211
|
|
212
|
if (item.title && item.title.length == 1) {
|
213
|
citationBuffer["name"] = item.title[0];
|
214
|
}
|
215
|
else if (item.title && item.title.length > 1) {
|
216
|
citationBuffer["name"] = item.title;
|
217
|
}
|
218
|
|
219
|
if (item.identifier && item.identifier.length == 1) {
|
220
|
citationBuffer["identifier"] = this.buildIdentifier(item.identifier[0]);
|
221
|
}
|
222
|
else if (item.identifier && item.identifier.length > 1) {
|
223
|
const array = new Array<any>();
|
224
|
for (var i = 0; i < item.identifier.length; i += 1) {
|
225
|
array.push(this.buildIdentifier(item.identifier[i]));
|
226
|
}
|
227
|
citationBuffer["identifier"] = array;
|
228
|
}
|
229
|
|
230
|
return citationBuffer;
|
231
|
}
|
232
|
|
233
|
}
|