Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for historical data sorting #4244

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Handles common syntax for generating DDL
*/
public class DataDefinitionUtil {
private static final String NAME_PATTERN_RGX = "[a-zA-Z_][-\\w]*$";
private static final String NAME_PATTERN_RGX = "[a-zA-Z_][\\w]*$";
private static final Pattern NAME_PATTERN = Pattern.compile(NAME_PATTERN_RGX);

/**
Expand Down
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
Loading