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

265 lines
9.8 KiB
TypeScript

import {Component, Input, Output, EventEmitter, ViewChild} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Router} from '@angular/router';
import {ClaimsService} from '../../../services/claims.service';
import {ModalLoading} from '../../../utils/modal/loading.component';
import {AlertModal} from '../../../utils/modal/alert';
@Component({
selector: 'claim-insert',
template: `
<button *ngIf="claiming == false && showButton == true" (click)="validateInsertions()" class="btn btn-primary" style="float:right">Finish</button>
<div *ngIf="errorMessage.length > 0">
<div class="alert alert-danger" role="alert">{{errorMessage}}</div>
</div>
<div *ngIf="warningMessage.length > 0">
<div class="alert alert-warning" role="alert">{{warningMessage}}</div>
</div>
<modal-loading [message]= "'Please wait...'"></modal-loading>
<modal-alert (alertOutput)="confirmClose($event)">
</modal-alert>
`
})
export class ClaimInsertComponent {
constructor (private claimService: ClaimsService, private _router:Router) {}
ngOnInit() {
// console.info("Inlineentity:" +(this.inlineEntity)?this.inlineEntity+(this.inlineEntity.id)?this.inlineEntity.id:"no id":"null"+ + " show "+ (!this.claiming && this.showButton) );
}
@Input() public contexts;
@Input() public projects;
@Input() public publications;
@Input() public datasets;
@Input() public showButton:boolean = true;
@Input() show='claim';
@Input() inline: boolean = false; // link from landing page?
@Input() inlineEntity; // the entity from the landing page
@Output() showChange = new EventEmitter();
@ViewChild (ModalLoading) loading : ModalLoading ;
@ViewChild(AlertModal) alert;
private claiming =false;
private error = false;
private errorMessage = "";
private warningMessage = "";
private claimsTODO:number = 0;
private claims:number = 0;
private errorclaims:number = 0;
public validateInsertions(){
// console.info("Inlineentity:" +(this.inlineEntity)?this.inlineEntity+(this.inlineEntity.id)?this.inlineEntity.id:"no id":"null"+ + " show "+ (!this.claiming && this.showButton) );
if(this.validate()){
if(this.validateDates()){
this.insert();
return true;
}
}
return
}
private insert(){
this.claiming = true;
var user="argirok@di.uoa.gr"
this.loading.open();
var claims = [];
if(this.publications){
console.info("publications: "+this.publications.length);
for (var i = 0; i < this.publications.length; i++) {
var result=this.publications[i];
if(this.contexts){
for (var j = 0; j < this.contexts.length; j++) {
var context = this.contexts[j];
var claim = this.createContextClaim(result, context, user);
claims.push(claim);
}
}
if(this.projects){
for (var k = 0; k < this.projects.length; k++) {
var project = this.projects[k];
var projectClaim = this.createProjectClaim(result, project, user);
claims.push(projectClaim);
}
}
if(this.inline && this.inlineEntity){
var resultClaim = this.createResultClaim(this.inlineEntity, result, user);
claims.push(resultClaim);
}
}
}
if(this.datasets){
for (var i = 0; i < this.datasets.length; i++) {
var result=this.datasets[i];
if(this.contexts){
for (var j = 0; j < this.contexts.length; j++) {
var context = this.contexts[j];
var claim = this.createContextClaim(result, context, user);
claims.push(claim);
}
}
if(this.projects){
for (var k = 0; k < this.projects.length; k++) {
var project = this.projects[k];
var projectClaim = this.createProjectClaim(result, project, user);
claims.push(projectClaim);
}
}
if(this.inline && this.inlineEntity){
var resultClaim = this.createResultClaim(this.inlineEntity, result, user);
claims.push(resultClaim);
}
}
}
console.info("try to insert "+claims.length+" claims");
this.claimService.insertBulkClaims(claims).subscribe(
data => {
var inserted = data.insertedIds.length;
var inserted = data.insertedIds.length;
this.afterclaimsInsertion(data.insertedIds,data.errorInClaims);
},
err => {
console.error(err);
this.errorsInClaimsInsertion(err.insertedIds,err.errorInClaims);
}
);
}
private validate(){
this.warningMessage = "";
this.errorMessage = "";
if(this.datasets && this.datasets.length == 0 && this.publications && this.publications.length == 0){
this.warningMessage = "There are no publications or datasets selected.";
}else if((!this.contexts|| this.contexts.length==0 )&&(!this.projects|| this.projects.length==0 )&& ( !this.inlineEntity)){
this.warningMessage = "There are no projects or concepts to link.";
// }else if (this.inline && !this.inlineEntity){
// this.errorMessage = "No inline entity";
// console.log(this.inline + " "+ this.inlineEntity);
}else{
return true;
}
return false;
}
private validateDates(){
if(this.projects){
for (var k = 0; k < this.projects.length; k++) {
var project = this.projects[k];
console.info(project.startDate+" "+project.endDate + " "+project.projectAcronym);
if(this.publications){
for (var i = 0; i < this.publications.length; i++) {
var result = this.publications[i];
if(result.date && result.date != null){
console.info("Date :"+ result.date + " & embargoEndDate :" +result.embargoEndDate );
if((project.startDate && result.date < project.startDate) || ( project.endDate && result.date > project.endDate) ){
this.confirmOpen();
return false;
}
}
}
}
if(this.datasets){
for (var i = 0; i < this.datasets.length; i++) {
var result = this.datasets[i];
if(result.date && result.date != null){
console.info("Date :"+ result.date + " & embargoEndDate :" +result.embargoEndDate );
if((project.startDate && result.date < project.startDate) || ( project.endDate && result.date > project.endDate) ){
this.confirmOpen();
return false;
}
}
}
}
}
}
if(this.publications){
for (var i = 0; i < this.publications.length; i++) {
var result = this.publications[i];
if(result.date && result.date != null){
console.info("Date :"+ result.date + " & embargoEndDate :" +result.embargoEndDate );
if((result.embargoEndDate && result.embargoEndDate != null) && result.date >result.embargoEndDate ){
this.confirmOpen();
return false;
}
}
}
}
if(this.datasets){
for (var i = 0; i < this.datasets.length; i++) {
var result = this.datasets[i];
if(result.date && result.date != null){
console.info("Date :"+ result.date + " & embargoEndDate :" +result.embargoEndDate );
if((result.embargoEndDate && result.embargoEndDate != null) && result.date >result.embargoEndDate ){
this.confirmOpen();
return false;
}
}
}
}
return true;
}
private afterclaimsInsertion(insertedIds, errorInClaims){
this.loading.close();
if(errorInClaims.length == 0){
if(this.inline){
this.show = "end";
}else{
this._router.navigate( ['/my-claims'] );
}
this.showChange.emit({
value: this.show
});
}else{
this.errorsInClaimsInsertion(insertedIds, errorInClaims);
}
}
private errorsInClaimsInsertion(insertedIds, errorInClaims){
this.errorMessage = "An Error Occured.";
this.loading.close();
this.error = true;
if(this.inline){
this.show = "error";
this.showChange.emit({
value: this.show
});
}
}
private createContextClaim(result:any, context:any, user:any){
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};
return claim;
}
private createProjectClaim(result:any, project:any, user:any){
//project.projectId
// var dummyID = "dummyID";
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};
return claim;
}
private createResultClaim(inlineResult:any, result:any, user:any){
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};
return claim;
}
confirmOpen(){
this.alert.cancelButton = true;
this.alert.okButton = true;
this.alert.alertTitle = "Invalid dates";
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.";
this.alert.okButtonText = "Procceed anyway";
this.alert.cancelButtonText = "Cancel";
this.alert.open();
}
confirmClose(data){
this.insert();
}
}