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 OpenAireJsonldConverterService {
7
	constructor() { }
8

    
9
	createHome(name, URL, logoURL): any {
10
		const buffer = {};
11
		buffer["@context"] = "http://schema.org";
12
		buffer["@type"] = "Organization";
13
		buffer["name"] = name;
14
		buffer["url"] = URL;
15
		buffer["logo"] = logoURL;
16
		const action ={};
17
		action["@type"]= "SearchAction";
18
		action["target"]= URL+"/search/find/?keyword={search_term_string}";
19
		action["query-input"]= "required name=search_term_string";
20

    
21
		buffer["potentialAction"] = action;
22
		const sameAs =["https://www.openaire.eu",
23
		"https://www.facebook.com/groups/openaire/",
24
		"https://www.twitter.com/OpenAIRE_eu",
25
		"https://www.linkedin.com/groups/OpenAIRE-3893548",
26
		"https://www.slideshare.net/OpenAIRE_eu",
27
		"https://www.youtube.com/channel/UChFYqizc-S6asNjQSoWuwjw"];
28

    
29
		buffer["sameAs"] = sameAs;
30
		return buffer;
31
	}
32
	createSearchPage(name, URL, logoURL): any {
33
		const buffer = {};
34
		buffer["@context"] = "http://schema.org";
35
		buffer["@type"] = "Organization";
36
		buffer["name"] = name;
37
		buffer["url"] = URL;
38
		buffer["logo"] = logoURL;
39
		const action ={};
40
		action["@type"]= "SearchAction";
41
		action["target"]= URL+"?keyword={search_term_string}";
42
		action["query-input"]= "required name=search_term_string";
43
		buffer["potentialAction"] = action;
44
		return buffer;
45
	}
46
	convertResult(result: any, URL): Dataset {
47
		const doc = new Dataset();
48

    
49
		doc.title = this.getTitle(result);
50
		doc.headline = doc.title;
51
		if(this.getSubTitle(result)){
52
			doc.alternativeHeadline = this.getSubTitle(result);
53
		}
54
		doc.issn = this.getISSN(result);
55
		doc.description = this.getDescription(result);
56
		doc.identifier = this.getIdentifier(result);
57
		doc.url = URL;
58
		doc.sameAs = this.getSameAs(result);
59
		doc.creator = this.getCreator(result);
60
		doc.dateCreated = this.getDateCreated(result);
61
		doc.citation = this.getCitation(result);
62
		doc.license = this.getLicense(result);
63
		doc.keyword = this.getKeyword(result);
64
		return doc;
65
	}
66

    
67
	convertProject(project: any, URL, otherUrl): Organization {
68
		const doc = new Organization();
69

    
70
		doc.title = (project.name)?project.title:project.acronym;
71
		doc.identifier = new Array<Identifier>();
72
		doc.identifier.push({id:project.contractNum, schema: "grantid"});
73
		var funder = new Organization();
74
		funder.title = project.funder;
75
		doc.funder = funder;
76
		doc.url = URL;
77
		doc.sameAs =[project.url]
78
		return doc;
79
}
80

    
81
convertOrganization(organization: any, URL): Organization {
82
	const doc = new Organization();
83

    
84
	doc.title =  organization.title.name ;
85
	doc.legalName = organization.name;
86
	doc.areaServed = organization.country;
87
	doc.url = URL;
88

    
89
	return doc;
90
}
91

    
92
convertDatasource(datasource: any, URL, otherUrl): Organization {
93
	const doc = new Organization();
94

    
95
	doc.title = datasource.title.name
96
	doc.identifier = datasource.contractNum;
97
	doc.legalName = datasource.officialName;
98
	if(datasource.countries && datasource.countries.length > 0){
99
		doc.areaServed = datasource.countries[0];
100
	}
101
	doc.url = URL;
102
	if(datasource.oaiPmhURL || otherUrl || datasource.title.url){
103
		doc.sameAs = [];
104
		if(otherUrl){
105
			doc.sameAs.push(otherUrl);
106
		}
107
		if(datasource.oaiPmhURL){
108
			doc.sameAs.push(datasource.oaiPmhURL);
109

    
110
		}
111
		if(datasource.title.url){
112
			doc.sameAs.push(datasource.title.url);
113
		}
114
	}
115
	return doc;
116
}
117

    
118
	private getTitle(result: any): String[] {
119
		const title = _.get(result, "result.metadata.oaf:entity.oaf:result.title", null);
120
		if(title && Array.isArray(title) ){
121
			return title[0].content;
122
		}else{
123
			return title.content;
124
		}
125

    
126
	}
127
	private getSubTitle(result: any): String[] {
128
		const title = _.get(result, "result.metadata.oaf:entity.oaf:result.title", null);
129
		if(title && Array.isArray(title) ){
130
			if(title[1] && title[1].classid == "subtitle") {
131
				return title[1].content;
132
			}
133
		}
134
		return null;
135
	}
136
	private getISSN(result: any): String[] {
137
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.journal.issn", null);
138
		if (!item) return null;
139
		return [item as String];
140
	}
141
	private getDescription(result: any): String[] {
142
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.description", null);
143
		if (!item) return null;
144
		return [item as String];
145
	}
146

    
147
	private getDateCreated(result: any): String[] {
148
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.dateofacceptance", null);
149
		if (!item) return null;
150
		return [item as String];
151
	}
152

    
153
	private getLicense(result: any): License[] {
154
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.bestaccessright", null);
155
		if (!item) return null;
156
		if (!_.has(item, "classid")) return null;
157
		if (!_.has(item, "classname")) return null;
158
		if (!_.has(item, "schemeid")) return null;
159

    
160
		return [{
161
			title: [_.get(item, "classname")],
162
			identifier: [{
163
				id: _.get(item, "classid"),
164
				schema: _.get(item, "schemeid")
165
			}]
166
		}];
167
	}
168

    
169
	private getIdentifier(result: any): Identifier[] {
170
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.pid", null);
171
		if (!item) return null;
172
		const array = new Array<Identifier>();
173
		if (Array.isArray(item)) {
174
			const itemArray = item as Array<any>;
175
			for (var i = 0; i < itemArray.length; i += 1) {
176
				const val = this.getSingleIdentifier(itemArray[i]);
177
				if (val) array.push(val);
178
			}
179
		}
180
		else {
181
			const val = this.getSingleIdentifier(item);
182
			if (val) array.push(val);
183
		}
184
		if (array.length == 0) return null;
185
		return array;
186
	}
187

    
188
	private getSingleIdentifier(item: any): Identifier {
189
		if (!_.has(item, "classname")) return null;
190
		if (!_.has(item, "content")) return null;
191
		return {
192
			schema: _.get(item, "classname"),
193
			id: _.get(item, "content")
194
		};
195
	}
196

    
197
	private getSameAs(result: any): String[] {
198
		const instances = _.get(result, "result.metadata.oaf:entity.oaf:result.children.instance", null);
199
		if (!instances) return null;
200
		if (!Array.isArray(instances)) return null;
201

    
202
		const array = new Array<String>();
203

    
204
		const instanceArray = instances as Array<any>;
205
		for (var i = 0; i < instanceArray.length; i += 1) {
206
			const webresources = _.get(instanceArray[i], "webresource", null);
207
			if (!webresources) continue;
208
			if (Array.isArray(webresources)) {
209
				const webresourceArray = webresources as Array<any>;
210
				for (var q = 0; q < webresourceArray.length; q += 1) {
211
					const url = _.get(webresourceArray[q], "url", null);
212
					if (!url) continue;
213
					array.push(url as String);
214
				}
215
			}
216
			else {
217
				const url = _.get(webresources, "url", null);
218
				if (!url) continue;
219
				array.push(url as String);
220
			}
221
		}
222
		if (array.length == 0) return null;
223
		return array;
224
	}
225

    
226
	private getKeyword(result: any): String[] {
227
		const subjects = _.get(result, "result.metadata.oaf:entity.oaf:result.subject", null);
228
		if (!subjects) return null;
229
		if (!Array.isArray(subjects)) return null;
230

    
231
		const array = new Array<String>();
232

    
233
		const subjectArray = subjects as Array<any>;
234
		for (var i = 0; i < subjectArray.length; i += 1) {
235
			const classid = _.get(subjectArray[i], "classid", null);
236
			if (classid !== "keyword") continue;
237

    
238
			const sub = _.get(subjectArray[i], "content", null);
239
			if (!sub) return null;
240

    
241
			array.push(sub as String);
242
		}
243
		if (array.length == 0) return null;
244
		return array;
245
	}
246

    
247
	private getCreator(result: any): Person[] {
248
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.creator", null);
249
		if (!item) return null;
250
		const array = new Array<Person>();
251
		if (Array.isArray(item)) {
252
			const itemArray = item as Array<any>;
253
			for (var i = 0; i < itemArray.length; i += 1) {
254
				const val = this.getSinglePerson(itemArray[i]);
255
				if (val) array.push(val);
256
			}
257
		}
258
		else {
259
			const val = this.getSinglePerson(item);
260
			if (val) array.push(val);
261
		}
262
		if (array.length == 0) return null;
263
		return array;
264
	}
265

    
266
	private getSinglePerson(item: any): Person {
267
		if (!_.has(item, "surname") && !_.has(item, "name") && !_.has(item, "content")) return null;
268
		return {
269
			familyName: _.get(item, "surname", null),
270
			givenName: _.get(item, "name", null),
271
			name: _.get(item, "content", null)
272
		};
273
	}
274

    
275
	private getCitation(result: any): Citation[] {
276
		const item = _.get(result, "result.metadata.oaf:entity.extraInfo.citations.citation", null);
277
		if (!item) return null;
278
		const array = new Array<Citation>();
279
		if (Array.isArray(item)) {
280
			const itemArray = item as Array<any>;
281
			for (var i = 0; i < itemArray.length; i += 1) {
282
				const val = this.getSingleCitation(itemArray[i]);
283
				if (val) array.push(val);
284
			}
285
		}
286
		else {
287
			const val = this.getSingleCitation(item);
288
			if (val) array.push(val);
289
		}
290
		if (array.length == 0) return null;
291
		return array;
292
	}
293

    
294
	private getSingleCitation(item: any): Citation {
295
		if (!_.has(item, "rawText")) return null;
296
		if (!_.has(item, "id")) return null;
297

    
298
		const array = new Array<Identifier>();
299

    
300
		const ids = _.get(item, "id", null);
301
		if (Array.isArray(ids)) {
302
			const idsArray = ids as Array<any>;
303

    
304
			for (var i = 0; i < idsArray.length; i += 1) {
305
				const type = _.get(idsArray[i], "type", null);
306
				const value = _.get(idsArray[i], "value", null);
307
				if (!type || !value) continue;
308
				array.push({
309
					id: value,
310
					schema: type
311
				});
312
			}
313
		}
314
		else {
315
			const type = _.get(ids, "type", null);
316
			const value = _.get(ids, "value", null);
317
			if (type && value) {
318
				array.push({
319
					id: value,
320
					schema: type
321
				});
322
			}
323
		}
324

    
325
		if (array.length == 0) return null;
326

    
327
		return {
328
			title: [_.get(item, "rawText")],
329
			identifier: array
330
		};
331
	}
332

    
333

    
334
}
(2-2/2)