Project

General

Profile

1
import {Component, ViewChild, Input} from '@angular/core';
2
import {Location} from '@angular/common';
3
import {Observable}       from 'rxjs/Observable';
4
import {ActivatedRoute, Router} from '@angular/router';
5
import {ClaimsService} from '../../services/claims.service';
6
import {ModalLoading} from '../../utils/modal/loading.component';
7
import {AlertModal} from '../../utils/modal/alert';
8

    
9

    
10
@Component({
11
    selector: 'claims',
12
    templateUrl: 'claims.component.html',
13
    providers:[ ClaimsService]
14

    
15
})
16
export class ClaimsComponent {
17
  constructor (private _claimService: ClaimsService,  private route: ActivatedRoute, private _router:Router, private location: Location) {
18
   }
19

    
20
  ngOnInit() {
21
     this.sub = this.route.queryParams.subscribe(params => {
22
      if( this.myClaims == 'true' ){
23
          this.fetchBy = "User";
24
      }else{
25

    
26
        this.fetchBy = params['fetchBy'];
27
        this.fetchBy = (this.types.indexOf(this.fetchBy) != -1)? this.fetchBy:'All';
28
        this.fetchId = params['fetchId'];
29
        this.fetchId=this.fetchId?this.fetchId:'';
30

    
31
      }
32

    
33
      let page = (params['page']=== undefined)?1:+params['page'];
34
      let size = (params['size']=== undefined)?10:+params['size'];
35

    
36
      this.keyword = (params['keyword']?params['keyword']:"");
37
      this.inputkeyword = this.keyword;
38
      this.page = ( page <= 0 ) ? 1 : page;
39
      this.size = ( size <= 0 ) ? 10 : size;
40
      this.entityTypes = []//(params['types']?params['types']:[]);
41
      this.setTypes(params['types']); // check the appropriate checkboxes
42
      this.setSortby(params['sort']);
43
      this.getClaims();
44

    
45
    });
46
    // this.sub = this.route.params.subscribe(params => {
47
    //   console.info(this.isAdmin+"  "+this.myClaims+" Fetch by: "+this.fetchBy+" Fetch id: "+this.fetchId);
48
    //   if( this.myClaims == 'true' ){
49
    //     console.info("Is myclaims");
50
    //       this.fetchBy = "User";
51
    //   }else{
52
    //     console.info("Is admin");
53
    //
54
    //     console.info(this.isAdmin);
55
    //
56
    //     this.fetchBy = params['fetchBy'];
57
    //     this.fetchBy = (this.types.indexOf(this.fetchBy) != -1)? this.fetchBy:'All';
58
    //     this.fetchId = params['fetchId'];
59
    //     console.info("Fetch by:"+this.fetchBy+"Fetch id:"+this.fetchId);
60
    //     this.fetchId=this.fetchId?this.fetchId:'';
61
    //
62
    //   }
63
    //
64
    //   console.info(this.isAdmin+"  "+this.myClaims+" Fetch by: "+this.fetchBy+" Fetch id: "+this.fetchId);
65
    //   let page = (params['page']=== undefined)?1:+params['page'];
66
    //   let size = (params['size']=== undefined)?10:+params['size'];
67
    //
68
    //   this.keyword = (params['keyword']?params['keyword']:"");
69
    //   this.inputkeyword = this.keyword;
70
    //   this.page = ( page <= 0 ) ? 1 : page;
71
    //   this.size = ( size <= 0 ) ? 10 : size;
72
    //   this.entityTypes = []//(params['types']?params['types']:[]);
73
    //   this.setTypes(params['types']); // check the appropriate checkboxes
74
    //   this.setSortby(params['sort']);
75
    //   this.getClaims();
76
    //   console.info("params: "+params['page']+"   page "+page +" this.page: "+this.page );
77
    // });
78
  }
79
  ngOnDestroy() {
80
    this.sub.unsubscribe();
81
  }
82
    sub: any;
83
  //string because comes as input from component directive
84
  @Input() enableDelete: string = 'false';
85
  @Input() myClaims: string= 'false' ;
86
  @Input() isAdmin:string = 'false';
87
  page : number;
88
  size:number;
89
  keyword:string; // the keyword string to give to the request as parameter
90
  inputkeyword:string; // the string written in the input field (keyword=inputkeyword when its length is bigger than 3 and the user stops typing)
91
  lengths = [10,20,30,50];
92
  types = ["All","Project","Context","Result","User"];
93
  @Input() fetchBy:string;
94
  @Input() fetchId:string;
95

    
96
  navigateTo: string = "Claims";
97
  resultsNum: number ;
98
  claims: string[];
99

    
100
  @ViewChild (ModalLoading) loading : ModalLoading ;
101

    
102
  //checkboxes:
103
  publicationCB = false;
104
  datasetCB = false;
105
  contextCB = false;
106
  projectCB = false;
107
  entityTypes : string[] =[] ;
108

    
109
  descending = true;
110
  sortby = "date";
111

    
112
  selected=[];
113
  deleteMessage:string = "";
114
  showErrorMessage:boolean = false;
115

    
116
  //params for pagingFormatter to use when navigate to page
117
  params;
118
  @ViewChild(AlertModal) alert;
119

    
120
  claimsDeleted:number = 0;
121

    
122
  getClaims () {
123
    this.selected=[];
124
    var types = '';
125
    this.showErrorMessage = false;
126
    for (var type of this.entityTypes){
127
      types+=(types.length>0?'&':'')+"types="+type;
128
    }
129
    if(this.fetchBy =="Project" ){
130
      this._claimService.getClaimsByProject(this.size,this.page,this.fetchId,this.keyword,this.sortby,this.descending, types).subscribe(
131
        data => {
132
          this.claims = data.data;
133
          this.resultsNum= data.total;
134
          },
135
        err => {
136
          console.error(err);
137
          this.showErrorMessage = true;
138
        }
139
      );
140
    }else if(this.fetchBy =="User"){
141
      this._claimService.getClaimsByUser(this.size,this.page,this.fetchId,this.keyword,this.sortby,this.descending, types).subscribe(
142
        data => {
143
          this.claims = data.data;
144
          this.resultsNum= data.total;
145
          },
146
          err => {
147
            console.error(err);
148
            this.showErrorMessage = true;
149
          }
150
      );
151
    }else if(this.fetchBy =="Result"){
152
      this._claimService.getClaimsByResult(this.size,this.page,this.fetchId,this.keyword,this.sortby,this.descending, types).subscribe(
153
        data => {
154
          this.claims = data.data;
155
          this.resultsNum= data.total;
156
          },
157
          err => {
158
            console.error(err);
159
            this.showErrorMessage = true;
160
          }
161
      );
162
    }else if(this.fetchBy =="Context"){
163
      this._claimService.getClaimsBycontext(this.size,this.page,this.fetchId,this.keyword,this.sortby,this.descending, types).subscribe(
164
        data => {
165
          this.claims = data.data;
166
          this.resultsNum= null;
167
          this.resultsNum= data.total;//data.length; //TODO get the total results num
168
          },
169
          err => {
170
            console.error(err);
171
            this.showErrorMessage = true;
172
          }
173
      );
174
    }else{
175
      this._claimService.getClaims(this.size,this.page,this.keyword,this.sortby,this.descending, types).subscribe(
176
        data => {
177
          this.claims = data.data;
178
          this.resultsNum = null;
179
          this.resultsNum= data.total;//data.length; //TODO get the total results num
180
          },
181
          err => {
182
            console.error(err);
183
            this.showErrorMessage = true;
184
          }
185
      );
186
    }
187
  }
188

    
189
  goToClaim(claimId: number){
190
    this._router.navigate( ['Claim', { id: claimId}] );
191
  }
192
  goTo(page:number = 1){
193

    
194
    this.page = page;
195

    
196
    this.location.go(location.pathname,this.getParametersString());
197
    this.getClaims();
198
  }
199
  getParameters(){
200
    let params={ page: this.page, size: this.size, types: this.entityTypes, fetchBy: this.fetchBy, fetchId:this.fetchId, keyword : this.keyword, sort: this.getSortby()  };
201
    return params;
202
  }
203
  getParametersString(){
204
    var params='';
205
    params+=(this.page==1?"":(params.length>0?'&':'')+"page="+this.page);
206
    params+=(this.size==10?"":(params.length>0?'&':'')+"size="+this.size);
207
    // params+=(this.entityTypes==''?"":(params.length>0?'&':'')+"types="+this.entityTypes);
208
    var types="";
209
    for (var type of this.entityTypes){
210
         types+=(types.length>0?',':'')+type;
211
    }
212
    params+=(types.length>0)?"types="+types:"";
213

    
214
    if(this.isAdmin === 'true'){
215
      params+=(this.fetchBy=='All'?"":(params.length>0?'&':'')+"fetchBy="+this.fetchBy);
216
      params+=(this.fetchId==''?"":(params.length>0?'&':'')+"fetchId="+this.fetchId);
217
    }
218
    params+=(this. getSortby()=='datedesc'?"":(params.length>0?'&':'')+"sort="+this. getSortby());
219
    params+=(this.keyword==''?"":(params.length>0?'&':'')+"keyword="+this.keyword);
220
    return params;
221
  }
222
  changeSize(size: number ){
223
    this.size = size;
224
    this.goTo();
225
  }
226

    
227
  clearFilters(){
228
    this.keyword = '';
229
    this.inputkeyword = '';
230
    this.publicationCB = false;
231
    this.projectCB = false;
232
    this.datasetCB = false;
233
    this.contextCB = false;
234
    this.entityTypes = [];
235
    this.goTo();
236
   }
237
   changeOrderby(sortby:string){
238
     if(sortby==this.sortby){
239
       this.descending = !this.descending;
240
     }else{
241
       this.sortby = sortby;
242
       this.descending = false;
243
     }
244
     this.goTo();
245
   }
246
   setSortby(sortby:string){
247
     if(!sortby|| sortby == "datedesc"){
248
       this.descending = true;
249
       this.sortby = "date";
250
     }else if(sortby == "dateasc"){
251
       this.descending = false;
252
       this.sortby = "date";
253
     }else if(sortby == "userasc"){
254
       this.descending = false;
255
       this.sortby = "user";
256
     }else if(sortby == "userdesc"){
257
       this.descending = true;
258
       this.sortby = "user";
259
     }if(sortby =="sourceasc"){
260
       this.descending = false;
261
       this.sortby = "source";
262
     }else if(sortby == "sourcedesc"){
263
       this.descending = true;
264
       this.sortby = "source";
265
     }else if(sortby == "targetasc"){
266
       this.descending = false;
267
       this.sortby = "target";
268
     }else if(sortby == "targetdesc"){
269
       this.descending = true;
270
       this.sortby = "target";
271
     }
272
   }
273
   getSortby():string{
274
     if(this.descending){
275
       return this.sortby+"desc";
276
     }else{
277
       return this.sortby+"asc";
278
     }
279

    
280
   }
281
   changeType(){
282
     this.entityTypes = [];
283
     if(this.publicationCB){
284
       this.entityTypes.push('publication');
285
     }
286
     if(this.datasetCB){
287
       this.entityTypes.push('dataset');
288
     }
289
     if(this.projectCB){
290
       this.entityTypes.push('project');
291
     }
292
     if(this.contextCB){
293
       this.entityTypes.push('context');
294
     }
295

    
296
    //  if(this.publicationCB  == true && this.datasetCB  == true && this.contextCB  == true && this.projectCB  == true ){
297
    //    this.entityTypes="";
298
    //  }else{
299
    //    this.entityTypes = "";
300
    //    if(this.publicationCB == true){
301
    //      this.entityTypes = "publication";
302
    //    }
303
    //    if(this.datasetCB  == true){
304
    //      this.entityTypes += (this.entityTypes.length > 0?",":"")+"dataset";
305
    //    }
306
    //    if(this.contextCB  == true){
307
    //      this.entityTypes += (this.entityTypes.length > 0?",":"")+"context";
308
    //    }
309
    //    if(this.projectCB  == true){
310
    //      this.entityTypes += (this.entityTypes.length > 0?",":"")+"project";
311
    //    }
312
    //  }
313
    //  console.debug("Type changed: "+this.entityTypes+"   "+this.publicationCB+ this.datasetCB + this.contextCB  + this.projectCB);
314
     this.goTo();
315
   }
316
   setTypes(types:string){
317
     if(!types){
318
       return;
319
     }
320
     if(types.length > 0){
321
       this.entityTypes = [];
322
       if(types.indexOf("publication")!=-1){
323
         this.publicationCB = true;
324
         this.entityTypes.push("publication");
325
       }
326
       if(types.indexOf("dataset")!=-1){
327
         this.datasetCB = true;
328
         this.entityTypes.push("dataset");
329
       }
330
       if(types.indexOf("project")!=-1){
331
         this.projectCB = true;
332
         this.entityTypes.push("project");
333
       }
334
       if(types.indexOf("context")!=-1){
335
         this.contextCB = true;
336
         this.entityTypes.push("context");
337
       }
338
     }
339
     if(this.publicationCB && this.datasetCB && this.contextCB && this.projectCB){
340
       this.entityTypes=[];
341
     }
342
   }
343
   changekeyword(){
344

    
345
     if(this.inputkeyword.length >= 3 || this.inputkeyword.length == 0 ){
346
       this.keyword = this.inputkeyword;
347
       this.page = 1;
348
       this.goTo();
349
     }
350

    
351
   }
352
   select(item:any,event){
353
     this.deleteMessage="";
354
     var value = event.currentTarget.checked;
355
     if(value){
356
       this.selected.push(item);
357
     }else{
358
       for (var _i = 0; _i < this.selected.length; _i++) {
359
           let claim = this.selected[_i];
360
           if(claim['id'] == item.id){
361
                 this.selected.splice(_i, 1);
362
           }
363
        }
364

    
365

    
366
     }
367
   }
368
   selectAll(){
369
     this.selected = [];
370
    for (var _i = 0; _i < this.claims.length; _i++) {
371
       let claim = this.claims[_i];
372
       this.selected.push(claim);
373
     }
374
      this.deleteMessage = "";
375
   }
376
   deselectAll(){
377
     this.selected = [];
378
     this.deleteMessage="";
379
   }
380
   isSelected(id:string){
381
     for (var _i = 0; _i < this.selected.length; _i++) {
382
         let claim = this.selected[_i];
383
         if(claim['id'] == id){
384
              return true;
385
         }
386
      }
387
      return false;
388
   }
389

    
390

    
391
     confirmOpen(){
392
       if(this.selected.length <= 0){
393

    
394
       }else{
395
         this.alert.cancelButton = true;
396
         this.alert.okButton = true;
397
         this.alert.alertTitle = "Delete "+this.selected.length+" claim(s)";
398
         this.alert.message = this.selected.length+" claims will be deleted. Do you want to proceed? ";
399
         this.alert.okButtonText = "Yes";
400
         this.alert.cancelButtonText = "No";
401
         this.alert.open();
402
       }
403
     }
404
      confirmClose(data){
405
       this.delete();
406
     }
407
   delete(){
408
     this.deleteMessage="";
409
     this.loading.open();
410
     this.claimsDeleted = 0;
411
     var ids = [];
412
     for (var i = 0; i < this.selected.length; i++){
413
        var id  =this.selected[i].id;
414
        ids.push(id);
415
        // var selected  =this.selected[i].id;
416
        // console.warn("Deleting claim with id:"+id);
417
        //  this.deleteById(id);
418
         //TODO for multiple concurrent
419
        }
420
         this.batchDeleteById(ids);
421
    }
422

    
423
    deleteById(id:string){
424

    
425
         console.warn("Deleting claim with id:"+id);
426
         // this._claimService.deleteClaimById(id);
427
         this._claimService.deleteClaimById(id).subscribe(
428
            res =>  {
429
              console.info('Delete response'+res.code );
430
              console.warn("Deleted claim with id:"+ id);
431
              //remove this claim from the
432
             let newClaims=this.claims;
433
             for (var _i = 0; _i < this.claims.length; _i++) {
434
                 let claim = this.claims[_i];
435
                 if(claim['id'] == id){
436
                       newClaims.splice(_i, 1);
437
                 }
438
              }
439
              //TODO should call getClaims???
440
              this.claimsDeleted++;
441
              this.claims = newClaims;
442
              if(this.claimsDeleted == this.selected.length){
443
                this.resultsNum = this.resultsNum - this.selected.length;
444
                this.loading.close();
445
                this.selected = [];
446
              }
447

    
448

    
449
           });
450
    }
451
    batchDeleteById(ids:string[]){
452

    
453
         console.warn("Deleting claim with ids:"+ids);
454
         this._claimService.deleteBulk(ids).subscribe(
455
            res =>  {
456
              console.info('Delete response'+res.code );
457
              console.warn("Deleted ids:"+ res.deletedIds);
458
              console.warn("Not found ids:"+ res.notFoundIds);
459
              //remove this claim from the
460
             let newClaims=this.claims;
461
             for(var id of res.deletedIds){
462
               for (var _i = 0; _i < this.claims.length; _i++) {
463
                   let claim = this.claims[_i];
464
                   if(claim['id'] == id){
465
                         newClaims.splice(_i, 1);
466
                   }
467
                }
468
                for (var _i = 0; _i < this.selected.length; _i++) {
469
                    let claim = this.selected[_i];
470
                    if(claim['id'] == id){
471
                          this.selected.splice(_i, 1);
472
                    }
473
                 }
474
              }
475
              this.claims = newClaims;
476
              this.resultsNum = this.resultsNum - res.deletedIds.length;
477
              this.loading.close();
478
              if(res.deletedIds.length>0){
479
                  this.deleteMessage=this.deleteMessage+'<div class = "alert alert-success " >'+res.deletedIds.length+' claim(s) successfully deleted.</div>';
480
              }
481
              if(res.notFoundIds.length>0){
482
                  this.deleteMessage=this.deleteMessage+'<div class = "alert alert-warning " >'+res.notFoundIds.length+' claim(s) couldn\'t be deleted.</div>';
483
              }
484
           });
485
    }
486
    pageChange($event) {
487
      var page:number = +$event.value
488
      this.goTo(page);
489
    }
490
}
(8-8/8)