Project

General

Profile

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

    
10

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

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

    
23
  }
24
  ngOnInit() {
25
    this.sub = this.route.params.subscribe(params => {
26
      // let page = +params['page'];
27
      // let size = +params['size'];
28
      // this.page = ( page <= 0 ) ? 1 : page;
29
      // this.size = ( size <= 0 ) ? 10 : size;
30
       if(this.keyword !=null && this.keyword.length > 0){
31
        this.search(this.keyword);
32
      }
33
    });
34

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

    
49
  crossrefResults=[];
50
  crossrefResultsNum : Observable<number> ;
51
  crossrefPage : number = 1;
52
  crossrefStatus:number = this.errorCodes.NONE;
53

    
54
  openaireResults = [];
55
  openaireResultsNum: number ;
56
  openairePage : number = 1;
57
  openaireStatus:number = this.errorCodes.NONE;
58

    
59
  orcidResults: string[];
60
  orcidResultsNum: number ;
61
  totalPages: number;
62
  orcidResultsToShow: string[];
63
  orcidPage : number = 1;
64
  orcidStatus:number = this.errorCodes.NONE;
65
  authorId: string;
66
  authorGivenName: string;
67
  authorFamilyName: string;
68

    
69
  authorIds: string[];
70
  authorGivenNames: string[];
71
  authorFamilyNames: string[];
72

    
73
  authorsNum : number ;
74

    
75
  public warningMessage = "";
76
  public infoMessage = "";
77

    
78
    todayDate = '';
79
    nextDate = '';
80

    
81

    
82
public search(term: string){
83
  this.warningMessage = "";
84
  this.infoMessage = "";
85
  this.getCrossrefResults(term, this.size,1);
86
  this.searchOrcid(term);
87
  this.searchOpenaire(term, this.size, 1);
88
}
89

    
90

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

    
109
                            }
110
                            this.crossrefStatus = this.errorCodes.ERROR;
111

    
112
                        },
113
                        err =>{
114
                           console.error(err.status);
115
                           this.crossrefStatus = this.errorCodes.ERROR;
116
                         }
117

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

    
131
             },
132
            err => {
133
              console.error(err.status);
134
              this.crossrefStatus = this.errorCodes.ERROR;
135
            }
136

    
137
          );
138
        }
139
      );
140

    
141
    }else{
142

    
143

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

    
152
            }
153
            this.crossrefStatus = this.errorCodes.ERROR;
154

    
155
         },
156
        err => {
157
          console.error(err.status);
158
          this.crossrefStatus = this.errorCodes.ERROR;
159
          }
160
      );
161
    }
162
  }
163
  private searchOpenaire(term: string, size : number, page : number)  {
164
          this.openaireStatus = this.errorCodes.LOADING;
165
          this._searchPublicationsService.searchPublications('q='+term+'&op=and', null, page, size, []).subscribe(
166
            data => {
167
                if(data != null) {
168
                    this.openairePage=page;
169
                    this.openaireResultsNum = data[0];
170
                    this.openaireResults = data[1];
171
                    this.openaireStatus = this.errorCodes.DONE;
172
                    if(this.openaireResultsNum == 0){
173
                      this.openaireStatus = this.errorCodes.NONE;
174
                    }
175
                }else {
176
                  this.openaireStatus = this.errorCodes.ERROR;
177
                }
178
             },
179
            err => {
180
              console.error(err.status);
181
              this.openaireStatus = this.errorCodes.ERROR;
182
            }
183
          );
184

    
185
  }
186

    
187
  private searchOrcid (term: string) {
188
    this.orcidStatus = this.errorCodes.LOADING;
189
    this.authorIds = new Array<string>();
190
    this.authorGivenNames = new Array<string>();
191
    this.authorFamilyNames = new Array<string>();
192

    
193
    this.getOrcidAuthor(term);
194

    
195
    console.info('searchOrcid in searchOrcid file');
196
   }
197

    
198
  private readData(data: any) {
199
    this.authorIds.push(data[2].path);
200

    
201
    if(data[0] != null) {
202
      this.authorGivenNames.push(data[0].value);
203
    } else {
204
      this.authorGivenNames.push("");
205
    }
206
    if(data[1] != null) {
207
      this.authorFamilyNames.push(data[1].value);
208
    } else {
209
      this.authorFamilyNames.push("");
210
    }
211
  }
212

    
213
  private  getOrcidAuthor (term: string)  {
214
    this.orcidResultsNum = null;
215

    
216
    //passing structures in order to fill them in service
217
    this._searchOrcidService.searchOrcidAuthor(term, this.authorIds,
218
      this.authorGivenNames, this.authorFamilyNames).subscribe(
219
        data => {
220
          if(data != null && data == true) {
221
            this.getOrcidResultsById(0);
222
          }
223

    
224
          this.orcidStatus = this.errorCodes.NONE;
225

    
226
        },
227
        err => this.errorHandler(err, term)
228

    
229
      );
230
    }
231

    
232
    private  errorHandler(err: any, term: string) {
233
      if(err.status == 404){
234
        this.getOrcidAuthors(term);
235
      } else {
236
        console.error(err.status);
237
        this.orcidStatus = this.errorCodes.ERROR;
238

    
239
      }
240
    }
241

    
242
    private  getOrcidAuthors (term: string)  {
243
      this.orcidResultsNum = null;
244

    
245
      //passing structures in order to fill them in service
246
      this._searchOrcidService.searchOrcidAuthors(term, this.authorIds,
247
        this.authorGivenNames, this.authorFamilyNames).subscribe(
248
          data => {
249
            if(data != null && data == true) {
250
              this.getOrcidResultsById(0);
251
            }else{
252
              this.orcidStatus = this.errorCodes.ERROR;
253
            }
254

    
255
          },
256
          err => {
257
            console.error(err.status);
258
            this.orcidStatus = this.errorCodes.ERROR;
259
          }
260
        );
261
      }
262

    
263

    
264
      private    getOrcidResultsById (index:number) {
265
        if(this.authorIds.length > index) {
266
          let id = this.authorIds[index];
267

    
268
          console.info("getOrcidResultsById: "+id);
269
          this._searchOrcidService.searchOrcidPublications(id).subscribe(
270
            data => {
271
              if(data != null) {
272
                this.orcidResults=data['orcid-work'];
273
                this.orcidResultsNum = data['orcid-work'].length;
274
                this.orcidPage = 1;
275
                if((this.orcidResultsNum % this.size) == 0){
276
                  this.totalPages=parseInt(''+(this.orcidResultsNum/this.size));
277
                } else{
278
                  this.totalPages=parseInt(''+(this.orcidResultsNum/this.size+1));
279
                }
280

    
281
                this.orcidResultsToShow = this.orcidResults.slice(0,10);
282

    
283
                this.orcidStatus = this.errorCodes.DONE;
284
                if(this.orcidResultsNum == 0){
285
                  this.orcidStatus = this.errorCodes.NONE;
286
                }
287
              } else {
288
                this.orcidResultsNum = 0;
289
                this.totalPages=0;
290
                this.orcidStatus = this.errorCodes.NONE;
291
              }
292
              this.authorGivenName = this.authorGivenNames[index];
293
              this.authorFamilyName = this.authorFamilyNames[index];
294
              this.authorId = id;
295
            },
296
            err => {
297
              console.error(err.status);
298
              this.orcidStatus = this.errorCodes.ERROR;
299
            }
300
          );
301

    
302
        }
303
      }
304
  private  add(item, id, itemSource, itemType, itemUrl, itemTitle,  date, accessMode){
305
     var result: ClaimResult ;
306
      if(itemSource == 'crossref'){
307
        date = (date == null) ? null : date.substring(0,10);
308
        result = {id: id, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate: this.nextDate, date};
309
      }else if (itemSource == 'orcid'){
310
        date = (date == null) ? null : date + "-01.-01"
311
          result = {id:id, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate: this.nextDate, date};
312
      }else if (itemSource == 'openaire'){
313
        //TODO put right access rights
314
        result = {id:id, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: accessMode, embargoEndDate: null, date};
315

    
316
      }
317
      console.info("Added result:"+result.date);
318
        var found:boolean = this.isSelected(result.id);
319
        this.warningMessage = "";
320
        if (!found) {
321
          this.selectedPublications.push(result);
322
          this.publicationsChange.emit({
323
             value: this.selectedPublications
324
           });
325
        }else{
326
            this.warningMessage = "Publication already in selected list";
327
        }
328

    
329
   }
330
  private  remove(item){
331
     this.warningMessage = "";
332
     this.infoMessage = "";
333
        var index:number =this.selectedPublications.indexOf(item);
334
        item.selected=false;
335
         if (index > -1) {
336
            this.selectedPublications.splice(index, 1);
337
            this.publicationsChange.emit({
338
              value: this.selectedPublications
339
            });
340
        }
341

    
342
   }
343
private crossrefPageChange($event) {
344
     this.crossrefPage=$event.value;
345
     this.crossrefResults=[];
346
     console.log("Crossref chaenged "+this.crossrefPage);
347
     this.getCrossrefResults(this.keyword,this.size,this.crossrefPage);
348
}
349
private orcidPageChange($event) {
350
     this.orcidPage=$event.value;
351
     this.orcidResultsToShow=[];
352
     this.orcidResultsToShow = this.orcidResults.slice(($event.value-1)*this.size, $event.value*this.size);
353
}
354
private openairePageChange($event) {
355
     this.openairePage=$event.value;
356
     this.searchOpenaire(this.keyword,this.size,this.openairePage);
357
}
358

    
359
  private   isSelected(id:string){
360

    
361
      var found:boolean = false;
362
      this.warningMessage = "";
363
      for (var _i = 0; _i < this.selectedPublications.length; _i++) {
364
          let item = this.selectedPublications[_i];
365
          if(item.id == id){
366
                found=true;
367
                this.warningMessage = "Publication already in selected list";
368
          }
369
       }
370
       return found;
371

    
372

    
373
    }
374
}
(5-5/8)