Project

General

Profile

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
		this.serializeName(doc, buffer);
28
		this.serializeDescription(doc, buffer);
29
		this.serializeIdentifier(doc, buffer);
30
		this.serializeURL(doc, buffer);
31
		this.serializeSameAs(doc, buffer);
32
	}
33
	serializeDataset(doc: Dataset): any {
34
		var buffer = {};
35
		buffer["@context"] = "http://schema.org";
36
		buffer["@type"] = "Dataset";
37
		this.serializeDocument(doc, buffer);
38
		if(doc.headline){
39
			buffer["headline"] = doc.headline;
40
		}
41
		if(doc.alternativeHeadline){
42
			buffer["alternativeHeadline"] = doc.alternativeHeadline;
43
		}
44
		if(doc.issn){
45
			buffer["issn"] = doc.issn;
46
		}
47

    
48
		if (doc.creator && doc.creator.length == 1) {
49
			buffer["creator"] = this.buildCreator(doc.creator[0]);
50
		}
51
		else if (doc.creator && doc.creator.length > 1) {
52
			const array = new Array<any>();
53
			for (var i = 0; i < doc.creator.length; i += 1) {
54
				array.push(this.buildCreator(doc.creator[i]));
55
			}
56
			buffer["creator"] = array;
57
		}
58

    
59
		if (doc.dateCreated && doc.dateCreated.length == 1) {
60
			buffer["dateCreated"] = doc.dateCreated[0];
61
		}
62
		else if (doc.dateCreated && doc.dateCreated.length > 1) {
63
			buffer["dateCreated"] = doc.dateCreated;
64
		}
65

    
66
		if (doc.dateModified && doc.dateModified.length == 1) {
67
			buffer["dateModified"] = doc.dateModified[0];
68
		}
69
		else if (doc.dateModified && doc.dateModified.length > 1) {
70
			buffer["dateModified"] = doc.dateModified;
71
		}
72

    
73
		if (doc.license && doc.license.length == 1) {
74
			buffer["license"] = this.buildLicense(doc.license[0]);
75
		}
76
		else if (doc.license && doc.license.length > 1) {
77
			const array = new Array<any>();
78
			for (var i = 0; i < doc.license.length; i += 1) {
79
				array.push(this.buildLicense(doc.license[i]));
80
			}
81
			buffer["license"] = array;
82
		}
83

    
84
		if (doc.keyword && doc.keyword.length == 1) {
85
			buffer["keywords"] = doc.keyword[0];
86
		}
87
		else if (doc.keyword && doc.keyword.length > 1) {
88
			buffer["keywords"] = _.join(doc.keyword, ", ");
89
		}
90

    
91
		if (doc.citation && doc.citation.length == 1) {
92
			buffer["citation"] = this.buildCitation(doc.citation[0]);
93
		}
94
		else if (doc.citation && doc.citation.length > 1) {
95
			const array = new Array<any>();
96
			for (var i = 0; i < doc.citation.length; i += 1) {
97
				array.push(this.buildCitation(doc.citation[i]));
98
			}
99
			buffer["citation"] = array;
100
		}
101
		return buffer;
102
		// return JSON.stringify(buffer, (key, value) => {
103
		// 	if (value !== null) return value
104
		// }, 4);
105
	}
106

    
107
	serializeName(doc, buffer){
108
 		if (doc.title && doc.title.length == 1) {
109
			buffer["name"] = doc.title[0];
110
		}
111
		else if (doc.title && doc.title.length > 1) {
112
			buffer["name"] = doc.title;
113
		}
114
	}
115

    
116
	serializeDescription(doc, buffer){
117
 		if (doc.description && doc.description.length == 1) {
118
			buffer["description"] = doc.description[0];
119
		}
120
		else if (doc.description && doc.description.length > 1) {
121
			buffer["description"] = doc.description;
122
		}
123
	}
124
	serializeIdentifier(doc, buffer){
125
		if (doc.identifier && doc.identifier.length == 1) {
126
			buffer["identifier"] = this.buildIdentifier(doc.identifier[0]);
127
		}
128
		else if (doc.identifier && doc.identifier.length > 1) {
129
			const array = new Array<any>();
130
			for (var i = 0; i < doc.identifier.length; i += 1) {
131
				array.push(this.buildIdentifier(doc.identifier[i]));
132
			}
133
			buffer["identifier"] = array;
134
		}
135
	}
136
	serializeURL(doc, buffer){
137
		if (doc.url && doc.url.length == 1) {
138
			buffer["URL"] = doc.url[0];
139
		}
140
		else if (doc.url && doc.url.length > 1) {
141
			buffer["URL"] = doc.url;
142
		}
143
	}
144
	serializeSameAs(doc, buffer){
145
		if (doc.sameAs && doc.sameAs.length == 1) {
146
		buffer["sameAs"] = doc.sameAs[0];
147
		}
148
		else if (doc.sameAs && doc.sameAs.length > 1) {
149
			buffer["sameAs"] = doc.sameAs;
150
		}
151
	}
152

    
153
	buildIdentifier(item: Identifier): any {
154
		return {
155
			"@type": "PropertyValue",
156
			"propertyID": item.schema,
157
			"value": item.id
158
		};
159
	}
160

    
161
	buildCreator(item: Person): any {
162
		var person ={};
163
		person["@type"] = "Person";
164
		if(item.givenName){
165
			person["givenName"] = item.givenName;
166
		}
167
		if(item.familyName){
168
			person["familyName"] = item.familyName;
169
		}
170
		person["name"] = item.name;
171
		return person;
172
		// return {
173
		// 	"@type": "Person",
174
		// 	"givenName": item.givenName,
175
		// 	"familyName": item.familyName,
176
		// 	"name": item.name
177
		// };
178
	}
179

    
180
	buildLicense(item: License): any {
181
		const licenseBuffer = {
182
			"@type": "CreativeWork"
183
		};
184

    
185
		if (item.title && item.title.length == 1) {
186
			licenseBuffer["name"] = item.title[0];
187
		}
188
		else if (item.title && item.title.length > 1) {
189
			licenseBuffer["name"] = item.title;
190
		}
191

    
192
		if (item.identifier && item.identifier.length == 1) {
193
			licenseBuffer["identifier"] = this.buildIdentifier(item.identifier[0]);
194
		}
195
		else if (item.identifier && item.identifier.length > 1) {
196
			const array = new Array<any>();
197
			for (var i = 0; i < item.identifier.length; i += 1) {
198
				array.push(this.buildIdentifier(item.identifier[i]));
199
			}
200
			licenseBuffer["identifier"] = array;
201
		}
202

    
203
		return licenseBuffer;
204
	}
205

    
206
	buildCitation(item: Citation): any {
207
		const citationBuffer = {
208
			"@type": "CreativeWork"
209
		};
210

    
211
		if (item.title && item.title.length == 1) {
212
			citationBuffer["name"] = item.title[0];
213
		}
214
		else if (item.title && item.title.length > 1) {
215
			citationBuffer["name"] = item.title;
216
		}
217

    
218
		if (item.identifier && item.identifier.length == 1) {
219
			citationBuffer["identifier"] = this.buildIdentifier(item.identifier[0]);
220
		}
221
		else if (item.identifier && item.identifier.length > 1) {
222
			const array = new Array<any>();
223
			for (var i = 0; i < item.identifier.length; i += 1) {
224
				array.push(this.buildIdentifier(item.identifier[i]));
225
			}
226
			citationBuffer["identifier"] = array;
227
		}
228

    
229
		return citationBuffer;
230
	}
231

    
232
}
(1-1/2)