Skip to content

Commit

Permalink
Merge pull request #194 from digipost/date_search
Browse files Browse the repository at this point in the history
Add search archive documents by date
  • Loading branch information
eivinhb authored Nov 14, 2024
2 parents 9057ece + dabea06 commit 79e2cd3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
22 changes: 22 additions & 0 deletions docs/_v16_x/4_archive.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArchiveDocument> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,7 +124,41 @@ public Optional<URI> getNextDocumentsWithAttributes(Map<String, String> 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<URI> getNextDocumentsWithAttributesByDate(Map<String, String> 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<URI> 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);
}
Expand Down Expand Up @@ -180,4 +216,8 @@ public Archive build() {
}
}

private static String base64(String param){
return Base64.getEncoder().encodeToString(param.getBytes(StandardCharsets.UTF_8));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 79e2cd3

Please sign in to comment.