Jeg har sloss noen dager med det jeg trodde var en enkel endring i Nikita, en endring av datamodelen for dokumentobjekt, slik at filstien til lagret fil ligger i databasen og ikke avledes algoritmisk hver gang. Bakgrunnen er et ønske om å endre på algoritmen samtidig som endringen håndterer oppgradering av Nikita mellom en instans som bruker gammel algoritme og ny algoritme.
Endringen ligger i gren documentobject_file_path_db i Nikitaprosjektet på gitlab, og feiler i dag ved opplasting av ny fil med følgende melding:
2025-02-01T06:03:02.177Z ERROR 13151 --- [Nikita Noark 5 Core (Demo mode using H2 database)] [0.1-8092-exec-8] a.w.s.e.GlobalETAGExceptionHandler : Cannot invoke "String.toString()" because "this.storagePath" is null
(hentet fra <URL: https://gitlab.com/OsloMet-ABI/nikita-noark5-core/-/jobs/9014548778 >)
Kan noen hjelpe meg med å forstå hvilen toString()-kall det er som møter en verdi som er null? Jeg har forsøkt å beskytte alle slike kall, og klarer ikke finne den som er problematisk.
Tips mottas med takk.
Endringen i greinen ser i dag slik ut:
diff --git a/scripts/run-runtest b/scripts/run-runtest index 04cfec652..b5ee49619 100755 --- a/scripts/run-runtest +++ b/scripts/run-runtest @@ -12,6 +12,7 @@ atexit() { sleep 1 if $nikitastarted ; then tail -100 $basedir/nikita-run.log + grep -C 200 -i exception $basedir/nikita-run.log fi fi } diff --git a/src/main/java/app/domain/noark5/DocumentObject.java b/src/main/java/app/domain/noark5/DocumentObject.java index 05016c4ba..6e7c9f16c 100644 --- a/src/main/java/app/domain/noark5/DocumentObject.java +++ b/src/main/java/app/domain/noark5/DocumentObject.java @@ -18,6 +18,8 @@ import jakarta.validation.constraints.NotNull; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder;
+import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List;
@@ -136,6 +138,11 @@ public class DocumentObject @OneToOne(mappedBy = REFERENCE_DOCUMENT_OBJECT_DB, fetch = LAZY, cascade = ALL) private ElectronicSignature referenceElectronicSignature;
+ // Location in storage for the file + @Column(name = "storage_path", nullable = true) + private String storagePath; + + public Integer getVersionNumber() { return versionNumber; } @@ -286,6 +293,17 @@ public class DocumentObject this.referenceElectronicSignature = referenceElectronicSignature; }
+ public Path getStoragePath() { + if (null != storagePath) + return Paths.get(storagePath); + return null; + } + + public void setStoragePath(Path storagepath) { + if (null != storagepath) + this.storagePath = storagePath.toString(); + } + @Override public String toString() { return "DocumentObject{" + super.toString() + diff --git a/src/main/java/app/service/noark5/DocumentObjectService.java b/src/main/java/app/service/noark5/DocumentObjectService.java index 98f33a51f..40bc39c33 100644 --- a/src/main/java/app/service/noark5/DocumentObjectService.java +++ b/src/main/java/app/service/noark5/DocumentObjectService.java @@ -235,16 +235,19 @@ public class DocumentObjectService public Resource loadAsResource(@NotNull final UUID systemId) { DocumentObject documentObject = getDocumentObjectOrThrow(systemId); + Path file = documentObject.getStoragePath(); + if (null == file) { + throw new StorageFileNotFoundException( + "No file connected to " + systemId); + } HttpServletRequest request = getRequest(); HttpServletResponse response = getResponse(); // First make sure the file exist try { - Path file = getToFile(documentObject); Resource resource = new UrlResource(file.toUri()); if (!resource.exists() && !resource.isReadable()) { throw new StorageFileNotFoundException( - "Could not read file: " + - documentObject.getReferenceDocumentFile()); + "Could not read file " + file + " connected to " + systemId); } // When file exist, figure out how to return it. Note // both format, file name, file size and mime type can be @@ -278,8 +281,7 @@ public class DocumentObjectService return resource; } catch (MalformedURLException e) { throw new StorageFileNotFoundException( - "Could not read file: " + - documentObject.getReferenceDocumentFile()); + "Could not read file " + file + " connected to " + systemId); } }
@@ -834,7 +836,7 @@ public class DocumentObjectService private void storeAndCalculateChecksum(InputStream inputStream, DocumentObject documentObject) {
- if (null != documentObject.getReferenceDocumentFile()) { + if (null != documentObject.getStoragePath()) { throw new StorageException( "There is already a file associated with " + documentObject); @@ -981,6 +983,7 @@ public class DocumentObjectService Path toFile = getToFile(documentObject);
Files.move(incoming, toFile); + documentObject.setStoragePath(toFile); }
private void setGeneratedDocumentFilename(DocumentObject documentObject) {