package com.bgh.myopeninvoice.api.util;

import com.bgh.myopeninvoice.api.domain.SearchParameters;
import com.bgh.myopeninvoice.common.domain.DefaultResponse;
import com.bgh.myopeninvoice.common.domain.OperationResponse;
import com.bgh.myopeninvoice.common.exception.InvalidParameterException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.NumberUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
public class Utils {

  private Utils() {}

  public static <E> List<E> convertIterableToList(Iterable<E> iter) {
    List<E> list = new ArrayList<>();
    if (iter != null) {
      for (E item : iter) {
        list.add(item);
      }
    }
    return list;
  }

  public static SearchParameters mapQueryParametersToSearchParameters(
      Map<String, String> queryParameters) throws InvalidParameterException {
    SearchParameters searchParameters = new SearchParameters();

    if (queryParameters != null) {
      parseSort(queryParameters, searchParameters);

      parsePage(queryParameters, searchParameters);

      parseFilter(queryParameters, searchParameters);
    }

    return searchParameters;
  }

  private static void parseFilter(
      Map<String, String> queryParameters, SearchParameters searchParameters)
      throws InvalidParameterException {
    if (queryParameters.get("filter") != null) {
      String filter = queryParameters.get("filter");
      if (filter.length() > 100) {
        throw new InvalidParameterException(
            "Invalid parameters: 'filter'. Size cannot be more than 100");
      }

      searchParameters.setFilter(filter);
    }
  }

  private static void parsePage(
      Map<String, String> queryParameters, SearchParameters searchParameters)
      throws InvalidParameterException {
    if (queryParameters.get("page") != null && queryParameters.get("size") != null) {
      int page = NumberUtils.parseNumber(queryParameters.get("page"), Integer.class);
      int size = NumberUtils.parseNumber(queryParameters.get("size"), Integer.class);
      if (page < 0 || size > 1000) {
        throw new InvalidParameterException(
            "Invalid parameters 'page' or 'size'. Rule: page < 0 || size > 1000: page: "
                + page
                + ", size: "
                + size);
      }

      if (searchParameters.getSort() != null) {
        searchParameters.setPageRequest(PageRequest.of(page, size, searchParameters.getSort()));
      } else {
        searchParameters.setPageRequest(PageRequest.of(page, size));
      }
    }
  }

  private static void parseSort(
      Map<String, String> queryParameters, SearchParameters searchParameters)
      throws InvalidParameterException {
    if (queryParameters.get("sort") != null) {
      String[] fields = queryParameters.get("sort").trim().split("\\s*,\\s*");
      Sort finalSort = null;
      for (String field : fields) {

        if (!field.matches("^([+-]?)([a-zA-Z0-9_]{1,20})$")) {
          throw new InvalidParameterException(
              "Invalid parameters 'sort'. "
                  + "Rule: field.length() <= 20 and field.matches([+-]?)([a-zA-Z0-9_]{1,20}): "
                  + field);
        }

        finalSort = assignSortField(finalSort, field);
      }

      if (finalSort != null) {
        searchParameters.setSort(finalSort);
      }
    }
  }

  private static Sort assignSortField(Sort finalSort, String field) {
    Sort sort;
    if (field.startsWith("+")) {
      sort = Sort.by(field.substring(1)).ascending();
    } else if (field.startsWith("-")) {
      sort = Sort.by(field.substring(1)).descending();
    } else {
      sort = Sort.by(field);
    }

    finalSort = finalSort == null ? sort : finalSort.and(sort);
    return finalSort;
  }

}

@@ -96,130 +96,8 @@
 rs;%0A
-import com.bgh.myopeninvoice.common.domain.DefaultResponse;%0Aimport com.bgh.myopeninvoice.common.domain.OperationResponse;%0A
 impo
@@ -214,47 +214,39 @@
 org.
-springframework.data.domain.PageRequest
+apache.commons.lang.StringUtils
 ;%0Aim
@@ -282,19 +282,26 @@
 .domain.
-Sor
+PageReques
 t;%0Aimpor
@@ -326,222 +326,98 @@
 ork.
-http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;%0Aimport org.springframework.util.NumberUtils;%0A%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport java.util.ArrayList;%0Aimport java.util.Collections
+data.domain.Sort;%0Aimport org.springframework.util.NumberUtils;%0A%0Aimport java.util.ArrayList
 ;%0Aim
@@ -1334,32 +1334,55 @@
 eption %7B%0A    if 
+(StringUtils.isNotEmpty
 (queryParameters
@@ -1395,24 +1395,17 @@
 filter%22)
- != null
+)
 ) %7B%0A    
@@ -1821,32 +1821,55 @@
 eption %7B%0A    if 
+(StringUtils.isNotEmpty
 (queryParameters
@@ -1884,20 +1884,48 @@
 ge%22)
- != null && 
+)%0A            && StringUtils.isNotEmpty(
 quer
@@ -1947,24 +1947,17 @@
 (%22size%22)
- != null
+)
 ) %7B%0A    
@@ -2780,32 +2780,55 @@
 eption %7B%0A    if 
+(StringUtils.isNotEmpty
 (queryParameters
@@ -2839,24 +2839,17 @@
 (%22sort%22)
- != null
+)
 ) %7B%0A    
@@ -3843,11 +3843,10 @@
 rt;%0A  %7D%0A
-%0A
 %7D%0A

Update for the null check