Deal with max lenghts in search params

This commit is contained in:
James 2014-07-07 12:15:20 -04:00
parent 7f9cc38bde
commit 46828030db
6 changed files with 155 additions and 49 deletions

View File

@ -796,12 +796,12 @@ public class RestfulServer extends HttpServlet {
if (theContext.getNarrativeGenerator() != null) {
String title = theContext.getNarrativeGenerator().generateTitle(next);
ourLog.info("Narrative generator created title: {}", title);
ourLog.trace("Narrative generator created title: {}", title);
if (StringUtils.isNotBlank(title)) {
ResourceMetadataKeyEnum.TITLE.put(next, title);
}
} else {
ourLog.info("No narrative generator specified");
ourLog.trace("No narrative generator specified");
}
bundle.addResource(next, theContext, theServerBase);

View File

@ -469,7 +469,12 @@ public abstract class BaseFhirDao {
if (nextObject instanceof IPrimitiveDatatype<?>) {
IPrimitiveDatatype<?> nextValue = (IPrimitiveDatatype<?>) nextObject;
ResourceIndexedSearchParamString nextEntity = new ResourceIndexedSearchParamString(resourceName, normalizeString(nextValue.getValueAsString()), nextValue.getValueAsString());
String searchTerm = nextValue.getValueAsString();
if (searchTerm.length() > ResourceIndexedSearchParamString.MAX_LENGTH) {
searchTerm=searchTerm.substring(0, ResourceIndexedSearchParamString.MAX_LENGTH);
}
ResourceIndexedSearchParamString nextEntity = new ResourceIndexedSearchParamString(resourceName, normalizeString(searchTerm), searchTerm);
nextEntity.setResource(theEntity);
retVal.add(nextEntity);
} else {
@ -544,31 +549,32 @@ public abstract class BaseFhirDao {
}
List<Object> values = t.getValues(theResource, nextPath);
List<String> systems = new ArrayList<String>();
List<String> codes = new ArrayList<String>();
for (Object nextObject : values) {
ResourceIndexedSearchParamToken nextEntity;
if (nextObject instanceof IdentifierDt) {
IdentifierDt nextValue = (IdentifierDt) nextObject;
if (nextValue.isEmpty()) {
continue;
}
nextEntity = new ResourceIndexedSearchParamToken(nextSpDef.getName(), nextValue.getSystem().getValueAsString(), nextValue.getValue().getValue());
systems.add( nextValue.getSystem().getValueAsString());
codes.add( nextValue.getValue().getValue());
} else if (nextObject instanceof IPrimitiveDatatype<?>) {
IPrimitiveDatatype<?> nextValue = (IPrimitiveDatatype<?>) nextObject;
if (nextValue.isEmpty()) {
continue;
}
nextEntity = new ResourceIndexedSearchParamToken(nextSpDef.getName(), null, nextValue.getValueAsString());
systems.add(null);
codes .add (nextValue.getValueAsString());
} else if (nextObject instanceof CodeableConceptDt) {
CodeableConceptDt nextCC = (CodeableConceptDt) nextObject;
for (CodingDt nextCoding : nextCC.getCoding()) {
if (nextCoding.isEmpty()) {
continue;
}
nextEntity = new ResourceIndexedSearchParamToken(nextSpDef.getName(), nextCoding.getSystem().getValueAsString(), nextCoding.getCode().getValue());
nextEntity.setResource(theEntity);
retVal.add(nextEntity);
systems.add(nextCoding.getSystem().getValueAsString());
codes.add( nextCoding.getCode().getValue());
}
nextEntity = null;
} else {
if (!multiType) {
throw new ConfigurationException("Search param " + nextSpDef.getName() + " is of unexpected datatype: " + nextObject.getClass());
@ -576,11 +582,27 @@ public abstract class BaseFhirDao {
continue;
}
}
if (nextEntity != null) {
}
assert systems.size() == codes.size();
for (int i = 0; i < systems.size(); i++) {
String system = systems.get(i);
String code = codes.get(i);
if (system != null && system.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) {
system = system.substring(0, ResourceIndexedSearchParamToken.MAX_LENGTH);
}
if (code != null && code.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) {
code = code.substring(0, ResourceIndexedSearchParamToken.MAX_LENGTH);
}
ResourceIndexedSearchParamToken nextEntity;
nextEntity = new ResourceIndexedSearchParamToken(nextSpDef.getName(), system, code);
nextEntity.setResource(theEntity);
retVal.add(nextEntity);
}
}
}
theEntity.setParamsTokenPopulated(retVal.size() > 0);

View File

@ -366,20 +366,25 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
for (IQueryParameterType nextOr : theOrParams) {
IQueryParameterType params = nextOr;
String string;
String rawSearchTerm;
if (params instanceof IPrimitiveDatatype<?>) {
IPrimitiveDatatype<?> id = (IPrimitiveDatatype<?>) params;
string = id.getValueAsString();
rawSearchTerm = id.getValueAsString();
} else {
throw new IllegalArgumentException("Invalid token type: " + params.getClass());
}
String likeExpression = normalizeString(string);
if (rawSearchTerm.length() > ResourceIndexedSearchParamString.MAX_LENGTH) {
throw new InvalidRequestException("Parameter[" + theParamName + "] has length (" + rawSearchTerm.length() + ") that is longer than maximum allowed ("
+ ResourceIndexedSearchParamString.MAX_LENGTH + "): " + rawSearchTerm);
}
String likeExpression = normalizeString(rawSearchTerm);
likeExpression = likeExpression.replace("%", "[%]") + "%";
Predicate singleCode = builder.like(from.get("myValueNormalized").as(String.class), likeExpression);
if (params instanceof StringParam && ((StringParam) params).isExact()) {
Predicate exactCode = builder.equal(from.get("myValueExact"), string);
Predicate exactCode = builder.equal(from.get("myValueExact"), rawSearchTerm);
singleCode = builder.and(singleCode, exactCode);
}
codePredicates.add(singleCode);
@ -428,6 +433,15 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
throw new IllegalArgumentException("Invalid token type: " + params.getClass());
}
if (system != null && system.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) {
throw new InvalidRequestException("Parameter[" + theParamName + "] has system (" + system.length() + ") that is longer than maximum allowed ("
+ ResourceIndexedSearchParamToken.MAX_LENGTH + "): " + system);
}
if (code != null && code.length() > ResourceIndexedSearchParamToken.MAX_LENGTH) {
throw new InvalidRequestException("Parameter[" + theParamName + "] has code (" + code.length() + ") that is longer than maximum allowed (" + ResourceIndexedSearchParamToken.MAX_LENGTH
+ "): " + code);
}
ArrayList<Predicate> singleCodePredicates = (new ArrayList<Predicate>());
if (StringUtils.isNotBlank(system)) {
singleCodePredicates.add(builder.equal(from.get("mySystem"), system));
@ -470,10 +484,10 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
CriteriaQuery<Long> cq = builder.createQuery(Long.class);
Root<ResourceTable> from = cq.from(ResourceTable.class);
cq.select(from.get("myId").as(Long.class));
Predicate typePredicate = builder.equal(from.get("myResourceType"), myResourceName);
Predicate idPrecidate = from.get("myId").in(thePids);
cq.where(builder.and(typePredicate, idPrecidate));
TypedQuery<Long> q = myEntityManager.createQuery(cq);
@ -485,8 +499,6 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
return found;
}
@Override
public void addTag(IdDt theId, String theScheme, String theTerm, String theLabel) {
BaseHasResource entity = readEntity(theId);
@ -554,7 +566,8 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
final T current = currentTmp;
String querySring = "SELECT count(h) FROM ResourceHistoryTable h " + "WHERE h.myResourceId = :PID AND h.myResourceType = :RESTYPE" + " AND h.myUpdated < :END" + (theSince != null ? " AND h.myUpdated >= :SINCE" : "");
String querySring = "SELECT count(h) FROM ResourceHistoryTable h " + "WHERE h.myResourceId = :PID AND h.myResourceType = :RESTYPE" + " AND h.myUpdated < :END"
+ (theSince != null ? " AND h.myUpdated >= :SINCE" : "");
TypedQuery<Long> countQuery = myEntityManager.createQuery(querySring, Long.class);
countQuery.setParameter("PID", theId.getIdPartAsLong());
countQuery.setParameter("RESTYPE", resourceType);
@ -592,8 +605,9 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
retVal.add(current);
}
TypedQuery<ResourceHistoryTable> q = myEntityManager.createQuery("SELECT h FROM ResourceHistoryTable h WHERE h.myResourceId = :PID AND h.myResourceType = :RESTYPE AND h.myUpdated < :END " + (theSince != null ? " AND h.myUpdated >= :SINCE" : "")
+ " ORDER BY h.myUpdated ASC", ResourceHistoryTable.class);
TypedQuery<ResourceHistoryTable> q = myEntityManager.createQuery(
"SELECT h FROM ResourceHistoryTable h WHERE h.myResourceId = :PID AND h.myResourceType = :RESTYPE AND h.myUpdated < :END "
+ (theSince != null ? " AND h.myUpdated >= :SINCE" : "") + " ORDER BY h.myUpdated ASC", ResourceHistoryTable.class);
q.setParameter("PID", theId.getIdPartAsLong());
q.setParameter("RESTYPE", resourceType);
q.setParameter("END", end.getValue(), TemporalType.TIMESTAMP);
@ -639,7 +653,8 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
throw new ConfigurationException("Unknown search param on resource[" + myResourceName + "] for secondary key[" + mySecondaryPrimaryKeyParamName + "]");
}
if (sp.getParamType() != SearchParamTypeEnum.TOKEN) {
throw new ConfigurationException("Search param on resource[" + myResourceName + "] for secondary key[" + mySecondaryPrimaryKeyParamName + "] is not a token type, only token is supported");
throw new ConfigurationException("Search param on resource[" + myResourceName + "] for secondary key[" + mySecondaryPrimaryKeyParamName
+ "] is not a token type, only token is supported");
}
}
@ -670,7 +685,8 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
if (entity == null) {
if (theId.hasVersionIdPart()) {
TypedQuery<ResourceHistoryTable> q = myEntityManager.createQuery("SELECT t from ResourceHistoryTable t WHERE t.myResourceId = :RID AND t.myResourceType = :RTYP AND t.myResourceVersion = :RVER", ResourceHistoryTable.class);
TypedQuery<ResourceHistoryTable> q = myEntityManager.createQuery(
"SELECT t from ResourceHistoryTable t WHERE t.myResourceId = :RID AND t.myResourceType = :RTYP AND t.myResourceVersion = :RVER", ResourceHistoryTable.class);
q.setParameter("RID", theId.getIdPartAsLong());
q.setParameter("RTYP", myResourceName);
q.setParameter("RVER", theId.getVersionIdPartAsLong());
@ -882,7 +898,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
if (pids.isEmpty()) {
return new HashSet<Long>();
}
if (pids.isEmpty()) {
pids.addAll(joinPids);
} else {
@ -946,8 +962,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
}
/**
* If set, the given param will be treated as a secondary primary key, and multiple resources will not be able to
* share the same value.
* If set, the given param will be treated as a secondary primary key, and multiple resources will not be able to share the same value.
*/
public void setSecondaryPrimaryKeyParamName(String theSecondaryPrimaryKeyParamName) {
mySecondaryPrimaryKeyParamName = theSecondaryPrimaryKeyParamName;

View File

@ -4,15 +4,19 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.apache.commons.lang3.StringUtils;
@Entity
@Table(name = "HFJ_SPIDX_STRING"/*, indexes= {@Index(name="IDX_SP_STRING", columnList="SP_VALUE_NORMALIZED")}*/)
@org.hibernate.annotations.Table(appliesTo="HFJ_SPIDX_STRING",indexes= {
@org.hibernate.annotations.Index(name="IDX_SP_STRING", columnNames= {"RES_TYPE", "SP_NAME", "SP_VALUE_NORMALIZED"})})
public class ResourceIndexedSearchParamString extends BaseResourceIndexedSearchParam {
public static final int MAX_LENGTH = 100;
private static final long serialVersionUID = 1L;
@Column(name = "SP_VALUE_NORMALIZED", length = 100, nullable = true)
@Column(name = "SP_VALUE_NORMALIZED", length = MAX_LENGTH, nullable = true)
public String myValueNormalized;
@Column(name="SP_VALUE_EXACT",length=100,nullable=true)
@ -32,6 +36,9 @@ public class ResourceIndexedSearchParamString extends BaseResourceIndexedSearchP
}
public void setValueNormalized(String theValueNormalized) {
if (StringUtils.defaultString(theValueNormalized).length() > MAX_LENGTH) {
throw new IllegalArgumentException("Value is too long: " + theValueNormalized.length());
}
myValueNormalized = theValueNormalized;
}
@ -40,6 +47,9 @@ public class ResourceIndexedSearchParamString extends BaseResourceIndexedSearchP
}
public void setValueExact(String theValueExact) {
if (StringUtils.defaultString(theValueExact).length() > MAX_LENGTH) {
throw new IllegalArgumentException("Value is too long: " + theValueExact.length());
}
myValueExact = theValueExact;
}

View File

@ -14,9 +14,11 @@ import org.apache.commons.lang3.StringUtils;
})
public class ResourceIndexedSearchParamToken extends BaseResourceIndexedSearchParam {
public static final int MAX_LENGTH = 100;
private static final long serialVersionUID = 1L;
@Column(name = "SP_SYSTEM", nullable = true, length = 100)
@Column(name = "SP_SYSTEM", nullable = true, length = MAX_LENGTH)
public String mySystem;
@Column(name = "SP_VALUE", nullable = true, length = 100)

View File

@ -10,6 +10,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.RandomStringUtils;
import org.hamcrest.core.StringContains;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@ -17,6 +18,7 @@ import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamString;
import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
@ -40,6 +42,7 @@ import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.exceptions.FhirClientConnectionException;
import ca.uhn.fhir.rest.gclient.IQuery;
import ca.uhn.fhir.rest.gclient.TokenParam;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.IdentifierListParam;
import ca.uhn.fhir.rest.param.QualifiedDateParam;
@ -66,7 +69,7 @@ public class FhirResourceDaoTest {
@Test
public void testOrganizationName() {
//@formatter:off
String inputStr = "{\"resourceType\":\"Organization\",\n" +
" \"extension\":[\n" +
@ -95,17 +98,78 @@ public class FhirResourceDaoTest {
Set<Long> val = ourOrganizationDao.searchForIds("name", new StringParam("P"));
int initial = val.size();
Organization org = ourFhirCtx.newJsonParser().parseResource(Organization.class,inputStr);
Organization org = ourFhirCtx.newJsonParser().parseResource(Organization.class, inputStr);
ourOrganizationDao.create(org);
val = ourOrganizationDao.searchForIds("name", new StringParam("P"));
assertEquals(initial+1, val.size());
assertEquals(initial + 1, val.size());
}
@Test
public void testStringParamWhichIsTooLong() {
Organization org = new Organization();
String str = "testStringParamLong__lvdaoy843s89tll8gvs89l4s3gelrukveilufyebrew8r87bv4b77feli7fsl4lv3vb7rexloxe7olb48vov4o78ls7bvo7vb48o48l4bb7vbvx";
str = str + str;
org.getName().setValue(str);
assertThat(str.length(), greaterThan(ResourceIndexedSearchParamString.MAX_LENGTH));
Set<Long> val = ourOrganizationDao.searchForIds("name", new StringParam("P"));
int initial = val.size();
ourOrganizationDao.create(org);
val = ourOrganizationDao.searchForIds("name", new StringParam("P"));
assertEquals(initial + 0, val.size());
val = ourOrganizationDao.searchForIds("name", new StringParam(str.substring(0, ResourceIndexedSearchParamString.MAX_LENGTH)));
assertEquals(initial + 1, val.size());
try {
ourOrganizationDao.searchForIds("name", new StringParam(str.substring(0, ResourceIndexedSearchParamString.MAX_LENGTH + 1)));
fail();
} catch (InvalidRequestException e) {
// ok
}
}
@Test
public void testTokenParamWhichIsTooLong() {
String longStr1 = RandomStringUtils.randomAlphanumeric(ResourceIndexedSearchParamString.MAX_LENGTH + 100);
String longStr2 = RandomStringUtils.randomAlphanumeric(ResourceIndexedSearchParamString.MAX_LENGTH + 100);
Organization org = new Organization();
org.getName().setValue("testTokenParamWhichIsTooLong");
org.getType().addCoding().setSystem(longStr1).setCode(longStr2);
String subStr1 = longStr1.substring(0, ResourceIndexedSearchParamString.MAX_LENGTH);
String subStr2 = longStr2.substring(0, ResourceIndexedSearchParamString.MAX_LENGTH);
Set<Long> val = ourOrganizationDao.searchForIds("type", new IdentifierDt(subStr1, subStr2));
int initial = val.size();
ourOrganizationDao.create(org);
val = ourOrganizationDao.searchForIds("type", new IdentifierDt(subStr1, subStr2));
assertEquals(initial + 1, val.size());
try {
ourOrganizationDao.searchForIds("type", new IdentifierDt(longStr1, subStr2));
fail();
} catch (InvalidRequestException e) {
// ok
}
try {
ourOrganizationDao.searchForIds("type", new IdentifierDt(subStr1, longStr2));
fail();
} catch (InvalidRequestException e) {
// ok
}
}
@Test
public void testStoreUnversionedResources() {
Organization o1 = new Organization();
@ -167,7 +231,7 @@ public class FhirResourceDaoTest {
assertEquals(2, retrieved.size());
}
}
@Test
public void testIdParam() {
Patient patient = new Patient();
@ -303,7 +367,7 @@ public class FhirResourceDaoTest {
for (IResource next : value.getResources(0, value.size())) {
ourDeviceDao.delete(next.getId());
}
value = ourDeviceDao.search(new SearchParameterMap());
assertEquals(0, value.size());
@ -312,7 +376,6 @@ public class FhirResourceDaoTest {
}
@Test
public void testPersistSearchParamQuantity() {
Observation obs = new Observation();
@ -329,8 +392,6 @@ public class FhirResourceDaoTest {
}
@Test
public void testPersistSearchParams() {
Patient patient = new Patient();
@ -447,9 +508,7 @@ public class FhirResourceDaoTest {
assertEquals(0, patients.size());
}
@Test
public void testSearchByIdParam() {
IdDt id1;
@ -465,7 +524,6 @@ public class FhirResourceDaoTest {
id2 = ourOrganizationDao.create(patient).getId();
}
Map<String, IQueryParameterType> params = new HashMap<String, IQueryParameterType>();
params.put("_id", new StringDt(id1.getIdPart()));
assertEquals(1, toList(ourPatientDao.search(params)).size());
@ -477,7 +535,6 @@ public class FhirResourceDaoTest {
assertEquals(0, toList(ourPatientDao.search(params)).size());
}
@Test
public void testSearchResourceLinkWithChain() {