Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Citation, Dataset, Identifier, License, Organization, Person} from '../model/jsonld-document';
3
import * as _ from 'lodash';
4
import {properties} from '../../../../../environments/environment';
5

    
6
@Injectable()
7
export class OpenAireJsonldConverterService {
8

    
9
	constructor() {
10

    
11
	}
12

    
13
	createHome(name, URL, logoURL, description:string, searchActionRoute ): any {
14
		const organization = {};
15
		organization["@context"] = "http://schema.org";
16
		organization["@id"] = URL+"/#organization";
17
		organization["@type"] = "Organization";
18
		organization["name"] = name;
19
		organization["url"] = URL;
20
		if(logoURL && logoURL.length > 0 ) {
21
			organization["logo"] = logoURL;
22
		}
23
		if(description){
24
			organization["description"] = description;
25
		}
26
		if((properties.domain.indexOf("connect.openaire.eu")!=-1 || properties.domain.indexOf("explore.openaire.eu")!=-1|| properties.domain.indexOf("monitor.openaire.eu")!=-1) && properties.baseLink == "/") {
27
			const sameAs = ["https://www.openaire.eu",
28
				"https://www.facebook.com/groups/openaire/",
29
				"https://www.twitter.com/OpenAIRE_eu",
30
				"https://www.linkedin.com/groups/OpenAIRE-3893548",
31
				"https://www.slideshare.net/OpenAIRE_eu",
32
				"https://www.youtube.com/channel/UChFYqizc-S6asNjQSoWuwjw"];
33
			organization["sameAs"] = sameAs;
34
		}
35
		if(searchActionRoute) {
36
			return [organization, this.createDefaultSearchPage(name, URL,logoURL,description,searchActionRoute)];
37
		}
38
		return organization;
39
	}
40
	createDefaultSearchPage(name, URL, logoURL, description:string, searchRoute ){
41
		const searchPage = {};
42
		searchPage["@context"] = "http://schema.org";
43
		searchPage["@id"] = URL + "/#search";
44
		searchPage["@type"] = "Website";
45
		searchPage["name"] = name;
46
		searchPage["url"] = URL + searchRoute;
47
	/* no valid property for Website
48
		if(logoURL && logoURL.length > 0 ) {
49
			searchPage["logo"] = logoURL;
50
		}*/
51
		if (description) {
52
			searchPage["description"] = description;
53
		}
54
		const action = {};
55
		action["@type"] = "SearchAction";
56
		action["@id"] = URL + "/#search-action";
57
		action["target"] = URL+ searchRoute +  "?fv0={search_term_string}&f0=q";
58
		action["query-input"] = "required name=search_term_string";
59

    
60
		searchPage["potentialAction"] = action;
61
		return  searchPage;
62
	}
63
	createSimplePage(name, URL, description:string = null): any {
64
		const buffer = {};
65
		buffer["@context"] = "http://schema.org";
66
		buffer["@type"] = "WebPage";
67
		buffer["name"] = name;
68
		buffer["url"] = URL;
69
		if(description){
70
			buffer["description"] = description;
71
		}else{
72
			buffer["description"] = name;
73
		}
74
		buffer["@id"] = URL;
75
		return buffer;
76
	}
77
	createSearchPage(name, URL, logoURL, searchAction:boolean = true, description:string = null, searchActionRoute = null): any {
78
		const buffer = {};
79
		buffer["@context"] = "http://schema.org";
80
		buffer["@type"] = "SearchResultsPage";
81
		buffer["name"] = name;
82
		if(description){
83
			buffer["description"] = description;
84
		}
85
		buffer["url"] = URL;
86
		buffer["@id"] = URL;
87
		buffer["logo"] = logoURL;
88
		buffer["isPartOf"] = properties.domain+ properties.baseLink +"/#search";
89
		if(searchAction){
90
			 return [buffer,this.createDefaultSearchPage(name, URL,logoURL,description,searchActionRoute) ]
91
		}
92
		return buffer;
93
	}
94
	convertResult(result: any, URL): Dataset {
95
		const doc = new Dataset();
96

    
97
		doc.title = this.getTitle(result);
98
		doc.headline = doc.title;
99
		if(this.getSubTitle(result)){
100
			doc.alternativeHeadline = this.getSubTitle(result);
101
		}
102
		doc.issn = this.getISSN(result);
103
		doc.description = this.getDescription(result);
104
		doc.identifier = this.getIdentifier(result);
105
		doc.id = URL;
106
		doc.url = URL;
107
		doc.sameAs = this.getSameAs(result);
108
		doc.creator = this.getCreator(result);
109
		doc.dateCreated = this.getDateCreated(result);
110
		doc.citation = this.getCitation(result);
111
		doc.license = this.getLicense(result);
112
		doc.keyword = this.getKeyword(result);
113
		return doc;
114
	}
115

    
116
	convertProject(project: any, URL): Organization {
117
		const doc = new Organization();
118
		doc.title = (project.title)?project.title:project.acronym;
119
		doc.identifier = new Array<Identifier>();
120
		doc.identifier.push({id:project.funding.code, schema: "grantid"});
121
		let funder = new Organization();
122
		funder.id = properties.domain + properties.baseLink+"#funder-"+project.funding.funderShortName;
123
		funder.title = project.funding.funderName;
124
		doc.funder = funder;
125
		doc.url = URL;
126
		doc.id = URL;
127
		doc["description"] = [];
128
		doc["description"].push(project.description?project.description: ("project" + (project.title?"," + project.title:"") + (project.funding && project.funding.funderName?", funder: " + project.funding.funderName:"") + (project.acronym?"," + project.acronym:"")));
129
		doc.sameAs =[project.url];
130
		return doc;
131
}
132

    
133
convertOrganization(organization: any, URL, description:string = null): Organization {
134
	const doc = new Organization();
135

    
136
	doc.title =  organization.title.name ;
137
	doc.legalName = organization.name;
138
	doc.areaServed = organization.country;
139
	doc.url = URL;
140
	doc.id = URL;
141
	doc["description"] = [];
142
	doc["description"].push(((organization.title.name?organization.title.name:"") + " - " + (organization.name?organization.name:"")));
143
	return doc;
144
}
145

    
146
convertDatasource(datasource: any, URL, otherUrl): Organization {
147
	const doc = new Organization();
148

    
149
	doc.title = datasource.title.name?datasource.title.name:datasource.officialName;
150
	//doc.identifier = datasource.contractNum;
151
	doc.legalName = datasource.officialName;
152
	if(datasource.countries && datasource.countries.length > 0){
153
		doc.areaServed = datasource.countries[0];
154
	}
155
	doc.url = URL;
156
	doc.id = URL;
157
	doc["description"] = [];
158
	doc["description"].push(datasource.description?datasource.description:datasource.title.name?datasource.title.name:datasource.officialName);
159

    
160
	if(datasource.oaiPmhURL || otherUrl || datasource.title.url){
161
		doc.sameAs = [];
162
		if(otherUrl){
163
			doc.sameAs.push(otherUrl);
164
		}
165
		if(datasource.oaiPmhURL){
166
			doc.sameAs.push(datasource.oaiPmhURL);
167

    
168
		}
169
		if(datasource.title.url){
170
			doc.sameAs.push(datasource.title.url);
171
		}
172
	}
173
	return doc;
174
}
175

    
176
	private getTitle(result: any): String[] {
177
		const title = _.get(result, "result.metadata.oaf:entity.oaf:result.title", null);
178
		let return_title;
179
		if(title && Array.isArray(title) ){
180
      for(let i=0; i<title.length; i++) {
181
        if(title[i] && title[i].content) {
182
          if(!return_title || title[i].classid == "main title") {
183
            return_title = String(title[i].content);
184
          }
185
          if(title[i].classid == "main title") {
186
            break;
187
          }
188
        }
189
      }
190
      if(!return_title) {
191
        return_title = "";
192
      }
193
      return return_title;
194
			// return (title[0] && title[0].content)?title[0].content:"";
195
		}else{
196
			return (title && title.content)?title.content:"";
197
		}
198

    
199
	}
200
	private getSubTitle(result: any): String[] {
201
		const title = _.get(result, "result.metadata.oaf:entity.oaf:result.title", null);
202
		if(title && Array.isArray(title) ){
203
			if(title[1] && title[1].classid == "subtitle") {
204
				return title[1].content;
205
			}
206
		}
207
		return null;
208
	}
209
	private getISSN(result: any): String[] {
210
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.journal.issn", null);
211
		if (!item) return null;
212
		return [item as String];
213
	}
214
	private getDescription(result: any): String[] {
215
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.description", null);
216
		if (!item) return this.getTitle(result);
217
		let descr = Array.isArray(item) ? item[0]+ "":item + ""; //  +""  to make string: if description is number descr.substring returns error
218
		return [descr?(descr.substring(0,4997)+(descr.substring(0,4997).length == 4997?'...':'')):"" as String];
219
	}
220

    
221
	private getDateCreated(result: any): String[] {
222
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.dateofacceptance", null);
223
		if (!item) return null;
224
		return [item as String];
225
	}
226

    
227
	private getLicense(result: any): License[] {
228
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.bestaccessright", null);
229
		if (!item) return null;
230
		if (!_.has(item, "classid")) return null;
231
		if (!_.has(item, "classname")) return null;
232
		if (!_.has(item, "schemeid")) return null;
233

    
234
		return [{
235
			title: [_.get(item, "classname")],
236
			identifier: [{
237
				id: _.get(item, "classid"),
238
				schema: _.get(item, "schemeid")
239
			}]
240
		}];
241
	}
242

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

    
262
	private getSingleIdentifier(item: any): Identifier {
263
		if (!_.has(item, "classname")) return null;
264
		if (!_.has(item, "content")) return null;
265
		return {
266
			schema: _.get(item, "classname"),
267
			id: _.get(item, "content")
268
		};
269
	}
270

    
271
	private getSameAs(result: any): String[] {
272
		const instances = _.get(result, "result.metadata.oaf:entity.oaf:result.children.instance", null);
273
		if (!instances) return null;
274
		if (!Array.isArray(instances)) return null;
275

    
276
		const array = new Array<String>();
277

    
278
		const instanceArray = instances as Array<any>;
279
		for (var i = 0; i < instanceArray.length; i += 1) {
280
			const webresources = _.get(instanceArray[i], "webresource", null);
281
			if (!webresources) continue;
282
			if (Array.isArray(webresources)) {
283
				const webresourceArray = webresources as Array<any>;
284
				for (var q = 0; q < webresourceArray.length; q += 1) {
285
					const url = _.get(webresourceArray[q], "url", null);
286
					if (!url) continue;
287
					array.push(url as String);
288
				}
289
			}
290
			else {
291
				const url = _.get(webresources, "url", null);
292
				if (!url) continue;
293
				array.push(url as String);
294
			}
295
		}
296
		if (array.length == 0) return null;
297
		return array;
298
	}
299

    
300
	private getKeyword(result: any): String[] {
301
		const subjects = _.get(result, "result.metadata.oaf:entity.oaf:result.subject", null);
302
		if (!subjects) return null;
303
		if (!Array.isArray(subjects)) return null;
304

    
305
		const array = new Array<String>();
306

    
307
		const subjectArray = subjects as Array<any>;
308
		for (var i = 0; i < subjectArray.length; i += 1) {
309
			const classid = _.get(subjectArray[i], "classid", null);
310
			if (classid !== "keyword") continue;
311

    
312
			const sub = _.get(subjectArray[i], "content", null);
313
			if (!sub) return null;
314

    
315
			array.push(sub as String);
316
		}
317
		if (array.length == 0) return null;
318
		return array;
319
	}
320

    
321
	private getCreator(result: any): Person[] {
322
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.creator", null);
323
		if (!item) return null;
324
		const array = new Array<Person>();
325
		if (Array.isArray(item)) {
326
			const itemArray = item as Array<any>;
327
			for (var i = 0; i < itemArray.length; i += 1) {
328
				const val = this.getSinglePerson(itemArray[i]);
329
				if (val) array.push(val);
330
			}
331
		}
332
		else {
333
			const val = this.getSinglePerson(item);
334
			if (val) array.push(val);
335
		}
336
		if (array.length == 0) return null;
337
		return array;
338
	}
339

    
340
	private getSinglePerson(item: any): Person {
341
		if (!_.has(item, "surname") && !_.has(item, "name") && !_.has(item, "content")) return null;
342
		return {
343
			familyName: _.get(item, "surname", null),
344
			givenName: _.get(item, "name", null),
345
			name: _.get(item, "content", null)
346
		};
347
	}
348

    
349
	private getCitation(result: any): Citation[] {
350
		const item = _.get(result, "result.metadata.oaf:entity.extraInfo.citations.citation", null);
351
		if (!item) return null;
352
		const array = new Array<Citation>();
353
		if (Array.isArray(item)) {
354
			const itemArray = item as Array<any>;
355
			for (var i = 0; i < itemArray.length; i += 1) {
356
				const val = this.getSingleCitation(itemArray[i]);
357
				if (val) array.push(val);
358
			}
359
		}
360
		else {
361
			const val = this.getSingleCitation(item);
362
			if (val) array.push(val);
363
		}
364
		if (array.length == 0) return null;
365
		return array;
366
	}
367

    
368
	private getSingleCitation(item: any): Citation {
369
		if (!_.has(item, "rawText")) return null;
370
		if (!_.has(item, "id")) return null;
371

    
372
		const array = new Array<Identifier>();
373

    
374
		const ids = _.get(item, "id", null);
375
		if (Array.isArray(ids)) {
376
			const idsArray = ids as Array<any>;
377

    
378
			for (var i = 0; i < idsArray.length; i += 1) {
379
				const type = _.get(idsArray[i], "type", null);
380
				const value = _.get(idsArray[i], "value", null);
381
				if (!type || !value) continue;
382
				array.push({
383
					id: value,
384
					schema: type
385
				});
386
			}
387
		}
388
		else {
389
			const type = _.get(ids, "type", null);
390
			const value = _.get(ids, "value", null);
391
			if (type && value) {
392
				array.push({
393
					id: value,
394
					schema: type
395
				});
396
			}
397
		}
398

    
399
		if (array.length == 0) return null;
400

    
401
		return {
402
			title: [_.get(item, "rawText")],
403
			identifier: array
404
		};
405
	}
406

    
407

    
408
}
(2-2/2)