openaire-library/sharedComponents/schema2jsonld/service/open-aire-jsonld-converter....

346 lines
9.6 KiB
TypeScript

import { Injectable } from "@angular/core";
import { JsonldDocument, Identifier, Person, License, Citation, Dataset, Organization } from "../model/jsonld-document";
import * as _ from "lodash";
@Injectable()
export class OpenAireJsonldConverterService {
constructor() { }
createHome(name, URL, logoURL): any {
const buffer = {};
buffer["@context"] = "http://schema.org";
buffer["@type"] = "Organization";
buffer["name"] = name;
buffer["url"] = URL;
buffer["logo"] = logoURL;
const action ={};
action["@type"]= "SearchAction";
action["target"]= URL+"/search/find/?keyword={search_term_string}";
action["query-input"]= "required name=search_term_string";
buffer["potentialAction"] = action;
const sameAs =["https://www.openaire.eu",
"https://www.facebook.com/groups/openaire/",
"https://www.twitter.com/OpenAIRE_eu",
"https://www.linkedin.com/groups/OpenAIRE-3893548",
"https://www.slideshare.net/OpenAIRE_eu",
"https://www.youtube.com/channel/UChFYqizc-S6asNjQSoWuwjw"];
buffer["sameAs"] = sameAs;
return buffer;
}
createSimplePage(name, URL): any {
const buffer = {};
buffer["@context"] = "http://schema.org";
buffer["@type"] = "WebPage";
buffer["name"] = name;
buffer["url"] = URL;
return buffer;
}
createSearchPage(name, URL, logoURL, searchAction:boolean = true): any {
const buffer = {};
buffer["@context"] = "http://schema.org";
buffer["@type"] = "SearchResultsPage";
buffer["name"] = name;
buffer["url"] = URL;
buffer["logo"] = logoURL;
if(searchAction){
const action ={};
action["@type"]= "SearchAction";
action["target"]= URL+"?keyword={search_term_string}";
action["query-input"]= "required name=search_term_string";
buffer["potentialAction"] = action;
}
return buffer;
}
convertResult(result: any, URL): Dataset {
const doc = new Dataset();
doc.title = this.getTitle(result);
doc.headline = doc.title;
if(this.getSubTitle(result)){
doc.alternativeHeadline = this.getSubTitle(result);
}
doc.issn = this.getISSN(result);
doc.description = this.getDescription(result);
doc.identifier = this.getIdentifier(result);
doc.url = URL;
doc.sameAs = this.getSameAs(result);
doc.creator = this.getCreator(result);
doc.dateCreated = this.getDateCreated(result);
doc.citation = this.getCitation(result);
doc.license = this.getLicense(result);
doc.keyword = this.getKeyword(result);
return doc;
}
convertProject(project: any, URL, otherUrl): Organization {
const doc = new Organization();
doc.title = (project.name)?project.title:project.acronym;
doc.identifier = new Array<Identifier>();
doc.identifier.push({id:project.contractNum, schema: "grantid"});
var funder = new Organization();
funder.title = project.funder;
doc.funder = funder;
doc.url = URL;
doc.sameAs =[project.url]
return doc;
}
convertOrganization(organization: any, URL): Organization {
const doc = new Organization();
doc.title = organization.title.name ;
doc.legalName = organization.name;
doc.areaServed = organization.country;
doc.url = URL;
return doc;
}
convertDatasource(datasource: any, URL, otherUrl): Organization {
const doc = new Organization();
doc.title = datasource.title.name
doc.identifier = datasource.contractNum;
doc.legalName = datasource.officialName;
if(datasource.countries && datasource.countries.length > 0){
doc.areaServed = datasource.countries[0];
}
doc.url = URL;
if(datasource.oaiPmhURL || otherUrl || datasource.title.url){
doc.sameAs = [];
if(otherUrl){
doc.sameAs.push(otherUrl);
}
if(datasource.oaiPmhURL){
doc.sameAs.push(datasource.oaiPmhURL);
}
if(datasource.title.url){
doc.sameAs.push(datasource.title.url);
}
}
return doc;
}
private getTitle(result: any): String[] {
const title = _.get(result, "result.metadata.oaf:entity.oaf:result.title", null);
if(title && Array.isArray(title) ){
return title[0].content;
}else{
return title.content;
}
}
private getSubTitle(result: any): String[] {
const title = _.get(result, "result.metadata.oaf:entity.oaf:result.title", null);
if(title && Array.isArray(title) ){
if(title[1] && title[1].classid == "subtitle") {
return title[1].content;
}
}
return null;
}
private getISSN(result: any): String[] {
const item = _.get(result, "result.metadata.oaf:entity.oaf:result.journal.issn", null);
if (!item) return null;
return [item as String];
}
private getDescription(result: any): String[] {
const item = _.get(result, "result.metadata.oaf:entity.oaf:result.description", null);
if (!item) return null;
return [item as String];
}
private getDateCreated(result: any): String[] {
const item = _.get(result, "result.metadata.oaf:entity.oaf:result.dateofacceptance", null);
if (!item) return null;
return [item as String];
}
private getLicense(result: any): License[] {
const item = _.get(result, "result.metadata.oaf:entity.oaf:result.bestaccessright", null);
if (!item) return null;
if (!_.has(item, "classid")) return null;
if (!_.has(item, "classname")) return null;
if (!_.has(item, "schemeid")) return null;
return [{
title: [_.get(item, "classname")],
identifier: [{
id: _.get(item, "classid"),
schema: _.get(item, "schemeid")
}]
}];
}
private getIdentifier(result: any): Identifier[] {
const item = _.get(result, "result.metadata.oaf:entity.oaf:result.pid", null);
if (!item) return null;
const array = new Array<Identifier>();
if (Array.isArray(item)) {
const itemArray = item as Array<any>;
for (var i = 0; i < itemArray.length; i += 1) {
const val = this.getSingleIdentifier(itemArray[i]);
if (val) array.push(val);
}
}
else {
const val = this.getSingleIdentifier(item);
if (val) array.push(val);
}
if (array.length == 0) return null;
return array;
}
private getSingleIdentifier(item: any): Identifier {
if (!_.has(item, "classname")) return null;
if (!_.has(item, "content")) return null;
return {
schema: _.get(item, "classname"),
id: _.get(item, "content")
};
}
private getSameAs(result: any): String[] {
const instances = _.get(result, "result.metadata.oaf:entity.oaf:result.children.instance", null);
if (!instances) return null;
if (!Array.isArray(instances)) return null;
const array = new Array<String>();
const instanceArray = instances as Array<any>;
for (var i = 0; i < instanceArray.length; i += 1) {
const webresources = _.get(instanceArray[i], "webresource", null);
if (!webresources) continue;
if (Array.isArray(webresources)) {
const webresourceArray = webresources as Array<any>;
for (var q = 0; q < webresourceArray.length; q += 1) {
const url = _.get(webresourceArray[q], "url", null);
if (!url) continue;
array.push(url as String);
}
}
else {
const url = _.get(webresources, "url", null);
if (!url) continue;
array.push(url as String);
}
}
if (array.length == 0) return null;
return array;
}
private getKeyword(result: any): String[] {
const subjects = _.get(result, "result.metadata.oaf:entity.oaf:result.subject", null);
if (!subjects) return null;
if (!Array.isArray(subjects)) return null;
const array = new Array<String>();
const subjectArray = subjects as Array<any>;
for (var i = 0; i < subjectArray.length; i += 1) {
const classid = _.get(subjectArray[i], "classid", null);
if (classid !== "keyword") continue;
const sub = _.get(subjectArray[i], "content", null);
if (!sub) return null;
array.push(sub as String);
}
if (array.length == 0) return null;
return array;
}
private getCreator(result: any): Person[] {
const item = _.get(result, "result.metadata.oaf:entity.oaf:result.creator", null);
if (!item) return null;
const array = new Array<Person>();
if (Array.isArray(item)) {
const itemArray = item as Array<any>;
for (var i = 0; i < itemArray.length; i += 1) {
const val = this.getSinglePerson(itemArray[i]);
if (val) array.push(val);
}
}
else {
const val = this.getSinglePerson(item);
if (val) array.push(val);
}
if (array.length == 0) return null;
return array;
}
private getSinglePerson(item: any): Person {
if (!_.has(item, "surname") && !_.has(item, "name") && !_.has(item, "content")) return null;
return {
familyName: _.get(item, "surname", null),
givenName: _.get(item, "name", null),
name: _.get(item, "content", null)
};
}
private getCitation(result: any): Citation[] {
const item = _.get(result, "result.metadata.oaf:entity.extraInfo.citations.citation", null);
if (!item) return null;
const array = new Array<Citation>();
if (Array.isArray(item)) {
const itemArray = item as Array<any>;
for (var i = 0; i < itemArray.length; i += 1) {
const val = this.getSingleCitation(itemArray[i]);
if (val) array.push(val);
}
}
else {
const val = this.getSingleCitation(item);
if (val) array.push(val);
}
if (array.length == 0) return null;
return array;
}
private getSingleCitation(item: any): Citation {
if (!_.has(item, "rawText")) return null;
if (!_.has(item, "id")) return null;
const array = new Array<Identifier>();
const ids = _.get(item, "id", null);
if (Array.isArray(ids)) {
const idsArray = ids as Array<any>;
for (var i = 0; i < idsArray.length; i += 1) {
const type = _.get(idsArray[i], "type", null);
const value = _.get(idsArray[i], "value", null);
if (!type || !value) continue;
array.push({
id: value,
schema: type
});
}
}
else {
const type = _.get(ids, "type", null);
const value = _.get(ids, "value", null);
if (type && value) {
array.push({
id: value,
schema: type
});
}
}
if (array.length == 0) return null;
return {
title: [_.get(item, "rawText")],
identifier: array
};
}
}