Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {Meta, Title} from '@angular/platform-browser';
4

    
5
import {EnvProperties} from '../../utils/properties/env-properties';
6
import {ClaimEntity, ClaimProject, ShowOptions} from '../claim-utils/claimHelper.class';
7
import {EntitiesSearchService} from '../../utils/entitiesAutoComplete/entitySearch.service';
8
import {SearchPublicationsService} from '../../services/searchPublications.service';
9
import {SearchDatasetsService} from '../../services/searchDatasets.service';
10
import {SearchSoftwareService} from '../../services/searchSoftware.service';
11
import {SearchOrpsService} from '../../services/searchOrps.service';
12
import {LinkingGenericComponent} from "../linking/linkingGeneric.component";
13
import {ClaimResultSearchFormComponent} from "../claim-utils/claimResultSearchForm.component";
14

    
15

    
16
@Component({
17
  selector: 'directLinking',
18
  templateUrl: 'directLinking.component.html'
19
})
20
export class DirectLinkingComponent {
21
  @Input() piwikSiteId = null;
22

    
23
  @ViewChild(LinkingGenericComponent) linking: LinkingGenericComponent;
24

    
25
  results: ClaimEntity[] = [];
26

    
27
  // linkType: string = "project"; // link type (selected in home page) : project, context, software, etc
28
  /* url Parameters for inline linking */
29
  id: string = null; //entity id
30
  type: string = null; // entity type (publication or dataset)
31
  // linkTo: string = null; // entity type (project or context or entity)
32
  // linkToEntities: string[] = [];
33
  showOptions:ShowOptions = new ShowOptions();
34
  validEntityTypes = ["dataset", "publication", "software", "orp", "project", "context"];
35
  sources: ClaimEntity[] = [];
36
  inlineEntity: ClaimEntity = null;
37
  sub: any = null;
38
  validInput: boolean = null;//'true;
39
  properties: EnvProperties;
40
  @Input() communityId: string = null;
41
  localStoragePrefix: string = "";
42
  constructor(private _router: Router, private route: ActivatedRoute,private entitySearch:EntitiesSearchService,
43
              private publicationsSearch: SearchPublicationsService, private datasetsSearch: SearchDatasetsService,
44
              private softwareSearch: SearchSoftwareService, private ORPSearch: SearchOrpsService) {
45
  }
46

    
47
  ngOnInit() {
48
    this.route.data
49
      .subscribe((data: { envSpecific: EnvProperties }) => {
50
        this.properties = data.envSpecific;
51
      });
52

    
53
    this.sub = 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.getPublicationById(this.id);
80
        } else if (this.type == "dataset") {
81
          this.getDatasetById(this.id);
82
        } else if (this.type == "software") {
83
          this.getSoftwareById(this.id);
84
        } else if (this.type == "orp") {
85
          this.getOrpById(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.sub = 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
  getPublicationById(id: string) {
142

    
143
    this.sub = this.publicationsSearch.searchPublicationById(id, this.properties).subscribe(data => {
144
        this.createClaimEntity(data, "publication");
145
      },
146
      err => {
147
        this.validInput = this.isValidInput(null);
148
        //console.log("An error occured")
149
        this.handleError("Error getting publication by id: " + id, err);
150
      });
151
  }
152

    
153
  getDatasetById(id: string) {
154
    this.sub = this.datasetsSearch.searchDatasetById(id, this.properties).subscribe(
155
      data => {
156
        this.createClaimEntity(data, "dataset");
157
      },
158
      err => {
159
        this.validInput = this.isValidInput(null);
160
        //console.log("An error occured")
161
        this.handleError("Error getting research data by id: " + id, err);
162
      });
163
  }
164

    
165
  getSoftwareById(id: string) {
166
    this.sub = this.softwareSearch.searchSoftwareById(id, this.properties).subscribe(
167
      data => {
168
        this.createClaimEntity(data, "software");
169
      },
170
      err => {
171
        this.validInput = this.isValidInput(null);
172
        //console.log("An error occured")
173
        this.handleError("Error getting software by id: " + id, err);
174
      });
175
  }
176

    
177
  getOrpById(id: string) {
178
    this.sub = this.ORPSearch.searchOrpById(id, this.properties).subscribe(
179
      data => {
180
        this.createClaimEntity(data, "other");
181
      },
182
      err => {
183
        this.validInput = this.isValidInput(null);
184
        //console.log("An error occured")
185
        this.handleError("Error getting other research product by id: " + id, err);
186
      });
187
  }
188

    
189
  createClaimEntity(data, type: string) {
190
    let results: ClaimEntity[] =[] ;
191
     if(type =="project"){
192
       let project = data[0];
193
       let entity:ClaimEntity = new ClaimEntity();
194
       entity.id = project.id;
195
       entity.type = "project";
196
       entity.title = project.projectName;
197
       entity.project = new ClaimProject();
198
       entity.project.acronym = project.projectAcronym;
199
       entity.project.code = project.code;
200
       entity.project.endDate = project.endDate;
201
       entity.project.funderId = project.funderId;
202
       entity.project.funderName = project.funderName;
203
       entity.project.fundingLevel0 = project.fundingLevel0;
204
       entity.project.jurisdiction = project.jurisdiction;
205
       entity.project.startDate = project.startDate;
206
       this.inlineEntity = entity;
207
     }else{
208
      results = ClaimResultSearchFormComponent.openaire2ClaimResults(data, type);
209
     }
210

    
211
    if (results.length > 0) {
212
      this.inlineEntity = results[0]
213
    }
214
    this.validInput = this.isValidInput(this.inlineEntity);
215
  }
216

    
217
  private handleError(message: string, error) {
218
    console.error("Direct Linking Page: " + message, error);
219
  }
220
}
(2-2/3)