import {Component, Input} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {ErrorCodes} from '../utils/properties/errorCodes';
import { Router, ActivatedRoute } from '@angular/router';
import { FetchDataproviders } from '../utils/fetchEntitiesClasses/fetchDataproviders.class';
import { SearchDataprovidersService } from '../services/searchDataproviders.service';
import {OrganizationService} from '../services/organization.service';
import { Meta} from '../sharedComponents/metaService';
import {RouterHelper} from '../utils/routerHelper.class';
import {PiwikService} from '../utils/piwik/piwik.service';
import{EnvProperties} from '../utils/properties/env-properties';
@Component({
selector: 'deposit-result',
template: `
0">
Please use the information below and contact your repository to deposit your {{requestFor}}.
No organization with ID: {{organizationId}} found.
An error occured.
Service temprorarily unavailable. Please try again later.
No ID for organization.
You can still deposit your {{requestFor}} in
OpenAIRE's Zenodo catch-all repository ()
hosted by CERN.
`
})
export class DepositResultComponent {
@Input() compatibility: string = '';
// Type of entity: Publication or Research Data
@Input() requestFor: string = "Publications";
public organization: {"name": string, "url": string};
public organizationId: string = "";
// Id of the new selected organization to be searched
public selectedId: string = "";
public status: number;
public warningMessage: string = "";
public fetchDataproviders : FetchDataproviders;
public linkToSearchDataproviders: string = "";
// url of Zenodo
public zenodo: string;
public routerHelper:RouterHelper = new RouterHelper();
public errorCodes:ErrorCodes = new ErrorCodes();
sub: any; piwiksub: any;
properties:EnvProperties;
constructor ( private _router: Router,
private route: ActivatedRoute,
private _searchDataprovidersService: SearchDataprovidersService,
private _organizationService: OrganizationService,
private _meta: Meta, private _piwikService:PiwikService) {
this.fetchDataproviders = new FetchDataproviders(this._searchDataprovidersService);
this.status = this.errorCodes.LOADING;
this.updateTitle("Deposit "+this.requestFor);
this.updateDescription("Openaire, repositories, open access, content provider, compatibility, organization, deposit "+ this.requestFor);
}
ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
this.zenodo = this.properties.zenodo;
this.updateUrl(data.envSpecific.baseLink+this._router.url);
if(this.properties.enablePiwikTrack && (typeof document !== 'undefined')){
this.piwiksub = this._piwikService.trackView(this.properties, "Deposit "+this.requestFor).subscribe();
}
});
console.info('depositResult init');
this.sub = this.route.queryParams.subscribe(params => {
this.organizationId = params['organizationId'];
console.info("Id is :"+this.organizationId);
if(this.organizationId){
this.getOrganizationInfo();
}
this.selectedId = "";
});
}
ngDoCheck() {
if(this.organizationId == "" || this.organizationId == undefined) {
this.organizationId = "";
this.status = this.errorCodes.ERROR;
}
}
ngOnDestroy() {
this.sub.unsubscribe();
if(this.piwiksub){
this.piwiksub.unsubscribe();
}
}
private searchDataproviders() {
// if(this.organization != undefined) {
// this.fetchDataproviders.getResults(this.organization.name, false, 1, 10);
// } else if(this.organizationId != undefined) {
this.fetchDataproviders.getResultsForDeposit( this.organizationId,this.requestFor, 1, 10, this.properties);
//}
this.linkToSearchDataproviders = this.properties.searchLinkToDataProviders;
}
private getOrganizationInfo () {
console.info("inside getOrganizationInfo of component");
this._organizationService.getOrganizationInfo(this.organizationId,this.properties).subscribe(
data => {
if(data == null) {
this.status = this.errorCodes.NOT_FOUND;
} else {
this.organization = data.title;
this.status = this.errorCodes.DONE;
this.searchDataproviders();
}
},
err => {
//console.log(err)
if(err.status == '404') {
this.status = this.errorCodes.NOT_FOUND;
console.info("not found");
} else if(err.status == '500') {
this.status = this.errorCodes.ERROR;
console.info("error");
} else {
this.status = this.errorCodes.NOT_AVAILABLE;
console.info("not available");
}
}
);
}
public goToDeposit() {
if(this.requestFor == "Publications") {
this._router.navigate( ['participate/deposit-publications'] );
} else if(this.requestFor == "Research Data") {
this._router.navigate( ['participate/deposit-datasets'] );
}
}
public valueChanged($event){
this.selectedId = $event.value;
}
public organizationSelected(id: string) {
console.info("organization selected");
if(id && id.length > 0 && id != this.organizationId){
this.organization = null;
this.status = this.errorCodes.LOADING;
if(this.requestFor == "Publications") {
this._router.navigate( ['participate/deposit-publications-result'], { queryParams: { "organizationId": id } } );
} else if(this.requestFor == "Research Data") {
this._router.navigate( ['participate/deposit-datasets-result'], { queryParams: { "organizationId": id } } );
}
} else {
this.warningMessage = "No new organization selected";
}
}
private updateDescription(description:string){
this._meta.updateMeta("description", description);
this._meta.updateProperty("og:description", description);
}
private updateTitle(title:string){
var _prefix ="OpenAIRE | ";
var _title = _prefix + ((title.length> 50 ) ?title.substring(0,50):title);
this._meta.setTitle(_title );
this._meta.updateProperty("og:title",_title);
}
private updateUrl(url:string){
this._meta.updateProperty("og:url", url);
}
}