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


import model.Filter;
import model.grid.GridRows;
import model.grid.SortInfo;
import ru.micord.ervu.account_applications.component.util.GridUtils;
import service.GridV2ServiceImpl;

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


/**
 * @author Adel Kalimullin
 */
public class IpGridV2Service extends GridV2ServiceImpl {
  private static final String IP_FIELD = "link_user_application_ip_address$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;
  }
}
