109
109
import org .apache .commons .lang3 .NotImplementedException ;
110
110
import org .apache .commons .lang3 .StringUtils ;
111
111
import org .apache .commons .lang3 .Validate ;
112
+ import org .apache .commons .lang3 .builder .EqualsBuilder ;
112
113
import org .hl7 .fhir .instance .model .api .IAnyResource ;
113
114
import org .hl7 .fhir .instance .model .api .IBase ;
114
115
import org .hl7 .fhir .instance .model .api .IBaseCoding ;
@@ -291,7 +292,7 @@ private void extractHapiTags(
291
292
next .getVersion (),
292
293
myCodingSpy .getBooleanObject (next ));
293
294
if (def != null ) {
294
- ResourceTag tag = theEntity . addTag ( def );
295
+ ResourceTag tag = maybeAddTagToEntity ( theEntity , def );
295
296
allDefs .add (tag );
296
297
theEntity .setHasTags (true );
297
298
}
@@ -310,7 +311,7 @@ private void extractHapiTags(
310
311
next .getVersionElement ().getValue (),
311
312
next .getUserSelectedElement ().getValue ());
312
313
if (def != null ) {
313
- ResourceTag tag = theEntity . addTag ( def );
314
+ ResourceTag tag = maybeAddTagToEntity ( theEntity , def );
314
315
allDefs .add (tag );
315
316
theEntity .setHasTags (true );
316
317
}
@@ -323,7 +324,7 @@ private void extractHapiTags(
323
324
TagDefinition def = cacheTagDefinitionDao .getTagOrNull (
324
325
theTransactionDetails , TagTypeEnum .PROFILE , NS_JPA_PROFILE , next .getValue (), null , null , null );
325
326
if (def != null ) {
326
- ResourceTag tag = theEntity . addTag ( def );
327
+ ResourceTag tag = maybeAddTagToEntity ( theEntity , def );
327
328
allDefs .add (tag );
328
329
theEntity .setHasTags (true );
329
330
}
@@ -348,7 +349,7 @@ private void extractRiTags(
348
349
next .getVersion (),
349
350
myCodingSpy .getBooleanObject (next ));
350
351
if (def != null ) {
351
- ResourceTag tag = theEntity . addTag ( def );
352
+ ResourceTag tag = maybeAddTagToEntity ( theEntity , def );
352
353
theAllTags .add (tag );
353
354
theEntity .setHasTags (true );
354
355
}
@@ -367,7 +368,7 @@ private void extractRiTags(
367
368
next .getVersion (),
368
369
myCodingSpy .getBooleanObject (next ));
369
370
if (def != null ) {
370
- ResourceTag tag = theEntity . addTag ( def );
371
+ ResourceTag tag = maybeAddTagToEntity ( theEntity , def );
371
372
theAllTags .add (tag );
372
373
theEntity .setHasTags (true );
373
374
}
@@ -380,7 +381,7 @@ private void extractRiTags(
380
381
TagDefinition def = cacheTagDefinitionDao .getTagOrNull (
381
382
theTransactionDetails , TagTypeEnum .PROFILE , NS_JPA_PROFILE , next .getValue (), null , null , null );
382
383
if (def != null ) {
383
- ResourceTag tag = theEntity . addTag ( def );
384
+ ResourceTag tag = maybeAddTagToEntity ( theEntity , def );
384
385
theAllTags .add (tag );
385
386
theEntity .setHasTags (true );
386
387
}
@@ -400,13 +401,42 @@ private void extractProfileTags(
400
401
TagDefinition profileDef = cacheTagDefinitionDao .getTagOrNull (
401
402
theTransactionDetails , TagTypeEnum .PROFILE , NS_JPA_PROFILE , profile , null , null , null );
402
403
403
- ResourceTag tag = theEntity . addTag ( profileDef );
404
+ ResourceTag tag = maybeAddTagToEntity ( theEntity , profileDef );
404
405
theAllTags .add (tag );
405
406
theEntity .setHasTags (true );
406
407
}
407
408
}
408
409
}
409
410
411
+ /**
412
+ * Utility method adding <code>theTagDefToAdd</code> to <code>theEntity</code>. Since the database may contain
413
+ * duplicate tagDefinitions, we perform a logical comparison, i.e. we don't care about the tagDefiniton.id, and add
414
+ * the tag to the entity only if the entity does not already have that tag.
415
+ *
416
+ * @param theEntity receiving the tagDefinition
417
+ * @param theTagDefToAdd to theEntity
418
+ * @return <code>theTagDefToAdd</code> wrapped in a resourceTag if it was added to <code>theEntity</code> or <code>theEntity</code>'s
419
+ * resourceTag encapsulating a tagDefinition that is logically equal to theTagDefToAdd.
420
+ */
421
+ private ResourceTag maybeAddTagToEntity (ResourceTable theEntity , TagDefinition theTagDefToAdd ) {
422
+ for (ResourceTag resourceTagFromEntity : theEntity .getTags ()) {
423
+ TagDefinition tag = resourceTagFromEntity .getTag ();
424
+ EqualsBuilder equalsBuilder = new EqualsBuilder ();
425
+ equalsBuilder .append (tag .getSystem (), theTagDefToAdd .getSystem ());
426
+ equalsBuilder .append (tag .getCode (), theTagDefToAdd .getCode ());
427
+ equalsBuilder .append (tag .getDisplay (), theTagDefToAdd .getDisplay ());
428
+ equalsBuilder .append (tag .getVersion (), theTagDefToAdd .getVersion ());
429
+ equalsBuilder .append (tag .getUserSelected (), theTagDefToAdd .getUserSelected ());
430
+ equalsBuilder .append (tag .getTagType (), theTagDefToAdd .getTagType ());
431
+
432
+ if (equalsBuilder .isEquals ()) {
433
+ return resourceTagFromEntity ;
434
+ }
435
+ }
436
+
437
+ return theEntity .addTag (theTagDefToAdd );
438
+ }
439
+
410
440
private Set <ResourceTag > getAllTagDefinitions (ResourceTable theEntity ) {
411
441
HashSet <ResourceTag > retVal = Sets .newHashSet ();
412
442
if (theEntity .isHasTags ()) {
@@ -643,7 +673,7 @@ protected EncodedResource populateResourceIntoEntity(
643
673
return sourceExtension ;
644
674
}
645
675
646
- private boolean updateTags (
676
+ protected boolean updateTags (
647
677
TransactionDetails theTransactionDetails ,
648
678
RequestDetails theRequest ,
649
679
IBaseResource theResource ,
0 commit comments