/*
   * (non-Javadoc)
   *
   * @see
   * com.tramex.sisoprega.common.crud.Cruddable#Read(com.tramex.sisoprega.common
   * .GatewayRequest)
   */
  @Override
  public ReadGatewayResponse Read(GatewayRequest request) {
    this.log.entering(this.getClass().getCanonicalName(), "Read");

    ReadGatewayResponse response = new ReadGatewayResponse();
    response.setEntityName(request.getEntityName());

    PenCapacity capacity = null;
    try {
      capacity = entityFromRequest(request, PenCapacity.class);
      this.log.fine("Got barnyard capacity from request: " + capacity);

      String qryLogger = "";
      Map<String, Object> parameters = new HashMap<String, Object>();
      String queryName = "";

      if (capacity.getCapacityId() != 0) {
        queryName = "BARNYARD_CAPACITY_BY_ID";
        parameters.put("capacityId", capacity.getCapacityId());
        qryLogger = "By capacityId [" + capacity.getCapacityId() + "]";
      } else if (capacity.getBarnyardId() != 0 && capacity.getCatclassId() != 0) {
        queryName = "BARNYARD_CAPACITY_BY_BARNYARD_AND_CATTLE";
        parameters.put("barnyardId", capacity.getBarnyardId());
        parameters.put("cattleClassId", capacity.getCatclassId());
        qryLogger =
            "By barnyardId ["
                + capacity.getBarnyardId()
                + "] and cattleClassId ["
                + capacity.getCatclassId()
                + "]";
      } else {
        queryName = "ALL_BARNYARD_CAPACITIES";
        qryLogger = "By ALL BARNYARD CAPACITIES";
      }

      List<PenCapacity> result =
          dataModel.readDataModelList(queryName, parameters, PenCapacity.class);
      if (result.isEmpty()) {
        response.setError(
            new Error(
                "VAL02",
                "No se encontraron datos para el filtro seleccionado",
                "proxy.BarnyardBean.Read"));
      } else {
        List<GatewayContent> records = contentFromList(result, PenCapacity.class);
        response.getRecord().addAll(records);

        response.setError(new Error("0", "SUCCESS", "proxy.BarnyardBean.Read"));
        this.log.info(
            "Read operation "
                + qryLogger
                + " executed by principal["
                + getLoggedUser()
                + "] on BarnyardCapacityBean");
      }

    } catch (Exception e) {
      // something went wrong, alert the server and respond the client
      this.log.severe("Exception found while reading BarnyardCapacityBean");
      this.log.throwing(this.getClass().getCanonicalName(), "Read", e);

      response.setError(
          new Error(
              "DB02", "Read exception: " + e.getMessage(), "proxy.BarnyardCapacityBean.Read"));
    }

    this.log.exiting(this.getClass().getCanonicalName(), "Read");
    return response;
  }
  @Override
  public ReadGatewayResponse Read(GatewayRequest request) {
    this.log.entering(this.getClass().getCanonicalName(), "Read");

    ReadGatewayResponse response = new ReadGatewayResponse();
    response.setEntityName(request.getEntityName());

    Pedimento pedimento = null;
    try {
      pedimento = entityFromRequest(request, Pedimento.class);
      this.log.fine("Got pedimento from request: " + pedimento);

      List<Pedimento> pedimentos = new LinkedList<Pedimento>();

      if (ejbContext.isCallerInRole("rancher")) {
        long rancherId = getLoggedRancherId();
        if (pedimento.getFolio() != null && !pedimento.getFolio().trim().equals("")) {
          // Retrieve details for single pedimento
          Pedimento ped = getPedimentoDetails(rancherId, pedimento.getFolio());
          if (ped != null) {
            pedimentos.add(ped);
          }
        } else if (pedimento.getFechaPedimento() != null) {
          // Retrieve details for list of pedimentos
          String fechaPedimento =
              new SimpleDateFormat("yyyyMMdd").format(pedimento.getFechaPedimento());
          for (String folio : getFoliosInDate(rancherId, fechaPedimento)) {
            Pedimento p = new Pedimento();
            p.setFolio(folio);
            p.setRancherId(rancherId);
            p.setFechaPedimento(pedimento.getFechaPedimento());
            pedimentos.add(p);
          }
        } else {
          fillWithPedimentosByRancherId(pedimentos, rancherId);
        }
      } else if (pedimento.getFolio() != null && !pedimento.getFolio().trim().equals("")) {
        if (pedimento.getRancherId() != 0) {
          // Retrieve details for single pedimento
          Pedimento ped = getPedimentoDetails(pedimento.getRancherId(), pedimento.getFolio());
          if (ped != null) {
            pedimentos.add(ped);
          }
        } else {
          for (Long rancherId : getUploadedRanchers()) {
            Pedimento ped = getPedimentoDetails(rancherId, pedimento.getFolio());
            if (ped != null) {
              pedimentos.add(ped);
              break;
            }
          }
        }
      } else if (pedimento.getFechaPedimento() != null) {
        if (pedimento.getRancherId() != 0) {
          // Seek on especific rancher
          String fechaPedimento =
              new SimpleDateFormat("yyyyMMdd").format(pedimento.getFechaPedimento());
          for (String folio : getFoliosInDate(pedimento.getRancherId(), fechaPedimento)) {
            Pedimento p = new Pedimento();
            p.setFolio(folio);
            p.setRancherId(pedimento.getRancherId());
            p.setFechaPedimento(pedimento.getFechaPedimento());
            pedimentos.add(p);
          }
        } else {
          response.setError(
              new Error(
                  "VAL03",
                  "El filtro especificado no es válido en la lista de pedimentos",
                  "proxy.PdfUploader.Read"));
          return response;
        }
      } else if (pedimento.getRancherId() != 0) {
        // Retrieve date list of pedimentos for rancher
        fillWithPedimentosByRancherId(pedimentos, pedimento.getRancherId());
      } else {
        response.setError(
            new Error(
                "VAL03",
                "El filtro especificado no es válido en la lista de pedimentos",
                "proxy.PdfUploader.Read"));
        return response;
      }

      if (pedimentos.isEmpty()) {
        response.setError(
            new Error(
                "VAL02",
                "No se encontraron datos para el filtro seleccionado",
                "proxy.RancherBean.Read"));
      } else {
        // Add query results to response
        response.getRecord().addAll(contentFromList(pedimentos, Pedimento.class));

        // Add success message to response
        response.setError(new Error("0", "SUCCESS", "proxy.PdfUploader.Read"));
        this.log.info(
            "Read operation for pedimentos executed by principal["
                + getLoggedUser()
                + "] on RancherBean");
      }

    } catch (Exception e) {
      // something went wrong, alert the server and respond the client
      this.log.severe("Exception found while reading PdfUploader");
      this.log.throwing(this.getClass().getCanonicalName(), "Read", e);

      response.setError(
          new Error("DB02", "Read exception: " + e.getMessage(), "proxy.PdfUploader.Read"));
    }

    this.log.exiting(this.getClass().getCanonicalName(), "Read");
    return response;
  }