import {Component, Input, Output, EventEmitter,ViewChild} from '@angular/core'; import {Observable} from 'rxjs'; import {SearchCrossrefService} from '../../claim-utils/service/searchCrossref.service'; import {SearchDataciteService} from '../../claim-utils/service/searchDatacite.service'; import {ModalLoading} from '../../../utils/modal/loading.component'; import {Dates, DOI} from '../../../utils/string-utils.class'; import {ClaimResult} from '../../claim-utils/claimEntities.class'; import{EnvProperties} from '../../../utils/properties/env-properties'; declare var UIkit:any; @Component({ selector: 'bulk-claim', template: `
Upload a DOI csv file
` }) //[(ngModel)]="date" export class BulkClaimComponent { filesToUpload: Array; navigateTo: string = "Search"; source: string = "crossref"; type : string = "publication"; resultsFromSearch:number; @Input() public select:boolean = true ; @Input() public results; @Input() public properties:EnvProperties; allIds:string[] = []; foundIds:string[] = []; duplicateIds:string[] = []; duplicateIdsRow:number[] = []; notFoundIds:string[] = []; notFoundIdsRow:number[] = []; noValidIds:string[] = []; noValidIdsRow:number[] = []; showReport:boolean = false; showInfo :boolean = false; @ViewChild (ModalLoading) loading : ModalLoading ; errorMessage = ""; infoMEssage = ""; enableUpload:boolean = true; @Input() localStoragePrefix:string=""; exceedsLimit = false; limit =150; constructor(private _searchCrossrefService: SearchCrossrefService, private _searchDataciteService: SearchDataciteService) { this.filesToUpload = []; } ngOnInit() {} upload() { this.enableUpload = false; this.showReport = false; this.errorMessage = ""; if(this.filesToUpload.length == 0){ this.errorMessage = "There is no selected file to upload."; return ; }else{ if(this.filesToUpload[0].name.indexOf(".csv") == -1 || this.filesToUpload[0].type != "text/csv"){ this.errorMessage = "No valid file type. The required type is CSV"; return ; } } this.loading.open(); this.makeFileRequest(this.properties.utilsService + '/upload', [], this.filesToUpload).then((result) => { var rows = (result as any).split('\n'); // I have used space, you can use any thing. this.exceedsLimit = false; var i = 0; var invalid_rows = 0; this.duplicateIds = []; this.allIds = []; this.foundIds = []; this.noValidIds = []; this.results.slice(0,this.results.length); this.notFoundIds = []; if(rows.length + this.results.length > this.limit){ this.exceedsLimit = true; UIkit.notification({ message : 'Your basket exceeds the number of allowed results (150)', status : 'warning', timeout : 1500, pos : 'top-center' }); } for(i=0;i<((rows.length>this.limit-this.results.length)?(this.limit-this.results.length):rows.length);i++){ if(rows[i] && rows[i] != null ){ var values = rows[i].split(','); var id=this.removeDoubleQuotes(values[0]); if(DOI.isValidDOI(id)){ var accessMode = (values[1] != undefined) ? this.removeDoubleQuotes(values[1]):"OPEN"; accessMode = (this.validateAccessMode(accessMode)?accessMode:"OPEN"); var embargoDate =(values[2] != undefined) ? this.removeDoubleQuotes(values[2]):Dates.getDateToday(); embargoDate = (Dates.isValidDate(embargoDate)?embargoDate:Dates.getDateToday()); if(this.allIds.indexOf(id)>-1){ this.duplicateIds.push(id); this.duplicateIdsRow.push(i+1); }else{ this.allIds.push(id); this.fetchResult(id,accessMode,embargoDate,i+1); } }else{ this.noValidIds.push(id); this.noValidIdsRow.push(i+1); } }else{ invalid_rows++; } } if(rows.length == 0 || rows.length == invalid_rows || rows.length == (invalid_rows + this.noValidIds.length) || this.limit == this.results.length ){ this.endOfFetching(); } }, (error) => { this.enableUpload = true; console.log(error); this.loading.close(); //this.errorMessage = "An error occured while uploading..."; this.handleError("Error uploading file", error); }); } private removeDoubleQuotes(value){ if(value.indexOf('"')== 0){ value = value.substring(1,value.length); } var index =+value.indexOf('"'); if(index == (value.length - 1) || index == (value.length - 2) ){ value = value.substring(0,index); } return value; } private validateAccessMode(value){ var accessModes = ["OPEN", "CLOSED", "EMBARGO"]; if(accessModes.indexOf(value) > -1){ return true; } return false; } fileChangeEvent(fileInput: any){ this.filesToUpload = > fileInput.target.files; this.upload(); } makeFileRequest(url: string, params: Array, files: Array) { return new Promise((resolve, reject) => { var formData: any = new FormData(); var xhr = new XMLHttpRequest(); for(var i = 0; i < files.length; i++) { formData.append("uploads[]", files[i], files[i].name); } xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if (xhr.status == 200) { resolve(xhr.response); } else { reject(xhr.response); } } } xhr.open("POST", url, true); xhr.send(formData); }); } fetchResult(id:string,accessMode:string,date:string, row:number){ this._searchCrossrefService.searchCrossrefByDOIs([id], this.properties.searchCrossrefAPIURL, true).subscribe( data => { var result = data[0]; if(data.length > 0){ this.foundIds.push(id); result.embargoEndDate = date; this.results.push(result); this.endOfFetching(); }else{ this.searchInDatacite(id,accessMode,date, row); // this.notFoundIds.push(id); } }, err => { //console.log(err); this.handleError("Error getting crossref by DOIs: "+id, err); this.notFoundIds.push(id); this.notFoundIdsRow.push(row); this.endOfFetching(); } ); } searchInDatacite(id:string,accessMode:string,date:string, row:number){ this._searchDataciteService.getDataciteResultByDOI(id,this.properties,true).subscribe( items => { if(items.length > 0){ var result = items[0]; this.foundIds.push(id); result.embargoEndDate = date; this.results.push(result); }else{ this.notFoundIds.push(id); this.notFoundIdsRow.push(row); } this.endOfFetching(); }, err => { //console.log(err); this.handleError("Error getting datacite result by DOI: "+id, err); this.notFoundIds.push(id); this.notFoundIdsRow.push(row); this.endOfFetching(); } ); } endOfFetching(){ if(this.limit == this.results.length){ this.enableUpload = true; this.loading.close(); return; } if(this.allIds.length == this.foundIds.length+this.notFoundIds.length ){ this.showReport = true; this.enableUpload = true; this.loading.close(); if(this.results != null){ localStorage.setItem(this.localStoragePrefix + "results", JSON.stringify(this.results)); } } } private handleError(message: string, error) { console.error("Bulk Claim (component): "+message, error); } }