add schemajsonld annotaion in result landing pages | requires installation of ngx-json-ld

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@52061 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
argiro.kokogiannaki 2018-05-17 08:29:22 +00:00
parent 6507d1de07
commit dacdc76ea1
17 changed files with 502 additions and 11 deletions

View File

@ -10,6 +10,7 @@
<div *ngIf="datasetInfo != null" uk-grid class="uk-grid-large">
<div class="uk-width-expand@m uk-width-1-1@s">
<schema2jsonld *ngIf="datasetInfo.record" [data]=datasetInfo.record></schema2jsonld>
<showTitle [title]="datasetInfo.title"></showTitle>
<span *ngIf="datasetInfo.types.length > 0"class="uk-label custom-label label-dataset " title="Type">{{datasetInfo.types.join(", ")}}</span>
<span *ngIf="datasetInfo.languages && datasetInfo.languages.length > 0" class="uk-label custom-label label-language " title="Language">{{datasetInfo.languages.join(", ")}}</span>

View File

@ -21,6 +21,7 @@ import {DatasetService} from './dataset.service';
import {FreeGuard} from'../../login/freeGuard.guard';
import {IsRouteEnabled} from '../../error/isRouteEnabled.guard';
import {Schema2jsonldModule} from '../landing-utils/schema2jsonld/schema2jsonld.module';
@NgModule({
@ -28,7 +29,7 @@ import {IsRouteEnabled} from '../../error/isRouteEnabled.guard';
//MaterialModule.forRoot(),
CommonModule, FormsModule, SharedModule, RouterModule, LandingModule,
CiteThisModule, ResultLandingModule, MetricsModule, IFrameModule,
AltMetricsModule, ConfigurationServiceModule
AltMetricsModule, ConfigurationServiceModule, Schema2jsonldModule
],
declarations: [
DatasetComponent

View File

@ -29,7 +29,7 @@ export class DatasetService {
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.do(res => console.info(res['result']['metadata']['oaf:entity']))
.map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result']])
.map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result'],res])
.map(res => [res[1],
res[1]['title'],
res[1]['rels']['rel'],
@ -42,7 +42,8 @@ export class DatasetService {
//res[1]['resulttype'],
res[0],
res[1]['creator'],
res[1]['language']
res[1]['language'],
res[2]
]).map(res => this.parseDatasetInfo(res));
}
@ -55,7 +56,7 @@ export class DatasetService {
parseDatasetInfo (data: any):any {
this.datasetInfo = new DatasetInfo();
this.datasetInfo.record = data[12];
if(data[0] != null) {
var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
this.datasetInfo.date = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;

View File

@ -0,0 +1,34 @@
export class JsonldDocument {
title: String[];
description: String[];
identifier: Identifier[];
url: String[];
sameAs: String[];
creator: Person[];
dateCreated: String[];
dateModified: String[];
citation: Citation[];
license: License[];
keyword: String[];
}
export interface Identifier {
schema?: string;
id?: string;
}
export interface Person {
givenName: string;
familyName: string;
name: string;
}
export class Citation {
title: string[];
identifier: Identifier[];
}
export interface License {
title: string[];
identifier: Identifier[];
}

View File

@ -0,0 +1,26 @@
import {Component, ElementRef, Input} from '@angular/core';
import { OpenAireJsonldConverterService } from './service/open-aire-jsonld-converter.service';
import { JsonldDocumentSerializerService } from './service/jsonld-document-serializer.service';
@Component({
selector: 'schema2jsonld',
template: `
<ngx-json-ld [json]="json"></ngx-json-ld>
`
})
export class Schema2jsonldComponent {
@Input() data;
json;
constructor( private documentParser: OpenAireJsonldConverterService,
private documentSerializer: JsonldDocumentSerializerService) {
}
ngOnInit() {
if(typeof window !== 'undefined') {
const docOvject = this.documentParser.convertPublication(this.data);
this.json = this.documentSerializer.serialize(docOvject);
}
}
}

View File

@ -0,0 +1,14 @@
import { NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { OpenAireJsonldConverterService } from './service/open-aire-jsonld-converter.service';
import { JsonldDocumentSerializerService } from './service/jsonld-document-serializer.service';
import {Schema2jsonldComponent} from './schema2jsonld.component';
import { NgxJsonLdModule } from 'ngx-json-ld';
@NgModule({
imports: [CommonModule, FormsModule, NgxJsonLdModule],
declarations: [Schema2jsonldComponent],
providers:[OpenAireJsonldConverterService, JsonldDocumentSerializerService],
exports: [Schema2jsonldComponent]
})
export class Schema2jsonldModule { }

View File

@ -0,0 +1,180 @@
import { Injectable } from "@angular/core";
import { JsonldDocument, Identifier, Person, License, Citation } from "../model/jsonld-document";
import * as _ from "lodash";
@Injectable()
export class JsonldDocumentSerializerService {
constructor() { }
serialize(doc: JsonldDocument): any {
const buffer = {};
buffer["@context"] = "http://schema.org";
buffer["@type"] = "Dataset";
if (doc.title && doc.title.length == 1) {
buffer["name"] = doc.title[0];
}
else if (doc.title && doc.title.length > 1) {
buffer["name"] = doc.title;
}
if (doc.description && doc.description.length == 1) {
buffer["description"] = doc.description[0];
}
else if (doc.description && doc.description.length > 1) {
buffer["description"] = doc.description;
}
if (doc.identifier && doc.identifier.length == 1) {
buffer["identifier"] = this.buildIdentifier(doc.identifier[0]);
}
else if (doc.identifier && doc.identifier.length > 1) {
const array = new Array<any>();
for (var i = 0; i < doc.identifier.length; i += 1) {
array.push(this.buildIdentifier(doc.identifier[i]));
}
buffer["identifier"] = array;
}
if (doc.url && doc.url.length == 1) {
buffer["url"] = doc.url[0];
}
else if (doc.url && doc.url.length > 1) {
buffer["url"] = doc.url;
}
if (doc.sameAs && doc.sameAs.length == 1) {
buffer["sameAs"] = doc.sameAs[0];
}
else if (doc.sameAs && doc.sameAs.length > 1) {
buffer["sameAs"] = doc.sameAs;
}
if (doc.creator && doc.creator.length == 1) {
buffer["creator"] = this.buildCreator(doc.creator[0]);
}
else if (doc.creator && doc.creator.length > 1) {
const array = new Array<any>();
for (var i = 0; i < doc.creator.length; i += 1) {
array.push(this.buildCreator(doc.creator[i]));
}
buffer["creator"] = array;
}
if (doc.dateCreated && doc.dateCreated.length == 1) {
buffer["dateCreated"] = doc.dateCreated[0];
}
else if (doc.dateCreated && doc.dateCreated.length > 1) {
buffer["dateCreated"] = doc.dateCreated;
}
if (doc.dateModified && doc.dateModified.length == 1) {
buffer["dateModified"] = doc.dateModified[0];
}
else if (doc.dateModified && doc.dateModified.length > 1) {
buffer["dateModified"] = doc.dateModified;
}
if (doc.license && doc.license.length == 1) {
buffer["license"] = this.buildLicense(doc.license[0]);
}
else if (doc.license && doc.license.length > 1) {
const array = new Array<any>();
for (var i = 0; i < doc.license.length; i += 1) {
array.push(this.buildLicense(doc.license[i]));
}
buffer["license"] = array;
}
if (doc.keyword && doc.keyword.length == 1) {
buffer["keyword"] = doc.keyword[0];
}
else if (doc.keyword && doc.keyword.length > 1) {
buffer["keyword"] = _.join(doc.keyword, ", ");
}
if (doc.citation && doc.citation.length == 1) {
buffer["citation"] = this.buildCitation(doc.citation[0]);
}
else if (doc.citation && doc.citation.length > 1) {
const array = new Array<any>();
for (var i = 0; i < doc.citation.length; i += 1) {
array.push(this.buildCitation(doc.citation[i]));
}
buffer["citation"] = array;
}
return buffer;
// return JSON.stringify(buffer, (key, value) => {
// if (value !== null) return value
// }, 4);
}
buildIdentifier(item: Identifier): any {
return {
"@type": "PropertyValue",
"propertyID": item.schema,
"value": item.id
};
}
buildCreator(item: Person): any {
return {
"@type": "Person",
"givenName": item.givenName,
"familyName": item.familyName,
"name": item.name
};
}
buildLicense(item: License): any {
const licenseBuffer = {
"@type": "CreativeWork"
};
if (item.title && item.title.length == 1) {
licenseBuffer["name"] = item.title[0];
}
else if (item.title && item.title.length > 1) {
licenseBuffer["name"] = item.title;
}
if (item.identifier && item.identifier.length == 1) {
licenseBuffer["identifier"] = this.buildIdentifier(item.identifier[0]);
}
else if (item.identifier && item.identifier.length > 1) {
const array = new Array<any>();
for (var i = 0; i < item.identifier.length; i += 1) {
array.push(this.buildIdentifier(item.identifier[i]));
}
licenseBuffer["identifier"] = array;
}
return licenseBuffer;
}
buildCitation(item: Citation): any {
const citationBuffer = {
"@type": "CreativeWork"
};
if (item.title && item.title.length == 1) {
citationBuffer["name"] = item.title[0];
}
else if (item.title && item.title.length > 1) {
citationBuffer["name"] = item.title;
}
if (item.identifier && item.identifier.length == 1) {
citationBuffer["identifier"] = this.buildIdentifier(item.identifier[0]);
}
else if (item.identifier && item.identifier.length > 1) {
const array = new Array<any>();
for (var i = 0; i < item.identifier.length; i += 1) {
array.push(this.buildIdentifier(item.identifier[i]));
}
citationBuffer["identifier"] = array;
}
return citationBuffer;
}
}

View File

@ -0,0 +1,225 @@
import { Injectable } from "@angular/core";
import { JsonldDocument, Identifier, Person, License, Citation } from "../model/jsonld-document";
import * as _ from "lodash";
@Injectable()
export class OpenAireJsonldConverterService {
constructor() { }
convertPublication(result: any): JsonldDocument {
const doc = new JsonldDocument();
doc.title = this.getTitle(result);
doc.description = this.getDescription(result);
doc.identifier = this.getIdentifier(result);
doc.url = [window.location.toString()];
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;
}
private getTitle(result: any): String[] {
const item = _.get(result, "result.metadata.oaf:entity.oaf:result.title.content", 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
};
}
}

View File

@ -9,6 +9,7 @@
<div *ngIf="publicationInfo != null" uk-grid class="uk-grid-large">
<div class="uk-width-expand@m uk-width-1-1@s">
<schema2jsonld *ngIf="publicationInfo.record" [data]=publicationInfo.record></schema2jsonld>
<showTitle [title]="publicationInfo.title"></showTitle>
<span *ngIf="publicationInfo.types.length > 0"class="uk-label custom-label label-blue label-publication" title="Type">{{publicationInfo.types.join(", ")}}</span>
<span *ngIf="publicationInfo.languages.length > 0" class="uk-label custom-label label-language " title="Language">{{publicationInfo.languages.join(", ")}}</span>

View File

@ -19,13 +19,14 @@ import {FreeGuard} from'../../login/freeGuard.guard';
import {IsRouteEnabled} from '../../error/isRouteEnabled.guard';
import {PublicationService} from './publication.service';
import {PublicationComponent} from './publication.component';
import {Schema2jsonldModule} from '../landing-utils/schema2jsonld/schema2jsonld.module';
@NgModule({
imports: [
CommonModule, FormsModule, LandingModule, SharedModule, RouterModule,
CiteThisModule, PagingModule, ResultLandingModule, IFrameModule,
MetricsModule, AltMetricsModule, ConfigurationServiceModule
MetricsModule, AltMetricsModule, ConfigurationServiceModule, Schema2jsonldModule
],
declarations: [
PublicationComponent

View File

@ -29,7 +29,7 @@ export class PublicationService {
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']])
.map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity'], res])
.map(res => [ res[1]['oaf:result'],
res[1]['oaf:result']['title'],
res[1]['oaf:result']['rels']['rel'],
@ -43,7 +43,8 @@ export class PublicationService {
(res[1]['extraInfo']!= undefined && res[1]['extraInfo']['citations']!= undefined)? res[1]['extraInfo']['citations']['citation']:null,
res[1]['oaf:result']['context'],
res[0],
res[1]['oaf:result']['creator']
res[1]['oaf:result']['creator'],
res[2]
])
.map(res => this.parsePublicationInfo(res));
}
@ -57,7 +58,7 @@ export class PublicationService {
parsePublicationInfo (data: any):any {
this.publicationInfo = new PublicationInfo();
this.publicationInfo.record = data[14];
if(data[0] != null) {
var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
this.publicationInfo.date = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;

View File

@ -10,6 +10,7 @@
<div *ngIf="softwareInfo != null" uk-grid class="uk-grid-large">
<div class="uk-width-expand@m uk-width-1-1@s">
<schema2jsonld *ngIf="softwareInfo.record" [data]=softwareInfo.record></schema2jsonld>
<showTitle [title]="softwareInfo.title"></showTitle>
<span *ngIf="softwareInfo.types.length > 0"class="uk-label custom-label label-software " title="Type">{{softwareInfo.types.join(", ")}}</span>
<span *ngIf="softwareInfo.languages && softwareInfo.languages.length > 0" class="uk-label custom-label label-language " title="Language">{{softwareInfo.languages.join(", ")}}</span>

View File

@ -17,11 +17,12 @@ import { PagingModule } from '../../utils/paging.module';
import { ResultLandingModule } from '../landing-utils/resultLanding.module';
import { LandingModule } from '../landing-utils/landing.module';
import { FreeGuard } from'../../login/freeGuard.guard';
import {Schema2jsonldModule} from '../landing-utils/schema2jsonld/schema2jsonld.module';
@NgModule({
imports: [
CommonModule, FormsModule, LandingModule, RouterModule, CiteThisModule, PagingModule,
ResultLandingModule, IFrameModule, MetricsModule, AltMetricsModule
ResultLandingModule, IFrameModule, MetricsModule, AltMetricsModule, Schema2jsonldModule
],
declarations: [
SoftwareComponent

View File

@ -29,7 +29,7 @@ export class SoftwareService {
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.do(res => console.info(res['result']['metadata']['oaf:entity']))
.map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result']])
.map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result'], res])
.map(res => [res[1],
res[1]['title'],
res[1]['rels']['rel'],
@ -43,6 +43,7 @@ export class SoftwareService {
res[0],
res[1]['creator'],
res[1]['language'],
res[2]
]).map(res => this.parseSoftwareInfo(res));
}
@ -55,7 +56,7 @@ export class SoftwareService {
parseSoftwareInfo (data: any):any {
this.softwareInfo = new SoftwareInfo();
this.softwareInfo.record = data[12];
if(data[0] != null) {
var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
this.softwareInfo.date = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;

View File

@ -42,4 +42,5 @@ export class DatasetInfo {
relatedResearchResults: Map<string, { "name": string, "id": string, "date": string, "trust": number, "class": string}[]>;
similarResearchResults: { "name": string, "id": string, "date": string, "trust": number, "class": string}[];
contexts: { "labelContext": string, "labelCategory": string, "labelConcept": string, "inline": boolean}[];
record;
}

View File

@ -55,4 +55,5 @@ export class PublicationInfo {
references: { "name": string, "url": string}[];
contexts: { "labelContext": string, "labelCategory": string, "labelConcept": string, "inline": boolean}[];
organizations: {"name": string, "shortname":string, "id": string, "websiteUrl": string, "country": string, "trust": number}[];
record;
}

View File

@ -42,4 +42,5 @@ export class SoftwareInfo {
relatedResearchResults: Map<string, { "name": string, "id": string, "date": string, "trust": number, "class": string}[]>;
similarResearchResults: { "name": string, "id": string, "date": string, "trust": number, "class": string}[];
contexts: { "labelContext": string, "labelCategory": string, "labelConcept": string, "inline": boolean}[];
record;
}