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

221 lines
8.7 KiB
TypeScript
Raw Normal View History

import {Component, Input, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Meta, Title} from '@angular/platform-browser';
import {EnvProperties} from '../../utils/properties/env-properties';
import {ClaimEntity, ClaimProject, ShowOptions} from '../claim-utils/claimHelper.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 {LinkingGenericComponent} from "../linking/linkingGeneric.component";
import {ClaimResultSearchFormComponent} from "../claim-utils/claimResultSearchForm.component";
@Component({
selector: 'directLinking',
templateUrl: 'directLinking.component.html'
})
export class DirectLinkingComponent {
@Input() piwikSiteId = null;
@ViewChild(LinkingGenericComponent) linking: LinkingGenericComponent;
results: ClaimEntity[] = [];
// 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 entity)
// linkToEntities: string[] = [];
showOptions:ShowOptions = new ShowOptions();
validEntityTypes = ["dataset", "publication", "software", "orp", "project", "context"];
sources: ClaimEntity[] = [];
inlineEntity: ClaimEntity = null;
sub: any = null;
validInput: boolean = null;//'true;
properties: EnvProperties;
@Input() communityId: string = null;
localStoragePrefix: string = "";
constructor(private _router: Router, private route: ActivatedRoute,private entitySearch:EntitiesSearchService,
private publicationsSearch: SearchPublicationsService, private datasetsSearch: SearchDatasetsService,
private softwareSearch: SearchSoftwareService, private ORPSearch: SearchOrpsService) {
}
ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
});
this.sub = this.route.queryParams.subscribe(params => {
this.id = params['id'];
this.type = params['type'];
this.showOptions.linkTo = params['linkTo'];
if (this.type != null && this.showOptions.linkTo != null) {
this.type = (this.validEntityTypes.indexOf(this.type) != -1) ? this.type : 'publication';
this.showOptions.linkTo = (this.validEntityTypes.indexOf(this.showOptions.linkTo) != -1 || this.showOptions.linkTo == "result") ? this.showOptions.linkTo : 'project';
// this.show = (this.linkTo != "entity") ? "claim" : "entity";
// this.linkType = this.linkTo;
// let isInlineResult: boolean = false; // is a link entity - entity
// if ((this.type == "publication" || this.type == "dataset" || this.type == "software") && ((this.linkTo == "publication" || this.linkTo == "dataset" || this.linkTo == "software" || this.linkTo == "orp") || this.linkTo == "entity")) {
// isInlineResult = true;
// }
this.localStoragePrefix = (this.communityId ? (this.communityId + "_") : '') + this.type.substr(0, 3) + "_" + this.showOptions.linkTo.substr(0, 3) + "_";
if (localStorage.getItem(this.localStoragePrefix + "results")) {
this.results = JSON.parse(localStorage.getItem(this.localStoragePrefix + "results"));
}
if (localStorage.getItem(this.localStoragePrefix + "sources")) {
this.sources = JSON.parse(localStorage.getItem(this.localStoragePrefix + "sources"));
}
//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);
} else if (this.type == "dataset") {
this.getDatasetById(this.id);
} else if (this.type == "software") {
this.getSoftwareById(this.id);
} else if (this.type == "orp") {
this.getOrpById(this.id);
} else {
this.validInput = this.isValidInput(null);
}
//set which entities it is allowed to link to.
// add first the
if(this.type == "project"){
this.showOptions.linkToEntities = ["result"];
this.showOptions.linkTo = "result";
}else{
// entity type (project or context or entity)
if(this.showOptions.linkTo == "project"){
this.showOptions.linkToEntities = ["project","context" ,"result"];
}else if(this.showOptions.linkTo == "context"){
this.showOptions.linkToEntities = ["context","project", "result" ];
}else{
this.showOptions.linkTo = "result";
this.showOptions.linkToEntities = ["result","project","context" ];
}
}
} else {
this.validInput = this.isValidInput(null);
}
});
}
isValidInput(result: ClaimEntity) {
if (result == null) {
return false;
} else if (this.type == "project" && this.showOptions.linkTo != "result") {
return false;
} else if (["dataset", "publication", "software", "orp"].indexOf(this.type) != -1 && (["project", "context", "result"].indexOf(this.showOptions.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 => {
this.createClaimEntity(data, "project");
},
err => {
this.validInput = this.isValidInput(null);
//console.log("An error occured")
this.handleError("Error getting project by id: " + id, err);
});
}
getPublicationById(id: string) {
this.sub = this.publicationsSearch.searchPublicationById(id, this.properties).subscribe(data => {
this.createClaimEntity(data, "publication");
},
err => {
this.validInput = this.isValidInput(null);
//console.log("An error occured")
this.handleError("Error getting publication by id: " + id, err);
});
}
getDatasetById(id: string) {
this.sub = this.datasetsSearch.searchDatasetById(id, this.properties).subscribe(
data => {
this.createClaimEntity(data, "dataset");
},
err => {
this.validInput = this.isValidInput(null);
//console.log("An error occured")
this.handleError("Error getting research data by id: " + id, err);
});
}
getSoftwareById(id: string) {
this.sub = this.softwareSearch.searchSoftwareById(id, this.properties).subscribe(
data => {
this.createClaimEntity(data, "software");
},
err => {
this.validInput = this.isValidInput(null);
//console.log("An error occured")
this.handleError("Error getting software by id: " + id, err);
});
}
getOrpById(id: string) {
this.sub = this.ORPSearch.searchOrpById(id, this.properties).subscribe(
data => {
this.createClaimEntity(data, "other");
},
err => {
this.validInput = this.isValidInput(null);
//console.log("An error occured")
this.handleError("Error getting other research product by id: " + id, err);
});
}
createClaimEntity(data, type: string) {
let results: ClaimEntity[] =[] ;
if(type =="project"){
let project = data[0];
let entity:ClaimEntity = new ClaimEntity();
entity.id = project.id;
entity.type = "project";
entity.title = project.projectName;
entity.project = new ClaimProject();
entity.project.acronym = project.projectAcronym;
entity.project.code = project.code;
entity.project.endDate = project.endDate;
entity.project.funderId = project.funderId;
entity.project.funderName = project.funderName;
entity.project.fundingLevel0 = project.fundingLevel0;
entity.project.jurisdiction = project.jurisdiction;
entity.project.startDate = project.startDate;
this.inlineEntity = entity;
}else{
results = ClaimResultSearchFormComponent.openaire2ClaimResults(data, type);
}
if (results.length > 0) {
this.inlineEntity = results[0]
}
this.validInput = this.isValidInput(this.inlineEntity);
}
private handleError(message: string, error) {
console.error("Direct Linking Page: " + message, error);
}
}