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 {Loading} from '../../common/modal/loading.component';
7
import {Alert} from '../../common/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

    
23
    // var params = [];
24
      if( this.myClaims == 'true' ){
25
          this.fetchBy = "User";
26
      }else{
27

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

    
33
      }
34

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

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

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

    
98
  navigateTo: string = "Claims";
99
  resultsNum: number ;
100
  claims: string[];
101

    
102
  @ViewChild (Loading) loading : Loading ;
103

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

    
111
  descending = true;
112
  sortby = "date";
113

    
114
  selected=[];
115
  deleteMessage:string = "";
116
  showErrorMessage:boolean = false;
117

    
118
  //params for pagingFormatter to use when navigate to page
119
  params;
120
  @ViewChild(Alert) alert;
121

    
122
  claimsDeleted:number = 0;
123

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

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

    
196
    this.page = page;
197

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

    
216
    if(this.isAdmin === 'true'){
217
      params+=(this.fetchBy=='All'?"":(params.length>0?'&':'')+"fetchBy="+this.fetchBy);
218
      params+=(this.fetchId==''?"":(params.length>0?'&':'')+"fetchId="+this.fetchId);
219
    }
220
    params+=(this. getSortby()=='datedesc'?"":(params.length>0?'&':'')+"sort="+this. getSortby());
221
    params+=(this.keyword==''?"":(params.length>0?'&':'')+"keyword="+this.keyword);
222
    return params;
223
  }
224
  changeLength(){
225
    this.goTo();
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
}
(2-2/3)