17
17
18
18
package bio .overture .song .server .service ;
19
19
20
+ import static bio .overture .song .core .exceptions .ServerErrors .MISMATCHING_SAMPLE_DATA ;
21
+ import static bio .overture .song .core .exceptions .ServerErrors .MISMATCHING_SPECIMEN_DATA ;
20
22
import static bio .overture .song .core .exceptions .ServerErrors .SAMPLE_TO_SPECIMEN_ID_MISMATCH ;
21
23
import static bio .overture .song .core .exceptions .ServerErrors .SPECIMEN_TO_DONOR_ID_MISMATCH ;
22
24
import static bio .overture .song .core .exceptions .ServerException .checkServer ;
23
25
import static java .util .Objects .isNull ;
24
26
25
27
import bio .overture .song .server .model .entity .Sample ;
28
+ import bio .overture .song .server .model .entity .Specimen ;
26
29
import bio .overture .song .server .model .entity .composites .CompositeEntity ;
27
30
import lombok .AllArgsConstructor ;
28
31
import lombok .extern .slf4j .Slf4j ;
@@ -74,26 +77,38 @@ public String save(String studyId, CompositeEntity s) {
74
77
specimen .getSubmitterSpecimenId ().equals (submitterSpecimenId ),
75
78
getClass (),
76
79
SAMPLE_TO_SPECIMEN_ID_MISMATCH ,
77
- "Existing sample (sampleId ='%s') has specimenId ='%s', but this submission says it has "
78
- + "specimenId ='%s' instead. Please re-submit with the correct specimenId ." ,
80
+ "Existing sample (submitterSampleId ='%s') has submitterSpecimenId ='%s', but this submission says it has "
81
+ + "submitterSpecimenId ='%s' instead. Please re-submit with the correct submitterSpecimenId ." ,
79
82
submitterSampleId ,
80
83
specimen .getSubmitterSpecimenId (),
81
84
submitterSpecimenId );
82
85
s .setSampleId (id );
83
- sampleService . update ( s );
86
+ checkSameSample ( sample , s );
84
87
}
85
88
return id ;
86
89
}
87
90
91
+ private void checkSameSample (Sample existing , CompositeEntity input ) {
92
+ val newSample = new Sample ();
93
+ newSample .setWithSample (input );
94
+ checkServer (
95
+ existing .equals (newSample ),
96
+ getClass (),
97
+ MISMATCHING_SAMPLE_DATA ,
98
+ "Input Sample data does not match the existing Sample data for submitterSampleId '%s'. Ensure the data matches." ,
99
+ existing .getSubmitterSampleId ());
100
+ }
101
+
88
102
private String getSampleParent (String studyId , CompositeEntity s ) {
89
- val specimen = s .getSpecimen ();
90
- String id = specimenService .findByBusinessKey (studyId , specimen .getSubmitterSpecimenId ());
103
+ val inputSpecimen = s .getSpecimen ();
104
+ String specimenId =
105
+ specimenService .findByBusinessKey (studyId , inputSpecimen .getSubmitterSpecimenId ());
91
106
92
- if (isNull (id )) {
93
- specimen .setDonorId (getSpecimenParent (studyId , s ));
94
- id = specimenService .create (studyId , specimen );
107
+ if (isNull (specimenId )) {
108
+ inputSpecimen .setDonorId (getSpecimenParent (studyId , s ));
109
+ specimenId = specimenService .create (studyId , inputSpecimen );
95
110
} else {
96
- val existingSpecimen = specimenService .securedRead (studyId , id );
111
+ val existingSpecimen = specimenService .securedRead (studyId , specimenId );
97
112
val existingDonor = donorService .securedRead (studyId , existingSpecimen .getDonorId ());
98
113
checkServer (
99
114
s .getDonor ().getSubmitterDonorId ().equals (existingDonor .getSubmitterDonorId ()),
@@ -104,11 +119,20 @@ private String getSampleParent(String studyId, CompositeEntity s) {
104
119
existingSpecimen .getSubmitterSpecimenId (),
105
120
existingDonor .getSubmitterDonorId (),
106
121
s .getDonor ().getSubmitterDonorId ());
107
- specimen .setSpecimenId (id );
108
- specimen .setDonorId (getSpecimenParent (studyId , s ));
109
- specimenService . update ( specimen );
122
+ inputSpecimen .setSpecimenId (specimenId );
123
+ inputSpecimen .setDonorId (getSpecimenParent (studyId , s ));
124
+ checkSameSpecimen ( existingSpecimen , inputSpecimen );
110
125
}
111
- return id ;
126
+ return specimenId ;
127
+ }
128
+
129
+ private void checkSameSpecimen (Specimen existing , Specimen input ) {
130
+ checkServer (
131
+ existing .equals (input ),
132
+ getClass (),
133
+ MISMATCHING_SPECIMEN_DATA ,
134
+ "Input Specimen data does not match the existing Specimen data for submitterSpecimenId '%s'. Ensure the data matches." ,
135
+ existing .getSubmitterSpecimenId ());
112
136
}
113
137
114
138
private String getSpecimenParent (String studyId , CompositeEntity s ) {
0 commit comments