Project

General

Profile

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

    
8

    
9
@Component({
10
    selector: 'claim-insert',
11
    template: `
12

    
13
    <button  *ngIf="claiming == false && showButton == true" (click)="validateInsertions()"  class="btn btn-primary" style="float:right">Finish</button>
14
    <div *ngIf="errorMessage.length > 0">
15
      <div class="alert alert-danger" role="alert">{{errorMessage}}</div>
16
    </div>
17
    <div *ngIf="warningMessage.length > 0">
18
      <div class="alert alert-warning" role="alert">{{warningMessage}}</div>
19
    </div>
20
    <modal-loading [message]= "'Please wait...'"></modal-loading>
21
    <modal-alert (alertOutput)="confirmClose($event)">
22
    </modal-alert>
23
    `
24
})
25
export class ClaimInsertComponent {
26
  constructor (private claimService: ClaimsService, private _router:Router) {}
27
  ngOnInit() {
28
    // console.info("Inlineentity:" +(this.inlineEntity)?this.inlineEntity+(this.inlineEntity.id)?this.inlineEntity.id:"no id":"null"+ + " show "+ (!this.claiming  && this.showButton) );
29
  }
30

    
31

    
32
  @Input() public contexts;
33
  @Input() public projects;
34
  @Input() public publications;
35
  @Input() public datasets;
36
  @Input() public showButton:boolean = true;
37
  @Input() show='claim';
38
  @Input() inline: boolean = false; // link from landing page?
39
  @Input() inlineEntity; // the entity from the landing page
40
  @Output() showChange = new EventEmitter();
41

    
42
  @ViewChild (ModalLoading) loading : ModalLoading ;
43
  @ViewChild(AlertModal) alert;
44

    
45
  public claiming =false;
46
  public error = false;
47
  public errorMessage = "";
48
  public warningMessage = "";
49
  public claimsTODO:number = 0;
50
  public claims:number = 0;
51
  public errorclaims:number = 0;
52

    
53
public validateInsertions(){
54
  // console.info("Inlineentity:" +(this.inlineEntity)?this.inlineEntity+(this.inlineEntity.id)?this.inlineEntity.id:"no id":"null"+ + " show "+ (!this.claiming  && this.showButton) );
55
    if(this.validate()){
56
      if(this.validateDates()){
57
        this.insert();
58
        return true;
59
      }
60
    }
61
    return
62
}
63
private insert(){
64
  this.claiming = true;
65
  var user="argirok@di.uoa.gr"
66
  this.loading.open();
67
  var claims = [];
68
  if(this.publications){
69
    console.info("publications: "+this.publications.length);
70

    
71
      for (var i = 0; i < this.publications.length; i++) {
72

    
73
          var result=this.publications[i];
74
          if(this.contexts){
75
            for (var j = 0; j < this.contexts.length; j++) {
76
              var context = this.contexts[j];
77
              var claim = this.createContextClaim(result, context, user);
78
              claims.push(claim);
79
            }
80
          }
81
          if(this.projects){
82
            for (var k = 0; k < this.projects.length; k++) {
83
              var project = this.projects[k];
84
              var projectClaim = this.createProjectClaim(result, project, user);
85
              claims.push(projectClaim);
86
            }
87
          }
88
           if(this.inline && this.inlineEntity){
89
            var resultClaim = this.createResultClaim(this.inlineEntity, result, user);
90
            claims.push(resultClaim);
91
          }
92

    
93
      }
94
    }
95
    if(this.datasets){
96
       for (var i = 0; i < this.datasets.length; i++) {
97
        var result=this.datasets[i];
98
        if(this.contexts){
99
          for (var j = 0; j < this.contexts.length; j++) {
100
            var context = this.contexts[j];
101
            var claim = this.createContextClaim(result, context, user);
102
            claims.push(claim);
103
          }
104
        }
105
        if(this.projects){
106
          for (var k = 0; k < this.projects.length; k++) {
107
            var project = this.projects[k];
108
            var projectClaim = this.createProjectClaim(result, project, user);
109
            claims.push(projectClaim);
110
          }
111
        }
112
        if(this.inline  && this.inlineEntity){
113
          var resultClaim = this.createResultClaim(this.inlineEntity, result, user);
114
          claims.push(resultClaim);
115
        }
116

    
117
      }
118
    }
119
    console.info("try to insert "+claims.length+" claims");
120
    this.claimService.insertBulkClaims(claims).subscribe(
121
      data => {
122
        var inserted = data.insertedIds.length;
123

    
124
        var inserted = data.insertedIds.length;
125
        this.afterclaimsInsertion(data.insertedIds,data.errorInClaims);
126
      },
127
      err =>  {
128
        console.error(err);
129
        this.errorsInClaimsInsertion(err.insertedIds,err.errorInClaims);
130
      }
131
    );
132
}
133
private validate(){
134
  this.warningMessage = "";
135
  this.errorMessage = "";
136
  if(this.datasets && this.datasets.length == 0 && this.publications && this.publications.length == 0){
137
   this.warningMessage = "There are no publications or datasets selected.";
138
 }else if((!this.contexts|| this.contexts.length==0 )&&(!this.projects|| this.projects.length==0 )&&  ( !this.inlineEntity)){
139
   this.warningMessage = "There are no projects or concepts to link.";
140
 // }else if (this.inline && !this.inlineEntity){
141
 //   this.errorMessage = "No inline entity";
142
 //   console.log(this.inline + "   "+ this.inlineEntity);
143
 }else{
144
   return true;
145
 }
146
 return false;
147
}
148
private validateDates(){
149
  if(this.projects){
150
    for (var k = 0; k < this.projects.length; k++) {
151
      var project = this.projects[k];
152
      console.info(project.startDate+" "+project.endDate + " "+project.projectAcronym);
153
      if(this.publications){
154
        for (var i = 0; i < this.publications.length; i++) {
155
          var result = this.publications[i];
156
          if(result.date && result.date != null){
157
            console.info("Date :"+ result.date + " & embargoEndDate :" +result.embargoEndDate );
158
            if((project.startDate && result.date < project.startDate) || ( project.endDate && result.date > project.endDate) ){
159
              this.confirmOpen();
160
              return false;
161
            }
162
          }
163
        }
164
      }
165
      if(this.datasets){
166
        for (var i = 0; i < this.datasets.length; i++) {
167
          var result = this.datasets[i];
168
          if(result.date && result.date != null){
169
            console.info("Date :"+ result.date + " & embargoEndDate :" +result.embargoEndDate );
170
            if((project.startDate && result.date < project.startDate) || ( project.endDate && result.date > project.endDate) ){
171
              this.confirmOpen();
172
              return false;
173
            }
174
          }
175
        }
176
      }
177

    
178

    
179
    }
180
  }
181
  if(this.publications){
182
    for (var i = 0; i < this.publications.length; i++) {
183
      var result = this.publications[i];
184
      if(result.date && result.date != null){
185
        console.info("Date :"+ result.date + " & embargoEndDate :" +result.embargoEndDate );
186
         if((result.embargoEndDate && result.embargoEndDate != null) && result.date >result.embargoEndDate ){
187
          this.confirmOpen();
188
          return false;
189
        }
190
      }
191
    }
192
  }
193
  if(this.datasets){
194
    for (var i = 0; i < this.datasets.length; i++) {
195
      var result = this.datasets[i];
196
      if(result.date && result.date != null){
197
        console.info("Date :"+ result.date + " & embargoEndDate :" +result.embargoEndDate );
198
        if((result.embargoEndDate && result.embargoEndDate != null) && result.date >result.embargoEndDate ){
199
          this.confirmOpen();
200
          return false;
201
        }
202
      }
203
    }
204
  }
205
 return true;
206
}
207
private afterclaimsInsertion(insertedIds, errorInClaims){
208
  this.loading.close();
209
  if(errorInClaims.length == 0){
210
    if(this.inline){
211
      this.show = "end";
212
    }else{
213
      this._router.navigate( ['/myclaims'] );
214
    }
215
    this.showChange.emit({
216
      value: this.show
217
    });
218
  }else{
219
    this.errorsInClaimsInsertion(insertedIds, errorInClaims);
220
  }
221
}
222
private errorsInClaimsInsertion(insertedIds, errorInClaims){
223
  this.errorMessage = "An Error Occured.";
224
  this.loading.close();
225
  this.error =  true;
226
    if(this.inline){
227
      this.show = "error";
228
      this.showChange.emit({
229
        value: this.show
230
      });
231
  }
232

    
233
}
234

    
235

    
236

    
237
  private createContextClaim(result:any, context:any, user:any){
238
    var claim =  { claimedBy : user, sourceId : context.concept.id, sourceType : "context", sourceCollectedFrom:"openaire", sourceAccessRights:"OPEN", sourceEmbargoEndDate:"no", targetId :  result.id , targetType : result.type, targetCollectedFrom: result.source, targetAccessRights:result.accessRights, targetEmbargoEndDate:result.embargoEndDate};
239
    return claim;
240
  }
241
  private createProjectClaim(result:any, project:any, user:any){
242
    //project.projectId
243
    // var dummyID = "dummyID";
244
    var claim =  { claimedBy : user, sourceId : project.projectId, sourceType : "project", sourceCollectedFrom:"openaire", sourceAccessRights:"OPEN", sourceEmbargoEndDate:"", targetId :  result.id , targetType : result.type, targetCollectedFrom: result.source, targetAccessRights:result.accessRights, targetEmbargoEndDate:result.embargoEndDate};
245
    return claim;
246
  }
247
  private createResultClaim(inlineResult:any, result:any, user:any){
248
    var claim =  { claimedBy : user, sourceId : result.id, sourceType : result.type, sourceCollectedFrom: result.source, sourceAccessRights: result.accessRights, sourceEmbargoEndDate: result.embargoEndDate, targetId :  inlineResult.id , targetType : inlineResult.type, targetCollectedFrom: inlineResult.source, targetAccessRights: inlineResult.accessRights, targetEmbargoEndDate: inlineResult.embargoEndDate};
249
    return claim;
250
  }
251

    
252
  confirmOpen(){
253
    this.alert.cancelButton = true;
254
    this.alert.okButton = true;
255
    this.alert.alertTitle = "Invalid dates";
256
    this.alert.message = "There is a research result whose  publication date  is after project end date or before project start date. Or embargo end date of a research result is before research result's publication date.";
257
    this.alert.okButtonText = "Procceed anyway";
258
    this.alert.cancelButtonText = "Cancel";
259
    this.alert.open();
260
  }
261
   confirmClose(data){
262
    this.insert();
263
  }
264
}
    (1-1/1)