package ru.micord.ervu.account_applications.component.service;


import component.complex.FilterableByPKGridService;
import model.Filter;
import model.grid.GridRows;
import model.grid.SortInfo;
import ru.micord.ervu.account_applications.component.util.GridUtils;

import ru.cg.webbpm.modules.database.api.dao.option.SortOrder;

/**
 * @author Adel Kalimullin
 */
public class IpFilterableGridService extends FilterableByPKGridService {
  private static final String IP_FIELD = "ip_directory$ip_address";

  @Override
  public GridRows loadData(Integer offset, Integer limit, Filter[] filters, SortInfo[] sortInfos) {
    GridRows gridRows = super.loadData(offset, limit, filters, null);
    SortOrder sortOrder = (sortInfos != null && sortInfos.length > 0)
                          ? sortInfos[0].getSortOrder()
                          : SortOrder.ASC;

    GridUtils.sortByIp(gridRows, sortOrder, IP_FIELD);
    return gridRows;
  }

  @Override
  public GridRows loadDataWithRowCount(Integer offset, Integer limit, Filter[] filters,
      SortInfo[] sortInfos) {
    GridRows gridRows = super.loadDataWithRowCount(offset, limit, filters, null);
    SortOrder sortOrder = (sortInfos != null && sortInfos.length > 0)
                          ? sortInfos[0].getSortOrder()
                          : SortOrder.ASC;

    GridUtils.sortByIp(gridRows, sortOrder, IP_FIELD);
    return gridRows;
  }
}