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

Issue/175 Template Engine for HTML UIs #180

Merged
merged 12 commits into from
Feb 21, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ private ProcessDefinition getProcessDefinition(String processDomain, String proc
if (processVersion != null && !processVersion.isBlank())
return repositoryService.createProcessDefinitionQuery().active()
.processDefinitionKey(processDomain + "_" + processDefinitionKey).versionTag(processVersion).list()
.stream().sorted(Comparator.comparing(ProcessDefinition::getVersion).reversed()).findFirst()
.orElse(null);
.stream().max(Comparator.comparing(ProcessDefinition::getVersion)).orElse(null);
else
return repositoryService.createProcessDefinitionQuery().active()
.processDefinitionKey(processDomain + "_" + processDefinitionKey).latestVersion().singleResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Consumer;

Expand Down Expand Up @@ -101,7 +102,8 @@ private ProcessDefinition getProcessDefinition(String processDefinitionKey, Stri
{
if (versionTag != null && !versionTag.isBlank())
return repositoryService.createProcessDefinitionQuery().processDefinitionKey(processDefinitionKey)
.versionTag(versionTag).singleResult();
.versionTag(versionTag).list().stream().max(Comparator.comparing(ProcessDefinition::getVersion))
.orElse(null);
else
return repositoryService.createProcessDefinitionQuery().processDefinitionKey(processDefinitionKey)
.latestVersion().singleResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private List<ProcessEntry> processes()
return repositoryService.createProcessDefinitionQuery().active().unlimitedList().stream()
.map(def -> new ProcessEntry("Process/" + def.getKey() + "/" + def.getVersionTag(),
def.getKey() + " | " + def.getVersionTag()))
.sorted(Comparator.comparing(ProcessEntry::value)).toList();
.sorted(Comparator.comparing(ProcessEntry::value)).distinct().toList();
}

private List<String> processInstances()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ protected void doSetVariables(BiConsumer<String, Object> variables, Bundle resou

int currentPage = getPage(params);
int count = getCount(params, defaultPageCount);
int maxPages = (int) Math.ceil((double) resource.getTotal() / count);
int maxPages = count <= 0 ? 0 : (int) Math.ceil((double) resource.getTotal() / count);
int offset = Math.multiplyExact(currentPage - 1, count);
int firstResource = Integer.MAX_VALUE - 1 < offset ? 0 : offset + 1;
int firstResource = count == 0 ? 0 : Integer.MAX_VALUE - 1 < offset ? 0 : offset + 1;
int lastResource = Integer.MAX_VALUE - matchResources.size() < offset ? 0 : offset + matchResources.size();

long includeResources = resource.getEntry().stream().filter(
Expand Down Expand Up @@ -142,11 +142,11 @@ private int getPage(MultivaluedMap<String, String> params)
}
catch (NumberFormatException e)
{
return 1;
return 0;
}
}
else
return 1;
return 0;
}

private int getCount(MultivaluedMap<String, String> params, int defaultPageCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import dev.dsf.fhir.dao.ResourceDao;
import dev.dsf.fhir.dao.provider.DaoProvider;
import dev.dsf.fhir.help.ParameterConverter;
import dev.dsf.fhir.search.PageAndCount;
import dev.dsf.fhir.search.PartialResult;
import dev.dsf.fhir.search.SearchQuery;
import dev.dsf.fhir.search.SearchQueryParameterError;
Expand Down Expand Up @@ -189,7 +190,7 @@ protected final boolean organizationWithIdentifierExists(Connection connection,
Map<String, List<String>> queryParameters = Map.of("identifier",
Collections.singletonList(iSystem + "|" + iValue));
OrganizationDao dao = daoProvider.getOrganizationDao();
SearchQuery<Organization> query = dao.createSearchQueryWithoutUserFilter(0, 0)
SearchQuery<Organization> query = dao.createSearchQueryWithoutUserFilter(PageAndCount.exists())
.configureParameters(queryParameters);

List<SearchQueryParameterError> uQp = query.getUnsupportedQueryParameters();
Expand Down Expand Up @@ -223,7 +224,7 @@ protected final boolean roleExists(Connection connection, Coding coding)
Map<String, List<String>> queryParameters = Map.of("url",
Collections.singletonList(cSystem + (coding.hasVersion() ? "|" + cVersion : "")));
CodeSystemDao dao = daoProvider.getCodeSystemDao();
SearchQuery<CodeSystem> query = dao.createSearchQueryWithoutUserFilter(1, 1)
SearchQuery<CodeSystem> query = dao.createSearchQueryWithoutUserFilter(PageAndCount.single())
.configureParameters(queryParameters);

List<SearchQueryParameterError> uQp = query.getUnsupportedQueryParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import dev.dsf.fhir.dao.EndpointDao;
import dev.dsf.fhir.dao.provider.DaoProvider;
import dev.dsf.fhir.help.ParameterConverter;
import dev.dsf.fhir.search.PageAndCount;
import dev.dsf.fhir.search.PartialResult;
import dev.dsf.fhir.search.SearchQuery;
import dev.dsf.fhir.search.SearchQueryParameterError;
Expand All @@ -31,7 +32,7 @@ public class EndpointAuthorizationRule extends AbstractMetaTagAuthorizationRule<
private static final Logger logger = LoggerFactory.getLogger(EndpointAuthorizationRule.class);

private static final String ENDPOINT_IDENTIFIER_SYSTEM = "http://dsf.dev/sid/endpoint-identifier";
private static final String ENDPOINT_ADDRESS_PATTERN_STRING = "https://([0-9a-zA-Z\\.-]+)+(:\\d{1,4})?([-\\w/]*)";
private static final String ENDPOINT_ADDRESS_PATTERN_STRING = "https://[0-9a-zA-Z.-]+(?::\\d{1,4})?[-\\w/]*";
private static final Pattern ENDPOINT_ADDRESS_PATTERN = Pattern.compile(ENDPOINT_ADDRESS_PATTERN_STRING);

public EndpointAuthorizationRule(DaoProvider daoProvider, String serverBase, ReferenceResolver referenceResolver,
Expand Down Expand Up @@ -111,7 +112,8 @@ private boolean endpointWithAddressExists(Connection connection, String address)
{
Map<String, List<String>> queryParameters = Map.of("address", Collections.singletonList(address));
EndpointDao dao = getDao();
SearchQuery<Endpoint> query = dao.createSearchQueryWithoutUserFilter(0, 0).configureParameters(queryParameters);
SearchQuery<Endpoint> query = dao.createSearchQueryWithoutUserFilter(PageAndCount.exists())
.configureParameters(queryParameters);

List<SearchQueryParameterError> uQp = query.getUnsupportedQueryParameters();
if (!uQp.isEmpty())
Expand Down Expand Up @@ -140,7 +142,8 @@ private boolean endpointWithIdentifierExists(Connection connection, String ident
Map<String, List<String>> queryParameters = Map.of("identifier",
Collections.singletonList(ENDPOINT_IDENTIFIER_SYSTEM + "|" + identifierValue));
EndpointDao dao = getDao();
SearchQuery<Endpoint> query = dao.createSearchQueryWithoutUserFilter(0, 0).configureParameters(queryParameters);
SearchQuery<Endpoint> query = dao.createSearchQueryWithoutUserFilter(PageAndCount.exists())
.configureParameters(queryParameters);

List<SearchQueryParameterError> uQp = query.getUnsupportedQueryParameters();
if (!uQp.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import dev.dsf.fhir.dao.OrganizationAffiliationDao;
import dev.dsf.fhir.dao.provider.DaoProvider;
import dev.dsf.fhir.help.ParameterConverter;
import dev.dsf.fhir.search.PageAndCount;
import dev.dsf.fhir.search.PartialResult;
import dev.dsf.fhir.search.SearchQuery;
import dev.dsf.fhir.search.SearchQueryParameterError;
Expand Down Expand Up @@ -154,7 +155,7 @@ private Map<String, List<String>> queryParameters(OrganizationAffiliation newRes
private boolean organizationAffiliationExists(Connection connection, Map<String, List<String>> queryParameters)
{
OrganizationAffiliationDao dao = getDao();
SearchQuery<OrganizationAffiliation> query = dao.createSearchQueryWithoutUserFilter(0, 0)
SearchQuery<OrganizationAffiliation> query = dao.createSearchQueryWithoutUserFilter(PageAndCount.exists())
.configureParameters(queryParameters);

List<SearchQueryParameterError> uQp = query.getUnsupportedQueryParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import dev.dsf.fhir.dao.SubscriptionDao;
import dev.dsf.fhir.dao.provider.DaoProvider;
import dev.dsf.fhir.help.ParameterConverter;
import dev.dsf.fhir.search.PageAndCount;
import dev.dsf.fhir.search.PartialResult;
import dev.dsf.fhir.search.SearchQuery;
import dev.dsf.fhir.search.SearchQueryParameterError;
Expand Down Expand Up @@ -87,7 +88,8 @@ private Optional<String> newResourceOk(Connection connection, Subscription newRe
Optional<ResourceDao<?>> optDao = daoProvider.getDao(cComponentes.getPathSegments().get(0));
if (optDao.isPresent())
{
SearchQuery<?> searchQuery = optDao.get().createSearchQueryWithoutUserFilter(1, 1);
SearchQuery<?> searchQuery = optDao.get().createSearchQueryWithoutUserFilter(PageAndCount.exists())
.configureParameters(cComponentes.getQueryParams());
List<SearchQueryParameterError> uQp = searchQuery.getUnsupportedQueryParameters();
if (!uQp.isEmpty())
{
Expand Down Expand Up @@ -131,7 +133,7 @@ protected boolean resourceExists(Connection connection, Subscription newResource
Collections.singletonList(newResource.getChannel().getType().toCode()), "payload",
Collections.singletonList(newResource.getChannel().getPayload()));
SubscriptionDao dao = getDao();
SearchQuery<Subscription> query = dao.createSearchQueryWithoutUserFilter(1, 1)
SearchQuery<Subscription> query = dao.createSearchQueryWithoutUserFilter(PageAndCount.exists())
.configureParameters(queryParameters);

List<SearchQueryParameterError> uQp = query.getUnsupportedQueryParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.dsf.fhir.dao.exception.ResourceNotMarkedDeletedException;
import dev.dsf.fhir.dao.exception.ResourceVersionNoMatchException;
import dev.dsf.fhir.search.DbSearchQuery;
import dev.dsf.fhir.search.PageAndCount;
import dev.dsf.fhir.search.PartialResult;
import dev.dsf.fhir.search.SearchQuery;

Expand Down Expand Up @@ -355,9 +356,21 @@ R updateWithTransaction(Connection connection, R resource, Long expectedVersion)
*/
PartialResult<R> searchWithTransaction(Connection connection, DbSearchQuery query) throws SQLException;

SearchQuery<R> createSearchQuery(Identity identity, int page, int count);
/**
* @param identity
* not <code>null</code>
* @param pageAndCount
* not <code>null</code>
* @return query
*/
SearchQuery<R> createSearchQuery(Identity identity, PageAndCount pageAndCount);

SearchQuery<R> createSearchQueryWithoutUserFilter(int page, int count);
/**
* @param pageAndCount
* not <code>null</code>
* @return query
*/
SearchQuery<R> createSearchQueryWithoutUserFilter(PageAndCount pageAndCount);

/**
* Permanently delete a resource that was previously marked as deleted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import dev.dsf.fhir.help.ParameterConverter;
import dev.dsf.fhir.help.ResponseGenerator;
import dev.dsf.fhir.prefer.PreferReturnType;
import dev.dsf.fhir.search.PageAndCount;
import dev.dsf.fhir.search.PartialResult;
import dev.dsf.fhir.search.SearchQuery;
import dev.dsf.fhir.search.SearchQueryParameterError;
Expand Down Expand Up @@ -199,7 +200,7 @@ private Optional<Resource> checkAlreadyExists(Connection connection, String ifNo
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}

SearchQuery<R> query = dao.createSearchQueryWithoutUserFilter(1, 1);
SearchQuery<R> query = dao.createSearchQueryWithoutUserFilter(PageAndCount.single());
query.configureParameters(queryParameters);

List<SearchQueryParameterError> unsupportedQueryParameters = query.getUnsupportedQueryParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import dev.dsf.fhir.help.ParameterConverter;
import dev.dsf.fhir.help.ResponseGenerator;
import dev.dsf.fhir.prefer.PreferReturnType;
import dev.dsf.fhir.search.PageAndCount;
import dev.dsf.fhir.search.PartialResult;
import dev.dsf.fhir.search.SearchQuery;
import dev.dsf.fhir.search.SearchQueryParameterError;
Expand Down Expand Up @@ -172,7 +173,7 @@ private Optional<Resource> search(Connection connection, ResourceDao<?> dao,
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}

SearchQuery<?> query = dao.createSearchQueryWithoutUserFilter(1, 1);
SearchQuery<?> query = dao.createSearchQueryWithoutUserFilter(PageAndCount.single());
query.configureParameters(queryParameters);

List<SearchQueryParameterError> unsupportedQueryParameters = query.getUnsupportedQueryParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import dev.dsf.fhir.help.ResponseGenerator;
import dev.dsf.fhir.prefer.PreferHandlingType;
import dev.dsf.fhir.prefer.PreferReturnType;
import dev.dsf.fhir.search.PageAndCount;
import dev.dsf.fhir.search.PartialResult;
import dev.dsf.fhir.search.SearchQuery;
import dev.dsf.fhir.search.SearchQueryParameterError;
Expand Down Expand Up @@ -193,14 +194,8 @@ private void readByCondition(Connection connection, String resourceTypeName,
responseResult = Response.status(Status.NOT_FOUND).build();
else
{
Integer page = parameterConverter.getFirstInt(cleanQueryParameters, SearchQuery.PARAMETER_PAGE);
int effectivePage = page == null ? 1 : page;

Integer count = parameterConverter.getFirstInt(cleanQueryParameters, SearchQuery.PARAMETER_COUNT);
int effectiveCount = (count == null || count < 0) ? defaultPageCount : count;

SearchQuery<? extends Resource> query = optDao.get().createSearchQuery(identity, effectivePage,
effectiveCount);
SearchQuery<? extends Resource> query = optDao.get().createSearchQuery(identity,
PageAndCount.from(cleanQueryParameters, defaultPageCount));
query.configureParameters(cleanQueryParameters);
List<SearchQueryParameterError> errors = query.getUnsupportedQueryParameters();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import dev.dsf.fhir.help.ParameterConverter;
import dev.dsf.fhir.help.ResponseGenerator;
import dev.dsf.fhir.prefer.PreferReturnType;
import dev.dsf.fhir.search.PageAndCount;
import dev.dsf.fhir.search.PartialResult;
import dev.dsf.fhir.search.SearchQuery;
import dev.dsf.fhir.search.SearchQueryParameterError;
Expand Down Expand Up @@ -151,7 +152,7 @@ private boolean addMissingIdToTranslationTableAndCheckConditionFindsResource(Map
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}

SearchQuery<R> query = dao.createSearchQueryWithoutUserFilter(1, 1);
SearchQuery<R> query = dao.createSearchQueryWithoutUserFilter(PageAndCount.single());
query.configureParameters(queryParameters);

List<SearchQueryParameterError> unsupportedParams = query.getUnsupportedQueryParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import dev.dsf.fhir.dao.exception.ResourceNotMarkedDeletedException;
import dev.dsf.fhir.dao.exception.ResourceVersionNoMatchException;
import dev.dsf.fhir.search.DbSearchQuery;
import dev.dsf.fhir.search.PageAndCount;
import dev.dsf.fhir.search.PartialResult;
import dev.dsf.fhir.search.SearchQuery;
import dev.dsf.fhir.search.SearchQuery.SearchQueryBuilder;
Expand Down Expand Up @@ -898,20 +899,24 @@ private void getResources(ResultSet result, int columnIndex, List<? super Resour
}

@Override
public final SearchQuery<R> createSearchQuery(Identity identity, int page, int count)
public final SearchQuery<R> createSearchQuery(Identity identity, PageAndCount pageAndCount)
{
return doCreateSearchQuery(identity, page, count);
Objects.requireNonNull(identity, "identity");

return doCreateSearchQuery(identity, pageAndCount);
}

@Override
public SearchQuery<R> createSearchQueryWithoutUserFilter(int page, int count)
public SearchQuery<R> createSearchQueryWithoutUserFilter(PageAndCount pageAndCount)
{
return doCreateSearchQuery(null, page, count);
return doCreateSearchQuery(null, pageAndCount);
}

private SearchQuery<R> doCreateSearchQuery(Identity identity, int page, int count)
private SearchQuery<R> doCreateSearchQuery(Identity identity, PageAndCount pageAndCount)
{
var builder = SearchQueryBuilder.create(resourceType, getResourceTable(), getResourceColumn(), page, count);
Objects.requireNonNull(pageAndCount, "pageAndCount");

var builder = SearchQueryBuilder.create(resourceType, getResourceTable(), getResourceColumn(), pageAndCount);

if (identity != null)
builder = builder.with(identityFilter.apply(identity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ public Bundle createSearchSet(PartialResult<? extends Resource> result, List<Sea

bundle.setTotal(result.getTotal());

setLinks(result.getPageAndCount(), bundleUri, format, pretty, summaryMode, bundle,
result.getPartialResult().isEmpty(), result.getTotal());
setLinks(result.getPageAndCount(), bundleUri, format, pretty, summaryMode, bundle, result.getTotal());

return bundle;
}
Expand Down Expand Up @@ -217,8 +216,7 @@ public Bundle createHistoryBundle(History history, List<SearchQueryParameterErro

bundle.setTotal(history.getTotal());

setLinks(history.getPageAndCount(), bundleUri, format, pretty, summaryMode, bundle,
history.getEntries().isEmpty(), history.getTotal());
setLinks(history.getPageAndCount(), bundleUri, format, pretty, summaryMode, bundle, history.getTotal());

return bundle;
}
Expand Down Expand Up @@ -262,7 +260,7 @@ private String toLocation(String resourceType, String id, String version)
}

private void setLinks(PageAndCount pageAndCount, UriBuilder bundleUri, String format, String pretty,
SummaryMode summaryMode, Bundle bundle, boolean isEmpty, int total)
SummaryMode summaryMode, Bundle bundle, int total)
{
if (format != null)
bundleUri = bundleUri.replaceQueryParam("_format", format);
Expand All @@ -271,17 +269,18 @@ private void setLinks(PageAndCount pageAndCount, UriBuilder bundleUri, String fo
if (summaryMode != null)
bundleUri = bundleUri.replaceQueryParam("_summary", summaryMode.toString());

if (pageAndCount.getCount() > 0)
boolean countOnly = pageAndCount.isCountOnly(total);
if (!countOnly)
{
bundleUri = bundleUri.replaceQueryParam("_count", pageAndCount.getCount());
bundleUri = bundleUri.replaceQueryParam("_page", isEmpty ? 1 : pageAndCount.getPage());
bundleUri = bundleUri.replaceQueryParam("_page", pageAndCount.getPage());
}
else
bundleUri = bundleUri.replaceQueryParam("_count", "0");

bundle.addLink().setRelation("self").setUrlElement(new UriType(bundleUri.build()));

if (pageAndCount.getCount() > 0 && !isEmpty)
if (!countOnly && pageAndCount.getCount() > 0)
{
bundleUri = bundleUri.replaceQueryParam("_page", 1);
bundleUri = bundleUri.replaceQueryParam("_count", pageAndCount.getCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ public Bundle getHistory(Identity identity, UriInfo uri, HttpHeaders headers,
{
MultivaluedMap<String, String> queryParameters = uri.getQueryParameters();

Integer page = parameterConverter.getFirstInt(queryParameters, SearchQuery.PARAMETER_PAGE);
int effectivePage = page == null ? 1 : page;

Integer count = parameterConverter.getFirstInt(queryParameters, SearchQuery.PARAMETER_COUNT);
int effectiveCount = count == null || count < 0 ? defaultPageCount : count;

PageAndCount pageAndCount = new PageAndCount(effectivePage, effectiveCount);
PageAndCount pageAndCount = PageAndCount.from(uri.getQueryParameters(), defaultPageCount);

List<AtParameter> atParameters = new ArrayList<>();
SinceParameter sinceParameter = new SinceParameter();
Expand Down
Loading
Loading