explore-services/src/app/claimPages/linking/insertClaim/insertClaim.component.ts

114 lines
4.1 KiB
TypeScript

import {Component, Input, Output, EventEmitter, ViewChild} from '@angular/core';
import {JSONP_PROVIDERS} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import { RouteParams, RouteConfig, ROUTER_DIRECTIVES, Router } from '@angular/router-deprecated';
import {ClaimsService} from '../../../services/claims.service';
import {PublicationTitleFormatter} from '../../../common/publicationTitleFormatter.component';
import {Loading} from '../../../common/modal/loading.component';
@Component({
selector: 'claim-insert',
directives: [...ROUTER_DIRECTIVES, Loading],
template: `
<button *ngIf="claiming == false" (click)="insert()" class="btn btn-primary" style="float:right">Finish</button>
<div *ngIf="error == true"> <div class="alert alert-warning" role="alert">{{errorMessage}}</div>
</div>
<div *ngIf="claiming == true && claimsTODO > 0 && claimsTODO == claims" >
Now show my claims
</div>
<loading [message]= "'Please wait...'"></loading>
`,
providers:[JSONP_PROVIDERS, ClaimsService]
})
export class ClaimInsertComponent {
constructor (private claimService: ClaimsService, private _routeParams: RouteParams, private _router:Router) {}
ngOnInit() {
}
@Input() contexts=[];
@Input() projects=[];
@Input() publications=[];
@Input() datasets=[];
@Input() show='claim';
@Output() showChange = new EventEmitter();
@ViewChild (Loading) loading : Loading ;
claiming =false;
error = false;
errorMessage = "";
claimsTODO:number = 0;
claims:number = 0;
insert(){
this.claiming = true;
var user="argirok@di.uoa.gr"
if( this.datasets.length == 0 && this.publications.length == 0){
this.errorMessage = "There are no publications or datasets selected.";
this.error = true;
}else if(this.contexts.length == 0 && this.projects.length == 0){
this.errorMessage = "There are no projects or concepts to link.";
this.error = true;
}else{
this.loading.open();
for (var i = 0; i < this.publications.length; i++) {
var result=this.publications[i];
this.insertClaim(result,user);
}
for (var i = 0; i < this.datasets.length; i++) {
var result=this.datasets[i];
this.insertClaim(result,user);
}
}
}
claimInserted(){
this.claims+=1;
if(this.claims == this.claimsTODO){
this.show = "myclaims";
this.loading.close();
// this.showChange.emit({
// value: this.show
// });
this._router.navigate( ['MyClaims'] );
}
}
insertClaim(result:any, user:any){
for (var j = 0; j < this.contexts.length; j++) {
var context=this.contexts[j];
var claim = { claimedBy : user, sourceId : context.concept.id, sourceType : "context", sourceCollectedFrom:"openaire", sourceAccessRights:"OPEN", sourceEmbargoEndDate:"", targetId : result.id , targetType : result.type,
targetCollectedFrom: result.source, targetAccessRights:result.accessRights, targetEmbargoEndDate:result.embargoEndDate};
console.info("Trying to insert context - result relation: contextId: "+claim.sourceId +" resultId : " + claim.targetId + " "+ claim.targetEmbargoEndDate );
this.claimsTODO +=1;
this.claimService.insertClaim(claim).subscribe(
data => {
this.claimInserted();
},
err => console.error(err)
);
}
for (var j = 0; j < this.projects.length; j++) {
var project=this.projects[j];
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};
console.info("Trying to insert project - result relation: projectId: "+claim.sourceId +" resultId : " + claim.targetId + " "+ claim.targetEmbargoEndDate );
this.claimsTODO +=1;
this.claimService.insertClaim(claim).subscribe(
data => {
this.claimInserted();
},
err => console.error(err)
);
}
}
}