DatasetConnectConfigModelWindow.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. //© 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.
  2. import React, { Component } from 'react';
  3. import {
  4. Modal,
  5. ModalHeader,
  6. ModalBody,
  7. ModalFooter,
  8. Button,
  9. FormGroup,
  10. Form,
  11. Label,
  12. Input,
  13. FormFeedback,
  14. Progress
  15. } from 'reactstrap';
  16. import axios from 'axios';
  17. import properties from '../../../config/dashboardProperties';
  18. import FileTooLarge from './fileLargeError';
  19. const host = properties.luzzuFramework.host;
  20. const port = properties.luzzuFramework.port;
  21. const supportedFileFormats = properties.supportedFileFormats;
  22. const initialState = {
  23. datasetName: "",
  24. isSparqlEndPoint: false,
  25. sparqlEndPointURL: "",
  26. datasetPLD: "",
  27. fileToUpload: null,
  28. historyFileToUpload: null,
  29. datasetNameValid: true,
  30. datasetNameDisabled : false,
  31. datasetPLDValid: true,
  32. datasetPLDDisabled : false,
  33. datasetPLDMandatoryValid: true,
  34. datasetPLDPrefixValid: true,
  35. sparqlEndPointURLValid: true,
  36. fileUploadValid: true,
  37. fileUploadMandatory: true,
  38. fileFormatValid: true,
  39. fileUploadProgress: 0,
  40. showFileUploadProgress: false,
  41. fileUploadCompleted : false,
  42. historyFileUploadCompleted : false,
  43. fileUploadSuccessfull: false,
  44. historyFileUploadSuccessfull: false,
  45. fileUploadedName : "",
  46. fileUploadedPath : "",
  47. dimensionsToAssess : [],
  48. assessmentDetails : [],
  49. prevAssessmentDetails : [],
  50. datasetID : "",
  51. displayUseExistingFileCheckBox : false,
  52. useExistingFile : false,
  53. qualityMetrics : [],
  54. lastAssessmentRequestID:"",
  55. fileToolarge: false
  56. }
  57. class DatasetConnectConfigModelWindow extends Component {
  58. state = {
  59. };
  60. componentDidMount() {
  61. //console.log(supportedFileFormats);
  62. this.setState(
  63. {
  64. ...initialState,
  65. ...this.props.currentState
  66. }
  67. );
  68. }
  69. triggerSubmitValidation = () => {
  70. let datasetNameValid = false;
  71. let datasetPLDValid = false;
  72. let datasetPLDMandatoryValid = false;
  73. let datasetPLDPrefixValid = false;
  74. let sparqlEndPointURLValid = false;
  75. let fileUploadValid = false;
  76. let fileUploadMandatory = false;
  77. let fileFormatValid = false;
  78. let fileToUpload = null;
  79. let fileUploadCompleted = false;
  80. //Dataset Name is mandatory
  81. if (this.state.datasetName === "") {
  82. datasetNameValid = false;
  83. }
  84. else {
  85. datasetNameValid = true;
  86. }
  87. //Dataset PLD is mandatory
  88. if (this.state.datasetPLD === "") {
  89. datasetPLDValid = false;
  90. datasetPLDMandatoryValid = false;
  91. datasetPLDPrefixValid = true;
  92. }
  93. else {
  94. if (this.state.datasetPLD.startsWith("uri:") || this.state.datasetPLD.startsWith("http://") || this.state.datasetPLD.startsWith("https://")) {
  95. datasetPLDValid = true;
  96. datasetPLDMandatoryValid = true;
  97. datasetPLDPrefixValid = true;
  98. }
  99. else {
  100. datasetPLDValid = false;
  101. datasetPLDMandatoryValid = true;
  102. datasetPLDPrefixValid = false;
  103. }
  104. }
  105. //SPARQL End-Point is mandatory if Is SPARQL is selected as Yes
  106. if (this.state.isSparqlEndPoint && this.state.sparqlEndPointURL === "") {
  107. sparqlEndPointURLValid = false;
  108. }
  109. else {
  110. sparqlEndPointURLValid = true;
  111. }
  112. //File Upload
  113. if (!this.state.isSparqlEndPoint && this.state.fileToUpload === null) {
  114. fileUploadValid = false;
  115. fileUploadMandatory = false;
  116. fileFormatValid = true;
  117. }
  118. else {
  119. if (this.state.isSparqlEndPoint) {
  120. //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.
  121. fileToUpload = null;
  122. fileUploadValid = true;
  123. fileUploadMandatory = true;
  124. fileFormatValid = true;
  125. }
  126. else {
  127. fileToUpload = this.state.fileToUpload;
  128. let fileName = this.state.fileToUpload.name;
  129. let period = fileName.lastIndexOf(".");
  130. let fileExtension = fileName.substring(period + 1);
  131. let fileFormatSupported = false;
  132. fileFormatSupported=supportedFileFormats.findIndex((fileFormats)=>{return fileFormats.extension===fileExtension});
  133. if (fileFormatSupported>=0) {
  134. fileUploadValid = true;
  135. fileUploadMandatory = true;
  136. fileFormatValid = true;
  137. }
  138. else {
  139. fileUploadValid = false;
  140. fileUploadMandatory = true;
  141. fileFormatValid = false;
  142. }
  143. }
  144. }
  145. if(this.state.useExistingFile)
  146. {
  147. fileUploadMandatory=true;
  148. }
  149. if (this.state.fileUploadCompleted && !this.state.fileUploadSuccessfull) {
  150. fileUploadCompleted = false;
  151. }
  152. else if (this.state.fileUploadCompleted && this.state.fileUploadSuccessfull) {
  153. fileUploadCompleted = true;
  154. }
  155. this.setState(
  156. {
  157. datasetNameValid: datasetNameValid,
  158. datasetPLDValid: datasetPLDValid,
  159. datasetPLDMandatoryValid: datasetPLDMandatoryValid,
  160. datasetPLDPrefixValid: datasetPLDPrefixValid,
  161. sparqlEndPointURLValid: sparqlEndPointURLValid,
  162. fileUploadValid: fileUploadValid,
  163. fileUploadMandatory: fileUploadMandatory,
  164. fileFormatValid: fileFormatValid,
  165. fileToUpload: fileToUpload,
  166. fileUploadCompleted: fileUploadCompleted
  167. }, () => { this.triggerSubmitAction(); }
  168. );
  169. }
  170. triggerSubmitAction = () => {
  171. if(this.state.datasetNameValid && this.state.datasetPLDValid && !this.state.isSparqlEndPoint && this.state.useExistingFile){
  172. this.props.onNext(this.state);
  173. }
  174. else {
  175. if (this.state.datasetNameValid && this.state.datasetPLDValid && !this.state.isSparqlEndPoint && this.state.fileUploadValid) {
  176. if (!this.state.fileUploadCompleted && !this.state.fileUploadSuccessfull) {
  177. if(this.state.fileToUpload.size<=52428800)
  178. {
  179. this.fileUploadHandler();
  180. }
  181. else {
  182. this.setState({
  183. fileToolarge:true
  184. });
  185. }
  186. }
  187. else {
  188. this.props.onNext(this.state);
  189. }
  190. }
  191. else if (this.state.datasetNameValid && this.state.datasetPLDValid && this.state.isSparqlEndPoint && this.state.sparqlEndPointURLValid) {
  192. this.props.onNext(this.state);
  193. }
  194. }
  195. }
  196. fileUploadHandler = () => {
  197. const formData = new FormData();
  198. formData.append('file', this.state.fileToUpload, this.state.fileToUpload.name);
  199. axios.post('http://'+host+':'+port+'/Luzzu/v4/import', formData, {
  200. onUploadProgress: (progressEvent) => {
  201. this.setState({
  202. showFileUploadProgress: true,
  203. fileUploadProgress: Math.round((progressEvent.loaded / progressEvent.total) * 100),
  204. });
  205. }, headers: {'Accept': 'application/json','Content-Type':'multipart/form-data'}
  206. })
  207. .then((res) => {
  208. if (res.status === 200) {
  209. this.setState({
  210. fileUploadCompleted: true,
  211. historyFileUploadCompleted : true,
  212. fileUploadSuccessfull: true,
  213. historyFileUploadSuccessfull: true,
  214. fileUploadedName: res.data.fileName,
  215. fileUploadedPath: res.data.filePath,
  216. historyFileToUpload : this.state.fileToUpload,
  217. displayUseExistingFileCheckBox : false
  218. });
  219. const sleep = (milliseconds) => {
  220. return new Promise(resolve => setTimeout(resolve, milliseconds))
  221. }
  222. sleep(1000).then(() => {
  223. this.props.onNext(this.state);
  224. });
  225. }
  226. else {
  227. this.setState({
  228. showFileUploadProgress: false,
  229. fileUploadProgress: 0,
  230. fileUploadCompleted: true,
  231. fileUploadSuccessfull: false,
  232. fileUploadedName: "",
  233. fileUploadedPath: ""
  234. });
  235. }
  236. })
  237. .catch((err) => {
  238. if (!err.response) {
  239. // network error
  240. //console.log('Error: Network Error. Unable to connect to Server');
  241. } else {
  242. //console.log(err.response.data.message);
  243. }
  244. this.setState({
  245. showFileUploadProgress: false,
  246. fileUploadProgress: 0,
  247. fileUploadCompleted: true,
  248. fileUploadSuccessfull: false,
  249. fileUploadedName: "",
  250. fileUploadedPath: ""
  251. });
  252. });
  253. }
  254. datasetNameChange = (event) => {
  255. let regExpr = /[^a-zA-Z0-9-_ ]/g;
  256. this.setState(
  257. { datasetName: event.target.value.replace(regExpr, "") }
  258. );
  259. }
  260. sparqlEndPointURLChange = (event) => {
  261. this.setState(
  262. { sparqlEndPointURL: event.target.value }
  263. );
  264. }
  265. useExistingFileChange = (event) => {
  266. if(this.state.useExistingFile)
  267. {
  268. this.setState({
  269. useExistingFile : false,
  270. fileToUpload: this.state.historyFileToUpload,
  271. fileUploadSuccessfull:this.state.historyFileUploadSuccessfull,
  272. fileUploadCompleted:this.state.historyFileUploadCompleted
  273. });
  274. }
  275. else {
  276. this.setState({
  277. useExistingFile : true,
  278. fileToUpload: null
  279. });
  280. }
  281. }
  282. isSparqlEndPointChange = (event) => {
  283. let isSparqlEndPoint = false;
  284. if (event.target.checked) {
  285. if (event.target.value === "true") {
  286. if (document.getElementById("uploadFileSelector")) {
  287. document.getElementById("uploadFileSelector").value = "";
  288. }
  289. isSparqlEndPoint = true;
  290. this.setState(
  291. {
  292. isSparqlEndPoint: isSparqlEndPoint,
  293. fileToUpload: null,
  294. fileUploadValid: true,
  295. fileUploadMandatory: true,
  296. fileFormatValid: true
  297. }
  298. );
  299. }
  300. else {
  301. this.setState(
  302. {
  303. isSparqlEndPoint: isSparqlEndPoint,
  304. sparqlEndPointURL: "",
  305. sparqlEndPointURLValid: true,
  306. fileToUpload : this.state.historyFileToUpload,
  307. fileUploadSuccessfull:this.state.historyFileUploadSuccessfull,
  308. fileUploadCompleted:this.state.historyFileUploadCompleted
  309. }
  310. );
  311. }
  312. }
  313. }
  314. is1SpatialChange = (event) => {
  315. let is1Spatial = false;
  316. if (event.target.checked) {
  317. if (event.target.value === "true") {
  318. if (document.getElementById("uploadFileSelector")) {
  319. document.getElementById("uploadFileSelector").value = "";
  320. }
  321. is1Spatial = true;
  322. this.setState(
  323. {
  324. is1Spatial: is1Spatial
  325. }
  326. );
  327. }
  328. else {
  329. this.setState(
  330. {
  331. is1Spatial: is1Spatial
  332. }
  333. );
  334. }
  335. }
  336. }
  337. datasetPLDChange = (event) => {
  338. this.setState(
  339. { datasetPLD: event.target.value }
  340. );
  341. }
  342. closeFileLargeError = () =>{
  343. this.setState({
  344. fileToolarge:false
  345. });
  346. }
  347. fileSelectionOnChange = (event) => {
  348. if (event.target.files.length > 0) {
  349. this.setState(
  350. {
  351. fileToUpload: event.target.files[0],
  352. fileUploadValid: true,
  353. fileUploadMandatory: true,
  354. fileFormatValid: true,
  355. fileUploadCompleted : false,
  356. fileUploadSuccessfull : false
  357. }
  358. );
  359. } else {
  360. this.setState(
  361. {
  362. fileToUpload: this.state.historyFileToUpload,
  363. fileUploadValid: true,
  364. fileUploadMandatory: true,
  365. fileFormatValid: true,
  366. fileUploadSuccessfull:this.state.historyFileUploadSuccessfull,
  367. fileUploadCompleted:this.state.historyFileUploadCompleted
  368. }
  369. );
  370. }
  371. if (this.state.fileUploadCompleted && !this.state.fileUploadSuccessfull) {
  372. this.setState(
  373. {
  374. fileUploadCompleted: false
  375. }
  376. );
  377. }
  378. }
  379. render() {
  380. //console.log(this.state);
  381. let formats = supportedFileFormats.reduce((prev,next)=>{
  382. if(prev.fileFormat!==undefined)
  383. {
  384. return (prev.fileFormat + " " + next.fileFormat);
  385. }
  386. else
  387. {
  388. return (prev + " " + next.fileFormat);
  389. }
  390. });
  391. return (
  392. <Modal isOpen={true} className="modal-dialog-centered">
  393. <ModalHeader style={{ margin: 'auto' }}>Connect a Dataset</ModalHeader>
  394. <ModalBody>
  395. <Form>
  396. <FormGroup>
  397. <Label >Dataset Name:</Label>
  398. {this.state.datasetNameValid ? <Input type="text" name="datasetName" id="datasetName" placeholder="Type dataset name here" onChange={this.datasetNameChange} value={this.state.datasetName} disabled={this.state.datasetNameDisabled}/> : <Input invalid type="text" name="datasetName" id="datasetName" placeholder="Type dataset name here" onChange={this.datasetNameChange} value={this.state.datasetName} />}
  399. <FormFeedback invalid>Dataset Name is Mandatory!!!</FormFeedback>
  400. </FormGroup>
  401. <FormGroup tag="onespatial">
  402. <Label>Is it a 1Spatial dataset?</Label>
  403. <FormGroup check>
  404. <Label check>
  405. <Input type="radio" name="is1Spatial" value="true" checked={this.state.is1Spatial} onChange={this.is1SpatialChange} />{' '}
  406. Yes
  407. </Label>
  408. </FormGroup>
  409. <FormGroup check>
  410. <Label check>
  411. <Input type="radio" name="is1Spatial" value="false" checked={!this.state.is1Spatial} onChange={this.is1SpatialChange} />{' '}
  412. No
  413. </Label>
  414. </FormGroup>
  415. </FormGroup>
  416. <FormGroup tag="fieldset">
  417. <Label>Is it a SPARQL end-point?</Label>
  418. <FormGroup check>
  419. <Label check>
  420. <Input type="radio" name="sparqlendpoint" value="true" checked={this.state.isSparqlEndPoint} onChange={this.isSparqlEndPointChange} />{' '}
  421. Yes
  422. </Label>
  423. </FormGroup>
  424. <FormGroup check>
  425. <Label check>
  426. <Input type="radio" name="sparqlendpoint" value="false" checked={!this.state.isSparqlEndPoint} onChange={this.isSparqlEndPointChange} />{' '}
  427. No
  428. </Label>
  429. </FormGroup>
  430. </FormGroup>
  431. <FormGroup>
  432. <Label >SPARQL End-Point:</Label>
  433. {this.state.sparqlEndPointURLValid ? <Input type="text" name="sparqlEndPoint" id="sparqlEndPoint" placeholder="Type SPARQL URL here" disabled={!this.state.isSparqlEndPoint} onChange={this.sparqlEndPointURLChange} value={this.state.sparqlEndPointURL} /> : <Input invalid type="text" name="sparqlEndPoint" id="sparqlEndPoint" placeholder="Type SPARQL URL here" disabled={!this.state.isSparqlEndPoint} onChange={this.sparqlEndPointURLChange} value={this.state.sparqlEndPointURL} />}
  434. {this.state.isSparqlEndPoint ? <FormFeedback invalid>SPARQL End-Point is Mandatory!!!</FormFeedback> : null}
  435. </FormGroup>
  436. <FormGroup>
  437. <Label >Dataset PLD:</Label>
  438. {this.state.datasetPLDValid ? <Input type="text" name="datasetPLD" id="datasetPLD" placeholder="Type Dataset PLD here" onChange={this.datasetPLDChange} value={this.state.datasetPLD} disabled={this.state.datasetPLDDisabled}/> : <Input invalid type="text" name="datasetPLD" id="datasetPLD" placeholder="Type Dataset PLD here" onChange={this.datasetPLDChange} value={this.state.datasetPLD} />}
  439. {this.state.datasetPLDMandatoryValid ? null : <FormFeedback invalid>Dataset PLD is Mandatory!!!</FormFeedback>}
  440. {this.state.datasetPLDPrefixValid ? null : <FormFeedback invalid>Dataset PLD should be prefixed with either 'uri:' or 'http://' or 'https://'</FormFeedback>}
  441. </FormGroup>
  442. <FormGroup>
  443. <Label >Upload Assessment File:</Label>
  444. {this.state.displayUseExistingFileCheckBox & !this.state.isSparqlEndPoint ? <div><FormGroup check><Label check><Input id="useexistingfile" type="checkbox" name="useexistingfile" checked={this.state.useExistingFile} onClick={this.useExistingFileChange} /> Use existing dump file</Label></FormGroup><br/></div>: null}
  445. {this.state.fileUploadValid ? <Input id="uploadFileSelector" type="file" disabled={this.state.isSparqlEndPoint | this.state.useExistingFile} onChange={this.fileSelectionOnChange} /> : <Input invalid id="uploadFileSelector" type="file" disabled={this.state.isSparqlEndPoint | this.state.useExistingFile} onChange={this.fileSelectionOnChange} />}
  446. {this.state.fileUploadMandatory ? null : <FormFeedback invalid>Please select a file!!!</FormFeedback>}
  447. {this.state.fileFormatValid ? null : <FormFeedback invalid>Only following file formats are allowed.<div>({formats})</div></FormFeedback>}
  448. </FormGroup>
  449. <FormGroup>
  450. {this.state.showFileUploadProgress ? <Progress value={this.state.fileUploadProgress}><small class="justify-content-center d-flex position-absolute w-100"><font size="2" color="black">{this.state.fileUploadProgress}%</font></small></Progress> : null}
  451. {this.state.fileUploadCompleted && !this.state.fileUploadSuccessfull ? <div className="text-danger">File Upload Failed. Server did not respond!!!</div> : null}
  452. {this.state.fileUploadCompleted && this.state.fileUploadSuccessfull ? <div className="text-success">File Upload Successfull.</div> : null}
  453. </FormGroup>
  454. </Form>
  455. </ModalBody>
  456. <ModalFooter>
  457. <Button color="secondary" onClick={this.props.onClose}>Discard</Button>
  458. <Button color="primary" onClick={this.triggerSubmitValidation}>Next</Button>{' '}
  459. </ModalFooter>
  460. {this.state.fileToolarge?<FileTooLarge clickHandler={this.closeFileLargeError}/>:null}
  461. </Modal>
  462. );
  463. }
  464. }
  465. export default DatasetConnectConfigModelWindow;