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

262 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);
this.seoService.createLinkForCanonicalURL(false);
}
ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
this.url = data.envSpecific.baseLink+this._router.url;
});
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 => {
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.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")
});
}
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")
});
}
getOrpById(id:string, isInlineResult:boolean){
this.sub = this.ORPSearch.searchOrpById(id,this.properties).subscribe(
data => {
console.log(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;
console.log(item)
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")
});
}
}