2021-05-19 13:40:29 +02:00
import { Component , OnInit , Input , Output , EventEmitter , ViewChild } from '@angular/core' ;
2022-07-04 10:53:55 +02:00
import { ActivatedRoute , Router } from "@angular/router" ;
import { SearchResult } from '../../openaireLibrary/utils/entities/searchResult' ;
2021-05-19 13:40:29 +02:00
import { ErrorCodes } from '../../openaireLibrary/utils/properties/errorCodes' ;
2022-07-04 10:53:55 +02:00
import { SearchUtilsClass } from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class' ;
2021-05-19 13:40:29 +02:00
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties' ;
import { SearchDataprovidersService } from '../../openaireLibrary/services/searchDataproviders.service' ;
import { RouterHelper } from '../../openaireLibrary/utils/routerHelper.class' ;
import { StringUtils } from '../../openaireLibrary/utils/string-utils.class' ;
import { ManageCommunityContentProvidersService } from '../../services/manageContentProviders.service' ;
import { Session } from '../../openaireLibrary/login/utils/helper.class' ;
import { LoginErrorCodes } from '../../openaireLibrary/login/utils/guardHelper.class' ;
import { properties } from "../../../environments/environment" ;
import { FormBuilder , FormGroup } from "@angular/forms" ;
import { SearchInputComponent } from "../../openaireLibrary/sharedComponents/search-input/search-input.component" ;
import { Subscriber } from "rxjs" ;
import { debounceTime , distinctUntilChanged } from "rxjs/operators" ;
import { ResultPreview } from "../../openaireLibrary/utils/result-preview/result-preview" ;
2022-07-04 10:53:55 +02:00
import { NotificationHandler } from "../../openaireLibrary/utils/notification-handler" ;
import { CommunityInfo } from "../../openaireLibrary/connect/community/communityInfo" ;
2022-07-04 20:45:20 +02:00
import { ContentProvider } from "../../openaireLibrary/utils/entities/contentProvider" ;
import { OpenaireEntities } from "../../openaireLibrary/utils/properties/searchFields" ;
import { HelperFunctions } from "../../openaireLibrary/utils/HelperFunctions.class" ;
2021-05-19 13:40:29 +02:00
@Component ( {
2022-07-04 10:53:55 +02:00
selector : 'add-content-providers' ,
templateUrl : './add-content-providers.component.html' ,
2021-05-19 13:40:29 +02:00
} )
export class AddContentProvidersComponent implements OnInit {
private subscriptions : any [ ] = [ ] ;
public subResults : any ;
2022-07-04 10:53:55 +02:00
@Input ( ) public community : CommunityInfo ;
2021-05-19 13:40:29 +02:00
public routerHelper : RouterHelper = new RouterHelper ( ) ;
public properties : EnvProperties = properties ;
public errorCodes : ErrorCodes ;
public openaireSearchUtils : SearchUtilsClass = new SearchUtilsClass ( ) ;
@Output ( ) communityContentProvidersChanged = new EventEmitter ( ) ;
2022-07-04 20:45:20 +02:00
@Input ( ) communityContentProviders : ContentProvider [ ] = [ ] ;
public openaireContentProviders : SearchResult [ ] = [ ] ;
public openAIREEntities = OpenaireEntities ;
2021-05-19 13:40:29 +02:00
public queryParameters : string = "" ;
public resultsPerPage : number = properties . resultsPerPage ;
filterForm : FormGroup ;
@ViewChild ( 'searchInputComponent' ) searchInputComponent : SearchInputComponent ;
2022-07-04 10:53:55 +02:00
public contentProviderUrl : string = "https://" + ( ( properties . environment == "beta" || properties . environment == "development" ) ? "beta." : "" ) + "explore.openaire.eu" + properties . searchLinkToDataProvider ;
2021-05-19 13:40:29 +02:00
public body : string = "Send from page" ;
2022-07-04 10:53:55 +02:00
2021-05-19 13:40:29 +02:00
@Output ( ) toggleView : EventEmitter < any > = new EventEmitter ( ) ;
2022-07-04 10:53:55 +02:00
constructor ( private route : ActivatedRoute , private router : Router ,
private searchDataprovidersService : SearchDataprovidersService ,
private manageCommunityContentProvidersService : ManageCommunityContentProvidersService ,
private fb : FormBuilder ) {
2021-05-19 13:40:29 +02:00
this . errorCodes = new ErrorCodes ( ) ;
this . openaireSearchUtils . status = this . errorCodes . LOADING ;
}
2022-07-04 10:53:55 +02:00
2021-05-19 13:40:29 +02:00
ngOnInit() {
this . subscriptions . push ( this . route . params . subscribe ( params = > {
this . openaireSearchUtils . status = this . errorCodes . LOADING ;
this . _getOpenaireContentProviders ( "" , 1 , this . resultsPerPage ) ;
this . body = "[Please write your message here]" ;
this . body = StringUtils . URIEncode ( this . body ) ;
} ) ) ;
this . openaireSearchUtils . keyword = "" ;
2022-07-04 10:53:55 +02:00
this . filterForm = this . fb . group ( {
2021-05-19 13:40:29 +02:00
keyword : [ '' ] ,
} ) ;
this . subscriptions . push ( this . filterForm . get ( 'keyword' ) . valueChanges
. pipe ( debounceTime ( 1000 ) , distinctUntilChanged ( ) )
. subscribe ( value = > {
this . keywordChanged ( value ) ;
} )
) ;
}
2022-07-04 10:53:55 +02:00
2021-05-19 13:40:29 +02:00
public ngOnDestroy() {
this . subscriptions . forEach ( sub = > {
if ( sub instanceof Subscriber ) {
sub . unsubscribe ( ) ;
}
} ) ;
2022-07-04 10:53:55 +02:00
if ( this . subResults ) {
2021-05-19 13:40:29 +02:00
this . subResults . unsubscribe ( ) ;
}
}
2022-07-04 10:53:55 +02:00
get loading() {
return this . openaireSearchUtils . status == this . errorCodes . LOADING
}
2022-07-11 17:48:24 +02:00
get infoMessage ( ) : string {
return "<div>" +
"If you cannot find a " + OpenaireEntities . DATASOURCE . toLowerCase ( ) + " relevant to your " + OpenaireEntities . COMMUNITY . toLowerCase ( ) + ", probably it is not OpenAIRE compliant." +
"<div>" +
"Feel free to contact us " +
"(<a href=\"mailto: " + properties . feedbackmailForMissingEntities +
"?Subject=[OpenAIRE Connect - " + this . community . shortTitle + "] report missing " + OpenaireEntities . DATASOURCE + "&body=" + this . body + "\" " +
"target=\"_top\">" + properties . feedbackmailForMissingEntities + "</a>) " +
"to let us know and we'll try to get the " + OpenaireEntities . DATASOURCE . toLowerCase ( ) + " on board!" +
"</div>" +
"</div>"
}
2022-07-04 10:53:55 +02:00
2022-07-11 17:48:24 +02:00
public addContentProvider ( contentProvider : SearchResult ) {
this . subscriptions . push ( this . manageCommunityContentProvidersService . addContentProvider ( this . properties , this . community . communityId , contentProvider ) . subscribe (
2022-07-04 10:53:55 +02:00
data = > {
this . communityContentProviders . push ( data ) ;
NotificationHandler . rise ( 'Content Provider successfully added!' )
this . communityContentProvidersChanged . emit ( {
value : this.communityContentProviders ,
} ) ;
} ,
error = > {
this . handleError ( 'An error has been occurred. Try again later!' , error ) ;
}
) ) ;
2021-05-19 13:40:29 +02:00
}
2022-07-04 10:53:55 +02:00
2021-05-19 13:40:29 +02:00
public removeContentProvider ( contentProvider ) {
2022-07-04 10:53:55 +02:00
let communityContentProvider = this . getCommunityContentProvider ( contentProvider ) ;
let contentProviderId : string = communityContentProvider [ 'id' ] ;
this . subscriptions . push ( this . manageCommunityContentProvidersService . removeContentProvider ( this . properties , this . community . communityId , contentProviderId ) . subscribe (
data = > {
let index = this . communityContentProviders . indexOf ( communityContentProvider ) ;
this . communityContentProviders . splice ( index , 1 ) ;
NotificationHandler . rise ( 'Content Provider successfully removed!' )
this . communityContentProvidersChanged . emit ( {
value : this.communityContentProviders ,
} ) ;
} ,
error = > {
this . handleError ( 'An error has been occurred. Try again later!' , error ) ;
}
) ) ;
2021-05-19 13:40:29 +02:00
}
2022-07-04 10:53:55 +02:00
public getCommunityContentProvider ( contentProvider : any ) : any {
2021-05-19 13:40:29 +02:00
let index : number = 0 ;
for ( let communityContentProvider of this . communityContentProviders ) {
if ( communityContentProvider . openaireId == contentProvider . id ) {
return communityContentProvider ;
}
index ++ ;
}
2022-07-04 10:53:55 +02:00
return null ;
2021-05-19 13:40:29 +02:00
}
2022-07-04 10:53:55 +02:00
2021-05-19 13:40:29 +02:00
public getResultPreview ( result : SearchResult ) : ResultPreview {
return ResultPreview . searchResultConvert ( result , "dataprovider" ) ;
}
2022-07-04 10:53:55 +02:00
2021-05-19 13:40:29 +02:00
private _getOpenaireContentProviders ( parameters : string , page : number , size : number ) {
2022-07-04 10:53:55 +02:00
if ( this . openaireSearchUtils . status == this . errorCodes . LOADING ) {
this . openaireSearchUtils . status = this . errorCodes . LOADING ;
this . openaireContentProviders = [ ] ;
this . openaireSearchUtils . totalResults = 0 ;
if ( this . subResults ) {
this . subResults . unsubscribe ( ) ;
2021-05-19 13:40:29 +02:00
}
2022-07-04 10:53:55 +02:00
this . subResults = this . searchDataprovidersService . searchDataproviders ( parameters , null , page , size , [ ] , this . properties ) . subscribe (
data = > {
this . openaireSearchUtils . totalResults = data [ 0 ] ;
this . openaireContentProviders = data [ 1 ] ;
this . openaireSearchUtils . status = this . errorCodes . DONE ;
if ( this . openaireSearchUtils . totalResults == 0 ) {
this . openaireSearchUtils . status = this . errorCodes . NONE ;
}
} ,
error = > {
if ( error . status == '404' ) {
this . handleError ( 'No OpenAIRE content providers found.' , error ) ;
this . openaireSearchUtils . status = this . errorCodes . NOT_FOUND ;
} else if ( error . status == '500' ) {
this . handleError ( 'An Error Occurred. No OpenAIRE content providers found.' , error ) ;
this . openaireSearchUtils . status = this . errorCodes . ERROR ;
} else {
this . handleError ( 'Service temporarily unavailable. Please try again later.' , error ) ;
this . openaireSearchUtils . status = this . errorCodes . NOT_AVAILABLE ;
}
}
) ;
2021-05-19 13:40:29 +02:00
}
}
2022-07-04 10:53:55 +02:00
2021-05-19 13:40:29 +02:00
keywordChanged ( keyword ) {
this . openaireSearchUtils . keyword = keyword ;
this . buildQueryParameters ( ) ;
this . goTo ( 1 ) ;
}
2022-07-04 10:53:55 +02:00
2021-05-19 13:40:29 +02:00
buildQueryParameters() {
this . queryParameters = "" ;
if ( this . openaireSearchUtils . keyword ) {
this . queryParameters = "q=" + StringUtils . URIEncode ( this . openaireSearchUtils . keyword ) ;
}
}
2022-07-04 10:53:55 +02:00
2021-05-19 13:40:29 +02:00
goTo ( page : number = 1 ) {
this . openaireSearchUtils . page = page ;
this . openaireSearchUtils . status = this . errorCodes . LOADING ;
this . _getOpenaireContentProviders ( this . queryParameters , page , this . resultsPerPage ) ;
}
2022-07-04 10:53:55 +02:00
handleError ( message : string , error = null ) {
console . error ( error ) ;
NotificationHandler . rise ( message , 'danger' ) ;
2021-05-19 13:40:29 +02:00
}
}