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 '../../claim-utils/service/claims.service';
5
import {ModalLoading} from '../../../utils/modal/loading.component';
6
import {AlertModal} from '../../../utils/modal/alert';
7
import {Md5} from 'ts-md5/dist/md5';
8
import {Session} from '../../../login/utils/helper.class';
9
@Component({
10
    selector: 'claim-insert',
11
    template: `
12

    
13
    <div *ngIf="errorMessage.length > 0">
14
      <div class="uk-alert uk-alert-danger" role="alert">{{errorMessage}}</div>
15
    </div>
16
    <div *ngIf="warningMessage.length > 0">
17
      <div class="uk-alert uk-alert-warning" role="alert">{{warningMessage}}</div>
18
    </div>
19
    <modal-loading [message]= "'Please wait...'"></modal-loading>
20
    <modal-alert (alertOutput)="confirmClose($event)">
21
    </modal-alert>
22
    <button  *ngIf="!claiming  && showButton" (click)="validateInsertions()"  class="uk-button uk-button-primary uk-align-right"  >Finish</button>
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 results;
35
  @Input() public showButton:boolean = true;
36
  @Input() show='claim';
37
  @Input() inlineEntity = null; // the entity from the landing page
38
  @Output() showChange = new EventEmitter();
39

    
40
  @ViewChild (ModalLoading) loading : ModalLoading ;
41
  @ViewChild(AlertModal) alert;
42

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

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

    
71
      for (var i = 0; i < this.results.length; i++) {
72
          var result=this.results[i];
73
          if(["crosreff","datacite","orcid"].indexOf(result.source) != -1){
74
            directclaims.push(this.createDirectClaim(result,this.projects,this.contexts));
75
          }
76
          if(this.contexts){
77
            for (var j = 0; j < this.contexts.length; j++) {
78
              var context = this.contexts[j];
79
              var claim = this.createContextClaim(result, context, user);
80
              claims.push(claim);
81
            }
82
          }
83
          if(this.projects){
84
            for (var k = 0; k < this.projects.length; k++) {
85
              var project = this.projects[k];
86
              var projectClaim = this.createProjectClaim(result, project, user);
87
              claims.push(projectClaim);
88
            }
89
          }
90
           if(this.inlineEntity != null){
91
            var resultClaim = this.createResultClaim(this.inlineEntity, result, user);
92
            claims.push(resultClaim);
93
          }
94

    
95
      }
96
    }
97

    
98
    console.info("try to insert "+claims.length+" claims");
99
    this.claimService.insertBulkClaims(claims).subscribe(
100
      data => {
101
        var inserted = data.insertedIds.length;
102

    
103
        var inserted = data.insertedIds.length;
104
        this.afterclaimsInsertion(data.insertedIds,data.errorInClaims);
105
      },
106
      err =>  {
107
        console.log(err);
108
        this.errorsInClaimsInsertion(err.insertedIds,err.errorInClaims);
109
      }
110
    );
111
}
112
private validate(){
113
  this.warningMessage = "";
114
  this.errorMessage = "";
115
  if( this.results && this.results.length == 0){
116
   this.warningMessage = "There are no research results selected.";
117
 }else if((!this.contexts|| this.contexts.length==0 )&&(!this.projects|| this.projects.length==0 )&&  ( this.inlineEntity == null)){
118
   this.warningMessage = "There are no projects or concepts to link.";
119
 // }else if (this.inline && !this.inlineEntity){
120
 //   this.errorMessage = "No inline entity";
121
 //   console.log(this.inline + "   "+ this.inlineEntity);
122
 }else{
123
   return true;
124
 }
125
 return false;
126
}
127
private validateDates(){
128
  if(this.projects){
129
    for (var k = 0; k < this.projects.length; k++) {
130
      var project = this.projects[k];
131
      console.info(project.startDate+" "+project.endDate + " "+project.projectAcronym);
132
      if(this.results){
133
        for (var i = 0; i < this.results.length; i++) {
134
          var result = this.results[i];
135
          if(result.date && result.date != null){
136
            console.info("Date :"+ result.date + " & embargoEndDate :" +result.embargoEndDate );
137
            if((project.startDate && result.date < project.startDate) || ( project.endDate && result.date > project.endDate) ){
138
              this.confirmOpen();
139
              return false;
140
            }
141
          }
142
        }
143
      }
144

    
145

    
146
    }
147
  }
148
  if(this.results){
149
    for (var i = 0; i < this.results.length; i++) {
150
      var result = this.results[i];
151
      if(result.date && result.date != null){
152
        console.info("Date :"+ result.date + " & embargoEndDate :" +result.embargoEndDate );
153
         if((result.embargoEndDate && result.embargoEndDate != null) && result.date >result.embargoEndDate ){
154
          this.confirmOpen();
155
          return false;
156
        }
157
      }
158
    }
159
  }
160

    
161
 return true;
162
}
163
private afterclaimsInsertion(insertedIds, errorInClaims){
164
  this.loading.close();
165
  this.claiming = false;
166
  if(errorInClaims.length == 0 && insertedIds.length > 0 ){
167
    // if(this.inline){
168
    //   this.show = "end";
169
    // }else{
170
      this._router.navigate( ['/myclaims'] );
171
    // }
172
    this.showChange.emit({
173
      value: this.show
174
    });
175
  }else{
176
    this.errorsInClaimsInsertion(insertedIds, errorInClaims);
177
  }
178
}
179
private errorsInClaimsInsertion(insertedIds, errorInClaims){
180
  this.errorMessage = "An Error Occured.";
181
  this.loading.close();
182
  this.error =  true;
183
  this.claiming = false;
184
  this.showButton = true;
185
  //   if(this.inline){
186
  //     this.show = "error";
187
  //     this.showChange.emit({
188
  //       value: this.show
189
  //     });
190
  // }
191

    
192
}
193

    
194

    
195

    
196
  private createContextClaim(result:any, context:any, user:any){
197
    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 == null?"":result.embargoEndDate)};
198
    return claim;
199
  }
200
  private createProjectClaim(result:any, project:any, user:any){
201
    //project.projectId
202
    // var dummyID = "dummyID";
203
    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 == null?"":result.embargoEndDate)};
204
    return claim;
205
  }
206
  private createResultClaim(inlineResult:any, result:any, user:any){
207
    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 == null?"":inlineResult.embargoEndDate)};
208
    return claim;
209
  }
210
createDirectClaim(result, projects, contexts){
211
  var entity = {};
212
  var md5_id = Md5.hashStr(result.id);
213
  entity["originalId"]="user:claim__"+md5_id;
214
  entity["title"]=result.title;
215
  if(result.authors && result.authors.length > 0){
216
    entity["authors"]=result.authors;
217
  }
218
  if(result.publisher){
219
    entity["publisher"]=result.publisher;
220
  }
221
  if(result.description){
222
    entity["description"]=result.description;
223
  }
224
  // entity["language"]=""; no info
225
  entity["type"]=result.type;
226
  if(result.source == "crossref" || result.source == "datacite"){
227
    entity["pids"]= [];//{type:string, value:string}[];
228
    entity["pids"].push({type:"DOI",value:result.id})
229
  }
230
  entity["licenseCode"]=result.accessRights;
231
  if(result.accessRights == "EMBARGO"){
232
    entity["embargoEndDate"]=result.embargoEndDate;
233
  }
234
  if(result.type =="publication"){
235
    entity["resourceType"]="0001";
236
  }else{
237
    entity["resourceType"]="0021";
238
  }
239
  entity["url"]=result.url;
240
  entity["hostedById"]="openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18";
241
  if(result.source == "crossref"){
242
    entity["collectedFromId"]="openaire____::crossref";
243
  }else if(result.source == "datacite"){
244
    entity["collectedFromId"]="openaire____::datacite";
245
  }else if(result.source == "orcid"){
246
    entity["collectedFromId"]="openaire____::orcid";
247
  }else if(result.source == "orpenaire"){
248
    entity["collectedFromId"]="openaire____::driver";
249
  }
250

    
251
  if(projects.length>0){
252
    entity["linksToProjects"]=[];
253
    for(var i =0; i < projects.length; i++){
254
      // "info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus",
255
      entity["linksToProjects"].push("info:eu-repo/grantAgreement/"+projects[i].funderName+"/"+projects[i].fundingLevel0+"/"+projects[i].code+"/"+projects[i].jurisdiction+"/"+projects[i].projectName+"/"+projects[i].projectAcronym);
256
    }
257
  }
258
  if(contexts.length > 0){
259
    entity["contexts"]=[];
260
    for(var i =0; i < contexts.length; i++){
261
      entity["contexts"].push(contexts[i].concept.id);
262
    }
263
  }
264
  var json = JSON.stringify(entity);
265
  console.log("\nJSON:\n"+json);
266
  return json;
267

    
268
/*
269
{
270
  "originalId": "ORIG_ID_12345",
271
  "title": "TEST TITLE",
272
  "authors": [
273
        "Michele Artini",
274
        "Claudio Atzori",
275
        "Alessia Bardi"
276
  ],
277
  "publisher": "Test publisher",
278
  "description": "DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION",
279
  "language": "ita",
280
  "pids": [
281
        {
282
          "type": "doi",
283
          "value": "10.000/xyz-123"
284
        },
285
        {
286
          "type": "oai",
287
          "value": "oai:1234"
288
        }
289
  ],
290
  "licenseCode": "OPEN",
291
  "resourceType": "0001",
292
  "url": "http://test.it/xyz",
293
  "collectedFromId": "opendoar____::2659",
294
  "hostedById": "opendoar____::2367",
295
  "linksToProjects": [
296
        "info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus",
297
        "info:eu-repo/grantAgreement/EC/FP7/244909/EU/Making Capabilities Work/WorkAble"
298
  ],
299
  "contexts": [
300
        "egi::classification::natsc::math::pure",
301
        "egi::classification::natsc::math::stats"
302
  ]
303
}
304
*/
305

    
306
}
307
  confirmOpen(){
308
    this.alert.cancelButton = true;
309
    this.alert.okButton = true;
310
    this.alert.alertTitle = "Invalid dates";
311
    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.";
312
    this.alert.okButtonText = "Procceed anyway";
313
    this.alert.cancelButtonText = "Cancel";
314
    this.alert.open();
315
  }
316
   confirmClose(data){
317
    this.insert();
318
  }
319
}
(1-1/2)