connect/src/app/deposit/zenodo/shareInZenodo.component.ts

174 lines
7.4 KiB
TypeScript
Raw Normal View History

import {Component} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Meta, Title} from '@angular/platform-browser';
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {ZenodoCommunitiesService} from '../../openaireLibrary/connect/zenodoCommunities/zenodo-communities.service';
import {SearchZenodoCommunitiesService} from '../../openaireLibrary/connect/zenodoCommunities/searchZenodoCommunities.service';
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
import {ConnectHelper} from '../../openaireLibrary/connect/connectHelper';
import {CommunityInfo} from '../../openaireLibrary/connect/community/communityInfo';
import {SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
@Component({
selector: 'share-in-zenodo',
templateUrl: './shareInZenodo.component.html'
})
export class ShareInZenodoComponent {
properties: EnvProperties;
private communityId: string = null;
private community: CommunityInfo = null;
// public warningMessage = "";
// public infoMessage = "";
masterZenodoCommunityId = null;
masterZenodoCommunity = null;
CommunityIds = null;
communities = [];
zenodocommunitiesloadedCount = 0;
zenodoSearchUtils: SearchUtilsClass = new SearchUtilsClass();
errorCodes: ErrorCodes = new ErrorCodes();
depositLink = "https://zenodo.org/deposit/new?c=";
constructor(private route: ActivatedRoute, private _router: Router,
private _meta: Meta, private _title: Title, private _ΖenodoCommunitieService: ZenodoCommunitiesService, private _communityService: CommunityService, private _searchZenodoCommunitiesService: SearchZenodoCommunitiesService) {
}
public ngOnInit() {
this.zenodoSearchUtils.status = this.errorCodes.LOADING;
;
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.updateUrl(data.envSpecific.baseLink + this._router.url);
this.updateTitle("Share in Zenodo");
this.updateDescription("Zenodo, repository, deposit, share");
this.properties = data.envSpecific;
this.route.queryParams.subscribe(params => {
this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain);
if(!this.communityId) {
this.communityId = params['communityId'];
}
if (this.communityId) {
if (this.communityId != null && this.communityId != '') {
this._communityService.getCommunity(this.properties, this.properties.communityAPI + this.communityId).subscribe(
community => {
this.community = community;
this.masterZenodoCommunityId = this.community.zenodoCommunity;
if (typeof document !== 'undefined') {
HelperFunctions.scroll();
}
if (this.masterZenodoCommunityId) {
this._ΖenodoCommunitieService.getZenodoCommunityById(this.properties, this.properties.zenodoCommunities + this.masterZenodoCommunityId, null).subscribe(
result => {
this.masterZenodoCommunity = result;
},
error => {
// var emptyCommunity:ZenodoCommunityInfo = new ZenodoCommunityInfo();
// emptyCommunity.id = this.masterZenodoCommunityId;
// emptyCommunity.title = this.masterZenodoCommunityId;
// this.masterZenodoCommunity = emptyCommunity;
//console.error("Master Zenodo community'"+this.masterZenodoCommunityId+"' couldn't be loaded");
this.handleError("Error getting Master Zenodo community with id: " + this.masterZenodoCommunityId, error);
}
);
}
this.zenodoSearchUtils.status = this.errorCodes.LOADING;
;
this._searchZenodoCommunitiesService.searchZCommunities(this.properties, this.communityId).subscribe(
result => {
this.CommunityIds = result;
this.zenodoSearchUtils.totalResults = this.CommunityIds.length;
if (this.CommunityIds.length == 0) {
this.zenodoSearchUtils.status = this.errorCodes.NONE;
}
for (let i = 0; i < this.CommunityIds.length; i++) {
this.getZenodoCommunityById(this.CommunityIds[i]["zenodoid"], this.CommunityIds[i]["id"]);
}
},
error => {
//console.error("list of zenodo communities couldn't be loaded");
this.handleError("Error getting list of zenodo communities for community with openaire id: " + this.communityId, error);
this.zenodoSearchUtils.status = this.errorCodes.ERROR;
} //this.handleError('System error retrieving community profile', error)
);
},
error => {
//console.error("Community couldn't be loaded");
this.handleError("Error getting community with id: " + this.communityId, error);
this.zenodoSearchUtils.status = this.errorCodes.ERROR;
}
);
}
}
});
});
}
private updateDescription(description: string) {
this._meta.updateTag({content: description}, "name='description'");
this._meta.updateTag({content: description}, "property='og:description'");
}
private updateTitle(title: string) {
var _prefix = "OpenAIRE | ";
var _title = _prefix + ((title.length > 50) ? title.substring(0, 50) : title);
this._title.setTitle(_title);
this._meta.updateTag({content: _title}, "property='og:title'");
}
private updateUrl(url: string) {
this._meta.updateTag({content: url}, "property='og:url'");
}
getZenodoCommunityById(zenodoid, openaireId) {
this._ΖenodoCommunitieService.getZenodoCommunityById(this.properties, this.properties.zenodoCommunities + zenodoid, openaireId).subscribe(
result => {
this.communities.push(result);
this.zenodocommunitiesloadedCount++;
if (this.zenodocommunitiesloadedCount >= this.CommunityIds.length) {
this.zenodoSearchUtils.status = this.errorCodes.DONE;
}
},
error => {
// var emptyCommunity:ZenodoCommunityInfo = new ZenodoCommunityInfo();
// emptyCommunity.id = zenodoid;
// emptyCommunity.openaireId = openaireId;
// emptyCommunity.title = zenodoid;
// this.communities.push(emptyCommunity);
this.zenodocommunitiesloadedCount++;
if (this.zenodocommunitiesloadedCount >= this.CommunityIds.length) {
this.zenodoSearchUtils.status = this.errorCodes.DONE;
}
//console.error("Zenodo community'"+zenodoid+"' couldn't be loaded");
this.handleError("Error getting Zenodo community with id: " + zenodoid + " and openaire id: " + openaireId, error);
}
);
}
private handleError(message: string, error) {
console.error("Share in Zenodo Page: " + message, error);
}
}