Skip to content

Commit

Permalink
Merge pull request #14 from searchspring/pagination-bits
Browse files Browse the repository at this point in the history
added pagination bits
  • Loading branch information
codeallthethingz authored Oct 19, 2021
2 parents edde0f3 + b7ab3cd commit 623a8f4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
27 changes: 17 additions & 10 deletions src/main/java/com/searchspring/nextopia/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,28 @@ public String convertSearchspringResponse(String searchspringResponse) {
return EMPTY_RESPONSE;
}
SearchspringResponse response = GSON.fromJson(searchspringResponse, SearchspringResponse.class);
StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='UTF-8'?><xml><pagination><total_products>"
+ getTotalResults(response) + "</total_products></pagination>");
StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='UTF-8'?><xml>");

appendPagination(sb, response);
appendRefinements(sb, response);
appendResults(sb, response);
sb.append("</xml>");
return sb.toString();
}

private void appendPagination(StringBuilder sb, SearchspringResponse response) {
sb.append("<pagination>");
sb.append("<total_products>").append(response.pagination.totalResults).append("</total_products>");
sb.append("<product_min>").append(response.pagination.begin).append("</product_min>");
sb.append("<product_max>").append(response.pagination.end).append("</product_max>");
sb.append("<current_page>").append(response.pagination.currentPage).append("</current_page>");
sb.append("<total_pages>").append(response.pagination.totalPages).append("</total_pages>");
sb.append("<prev_page>").append(response.pagination.currentPage > 1 ? "1" : "0").append("</prev_page>");
sb.append("<next_page>").append(response.pagination.currentPage < response.pagination.totalPages ? "1" : "0")
.append("</next_page>");
sb.append("</pagination>");
}

private void appendRefinements(StringBuilder sb, SearchspringResponse response) {
sb.append("<refinables>");

Expand Down Expand Up @@ -86,7 +100,7 @@ private String doubleToInteger(Object object) {
return "";
}
try {
int result = (int)Double.parseDouble(object.toString());
int result = (int) Double.parseDouble(object.toString());
return String.valueOf(result);
} catch (Exception e) {
return "";
Expand All @@ -110,13 +124,6 @@ private void appendResults(StringBuilder sb, SearchspringResponse response) {
sb.append("</results>");
}

private String getTotalResults(SearchspringResponse response) {
if (response.pagination != null) {
return String.valueOf(response.pagination.totalResults);
}
return "0";
}

public String convertNextopiaQueryUrl(String nextopiaQueryUrl) throws URISyntaxException {
nextopiaQueryUrl = nextopiaQueryUrl.replaceAll("\\^", "%5E");
URI uri = new URI(nextopiaQueryUrl);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/searchspring/nextopia/model/Pagination.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

public class Pagination {
public int totalResults;
public int currentPage;
public int totalPages;
public int begin;
public int end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;

public class SearchspringResponse {

public Pagination pagination;
public Map<String,Object>[] results;
public Map<String,Object>[] facets;
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/com/searchspring/nextopia/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public void setup() {
@Test
public void convertSearchspringResponsePaginationTest() {
String ssJson = "{\"pagination\": {\"totalResults\": 1981}}";
String expected = "<?xml version='1.0' encoding='UTF-8'?><xml><pagination><total_products>1981</total_products></pagination><refinables></refinables><results></results></xml>";
String expected = "<?xml version='1.0' encoding='UTF-8'?><xml>"
+ "<pagination><total_products>1981</total_products><product_min>0</product_min><product_max>0</product_max><current_page>0</current_page><total_pages>0</total_pages><prev_page>0</prev_page><next_page>0</next_page></pagination>"
+ "<refinables></refinables><results></results></xml>";
assertEquals(expected, converter.convertSearchspringResponse(ssJson));
}

Expand All @@ -48,7 +50,8 @@ public void convertSearchspringResponseResultsTest() {
+ "\"facets\": [{\"field\":\"pattern_id7741124012283333869\", \"label\": \"Pattern\",\"type\": null,\"collapse\": 0,\"facet_active\": 0,"
+ "\"values\": [{\"active\": false,\"type\": \"value\",\"value\": \"Baguette\",\"label\": \"Baguette\",\"count\": 21}]"
+ "}]" + "}";
String expected = "<?xml version='1.0' encoding='UTF-8'?><xml><pagination><total_products>1981</total_products></pagination>"
String expected = "<?xml version='1.0' encoding='UTF-8'?><xml>"
+ "<pagination><total_products>1981</total_products><product_min>0</product_min><product_max>0</product_max><current_page>0</current_page><total_pages>0</total_pages><prev_page>0</prev_page><next_page>0</next_page></pagination>"
+ "<refinables><refinable><name><![CDATA[pattern_id7741124012283333869]]></name><values><value><name><![CDATA[Baguette]]></name><num><![CDATA[21]]></num></value></values></refinable></refinables>"
+ "<results>" //
+ "<result><rank>0</rank><Sku><![CDATA[1234]]></Sku><results_flags><![CDATA[attributized]]></results_flags></result>" //
Expand Down

0 comments on commit 623a8f4

Please sign in to comment.