From dabea06823e9eb6bbc98f014e60b7ee6581f8cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eivind=20Bergst=C3=B8l?= Date: Thu, 14 Nov 2024 09:49:05 +0100 Subject: [PATCH] Add search archive documents by date There are 4 search-options now: * get all * by attributes * by date * by date and attributes --- docs/_v16_x/4_archive.md | 22 ++++++++ .../representations/archive/Archive.java | 52 ++++++++++++++++--- .../archive/ArchiveDocument.java | 2 +- .../GithubPagesArchiveExamples.java | 23 ++++++++ 4 files changed, 92 insertions(+), 7 deletions(-) diff --git a/docs/_v16_x/4_archive.md b/docs/_v16_x/4_archive.md index e5aff201..5df831a8 100644 --- a/docs/_v16_x/4_archive.md +++ b/docs/_v16_x/4_archive.md @@ -133,6 +133,28 @@ while (current.getNextDocuments().isPresent()) { System.out.println(documents); ``` +You can now also select by date or by attributes by date. Date is when the documents has been stored in Digipost archive. + +```java +final Archives archives = client.getArchives(); + +Archive current = archives.getArchives().get(0); +final List documents = new ArrayList<>(); + +while (current.getNextDocuments().isPresent()) { + current = current.getNextDocumentsWithAttributesByDate(Map.of("INR", "123123"), OffsetDateTime.now().minus(Period.ofDays(4)), OffsetDateTime.now()) + //current = defaultArchive.getNextDocumentsByDate(OffsetDateTime.now().minus(Period.ofDays(4)), OffsetDateTime.now()); + .map(client::getArchiveDocuments) + .orElse(new Archive()); + + documents.addAll(current.getDocuments()); +} + +// This prints to total content of the list of documents +System.out.println(documents); +``` + + ## Get documents by referenceID You can retrieve a set of documents by a given referenceID. You will then get the documents listed in their respective diff --git a/src/main/java/no/digipost/api/client/representations/archive/Archive.java b/src/main/java/no/digipost/api/client/representations/archive/Archive.java index b3bbb6e7..506ae8f2 100644 --- a/src/main/java/no/digipost/api/client/representations/archive/Archive.java +++ b/src/main/java/no/digipost/api/client/representations/archive/Archive.java @@ -15,20 +15,22 @@ */ package no.digipost.api.client.representations.archive; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; import no.digipost.api.client.SenderId; import no.digipost.api.client.representations.Link; import no.digipost.api.client.representations.Representation; import no.digipost.api.client.representations.SenderOrganization; import org.apache.http.client.utils.URIBuilder; -import jakarta.xml.bind.annotation.XmlAccessType; -import jakarta.xml.bind.annotation.XmlAccessorType; -import jakarta.xml.bind.annotation.XmlElement; -import jakarta.xml.bind.annotation.XmlRootElement; -import jakarta.xml.bind.annotation.XmlType; import java.net.URI; import java.net.URISyntaxException; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Base64; import java.util.Collections; @@ -122,7 +124,41 @@ public Optional getNextDocumentsWithAttributes(Map attribut return Optional.ofNullable(getLinkByRelationName(NEXT_DOCUMENTS)).map(Link::getUri) .map(uri -> { try { - return new URIBuilder(uri).addParameter("attributes", Base64.getEncoder().encodeToString(attributesCommaSeparated.getBytes(StandardCharsets.UTF_8))).build(); + return new URIBuilder(uri) + .addParameter("attributes", Base64.getEncoder().encodeToString(attributesCommaSeparated.getBytes(StandardCharsets.UTF_8))) + .build(); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + }); + } + + public Optional getNextDocumentsWithAttributesByDate(Map attributes, OffsetDateTime from, OffsetDateTime to) { + final String attributesCommaSeparated = attributes.entrySet().stream().flatMap(en -> Stream.of(en.getKey(), en.getValue())).collect(Collectors.joining(",")); + + return Optional.ofNullable(getLinkByRelationName(NEXT_DOCUMENTS)).map(Link::getUri) + .map(uri -> { + try { + return new URIBuilder(uri) + .addParameter("attributes", base64(attributesCommaSeparated)) + .addParameter("fromDate", base64(from.toString())) + .addParameter("toDate", base64(to.toString())) + .build(); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + }); + } + + public Optional getNextDocumentsByDate(OffsetDateTime from, OffsetDateTime to) { + + return Optional.ofNullable(getLinkByRelationName(NEXT_DOCUMENTS)).map(Link::getUri) + .map(uri -> { + try { + return new URIBuilder(uri) + .addParameter("fromDate", base64(from.toString())) + .addParameter("toDate", base64(to.toString())) + .build(); } catch (URISyntaxException e) { throw new RuntimeException(e); } @@ -180,4 +216,8 @@ public Archive build() { } } + private static String base64(String param){ + return Base64.getEncoder().encodeToString(param.getBytes(StandardCharsets.UTF_8)); + } + } diff --git a/src/main/java/no/digipost/api/client/representations/archive/ArchiveDocument.java b/src/main/java/no/digipost/api/client/representations/archive/ArchiveDocument.java index 542fbcf6..38e1c9af 100644 --- a/src/main/java/no/digipost/api/client/representations/archive/ArchiveDocument.java +++ b/src/main/java/no/digipost/api/client/representations/archive/ArchiveDocument.java @@ -205,7 +205,7 @@ public String toString() { ", fileType='" + fileType + '\'' + ", referenceid='" + referenceid + '\'' + ", contentType='" + contentType + '\'' + - ", contentHash=" + contentHash.getHashAlgorithm() + ":" + contentHash.getHash() + + (contentHash == null ? "" : ", contentHash=" + contentHash.getHashAlgorithm() + ":" + contentHash.getHash()) + ", attributes=" + attributes + ", archivedTime=" + archivedTime + ", deletionTime=" + deletionTime + diff --git a/src/test/java/no/digipost/api/client/eksempelkode/GithubPagesArchiveExamples.java b/src/test/java/no/digipost/api/client/eksempelkode/GithubPagesArchiveExamples.java index 9f81cfb3..9a053195 100644 --- a/src/test/java/no/digipost/api/client/eksempelkode/GithubPagesArchiveExamples.java +++ b/src/test/java/no/digipost/api/client/eksempelkode/GithubPagesArchiveExamples.java @@ -30,11 +30,13 @@ import java.io.InputStream; import java.net.URI; import java.time.Clock; +import java.time.OffsetDateTime; import java.time.Period; import java.time.ZoneId; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.UUID; @SuppressWarnings("unused") @@ -65,6 +67,27 @@ public void get_documents_from_archive(){ .map(Archive::getDocuments) .orElse(Collections.emptyList()); } + + public void get_documents_by_attributes(){ + Archives archives = client.getArchives(SenderId.of(123456)); + Archive defaultArchive = archives.getArchives().get(0); + + defaultArchive.getNextDocumentsWithAttributes(Map.of("Key", "Value")); + } + + public void get_documents_by_date_with_attributes_by_date(){ + Archives archives = client.getArchives(SenderId.of(123456)); + Archive defaultArchive = archives.getArchives().get(0); + + defaultArchive.getNextDocumentsWithAttributesByDate(Map.of("Key", "Value"), OffsetDateTime.now().minus(Period.ofDays(4)), OffsetDateTime.now()); + } + + public void get_documents_by_date(){ + Archives archives = client.getArchives(SenderId.of(123456)); + Archive defaultArchive = archives.getArchives().get(0); + + defaultArchive.getNextDocumentsByDate(OffsetDateTime.now().minus(Period.ofDays(4)), OffsetDateTime.now()); + } public void example_of_iteration_of_all_documents(){ final Archives archives = client.getArchives(SenderId.of(123456));