/*
   * (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.light.bcf.ILightBCF#updateLightMass(com.sensus.mlc.light.model.request.LightMassUpdateRequest)
   */
  @Override
  public MaintenanceResponse updateLightMass(LightMassUpdateRequest request) {
    MaintenanceResponse response = new MaintenanceResponse();

    try {
      InternalResponse internalResponse = null;

      ValidationContext context = new ValidationContext();
      context.putObjectToBeValidated(LIGHT_MASS_UPDATE_REQUEST_NAME, request);
      context.putObjectToBeValidated(LIGHT_NAME, request.getLight());

      if (getRequestValidationController().validate(context) // Validate Tenant and UserContext
          && getLightValidationController().validate(context)) // Validate light attributes
      {
        internalResponse = getLightBCL().updateLightMass(request);
      }

      handleOperationStatusAndMessages(response, internalResponse, context.getMessages(), false);

    } catch (Exception ex) {
      handleException(LOG, response, ex, DEFAULT_LIGHT_BCF_EXCEPTION_MSG);
    }

    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;
  }
  /*
   * (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;
  }
  /*
   * (non-Javadoc)
   * @see com.sensus.mlc.tabela.bcf.ITabelaBCF#fetchAllTabelas(com.sensus.mlc.tabela.model.request.InquiryTabelaRequest
   */
  @Override
  public InquiryTabelaResponse fetchAllTabela(InquiryTabelaRequest inquiryTabelaRequest) {
    InquiryTabelaResponse response = new InquiryTabelaResponse();
    InternalResultsResponse<Tabela> internalResponse = null;

    try {
      ValidationContext context = new ValidationContext();
      context.putObjectToBeValidated(INQUIRY_TABELA_REQUEST_NAME, inquiryTabelaRequest);

      if (getLightingControlRequestValidationController().validate(context)) {
        internalResponse = getTabelaBCL().fetchAllTabela(inquiryTabelaRequest);
      }

      handleOperationStatusAndMessages(response, internalResponse, context.getMessages(), true);
    } catch (Exception ex) {
      SensusInterfaceUtil.handleException(LOG, response, ex, DEFAULT_TABELA_BCF_EXCEPTION_MSG);
    }

    return response;
  }