2017-12-19 13:53:46 +01:00
|
|
|
import {Component, Input, Output, EventEmitter,ViewChild} from '@angular/core';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
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';
|
2018-02-05 14:14:59 +01:00
|
|
|
import{EnvProperties} from '../../../utils/properties/env-properties';
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'bulk-claim',
|
|
|
|
template: `
|
|
|
|
<div class="uk-animation " style=" ">
|
2018-07-25 15:56:02 +02:00
|
|
|
<form class=" uk-padding uk-padding-medium uk-padding-remove-left uk-margin-left uk-margin-small-top ">
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2018-02-15 11:36:12 +01:00
|
|
|
<div class="uk-grid">
|
|
|
|
<div class="uk-width-expand">
|
2018-07-30 16:22:19 +02:00
|
|
|
<div class="uk-text-lead">Upload a DOI csv file <helper div="link-result-bulk" tooltip=true ></helper></div>
|
2018-07-25 15:56:02 +02:00
|
|
|
<!--h3 for="exampleInputFile">Or upload a DOI csv file:</h3-->
|
|
|
|
<label for="exampleInputFile">Select a file: </label>
|
|
|
|
<div class="js-upload" uk-form-custom>
|
|
|
|
<input id="exampleInputFile" class="uk-width-medium" type="file" (change)="fileChangeEvent($event)"/>
|
2018-10-03 16:12:42 +02:00
|
|
|
<button class="uk-button portal-button" type="button" tabindex="-1" [class.disabled]="!enableUpload" ><span class="uk-margin-small-right uk-icon" >
|
2018-07-25 15:56:02 +02:00
|
|
|
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"> <polyline fill="none" stroke="#000" points="5 8 9.5 3.5 14 8 "></polyline> <rect x="3" y="17" width="13" height="1"></rect>
|
|
|
|
<line fill="none" stroke="#000" x1="9.5" y1="15" x2="9.5" y2="4"></line></svg></span> Select</button>
|
|
|
|
<!--button class="uk-button uk-button-small" [class.disabled]="!enableUpload" type="button" (click)="upload()">Upload</button-->
|
|
|
|
</div>
|
2017-12-19 13:53:46 +01:00
|
|
|
<div *ngIf="showReport" uk-alert class="uk-alert uk-alert-primary" role="alert" >
|
|
|
|
<a class="uk-alert-close" uk-close></a>
|
2018-06-29 13:01:48 +02:00
|
|
|
<div>Uploaded file contains {{allIds.length}} {{((allIds.length==1)?'DOI':'DOIs')}}.
|
|
|
|
<span *ngIf="allIds.length > 0">{{foundIds.length}} {{((foundIds.length==1)?'result was':'results were')}} succefully fetched from CrossRef and Datacite.</span>
|
|
|
|
</div>
|
|
|
|
<div *ngIf ="duplicateIds.length > 0" >{{duplicateIds.length | number}} duplicate DOIs in {{((duplicateIds.length==1)?'line':'lines')}} {{duplicateIdsRow}}.</div>
|
2017-12-19 13:53:46 +01:00
|
|
|
<div *ngIf = "notFoundIds.length > 0" >Couldn't be found:
|
|
|
|
<ul class="">
|
2018-06-29 13:01:48 +02:00
|
|
|
<li *ngFor="let id of notFoundIds; let i = index">"{{id}}" in line {{notFoundIdsRow[i]}}</li>
|
2017-12-19 13:53:46 +01:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<div *ngIf = "noValidIds.length > 0" >No valid DOIs:
|
|
|
|
<ul class="">
|
2018-06-29 13:01:48 +02:00
|
|
|
<li *ngFor="let id of noValidIds; let i = index">"{{id}}" in line {{noValidIdsRow[i]}}</li>
|
2017-12-19 13:53:46 +01:00
|
|
|
</ul>
|
|
|
|
</div>
|
2018-06-15 14:58:58 +02:00
|
|
|
<div *ngIf = "allIds.length == 0 || foundIds.length == 0" > Please make sure that the uploaded file, is a csv file with the proper format. </div>
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
</div>
|
|
|
|
<div *ngIf="errorMessage.length > 0 " class="uk-alert uk-alert-danger" role="alert">{{errorMessage}}</div>
|
2018-06-15 14:58:58 +02:00
|
|
|
<modal-loading [message]= "'Uploading, reading your documet and fetching results. Please give us a moment..'"></modal-loading>
|
2018-02-15 11:36:12 +01:00
|
|
|
</div>
|
2018-07-25 15:56:02 +02:00
|
|
|
<!--helper div="link-result-bulk" ></helper-->
|
2018-02-15 11:36:12 +01:00
|
|
|
</div>
|
|
|
|
</form>
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
|
|
|
|
})
|
|
|
|
//[(ngModel)]="date"
|
|
|
|
export class BulkClaimComponent {
|
|
|
|
filesToUpload: Array<File>;
|
|
|
|
navigateTo: string = "Search";
|
|
|
|
source: string = "crossref";
|
|
|
|
type : string = "publication";
|
|
|
|
resultsFromSearch:number;
|
|
|
|
@Input() public select:boolean = true ;
|
|
|
|
@Input() public results;
|
2018-02-05 14:14:59 +01:00
|
|
|
@Input() public properties:EnvProperties;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
allIds:string[] = [];
|
|
|
|
foundIds:string[] = [];
|
|
|
|
duplicateIds:string[] = [];
|
2018-06-29 13:01:48 +02:00
|
|
|
duplicateIdsRow:number[] = [];
|
2017-12-19 13:53:46 +01:00
|
|
|
notFoundIds:string[] = [];
|
2018-06-29 13:01:48 +02:00
|
|
|
notFoundIdsRow:number[] = [];
|
2017-12-19 13:53:46 +01:00
|
|
|
noValidIds:string[] = [];
|
2018-06-29 13:01:48 +02:00
|
|
|
noValidIdsRow:number[] = [];
|
2017-12-19 13:53:46 +01:00
|
|
|
showReport:boolean = false;
|
|
|
|
showInfo :boolean = false;
|
|
|
|
@ViewChild (ModalLoading) loading : ModalLoading ;
|
|
|
|
errorMessage = "";
|
|
|
|
infoMEssage = "";
|
|
|
|
enableUpload:boolean = true;
|
2018-07-25 15:56:02 +02:00
|
|
|
@Input() localStoragePrefix:string="";
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
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 ;
|
2018-06-15 14:58:58 +02:00
|
|
|
}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 ;
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
this.loading.open();
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
this.makeFileRequest(this.properties.uploadService, [], this.filesToUpload).then((result) => {
|
2017-12-19 13:53:46 +01:00
|
|
|
var rows = (result as any).split('\n'); // I have used space, you can use any thing.
|
2018-06-15 14:58:58 +02:00
|
|
|
console.log("Rows:" + rows.length);
|
2017-12-19 13:53:46 +01:00
|
|
|
var i = 0;
|
2018-06-15 14:58:58 +02:00
|
|
|
var invalid_rows = 0;
|
2017-12-19 13:53:46 +01:00
|
|
|
this.duplicateIds = [];
|
|
|
|
this.allIds = [];
|
|
|
|
this.foundIds = [];
|
|
|
|
this.noValidIds = [];
|
|
|
|
this.results.slice(0,this.results.length);
|
|
|
|
this.notFoundIds = [];
|
2018-05-02 14:26:54 +02:00
|
|
|
var limit = 100;
|
|
|
|
for(i=0;i<((rows.length>limit)?limit:rows.length);i++){
|
2017-12-19 13:53:46 +01:00
|
|
|
if(rows[i] && rows[i] != null ){
|
|
|
|
console.log("Row is:" + rows[i]);
|
|
|
|
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);
|
2018-06-29 13:01:48 +02:00
|
|
|
this.duplicateIdsRow.push(i+1);
|
2017-12-19 13:53:46 +01:00
|
|
|
}else{
|
|
|
|
this.allIds.push(id);
|
2018-06-29 13:01:48 +02:00
|
|
|
this.fetchResult(id,accessMode,embargoDate,i+1);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
this.noValidIds.push(id);
|
2018-06-29 13:01:48 +02:00
|
|
|
this.noValidIdsRow.push(i+1);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-06-15 14:58:58 +02:00
|
|
|
}else{
|
|
|
|
invalid_rows++;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-06-15 14:58:58 +02:00
|
|
|
if(rows.length == 0 || rows.length == invalid_rows || rows.length == (invalid_rows + this.noValidIds.length)){
|
|
|
|
this.endOfFetching();
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
}, (error) => {
|
|
|
|
this.enableUpload = true;
|
|
|
|
console.log(error);
|
|
|
|
this.loading.close();
|
|
|
|
this.errorMessage = "An error occured while uploading...";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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 = <Array<File>> fileInput.target.files;
|
2018-07-25 15:56:02 +02:00
|
|
|
this.upload();
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-29 13:01:48 +02:00
|
|
|
fetchResult(id:string,accessMode:string,date:string, row:number){
|
2018-05-03 11:58:30 +02:00
|
|
|
this._searchCrossrefService.searchCrossrefByDOIs([id], this.properties.searchCrossrefAPIURL, true).subscribe(
|
2017-12-19 13:53:46 +01:00
|
|
|
data => {
|
|
|
|
|
2018-05-03 11:58:30 +02:00
|
|
|
var result = data[0];
|
|
|
|
if(data.length > 0){
|
2017-12-19 13:53:46 +01:00
|
|
|
this.foundIds.push(id);
|
|
|
|
result.embargoEndDate = date;
|
|
|
|
this.results.push(result);
|
|
|
|
this.endOfFetching();
|
|
|
|
}else{
|
2018-06-29 13:01:48 +02:00
|
|
|
this.searchInDatacite(id,accessMode,date, row);
|
2017-12-19 13:53:46 +01:00
|
|
|
// this.notFoundIds.push(id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
console.log(err);
|
|
|
|
this.notFoundIds.push(id);
|
2018-06-29 13:01:48 +02:00
|
|
|
this.notFoundIdsRow.push(row);
|
2017-12-19 13:53:46 +01:00
|
|
|
this.endOfFetching();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-06-29 13:01:48 +02:00
|
|
|
searchInDatacite(id:string,accessMode:string,date:string, row:number){
|
2018-05-03 11:58:30 +02:00
|
|
|
this._searchDataciteService.getDataciteResultByDOI(id,this.properties,true).subscribe(
|
|
|
|
items => {
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2018-05-03 11:58:30 +02:00
|
|
|
if(items.length > 0){
|
|
|
|
var result = items[0];
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
this.foundIds.push(id);
|
|
|
|
result.embargoEndDate = date;
|
|
|
|
this.results.push(result);
|
|
|
|
}else{
|
|
|
|
this.notFoundIds.push(id);
|
2018-06-29 13:01:48 +02:00
|
|
|
this.notFoundIdsRow.push(row);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
this.endOfFetching();
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
console.log(err);
|
|
|
|
this.notFoundIds.push(id);
|
2018-06-29 13:01:48 +02:00
|
|
|
this.notFoundIdsRow.push(row);
|
2017-12-19 13:53:46 +01:00
|
|
|
this.endOfFetching();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
endOfFetching(){
|
2018-06-29 13:01:48 +02:00
|
|
|
if(this.allIds.length == this.foundIds.length+this.notFoundIds.length){
|
2017-12-19 13:53:46 +01:00
|
|
|
this.showReport = true;
|
|
|
|
this.enableUpload = true;
|
|
|
|
this.loading.close();
|
2018-07-25 15:56:02 +02:00
|
|
|
if(this.results != null){
|
|
|
|
localStorage.setItem(this.localStoragePrefix + "results", JSON.stringify(this.results));
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|