001package ca.uhn.fhir.mdm.api.paging;
002
003/*-
004 * #%L
005 * HAPI FHIR - Master Data Management
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.mdm.api.MdmLinkJson;
024import ca.uhn.fhir.rest.server.RestfulServerUtils;
025import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
026import org.springframework.data.domain.Page;
027
028import java.util.Arrays;
029
030import static ca.uhn.fhir.rest.api.Constants.PARAM_COUNT;
031import static ca.uhn.fhir.rest.api.Constants.PARAM_OFFSET;
032
033/**
034 * Builder to generate {@link MdmPageLinkTuple} objects, based on a given page of data and the incoming page request.
035 */
036public final class MdmPageLinkBuilder {
037
038        /**
039         * Generates an {@link MdmPageLinkTuple} which contains previous/self/next links for pagination purposes.
040         *
041         * @param theServletRequestDetails the incoming request details. Used to determine server base.
042         * @param theCurrentPage the page of MDM link data. Used for determining if there are next/previous pages available.
043         * @param thePageRequest the incoming Page request, containing requested offset and count. Used for building offset for outgoing URLs.
044         *
045         * @return the {@link MdmPageLinkTuple}
046         */
047        public static MdmPageLinkTuple buildMdmPageLinks(ServletRequestDetails theServletRequestDetails, Page<MdmLinkJson> theCurrentPage, MdmPageRequest thePageRequest) {
048                String urlWithoutPaging = RestfulServerUtils.createLinkSelfWithoutGivenParameters(theServletRequestDetails.getFhirServerBase(), theServletRequestDetails, Arrays.asList(PARAM_OFFSET, PARAM_COUNT));
049                return buildMdmPageLinks(urlWithoutPaging, theCurrentPage, thePageRequest);
050        }
051
052        public static MdmPageLinkTuple buildMdmPageLinks(String theUrlWithoutPaging, Page<MdmLinkJson> theCurrentPage, MdmPageRequest thePageRequest) {
053                MdmPageLinkTuple tuple = new MdmPageLinkTuple();
054                tuple.setSelfLink(buildLinkWithOffsetAndCount(theUrlWithoutPaging, thePageRequest.getCount(), thePageRequest.getOffset()));
055                if (theCurrentPage.hasNext()) {
056                        tuple.setNextLink(buildLinkWithOffsetAndCount(theUrlWithoutPaging,thePageRequest.getCount(), thePageRequest.getNextOffset()));
057                }
058                if (theCurrentPage.hasPrevious()) {
059                        tuple.setPreviousLink(buildLinkWithOffsetAndCount(theUrlWithoutPaging,thePageRequest.getCount(), thePageRequest.getPreviousOffset()));
060                }
061                return tuple;
062
063        }
064
065        public static String buildLinkWithOffsetAndCount(String theBaseUrl, int theCount, int theOffset) {
066                StringBuilder builder = new StringBuilder();
067                builder.append(theBaseUrl);
068                if (!theBaseUrl.contains("?")) {
069                        builder.append("?");
070                } else {
071                        builder.append("&");
072                }
073                builder.append(PARAM_OFFSET).append("=").append(theOffset);
074                builder.append("&");
075                builder.append(PARAM_COUNT).append("=").append(theCount);
076                return builder.toString();
077        }
078}