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

    
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 == false && showButton == true" (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="argirok@di.uoa.gr"
64
  this.loading.open();
65
  var claims = [];
66
  var directclaims = [];
67
  if(this.results){
68
    console.info("results: "+this.results.length);
69

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

    
94
      }
95
    }
96

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

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

    
144

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

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

    
188
}
189

    
190

    
191

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

    
247
  if(projects.length>0){
248
    entity["linksToProjects"]=[];
249
    for(var i =0; i < projects.length; i++){
250
      // "info:eu-repo/grantAgreement/EC/FP7/283595/EU//OpenAIREplus",
251
      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);
252
    }
253
  }
254
  if(contexts.length > 0){
255
    entity["contexts"]=[];
256
    for(var i =0; i < contexts.length; i++){
257
      entity["contexts"].push(contexts[i].concept.id);
258
    }
259
  }
260
  var json = JSON.stringify(entity);
261
  console.log("\nJSON:\n"+json);
262
  return json;
263

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

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