Skip to content

Commit 106c789

Browse files
author
Sebastian Benjamin
committed
Implement upsert + test
1 parent 15857ac commit 106c789

File tree

3 files changed

+398
-41
lines changed

3 files changed

+398
-41
lines changed

Studies/api-src/org/labkey/api/studies/study/StudyDefinition.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package org.labkey.api.studies.study;
22

3+
import com.fasterxml.jackson.annotation.JsonGetter;
4+
import com.fasterxml.jackson.annotation.JsonIgnore;
35
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.annotation.JsonSetter;
47
import com.fasterxml.jackson.core.JsonProcessingException;
58
import com.fasterxml.jackson.databind.ObjectMapper;
69
import com.fasterxml.jackson.databind.ObjectWriter;
710
import org.json.JSONObject;
811

912
import java.util.Date;
1013
import java.util.List;
14+
import java.util.Map;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.IntStream;
1117

1218
public class StudyDefinition
1319
{
@@ -246,12 +252,12 @@ public void setDescription(String description)
246252
_description = description;
247253
}
248254

249-
public Boolean getControlGroup()
255+
public Boolean getIsControlGroup()
250256
{
251257
return _isControlGroup;
252258
}
253259

254-
public void setControlGroup(Boolean controlGroup)
260+
public void setIsControlGroup(Boolean controlGroup)
255261
{
256262
_isControlGroup = controlGroup;
257263
}
@@ -448,7 +454,13 @@ public static class Timepoint
448454
private String _labelShort;
449455
private String _description;
450456

457+
@JsonIgnore
451458
private Integer _anchorEvent;
459+
460+
@JsonIgnore
461+
private String _anchorEventLabel;
462+
463+
private String _cohortName;
452464
private Integer _rangeMin;
453465
private Integer _rangeMax;
454466

@@ -464,6 +476,25 @@ public Timepoint()
464476

465477
}
466478

479+
// When reading from a JSON object, store the anchorEvent label
480+
@JsonSetter("anchorEvent")
481+
void readAnchorEvent(String lbl) { _anchorEventLabel = lbl; }
482+
483+
@JsonGetter("anchorEvent")
484+
String writeAnchorEvent() { return _anchorEventLabel; }
485+
486+
// Call this to translate from label to index in order to fit the DB schema
487+
void resolveAnchorEvent(Map<String,Integer> idxByLabel)
488+
{
489+
Integer idx = idxByLabel.get(_anchorEventLabel);
490+
if (idx == null)
491+
throw new IllegalArgumentException(
492+
"Unknown anchorEvent label '" + _anchorEventLabel + "'");
493+
_anchorEvent = idx;
494+
}
495+
496+
public Integer getAnchorEvent() { return _anchorEvent; }
497+
467498
public Integer getRowId()
468499
{
469500
return _rowId;
@@ -484,6 +515,16 @@ public void setStudyId(Integer studyId)
484515
_studyId = studyId;
485516
}
486517

518+
public String getCohortName()
519+
{
520+
return _cohortName;
521+
}
522+
523+
public void setCohortName(String cohortName)
524+
{
525+
_cohortName = cohortName;
526+
}
527+
487528
public Integer getCohortId()
488529
{
489530
return _cohortId;
@@ -524,16 +565,6 @@ public void setDescription(String description)
524565
_description = description;
525566
}
526567

527-
public Integer getAnchorEvent()
528-
{
529-
return _anchorEvent;
530-
}
531-
532-
public void setAnchorEvent(Integer anchorEvent)
533-
{
534-
_anchorEvent = anchorEvent;
535-
}
536-
537568
public Integer getRangeMin()
538569
{
539570
return _rangeMin;
@@ -608,8 +639,19 @@ public void setModified(Date modified)
608639
public static StudyDefinition fromJson(JSONObject json)
609640
{
610641
ObjectMapper mapper = new ObjectMapper();
642+
StudyDefinition sd = mapper.convertValue(json.toMap(), StudyDefinition.class);
643+
644+
// In our JSON, Timepoints store the anchorEvent label, not an ID. Since the DB schema requires an int, we need
645+
// to do that translation manually. Here, we store the anchorEvent by its index in the anchorEvent list.
646+
Map<String,Integer> idxByLabel = IntStream.range(0, sd.getAnchorEvents().size())
647+
.boxed()
648+
.collect(Collectors.toMap(
649+
i -> sd.getAnchorEvents().get(i).getLabel(),
650+
i -> i));
651+
652+
sd.getTimepoints().forEach(tp -> tp.resolveAnchorEvent(idxByLabel));
611653

612-
return mapper.convertValue(json, StudyDefinition.class);
654+
return sd;
613655
}
614656

615657
public String toJson() throws JsonProcessingException

Studies/resources/study/DemoStudy.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"cohortName": "Group1",
77
"label": "Group 1",
88
"description": "This is the first group",
9+
"isControlGroup": false,
910
"sortOrder": 1
1011
},{
1112
"cohortName": "Control",
@@ -14,7 +15,6 @@
1415
"isControlGroup": true,
1516
"sortOrder": 2
1617
}],
17-
1818
"anchorEvents": [{
1919
"label": "Study Enrollment",
2020
"description": "The represents Day 0 of the study",
@@ -30,14 +30,14 @@
3030
"label": "Vaccination",
3131
"labelShort": "V",
3232
"anchorEvent": "Study Enrollment",
33-
"minDate": 7,
34-
"maxDate": 10
33+
"rangeMin": 7,
34+
"rangeMax": 10
3535
},{
3636
"cohortName": "Control",
3737
"label": "Mock-Vaccination",
3838
"labelShort": "V",
3939
"anchorEvent": "Study Enrollment",
40-
"minDate": 7,
41-
"maxDate": 10
40+
"rangeMin": 7,
41+
"rangeMax": 10
4242
}]
4343
}

0 commit comments

Comments
 (0)