2019-07-23 14:23:12 +02:00
|
|
|
import {Component, Input, ViewChild} from '@angular/core';
|
2017-12-19 13:53:46 +01:00
|
|
|
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';
|
2019-07-23 14:23:12 +02:00
|
|
|
import {EnvProperties} from '../../../utils/properties/env-properties';
|
|
|
|
import {ClaimEntity} from "../../claim-utils/claimHelper.class";
|
|
|
|
|
|
|
|
declare var UIkit: any;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
2019-07-23 14:23:12 +02:00
|
|
|
selector: 'bulk-claim',
|
|
|
|
template: `
|
|
|
|
<div class="uk-animation uk-text-center" style=" ">
|
|
|
|
<form class=" ">
|
|
|
|
|
2019-07-24 14:46:29 +02:00
|
|
|
<div>
|
|
|
|
<div>
|
2019-07-23 14:23:12 +02:00
|
|
|
<!--div class="uk-text-lead">Upload a DOI csv file <helper div="link-result-bulk" tooltip=true ></helper></div>
|
|
|
|
<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)"/>
|
|
|
|
<span class="uk-link " style="text-decoration: underline;">Upload a DOI's CSV file </span>
|
|
|
|
<!--button class="uk-button portal-button" type="button" tabindex="-1" [class.disabled]="!enableUpload" ><span class="uk-margin-small-right uk-icon" >
|
|
|
|
<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-->
|
|
|
|
<!--helper div="link-result-bulk" tooltip=true ></helper-->
|
2017-12-19 13:53:46 +01:00
|
|
|
</div>
|
2019-07-23 14:23:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
<div *ngIf="showReport" uk-alert class="uk-alert uk-alert-primary" role="alert">
|
2019-07-24 14:46:29 +02:00
|
|
|
<a class="uk-alert-close uk-icon uk-close">
|
|
|
|
<svg width="14" height="14" viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"
|
|
|
|
data-svg="close-icon">
|
|
|
|
<line fill="none" stroke="#000" stroke-width="1.1" x1="1" y1="1" x2="13" y2="13"></line>
|
|
|
|
<line fill="none" stroke="#000" stroke-width="1.1" x1="13" y1="1" x2="1" y2="13"></line>
|
|
|
|
</svg>
|
|
|
|
</a>
|
|
|
|
<div>Uploaded file contains <span
|
|
|
|
class="uk-text-bold">{{allIds.length}} {{((allIds.length == 1) ? 'DOI' : 'DOIs')}}</span>.
|
2019-07-23 14:23:12 +02:00
|
|
|
<span
|
2019-07-24 14:46:29 +02:00
|
|
|
*ngIf="exceedsLimit">
|
|
|
|
<div class="uk-text-danger">Basket exceeds the size limit.</div>
|
|
|
|
<span *ngIf="allIds.length > 0 && foundIds.length > 0">Only </span>
|
|
|
|
<span *ngIf="allIds.length > 0 && foundIds.length == 0">No results added.</span>
|
|
|
|
</span>
|
|
|
|
<span
|
|
|
|
*ngIf="allIds.length > 0 && foundIds.length > 0">{{foundIds.length}} {{((foundIds.length == 1) ? 'result was' : 'results were')}} succefully fetched from
|
|
|
|
<span class="uk-text-bold">CrossRef</span>{{ ' and ' }}<span
|
|
|
|
class="uk-text-bold">Datacite</span>.</span>
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
*ngIf="duplicateIds.length > 0">{{duplicateIds.length | number}} duplicate DOIs in {{((duplicateIds.length == 1) ? 'line' : 'lines')}} {{duplicateIdsRow}}.
|
|
|
|
</div>
|
2019-07-24 14:46:29 +02:00
|
|
|
<div *ngIf="notFoundIds.length > 0">Couldn't be fetched:
|
2019-07-23 14:23:12 +02:00
|
|
|
<ul class="">
|
|
|
|
<li *ngFor="let id of notFoundIds; let i = index">"{{id}}" in line {{notFoundIdsRow[i]}}</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<div *ngIf="noValidIds.length > 0">No valid DOIs:
|
|
|
|
<ul class="">
|
|
|
|
<li *ngFor="let id of noValidIds; let i = index">"{{id}}" in line {{noValidIdsRow[i]}}</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<div
|
2019-07-24 14:46:29 +02:00
|
|
|
*ngIf="allIds.length == 0 || (foundIds.length == 0 && !exceedsLimit)"> Please make sure that the uploaded file, is a csv file with the proper format.
|
2019-07-23 14:23:12 +02:00
|
|
|
</div>
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
</div>
|
2019-07-23 14:23:12 +02:00
|
|
|
<div *ngIf="errorMessage.length > 0 " class="uk-alert uk-alert-danger" role="alert">
|
2019-07-24 14:46:29 +02:00
|
|
|
<a class="uk-alert-close uk-icon uk-close">
|
|
|
|
<svg width="14" height="14" viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"
|
|
|
|
data-svg="close-icon">
|
|
|
|
<line fill="none" stroke="#000" stroke-width="1.1" x1="1" y1="1" x2="13" y2="13"></line>
|
|
|
|
<line fill="none" stroke="#000" stroke-width="1.1" x1="13" y1="1" x2="1" y2="13"></line>
|
|
|
|
</svg>
|
|
|
|
</a>
|
2019-07-23 14:23:12 +02:00
|
|
|
{{errorMessage}}</div>
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
</div>
|
2019-07-23 14:23:12 +02:00
|
|
|
<!--helper div="link-result-bulk" ></helper-->
|
2018-02-15 11:36:12 +01:00
|
|
|
</div>
|
2019-07-23 14:23:12 +02:00
|
|
|
</form>
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
</div>
|
|
|
|
<div class="uk-width-1-1">
|
|
|
|
<modal-loading
|
|
|
|
[message]="'Uploading, reading your documet and fetching results. Please give us a moment..'"></modal-loading>
|
|
|
|
</div>
|
|
|
|
`
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
})
|
|
|
|
export class BulkClaimComponent {
|
|
|
|
filesToUpload: Array<File>;
|
|
|
|
source: string = "crossref";
|
2019-07-23 14:23:12 +02:00
|
|
|
type: string = "publication";
|
|
|
|
@Input() public select: boolean = true;
|
2017-12-19 13:53:46 +01:00
|
|
|
@Input() public results;
|
2019-07-23 14:23:12 +02:00
|
|
|
@Input() public properties: EnvProperties;
|
|
|
|
|
|
|
|
allIds: string[] = [];
|
|
|
|
foundIds: string[] = [];
|
2019-07-24 14:46:29 +02:00
|
|
|
existedIds: string[] = [];
|
2019-07-23 14:23:12 +02:00
|
|
|
duplicateIds: string[] = [];
|
|
|
|
duplicateIdsRow: number[] = [];
|
|
|
|
notFoundIds: string[] = [];
|
|
|
|
notFoundIdsRow: number[] = [];
|
|
|
|
noValidIds: string[] = [];
|
|
|
|
noValidIdsRow: number[] = [];
|
|
|
|
showReport: boolean = false;
|
|
|
|
@ViewChild(ModalLoading) loading: ModalLoading;
|
2017-12-19 13:53:46 +01:00
|
|
|
errorMessage = "";
|
2019-07-23 14:23:12 +02:00
|
|
|
enableUpload: boolean = true;
|
|
|
|
@Input() localStoragePrefix: string = "";
|
2018-11-12 16:36:20 +01:00
|
|
|
exceedsLimit = false;
|
2019-07-24 14:46:29 +02:00
|
|
|
@Input() basketLimit ;
|
2019-07-23 14:23:12 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
constructor(private _searchCrossrefService: SearchCrossrefService, private _searchDataciteService: SearchDataciteService) {
|
2019-07-23 14:23:12 +02:00
|
|
|
this.filesToUpload = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
upload() {
|
|
|
|
this.enableUpload = false;
|
|
|
|
this.showReport = false;
|
|
|
|
this.errorMessage = "";
|
2019-07-23 14:23:12 +02:00
|
|
|
if (this.filesToUpload.length == 0) {
|
2017-12-19 13:53:46 +01:00
|
|
|
this.errorMessage = "There is no selected file to upload.";
|
2019-07-23 14:23:12 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (this.filesToUpload[0].name.indexOf(".csv") == -1 || this.filesToUpload[0].type != "text/csv") {
|
2018-06-15 14:58:58 +02:00
|
|
|
this.errorMessage = "No valid file type. The required type is CSV";
|
2019-07-23 14:23:12 +02:00
|
|
|
return;
|
2018-06-15 14:58:58 +02:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
this.loading.open();
|
|
|
|
|
2019-06-28 12:28:39 +02:00
|
|
|
this.makeFileRequest(this.properties.utilsService + '/upload', [], this.filesToUpload).then((result) => {
|
2019-07-23 14:23:12 +02:00
|
|
|
const rows = (result as any).split('\n'); // I have used space, you can use any thing.
|
|
|
|
this.exceedsLimit = false;
|
|
|
|
let invalid_rows = 0;
|
|
|
|
this.duplicateIds = [];
|
2019-07-24 14:46:29 +02:00
|
|
|
this.existedIds = [];
|
2019-07-23 14:23:12 +02:00
|
|
|
this.allIds = [];
|
|
|
|
this.foundIds = [];
|
|
|
|
this.noValidIds = [];
|
|
|
|
this.results.slice(0, this.results.length);
|
|
|
|
this.notFoundIds = [];
|
2019-07-24 14:46:29 +02:00
|
|
|
if (rows.length + this.results.length > this.basketLimit) {
|
2019-07-23 14:23:12 +02:00
|
|
|
this.exceedsLimit = true;
|
|
|
|
|
|
|
|
}
|
2019-07-24 14:46:29 +02:00
|
|
|
let currentLength = this.results.length;
|
|
|
|
for (let i = 0; i < ( rows.length); i++) {
|
2019-07-23 14:23:12 +02:00
|
|
|
if (rows[i] && rows[i] != null) {
|
|
|
|
const values = rows[i].split(',');
|
2018-11-12 16:36:20 +01:00
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
const id = BulkClaimComponent.removeDoubleQuotes(values[0]);
|
|
|
|
if (DOI.isValidDOI(id)) {
|
|
|
|
let accessMode = (values[1] != undefined) ? BulkClaimComponent.removeDoubleQuotes(values[1]) : "OPEN";
|
|
|
|
accessMode = (BulkClaimComponent.validateAccessMode(accessMode) ? accessMode : "OPEN");
|
2019-07-24 14:46:29 +02:00
|
|
|
let embargoDate = (values[2] != undefined) ? Dates.getDateFromString(BulkClaimComponent.removeDoubleQuotes(values[2])) : Dates.getDateToday();
|
2019-07-23 14:23:12 +02:00
|
|
|
if (this.allIds.indexOf(id) > -1) {
|
|
|
|
this.duplicateIds.push(id);
|
|
|
|
this.duplicateIdsRow.push(i + 1);
|
|
|
|
} else {
|
|
|
|
this.allIds.push(id);
|
2019-07-24 14:46:29 +02:00
|
|
|
if (currentLength < this.basketLimit){
|
|
|
|
currentLength++;
|
|
|
|
this.fetchResult(id, accessMode, Dates.getDateToString(embargoDate), i + 1);
|
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.noValidIds.push(id);
|
|
|
|
this.noValidIdsRow.push(i + 1);
|
2018-11-12 16:36:20 +01:00
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
} else {
|
|
|
|
invalid_rows++;
|
|
|
|
}
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-24 14:46:29 +02:00
|
|
|
if (rows.length == 0 || rows.length == invalid_rows || rows.length == (invalid_rows + this.noValidIds.length) || this.basketLimit <= this.results.length) {
|
2019-07-23 14:23:12 +02:00
|
|
|
this.endOfFetching();
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
|
|
|
|
}, (error) => {
|
|
|
|
this.enableUpload = true;
|
|
|
|
this.loading.close();
|
2019-07-24 14:46:29 +02:00
|
|
|
this.errorMessage = "An error occured.";
|
2019-07-23 14:23:12 +02:00
|
|
|
BulkClaimComponent.handleError("Error uploading file", error);
|
|
|
|
});
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
private static removeDoubleQuotes(value) {
|
|
|
|
if (value.indexOf('"') == 0) {
|
|
|
|
value = value.substring(1, value.length);
|
|
|
|
}
|
|
|
|
const index = +value.indexOf('"');
|
|
|
|
if (index == (value.length - 1) || index == (value.length - 2)) {
|
|
|
|
value = value.substring(0, index);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static validateAccessMode(value) {
|
|
|
|
const accessModes = ["OPEN", "CLOSED", "EMBARGO"];
|
|
|
|
return accessModes.indexOf(value) > -1;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
fileChangeEvent(fileInput: any) {
|
|
|
|
this.filesToUpload = <Array<File>>fileInput.target.files;
|
|
|
|
this.upload();
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
|
2019-07-23 14:23:12 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const formData: any = new FormData();
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
for (let 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);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
xhr.open("POST", url, true);
|
|
|
|
xhr.send(formData);
|
|
|
|
});
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
fetchResult(id: string, accessMode: string, date: string, row: number) {
|
|
|
|
this._searchCrossrefService.searchCrossrefByDOIs([id], this.properties.searchCrossrefAPIURL, true).subscribe(
|
|
|
|
data => {
|
|
|
|
|
2019-07-24 14:46:29 +02:00
|
|
|
const result:ClaimEntity = data[1][0];
|
2019-07-23 14:23:12 +02:00
|
|
|
if (data[1].length > 0) {
|
|
|
|
this.foundIds.push(id);
|
2019-07-24 14:46:29 +02:00
|
|
|
result.result.accessRights = accessMode;
|
|
|
|
result.result.embargoEndDate = date;
|
2019-07-23 14:23:12 +02:00
|
|
|
if(!this.isSelected(result)){
|
|
|
|
this.results.push(result);
|
2019-07-24 14:46:29 +02:00
|
|
|
}else{
|
|
|
|
this.existedIds.push(id);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
this.endOfFetching();
|
|
|
|
} else {
|
|
|
|
this.searchInDatacite(id, accessMode, date, row);
|
|
|
|
// this.notFoundIds.push(id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
//console.log(err);
|
|
|
|
BulkClaimComponent.handleError("Error getting crossref by DOIs: " + id, err);
|
|
|
|
this.notFoundIds.push(id);
|
|
|
|
this.notFoundIdsRow.push(row);
|
|
|
|
this.endOfFetching();
|
|
|
|
}
|
|
|
|
);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
|
|
|
|
searchInDatacite(id: string, accessMode: string, date: string, row: number) {
|
|
|
|
this._searchDataciteService.getDataciteResultByDOI(id, this.properties, true).subscribe(
|
|
|
|
items => {
|
|
|
|
|
|
|
|
if (items[1].length > 0) {
|
|
|
|
var result = items[1][0];
|
|
|
|
|
|
|
|
this.foundIds.push(id);
|
2019-07-24 14:46:29 +02:00
|
|
|
result.result.accessRights = accessMode;
|
|
|
|
result.result.embargoEndDate = date;
|
2019-07-23 14:23:12 +02:00
|
|
|
if(!this.isSelected(result)){
|
|
|
|
this.results.push(result);
|
2019-07-24 14:46:29 +02:00
|
|
|
}else{
|
|
|
|
this.existedIds.push(id);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
} else {
|
|
|
|
this.notFoundIds.push(id);
|
|
|
|
this.notFoundIdsRow.push(row);
|
|
|
|
}
|
|
|
|
this.endOfFetching();
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
//console.log(err);
|
|
|
|
BulkClaimComponent.handleError("Error getting datacite resultentityI: " + id, err);
|
|
|
|
this.notFoundIds.push(id);
|
|
|
|
this.notFoundIdsRow.push(row);
|
|
|
|
this.endOfFetching();
|
|
|
|
}
|
|
|
|
);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
|
|
|
|
endOfFetching() {
|
2019-07-24 14:46:29 +02:00
|
|
|
// if (this.basketLimit <= this.results.length) {
|
|
|
|
// this.enableUpload = true;
|
|
|
|
// this.loading.close();
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// console.log(this.allIds.length+" "+this.foundIds.length +" "+ this.notFoundIds.length+" "+this.existedIds.length + " " + this.results.length);
|
|
|
|
if (this.allIds.length == this.foundIds.length + this.notFoundIds.length || this.basketLimit <= (this.results.length+this.existedIds.length+this.notFoundIds.length)) {
|
2017-12-19 13:53:46 +01:00
|
|
|
this.showReport = true;
|
|
|
|
this.enableUpload = true;
|
|
|
|
this.loading.close();
|
2019-07-23 14:23:12 +02:00
|
|
|
if (this.results != null) {
|
|
|
|
localStorage.setItem(this.localStoragePrefix, JSON.stringify(this.results));
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-02-18 15:00:43 +01:00
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
private static handleError(message: string, error) {
|
|
|
|
console.error("Bulk Claim (component): " + message, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
private isSelected(result: ClaimEntity) {
|
|
|
|
|
|
|
|
let found: boolean = false;
|
|
|
|
const id = result.id;
|
|
|
|
for (let _i = 0; _i < this.results.length; _i++) {
|
|
|
|
let item = this.results[_i];
|
|
|
|
if (item.id && item.id == id) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
// indexOf doesn't work when results came from
|
|
|
|
// return this.selectedResults.indexOf(entity)!=-1;
|
2019-02-18 15:00:43 +01:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|