Skip to content

Commit

Permalink
Use builder to create time interval
Browse files Browse the repository at this point in the history
RISDEV-0000
  • Loading branch information
malte-laukoetter committed Jan 3, 2025
1 parent 1786b2c commit d24a09c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,12 @@ private void addTemporalInformation(Norm norm) {
final TemporalGroup temporalGroup = new TemporalGroup.Builder()
.parentElement(temporalData)
.build();
new TimeInterval(
temporalGroup,
new Href.Builder().setEId(inkrafttreten.getEid().toString()).buildInternalReference(),
"geltungszeit"
);
new TimeInterval.Builder(temporalGroup)
.start(
new Href.Builder().setEId(inkrafttreten.getEid().toString()).buildInternalReference()
)
.refersTo("geltungszeit")
.build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/** Class representing a akn:timeInterval. */
@Getter
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
public class TimeInterval {

private final Node node;
Expand Down Expand Up @@ -44,4 +42,49 @@ public Optional<String> getEventRefEId() {
.map(Href::new)
.flatMap(Href::getEId);
}

/**
* Builder for creating new {@link TimeInterval}s
*/
public static class Builder {

private final Element element;

/**
* Create a new Builder for a {@link TimeInterval}.
* @param temporalGroup the parent element under which the new {@link TimeInterval} should be created.
*/
public Builder(TemporalGroup temporalGroup) {
this.element =
NodeCreator.createElementWithEidAndGuid("akn:timeInterval", temporalGroup.getNode());
}

/**
* The href to the akn:eventRef of the start date of the akn:timeInterval.
* @param start the href to the akn:eventRef of the start date
* @return the builder itself
*/
public TimeInterval.Builder start(Href start) {
element.setAttribute("start", start.toString());
return this;
}

/**
* The refersTo attribute of the akn:timeInterval.
* @param refersTo the refersTo attribute of the akn:timeInterval
* @return the builder itself
*/
public TimeInterval.Builder refersTo(String refersTo) {
element.setAttribute("refersTo", refersTo);
return this;
}

/**
* Create the new {@link TimeInterval}
* @return the new {@link TimeInterval}
*/
public TimeInterval build() {
return new TimeInterval(element);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,27 @@ class TimeIntervalTest {
@Test
void getEventRefEId() {
// given
final TimeInterval timeInterval = TimeInterval
.builder()
.node(
XmlMapper.toNode(
"""
<akn:timeInterval xmlns:akn="http://Inhaltsdaten.LegalDocML.de/1.7.1/" eId="meta-1_geltzeiten-1_geltungszeitgr-1_gelzeitintervall-1" GUID="ca9f53aa-d374-4bec-aca3-fff4e3485179" refersTo="geltungszeit" start="#meta-1_lebzykl-1_ereignis-2" />
"""
)
final TimeInterval timeInterval = new TimeInterval(
XmlMapper.toNode(
"""
<akn:timeInterval xmlns:akn="http://Inhaltsdaten.LegalDocML.de/1.7.1/" eId="meta-1_geltzeiten-1_geltungszeitgr-1_gelzeitintervall-1" GUID="ca9f53aa-d374-4bec-aca3-fff4e3485179" refersTo="geltungszeit" start="#meta-1_lebzykl-1_ereignis-2" />
"""
)
.build();
);

assertThat(timeInterval.getEventRefEId()).contains("meta-1_lebzykl-1_ereignis-2");
}

@Test
void getEventRefEIdEmpty() {
// given
final TimeInterval timeInterval = TimeInterval
.builder()
.node(
XmlMapper.toNode(
"""
<akn:timeInterval xmlns:akn="http://Inhaltsdaten.LegalDocML.de/1.7.1/" eId="meta-1_geltzeiten-1_geltungszeitgr-1_gelzeitintervall-1" GUID="ca9f53aa-d374-4bec-aca3-fff4e3485179" refersTo="geltungszeit" />
"""
)
final TimeInterval timeInterval = new TimeInterval(
XmlMapper.toNode(
"""
<akn:timeInterval xmlns:akn="http://Inhaltsdaten.LegalDocML.de/1.7.1/" eId="meta-1_geltzeiten-1_geltungszeitgr-1_gelzeitintervall-1" GUID="ca9f53aa-d374-4bec-aca3-fff4e3485179" refersTo="geltungszeit" />
"""
)
.build();
);

assertThat(timeInterval.getEventRefEId()).isEmpty();
}
Expand All @@ -54,11 +48,10 @@ void create() {
);

// when
var timeInterval = new TimeInterval(
temporalGroup,
new Href.Builder().setEId("meta-1_lebzykl-1_ereignis-2").buildInternalReference(),
"geltungszeit"
);
var timeInterval = new TimeInterval.Builder(temporalGroup)
.start(new Href.Builder().setEId("meta-1_lebzykl-1_ereignis-2").buildInternalReference())
.refersTo("geltungszeit")
.build();

// then
assertThat(timeInterval.getEventRefEId()).contains("meta-1_lebzykl-1_ereignis-2");
Expand Down

0 comments on commit d24a09c

Please sign in to comment.