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

225 lines
8.4 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';
@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", "project","context"];
inlineResult:ClaimResult =null;
displayedResult:ClaimResult =null;
sub:any =null;
show:string="claim"; //{claim,result}
validInput:boolean = null;//'true;
properties:EnvProperties;
constructor (private _router: Router, private route: ActivatedRoute, private entitySearch:EntitiesSearchService,
private publicationsSearch:SearchPublicationsService, private datasetsSearch:SearchDatasetsService,
private softwareSearch:SearchSoftwareService, private _meta: Meta, private _title: Title) {
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;
});
if(localStorage.getItem("projects")){
this.projects = JSON.parse(localStorage.getItem("projects"));
}
if(localStorage.getItem("contexts")){
this.contexts = JSON.parse(localStorage.getItem("contexts"));
}
if(localStorage.getItem("results")){
this.results = JSON.parse(localStorage.getItem("results"));
}
if(localStorage.getItem("inlineEntity")){
this.inlineResult = JSON.parse(localStorage.getItem("inlineEntity"));
}
localStorage.removeItem("projects");
localStorage.removeItem("contexts");
localStorage.removeItem("results");
localStorage.removeItem("inlineEntity");
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 == "result" )){
isInlineResult = true;
}
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{
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"].indexOf(this.type) != -1 && (["project","context","result"].indexOf(this.linkTo) == -1)){
return false;
}else if(["project","dataset","publication","software"].indexOf(this.type) == -1){
return false;
}else{
return true;
}
}
getProjectById(id:string){
this.sub = this.entitySearch.fetchByType(id,"project", this.properties).subscribe(
data => {
console.log(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")
});
}
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")
});
}
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.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")
});
}
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.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")
});
}
}