package service.loading;

import java.util.ArrayList;
import java.util.List;

import ru.cg.webbpm.modules.database.bean.annotation.LocalGraphSource;
import ru.cg.webbpm.modules.database.bean.entity_graph.EntityColumn;
import ru.cg.webbpm.modules.database.bean.filter.EntityFilter;
import ru.cg.webbpm.modules.database.bean.filter.FilterOperation;

/**
 * @author r.latypov
 */
public class ContainerValueLoaderServiceImpl extends ContainerValueLoaderServiceBase {
  @LocalGraphSource(sourceFieldName = "loadDao")
  public EntityColumn[] entityColumns;

  @Override
  protected void validateParametersNumber(int parametersLength) {
    if (entityColumns != null && entityColumns.length > 0
        && parametersLength > entityColumns.length) {
      throw new UnsupportedOperationException(
          "Can't support multiple params without matching entity columns");
    }
  }

  @Override
  protected boolean loadByPkCondition() {
    return entityColumns == null || entityColumns.length == 0;
  }

  @Override
  protected List<EntityFilter> createEntityFilters(Object[] params) {
    List<EntityFilter> entityFilters = new ArrayList<>();
    for (int i = 0; i < params.length; i++) {
      entityFilters.add(new EntityFilter(params[i], FilterOperation.EQUAL, entityColumns[i]));
    }
    return entityFilters;
  }
}

