Project

General

Profile

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