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

158 lines
5.3 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 && showButton == 'true' " (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() public contexts;
@Input() public projects;
@Input() public publications;
@Input() public datasets;
@Input() public showButton:string = 'true';
@Input() show='claim';
@Input() inline='false';
@Output() showChange = new EventEmitter();
@ViewChild (Loading) loading : Loading ;
claiming =false;
error = false;
errorMessage = "";
claimsTODO:number = 0;
claims:number = 0;
errorclaims:number = 0;
insert(){
this.claiming = true;
var user="argirok@di.uoa.gr"
if( this.datasets && this.datasets.length == 0 && this.publications && this.publications.length == 0){
this.showError("There are no publications or datasets selected.");
}else if((!this.contexts|| this.contexts.length==0 )&&(!this.projects|| this.projects.length==0 )){
this.showError("There are no projects or concepts to link.");
}else if((!this.publications|| this.publications.length==0 )&&(!this.datasets|| this.datasets.length==0 )){
this.showError("There are no publications or datasets to link.");
}else{
this.loading.open();
if(this.publications){
for (var i = 0; i < this.publications.length; i++) {
var result=this.publications[i];
this.insertClaim(result,user);
}
}
if(this.datasets){
for (var i = 0; i < this.datasets.length; i++) {
var result=this.datasets[i];
this.insertClaim(result,user);
}
}
}
}
private claimInserted(){
this.claims+=1;
if(this.claims == this.claimsTODO){
this.show = "context";
this.loading.close();
if(this.inline == "true"){
this.show = "end";
}else{
this._router.navigate( ['MyClaims'] );
}
this.showChange.emit({
value: this.show
});
}
}
private showError(error:string){
this.errorMessage = error;
this.error = true;
if(this.inline == "true"){
this.show = "error";
this.showChange.emit({
value: this.show
});
}
}
private claimFailed(){
this.errorMessage = "An Error Occured.";
this.errorclaims++;
if((this.claims+this.errorclaims) == this.claimsTODO){
this.show = "context";
this.loading.close();
if(this.inline == "true"){
this.show = "end";
}else{
this._router.navigate( ['MyClaims'] );
}
this.showChange.emit({
value: this.show
});
}
}
insertClaim(result:any, user:any){
if(this.contexts){
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:"no", targetId : result.id , targetType : result.type, targetCollectedFrom: result.source, targetAccessRights:result.accessRights, targetEmbargoEndDate:result.embargoEndDate};
this.claimsTODO +=1;
this.claimService.insertClaim(claim).subscribe(
data => {
this.claimInserted();
},
err => {
console.error(err);
this.claimFailed();
}
);
}
}
if(this.projects){
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);
this.claimFailed();
}
);
}
}
}
}