openaire-library/claims/directLinking/directLinking.component.ts

268 lines
10 KiB
TypeScript

import {Component, Input} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {ActivatedRoute, Router} from '@angular/router';
import {Title, Meta} from '@angular/platform-browser';
import {EnvProperties} from '../../utils/properties/env-properties';
import {ClaimProject, ClaimResult} from '../claim-utils/claimEntities.class';
import {EntitiesSearchService} from '../../utils/entitiesAutoComplete/entitySearch.service';
import {SearchPublicationsService} from '../../services/searchPublications.service';
import {SearchDatasetsService} from '../../services/searchDatasets.service';
import {SearchSoftwareService} from '../../services/searchSoftware.service';
import {SearchOrpsService} from '../../services/searchOrps.service';
import { SEOService } from '../../sharedComponents/SEO/SEO.service';
@Component({
selector: 'directLinking',
templateUrl: 'directLinking.component.html'
})
export class DirectLinkingComponent {
contexts=[];
projects=[];
results = [];
linkType:string ="project"; // link type (selected in home page) : project, context, software, etc
/* url Parameters for inline linking */
id:string = null; //entity id
type:string = null; // entity type (publication or dataset)
linkTo:string = null; // entity type (project or context or result)
entityTypes=["dataset", "publication","software","orp", "project","context"];
inlineResult:ClaimResult =null;
displayedResult:ClaimResult =null;
sub:any =null;
show:string="claim"; //{claim,result}
validInput:boolean = null;//'true;
properties:EnvProperties;
@Input() communityId:string= null;
localStoragePrefix:string = "";
url=null;
constructor (private _router: Router, private route: ActivatedRoute, private entitySearch:EntitiesSearchService,
private publicationsSearch:SearchPublicationsService, private datasetsSearch:SearchDatasetsService,
private softwareSearch:SearchSoftwareService,private ORPSearch:SearchOrpsService,
private _meta: Meta, private _title: Title, private seoService: SEOService ) {
var title = "OpenAIRE | Direct Linking";
this._meta.updateTag({content:title},"property='og:title'");
this._title.setTitle(title);
}
ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
this.url = data.envSpecific.baseLink+this._router.url;
this.seoService.createLinkForCanonicalURL(this.properties.baseLink+this._router.url, false);
});
this.sub = this.route.queryParams.subscribe(params => {
this.id = params['id'];
this.type = params['type'];
this.linkTo = params['linkTo'];
if(this.type!=null && this.linkTo!=null){
this.type = (this.entityTypes.indexOf(this.type) != -1)? this.type:'publication';
this.linkTo = (this.entityTypes.indexOf(this.linkTo) != -1 || this.linkTo == "result")? this.linkTo:'project';
this.show = (this.linkTo != "result")?"claim":"result";
this.linkType = this.linkTo;
var isInlineResult:boolean = false; // is a link result - result
if((this.type == "publication" || this.type == "dataset" || this.type == "software") && ((this.linkTo == "publication" || this.linkTo == "dataset" || this.linkTo == "software" || this.linkTo == "orp") || this.linkTo == "result" )){
isInlineResult = true;
}
this.localStoragePrefix = this.type.substr(0,3)+"_"+this.linkTo.substr(0,3)+"_";
//console.log("\n\n"+this.localStoragePrefix+"\n\n");
if(localStorage.getItem(this.localStoragePrefix + "projects")){
this.projects = JSON.parse(localStorage.getItem(this.localStoragePrefix + "projects"));
}
if(localStorage.getItem(this.localStoragePrefix + "contexts")){
this.contexts = JSON.parse(localStorage.getItem(this.localStoragePrefix + "contexts"));
}
if(localStorage.getItem(this.localStoragePrefix + "results")){
this.results = JSON.parse(localStorage.getItem(this.localStoragePrefix + "results"));
}
//console.log("\n\nGetting inline entity "+this.type+"\n\n");
if(this.type == "project"){
this.linkType = "project";
this.getProjectById(this.id);
}else if(this.type == "publication"){
this.getPublicationById(this.id,true);
}else if(this.type == "dataset"){
this.getDatasetById(this.id,true);
}else if(this.type == "software"){
this.getSoftwareById(this.id,true);
}else if(this.type == "orp"){
this.getOrpById(this.id,true);
}else{
this.validInput = this.isValidInput(null);
}
}else{
this.validInput = this.isValidInput(null);
}
});
}
isValidInput(result){
if(result == null){
return false;
}else if(this.type == "project" && this.linkTo != "result"){
return false;
}else if(["dataset","publication","software", "orp"].indexOf(this.type) != -1 && (["project","context","result"].indexOf(this.linkTo) == -1)){
return false;
}else if(["project","dataset","publication","software","orp"].indexOf(this.type) == -1){
return false;
}else{
return true;
}
}
getProjectById(id:string){
this.sub = this.entitySearch.fetchByType(id,"project", this.properties).subscribe(
data => {
var item =data[0];
var project: ClaimProject = new ClaimProject();
project.funderId = item.funderId;
project.funderName = item.funderName;
project.projectId = id;
project.projectName = item.projectName;
project.projectAcronym = item.projectAcronym;
project.startDate = item.startDate;
project.endDate = item.endDate;
project.code = item.code;
project.jurisdiction = item.jurisdiction;
project.fundingLevel0 = item.fundingLevel0;
this.projects.push( project);
this.validInput = this.isValidInput(project);
},
err => {
this.validInput = this.isValidInput(null);
//console.log("An error occured")
this.handleError("Error getting project by id: "+id, err);
});
}
getPublicationById(id:string, isInlineResult:boolean){
this.sub = this.publicationsSearch.searchPublicationById(id,this.properties).subscribe(
data => {
var item =data[0];
var result: ClaimResult = new ClaimResult();
result.id=id;
result.type="publication";
result.source="openaire";
result.title = item['title'].name;
result.url= item['title'].url;
result.result = item;
result.accessRights = item['title'].accessMode;
result.date = item.year;
this.displayedResult = result;
if(isInlineResult){
this.inlineResult = result;
}else{
this.results.push( result);
}
this.validInput = this.isValidInput(result);
},
err => {
this.validInput = this.isValidInput(null);
//console.log("An error occured")
this.handleError("Error getting publication by id: "+id, err);
});
}
getDatasetById(id:string, isInlineResult:boolean){
this.sub = this.datasetsSearch.searchDatasetById(id,this.properties).subscribe(
data => {
var item =data[0];
var result: ClaimResult = new ClaimResult();
result.id=id;
result.type="dataset";
result.source="openaire";
result.title = item['title'].name;
result.url= item['title'].url;
result.result = item;
result.accessRights = item['title'].accessMode;
result.embargoEndDate ="";
result.date = item.year;
this.displayedResult = result;
if(isInlineResult){
this.inlineResult = result;
}else{
this.results.push( result);
}
this.validInput = this.isValidInput(result);
},
err => {
this.validInput = this.isValidInput(null);
//console.log("An error occured")
this.handleError("Error getting research data by id: "+id, err);
});
}
getSoftwareById(id:string, isInlineResult:boolean){
this.sub = this.softwareSearch.searchSoftwareById(id,this.properties).subscribe(
data => {
var item =data[0];
var result: ClaimResult = new ClaimResult();
result.id=id;
result.type="software";
result.source="openaire";
result.title = item['title'].name;
result.url= item['title'].url;
result.result = item;
result.accessRights = item['title'].accessMode;
result.embargoEndDate ="";
result.date = item.year;
this.displayedResult = result;
if(isInlineResult){
this.inlineResult = result;
}else{
this.results.push( result);
}
this.validInput = this.isValidInput(result);
},
err => {
this.validInput = this.isValidInput(null);
//console.log("An error occured")
this.handleError("Error getting software by id: "+id, err);
});
}
getOrpById(id:string, isInlineResult:boolean){
this.sub = this.ORPSearch.searchOrpById(id,this.properties).subscribe(
data => {
var item =data[0];
var result: ClaimResult = new ClaimResult();
result.id=id;
result.type="other";
result.source="openaire";
result.title = item['title'].name;
result.url= item['title'].url;
result.result = item;
result.accessRights = item['title'].accessMode;
result.embargoEndDate ="";
result.date = item.year;
this.displayedResult = result;
if(isInlineResult){
this.inlineResult = result;
}else{
this.results.push( result);
}
this.validInput = this.isValidInput(result);
},
err => {
this.validInput = this.isValidInput(null);
//console.log("An error occured")
this.handleError("Error getting other research product by id: "+id, err);
});
}
private handleError(message: string, error) {
console.error("Direct Linking Page: "+message, error);
}
}