From 9f569ac4c469c9474d8a19150f9693f1be9d0eac Mon Sep 17 00:00:00 2001 From: "konstantina.galouni" Date: Sun, 12 Jul 2020 22:10:43 +0000 Subject: [PATCH] [Trunk | Library]: 1. resultLanding.component.ts: Get provenanceAction vocabulary from ISVocabulariesService. 2. resultLanding.service.ts: Remove methods for "provenanceAction" vocabulary | [Bug fix] Do not stringify "dateofacceptance" when undefined. 3. ISVocabularies.service.ts: Add BehaviorSubjects for vocabularies | Add methods for provenanceAction vocabulary (called by result landing pages). 4. staticAutoComplete.module.ts: Remove ISVocabulariesService from providers (singleton service, providedIn: 'root'). git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59072 d315682c-612b-4755-9ff5-7f18f6832af3 --- .../result/resultLanding.component.ts | 4 +- landingPages/result/resultLanding.service.ts | 32 +++--- .../ISVocabularies.service.ts | 101 ++++++++++++++---- .../staticAutoComplete.module.ts | 4 +- 4 files changed, 102 insertions(+), 39 deletions(-) diff --git a/landingPages/result/resultLanding.component.ts b/landingPages/result/resultLanding.component.ts index efd06a35..083eacee 100644 --- a/landingPages/result/resultLanding.component.ts +++ b/landingPages/result/resultLanding.component.ts @@ -18,6 +18,7 @@ import {IndexInfoService} from "../../utils/indexInfo.service"; import {FormBuilder, FormGroup} from "@angular/forms"; import {StringUtils} from "../../utils/string-utils.class"; import {properties} from "../../../../environments/environment"; +import {ISVocabulariesService} from "../../utils/staticAutoComplete/ISVocabularies.service"; @Component({ @@ -102,6 +103,7 @@ export class ResultLandingComponent { 'Persistent identifiers', 'Other']; constructor(private _resultLaningService: ResultLandingService, + private _vocabulariesService: ISVocabulariesService, private _piwikService: PiwikService, private route: ActivatedRoute, private router: Router, @@ -262,7 +264,7 @@ export class ResultLandingComponent { this.resultLandingInfo = null; if (typeof document !== 'undefined') { - this._resultLaningService.getProvenanceActionVocabulary(this.properties).subscribe( + this._vocabulariesService.getProvenanceActionVocabulary(this.properties).subscribe( provenanceActionVocabulary => { this.getResultLandingInfo(provenanceActionVocabulary); }, err => { diff --git a/landingPages/result/resultLanding.service.ts b/landingPages/result/resultLanding.service.ts index 71f43e7d..b73bb932 100644 --- a/landingPages/result/resultLanding.service.ts +++ b/landingPages/result/resultLanding.service.ts @@ -56,21 +56,21 @@ export class ResultLandingService { .pipe(map(res => this.parseResultLandingInfo(res, provenanceActionVocabulary, properties))); } - getProvenanceActionVocabulary (properties: EnvProperties): any { - let url = properties.vocabulariesAPI+"dnet:provenanceActions.json"; - - return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) - .pipe(map(res => res['terms'])) - .pipe(map(res => this.parseProvenanceActionVocabulary(res, properties))); - } - - parseProvenanceActionVocabulary(terms: any, properties: EnvProperties) { - var provenanceActionVocabulary: {} = {}; - for(let term of terms) { - provenanceActionVocabulary[term.code] = term.englishName; - } - return provenanceActionVocabulary; - } + // getProvenanceActionVocabulary (properties: EnvProperties): any { + // let url = properties.vocabulariesAPI+"dnet:provenanceActions.json"; + // + // return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) + // .pipe(map(res => res['terms'])) + // .pipe(map(res => this.parseProvenanceActionVocabulary(res, properties))); + // } + // + // parseProvenanceActionVocabulary(terms: any, properties: EnvProperties) { + // var provenanceActionVocabulary: {} = {}; + // for(let term of terms) { + // provenanceActionVocabulary[term.code] = term.englishName; + // } + // return provenanceActionVocabulary; + // } private handleError (error: HttpErrorResponse) { // in a real world app, we may send the error to some remote logging infrastructure @@ -88,7 +88,7 @@ export class ResultLandingService { // res['result']['metadata']['oaf:entity']['oaf:result'] if (data[0] != null) { - let date: string = (data[0].dateofacceptance) + ''; // transform to string in case it is an integer + let date: string = (data[0].dateofacceptance ? data[0].dateofacceptance : '') + ''; // transform to string in case it is an integer this.resultLandingInfo.date = (date && (date).indexOf('-') !== -1) ? date.split('-')[0] : date; this.resultLandingInfo.dateofacceptance = data[0].dateofacceptance?new Date(data[0].dateofacceptance):null; this.resultLandingInfo.publisher = data[0].publisher; diff --git a/utils/staticAutoComplete/ISVocabularies.service.ts b/utils/staticAutoComplete/ISVocabularies.service.ts index cd016fc6..feb92289 100644 --- a/utils/staticAutoComplete/ISVocabularies.service.ts +++ b/utils/staticAutoComplete/ISVocabularies.service.ts @@ -1,18 +1,20 @@ import {Injectable} from '@angular/core'; import {HttpClient, HttpErrorResponse} from "@angular/common/http"; -import {Observable, throwError, of} from 'rxjs'; +import {Observable, throwError, of, BehaviorSubject, from} from 'rxjs'; import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class'; import 'rxjs/add/observable/zip'; import {EnvProperties} from '../properties/env-properties'; import {catchError, map} from "rxjs/operators"; -@Injectable() +@Injectable({ providedIn: 'root' }) export class ISVocabulariesService { - constructor(private http: HttpClient) { - } + private vocabularies: Map> = new Map>(); + private provenanceActionVocabulary: BehaviorSubject<{}> = new BehaviorSubject(null); + + constructor(private http: HttpClient) {} - getVocabularyByType(field: string, entity: string, properties: EnvProperties): any { + getVocabularyByType(field: string, entity: string, properties: EnvProperties): Observable { //console.log("getVocabulary field: "+ field + " for entity: "+ entity); var file = ""; var vocabulary = ""; @@ -20,55 +22,77 @@ export class ISVocabulariesService { // file="languages.json"; // return this.getVocabularyFromFile(file); vocabulary = "dnet:languages.json"; - return this.getVocabularyFromService(vocabulary, properties); + //return this.getVocabularyFromService(vocabulary, properties); + return from(this.getVocabularyFromServiceAsync(vocabulary, properties)); } else if (field == "type" && (entity == "publication")) { // file = "publicationTypes.json"; // return this.getVocabularyFromFile(file); vocabulary = "dnet:publication_resource.json"; - return this.getVocabularyFromService(vocabulary, properties); - + //return this.getVocabularyFromService(vocabulary, properties); + return from(this.getVocabularyFromServiceAsync(vocabulary, properties)); } else if (field == "type" && (entity == "dataset")) { // file = "dnet:dataCite_resource.json"; // return this.getVocabularyFromFile(file); vocabulary = "dnet:dataCite_resource.json"; - return this.getVocabularyFromService(vocabulary, properties); - + //return this.getVocabularyFromService(vocabulary, properties); + return from(this.getVocabularyFromServiceAsync(vocabulary, properties)); } else if (field == "type" && (entity == "software" || entity == "other")) { return of([]); } else if (field == "type" && entity == "result" ) { - return Observable.zip(this.getVocabularyFromService("dnet:publication_resource.json", properties),this.getVocabularyFromService("dnet:dataCite_resource.json", properties)); + //return Observable.zip(this.getVocabularyFromService("dnet:publication_resource.json", properties),this.getVocabularyFromService("dnet:dataCite_resource.json", properties)); + return Observable.zip(from(this.getVocabularyFromServiceAsync("dnet:publication_resource.json", properties)),from(this.getVocabularyFromServiceAsync("dnet:dataCite_resource.json", properties))); } else if (field == "access" && (entity == "publication" || entity == "dataset" || entity == "software" || entity == "other" || entity == "result")) { // file= "accessMode.json"; // return this.getVocabularyFromFile(file); vocabulary = "dnet:access_modes.json"; - return this.getVocabularyFromService(vocabulary, properties); - + //return this.getVocabularyFromService(vocabulary, properties); + return from(this.getVocabularyFromServiceAsync(vocabulary, properties)); } else if ((field == "type") && (entity == "dataprovider")) { // file = "dataProviderType.json"; // return this.getVocabularyFromFile(file); vocabulary = "dnet:datasource_typologies.json"; - return this.getVocabularyFromService(vocabulary, properties); + //return this.getVocabularyFromService(vocabulary, properties); + return from(this.getVocabularyFromServiceAsync(vocabulary, properties)); } else if (field == "compatibility" && (entity == "dataprovider")) { // file = "dataProviderCompatibility.json"; // return this.getVocabularyFromFile(file); vocabulary = "dnet:datasourceCompatibilityLevel.json"; - return this.getVocabularyFromService(vocabulary, properties); + //return this.getVocabularyFromService(vocabulary, properties); + return from(this.getVocabularyFromServiceAsync(vocabulary, properties)); } else if (field == "country") { // file = "countries.json"; // return this.getVocabularyFromFile(file); vocabulary = "dnet:countries.json"; - return this.getVocabularyFromService(vocabulary, properties); + //return this.getVocabularyFromService(vocabulary, properties); + return from(this.getVocabularyFromServiceAsync(vocabulary, properties)); } return null; } - - getVocabularyFromService(vocabularyName: string, properties: EnvProperties): any { + + async getVocabularyFromServiceAsync(vocabularyName: string, properties: EnvProperties): Promise { + if(!this.vocabularies.has(vocabularyName)) { + await new Promise(resolve => { + this.vocabularies.set(vocabularyName, new BehaviorSubject(null)); + + this.getVocabularyFromService(vocabularyName, properties).subscribe( + vocabularyRes => { + this.vocabularies.get(vocabularyName).next(vocabularyRes); + resolve(); + } + ); + }); + } + + return this.vocabularies.get(vocabularyName).getValue(); + } + + getVocabularyFromService(vocabularyName: string, properties: EnvProperties): Observable { let url = properties.vocabulariesAPI + vocabularyName; - return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) + return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) //.map(res => res.json()) .pipe(map(res => res['terms'])) .pipe(map(res => this.parse(res, vocabularyName))) @@ -91,7 +115,44 @@ export class ISVocabulariesService { return array; } - + + getProvenanceActionVocabulary(properties: EnvProperties): Observable { + let vocabulary = "dnet:provenanceActions.json"; + return from(this.getProvenanceActionVocabularyFromServiceAsync(vocabulary, properties)); + } + + async getProvenanceActionVocabularyFromServiceAsync (vocabularyName: string, properties: EnvProperties): Promise<{}> { + if(!this.provenanceActionVocabulary || !this.provenanceActionVocabulary.getValue()) { + await new Promise(resolve => { + this.getProvenanceActionVocabularyFromService(vocabularyName, properties).subscribe( + vocabularyRes => { + this.provenanceActionVocabulary.next(vocabularyRes); + resolve(); + } + ); + }); + } +console.debug(this.provenanceActionVocabulary.getValue()); + return this.provenanceActionVocabulary.getValue(); + } + + getProvenanceActionVocabularyFromService (vocabularyName: string, properties: EnvProperties): any { + let url = properties.vocabulariesAPI+vocabularyName; + + return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) + .pipe(map(res => res['terms'])) + .pipe(map(res => this.parseProvenanceActionVocabulary(res))); + } + + parseProvenanceActionVocabulary(terms: any) { + var provenanceActionVocabulary: {} = {}; + for(let term of terms) { + provenanceActionVocabulary[term.code] = term.englishName; + } + return provenanceActionVocabulary; + } + + private handleError(error: HttpErrorResponse) { // in a real world app, we may send the error to some remote logging infrastructure // instead of just logging it to the console diff --git a/utils/staticAutoComplete/staticAutoComplete.module.ts b/utils/staticAutoComplete/staticAutoComplete.module.ts index 1c876a15..dbd5395f 100644 --- a/utils/staticAutoComplete/staticAutoComplete.module.ts +++ b/utils/staticAutoComplete/staticAutoComplete.module.ts @@ -4,7 +4,7 @@ import { FormsModule } from '@angular/forms'; import {StaticAutoCompleteComponent} from './staticAutoComplete.component'; import {RefineFieldResultsServiceModule} from '../../services/refineFieldResultsService.module'; -import {ISVocabulariesService} from './ISVocabularies.service'; +//import {ISVocabulariesService} from './ISVocabularies.service'; @NgModule({ @@ -17,6 +17,6 @@ import {ISVocabulariesService} from './ISVocabularies.service'; exports: [ StaticAutoCompleteComponent ], - providers:[ ISVocabulariesService] + providers:[ /*ISVocabulariesService*/ ] }) export class StaticAutocompleteModule { }