@@ -121,15 +121,20 @@ public Boolean sampleHasLabelSpecificUpdates(CmoLabelParts sample,
121121 if (existingPatientSamples .isEmpty ()) {
122122 return Boolean .TRUE ;
123123 }
124+
124125 // find matching sample in set of existing patient samples
125- for (CmoLabelParts existingSample : existingPatientSamples ) {
126- if (existingSample .getPrimaryId ().equals (sample .getPrimaryId ())) {
126+ CmoLabelParts matchingSample = null ;
127+ Boolean hasUpdates = Boolean .FALSE ;
128+ for (CmoLabelParts ptSample : existingPatientSamples ) {
129+ if (ptSample .getPrimaryId ().equals (sample .getPrimaryId ())) {
130+ matchingSample = ptSample ;
127131 try {
128132 // if there are data differences or the existing sample does not have a label then
129133 // return true to permit label generation
130134 Boolean isConsistentData = jsonComparator .isConsistent (mapper .writeValueAsString (sample ),
131- mapper .writeValueAsString (existingSample ));
132- return (!isConsistentData || StringUtils .isBlank (existingSample .getCmoSampleName ()));
135+ mapper .writeValueAsString (ptSample ));
136+ hasUpdates = (!isConsistentData || StringUtils .isBlank (ptSample .getCmoSampleName ()));
137+ break ;
133138 } catch (JsonProcessingException ex ) {
134139 LOG .error ("Encountered JSON processing error while comparing sample "
135140 + "label parts to existing sample label parts." , ex );
@@ -138,7 +143,25 @@ public Boolean sampleHasLabelSpecificUpdates(CmoLabelParts sample,
138143 }
139144 }
140145 }
141- return Boolean .TRUE ;
146+ if (matchingSample == null || hasUpdates ) {
147+ return Boolean .TRUE ;
148+ }
149+
150+ // if no label specific updates then check if existing sample label is in use by another sample
151+ if (!StringUtils .isBlank (matchingSample .getCmoSampleName ())) {
152+ for (CmoLabelParts otherSample : existingPatientSamples ) {
153+ if (otherSample .getPrimaryId ().equals (sample .getPrimaryId ())) {
154+ continue ;
155+ }
156+ if (StringUtils .isBlank (otherSample .getCmoSampleName ())) {
157+ continue ;
158+ }
159+ if (otherSample .getCmoSampleName ().equals (matchingSample .getCmoSampleName ())) {
160+ return Boolean .TRUE ;
161+ }
162+ }
163+ }
164+ return Boolean .FALSE ;
142165 }
143166
144167 /**
0 commit comments