Skip to content

Commit

Permalink
refactor: media-reference management
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Jun 8, 2022
1 parent 6d6214e commit c97c550
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 45 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ Basic CRUD operations for repository entries

Publisher to handle publishing

#### MediaReferenceManager

Manage the references to the media-repository

#### EntityLockManager

Manage it exclusive write access.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.sitepark.ies.contentrepository.core.domain.entity;

public class MediaReference {

private final long mediaId;
final long usedBy;
private final MediaReferenceType type;

protected MediaReference(Builder builder) {
this.mediaId = builder.mediaId;
this.usedBy = builder.usedBy;
this.type = builder.type;
}

public long getMediaId() {
return this.mediaId;
}

public long getUsedBy() {
return this.usedBy;
}

public MediaReferenceType getType() {
return this.type;
}

public static Builder builder() {
return new Builder();
}

public Builder toBuilder() {
return new Builder(this);
}

public static class Builder {

private long mediaId;
private long usedBy;
private MediaReferenceType type;

protected Builder() {}

protected Builder(MediaReference media) {
this.mediaId = media.mediaId;
this.usedBy = media.usedBy;
this.type = media.type;
}

public Builder mediaId(long mediaId) {
assert mediaId > 0;
this.mediaId = mediaId;
return this;
}

public Builder usedBy(long usedBy) {
assert usedBy > 0;
this.usedBy = usedBy;
return this;
}
public Builder type(MediaReferenceType type) {
assert type != null;
this.type = type;
return this;
}
public MediaReference build() {
return new MediaReference(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.sitepark.ies.contentrepository.core.domain.entity;

public enum MediaReferenceType {
EMBEDDED
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sitepark.ies.contentrepository.core.port;

import java.util.List;
import java.util.Optional;

import com.sitepark.ies.contentrepository.core.domain.entity.Anchor;
Expand All @@ -16,5 +15,4 @@ public interface ContentRepository {
void removeGroup(long id);
Optional<Identifier> resolveAnchor(Anchor anchor);
long resolve(Identifier identifier);
List<Long> getAllMediaReferences(long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sitepark.ies.contentrepository.core.port;

import com.sitepark.ies.contentrepository.core.domain.entity.MediaReference;

public interface MediaReferenceManager {
void removeByReference(long usedBy);
void addReference(MediaReference reference);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.sitepark.ies.contentrepository.core.usecase;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.inject.Inject;
Expand All @@ -13,7 +11,7 @@
import com.sitepark.ies.contentrepository.core.port.ContentRepository;
import com.sitepark.ies.contentrepository.core.port.EntityLockManager;
import com.sitepark.ies.contentrepository.core.port.HistoryManager;
import com.sitepark.ies.contentrepository.core.port.MediaRepository;
import com.sitepark.ies.contentrepository.core.port.MediaReferenceManager;
import com.sitepark.ies.contentrepository.core.port.Publisher;
import com.sitepark.ies.contentrepository.core.port.RecycleBin;
import com.sitepark.ies.contentrepository.core.port.SearchIndex;
Expand All @@ -28,13 +26,14 @@ public final class PurgeEntity {
private final AccessControl accessControl;
private final RecycleBin recycleBin;
private final SearchIndex searchIndex;
private final MediaRepository mediaRepository;
private final MediaReferenceManager mediaReferenceManager;
private final Publisher publisher;

@Inject
protected PurgeEntity(ContentRepository repository, EntityLockManager lockManager,
VersioningManager versioningManager, HistoryManager historyManager, AccessControl accessControl,
RecycleBin recycleBin, SearchIndex searchIndex, MediaRepository mediaRepository, Publisher publisher) {
RecycleBin recycleBin, SearchIndex searchIndex, MediaReferenceManager mediaReferenceManager,
Publisher publisher) {

this.repository = repository;
this.lockManager = lockManager;
Expand All @@ -43,7 +42,7 @@ protected PurgeEntity(ContentRepository repository, EntityLockManager lockManage
this.accessControl = accessControl;
this.recycleBin = recycleBin;
this.searchIndex = searchIndex;
this.mediaRepository = mediaRepository;
this.mediaReferenceManager = mediaReferenceManager;
this.publisher = publisher;
}

Expand All @@ -62,20 +61,15 @@ public void purgeEntity(long id) {

this.publisher.depublish(id);

List<Long> mediaRefs = this.repository.getAllMediaReferences(id);
this.mediaReferenceManager.removeByReference(id);

this.repository.removeEntity(id);

this.historyManager.purge(id);

List<Long> mediaRefsFromOtherVersions = this.versioningManager.getAllMediaReferences(id);
this.versioningManager.removeAllVersions(id);

this.recycleBin.removeByObject(id);

List<Long> allMediaRefs = new ArrayList<>();
allMediaRefs.addAll(mediaRefs);
allMediaRefs.addAll(mediaRefsFromOtherVersions);

this.mediaRepository.remove(allMediaRefs);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.sitepark.ies.contentrepository.core.usecase;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.inject.Inject;
Expand All @@ -14,7 +12,7 @@
import com.sitepark.ies.contentrepository.core.port.ContentRepository;
import com.sitepark.ies.contentrepository.core.port.EntityLockManager;
import com.sitepark.ies.contentrepository.core.port.HistoryManager;
import com.sitepark.ies.contentrepository.core.port.MediaRepository;
import com.sitepark.ies.contentrepository.core.port.MediaReferenceManager;
import com.sitepark.ies.contentrepository.core.port.Publisher;
import com.sitepark.ies.contentrepository.core.port.RecycleBin;
import com.sitepark.ies.contentrepository.core.port.SearchIndex;
Expand All @@ -29,13 +27,14 @@ public final class PurgeGroup {
private final AccessControl accessControl;
private final RecycleBin recycleBin;
private final SearchIndex searchIndex;
private final MediaRepository mediaRepository;
private final MediaReferenceManager mediaReferenceManager;
private final Publisher publisher;

@Inject
protected PurgeGroup(ContentRepository repository, EntityLockManager lockManager,
VersioningManager versioningManager, HistoryManager historyManager, AccessControl accessControl,
RecycleBin recycleBin, SearchIndex searchIndex, MediaRepository mediaRepository, Publisher publisher) {
RecycleBin recycleBin, SearchIndex searchIndex, MediaReferenceManager mediaReferenceManager,
Publisher publisher) {

this.repository = repository;
this.lockManager = lockManager;
Expand All @@ -44,7 +43,7 @@ protected PurgeGroup(ContentRepository repository, EntityLockManager lockManager
this.accessControl = accessControl;
this.recycleBin = recycleBin;
this.searchIndex = searchIndex;
this.mediaRepository = mediaRepository;
this.mediaReferenceManager = mediaReferenceManager;
this.publisher = publisher;
}

Expand All @@ -67,21 +66,14 @@ public void purgeGroup(long id) {

this.publisher.depublish(id);

List<Long> mediaRefs = this.repository.getAllMediaReferences(id);
this.mediaReferenceManager.removeByReference(id);

this.repository.removeGroup(id);

this.historyManager.purge(id);

List<Long> mediaRefsFromOtherVersions = this.versioningManager.getAllMediaReferences(id);
this.versioningManager.removeAllVersions(id);

this.recycleBin.removeByObject(id);

List<Long> allMediaRefs = new ArrayList<>();
allMediaRefs.addAll(mediaRefs);
allMediaRefs.addAll(mediaRefsFromOtherVersions);

this.mediaRepository.remove(allMediaRefs);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
Expand All @@ -20,7 +19,7 @@
import com.sitepark.ies.contentrepository.core.port.ContentRepository;
import com.sitepark.ies.contentrepository.core.port.EntityLockManager;
import com.sitepark.ies.contentrepository.core.port.HistoryManager;
import com.sitepark.ies.contentrepository.core.port.MediaRepository;
import com.sitepark.ies.contentrepository.core.port.MediaReferenceManager;
import com.sitepark.ies.contentrepository.core.port.Publisher;
import com.sitepark.ies.contentrepository.core.port.RecycleBin;
import com.sitepark.ies.contentrepository.core.port.SearchIndex;
Expand Down Expand Up @@ -93,7 +92,7 @@ void testPurge() {

RecycleBin recycleBin = mock(RecycleBin.class);
SearchIndex searchIndex = mock(SearchIndex.class);
MediaRepository mediaRepository = mock(MediaRepository.class);
MediaReferenceManager mediaReferenceManager = mock(MediaReferenceManager.class);
Publisher publisher = mock(Publisher.class);

PurgeEntity purgeEntity = new PurgeEntity(
Expand All @@ -104,26 +103,26 @@ void testPurge() {
accessControl,
recycleBin,
searchIndex,
mediaRepository,
mediaReferenceManager,
publisher);
purgeEntity.purgeEntity(10L);

InOrder inOrder = inOrder(
searchIndex,
publisher,
mediaReferenceManager,
repository,
historyManager,
versioningManager,
recycleBin,
mediaRepository
recycleBin
);

inOrder.verify(searchIndex).remove(10L);
inOrder.verify(publisher).depublish(10L);
inOrder.verify(mediaReferenceManager).removeByReference(10L);
inOrder.verify(repository).removeEntity(10L);
inOrder.verify(historyManager).purge(10L);
inOrder.verify(versioningManager).removeAllVersions(10L);
inOrder.verify(recycleBin).removeByObject(10L);
inOrder.verify(mediaRepository).remove(anyList());
}
}

0 comments on commit c97c550

Please sign in to comment.