Project

General

Profile

1
import {Component, Input, Output, EventEmitter} from '@angular/core';
2
import { ActivatedRoute } from '@angular/router';
3
import {SearchCrossrefService} from '../../services/searchCrossref.service';
4
import {SearchOrcidService} from '../../services/searchOrcid.service';
5
import {SearchPublicationsService} from '../../services/searchPublications.service';
6
import { ErrorCodes} from '../../utils/properties/openaireProperties';
7
import {ClaimResult} from '../../utils/entities/claimEntities.class';
8
import{DOI} from '../../utils/string-utils.class';
9

    
10
@Component({
11
    selector: 'claim-publication',
12
    templateUrl: 'claimPublication.component.html',
13

    
14
})
15
export class ClaimPublicationComponent {
16
  constructor (private _searchCrossrefService: SearchCrossrefService,private _searchOrcidService: SearchOrcidService, private _searchPublicationsService: SearchPublicationsService,
17
  private route: ActivatedRoute) {
18
    var myDate = new Date();
19
    this.todayDate = myDate.getFullYear()+ "-" +(myDate.getMonth() + 1) + "-" + myDate.getDate() ;
20
    this.nextDate =  (myDate.getFullYear()+100)+ "-01-01" ;
21

    
22
  }
23
  ngOnInit() {
24
    this.sub = this.route.params.subscribe(params => {
25
       if(this.keyword !=null && this.keyword.length > 0){
26
        this.search(this.keyword);
27
      }
28
    });
29

    
30
}
31
ngOnDestroy() {
32
  this.sub.unsubscribe();
33
}
34
@Input() public keyword:string = "";
35
@Input() public select:boolean = true ;
36
@Input() public selectedPublications = [] ;
37
@Output() publicationsChange = new EventEmitter();
38
  sub: any;
39
  size:number = 10;
40
  source: string = "crossref";
41
  type : string = "publication";
42
  errorCodes:ErrorCodes = new ErrorCodes();
43

    
44
  crossrefResults=[];
45
  crossrefResultsNum : number = null;
46
  crossrefPage : number = 1;
47
  crossrefStatus:number = this.errorCodes.NONE;
48

    
49
  openaireResults = [];
50
  openaireResultsNum: number ;
51
  openairePage : number = 1;
52
  openaireStatus:number = this.errorCodes.NONE;
53

    
54
  orcidResults: string[];
55
  orcidResultsNum: number ;
56
  totalPages: number;
57
  orcidResultsToShow: string[];
58
  orcidPage : number = 1;
59
  orcidStatus:number = this.errorCodes.NONE;
60
  authorId: string;
61
  authorGivenName: string;
62
  authorFamilyName: string;
63

    
64
  authorIds: string[];
65
  authorGivenNames: string[];
66
  authorFamilyNames: string[];
67

    
68
  authorsNum : number ;
69

    
70
  public warningMessage = "";
71
  public infoMessage = "";
72

    
73
    todayDate = '';
74
    nextDate = '';
75
private DOIs:string[] = [];
76

    
77
public search(term: string){
78
  this.warningMessage = "";
79
  this.infoMessage = "";
80
  this.DOIs = DOI.getDOIsFromString(term);
81
  this.getCrossrefResults(term, this.size,1);
82
  this.searchOrcid(term);
83
  this.searchOpenaire(term, this.size, 1);
84
}
85

    
86

    
87
private   getCrossrefResults (term: string, size : number, page : number)  {
88
    this.crossrefStatus = this.errorCodes.LOADING;
89
    if( this.DOIs.length > 0 ){
90
      this._searchCrossrefService.searchCrossrefByDOIs(this.DOIs).subscribe(
91
        data => {
92
            if(data != null) {
93
                this.crossrefResults = data.items;
94
                this.crossrefPage=page;
95
                this.crossrefResultsNum = data['total-results'];
96
                if(data.items == 0){
97
                    this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
98
                        data => {
99
                            if(data != null) {
100
                                this.crossrefResults = data.items;
101
                                this.crossrefPage=page;
102
                                this.crossrefResultsNum = data['total-results'];
103
                                this.crossrefStatus = this.errorCodes.DONE;
104

    
105
                            }else{
106
                              this.crossrefStatus = this.errorCodes.ERROR;
107
                            }
108
                        },
109
                        err =>{
110
                           console.log(err.status);
111
                           this.crossrefStatus = this.errorCodes.ERROR;
112
                         }
113

    
114
                    );
115
                }else{
116
                  this.crossrefStatus = this.errorCodes.DONE;
117
                }
118
            }
119
         },
120
        err => {
121
          //console.log(err);
122
          this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
123
            data => {
124
              this.crossrefResults = data.items;
125
              this.crossrefPage=page;
126
              this.crossrefResultsNum = data['total-results'];
127
              this.crossrefStatus = this.errorCodes.DONE;
128

    
129
             },
130
            err => {
131
              console.log(err.status);
132
              this.crossrefStatus = this.errorCodes.ERROR;
133
            }
134

    
135
          );
136
        }
137
      );
138

    
139
    }else{
140

    
141

    
142
      this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
143
        data => {
144
            if(data != null) {
145
                this.crossrefResults = data.items;
146
                this.crossrefPage=page;
147
                this.crossrefResultsNum = data['total-results'];
148
                this.crossrefStatus = this.errorCodes.DONE;
149

    
150
            }else{
151
              this.crossrefStatus = this.errorCodes.ERROR;
152
            }
153

    
154
         },
155
        err => {
156
          console.log(err.status);
157
          this.crossrefStatus = this.errorCodes.ERROR;
158
          }
159
      );
160
    }
161
  }
162
  private searchOpenaire(term: string, size : number, page : number)  {
163

    
164
          if(this.DOIs.length > 0 ){
165
            this.openaireStatus = this.errorCodes.LOADING;
166
            this._searchPublicationsService.searchPublicationsByDois(this.DOIs, null, page, size, []).subscribe(
167
              data => {
168
                  if(data != null) {
169
                      this.openairePage=page;
170
                      this.openaireResultsNum = data[0];
171
                      this.openaireResults = data[1];
172
                      this.openaireStatus = this.errorCodes.DONE;
173
                      if(this.openaireResultsNum == 0){
174
                        this.openaireStatus = this.errorCodes.NONE;
175
                      }
176
                  }else {
177
                    this.openaireStatus = this.errorCodes.ERROR;
178
                  }
179
               },
180
              err => {
181
                this.openaireStatus = this.errorCodes.ERROR;
182
                console.log(err.status);
183
              }
184
            );
185
          }else{
186
            this.openaireStatus = this.errorCodes.LOADING;
187
            this._searchPublicationsService.searchPublications('q="'+term+'"', null, page, size, []).subscribe(
188
              data => {
189
                  if(data != null) {
190
                      this.openairePage=page;
191
                      this.openaireResultsNum = data[0];
192
                      this.openaireResults = data[1];
193
                      this.openaireStatus = this.errorCodes.DONE;
194
                      if(this.openaireResultsNum == 0){
195
                        this.openaireStatus = this.errorCodes.NONE;
196
                      }
197
                  }else {
198
                    this.openaireStatus = this.errorCodes.ERROR;
199
                  }
200
               },
201
              err => {
202
                this.openaireStatus = this.errorCodes.ERROR;
203
                console.log(err.status);
204
              }
205
            );
206
          }
207
  }
208

    
209
  private searchOrcid (term: string) {
210
    if(this.DOIs.length > 0){
211
      this.orcidStatus = this.errorCodes.NONE;
212
      return;
213
    }
214
    this.orcidStatus = this.errorCodes.LOADING;
215
    this.authorIds = new Array<string>();
216
    this.authorGivenNames = new Array<string>();
217
    this.authorFamilyNames = new Array<string>();
218

    
219
    this.getOrcidAuthor(term);
220

    
221
    console.info('searchOrcid in searchOrcid file');
222
   }
223

    
224
  private readData(data: any) {
225
    this.authorIds.push(data[2].path);
226

    
227
    if(data[0] != null) {
228
      this.authorGivenNames.push(data[0].value);
229
    } else {
230
      this.authorGivenNames.push("");
231
    }
232
    if(data[1] != null) {
233
      this.authorFamilyNames.push(data[1].value);
234
    } else {
235
      this.authorFamilyNames.push("");
236
    }
237
  }
238

    
239
  private  getOrcidAuthor (term: string)  {
240
    this.orcidResultsNum = null;
241

    
242
    //passing structures in order to fill them in service
243
    this._searchOrcidService.searchOrcidAuthor(term, this.authorIds,
244
      this.authorGivenNames, this.authorFamilyNames).subscribe(
245
        data => {
246
          if(data != null && data == true) {
247
            this.getOrcidResultsById(0);
248
          }
249

    
250
          this.orcidStatus = this.errorCodes.NONE;
251

    
252
        },
253
        err => this.errorHandler(err, term)
254

    
255
      );
256
    }
257

    
258
    private  errorHandler(err: any, term: string) {
259
      if(err.status == 404){
260
        this.getOrcidAuthors(term);
261
      } else {
262
        this.orcidStatus = this.errorCodes.ERROR;
263
        console.log(err.status);
264

    
265
      }
266
    }
267

    
268
    private  getOrcidAuthors (term: string)  {
269
      this.orcidResultsNum = null;
270

    
271
      //passing structures in order to fill them in service
272
      this._searchOrcidService.searchOrcidAuthors(term, this.authorIds,
273
        this.authorGivenNames, this.authorFamilyNames).subscribe(
274
          data => {
275
            if(data != null && data == true) {
276
              this.getOrcidResultsById(0);
277
            }else{
278
              this.orcidStatus = this.errorCodes.ERROR;
279
            }
280

    
281
          },
282
          err => {
283
            this.orcidStatus = this.errorCodes.ERROR;
284
            console.log(err.status);
285
          }
286
        );
287
      }
288

    
289

    
290
      private    getOrcidResultsById (index:number) {
291
        if(this.authorIds.length > index) {
292
          let id = this.authorIds[index];
293

    
294
          console.info("getOrcidResultsById: "+id);
295
          this._searchOrcidService.searchOrcidPublications(id).subscribe(
296
            data => {
297
              if(data != null) {
298
                this.orcidResults=data['orcid-work'];
299
                this.orcidResultsNum = data['orcid-work'].length;
300
                this.orcidPage = 1;
301
                if((this.orcidResultsNum % this.size) == 0){
302
                  this.totalPages=parseInt(''+(this.orcidResultsNum/this.size));
303
                } else{
304
                  this.totalPages=parseInt(''+(this.orcidResultsNum/this.size+1));
305
                }
306

    
307
                this.orcidResultsToShow = this.orcidResults.slice(0,10);
308

    
309
                this.orcidStatus = this.errorCodes.DONE;
310
                if(this.orcidResultsNum == 0){
311
                  this.orcidStatus = this.errorCodes.NONE;
312
                }
313
              } else {
314
                this.orcidResultsNum = 0;
315
                this.totalPages=0;
316
                this.orcidStatus = this.errorCodes.NONE;
317
              }
318
              this.authorGivenName = this.authorGivenNames[index];
319
              this.authorFamilyName = this.authorFamilyNames[index];
320
              this.authorId = id;
321
            },
322
            err => {
323
              console.log(err.status);
324
              this.orcidStatus = this.errorCodes.ERROR;
325
            }
326
          );
327

    
328
        }
329
      }
330
  private  add(item, id, itemSource, itemType, itemUrl, itemTitle,  date, accessMode){
331
     var result: ClaimResult ;
332
      if(itemSource == 'crossref'){
333
        date = (date == null) ? null : date.substring(0,10);
334
        result = {id: id, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate: this.nextDate, date};
335
      }else if (itemSource == 'orcid'){
336
        date = (date == null) ? null : date + "-01.-01"
337
          result = {id:id, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate: this.nextDate, date};
338
      }else if (itemSource == 'openaire'){
339
        //TODO put right access rights
340
        result = {id:id, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: accessMode, embargoEndDate: null, date};
341

    
342
      }
343
      console.info("Added result:"+result.date);
344
        var found:boolean = this.isSelected(result.id);
345
        this.warningMessage = "";
346
        if (!found) {
347
          this.selectedPublications.push(result);
348
          this.publicationsChange.emit({
349
             value: this.selectedPublications
350
           });
351
        }else{
352
            this.warningMessage = "Publication already in selected list";
353
        }
354

    
355
   }
356
  private  remove(item){
357
     this.warningMessage = "";
358
     this.infoMessage = "";
359
        var index:number =this.selectedPublications.indexOf(item);
360
        item.selected=false;
361
         if (index > -1) {
362
            this.selectedPublications.splice(index, 1);
363
            this.publicationsChange.emit({
364
              value: this.selectedPublications
365
            });
366
        }
367

    
368
   }
369
private crossrefPageChange($event) {
370
     this.crossrefPage=$event.value;
371
     this.crossrefResults=[];
372
     console.log("Crossref chaenged "+this.crossrefPage);
373
     this.getCrossrefResults(this.keyword,this.size,this.crossrefPage);
374
}
375
private orcidPageChange($event) {
376
     this.orcidPage=$event.value;
377
     this.orcidResultsToShow=[];
378
     this.orcidResultsToShow = this.orcidResults.slice(($event.value-1)*this.size, $event.value*this.size);
379
}
380
private openairePageChange($event) {
381
     this.openairePage=$event.value;
382
     this.searchOpenaire(this.keyword,this.size,this.openairePage);
383
}
384

    
385
  private   isSelected(id:string){
386

    
387
      var found:boolean = false;
388
      this.warningMessage = "";
389
      for (var _i = 0; _i < this.selectedPublications.length; _i++) {
390
          let item = this.selectedPublications[_i];
391
          if(item.id == id){
392
                found=true;
393
                this.warningMessage = "Publication already in selected list";
394
          }
395
       }
396
       return found;
397

    
398

    
399
    }
400
}
(5-5/8)