Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {Location} from '@angular/common';
4
import {ErrorCodes} from "../../utils/properties/errorCodes";
5
import {ErrorMessagesComponent} from "../../utils/errorMessages.component";
6
import {SearchUtilsClass} from "../../searchPages/searchUtils/searchUtils.class";
7
import {RouterHelper} from "../../utils/routerHelper.class";
8
import {EnvProperties} from "../../utils/properties/env-properties";
9
import {properties} from "../../../../environments/environment";
10
import {OrcidService} from "../orcid.service";
11
import {Identifier, StringUtils} from "../../utils/string-utils.class";
12
import {SearchResearchResultsService} from "../../services/searchResearchResults.service";
13
import {SearchResult} from "../../utils/entities/searchResult";
14
import {ResultPreview} from "../../utils/result-preview/result-preview";
15
import {Meta, Title} from "@angular/platform-browser";
16
import {UserManagementService} from "../../services/user-management.service";
17
import {PiwikService} from "../../utils/piwik/piwik.service";
18
import {IndexInfoService} from "../../utils/indexInfo.service";
19

    
20
declare var UIkit: any;
21

    
22
@Component({
23
  selector: 'my-orcid-links',
24
  template: `
25
    
26
    <div class="uk-container uk-container-large">
27
      <div class="uk-margin-medium-bottom">
28
        <div class="uk-margin-top">
29
          <div class="uk-grid uk-flex uk-flex-middle">
30
            <div class="uk-article-title custom-article-title uk-width-auto">
31
              My ORCID links
32
            </div>
33
            <span *ngIf="authorNameParam" class="uk-width-1-1 uk-width-expand@m">
34
              <a class="uk-button-text uk-align-left uk-align-right@m" [queryParams]="authorNameParam" 
35
                  [routerLink]="properties.searchLinkToAdvancedResults" routerLinkActive="router-link-active">
36
                Discover research results related to you
37
              </a>
38
            </span>
39
          </div>
40
          
41
          <div class="uk-margin-top">
42
            <span>
43
              Did you link a result with your ORCID <img src="assets/common-assets/common/ORCIDiD_icon16x16.png" alt="" loading="lazy">
44
              but the <span class="orcid-color">green icon</span> is missing?
45
              <br>
46
              No worries! It will appear, as soon as we synchronize again with ORCID data.
47
              <span *ngIf="lastOrcidUpdateDate">Latest synchronization was on {{lastOrcidUpdateDate | date}}. </span>
48
              <a href="https://www.openaire.eu/openaire-explore-integration-with-the-orcid-search-and-link-wizard"
49
                     target="_blank" class="uk-display-inline-block">
50
                Read more  <span class="custom-external custom-icon space"></span>
51
              </a>
52
            </span>
53
          </div>
54
          
55
          <div *ngIf="showErrorMessage" class="uk-animation-fade uk-alert uk-alert-warning uk-margin-large-top" role="alert">
56
            An Error Occurred
57
          </div>
58
          <div *ngIf="!showLoading && !orcidQuery && !requestGrant && !showErrorMessage" 
59
                class="uk-animation-fade uk-alert uk-alert-primary uk-margin-large-top" role="alert">
60
            No ORCID links found
61
          </div>
62
          <div *ngIf="requestGrant" class="uk-margin-large-top">
63
            <div class="uk-text-center uk-padding-small">
64
              <div>
65
                This is a private page, only for users that have connected their ORCID iD with OpenAIRE.
66
                Please grant OpenAIRE to access and update your ORCID works.
67
              </div>
68
  
69
              <div class="uk-margin-medium-top uk-align-center">
70
                <button (click)="openGrantWindow()" type="submit"
71
                        class="uk-button uk-padding-small uk-padding-remove-vertical uk-margin-left uk-button-primary">
72
                  <span>Grant OpenAIRE</span>
73
                </button>
74
              </div>
75
            </div>
76
          </div>
77
        </div>
78
  
79
        <div *ngIf="orcidQuery" class="tm-middle" id="tm-main">
80
          <div uk-grid="" class="uk-grid uk-grid-stack">
81
            <div class="tm-main uk-width-1-1@s uk-width-1-1@m uk-width-1-1@l uk-row-first uk-first-column">
82
              <div class="uk-container uk-container-large">
83
                <my-orcid-result [previewResults]="currentResults" 
84
                                 [status]="searchUtils.status" 
85
                                 [type]="resultType" 
86
                                 [properties]=properties
87
                                 [currentPage]="currentPage" 
88
                                 [totalResults]="totalOrcidResults" 
89
                                 [resultsPerPage]="resultsPerPage"
90
                                 (pageChange)="pageChanged($event)">
91
                </my-orcid-result>
92
              </div>
93
            </div>
94
          </div>
95
        </div>
96
      </div>
97
    </div>
98
    `,
99
  styles: ['.orcid-color { color: #a6ce39; }']
100
})
101
export class MyOrcidLinksComponent {
102
  public resultType: string = "result";
103

    
104
  private errorCodes: ErrorCodes;
105
  private errorMessages: ErrorMessagesComponent;
106
  @Input() piwikSiteId = null;
107
  public results =[];
108
  public currentResults = [];
109
  public totalResults:number  = 0 ;
110
  public baseUrl:string;
111
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
112
  public subscriptions: any[] = [];
113
  public _location:Location;
114

    
115
  public disableForms: boolean = false;
116
  public loadPaging: boolean = true;
117
  public oldTotalResults: number = 0;
118
  pagingLimit = 0;
119

    
120
  properties:EnvProperties;
121
  public lastOrcidUpdateDate: string = "";
122

    
123
  @Input() public communityId: string = null;
124

    
125
  public routerHelper:RouterHelper = new RouterHelper();
126
  parameters = {};
127
  keyword  = "";
128

    
129
  identifiers: string[] = [];
130
  works: any[] = [];
131

    
132
  orcidQuery: string = "";
133
  typeQuery: string = "";
134

    
135
  public showLoading: boolean = true;
136

    
137
  currentPage: number = 1;
138
  totalOrcidResults: number;
139
  resultsPerPage: number = 5;
140

    
141
  public requestGrant: boolean = false;
142
  public window: any;
143
  private tokenUrl: string;
144
  private clientId: string = "APP-A5M3KTX6NCN67L91";
145

    
146
  public authorNameParam: any = null;
147

    
148
  public showErrorMessage: boolean = false;
149

    
150
  constructor (private route: ActivatedRoute, private router: Router,
151
               private _piwikService:PiwikService,
152
               private _orcidService: OrcidService,
153
               private indexInfoService: IndexInfoService,
154
               private _searchResearchResultsService: SearchResearchResultsService,
155
               private userManagementService: UserManagementService,
156
               private _meta: Meta, private _title: Title
157
  ) {
158
    this.errorCodes = new ErrorCodes();
159
    this.errorMessages = new ErrorMessagesComponent();
160
    this.searchUtils.status = this.errorCodes.LOADING;
161
    this.searchUtils.page =1;
162

    
163
    if(typeof document !== 'undefined') {
164
      this.tokenUrl = properties.orcidTokenURL
165
        + "client_id="+properties.orcidClientId
166
        // + "&response_type=code&scope=/activities/update"
167
        // + "&response_type=code&scope=/authenticate /activities/update /person/update /read-limited"
168
        + "&response_type=code&scope=/activities/update /read-limited"
169
        + "&redirect_uri="+location.origin+"/orcid?source=openaire";
170

    
171
      this.indexInfoService.getLastOrcidUpdateDate(properties).subscribe(
172
        date => {
173
          this.lastOrcidUpdateDate = date;
174
        },
175
        error => { console.error("Error in fetching last orcid update date ",error); }
176
      )
177
    }
178
  }
179

    
180
  public ngOnInit() {
181
    this.properties = properties;
182
    this.baseUrl = this.properties.myOrcidLinksPage;
183
    this.pagingLimit = this.properties.pagingLimit;
184
    var description = "Openaire, ORCID linking, publication, research data, software, other research product";
185
    this.updateTitle("My ORCID Links");
186
    this.updateDescription(description);
187
    this.updateUrl( properties.domain + properties.baseLink + this.route.url);
188

    
189
    if(this.properties.enablePiwikTrack && (typeof document !== 'undefined')){
190
      this.subscriptions.push(this._piwikService.trackView(this.properties, "My ORCID links", this.piwikSiteId).subscribe());
191
    }
192

    
193
    this.typeQuery = "&type=results";
194
    this.getLocalWorks();
195
    this.getPersonalDetails();
196
  }
197

    
198
  public ngOnDestroy() {
199
    for(let sub of this.subscriptions){
200
      sub.unsubscribe();
201
    }
202
  }
203

    
204
  private getPersonalDetails() {
205
    //get author name
206
    this.subscriptions.push(this._orcidService.getPersonalDetails().subscribe(
207
      details => {
208
        let author: string = "";
209

    
210
        if(details && details['name']) {
211
          let name: string = details['name'];
212
          if(name['given-names'] && name['given-names']['value']) {
213
            author = name['given-names']['value'];
214
          }
215
          if(name['family-name'] && name['family-name']['value']) {
216
            author += (author ? " " : "") + name['family-name']['value'];
217
          }
218
          this.authorNameParam = this.routerHelper.createQueryParams(['f0', 'fv0'], ['resultauthor', author]);
219
        }
220
      },
221
      error => {
222
        console.error("Error getting personal details", error);
223
        this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
224
          if (user) {
225
            this.authorNameParam = this.routerHelper.createQueryParams(['f0', 'fv0'], ['resultauthor', user.fullname]);
226
          }
227
        }));
228
      }
229
    ));
230
  }
231

    
232
  openGrantWindow() {
233
    this.window = window.open(this.tokenUrl, '_blank',
234
      'location=yes,height=700,width=540,left=500,top=100,scrollbars=yes,status=yes');
235

    
236
    let self = this;
237
    window.onmessage = function (ev) {
238
      if (ev.isTrusted && ev.origin !== location.origin && ev.data !== 'success')
239
        return;
240
      self.requestGrant = false;
241
      UIkit.notification({
242
        message: 'Thank you for <strong>connecting your ORCID iD</strong> with OpenAIRE!',
243
        status: 'success',
244
        timeout: 6000,
245
        pos: 'bottom-right'
246
      });
247
      self.getLocalWorks();
248
    };
249
  }
250

    
251
  getLocalWorks() {
252
    this.showErrorMessage = false;
253

    
254
    this.showLoading = true;
255
    this.subscriptions.push(this._orcidService.getLocalWorks().subscribe(
256
      (response: any[]) => {
257

    
258
        this.works = response;//['results'];
259
        this.totalOrcidResults = this.works.length;//response['total'];
260

    
261
        this.prepareOrcidQuery();
262
      },
263
      error => {
264
        this.handleError(error, "Could not get locally stored user's ORCID works");
265
        this.showLoading = false;
266
      }
267
    ));
268
  }
269

    
270
  public prepareOrcidQuery() {
271

    
272
    if(this.results.length >= this.currentPage) {
273
      this.currentResults = this.results[this.currentPage-1];
274
      return;
275
    }
276

    
277
    this.showLoading = true;
278

    
279
    this.orcidQuery = "";
280

    
281
    let from: number = (this.currentPage-1)*this.resultsPerPage;
282
    if(from >= this.works.length) {
283
      this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
284
    }
285
    let to: number = this.currentPage*this.resultsPerPage;
286

    
287
    if(to > this.works.length) {
288
      to = this.works.length;
289
    }
290

    
291
    let works = this.works.slice(from, to);
292

    
293
    for(let work of works) {
294
      for(let pid of work['pids']) {
295
        let identifier: Identifier = Identifier.getIdentifierFromString(pid, false);
296
        this.orcidQuery += (this.orcidQuery ? " or " : "") + ('(pidclassid exact "'+identifier.class+'" and pid="'+StringUtils.URIEncode(identifier.id)+'")');
297
      }
298
    }
299
    this.showLoading = false;
300
    this._getResults(works);
301
  }
302

    
303
  public _getResults(works: any) {
304
    this.searchUtils.status = this.errorCodes.LOADING;
305
    this.disableForms = true;
306
    this.currentResults = [];
307
    this.searchUtils.totalResults = 0;
308

    
309
    // let params: string = this.orcidQuery + this.typeQuery;
310
    //this.subs.push(this._searchResearchResultsService.advancedSearchResults(this.resultType, parameters, page, size, sortBy, this.properties, (refine) ? this.searchPage.getRefineFieldsQuery() : null, this.searchPage.getFields(), refineFieldsFilterQuery)
311
    this.subscriptions.push(this._searchResearchResultsService.searchForMyOrcidLinks(this.resultType, this.orcidQuery, this.typeQuery, 1, 50)
312
    //this.subs.push(this._searchResearchResultsService.advancedSearchResults(this.resultType, parameters, page, size, sortBy, this.properties, null, this.searchPage.getFields(), refineFieldsFilterQuery)
313
      .subscribe(
314
        data => {
315
          let totalResults = data[0];
316
          let results = data[1];
317

    
318
          this.resultsReturned(results, totalResults, works);
319
        },
320
        err => {
321
          console.error("Error getting " + this.getEntityName(this.resultType, true, true));
322
          this.showLoading = false;
323
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
324
          this.disableForms = false;
325
        }
326
      )
327
    );
328
  }
329

    
330
  public resultsReturned(results: any, totalResults, works: any[]) {
331
    this.searchUtils.totalResults = totalResults;
332

    
333
    let resultsFound: Map<string[], number> = new Map<string[], number>();
334

    
335
    for(let work of works) {
336
      results.forEach(result => {
337
        let identifierValues: string[] = [].concat(...Array.from(result.identifiers.values()));
338
        if(work['pids'].some(pid => identifierValues.includes(pid))) {
339

    
340
          let index: number = resultsFound.get(identifierValues);
341
          if(!index) {
342
            this.currentResults.push(this.getResultPreview(result));
343
            index = this.currentResults.length - 1;
344

    
345
            this.currentResults[index].orcidPutCodes = [];
346
            this.currentResults[index].orcidCreationDates = [];
347
            this.currentResults[index].orcidUpdateDates = [];
348
          }
349

    
350
          if (work['putCode']) {
351
            this.currentResults[index].orcidPutCodes.push(work['putCode']);
352
          }
353
          if (work['creationDate']) {
354
            this.currentResults[index].orcidCreationDates.push(work['creationDate']);
355
          }
356
          if (work['updateDate']) {
357
            this.currentResults[index].orcidUpdateDates.push(work['updateDate']);
358
          }
359
        }
360
      })
361
    }
362

    
363
    this.results[this.currentPage-1] = this.currentResults;
364

    
365
    this.searchUtils.status = this.errorCodes.DONE;
366
    if (this.searchUtils.totalResults == 0) {
367
      this.searchUtils.status = this.errorCodes.NONE;
368
    }
369

    
370
    this.disableForms = false;
371
  }
372

    
373
  public resultsReturned2(results: any, totalResults, works: any[]) {
374
    this.searchUtils.totalResults = totalResults;
375

    
376
    for(let result of results) {
377
      let identifierValues: string[] = [].concat(...Array.from(result.identifiers.values()));
378

    
379
      this.currentResults.push(this.getResultPreview(result));
380

    
381
      let filteredWorks = works.filter(work => {
382
        return work['pids'].some(pid => identifierValues.includes(pid));
383
      })
384

    
385
      this.currentResults[this.currentResults.length - 1].orcidPutCodes = [];
386
      this.currentResults[this.currentResults.length - 1].orcidCreationDates = [];
387
      this.currentResults[this.currentResults.length - 1].orcidUpdateDates = [];
388

    
389
      filteredWorks.forEach(work => {
390
        if(work['putCode']) {
391
          this.currentResults[this.currentResults.length - 1].orcidPutCodes.push(work['putCode']);
392
        }
393
        if(work['creationDate']) {
394
          this.currentResults[this.currentResults.length - 1].orcidCreationDates.push(work['creationDate']);
395
        }
396
        if(work['updateDate']) {
397
          this.currentResults[this.currentResults.length - 1].orcidUpdateDates.push(work['updateDate']);
398
        }
399
      });
400
    }
401

    
402
    this.results[this.currentPage-1] = this.currentResults;
403
    this.searchUtils.status = this.errorCodes.DONE;
404
    if (this.searchUtils.totalResults == 0) {
405
      this.searchUtils.status = this.errorCodes.NONE;
406
    }
407

    
408
    this.disableForms = false;
409
  }
410

    
411
  public getResultPreview(result: SearchResult): ResultPreview {
412
    return ResultPreview.searchResultConvert(result, (result.entityType)?result.entityType:this.resultType);
413
  }
414

    
415
  public getEntityName(entityType: string, plural: boolean, full: boolean): string {
416
    if (entityType == "publication") {
417
      return "publication" + (plural ? "s" : "");
418
    } else if (entityType == "dataset") {
419
      return (full ? "research data" : ("dataset" + (plural ? "s" : "")));
420
    } else if (entityType == "software") {
421
      return "software";
422
    } else if (entityType == "other") {
423
      return (full ? ("other research product" + (plural ? "s" : "")) : "other");
424
    } else if (entityType == "result") {
425
      return (full ? ("research outcome" + (plural ? "s" : "")) : "result");
426
    }
427
  }
428

    
429
  public pageChanged($event) {
430
    this.currentPage = $event.value;
431
    this.prepareOrcidQuery();
432
  }
433

    
434
  handleError(error, errorMsg: string) {
435
    if(error && (error.status == "401" || error.status == "403")) {
436
      this.requestGrant = true;
437
    } else {
438
      this.showErrorMessage = true;
439
    }
440
    this.showLoading = false;
441
  }
442

    
443
  private updateTitle(title: string) {
444
    this._title.setTitle(title);
445
    this._meta.updateTag({content: title}, "property='og:title'");
446
  }
447

    
448
  private updateDescription(description: string) {
449
    this._meta.updateTag({content: description}, "name='description'");
450
    this._meta.updateTag({content: description}, "property='og:description'");
451
  }
452

    
453
  private updateUrl(url: string) {
454
    this._meta.updateTag({content: url}, "property='og:url'");
455
  }
456
}
(1-1/5)