//© 2019 Dublin City University, Trinity College Dublin. All rights reserved. This material may not be reproduced, displayed, modified or distributed without the express prior written permission of the copyright holder. import React, { Component } from 'react'; import { Modal, ModalHeader, ModalBody, ModalFooter, Button, FormGroup, Form, Label, Input, FormFeedback, Progress } from 'reactstrap'; import axios from 'axios'; import properties from '../../../config/dashboardProperties'; import FileTooLarge from './fileLargeError'; const host = properties.luzzuFramework.host; const port = properties.luzzuFramework.port; const supportedFileFormats = properties.supportedFileFormats; const initialState = { datasetName: "", isSparqlEndPoint: false, sparqlEndPointURL: "", datasetPLD: "", fileToUpload: null, historyFileToUpload: null, datasetNameValid: true, datasetNameDisabled : false, datasetPLDValid: true, datasetPLDDisabled : false, datasetPLDMandatoryValid: true, datasetPLDPrefixValid: true, sparqlEndPointURLValid: true, fileUploadValid: true, fileUploadMandatory: true, fileFormatValid: true, fileUploadProgress: 0, showFileUploadProgress: false, fileUploadCompleted : false, historyFileUploadCompleted : false, fileUploadSuccessfull: false, historyFileUploadSuccessfull: false, fileUploadedName : "", fileUploadedPath : "", dimensionsToAssess : [], assessmentDetails : [], prevAssessmentDetails : [], datasetID : "", displayUseExistingFileCheckBox : false, useExistingFile : false, qualityMetrics : [], lastAssessmentRequestID:"", fileToolarge: false } class DatasetConnectConfigModelWindow extends Component { state = { }; componentDidMount() { //console.log(supportedFileFormats); this.setState( { ...initialState, ...this.props.currentState } ); } triggerSubmitValidation = () => { let datasetNameValid = false; let datasetPLDValid = false; let datasetPLDMandatoryValid = false; let datasetPLDPrefixValid = false; let sparqlEndPointURLValid = false; let fileUploadValid = false; let fileUploadMandatory = false; let fileFormatValid = false; let fileToUpload = null; let fileUploadCompleted = false; //Dataset Name is mandatory if (this.state.datasetName === "") { datasetNameValid = false; } else { datasetNameValid = true; } //Dataset PLD is mandatory if (this.state.datasetPLD === "") { datasetPLDValid = false; datasetPLDMandatoryValid = false; datasetPLDPrefixValid = true; } else { if (this.state.datasetPLD.startsWith("uri:") || this.state.datasetPLD.startsWith("http://") || this.state.datasetPLD.startsWith("https://")) { datasetPLDValid = true; datasetPLDMandatoryValid = true; datasetPLDPrefixValid = true; } else { datasetPLDValid = false; datasetPLDMandatoryValid = true; datasetPLDPrefixValid = false; } } //SPARQL End-Point is mandatory if Is SPARQL is selected as Yes if (this.state.isSparqlEndPoint && this.state.sparqlEndPointURL === "") { sparqlEndPointURLValid = false; } else { sparqlEndPointURLValid = true; } //File Upload if (!this.state.isSparqlEndPoint && this.state.fileToUpload === null) { fileUploadValid = false; fileUploadMandatory = false; fileFormatValid = true; } else { if (this.state.isSparqlEndPoint) { //No need to validate file if it is a SPARQL End-Point. So set state of fileUploadMandatory and fileFormatValid TRUE so that no validation is called. fileToUpload = null; fileUploadValid = true; fileUploadMandatory = true; fileFormatValid = true; } else { fileToUpload = this.state.fileToUpload; let fileName = this.state.fileToUpload.name; let period = fileName.lastIndexOf("."); let fileExtension = fileName.substring(period + 1); let fileFormatSupported = false; fileFormatSupported=supportedFileFormats.findIndex((fileFormats)=>{return fileFormats.extension===fileExtension}); if (fileFormatSupported>=0) { fileUploadValid = true; fileUploadMandatory = true; fileFormatValid = true; } else { fileUploadValid = false; fileUploadMandatory = true; fileFormatValid = false; } } } if(this.state.useExistingFile) { fileUploadMandatory=true; } if (this.state.fileUploadCompleted && !this.state.fileUploadSuccessfull) { fileUploadCompleted = false; } else if (this.state.fileUploadCompleted && this.state.fileUploadSuccessfull) { fileUploadCompleted = true; } this.setState( { datasetNameValid: datasetNameValid, datasetPLDValid: datasetPLDValid, datasetPLDMandatoryValid: datasetPLDMandatoryValid, datasetPLDPrefixValid: datasetPLDPrefixValid, sparqlEndPointURLValid: sparqlEndPointURLValid, fileUploadValid: fileUploadValid, fileUploadMandatory: fileUploadMandatory, fileFormatValid: fileFormatValid, fileToUpload: fileToUpload, fileUploadCompleted: fileUploadCompleted }, () => { this.triggerSubmitAction(); } ); } triggerSubmitAction = () => { if(this.state.datasetNameValid && this.state.datasetPLDValid && !this.state.isSparqlEndPoint && this.state.useExistingFile){ this.props.onNext(this.state); } else { if (this.state.datasetNameValid && this.state.datasetPLDValid && !this.state.isSparqlEndPoint && this.state.fileUploadValid) { if (!this.state.fileUploadCompleted && !this.state.fileUploadSuccessfull) { if(this.state.fileToUpload.size<=52428800) { this.fileUploadHandler(); } else { this.setState({ fileToolarge:true }); } } else { this.props.onNext(this.state); } } else if (this.state.datasetNameValid && this.state.datasetPLDValid && this.state.isSparqlEndPoint && this.state.sparqlEndPointURLValid) { this.props.onNext(this.state); } } } fileUploadHandler = () => { const formData = new FormData(); formData.append('file', this.state.fileToUpload, this.state.fileToUpload.name); axios.post('http://'+host+':'+port+'/Luzzu/v4/import', formData, { onUploadProgress: (progressEvent) => { this.setState({ showFileUploadProgress: true, fileUploadProgress: Math.round((progressEvent.loaded / progressEvent.total) * 100), }); }, headers: {'Accept': 'application/json','Content-Type':'multipart/form-data'} }) .then((res) => { if (res.status === 200) { this.setState({ fileUploadCompleted: true, historyFileUploadCompleted : true, fileUploadSuccessfull: true, historyFileUploadSuccessfull: true, fileUploadedName: res.data.fileName, fileUploadedPath: res.data.filePath, historyFileToUpload : this.state.fileToUpload, displayUseExistingFileCheckBox : false }); const sleep = (milliseconds) => { return new Promise(resolve => setTimeout(resolve, milliseconds)) } sleep(1000).then(() => { this.props.onNext(this.state); }); } else { this.setState({ showFileUploadProgress: false, fileUploadProgress: 0, fileUploadCompleted: true, fileUploadSuccessfull: false, fileUploadedName: "", fileUploadedPath: "" }); } }) .catch((err) => { if (!err.response) { // network error //console.log('Error: Network Error. Unable to connect to Server'); } else { //console.log(err.response.data.message); } this.setState({ showFileUploadProgress: false, fileUploadProgress: 0, fileUploadCompleted: true, fileUploadSuccessfull: false, fileUploadedName: "", fileUploadedPath: "" }); }); } datasetNameChange = (event) => { let regExpr = /[^a-zA-Z0-9-_ ]/g; this.setState( { datasetName: event.target.value.replace(regExpr, "") } ); } sparqlEndPointURLChange = (event) => { this.setState( { sparqlEndPointURL: event.target.value } ); } useExistingFileChange = (event) => { if(this.state.useExistingFile) { this.setState({ useExistingFile : false, fileToUpload: this.state.historyFileToUpload, fileUploadSuccessfull:this.state.historyFileUploadSuccessfull, fileUploadCompleted:this.state.historyFileUploadCompleted }); } else { this.setState({ useExistingFile : true, fileToUpload: null }); } } isSparqlEndPointChange = (event) => { let isSparqlEndPoint = false; if (event.target.checked) { if (event.target.value === "true") { if (document.getElementById("uploadFileSelector")) { document.getElementById("uploadFileSelector").value = ""; } isSparqlEndPoint = true; this.setState( { isSparqlEndPoint: isSparqlEndPoint, fileToUpload: null, fileUploadValid: true, fileUploadMandatory: true, fileFormatValid: true } ); } else { this.setState( { isSparqlEndPoint: isSparqlEndPoint, sparqlEndPointURL: "", sparqlEndPointURLValid: true, fileToUpload : this.state.historyFileToUpload, fileUploadSuccessfull:this.state.historyFileUploadSuccessfull, fileUploadCompleted:this.state.historyFileUploadCompleted } ); } } } is1SpatialChange = (event) => { let is1Spatial = false; if (event.target.checked) { if (event.target.value === "true") { if (document.getElementById("uploadFileSelector")) { document.getElementById("uploadFileSelector").value = ""; } is1Spatial = true; this.setState( { is1Spatial: is1Spatial } ); } else { this.setState( { is1Spatial: is1Spatial } ); } } } datasetPLDChange = (event) => { this.setState( { datasetPLD: event.target.value } ); } closeFileLargeError = () =>{ this.setState({ fileToolarge:false }); } fileSelectionOnChange = (event) => { if (event.target.files.length > 0) { this.setState( { fileToUpload: event.target.files[0], fileUploadValid: true, fileUploadMandatory: true, fileFormatValid: true, fileUploadCompleted : false, fileUploadSuccessfull : false } ); } else { this.setState( { fileToUpload: this.state.historyFileToUpload, fileUploadValid: true, fileUploadMandatory: true, fileFormatValid: true, fileUploadSuccessfull:this.state.historyFileUploadSuccessfull, fileUploadCompleted:this.state.historyFileUploadCompleted } ); } if (this.state.fileUploadCompleted && !this.state.fileUploadSuccessfull) { this.setState( { fileUploadCompleted: false } ); } } render() { //console.log(this.state); let formats = supportedFileFormats.reduce((prev,next)=>{ if(prev.fileFormat!==undefined) { return (prev.fileFormat + " " + next.fileFormat); } else { return (prev + " " + next.fileFormat); } }); return ( Connect a Dataset
{this.state.datasetNameValid ? : } Dataset Name is Mandatory!!! {this.state.sparqlEndPointURLValid ? : } {this.state.isSparqlEndPoint ? SPARQL End-Point is Mandatory!!! : null} {this.state.datasetPLDValid ? : } {this.state.datasetPLDMandatoryValid ? null : Dataset PLD is Mandatory!!!} {this.state.datasetPLDPrefixValid ? null : Dataset PLD should be prefixed with either 'uri:' or 'http://' or 'https://'} {this.state.displayUseExistingFileCheckBox & !this.state.isSparqlEndPoint ?

: null} {this.state.fileUploadValid ? : } {this.state.fileUploadMandatory ? null : Please select a file!!!} {this.state.fileFormatValid ? null : Only following file formats are allowed.
({formats})
}
{this.state.showFileUploadProgress ? {this.state.fileUploadProgress}% : null} {this.state.fileUploadCompleted && !this.state.fileUploadSuccessfull ?
File Upload Failed. Server did not respond!!!
: null} {this.state.fileUploadCompleted && this.state.fileUploadSuccessfull ?
File Upload Successfull.
: null}
{' '} {this.state.fileToolarge?:null}
); } } export default DatasetConnectConfigModelWindow;