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
	convertResult(result: any, URL): Dataset {
33
		const doc = new Dataset();
34

    
35
		doc.title = this.getTitle(result);
36
		doc.description = this.getDescription(result);
37
		doc.identifier = this.getIdentifier(result);
38
		doc.url = URL;
39
		doc.sameAs = this.getSameAs(result);
40
		doc.creator = this.getCreator(result);
41
		doc.dateCreated = this.getDateCreated(result);
42
		doc.citation = this.getCitation(result);
43
		doc.license = this.getLicense(result);
44
		doc.keyword = this.getKeyword(result);
45
		return doc;
46
	}
47

    
48
	convertProject(project: any, URL, otherUrl): Organization {
49
		const doc = new Organization();
50

    
51
		doc.title = (project.name)?project.title:project.acronym;
52
		doc.identifier = new Array<Identifier>();
53
		doc.identifier.push({id:project.contractNum, schema: "grantid"});
54
		var funder = new Organization();
55
		funder.title = project.funder;
56
		doc.funder = funder;
57
		doc.url = URL;
58
		doc.sameAs =[project.url]
59
		return doc;
60
}
61

    
62
convertOrganization(organization: any, URL): Organization {
63
	const doc = new Organization();
64

    
65
	doc.title =  organization.title.name ;
66
	doc.legalName = organization.name;
67
	doc.areaServed = organization.country;
68
	doc.url = URL;
69

    
70
	return doc;
71
}
72

    
73
convertDatasource(datasource: any, URL, otherUrl): Organization {
74
	const doc = new Organization();
75

    
76
	doc.title = datasource.title.name
77
	doc.identifier = datasource.contractNum;
78
	doc.legalName = datasource.officialName;
79
	// doc.areaServed = datasource.country;
80
console.log("HERE")
81
console.log(datasource.countries)
82
	if(datasource.countries && datasource.countries.length > 0){
83
		doc.areaServed = datasource.countries[0];
84
	}
85
	doc.url = URL;
86
	if(datasource.oaiPmhURL || otherUrl || datasource.title.url){
87
		doc.sameAs = [];
88
		if(otherUrl){
89
			doc.sameAs.push(otherUrl);
90
		}
91
		if(datasource.oaiPmhURL){
92
			doc.sameAs.push(datasource.oaiPmhURL);
93

    
94
		}
95
		if(datasource.title.url){
96
			doc.sameAs.push(datasource.title.url);
97
		}
98
	}
99
	return doc;
100
}
101

    
102
	private getTitle(result: any): String[] {
103
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.title.content", null);
104
		if (!item) return null;
105
		return [item as String];
106
	}
107

    
108
	private getDescription(result: any): String[] {
109
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.description", null);
110
		if (!item) return null;
111
		return [item as String];
112
	}
113

    
114
	private getDateCreated(result: any): String[] {
115
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.dateofacceptance", null);
116
		if (!item) return null;
117
		return [item as String];
118
	}
119

    
120
	private getLicense(result: any): License[] {
121
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.bestaccessright", null);
122
		if (!item) return null;
123
		if (!_.has(item, "classid")) return null;
124
		if (!_.has(item, "classname")) return null;
125
		if (!_.has(item, "schemeid")) return null;
126

    
127
		return [{
128
			title: [_.get(item, "classname")],
129
			identifier: [{
130
				id: _.get(item, "classid"),
131
				schema: _.get(item, "schemeid")
132
			}]
133
		}];
134
	}
135

    
136
	private getIdentifier(result: any): Identifier[] {
137
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.pid", null);
138
		if (!item) return null;
139
		const array = new Array<Identifier>();
140
		if (Array.isArray(item)) {
141
			const itemArray = item as Array<any>;
142
			for (var i = 0; i < itemArray.length; i += 1) {
143
				const val = this.getSingleIdentifier(itemArray[i]);
144
				if (val) array.push(val);
145
			}
146
		}
147
		else {
148
			const val = this.getSingleIdentifier(item);
149
			if (val) array.push(val);
150
		}
151
		if (array.length == 0) return null;
152
		return array;
153
	}
154

    
155
	private getSingleIdentifier(item: any): Identifier {
156
		if (!_.has(item, "classname")) return null;
157
		if (!_.has(item, "content")) return null;
158
		return {
159
			schema: _.get(item, "classname"),
160
			id: _.get(item, "content")
161
		};
162
	}
163

    
164
	private getSameAs(result: any): String[] {
165
		const instances = _.get(result, "result.metadata.oaf:entity.oaf:result.children.instance", null);
166
		if (!instances) return null;
167
		if (!Array.isArray(instances)) return null;
168

    
169
		const array = new Array<String>();
170

    
171
		const instanceArray = instances as Array<any>;
172
		for (var i = 0; i < instanceArray.length; i += 1) {
173
			const webresources = _.get(instanceArray[i], "webresource", null);
174
			if (!webresources) continue;
175
			if (Array.isArray(webresources)) {
176
				const webresourceArray = webresources as Array<any>;
177
				for (var q = 0; q < webresourceArray.length; q += 1) {
178
					const url = _.get(webresourceArray[q], "url", null);
179
					if (!url) continue;
180
					array.push(url as String);
181
				}
182
			}
183
			else {
184
				const url = _.get(webresources, "url", null);
185
				if (!url) continue;
186
				array.push(url as String);
187
			}
188
		}
189
		if (array.length == 0) return null;
190
		return array;
191
	}
192

    
193
	private getKeyword(result: any): String[] {
194
		const subjects = _.get(result, "result.metadata.oaf:entity.oaf:result.subject", null);
195
		if (!subjects) return null;
196
		if (!Array.isArray(subjects)) return null;
197

    
198
		const array = new Array<String>();
199

    
200
		const subjectArray = subjects as Array<any>;
201
		for (var i = 0; i < subjectArray.length; i += 1) {
202
			const classid = _.get(subjectArray[i], "classid", null);
203
			if (classid !== "keyword") continue;
204

    
205
			const sub = _.get(subjectArray[i], "content", null);
206
			if (!sub) return null;
207

    
208
			array.push(sub as String);
209
		}
210
		if (array.length == 0) return null;
211
		return array;
212
	}
213

    
214
	private getCreator(result: any): Person[] {
215
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.creator", null);
216
		if (!item) return null;
217
		const array = new Array<Person>();
218
		if (Array.isArray(item)) {
219
			const itemArray = item as Array<any>;
220
			for (var i = 0; i < itemArray.length; i += 1) {
221
				const val = this.getSinglePerson(itemArray[i]);
222
				if (val) array.push(val);
223
			}
224
		}
225
		else {
226
			const val = this.getSinglePerson(item);
227
			if (val) array.push(val);
228
		}
229
		if (array.length == 0) return null;
230
		return array;
231
	}
232

    
233
	private getSinglePerson(item: any): Person {
234
		if (!_.has(item, "surname") && !_.has(item, "name") && !_.has(item, "content")) return null;
235
		return {
236
			familyName: _.get(item, "surname", null),
237
			givenName: _.get(item, "name", null),
238
			name: _.get(item, "content", null)
239
		};
240
	}
241

    
242
	private getCitation(result: any): Citation[] {
243
		const item = _.get(result, "result.metadata.oaf:entity.extraInfo.citations.citation", null);
244
		if (!item) return null;
245
		const array = new Array<Citation>();
246
		if (Array.isArray(item)) {
247
			const itemArray = item as Array<any>;
248
			for (var i = 0; i < itemArray.length; i += 1) {
249
				const val = this.getSingleCitation(itemArray[i]);
250
				if (val) array.push(val);
251
			}
252
		}
253
		else {
254
			const val = this.getSingleCitation(item);
255
			if (val) array.push(val);
256
		}
257
		if (array.length == 0) return null;
258
		return array;
259
	}
260

    
261
	private getSingleCitation(item: any): Citation {
262
		if (!_.has(item, "rawText")) return null;
263
		if (!_.has(item, "id")) return null;
264

    
265
		const array = new Array<Identifier>();
266

    
267
		const ids = _.get(item, "id", null);
268
		if (Array.isArray(ids)) {
269
			const idsArray = ids as Array<any>;
270

    
271
			for (var i = 0; i < idsArray.length; i += 1) {
272
				const type = _.get(idsArray[i], "type", null);
273
				const value = _.get(idsArray[i], "value", null);
274
				if (!type || !value) continue;
275
				array.push({
276
					id: value,
277
					schema: type
278
				});
279
			}
280
		}
281
		else {
282
			const type = _.get(ids, "type", null);
283
			const value = _.get(ids, "value", null);
284
			if (type && value) {
285
				array.push({
286
					id: value,
287
					schema: type
288
				});
289
			}
290
		}
291

    
292
		if (array.length == 0) return null;
293

    
294
		return {
295
			title: [_.get(item, "rawText")],
296
			identifier: array
297
		};
298
	}
299

    
300

    
301
}
(2-2/2)