From 8b223a1fd9d7a34f22fdb688472ae59db524e93e Mon Sep 17 00:00:00 2001 From: Tadgh Date: Tue, 3 Mar 2020 16:07:25 -0800 Subject: [PATCH] Rework selection generation of calculator. Add null check for empty dates --- .../migrate/taskdef/BaseColumnCalculatorTask.java | 6 +++--- .../jpa/migrate/taskdef/BaseTableColumnTask.java | 14 +++++++++++++- .../fhir/jpa/migrate/taskdef/BaseTableTask.java | 3 ++- .../migrate/taskdef/CalculateOrdinalDatesTask.java | 4 +++- .../entity/ResourceIndexedSearchParamDate.java | 3 +++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseColumnCalculatorTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseColumnCalculatorTask.java index ddd5300bfcb..909d830bec3 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseColumnCalculatorTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseColumnCalculatorTask.java @@ -20,7 +20,6 @@ package ca.uhn.fhir.jpa.migrate.taskdef; * #L% */ -import ca.uhn.fhir.jpa.migrate.JdbcUtils; import ca.uhn.fhir.util.StopWatch; import ca.uhn.fhir.util.VersionEnum; import com.google.common.collect.ForwardingMap; @@ -77,8 +76,9 @@ public abstract class BaseColumnCalculatorTask extends BaseTableColumnTask { JdbcTemplate jdbcTemplate = newJdbcTemplate(); jdbcTemplate.setMaxRows(100000); - String sql = "SELECT * FROM " + getTableName() + " WHERE " + getColumnName() + " IS NULL"; - logInfo(ourLog, "Finding up to {} rows in {} that requires calculations", myBatchSize, getTableName()); + + String sql = "SELECT * FROM " + getTableName() + " WHERE " + getWhereClause(); + logInfo(ourLog, "Finding up to {} rows in {} that requires calculations, using query: {}", myBatchSize, getTableName(), sql); jdbcTemplate.query(sql, rch); rch.done(); diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTask.java index e4e024ed1e1..dc38d081ad4 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTask.java @@ -30,6 +30,8 @@ import java.util.Locale; public abstract class BaseTableColumnTask> extends BaseTableTask { private String myColumnName; + //If a concrete class decides to, they can define a custom WHERE clause for the task. + private String myWhereClause; public BaseTableColumnTask(String theProductVersion, String theSchemaVersion) { super(theProductVersion, theSchemaVersion); @@ -41,11 +43,21 @@ public abstract class BaseTableColumnTask> extends Ba return (T) this; } - public String getColumnName() { return myColumnName; } + protected void setWhereClause(String theWhereClause) { + this.myWhereClause = theWhereClause; + } + protected String getWhereClause() { + if (myWhereClause == null) { + return getColumnName() + " IS NULL"; + } else { + return myWhereClause; + } + } + @Override public void validate() { super.validate(); diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableTask.java index 3fce1b54fa3..73def6f5bc0 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableTask.java @@ -27,6 +27,8 @@ import org.apache.commons.lang3.builder.HashCodeBuilder; public abstract class BaseTableTask> extends BaseTask { private String myTableName; + + public BaseTableTask(String theProductVersion, String theSchemaVersion) { super(theProductVersion, theSchemaVersion); } @@ -34,7 +36,6 @@ public abstract class BaseTableTask> extends BaseTask public String getTableName() { return myTableName; } - public T setTableName(String theTableName) { Validate.notBlank(theTableName); myTableName = theTableName; diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateOrdinalDatesTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateOrdinalDatesTask.java index d0d16c1e8d4..29371cc1c6d 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateOrdinalDatesTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateOrdinalDatesTask.java @@ -26,8 +26,10 @@ public class CalculateOrdinalDatesTask extends BaseColumnCalculatorTask { public CalculateOrdinalDatesTask(VersionEnum theRelease, String theVersion) { super(theRelease, theVersion); - setDescription("Calculate SP_LOW_VALUE_DATE and SP_HIGH_VALUE_DATE based on existing SP_LOW and SP_HIGH date values in Date Search Params"); + setDescription("Calculate SP_LOW_VALUE_DATE_ORDINAL and SP_HIGH_VALUE_DATE_ORDINAL based on existing SP_VALUE_LOW and SP_VALUE_HIGH date values in Date Search Params"); + setWhereClause("SP_VALUE_LOW_DATE_ORDINAL IS NULL AND SP_VALUE_LOW IS NOT NULL) OR (SP_VALUE_HIGH_DATE_ORDINAL IS NULL AND SP_VALUE_HIGH IS NOT NULL"); } + @Override protected boolean shouldSkipTask() { return false; // TODO Is there a case where we should just not do this? diff --git a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamDate.java b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamDate.java index 93e90200079..8ef1d524fe2 100644 --- a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamDate.java +++ b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamDate.java @@ -298,6 +298,9 @@ public class ResourceIndexedSearchParamDate extends BaseResourceIndexedSearchPar public static Long calculateOrdinalValue(Date theDate) { + if (theDate == null) { + return null; + } return (long) DateUtils.convertDatetoDayInteger(theDate); }