Add example interceptor

This commit is contained in:
James Agnew 2020-01-06 14:26:03 -05:00
parent e42acef66b
commit 23ca94d3c5
4 changed files with 160 additions and 7 deletions

View File

@ -23,8 +23,13 @@ package ca.uhn.fhir.jpa.term;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.context.support.IContextValidationSupport;
import ca.uhn.fhir.jpa.dao.*;
import ca.uhn.fhir.jpa.dao.DaoConfig;
import ca.uhn.fhir.jpa.dao.DaoRegistry;
import ca.uhn.fhir.jpa.dao.IDao;
import ca.uhn.fhir.jpa.dao.IFhirResourceDaoCodeSystem;
import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet;
import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult;
import ca.uhn.fhir.jpa.dao.IFulltextSearchSvc;
import ca.uhn.fhir.jpa.dao.data.*;
import ca.uhn.fhir.jpa.entity.*;
import ca.uhn.fhir.jpa.entity.TermConceptParentChildLink.RelationshipTypeEnum;
@ -56,7 +61,11 @@ import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.TermsQuery;
import org.apache.lucene.search.*;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RegexpQuery;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.search.jpa.FullTextEntityManager;
@ -92,14 +101,21 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import static org.apache.commons.lang3.StringUtils.*;
import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNoneBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
public abstract class BaseTermReadSvcImpl implements ITermReadSvc, ApplicationContextAware {
public static final int DEFAULT_FETCH_SIZE = 250;
@ -1056,9 +1072,11 @@ public abstract class BaseTermReadSvcImpl implements ITermReadSvc, ApplicationCo
private void expandWithoutHibernateSearch(IValueSetConceptAccumulator theValueSetCodeAccumulator, TermCodeSystemVersion theVersion, Set<String> theAddedCodes, ValueSet.ConceptSetComponent theInclude, String theSystem, boolean theAdd, AtomicInteger theCodeCounter) {
ourLog.trace("Hibernate search is not enabled");
if (theValueSetCodeAccumulator instanceof ValueSetExpansionComponentWithConceptAccumulator) {
Validate.isTrue(((ValueSetExpansionComponentWithConceptAccumulator) theValueSetCodeAccumulator).getParameter().isEmpty(), "Can not expand ValueSet with parameters - Hibernate Search is not enabled on this server.");
}
aaaa
Validate.isTrue(theInclude.getFilter().isEmpty(), "Can not expand ValueSet with filters - Hibernate Search is not enabled on this server.");
Validate.isTrue(isNotBlank(theSystem), "Can not expand ValueSet without explicit system - Hibernate Search is not enabled on this server.");

View File

@ -3,14 +3,17 @@ package ca.uhn.fhir.jpa.dao.r4;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.bulk.IBulkDataExportSvc;
import ca.uhn.fhir.jpa.config.TestR4WithLuceneDisabledConfig;
import ca.uhn.fhir.jpa.dao.*;
import ca.uhn.fhir.jpa.dao.BaseJpaTest;
import ca.uhn.fhir.jpa.dao.DaoConfig;
import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet;
import ca.uhn.fhir.jpa.dao.IFhirSystemDao;
import ca.uhn.fhir.jpa.dao.dstu2.FhirResourceDaoDstu2SearchNoFtTest;
import ca.uhn.fhir.jpa.search.ISearchCoordinatorSvc;
import ca.uhn.fhir.jpa.search.reindex.IResourceReindexingSvc;
import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
import ca.uhn.fhir.jpa.searchparam.registry.ISearchParamRegistry;
import ca.uhn.fhir.jpa.sp.ISearchParamPresenceSvc;
import ca.uhn.fhir.jpa.term.BaseTermReadSvcImpl;
import ca.uhn.fhir.jpa.term.api.ITermReadSvc;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.api.EncodingEnum;
@ -197,7 +200,7 @@ public class FhirResourceDaoR4SearchWithLuceneDisabledTest extends BaseJpaTest {
}
@Test
public void testExpandValueSet() {
public void testExpandValueSet() {
CodeSystem cs = new CodeSystem();
cs.setUrl("http://fooCS");
cs.setContent(CodeSystem.CodeSystemContentMode.COMPLETE);
@ -226,6 +229,32 @@ public class FhirResourceDaoR4SearchWithLuceneDisabledTest extends BaseJpaTest {
}
@Test
public void testExpandValueSetWithFilter() {
CodeSystem cs = new CodeSystem();
cs.setUrl("http://fooCS");
cs.setContent(CodeSystem.CodeSystemContentMode.COMPLETE);
cs.addConcept().setCode("CODEA");
cs.addConcept().setCode("CODEB");
myCodeSystemDao.create(cs);
ValueSet vs = new ValueSet();
vs.setUrl("http://fooVS");
vs.getCompose()
.addInclude()
.setSystem("http://fooCS")
.addFilter()
.setOp(ValueSet.FilterOperator.EQUAL)
.setProperty("code")
.setValue("CODEA");
try {
myValueSetDao.expand(vs, null);
} catch (NullPointerException e) {
assertEquals("", e.getMessage());
}
}
@Test
public void testSearchByCodeIn() {
CodeSystem cs = new CodeSystem();

View File

@ -1,7 +1,9 @@
package ca.uhn.fhir.jpa.provider.r4;
import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.HookParams;
import ca.uhn.fhir.interceptor.api.IAnonymousInterceptor;
import ca.uhn.fhir.interceptor.api.Interceptor;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.jpa.dao.DaoConfig;
import ca.uhn.fhir.jpa.interceptor.PerformanceTracingLoggingInterceptor;
@ -45,8 +47,10 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
@ -421,6 +425,81 @@ public class ResourceProviderInterceptorR4Test extends BaseResourceProviderR4Tes
}
}
@Test
public void testInterceptorExpandsSearch() {
@Interceptor
class SearchExpandingInterceptor {
@Hook(Pointcut.SERVER_INCOMING_REQUEST_POST_PROCESSED)
public void enrich(RequestDetails theRequestDetails) {
String[] subjectValues = theRequestDetails.getParameters().get("subject");
if (subjectValues != null) {
for (int index = 0; index < subjectValues.length; index++) {
String nextValue = subjectValues[index];
if (nextValue.equals("Patient/p1")) {
nextValue = "Patient/p1,Patient/p2";
subjectValues[index] = nextValue;
}
}
}
}
}
Patient p1 = new Patient();
p1.setId("p1");
p1.addIdentifier().setValue("p1");
myPatientDao.update(p1);
Observation o1 = new Observation();
o1.setId("o1");
o1.getSubject().setReference("Patient/p1");
myObservationDao.update(o1);
Patient p2 = new Patient();
p2.setId("p2");
p2.addIdentifier().setValue("p2");
myPatientDao.update(p2);
Observation o2 = new Observation();
o2.setId("o2");
o2.getSubject().setReference("Patient/p2");
myObservationDao.update(o2);
Patient p3 = new Patient();
p3.setId("p3");
p3.addIdentifier().setValue("p3");
myPatientDao.update(p3);
Observation o3 = new Observation();
o3.setId("o3");
o3.getSubject().setReference("Patient/p3");
myObservationDao.update(o3);
SearchExpandingInterceptor interceptor = new SearchExpandingInterceptor();
try {
ourRestServer.registerInterceptor(interceptor);
Bundle bundle = ourClient
.search()
.forResource(Observation.class)
.where(Observation.SUBJECT.hasId("Patient/p1"))
.returnBundle(Bundle.class)
.execute();
List<String> ids = toUnqualifiedVersionlessIdValues(bundle);
assertThat(ids, containsInAnyOrder("Observation/o1", "Observation/o2"));
} finally {
ourRestServer.unregisterInterceptor(interceptor);
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();

View File

@ -0,0 +1,27 @@
package ca.uhn.fhir.jpa.term;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.Interceptor;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import org.springframework.beans.factory.annotation.Autowired;
@Interceptor
public class ParserConfigInterceptor {
@Autowired
private FhirContext myContext;
@Hook(Pointcut.INTERCEPTOR_REGISTERED)
public void start() {
myContext.getParserOptions().setStripVersionsFromReferences(false);
IGenericClient client = FhirContext.forR4().newRestfulGenericClient("aa");
client.read().resource("Patient").withIdAndVersion("100", "25").execute();
}
}