-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #481: added interceptor to check update services cache when PlanDefinitions are inserted/updated/deleted ... added test * #481: added CDS Hooks services cache as a resource listener ... updated tests * Fixing tests
- Loading branch information
Showing
11 changed files
with
728 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
plugin/cds-hooks/src/main/java/org/opencds/cqf/ruler/cdshooks/CdsServicesCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.opencds.cqf.ruler.cdshooks; | ||
|
||
import ca.uhn.fhir.jpa.api.dao.DaoRegistry; | ||
import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; | ||
import ca.uhn.fhir.jpa.cache.IResourceChangeEvent; | ||
import ca.uhn.fhir.jpa.cache.IResourceChangeListener; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import org.hl7.fhir.instance.model.api.IBaseResource; | ||
import org.hl7.fhir.instance.model.api.IIdType; | ||
import org.hl7.fhir.r4.model.PlanDefinition; | ||
import org.opencds.cqf.ruler.cdshooks.discovery.DiscoveryResolutionR4; | ||
import org.opencds.cqf.ruler.cdshooks.discovery.DiscoveryResolutionStu3; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public class CdsServicesCache implements IResourceChangeListener { | ||
private static final Logger logger = LoggerFactory.getLogger(CdsServicesCache.class); | ||
|
||
private AtomicReference<JsonArray> cdsServiceCache; | ||
private IFhirResourceDao planDefinitionDao; | ||
private DiscoveryResolutionR4 discoveryResolutionR4; | ||
private DiscoveryResolutionStu3 discoveryResolutionStu3; | ||
|
||
public CdsServicesCache(DaoRegistry daoRegistry) { | ||
this.planDefinitionDao = daoRegistry.getResourceDao("PlanDefinition"); | ||
this.discoveryResolutionR4 = new DiscoveryResolutionR4(daoRegistry); | ||
this.discoveryResolutionStu3 = new DiscoveryResolutionStu3(daoRegistry); | ||
this.cdsServiceCache = new AtomicReference<>(new JsonArray()); | ||
} | ||
|
||
public AtomicReference<JsonArray> getCdsServiceCache() { | ||
return this.cdsServiceCache; | ||
} | ||
|
||
public void clearCache() { | ||
this.cdsServiceCache = new AtomicReference<>(new JsonArray()); | ||
} | ||
|
||
@Override | ||
public void handleInit(Collection<IIdType> collection) { | ||
|
||
} | ||
|
||
@Override | ||
public void handleChange(IResourceChangeEvent iResourceChangeEvent) { | ||
if (iResourceChangeEvent == null) return; | ||
if (iResourceChangeEvent.getCreatedResourceIds() != null && !iResourceChangeEvent.getCreatedResourceIds().isEmpty()) { | ||
insert(iResourceChangeEvent.getCreatedResourceIds()); | ||
} | ||
if (iResourceChangeEvent.getUpdatedResourceIds() != null && !iResourceChangeEvent.getUpdatedResourceIds().isEmpty()) { | ||
update(iResourceChangeEvent.getUpdatedResourceIds()); | ||
} | ||
if (iResourceChangeEvent.getDeletedResourceIds() != null && !iResourceChangeEvent.getDeletedResourceIds().isEmpty()) { | ||
delete(iResourceChangeEvent.getDeletedResourceIds()); | ||
} | ||
} | ||
|
||
private void insert(List<IIdType> createdIds) { | ||
for (IIdType id : createdIds) { | ||
try { | ||
IBaseResource resource = planDefinitionDao.read(id); | ||
if (resource instanceof PlanDefinition) { | ||
cdsServiceCache.get().add(discoveryResolutionR4.resolveService((PlanDefinition) resource)); | ||
} else if (resource instanceof org.hl7.fhir.dstu3.model.PlanDefinition) { | ||
cdsServiceCache.get().add(discoveryResolutionStu3.resolveService((org.hl7.fhir.dstu3.model.PlanDefinition) resource)); | ||
} | ||
} catch (Exception e) { | ||
logger.info(String.format("Failed to create service for %s", id.getIdPart())); | ||
} | ||
} | ||
} | ||
|
||
private void update(List<IIdType> updatedIds) { | ||
try { | ||
delete(updatedIds); | ||
insert(updatedIds); | ||
} catch (Exception e) { | ||
logger.info(String.format("Failed to update service(s) for %s", updatedIds)); | ||
} | ||
} | ||
|
||
private void delete(List<IIdType> deletedIds) { | ||
for (IIdType id : deletedIds) { | ||
for (int i = 0; i < cdsServiceCache.get().size(); i++) { | ||
if (((JsonObject) cdsServiceCache.get().get(i)).get("id").getAsString().equals(id.getIdPart())) { | ||
cdsServiceCache.get().remove(i); | ||
break; | ||
} | ||
else logger.info(String.format("Failed to delete service for %s", id.getIdPart())); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...in/cds-hooks/src/test/java/org/opencds/cqf/ruler/plugin/cdshooks/ResourceChangeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.opencds.cqf.ruler.plugin.cdshooks; | ||
|
||
import ca.uhn.fhir.jpa.cache.IResourceChangeEvent; | ||
import org.hl7.fhir.instance.model.api.IIdType; | ||
|
||
import java.util.List; | ||
|
||
public class ResourceChangeEvent implements IResourceChangeEvent { | ||
private List<IIdType> createdResourceIds; | ||
private List<IIdType> updatedResourceIds; | ||
private List<IIdType> deletedResourceIds; | ||
|
||
@Override | ||
public List<IIdType> getCreatedResourceIds() { | ||
return this.createdResourceIds; | ||
} | ||
|
||
public void setCreatedResourceIds(List<IIdType> createdResourceIds) { | ||
this.createdResourceIds = createdResourceIds; | ||
} | ||
|
||
@Override | ||
public List<IIdType> getUpdatedResourceIds() { | ||
return this.updatedResourceIds; | ||
} | ||
|
||
public void setUpdatedResourceIds(List<IIdType> updatedResourceIds) { | ||
this.updatedResourceIds = updatedResourceIds; | ||
} | ||
|
||
@Override | ||
public List<IIdType> getDeletedResourceIds() { | ||
return this.deletedResourceIds; | ||
} | ||
|
||
public void setDeletedResourceIds(List<IIdType> deletedResourceIds) { | ||
this.deletedResourceIds = deletedResourceIds; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
} | ||
} |
Oops, something went wrong.