Skip to content
Snippets Groups Projects
Commit ca46a99b authored by dre0059's avatar dre0059
Browse files

Chybové hlášky aktualizované

parent 2e3954c5
Branches
No related merge requests found
No preview for this file type
......@@ -7,9 +7,11 @@ import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import reactor.core.publisher.Mono;
import java.io.File;
import java.net.ConnectException;
@Service
......@@ -25,18 +27,30 @@ public class GrobidClient {
// get METADATA of the file
public String processHeader(File pdfFile){ // Mono - vráti jeden string, výsledok je JSON
return webClient.post()
.uri("/api/processHeaderDocument")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData("input", new FileSystemResource(pdfFile)))
.attribute("consolidateHeader", 1) // Možnosť na zjednotenie hlavičky
.attribute("includeRawAffiliations", 1) // Prípadne pridať ďalšie parametre, ak Grobid podporuje takéto rozšírenie
.attribute("includeRawCopyrights", 1) // Prípadne pridať ďalšie parametre, ak Grobid podporuje takéto rozšírenie
//.attribute("includeReferences", 1) // Možnosť pridať aj referencie priamo do hlavičky
.retrieve()
.bodyToMono(String.class)
.block(); // returns String instead of Mono<String>
}
try {
return webClient.post()
.uri("/api/processHeaderDocument")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData("input", new FileSystemResource(pdfFile)))
.attribute("consolidateHeader", 1) // Možnosť na zjednotenie hlavičky
.attribute("includeRawAffiliations", 1) // Prípadne pridať ďalšie parametre, ak Grobid podporuje takéto rozšírenie
.attribute("includeRawCopyrights", 1) // Prípadne pridať ďalšie parametre, ak Grobid podporuje takéto rozšírenie
//.attribute("includeReferences", 1) // Možnosť pridať aj referencie priamo do hlavičky
.retrieve()
.onStatus(status -> status.isError(), clientResponse ->
clientResponse.bodyToMono(String.class)
.defaultIfEmpty("Unknown error from GROBID server - failed to process HEADER")
.flatMap(errorBody -> Mono.error(new RuntimeException("GROBID server error: " + errorBody)))
)
.bodyToMono(String.class)
.block(); // returns String instead of Mono<String>
} catch (WebClientRequestException e) {
throw new RuntimeException("Failed to connect to GROBID server: " + e.getMessage(), e);
} catch (Exception e) {
throw new RuntimeException("Error communicating with GROBID server: " + e.getMessage(), e);
}
}
public String processFullMetadata(File pdfFile) {
return webClient.post()
......@@ -51,13 +65,24 @@ public class GrobidClient {
// spracuje REFERENCIE z PDF
public String processReferences(File pdfFile){
return webClient.post()
.uri("/api/processReferences")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData("input", new FileSystemResource(pdfFile)))
.retrieve()
.bodyToMono(String.class)
.block();
try {
return webClient.post()
.uri("/api/processReferences")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData("input", new FileSystemResource(pdfFile)))
.retrieve()
.onStatus(status -> status.isError(), clientResponse ->
clientResponse.bodyToMono(String.class)
.defaultIfEmpty("Unknown error from GROBID server - failed to process REFERENCES")
.flatMap(errorBody -> Mono.error(new RuntimeException("GROBID server error: " + errorBody)))
)
.bodyToMono(String.class)
.block();
} catch (WebClientRequestException e) {
throw new RuntimeException("Failed to connect to GROBID server: " + e.getMessage(), e);
} catch (Exception e) {
throw new RuntimeException("Error communicating with GROBID server: " + e.getMessage(), e);
}
}
}
\ No newline at end of file
......@@ -2,20 +2,17 @@ package com.dre0059.articleprocessor.controller;
import com.dre0059.articleprocessor.GrobidClient;
import com.dre0059.articleprocessor.model.Dokument;
import com.dre0059.articleprocessor.model.Tag;
import com.dre0059.articleprocessor.repository.TagRepository;
import com.dre0059.articleprocessor.service.CategoryService;
import com.dre0059.articleprocessor.service.HeaderService;
import com.dre0059.articleprocessor.service.ReferenceService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
......@@ -72,7 +69,16 @@ public class FileUploadController {
}
//System.out.println("File written to temporary location. ");
String header = grobidClient.processHeader(tmpFile);
String header;
try {
header = grobidClient.processHeader(tmpFile);
} catch (RuntimeException e) {
if (e.getMessage().contains("Failed to connect to GROBID server")) {
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(Map.of("error", "Cannot connect to GROBID server"));
}
return ResponseEntity.internalServerError().body(Map.of("error", "GROBID processing error"));
}
//System.out.println("GROBID Header processed: " + header);
String references = grobidClient.processReferences(tmpFile);
......@@ -81,10 +87,17 @@ public class FileUploadController {
Dokument savedDocument = headerService.processHeader(header, categoryId, tags, tmpFile);
//System.out.println("Header saved to database.");
// dokument already exists
if(savedDocument == null) {
if (tmpFile.exists()) tmpFile.delete();
return ResponseEntity.status(HttpStatus.CONFLICT)
.body(Map.of("error", "Document already exists in the database"));
}
referenceService.extractReferences(references);
//System.out.println("References extracted..");
tmpFile.delete();
if (tmpFile.exists()) tmpFile.delete();
Map<String, Object> response = new HashMap<>();
response.put("id", savedDocument.getId());
......
......@@ -148,7 +148,7 @@
cache: true
},
createTag: function (params) {
let term = $.trim(params.term.toLowerCase()); // 👈 vytváraj lowercase tagy
let term = $.trim(params.term.toLowerCase()); // lowercase tagy
if (term === '') {
return null;
......@@ -192,7 +192,7 @@
const formData = new FormData();
formData.append("file", fileInput);
formData.append("categoryId", category);
formData.append("tags",tags); // Dôležité!
formData.append("tags", tags); // Dôležité!
$.ajax({
url: "/api/upload",
......@@ -210,13 +210,24 @@
alert("Upload completed, but no ID returned.");
}
},
error: function() {
error: function(xhr, status, error) { // <-- tu teraz 3 argumenty!
clearInterval(dotInterval);
$('#processing-message').hide();
alert("Error processing PDF.");
let errorMessage = "Error while processing PDF.";
// Tu bezpečne kontrolujeme odpoveď
if (xhr.responseJSON && xhr.responseJSON.error) {
errorMessage = xhr.responseJSON.error;
} else if (xhr.responseText) {
errorMessage = xhr.responseText;
}
alert(errorMessage); // zobrazí chybu
}
});
});
</script>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment