11package org .labkey .api .studies .study ;
22
3+ import com .fasterxml .jackson .annotation .JsonGetter ;
4+ import com .fasterxml .jackson .annotation .JsonIgnore ;
35import com .fasterxml .jackson .annotation .JsonProperty ;
6+ import com .fasterxml .jackson .annotation .JsonSetter ;
47import com .fasterxml .jackson .core .JsonProcessingException ;
58import com .fasterxml .jackson .databind .ObjectMapper ;
69import com .fasterxml .jackson .databind .ObjectWriter ;
710import org .json .JSONObject ;
811
912import java .util .Date ;
1013import java .util .List ;
14+ import java .util .Map ;
15+ import java .util .stream .Collectors ;
16+ import java .util .stream .IntStream ;
1117
1218public 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
0 commit comments