1
|
import {Component, Input, ViewChild} from '@angular/core';
|
2
|
import {ActivatedRoute, Router} from '@angular/router';
|
3
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
4
|
import {ClaimEntity, ClaimProject, ShowOptions} from '../claim-utils/claimHelper.class';
|
5
|
import {EntitiesSearchService} from '../../utils/entitiesAutoComplete/entitySearch.service';
|
6
|
import {SearchResearchResultsService} from '../../services/searchResearchResults.service';
|
7
|
import {LinkingGenericComponent} from "../linking/linkingGeneric.component";
|
8
|
import {ClaimResultSearchFormComponent} from "../claim-utils/claimResultSearchForm.component";
|
9
|
import {Subscriber} from "rxjs";
|
10
|
|
11
|
|
12
|
@Component({
|
13
|
selector: 'directLinking',
|
14
|
templateUrl: 'directLinking.component.html'
|
15
|
})
|
16
|
export class DirectLinkingComponent {
|
17
|
@Input() piwikSiteId = null;
|
18
|
|
19
|
@ViewChild(LinkingGenericComponent) linking: LinkingGenericComponent;
|
20
|
|
21
|
results: ClaimEntity[] = [];
|
22
|
|
23
|
// linkType: string = "project"; // link type (selected in home page) : project, context, software, etc
|
24
|
/* url Parameters for inline linking */
|
25
|
id: string = null; //entity id
|
26
|
type: string = null; // entity type (publication or dataset)
|
27
|
// linkTo: string = null; // entity type (project or context or entity)
|
28
|
// linkToEntities: string[] = [];
|
29
|
showOptions:ShowOptions = new ShowOptions();
|
30
|
validEntityTypes = ["dataset", "publication", "software", "orp", "project", "context"];
|
31
|
sources: ClaimEntity[] = [];
|
32
|
inlineEntity: ClaimEntity = null;
|
33
|
validInput: boolean = null;//'true;
|
34
|
properties: EnvProperties;
|
35
|
@Input() communityId: string = null;
|
36
|
localStoragePrefix: string = "";
|
37
|
constructor(private _router: Router, private route: ActivatedRoute,private entitySearch:EntitiesSearchService,
|
38
|
private _searchResearchResultsService: SearchResearchResultsService) {}
|
39
|
subscriptions = [];
|
40
|
ngOnDestroy() {
|
41
|
this.subscriptions.forEach(subscription => {
|
42
|
if (subscription instanceof Subscriber) {
|
43
|
subscription.unsubscribe();
|
44
|
}
|
45
|
});
|
46
|
}
|
47
|
ngOnInit() {
|
48
|
this.subscriptions.push(this.route.data
|
49
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
50
|
this.properties = data.envSpecific;
|
51
|
}));
|
52
|
|
53
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
54
|
this.id = params['id'];
|
55
|
this.type = params['type'];
|
56
|
this.showOptions.linkTo = params['linkTo'];
|
57
|
if (this.type != null && this.showOptions.linkTo != null) {
|
58
|
this.type = (this.validEntityTypes.indexOf(this.type) != -1) ? this.type : 'publication';
|
59
|
this.showOptions.linkTo = (this.validEntityTypes.indexOf(this.showOptions.linkTo) != -1 || this.showOptions.linkTo == "result") ? this.showOptions.linkTo : 'project';
|
60
|
// this.show = (this.linkTo != "entity") ? "claim" : "entity";
|
61
|
// this.linkType = this.linkTo;
|
62
|
// let isInlineResult: boolean = false; // is a link entity - entity
|
63
|
// 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")) {
|
64
|
// isInlineResult = true;
|
65
|
// }
|
66
|
this.localStoragePrefix = (this.communityId ? (this.communityId + "_") : '') + this.type.substr(0, 3) + "_" + this.showOptions.linkTo.substr(0, 3) + "_";
|
67
|
|
68
|
if (localStorage.getItem(this.localStoragePrefix + "results")) {
|
69
|
this.results = JSON.parse(localStorage.getItem(this.localStoragePrefix + "results"));
|
70
|
}
|
71
|
if (localStorage.getItem(this.localStoragePrefix + "sources")) {
|
72
|
this.sources = JSON.parse(localStorage.getItem(this.localStoragePrefix + "sources"));
|
73
|
}
|
74
|
//console.log("\n\nGetting inline entity "+this.type+"\n\n");
|
75
|
if (this.type == "project") {
|
76
|
// this.linkType = "project";
|
77
|
this.getProjectById(this.id);
|
78
|
} else if (this.type == "publication") {
|
79
|
this.getResearchResultById("publication", this.id);
|
80
|
} else if (this.type == "dataset") {
|
81
|
this.getResearchResultById("dataset", this.id);
|
82
|
} else if (this.type == "software") {
|
83
|
this.getResearchResultById("software", this.id);
|
84
|
} else if (this.type == "orp") {
|
85
|
this.getResearchResultById("other", this.id);
|
86
|
} else {
|
87
|
this.validInput = this.isValidInput(null);
|
88
|
}
|
89
|
//set which entities it is allowed to link to.
|
90
|
// add first the
|
91
|
if(this.type == "project"){
|
92
|
this.showOptions.linkToEntities = ["result"];
|
93
|
this.showOptions.linkTo = "result";
|
94
|
}else{
|
95
|
// entity type (project or context or entity)
|
96
|
if(this.showOptions.linkTo == "project"){
|
97
|
this.showOptions.linkToEntities = ["project","context" ,"result"];
|
98
|
}else if(this.showOptions.linkTo == "context"){
|
99
|
this.showOptions.linkToEntities = ["context","project", "result" ];
|
100
|
}else{
|
101
|
this.showOptions.linkTo = "result";
|
102
|
this.showOptions.linkToEntities = ["result","project","context" ];
|
103
|
}
|
104
|
|
105
|
}
|
106
|
|
107
|
} else {
|
108
|
this.validInput = this.isValidInput(null);
|
109
|
|
110
|
}
|
111
|
|
112
|
}));
|
113
|
}
|
114
|
|
115
|
isValidInput(result: ClaimEntity) {
|
116
|
if (result == null) {
|
117
|
return false;
|
118
|
} else if (this.type == "project" && this.showOptions.linkTo != "result") {
|
119
|
return false;
|
120
|
} else if (["dataset", "publication", "software", "orp"].indexOf(this.type) != -1 && (["project", "context", "result"].indexOf(this.showOptions.linkTo) == -1)) {
|
121
|
return false;
|
122
|
} else if (["project", "dataset", "publication", "software", "orp"].indexOf(this.type) == -1) {
|
123
|
return false;
|
124
|
} else {
|
125
|
return true;
|
126
|
}
|
127
|
}
|
128
|
|
129
|
getProjectById(id: string) {
|
130
|
this.subscriptions.push(this.entitySearch.fetchByType(id,"project", this.properties).subscribe(
|
131
|
data => {
|
132
|
this.createClaimEntity(data, "project");
|
133
|
},
|
134
|
err => {
|
135
|
this.validInput = this.isValidInput(null);
|
136
|
//console.log("An error occured")
|
137
|
this.handleError("Error getting project by id: " + id, err);
|
138
|
}));
|
139
|
}
|
140
|
|
141
|
getResearchResultById(resultType: string, id: string) {
|
142
|
this.subscriptions.push(this._searchResearchResultsService.searchById(resultType, id, this.properties).subscribe(data => {
|
143
|
this.createClaimEntity(data, resultType);
|
144
|
},
|
145
|
err => {
|
146
|
this.validInput = this.isValidInput(null);
|
147
|
//console.log("An error occured")
|
148
|
this.handleError("Error getting "+this.getEntityName(resultType, false, true)+" by id: " + id, err);
|
149
|
}));
|
150
|
}
|
151
|
|
152
|
createClaimEntity(data, type: string) {
|
153
|
let results: ClaimEntity[] =[] ;
|
154
|
if(type =="project"){
|
155
|
let project = data[0];
|
156
|
let entity:ClaimEntity = new ClaimEntity();
|
157
|
entity.id = project.id;
|
158
|
entity.type = "project";
|
159
|
entity.title = project.projectName;
|
160
|
entity.project = new ClaimProject();
|
161
|
entity.project.acronym = project.projectAcronym;
|
162
|
entity.project.code = project.code;
|
163
|
entity.project.endDate = project.endDate;
|
164
|
entity.project.funderId = project.funderId;
|
165
|
entity.project.funderName = project.funderName;
|
166
|
entity.project.fundingLevel0 = project.fundingLevel0;
|
167
|
entity.project.jurisdiction = project.jurisdiction;
|
168
|
entity.project.startDate = project.startDate;
|
169
|
this.inlineEntity = entity;
|
170
|
}else{
|
171
|
results = ClaimResultSearchFormComponent.openaire2ClaimResults(data, this.properties);
|
172
|
}
|
173
|
|
174
|
if (results.length > 0) {
|
175
|
this.inlineEntity = results[0]
|
176
|
}
|
177
|
this.validInput = this.isValidInput(this.inlineEntity);
|
178
|
}
|
179
|
|
180
|
private handleError(message: string, error) {
|
181
|
console.error("Direct Linking Page: " + message, error);
|
182
|
}
|
183
|
|
184
|
private getEntityName (entityType:string, plural:boolean, full:boolean): string {
|
185
|
if(entityType == "publication") {
|
186
|
return "publication" + (plural ? "s" : "");
|
187
|
} else if(entityType == "dataset") {
|
188
|
return (full ? "research data" : ("dataset" + (plural ? "s" : "")));
|
189
|
} else if(entityType == "software") {
|
190
|
return "software";
|
191
|
} else if(entityType == "other") {
|
192
|
return (full ? ("other research product" + (plural ? "s" : "")) : "other");
|
193
|
}
|
194
|
}
|
195
|
}
|