package ca.uhn.fhir.mdm.api.paging;

import ca.uhn.fhir.mdm.api.MdmLinkJson;
import ca.uhn.fhir.rest.server.RestfulServerUtils;
import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
import org.springframework.data.domain.Page;

import java.util.Arrays;

import static ca.uhn.fhir.rest.api.Constants.PARAM_COUNT;
import static ca.uhn.fhir.rest.api.Constants.PARAM_OFFSET;

/**
 * Builder to generate {@link MdmPageLinkTuple} objects, based on a given page of data and the incoming page request.
 */
public final class MdmPageLinkBuilder {

	/**
	 * Generates an {@link MdmPageLinkTuple} which contains previous/self/next links for pagination purposes.
	 *
	 * @param theServletRequestDetails the incoming request details. Used to determine server base.
	 * @param theCurrentPage the page of MDM link data. Used for determining if there are next/previous pages available.
	 * @param thePageRequest the incoming Page request, containing requested offset and count. Used for building offset for outgoing URLs.
	 *
	 * @return the {@link MdmPageLinkTuple}
	 */
	public static MdmPageLinkTuple buildMdmPageLinks(ServletRequestDetails theServletRequestDetails, Page<MdmLinkJson> theCurrentPage, MdmPageRequest thePageRequest) {
		MdmPageLinkTuple tuple = new MdmPageLinkTuple();
		String urlWithoutPaging = RestfulServerUtils.createLinkSelfWithoutGivenParameters(theServletRequestDetails.getFhirServerBase(), theServletRequestDetails, Arrays.asList(PARAM_OFFSET, PARAM_COUNT));
		tuple.setSelfLink(buildLinkWithOffsetAndCount(urlWithoutPaging, thePageRequest.getCount(), thePageRequest.getOffset()));
		if (theCurrentPage.hasNext()) {
			tuple.setNextLink(buildLinkWithOffsetAndCount(urlWithoutPaging,thePageRequest.getCount(), thePageRequest.getNextOffset()));
		}
		if (theCurrentPage.hasPrevious()) {
			tuple.setPreviousLink(buildLinkWithOffsetAndCount(urlWithoutPaging,thePageRequest.getCount(), thePageRequest.getPreviousOffset()));
		}
		return tuple;
	}

	private static String buildLinkWithOffsetAndCount(String theStartingUrl, int theCount, int theOffset) {
		StringBuilder builder = new StringBuilder();
		builder.append(theStartingUrl);
		if (!theStartingUrl.contains("?")) {
			builder.append("?");
		}
		builder.append(PARAM_OFFSET).append("=").append(theOffset);
		builder.append(PARAM_COUNT).append("=").append(theCount);
		return builder.toString();
	}
}

@@ -1226,59 +1226,8 @@
 ) %7B%0A
-%09%09MdmPageLinkTuple tuple = new MdmPageLinkTuple();%0A
 %09%09St
@@ -1421,16 +1421,294 @@
 OUNT));%0A
+%09%09return buildMdmPageLinks(urlWithoutPaging, theCurrentPage, thePageRequest);%0A%09%7D%0A%0A%09public static MdmPageLinkTuple buildMdmPageLinks(String theUrlWithoutPaging, Page%3CMdmLinkJson%3E theCurrentPage, MdmPageRequest thePageRequest) %7B%0A%09%09MdmPageLinkTuple tuple = new MdmPageLinkTuple();%0A
 %09%09tuple.
@@ -1739,33 +1739,36 @@
 hOffsetAndCount(
-u
+theU
 rlWithoutPaging,
@@ -1900,33 +1900,36 @@
 hOffsetAndCount(
-u
+theU
 rlWithoutPaging,
@@ -2084,17 +2084,20 @@
 ndCount(
-u
+theU
 rlWithou
@@ -2193,20 +2193,20 @@
 le;%0A
+%0A
 %09%7D%0A%0A%09p
-rivate
+ublic
  sta
@@ -2254,24 +2254,20 @@
 ring the
-Starting
+Base
 Url, int
@@ -2361,24 +2361,20 @@
 pend(the
-Starting
+Base
 Url);%0A%09%09
@@ -2385,16 +2385,12 @@
 !the
-Starting
+Base
 Url.

Add alternate caller methods