Skip to content

Commit

Permalink
feat: add parentAnchor and parentAnchorList filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Sep 4, 2023
1 parent 864ce68 commit d2392ac
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.sitepark.ies.contentrepository.core.domain.entity;


import java.io.Serializable;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

import com.fasterxml.jackson.annotation.JsonValue;
import com.sitepark.ies.contentrepository.core.domain.exception.InvalidAnchor;
Expand All @@ -11,8 +12,14 @@ public final class Anchor implements Serializable {

private static final long serialVersionUID = 1L;

public static final String VALID_CHARS_REGEX = "[a-zA-Z0-9_.\\-]+";

private static final Pattern VALIDATOR_PATTERN = Pattern.compile(VALID_CHARS_REGEX);

private static final Pattern ONLY_NUMBERS_PATTERN = Pattern.compile("[0-9]+");

/**
* Wird verwendet, um Anchor beim speichern von Einträgen zurück zu setzen.
* Used to reset anchor when saving entries.
*/
public static final Anchor EMPTY = new Anchor("");

Expand All @@ -23,18 +30,18 @@ private Anchor(String name) {
this.name = name;
}

public static Optional<Anchor> ofString(String name) {
if (name == null) {
return Optional.empty();
} else {
public static Anchor ofString(String name) {

if (name.isBlank()) {
return Optional.of(EMPTY);
}
if (name == null) {
return null;
}

Anchor.validate(name);
return Optional.of(new Anchor(name));
if (name.isBlank()) {
return EMPTY;
}

Anchor.validate(name);
return new Anchor(name);
}

public String getName() {
Expand All @@ -45,12 +52,12 @@ public String getName() {
* @throws InvalidAnchor
*/
private static void validate(String name) {
if (name == null) {
return;

if (ONLY_NUMBERS_PATTERN.matcher(name).matches()) {
throw new InvalidAnchor(name, "Anchor must not only consist of numbers");
}

// TODO: implement validator
if (name.indexOf(' ') != -1) {
if (!VALIDATOR_PATTERN.matcher(name).matches()) {
throw new InvalidAnchor(name, "Anchor contains Spaces");
}
}
Expand All @@ -60,14 +67,16 @@ public int hashCode() {
return this.name != null ? this.name.hashCode() : 0;
}


@Override
public boolean equals(Object o) {

if (!(o instanceof Anchor)) {
return false;
}

Anchor anchor = (Anchor)o;
return Objects.equals(anchor.name, this.name);
return Objects.equals(this.name, anchor.name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public B id(long id) {

public B anchor(String anchor) {
assert anchor != null;
this.anchor = Anchor.ofString(anchor).get();
this.anchor = Anchor.ofString(anchor);
return this.self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public static ParentList parentList(Long... parentList) {
return new ParentList(parentList);
}

public static ParentAnchor parentAnchor(com.sitepark.ies.contentrepository.core.domain.entity.Anchor parentAnchor) {
return new ParentAnchor(parentAnchor);
}

public static ParentAnchorList parentAnchorList(
com.sitepark.ies.contentrepository.core.domain.entity.Anchor... parentAnchorList) {
return new ParentAnchorList(parentAnchorList);
}

public static Root root(Long root) {
return new Root(root);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sitepark.ies.contentrepository.core.domain.entity.filter;

import com.fasterxml.jackson.annotation.JsonProperty;

public class ParentAnchor implements Filter {

@SuppressWarnings("PMD.AvoidFieldNameMatchingTypeName") // so that when deserializing it has the desired format
private final com.sitepark.ies.contentrepository.core.domain.entity.Anchor parentAnchor;

protected ParentAnchor(@JsonProperty("parentAnchor")
com.sitepark.ies.contentrepository.core.domain.entity.Anchor parentAnchor) {
assert parentAnchor != null : "parentAnchor is null";
this.parentAnchor = parentAnchor;
}

public com.sitepark.ies.contentrepository.core.domain.entity.Anchor getParentAnchor() {
return this.parentAnchor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sitepark.ies.contentrepository.core.domain.entity.filter;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class ParentAnchorList implements Filter {

@SuppressWarnings("PMD.AvoidFieldNameMatchingTypeName") // so that when deserializing it has the desired format

private final List<com.sitepark.ies.contentrepository.core.domain.entity.Anchor> parentAnchorList;

protected ParentAnchorList(@JsonProperty("parentAnchorList")
com.sitepark.ies.contentrepository.core.domain.entity.Anchor... parentAnchorList) {
assert parentAnchorList != null : "parentAnchorList is null";
this.parentAnchorList = List.of(parentAnchorList);
}

@SuppressFBWarnings("EI_EXPOSE_REP")
public List<com.sitepark.ies.contentrepository.core.domain.entity.Anchor> getParentAnchorList() {
return this.parentAnchorList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void testSerialize() throws Exception {
Filter filter =
or(
idList(6L),
anchor(com.sitepark.ies.contentrepository.core.domain.entity.Anchor.ofString("abc").get()),
anchor(com.sitepark.ies.contentrepository.core.domain.entity.Anchor.ofString("abc")),
and(
root(5L),
parent(7L),
Expand Down

0 comments on commit d2392ac

Please sign in to comment.