diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java index 851c5de2856..49cd5d03f9e 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java @@ -92,9 +92,9 @@ import static org.apache.commons.lang3.StringUtils.*; * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -1167,7 +1167,7 @@ public abstract class BaseHapiFhirDao implements IDao, public SearchBuilder newSearchBuilder() { SearchBuilder builder = new SearchBuilder( getContext(), myEntityManager, myFulltextSearchSvc, this, myResourceIndexedSearchParamUriDao, - myForcedIdDao, myTerminologySvc, mySerarchParamRegistry, myResourceTagDao, myResourceViewDao); + myForcedIdDao, myTerminologySvc, mySerarchParamRegistry, myResourceTagDao, myResourceViewDao); return builder; } @@ -1301,21 +1301,24 @@ public abstract class BaseHapiFhirDao implements IDao, } } - // Don't keep duplicate tags + Set allTagsNew = getAllTagDefinitions(theEntity); Set allDefsPresent = new HashSet<>(); - Collection tags; - allTagsOld.removeIf(theResourceTag -> !allDefsPresent.add(theResourceTag.getTag())); + allTagsNew.forEach(tag -> { - // Remove any tags that have been removed - for (ResourceTag next : allTagsOld) { - if (!allDefs.contains(next)) { - if (shouldDroppedTagBeRemovedOnUpdate(theRequest, next)) { - allTagsOld.remove(next); + // Don't keep duplicate tags + if (!allDefsPresent.add(tag.getTag())) { + theEntity.getTags().remove(tag); + } + + // Drop any tags that have been removed + if (!allDefs.contains(tag)) { + if (shouldDroppedTagBeRemovedOnUpdate(theRequest, tag)) { + theEntity.getTags().remove(tag); } } - } - Set allTagsNew = getAllTagDefinitions(theEntity); + }); + if (!allTagsOld.equals(allTagsNew)) { changed = true; } @@ -1474,6 +1477,15 @@ public abstract class BaseHapiFhirDao implements IDao, return retVal; } + /** + * Subclasses may override to provide behaviour. Called when a pre-existing resource has been updated in the database + * + * @param theEntity The resource + */ + protected void postDelete(ResourceTable theEntity) { + // nothing + } + /** * Subclasses may override to provide behaviour. Called when a resource has been inserted into the database for the first time. * @@ -1484,15 +1496,6 @@ public abstract class BaseHapiFhirDao implements IDao, // nothing } - /** - * Subclasses may override to provide behaviour. Called when a pre-existing resource has been updated in the database - * - * @param theEntity The resource - */ - protected void postDelete(ResourceTable theEntity) { - // nothing - } - /** * Subclasses may override to provide behaviour. Called when a pre-existing resource has been updated in the database * @@ -1627,20 +1630,20 @@ public abstract class BaseHapiFhirDao implements IDao, @SuppressWarnings("unchecked") @Override public R toResource(Class theResourceType, IBaseResourceEntity theEntity, Collection theTagList, boolean theForHistoryOperation) { - + // 1. get resource, it's encoding and the tags if any byte[] resourceBytes = null; ResourceEncodingEnum resourceEncoding = null; Collection myTagList = null; - + if (theEntity instanceof ResourceHistoryTable) { ResourceHistoryTable history = (ResourceHistoryTable) theEntity; resourceBytes = history.getResource(); resourceEncoding = history.getEncoding(); myTagList = history.getTags(); } else if (theEntity instanceof ResourceTable) { - ResourceTable resource = (ResourceTable)theEntity; - ResourceHistoryTable history = myResourceHistoryTableDao.findForIdAndVersion(theEntity.getId(), theEntity.getVersion()); + ResourceTable resource = (ResourceTable) theEntity; + ResourceHistoryTable history = myResourceHistoryTableDao.findForIdAndVersion(theEntity.getId(), theEntity.getVersion()); if (history == null) { return null; } @@ -1649,7 +1652,7 @@ public abstract class BaseHapiFhirDao implements IDao, myTagList = resource.getTags(); } else if (theEntity instanceof ResourceSearchView) { // This is the search View - ResourceSearchView myView = (ResourceSearchView)theEntity; + ResourceSearchView myView = (ResourceSearchView) theEntity; resourceBytes = myView.getResource(); resourceEncoding = myView.getEncoding(); if (theTagList == null) @@ -1664,7 +1667,7 @@ public abstract class BaseHapiFhirDao implements IDao, // 2. get The text String resourceText = null; switch (resourceEncoding) { - case JSON: + case JSON: try { resourceText = new String(resourceBytes, "UTF-8"); } catch (UnsupportedEncodingException e) { @@ -1677,7 +1680,7 @@ public abstract class BaseHapiFhirDao implements IDao, case DEL: break; } - + // 3. Use the appropriate custom type if one is specified in the context Class resourceType = theResourceType; if (myContext.hasDefaultTypeForProfile()) { @@ -2197,16 +2200,11 @@ public abstract class BaseHapiFhirDao implements IDao, } // if thePerformIndexing - // FIXME: needed? - //theEntity = myEntityManager.merge(theEntity); - myEntityManager.flush(); - if (theResource != null) { populateResourceIdFromEntity(theEntity, theResource); } - return theEntity; } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4UpdateTagSnapshotTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4UpdateTagSnapshotTest.java index 632dd8bfa86..9f727ea1aa4 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4UpdateTagSnapshotTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4UpdateTagSnapshotTest.java @@ -39,9 +39,7 @@ public class FhirResourceDaoR4UpdateTagSnapshotTest extends BaseJpaR4Test { myPatientDao.update(p, mySrd); p = myPatientDao.read(new IdType("A"), mySrd); - // It would be nice if this didn't trigger a version update but - // i guess it's not so bad that it does - assertEquals("2", p.getIdElement().getVersionIdPart()); + assertEquals("1", p.getIdElement().getVersionIdPart()); assertEquals(true, p.getActive()); assertEquals(1, p.getMeta().getTag().size()); } @@ -86,9 +84,7 @@ public class FhirResourceDaoR4UpdateTagSnapshotTest extends BaseJpaR4Test { myPatientDao.update(p, mySrd); p = myPatientDao.read(new IdType("A"), mySrd); - // It would be nice if this didn't trigger a version update but - // i guess it's not so bad that it does - assertEquals("2", p.getIdElement().getVersionIdPart()); + assertEquals("1", p.getIdElement().getVersionIdPart()); assertEquals(true, p.getActive()); assertEquals(1, p.getMeta().getTag().size()); assertEquals("urn:foo", p.getMeta().getTag().get(0).getSystem()); @@ -136,9 +132,7 @@ public class FhirResourceDaoR4UpdateTagSnapshotTest extends BaseJpaR4Test { p = myPatientDao.read(new IdType("A"), mySrd); assertEquals(true, p.getActive()); assertEquals(0, p.getMeta().getTag().size()); - // It would be nice if this didn't trigger a version update but - // i guess it's not so bad that it does - assertEquals("2", p.getIdElement().getVersionIdPart()); + assertEquals("1", p.getIdElement().getVersionIdPart()); } @AfterClass