Skip to content

Commit

Permalink
Add support for historical data sorting
Browse files Browse the repository at this point in the history
This update allows sorting of historical data based on the '_sort' parameter. A new method, 'getHistorySortOrder', validates the provided _sort parameter and a new field, 'historySortOrder', has been added to the FHIRHistoryContext class. Additionally, the error message for invalid _sort parameter has been improved.

Signed-off-by: Berkant KARDUMAN <berkant.karduman@tigahealth.com>
  • Loading branch information
berkant-k committed Jan 6, 2024
1 parent 8429bf7 commit 94a7ab7
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Map;

import org.linuxforhealth.fhir.database.utils.query.Select;
import org.linuxforhealth.fhir.persistence.HistorySortOrder;
import org.linuxforhealth.fhir.persistence.context.FHIRPersistenceContext;
import org.linuxforhealth.fhir.persistence.exception.FHIRPersistenceDataAccessException;
import org.linuxforhealth.fhir.persistence.exception.FHIRPersistenceException;
Expand Down Expand Up @@ -60,7 +61,7 @@ Resource versionRead(String logicalId, String resourceType, int versionId)
* @throws FHIRPersistenceDataAccessException
* @throws FHIRPersistenceDBConnectException
*/
List<Resource> history(String resourceType, String logicalId, Timestamp fromDateTime, int offset, int maxResults)
List<Resource> history(String resourceType, String logicalId, Timestamp fromDateTime, int offset, int maxResults, HistorySortOrder historySortOrder)
throws FHIRPersistenceDataAccessException, FHIRPersistenceDBConnectException;

/**
Expand Down Expand Up @@ -184,8 +185,8 @@ List<Resource> search(String sqlSelect)
* @throws FHIRPersistenceVersionIdMismatchException
* @throws FHIRPersistenceException
*/
Resource insert(Resource resource, List<ExtractedParameterValue> parameters, String parameterHashB64, ParameterDAO parameterDao,
Integer ifNoneMatch)
Resource insert(Resource resource, List<ExtractedParameterValue> parameters, String parameterHashB64, ParameterDAO parameterDao,
Integer ifNoneMatch)
throws FHIRPersistenceException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.linuxforhealth.fhir.database.utils.common.CalendarHelper;
import org.linuxforhealth.fhir.database.utils.query.QueryUtil;
import org.linuxforhealth.fhir.database.utils.query.Select;
import org.linuxforhealth.fhir.persistence.HistorySortOrder;
import org.linuxforhealth.fhir.persistence.context.FHIRPersistenceContext;
import org.linuxforhealth.fhir.persistence.exception.FHIRPersistenceDataAccessException;
import org.linuxforhealth.fhir.persistence.exception.FHIRPersistenceException;
Expand Down Expand Up @@ -80,7 +81,7 @@ public abstract class ResourceDAOImpl extends FHIRDbDAOImpl implements ResourceD
"SELECT R.RESOURCE_ID, R.LOGICAL_RESOURCE_ID, R.VERSION_ID, R.LAST_UPDATED, R.IS_DELETED, R.DATA, LR.LOGICAL_ID, R.RESOURCE_PAYLOAD_KEY " +
"FROM %s_RESOURCES R, %s_LOGICAL_RESOURCES LR WHERE " +
"LR.LOGICAL_ID = ? AND R.LOGICAL_RESOURCE_ID = LR.LOGICAL_RESOURCE_ID " +
"ORDER BY R.VERSION_ID DESC ";
"ORDER BY R.VERSION_ID %s ";

// Count the number of versions we have for the resource identified by its logical-id
private static final String SQL_HISTORY_COUNT = "SELECT COUNT(R.VERSION_ID) FROM %s_RESOURCES R, %s_LOGICAL_RESOURCES LR WHERE LR.LOGICAL_ID = ? AND " +
Expand All @@ -90,7 +91,7 @@ public abstract class ResourceDAOImpl extends FHIRDbDAOImpl implements ResourceD
"SELECT R.RESOURCE_ID, R.LOGICAL_RESOURCE_ID, R.VERSION_ID, R.LAST_UPDATED, R.IS_DELETED, R.DATA, LR.LOGICAL_ID, R.RESOURCE_PAYLOAD_KEY " +
"FROM %s_RESOURCES R, %s_LOGICAL_RESOURCES LR WHERE " +
"LR.LOGICAL_ID = ? AND R.LAST_UPDATED >= ? AND R.LOGICAL_RESOURCE_ID = LR.LOGICAL_RESOURCE_ID " +
"ORDER BY R.VERSION_ID DESC ";
"ORDER BY R.VERSION_ID %s ";

private static final String SQL_HISTORY_FROM_DATETIME_COUNT =
"SELECT COUNT(R.VERSION_ID) FROM %s_RESOURCES R, %s_LOGICAL_RESOURCES LR WHERE LR.LOGICAL_ID = ? AND " +
Expand Down Expand Up @@ -147,7 +148,7 @@ public abstract class ResourceDAOImpl extends FHIRDbDAOImpl implements ResourceD
* @param trxSyncRegistry
*/
public ResourceDAOImpl(Connection c, String schemaName, FHIRDbFlavor flavor, TransactionSynchronizationRegistry trxSynchRegistry,
FHIRPersistenceJDBCCache cache, ParameterTransactionDataImpl ptdi) {
FHIRPersistenceJDBCCache cache, ParameterTransactionDataImpl ptdi) {
super(c, schemaName, flavor);
this.runningInTrx = true;
this.trxSynchRegistry = trxSynchRegistry;
Expand Down Expand Up @@ -251,7 +252,7 @@ protected Resource createDTO(ResultSet resultSet, boolean hasResourceTypeId) thr
resource.setVersionId(resultSet.getInt(IDX_VERSION_ID));
resource.setDeleted(resultSet.getString(IDX_IS_DELETED).equals("Y") ? true : false);
resource.setResourcePayloadKey(resultSet.getString(IDX_RESOURCE_PAYLOAD_KEY));

if (hasResourceTypeId) {
resource.setResourceTypeId(resultSet.getInt(IDX_RESOURCE_TYPE_ID));
}
Expand All @@ -266,20 +267,27 @@ protected Resource createDTO(ResultSet resultSet, boolean hasResourceTypeId) thr
}

@Override
public List<Resource> history(String resourceType, String logicalId, Timestamp fromDateTime, int offset, int maxResults) throws FHIRPersistenceDataAccessException, FHIRPersistenceDBConnectException {
public List<Resource> history(String resourceType, String logicalId, Timestamp fromDateTime, int offset, int maxResults, HistorySortOrder sortOrder) throws FHIRPersistenceDataAccessException, FHIRPersistenceDBConnectException {
final String METHODNAME = "history";
log.entering(CLASSNAME, METHODNAME);

List<Resource> resources = null;
String stmtString = null;

try {
final String sortDirection;
if(HistorySortOrder.ASC_LAST_UPDATED.equals(sortOrder)){
sortDirection="Asc";
}else{
sortDirection="Desc";
}

if (fromDateTime != null) {
stmtString = String.format(SQL_HISTORY_FROM_DATETIME, resourceType, resourceType);
stmtString = String.format(SQL_HISTORY_FROM_DATETIME, resourceType, resourceType,sortDirection);
stmtString = stmtString + DERBY_PAGINATION_PARMS;
resources = this.runQuery(stmtString, logicalId, fromDateTime, offset, maxResults);
} else {
stmtString = String.format(SQL_HISTORY, resourceType, resourceType);
stmtString = String.format(SQL_HISTORY, resourceType, resourceType,sortDirection);
stmtString = stmtString + DERBY_PAGINATION_PARMS;
resources = this.runQuery(stmtString, logicalId, offset, maxResults);
}
Expand Down Expand Up @@ -409,7 +417,7 @@ protected Integer getResourceTypeId(String resourceType) throws FHIRPersistenceE
}
// cache miss, so read from the database
resourceTypeId = this.readResourceTypeId(resourceType);

if (resourceTypeId != null) {
cache.getResourceTypeCache().addEntry(resourceType, resourceTypeId);
cache.getResourceTypeNameCache().addEntry(resourceTypeId, resourceType);
Expand Down Expand Up @@ -553,7 +561,7 @@ protected FHIRPersistenceJDBCCache getCache() {
@Override
public int searchCount(Select countQuery) throws FHIRPersistenceDataAccessException, FHIRPersistenceDBConnectException {
return runCountQuery(countQuery);
}
}

@Override
public List<Resource> search(Select select) throws FHIRPersistenceDataAccessException, FHIRPersistenceDBConnectException {
Expand Down
Loading

0 comments on commit 94a7ab7

Please sign in to comment.