Skip to content

Commit

Permalink
MORE2-5 set participants start datetime on registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kurz committed Nov 17, 2023
1 parent 9fa9913 commit 81d0963
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.commons.lang3.tuple.Pair;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;

public final class StudyTransformer {
Expand All @@ -30,7 +31,7 @@ public static StudyDTO toDTO(Study study) {
.contact(toDTO(study.contact()))
.start(study.startDate())
.end(study.endDate())
.observations(toDTO(study.observations()))
.observations(toDTO(study.observations(), study.participant().start()))
.version(BaseTransformers.toVersionTag(study.modified()))
;
}
Expand All @@ -53,11 +54,11 @@ public static ContactInfoDTO toDTO(Contact contact) {
;
}

public static List<ObservationDTO> toDTO(List<Observation> observations) {
return observations.stream().map(StudyTransformer::toDTO).toList();
public static List<ObservationDTO> toDTO(List<Observation> observations, LocalDateTime start) {
return observations.stream().map(o -> StudyTransformer.toDTO(o, start)).toList();
}

public static ObservationDTO toDTO(Observation observation) {
public static ObservationDTO toDTO(Observation observation, LocalDateTime start) {
ObservationDTO dto = new ObservationDTO()
.observationId(String.valueOf(observation.observationId()))
.observationType(observation.type())
Expand All @@ -68,9 +69,9 @@ public static ObservationDTO toDTO(Observation observation) {
.hidden(observation.hidden())
.noSchedule(observation.noSchedule())
;
if(observation.observationSchedule() != null) {
if(observation.observationSchedule() != null && start != null) {
dto.schedule(ICalendarParser
.parseToObservationSchedules(observation.observationSchedule())
.parseToObservationSchedules(observation.observationSchedule(), start)
.stream()
.map(StudyTransformer::toObservationScheduleDTO)
.toList());
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/redlink/more/data/model/Observation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.redlink.more.data.model;

import io.redlink.more.data.model.scheduler.ScheduleEvent;

import java.time.Instant;

public record Observation(
Expand All @@ -8,7 +10,7 @@ public record Observation(
String type,
String participantInfo,
Object properties,
Event observationSchedule,
ScheduleEvent observationSchedule,
Instant created,
Instant modified,
boolean hidden,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.redlink.more.data.model;

import java.time.LocalDateTime;

public record SimpleParticipant(
int id,
String alias
String alias,
LocalDateTime start
) {
}
2 changes: 0 additions & 2 deletions src/main/java/io/redlink/more/data/model/Study.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.redlink.more.data.model;

import io.redlink.more.data.api.app.v1.model.StudyDTO;

import java.time.Instant;
import java.time.LocalDate;
import java.util.List;
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/io/redlink/more/data/model/scheduler/Duration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.redlink.more.data.model.scheduler;

import com.fasterxml.jackson.annotation.JsonCreator;

public class Duration {

private Integer value;

/**
* unit of time to offset
*/
public enum Unit {
MINUTE("MINUTE"),

HOUR("HOUR"),

DAY("DAY");

private String value;

Unit(String value) {
this.value = value;
}

public String getValue() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static Unit fromValue(String value) {
for (Unit b : Unit.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}

private Unit unit;

public Duration() {
}

public Integer getValue() {
return value;
}

public Duration setValue(Integer value) {
this.value = value;
return this;
}

public Unit getUnit() {
return unit;
}

public Duration setUnit(Unit unit) {
this.unit = unit;
return this;
}

@Override
public String toString() {
return "Duration{" +
"offset=" + value +
", unit=" + unit +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package io.redlink.more.data.model;
package io.redlink.more.data.model.scheduler;

import java.time.Instant;

public class Event {
public class Event implements ScheduleEvent {
public static final String TYPE = "Event";
private String type;
private Instant dateStart;
private Instant dateEnd;
private RecurrenceRule recurrenceRule;

@Override
public String getType() {
return TYPE;
}

public Instant getDateStart() {
return dateStart;
}
Expand All @@ -33,4 +40,6 @@ public Event setRRule(RecurrenceRule recurrenceRule) {
this.recurrenceRule = recurrenceRule;
return this;
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.redlink.more.data.model;
package io.redlink.more.data.model.scheduler;

import java.time.Instant;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.redlink.more.data.model.scheduler;

public class RelativeDate {

private Duration offset;
private String time;

public RelativeDate() {
}

public Duration getOffset() {
return offset;
}

public RelativeDate setOffset(Duration offset) {
this.offset = offset;
return this;
}

public String getTime() {
return time;
}

public RelativeDate setTime(String time) {
this.time = time;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.redlink.more.data.model.scheduler;

public class RelativeEvent implements ScheduleEvent {

public static final String TYPE = "RelativeEvent";

private String type;

private RelativeDate dtstart;

private RelativeDate dtend;

private RelativeRecurrenceRule rrrule;

public RelativeEvent() {
}

@Override
public String getType() {
return TYPE;
}

public RelativeEvent setType(String type) {
this.type = type;
return this;
}

public RelativeDate getDtstart() {
return dtstart;
}

public RelativeEvent setDtstart(RelativeDate dtstart) {
this.dtstart = dtstart;
return this;
}

public RelativeDate getDtend() {
return dtend;
}

public RelativeEvent setDtend(RelativeDate dtend) {
this.dtend = dtend;
return this;
}

public RelativeRecurrenceRule getRrrule() {
return rrrule;
}

public RelativeEvent setRrrule(RelativeRecurrenceRule rrrule) {
this.rrrule = rrrule;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.redlink.more.data.model.scheduler;

public class RelativeRecurrenceRule {

private Duration frequency;

private Duration endAfter;

public RelativeRecurrenceRule() {
}

public Duration getFrequency() {
return frequency;
}

public RelativeRecurrenceRule setFrequency(Duration frequency) {
this.frequency = frequency;
return this;
}

public Duration getEndAfter() {
return endAfter;
}

public RelativeRecurrenceRule setEndAfter(Duration endAfter) {
this.endAfter = endAfter;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.redlink.more.data.model.scheduler;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonIgnoreProperties(
value = "type", // ignore manually set type, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the type to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true, defaultImpl = Event.class)
@JsonSubTypes({
@JsonSubTypes.Type(value = Event.class, name = Event.TYPE),
@JsonSubTypes.Type(value = RelativeEvent.class, name = RelativeEvent.TYPE)
})
public interface ScheduleEvent {
public String getType();
}
6 changes: 3 additions & 3 deletions src/main/java/io/redlink/more/data/repository/DbUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.redlink.more.data.model.Event;
import io.redlink.more.data.model.scheduler.ScheduleEvent;

import java.sql.*;
import java.time.Instant;
Expand Down Expand Up @@ -50,11 +50,11 @@ public static OptionalInt readOptionalInt(ResultSet row, String columnLabel) thr
}
}

public static Event readEvent(ResultSet row, String columnLabel) throws SQLException {
public static ScheduleEvent readEvent(ResultSet row, String columnLabel) throws SQLException {
var rawValue = row.getString(columnLabel);
if(rawValue == null) return null;
try {
return MAPPER.readValue(rawValue, Event.class);
return MAPPER.readValue(rawValue, ScheduleEvent.class);
} catch (JsonProcessingException e) {
throw new SQLDataException("Could not read Event from column '" + columnLabel + "'", e);
}
Expand Down
Loading

0 comments on commit 81d0963

Please sign in to comment.