Skip to content
Snippets Groups Projects
Commit 018aeccc authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6508 get attachments and representations by vorgangId and/or eingangId

parent ab27b85c
No related branches found
No related tags found
No related merge requests found
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
package de.ozgcloud.vorgang.files; package de.ozgcloud.vorgang.files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -34,6 +33,7 @@ import org.springframework.data.mongodb.core.aggregation.AggregationOperation; ...@@ -34,6 +33,7 @@ import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import de.ozgcloud.vorgang.common.db.CriteriaUtil;
import de.ozgcloud.vorgang.vorgang.Vorgang; import de.ozgcloud.vorgang.vorgang.Vorgang;
@Repository @Repository
...@@ -42,36 +42,37 @@ class EingangFilesRepository { ...@@ -42,36 +42,37 @@ class EingangFilesRepository {
@Autowired @Autowired
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
private static final String DB_FIELDNAME_EINGANG_ID = "eingangs._id";
private static final String DB_FIELDNAME_EINGANGS = "eingangs"; private static final String DB_FIELDNAME_EINGANGS = "eingangs";
private static final String DB_FIELDNAME_ATTACHMENTS = "attachments"; private static final String DB_FIELDNAME_EINGANG_ID = DB_FIELDNAME_EINGANGS + "._id";
private static final String DB_FIELDNAME_FILES = "files"; private static final String DB_FIELDNAME_FILES = "files";
private static final String FIELD_ATTACHMENT_FILES = DB_FIELDNAME_EINGANGS + "." + DB_FIELDNAME_ATTACHMENTS + "." + DB_FIELDNAME_FILES; private static final String FIELD_ATTACHMENT_FILES = DB_FIELDNAME_EINGANGS + ".attachments." + DB_FIELDNAME_FILES;
private static final String FIELD_REPRESENTATION_FILES = DB_FIELDNAME_EINGANGS + ".representations";
private static final String DB_FIELDNAME_REPRESENTATIONS = "representations";
private static final String FIELD_REPRESENTATION_FILES = DB_FIELDNAME_EINGANGS + "." + DB_FIELDNAME_REPRESENTATIONS;
public List<OzgFile> findAttachmentsByEingangId(String eingangId) { public List<OzgFile> findAttachmentsByEingangId(String eingangId) {
return findAttachments(Criteria.where(DB_FIELDNAME_EINGANG_ID).is(eingangId));
List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.match(new Criteria(DB_FIELDNAME_EINGANG_ID).is(eingangId)));
operations.addAll(buildFindAttachmentsAggregation());
return mongoTemplate.aggregate(Aggregation.newAggregation(operations), Vorgang.COLLECTION_NAME, OzgFile.class).getMappedResults();
} }
public List<OzgFile> findAttachmentsByVorgangId(String vorgangId) { public List<OzgFile> findAttachmentsByVorgangId(String vorgangId) {
return Collections.emptyList(); return findAttachments(CriteriaUtil.isId(vorgangId));
} }
public List<OzgFile> findAttachmentsByVorgangIdAndEingangId(final String vorgangId, final String eingangId) { public List<OzgFile> findAttachmentsByVorgangIdAndEingangId(final String vorgangId, final String eingangId) {
return Collections.emptyList(); return findAttachments(new Criteria().andOperator(CriteriaUtil.isId(vorgangId), Criteria.where(DB_FIELDNAME_EINGANG_ID).is(eingangId)));
} }
private List<AggregationOperation> buildFindAttachmentsAggregation() { private List<OzgFile> findAttachments(Criteria criteria) {
List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.match(criteria));
operations.addAll(buildExtractAttachmentsAggregation());
return mongoTemplate.aggregate(Aggregation.newAggregation(operations), Vorgang.COLLECTION_NAME, OzgFile.class).getMappedResults();
}
private List<AggregationOperation> buildExtractAttachmentsAggregation() {
return List.of( return List.of(
Aggregation.project().andExpression(FIELD_ATTACHMENT_FILES).as(DB_FIELDNAME_FILES), Aggregation.project().andExpression(FIELD_ATTACHMENT_FILES).as(DB_FIELDNAME_FILES), //
// extract 'files' from deeply nested arrays within 'eingangs[].attachments[].files[]' and promote each file to a separate document
// at the root level.
Aggregation.unwind(DB_FIELDNAME_FILES), Aggregation.unwind(DB_FIELDNAME_FILES),
Aggregation.unwind(DB_FIELDNAME_FILES), Aggregation.unwind(DB_FIELDNAME_FILES),
Aggregation.unwind(DB_FIELDNAME_FILES), Aggregation.unwind(DB_FIELDNAME_FILES),
...@@ -80,27 +81,30 @@ class EingangFilesRepository { ...@@ -80,27 +81,30 @@ class EingangFilesRepository {
} }
List<OzgFile> findRepresentationsByEingangId(String eingangId) { List<OzgFile> findRepresentationsByEingangId(String eingangId) {
return findRepresentations(Criteria.where(DB_FIELDNAME_EINGANG_ID).is(eingangId));
List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.match(new Criteria(DB_FIELDNAME_EINGANG_ID).is(eingangId)));
operations.addAll(buildFindRepresentationsAggregation());
return mongoTemplate.aggregate(Aggregation.newAggregation(operations), Vorgang.COLLECTION_NAME, OzgFile.class).getMappedResults();
} }
public List<OzgFile> findRepresentationsByVorgangId(String vorgangId) { public List<OzgFile> findRepresentationsByVorgangId(String vorgangId) {
return Collections.emptyList(); return findRepresentations(CriteriaUtil.isId(vorgangId));
} }
public List<OzgFile> findRepresentationsByVorgangIdAndEingangId(String vorgangId, String eingangId) { public List<OzgFile> findRepresentationsByVorgangIdAndEingangId(String vorgangId, String eingangId) {
return Collections.emptyList(); return findRepresentations(new Criteria().andOperator(CriteriaUtil.isId(vorgangId), Criteria.where(DB_FIELDNAME_EINGANG_ID).is(eingangId)));
} }
private List<AggregationOperation> buildFindRepresentationsAggregation() { private List<OzgFile> findRepresentations(Criteria criteria) {
List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.match(criteria));
operations.addAll(buildFindRepresentationsAggregation());
return mongoTemplate.aggregate(Aggregation.newAggregation(operations), Vorgang.COLLECTION_NAME, OzgFile.class).getMappedResults();
}
private List<AggregationOperation> buildFindRepresentationsAggregation() {
return List.of( return List.of(
Aggregation.project().andExpression(FIELD_REPRESENTATION_FILES).as(DB_FIELDNAME_FILES), Aggregation.project().andExpression(FIELD_REPRESENTATION_FILES).as(DB_FIELDNAME_FILES),
// extract 'files' from deeply nested arrays within 'eingangs[].representations[]' and promote each file to a separate document at
// the root level.
Aggregation.unwind(DB_FIELDNAME_FILES), Aggregation.unwind(DB_FIELDNAME_FILES),
Aggregation.unwind(DB_FIELDNAME_FILES), Aggregation.unwind(DB_FIELDNAME_FILES),
Aggregation.replaceRoot(DB_FIELDNAME_FILES)); Aggregation.replaceRoot(DB_FIELDNAME_FILES));
......
...@@ -26,6 +26,7 @@ package de.ozgcloud.vorgang.files; ...@@ -26,6 +26,7 @@ package de.ozgcloud.vorgang.files;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import java.util.List; import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
...@@ -61,9 +62,12 @@ class EingangFilesRepositoryITCase { ...@@ -61,9 +62,12 @@ class EingangFilesRepositoryITCase {
mongoOperations.save(createVorgang()); mongoOperations.save(createVorgang());
} }
@Nested
class TestByEingangId {
@Test @Test
void shouldHaveTwoAttachments() { void shouldHaveTwoAttachments() {
List<OzgFile> attachments = repository.findAttachmentsByEingangId(EingangTestFactory.ID); var attachments = repository.findAttachmentsByEingangId(EingangTestFactory.ID);
assertThat(attachments).hasSize(2); assertThat(attachments).hasSize(2);
} }
...@@ -85,6 +89,75 @@ class EingangFilesRepositoryITCase { ...@@ -85,6 +89,75 @@ class EingangFilesRepositoryITCase {
} }
} }
@Nested
class TestByVorgangId {
@Test
void shouldFindWhenOneEingang() {
var vorgang = mongoOperations.save(VorgangTestFactory.createBuilder().id(null).version(0L).build());
var result = repository.findAttachmentsByVorgangId(vorgang.getId());
assertThat(result).hasSize(1);
}
@Test
void shouldFindWhenManyEingangs() {
var vorang = mongoOperations.save(
VorgangTestFactory.createBuilder().id(null).version(0L).clearEingangs()
.eingang(EingangTestFactory.createBuilder().id(UUID.randomUUID().toString()).clearAttachments()
.attachment(IncomingFileGroupTestFactory.createBuilder().clearFiles().files(createTestFiles()).build())
.build())
.eingang(EingangTestFactory.createBuilder().id(UUID.randomUUID().toString()).clearAttachments()
.attachment(IncomingFileGroupTestFactory.create())
.attachment(IncomingFileGroupTestFactory.create())
.build())
.build());
var result = repository.findAttachmentsByVorgangId(vorang.getId());
assertThat(result).hasSize(4);
}
@Test
void shouldReturnEmpty() {
var vorgang = mongoOperations.save(VorgangTestFactory.createBuilder().id(null).version(0L).clearEingangs()
.eingang(EingangTestFactory.createBuilder().clearAttachments().build()).build());
var result = repository.findAttachmentsByVorgangId(vorgang.getId());
assertThat(result).isEmpty();
}
}
@Nested
class TestByVorgangIdAndEingangId {
@Test
void shouldReturnEmpty() {
var vorgang = mongoOperations.save(VorgangTestFactory.createBuilder().id(null).version(0L).build());
var result = repository.findAttachmentsByVorgangIdAndEingangId(vorgang.getId(), "not-existing");
assertThat(result).isEmpty();
}
@Test
void shouldFindAttachments() {
var eingangId = UUID.randomUUID().toString();
var vorgang = mongoOperations.save(VorgangTestFactory.createBuilder().id(null).version(0L).clearEingangs()
.eingang(EingangTestFactory.createBuilder().id(eingangId)
.attachment(IncomingFileGroupTestFactory.createBuilder().clearFiles().files(createTestFiles()).build())
.build())
.build());
var result = repository.findAttachmentsByVorgangIdAndEingangId(vorgang.getId(), eingangId);
assertThat(result).hasSize(3);
}
}
}
@Nested @Nested
class TestFindRepresentations { class TestFindRepresentations {
...@@ -96,6 +169,9 @@ class EingangFilesRepositoryITCase { ...@@ -96,6 +169,9 @@ class EingangFilesRepositoryITCase {
mongoOperations.save(vorgangWithRepresentations); mongoOperations.save(vorgangWithRepresentations);
} }
@Nested
class TestByEingangId {
@Test @Test
void shouldHaveTwoRepresentations() { void shouldHaveTwoRepresentations() {
List<OzgFile> representations = repository.findRepresentationsByEingangId(EingangTestFactory.ID); List<OzgFile> representations = repository.findRepresentationsByEingangId(EingangTestFactory.ID);
...@@ -122,6 +198,72 @@ class EingangFilesRepositoryITCase { ...@@ -122,6 +198,72 @@ class EingangFilesRepositoryITCase {
} }
} }
@Nested
class TestByVorgangId {
@Test
void shouldFindWhenOneEingang() {
var vorgang = mongoOperations.save(VorgangTestFactory.createBuilder().id(null).version(0L).build());
var result = repository.findRepresentationsByVorgangId(vorgang.getId());
assertThat(result).hasSize(1);
}
@Test
void shouldFindWhenManyEingangs() {
var vorang = mongoOperations.save(
VorgangTestFactory.createBuilder().id(null).version(0L).clearEingangs()
.eingang(EingangTestFactory.createBuilder().id(UUID.randomUUID().toString()).clearRepresentations()
.representation(IncomingFileTestFactory.create())
.representation(IncomingFileTestFactory.create())
.build())
.eingang(EingangTestFactory.createBuilder().id(UUID.randomUUID().toString()).clearRepresentations()
.representation(IncomingFileTestFactory.create())
.build())
.build());
var result = repository.findRepresentationsByVorgangId(vorang.getId());
assertThat(result).hasSize(3);
}
@Test
void shouldReturnEmpty() {
var vorgang = mongoOperations.save(VorgangTestFactory.createBuilder().id(null).version(0L).clearEingangs()
.eingang(EingangTestFactory.createBuilder().clearRepresentations().build()).build());
var result = repository.findRepresentationsByVorgangId(vorgang.getId());
assertThat(result).isEmpty();
}
}
@Nested
class TestByVorgangIdAndEingangId {
@Test
void shouldReturnEmpty() {
var vorgang = mongoOperations.save(VorgangTestFactory.createBuilder().id(null).version(0L).build());
var result = repository.findAttachmentsByVorgangIdAndEingangId(vorgang.getId(), "not-existing");
assertThat(result).isEmpty();
}
@Test
void shouldFindRepresentations() {
var eingangId = UUID.randomUUID().toString();
var vorgang = mongoOperations.save(VorgangTestFactory.createBuilder().id(null).version(0L).clearEingangs()
.eingang(EingangTestFactory.createBuilder().id(eingangId).representation(IncomingFileTestFactory.create()).build()).build());
var result = repository.findRepresentationsByVorgangIdAndEingangId(vorgang.getId(), eingangId);
assertThat(result).hasSize(2);
}
}
}
private Vorgang createVorgang() { private Vorgang createVorgang() {
return VorgangTestFactory.createBuilder().clearEingangs() return VorgangTestFactory.createBuilder().clearEingangs()
.eingang(EingangTestFactory.createBuilder().clearAttachments().clearRepresentations() .eingang(EingangTestFactory.createBuilder().clearAttachments().clearRepresentations()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment