implementing CSV will be fun they said
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
19a67b29df
commit
e9e2256e82
|
@ -3,8 +3,11 @@ package persistence.csv;
|
||||||
public interface CSVSerializable {
|
public interface CSVSerializable {
|
||||||
|
|
||||||
static String seperator = ",";
|
static String seperator = ",";
|
||||||
|
static String listSeperator = "-";
|
||||||
|
static String memberSeperator = "#";
|
||||||
|
|
||||||
String[] getHeaders();
|
String[] getHeaders();
|
||||||
|
|
||||||
String toCSVString();
|
String toCSVString();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package persistence.errors;
|
||||||
|
|
||||||
|
public class PersistenceError extends RuntimeException {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package persistence.link;
|
||||||
|
|
||||||
|
import abstraction.PersistenceAdapter;
|
||||||
|
import link.Link;
|
||||||
|
import persistence.GenericDAO;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class CSVLinkPersistenceAdapter implements PersistenceAdapter<Link> {
|
||||||
|
|
||||||
|
private final GenericDAO<LinkEntity> linkDAO;
|
||||||
|
|
||||||
|
public CSVLinkPersistenceAdapter(GenericDAO<LinkEntity> linkDAO) {
|
||||||
|
this.linkDAO = linkDAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Link> getAll() {
|
||||||
|
return linkDAO.getALl().stream().map(LinkEntity::toLink).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(Link value) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(Link value) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
103
1-Adapter/src/main/java/persistence/link/LinkEntity.java
Normal file
103
1-Adapter/src/main/java/persistence/link/LinkEntity.java
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
package persistence.link;
|
||||||
|
|
||||||
|
import category.CategoryId;
|
||||||
|
import link.Link;
|
||||||
|
import link.LinkId;
|
||||||
|
import link.LinkUrl;
|
||||||
|
import persistence.csv.CSVSerializable;
|
||||||
|
import persistence.tag.TagEntity;
|
||||||
|
import user.Username;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class LinkEntity implements CSVSerializable {
|
||||||
|
|
||||||
|
private final Integer id;
|
||||||
|
private final String creator;
|
||||||
|
private final String url;
|
||||||
|
private final Set<Integer> categoryIds;
|
||||||
|
private final Set<TagEntity> tags;
|
||||||
|
|
||||||
|
public LinkEntity(Integer id, String creator, String url, Set<Integer> categoryIds, Set<TagEntity> tags) {
|
||||||
|
this.id = id;
|
||||||
|
this.creator = creator;
|
||||||
|
this.url = url;
|
||||||
|
this.categoryIds = categoryIds;
|
||||||
|
this.tags = tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LinkEntity(String[] fields) {
|
||||||
|
this.id = Integer.parseInt(fields[0]);
|
||||||
|
this.creator = fields[1];
|
||||||
|
this.url = fields[2];
|
||||||
|
this.categoryIds =
|
||||||
|
(Arrays.stream(fields[3].split(CSVSerializable.listSeperator)).map(Integer::parseInt)).collect(
|
||||||
|
Collectors.toSet());
|
||||||
|
|
||||||
|
this.tags = (Arrays
|
||||||
|
.stream(fields[4].split(CSVSerializable.listSeperator))
|
||||||
|
.map(TagEntity::new)).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public LinkEntity(Link link) {
|
||||||
|
this.id = link.getId().id();
|
||||||
|
this.creator = link.getCreator().username();
|
||||||
|
this.url = link.getUrl().toString();
|
||||||
|
this.categoryIds = link.getCategoryIds().stream().map(CategoryId::id).collect(Collectors.toSet());
|
||||||
|
this.tags = link.getTags().stream().map(TagEntity::new).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Link toLink() {
|
||||||
|
return new Link(new LinkId(id),
|
||||||
|
new Username(creator),
|
||||||
|
new LinkUrl(url),
|
||||||
|
categoryIds.stream().map(CategoryId::new).collect(Collectors.toSet()),
|
||||||
|
tags.stream().map(TagEntity::toTag).collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] getHeaders() {
|
||||||
|
return new String[]{"id", "creator", "url", "categoryIds", "tagIds"};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toCSVString() {
|
||||||
|
return Integer.toString(id) + CSVSerializable.seperator + creator + CSVSerializable.seperator + url + CSVSerializable.seperator + String.join(
|
||||||
|
CSVSerializable.listSeperator,
|
||||||
|
categoryIds
|
||||||
|
.stream()
|
||||||
|
.map(Object::toString)
|
||||||
|
.collect(Collectors.toSet())) + CSVSerializable.seperator + String.join(CSVSerializable.listSeperator,
|
||||||
|
tags
|
||||||
|
.stream()
|
||||||
|
.map(TagEntity::toCSVString)
|
||||||
|
.collect(
|
||||||
|
Collectors.toSet()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
LinkEntity that = (LinkEntity) o;
|
||||||
|
|
||||||
|
if (id != null ? !id.equals(that.id) : that.id != null) return false;
|
||||||
|
if (creator != null ? !creator.equals(that.creator) : that.creator != null) return false;
|
||||||
|
if (url != null ? !url.equals(that.url) : that.url != null) return false;
|
||||||
|
if (categoryIds != null ? !categoryIds.equals(that.categoryIds) : that.categoryIds != null) return false;
|
||||||
|
return tags != null ? tags.equals(that.tags) : that.tags == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = id != null ? id.hashCode() : 0;
|
||||||
|
result = 31 * result + (creator != null ? creator.hashCode() : 0);
|
||||||
|
result = 31 * result + (url != null ? url.hashCode() : 0);
|
||||||
|
result = 31 * result + (categoryIds != null ? categoryIds.hashCode() : 0);
|
||||||
|
result = 31 * result + (tags != null ? tags.hashCode() : 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
73
1-Adapter/src/main/java/persistence/tag/TagEntity.java
Normal file
73
1-Adapter/src/main/java/persistence/tag/TagEntity.java
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package persistence.tag;
|
||||||
|
|
||||||
|
import persistence.csv.CSVSerializable;
|
||||||
|
import persistence.errors.PersistenceError;
|
||||||
|
import tag.Tag;
|
||||||
|
import tag.TagName;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class TagEntity {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
private Optional<String> additionalData = Optional.empty();
|
||||||
|
|
||||||
|
public TagEntity(String name, Optional<String> additionalData) {
|
||||||
|
this.name = name;
|
||||||
|
this.additionalData = additionalData;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public TagEntity(String name, String additionalData) {
|
||||||
|
this.name = name;
|
||||||
|
this.additionalData = Optional.of(additionalData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TagEntity(String csvString) {
|
||||||
|
var splittedString = csvString.split(CSVSerializable.memberSeperator);
|
||||||
|
|
||||||
|
if (splittedString.length == 0 || splittedString.length > 2) {
|
||||||
|
throw new PersistenceError();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.name = splittedString[0];
|
||||||
|
|
||||||
|
if (splittedString.length == 2) {
|
||||||
|
this.additionalData = Optional.of(splittedString[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TagEntity(Tag tag) {
|
||||||
|
this.name = tag.getName().name();
|
||||||
|
this.additionalData = tag.getAdditionalData();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toCSVString() {
|
||||||
|
return name + CSVSerializable.memberSeperator + additionalData.orElse("");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tag toTag(){
|
||||||
|
return new Tag(new TagName(name),additionalData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
TagEntity tagEntity = (TagEntity) o;
|
||||||
|
|
||||||
|
if (name != null ? !name.equals(tagEntity.name) : tagEntity.name != null) return false;
|
||||||
|
return additionalData != null
|
||||||
|
? additionalData.equals(tagEntity.additionalData)
|
||||||
|
: tagEntity.additionalData == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = name != null ? name.hashCode() : 0;
|
||||||
|
result = 31 * result + (additionalData != null ? additionalData.hashCode() : 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
45
1-Adapter/src/test/java/persistence/link/LinkEntityTest.java
Normal file
45
1-Adapter/src/test/java/persistence/link/LinkEntityTest.java
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
package persistence.link;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import persistence.csv.CSVSerializable;
|
||||||
|
import persistence.tag.TagEntity;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class LinkEntityTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toCSVString() {
|
||||||
|
|
||||||
|
var expected =
|
||||||
|
"21" + CSVSerializable.seperator + "hans" + CSVSerializable.seperator + "https://tea.filefighter.de" +
|
||||||
|
CSVSerializable.seperator + "1" + CSVSerializable.listSeperator + "2" +
|
||||||
|
CSVSerializable.listSeperator + "3" + CSVSerializable.seperator + "name" + CSVSerializable.memberSeperator + "hallo";
|
||||||
|
|
||||||
|
TagEntity tagEntity = new TagEntity("name", "hallo");
|
||||||
|
Set<Integer> categoryIds = Set.of(1, 2, 3);
|
||||||
|
Set<TagEntity> tags = Set.of(tagEntity);
|
||||||
|
var actual = new LinkEntity(21, "hans", "https://tea.filefighter.de", categoryIds, tags).toCSVString();
|
||||||
|
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fromCSVString() {
|
||||||
|
|
||||||
|
var input =
|
||||||
|
"21" + CSVSerializable.seperator + "hans" + CSVSerializable.seperator + "https://tea.filefighter.de" +
|
||||||
|
CSVSerializable.seperator + "1" + CSVSerializable.listSeperator + "2" +
|
||||||
|
CSVSerializable.listSeperator + "3" + CSVSerializable.seperator + "name" + CSVSerializable.memberSeperator + "hallo";
|
||||||
|
|
||||||
|
TagEntity tagEntity = new TagEntity("name", "hallo");
|
||||||
|
Set<Integer> categoryIds = Set.of(1, 2, 3);
|
||||||
|
Set<TagEntity> tags = Set.of(tagEntity);
|
||||||
|
var expected = new LinkEntity(21, "hans", "https://tea.filefighter.de", categoryIds, tags);
|
||||||
|
|
||||||
|
assertEquals(expected, new LinkEntity(input.split(CSVSerializable.seperator)));
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,12 +11,25 @@ public class Tag {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Tag(TagName name, String additionalData) {
|
||||||
|
this.name = name;
|
||||||
|
this.additionalData = Optional.ofNullable(additionalData);
|
||||||
|
}
|
||||||
|
|
||||||
public Tag(TagName name, Optional<String> additionalData) {
|
public Tag(TagName name, Optional<String> additionalData) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.additionalData = additionalData;
|
this.additionalData = additionalData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TagName getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<String> getAdditionalData() {
|
||||||
|
return additionalData;
|
||||||
|
}
|
||||||
|
|
||||||
public Tag addAdditionalData(String additionalData) {
|
public Tag addAdditionalData(String additionalData) {
|
||||||
return new Tag(this.name, Optional.of(additionalData));
|
return new Tag(this.name, additionalData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue