/*
   * (non-Javadoc)
   * @see com.sensus.mlc.ecomode.bcf.IEcoModeBCF#fetchLightConsumptionsByLightId(com.sensus.mlc.ecomode.model.request.
   * InquiryEcoModeRequest)
   */
  @Override
  public InquiryEcoModeResponse fetchLightConsumptionsByLightId(
      InquiryEcoModeRequest ecoModeRequest) {
    InquiryEcoModeResponse response = new InquiryEcoModeResponse();
    InternalResultsResponse<Consumption> internalResponse = null;

    try {
      ValidationContext context = new ValidationContext();
      context.getValidationArguments().put(getSlcActionName(), FETCH_LIGHT_CONSUMPTION);
      context.putObjectToBeValidated(INQUIRY_ECOMODE_REQUEST_NAME, ecoModeRequest);
      context.putObjectToBeValidated(INITIAL_DATE.getValue(), ecoModeRequest.getInitialDate());
      context.putObjectToBeValidated(END_DATE.getValue(), ecoModeRequest.getEndDate());

      checkDefaultsFetchAll(ecoModeRequest);
      if (getRequestValidationController().validate(context)
          && getInquiryRequestValidationController().validate(context)
          && getLightSelectionRequestValidationController().validate(context)
          && getRangeDateValidationController().validate(context)) {
        // fetch light consumptions
        internalResponse = getEcoModeBCL().fetchLightConsumptionsByLightId(ecoModeRequest);
      }

      handleOperationStatusAndMessages(response, internalResponse, context.getMessages(), true);
    } catch (Exception ex) {
      handleException(LOG, response, ex, SENSUS_MLC_ECOMODEBCLIMPL_DEFAULTEXCEPTION);
    }
    return response;
  }
  /*
   * (non-Javadoc)
   * @see com.sensus.mlc.lflivrofiscal.bcf.ILflivrofiscalBCF#insertLflivrofiscal(com.sensus.mlc.lflivrofiscal.model.request.LflivrofiscalRequest)
   */
  @Override
  public LflivrofiscalResponse insertLflivrofiscal(LflivrofiscalRequest lflivrofiscalRequest) {
    LflivrofiscalResponse response = new LflivrofiscalResponse();
    InternalResultsResponse<Lflivrofiscal> internalResponse = null;
    try {
      ValidationContext context = new ValidationContext();
      context.getValidationArguments().put(getSlcActionName(), INSERT);
      context.putObjectToBeValidated(LFLIVROFISCAL_REQUEST_NAME, lflivrofiscalRequest);
      context.putObjectToBeValidated(LFLIVROFISCAL_NAME, lflivrofiscalRequest.getLflivrofiscal());

      if (getLightingControlRequestValidationController().validate(context)
          && getLflivrofiscalValidationController().validate(context)) {

        if (getLightListValidationController().validate(context)) {
          internalResponse = getLflivrofiscalBCL().insertLflivrofiscal(lflivrofiscalRequest);
          response.setLflivrofiscal(internalResponse.getResultsList());
        }
      }

      handleOperationStatusAndMessages(response, internalResponse, context.getMessages(), true);
    } catch (Exception ex) {
      SensusInterfaceUtil.handleException(
          LOG, response, ex, DEFAULT_LFLIVROFISCAL_BCF_EXCEPTION_MSG);
    }
    return response;
  }
  /*
   * (non-Javadoc)
   * @see com.sensus.mlc.tabela.bcf.ITabelaBCF#insertTabela(com.sensus.mlc.tabela.model.request.TabelaRequest)
   */
  @Override
  public TabelaResponse insertTabela(TabelaRequest tabelaRequest) {
    TabelaResponse response = new TabelaResponse();
    InternalResultsResponse<Tabela> internalResponse = null;
    try {
      ValidationContext context = new ValidationContext();
      context.getValidationArguments().put(getSlcActionName(), INSERT);
      context.putObjectToBeValidated(TABELA_REQUEST_NAME, tabelaRequest);
      context.putObjectToBeValidated(TABELA_NAME, tabelaRequest.getTabela());

      if (getLightingControlRequestValidationController().validate(context)
          && getTabelaValidationController().validate(context)) {

        if (getLightListValidationController().validate(context)) {
          internalResponse = getTabelaBCL().insertTabela(tabelaRequest);
          response.setTabela(internalResponse.getResultsList());
        }
      }

      handleOperationStatusAndMessages(response, internalResponse, context.getMessages(), true);
    } catch (Exception ex) {
      SensusInterfaceUtil.handleException(LOG, response, ex, DEFAULT_TABELA_BCF_EXCEPTION_MSG);
    }
    return response;
  }
  /**
   * @see
   *     com.sensus.common.validation.IValidator#validate(com.sensus.common.validation.ValidationContext)
   */
  @Override
  public void validate(ValidationContext validationContext) {
    DMPersistanceActionEnum action =
        (DMPersistanceActionEnum)
            validationContext
                .getValidationArguments()
                .get(DMPersistanceActionEnum.getDefaultName());

    Device device = (Device) validationContext.getObjectToBeValidated(Device.class.getSimpleName());

    if (ValidationUtil.isNull(action) || ValidationUtil.isNull(device)) {
      validationContext.getMessages().add(MessageInfo.createFieldValidationError(DEVICE_REQUIRED));
      return;
    }

    switch (action) {
      case FETCH_UPDATED_LOAD_PROFILE:
      case FETCH_ALL_WATER_GAS_DATA_READ:
      case FETCH_ALL_INTERVAL_READ:
      case FETCH_ALL_LOAD_PROFILE_READ:
      case FETCH_ALL_TOU_READ:
      case FETCH_ALL_SNAPSHOT:
      case GENERATE_FILE_CSV_WATER_GAS_DATA_READ:
      case GENERATE_FILE_CSV_INTERVAL_READ:
      case GENERATE_FILE_CSV_LOAD_PROFILE_READ:
      case GENERATE_FILE_CSV_SNAPSHOT:
      case GENERATE_FILE_CSV_TOU:
        ValidationUtil.isNullOrEmpty(
            device.getDeviceId(), DEVICE_ID_REQUIRED, validationContext.getMessages());
        ValidationUtil.isNull(
            device.getDeviceType(), DEVICE_TYPE_ENUM_REQUIRED, validationContext.getMessages());
        break;
      case FETCH_ALL:
        validateDeviceId(validationContext.getMessages(), device);
        break;
      case FETCH_DEVICE_BY_ID:
        validateDeviceIdOrFlexNetIdRequired(validationContext.getMessages(), device);
        ValidationUtil.isNull(
            device.getDeviceType(), DEVICE_TYPE_ENUM_REQUIRED, validationContext.getMessages());
        break;
      case FETCH_DEVICE_BY_ID_IMPORT:
        validateDeviceIdOrFlexNetIdRequired(validationContext.getMessages(), device);
        break;
      case FETCH_ALL_MODEL_BY_HAN_DEVICE_TYPE:
      case FETCH_ALL_MANUFACTURE_BY_HAN_DEVICE_TYPE:
        ValidationUtil.isNull(
            device.getDeviceType(), DEVICE_TYPE_ENUM_REQUIRED, validationContext.getMessages());
        break;
      case FETCH_ALL_PEAK_DEMAND:
      case GENERATE_FILE_CSV_PEAK_READ:
        ValidationUtil.isNullOrEmpty(
            device.getDeviceId(), DEVICE_ID_REQUIRED, validationContext.getMessages());
        break;
      default:
        break;
    }
  }
  /*
   * (non-Javadoc)
   * @see
   * com.sensus.mlc.ecomode.bcf.IEcoModeBCF#importEcoModeBaselineFromFileCSV(com.sensus.mlc.ecomode.model.request.
   * InquiryEcoModeRequest)
   */
  @Override
  public EcoModeResponse importEcoModeBaselineFromFileCSV(EcoModeRequest ecoModeRequest) {
    EcoModeResponse response = new EcoModeResponse();
    InternalResponse internalResponse = null;

    try {
      ValidationContext context = new ValidationContext();
      context.getValidationArguments().put(getSlcActionName(), IMPORT_CSV_FILE);
      context.putObjectToBeValidated(ECOMODE_REQUEST_NAME, ecoModeRequest);
      context.putObjectToBeValidated(CSV_FILE.getValue(), ecoModeRequest.getEcoModeCSVImport());

      if (getRequestValidationController().validate(context)
          && getCsvFileValidationController().validate(context)) {
        // Import eco mode baseline
        internalResponse = getEcoModeBCL().importEcoModeBaselineFromFileCSV(ecoModeRequest);
      }

      handleOperationStatusAndMessages(response, internalResponse, context.getMessages(), true);
    } catch (Exception ex) {
      handleException(LOG, response, ex, SENSUS_MLC_ECOMODEBCLIMPL_DEFAULTEXCEPTION);
    }
    return response;
  }
  /*
   * (non-Javadoc)
   * @see
   * com.sensus.mlc.ecomode.bcf.IEcoModeBCF#upsertEcoMode(com.sensus.mlc.ecomode.model.request.InquiryEcoModeRequest)
   */
  @Override
  public EcoModeResponse upsertEcoMode(EcoModeRequest ecoModeRequest) {
    EcoModeResponse response = new EcoModeResponse();
    InternalResultsResponse<Light> internalResponse = null;

    try {
      ValidationContext context = new ValidationContext();
      context.getValidationArguments().put(getSlcActionName(), UPSERT_ECOMODE);
      context.putObjectToBeValidated(ECOMODE_REQUEST_NAME, ecoModeRequest);
      context.putObjectToBeValidated(LIGHT_LIST.getValue(), ecoModeRequest.getLights());

      if (getRequestValidationController().validate(context)
          && getLightListValidationController().validate(context)) {
        // upsert EcoMode
        internalResponse = getEcoModeBCL().upsertEcoMode(ecoModeRequest);
      }

      handleOperationStatusAndMessages(response, internalResponse, context.getMessages(), true);
    } catch (Exception ex) {
      handleException(LOG, response, ex, SENSUS_MLC_ECOMODEBCLIMPL_DEFAULTEXCEPTION);
    }
    return response;
  }
  /*
   * (non-Javadoc)
   * @see
   * com.sensus.mlc.ecomode.bcf.IEcoModeBCF#generateFileCSV(com.sensus.mlc.ecomode.model.request.InquiryEcoModeRequest
   * )
   */
  @Override
  public CSVResponse generateFileCSV(EcoModeCSVRequest ecoModeRequest) {
    CSVResponse response = new CSVResponse();
    CSVInternalResponse internalResponse = null;
    try {
      ValidationContext requestContext = new ValidationContext();
      ValidationContext context = new ValidationContext();
      context.putObjectToBeValidated(
          INQUIRY_ECOMODE_REQUEST_NAME, ecoModeRequest.getInquiryEcoModeRequest());
      context.getValidationArguments().put(getSlcActionName(), FETCH_LIGHT_CONSUMPTION);
      requestContext.putObjectToBeValidated(ECOMODE_CSV_REQUEST_NAME, ecoModeRequest);
      context.putObjectToBeValidated(
          INITIAL_DATE.getValue(), ecoModeRequest.getInquiryEcoModeRequest().getInitialDate());
      context.putObjectToBeValidated(
          END_DATE.getValue(), ecoModeRequest.getInquiryEcoModeRequest().getEndDate());

      if (!getRequestValidationController()
          .validate(requestContext)) // Validate Tenant and UserContext
      {
        handleOperationStatusAndMessages(
            response, internalResponse, requestContext.getMessages(), false);
        return response;
      }

      if (getInquiryRequestValidationController().validate(context)
          && getLightSelectionRequestValidationController().validate(context)
          && getRangeDateValidationController().validate(context)) {
        internalResponse = getEcoModeBCL().generateFileCSV(ecoModeRequest);
      }

      handleOperationStatusAndMessages(response, internalResponse, context.getMessages(), false);
    } catch (Exception ex) {
      handleException(LOG, response, ex, SENSUS_MLC_ECOMODEBCLIMPL_DEFAULTEXCEPTION);
    }

    return response;
  }