/*
   * (non-Javadoc)
   * @see com.sensus.mlc.light.bcf.ILightBCF#fetchById(com.sensus.mlc.light.model.request.FetchByIdRequest)
   */
  @Override
  public LightResponse fetchById(FetchByIdRequest request) {
    LightResponse response = new LightResponse();

    try {
      InternalResultsResponse<Light> lightResponse = null;

      ValidationContext context =
          new ValidationContext(
              LightRequest.class.getSimpleName(),
              request,
              MLCPersistanceActionEnum.getSlcActionName(),
              MLCPersistanceActionEnum.FETCH_BY_ID);

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

      if (ValidationUtil.isNull(request.getLightId())
          && ValidationUtil.isNull(request.getRniId())) {
        response.addMessage(
            MessageInfo.createFieldValidationError(SENSUS_MLC_VALIDATOR_ID_RNI_REQUIRED));
        return response;
      }

      lightResponse = getLightBCL().fetchById(request);
      response.setLight(lightResponse.getFirstResult());
      handleOperationStatusAndMessages(response, lightResponse, context.getMessages(), false);
    } catch (Exception ex) {
      handleException(LOG, response, ex, DEFAULT_LIGHT_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#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#updateSchedule(com.sensus.mlc.light.model.request.ScheduleMaintenanceRequest)
   */
  @Override
  public ScheduleMaintenanceResponse updateSchedule(ScheduleMaintenanceRequest request) {
    ScheduleMaintenanceResponse response = new ScheduleMaintenanceResponse();

    try {
      InternalResponse internalResponse = null;

      ValidationContext context =
          new ValidationContext(
              ScheduleMaintenanceRequest.class.getSimpleName(),
              request,
              MLCPersistanceActionEnum.getSlcActionName(),
              MLCPersistanceActionEnum.UPDATE_SCHEDULE);

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

      if (ValidationUtil.isNull(request.getSchedule())
          || ValidationUtil.isNull(request.getSchedule().getParentId())) {
        response.addMessage(
            MessageInfo.createFieldValidationError(SENSUS_MLC_VALIDATOR_PARENT_ID_REQUIRED));
        return response;
      }

      internalResponse = getLightBCL().updateSchedule(request);
      handleOperationStatusAndMessages(response, internalResponse, null, true);

    } catch (Exception ex) {
      SensusInterfaceUtil.handleException(LOG, response, ex, DEFAULT_LIGHT_BCF_EXCEPTION_MSG);
    }
    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.light.bcf.ILightBCF#resetMinMaxValue(com.sensus.mlc.light.model.request.LightRequest)
   */
  @Override
  public LightResponse resetMinMaxValue(LightRequest request) {
    LightResponse response = new LightResponse();
    InternalResultsResponse<Light> internalResponse = null;

    try {
      ValidationContext context =
          new ValidationContext(
              LightRequest.class.getSimpleName(),
              request,
              MLCPersistanceActionEnum.getSlcActionName(),
              MLCPersistanceActionEnum.RESET_MIN_MAX);
      checkDefaultsFetchAll(request);
      if (getRequestValidationController().validate(context) // Validate Tenant and UserContext
          && getInquiryRequestValidationController().validate(context)) // Validate pagination
      {
        internalResponse = getLightBCL().resetMinMaxValue(request);
        response.setLights(internalResponse.getResultsList());
      }

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

    } catch (Exception ex) {

      handleException(LOG, response, ex, DEFAULT_LIGHT_BCF_EXCEPTION_MSG);
    }

    return response;
  }
  /*
   * (non-Javadoc)
   * @see com.sensus.mlc.light.bcf.ILightBCF#fetchMapBoundsByRequest(com.sensus.mlc.light.model.request.LightRequest)
   */
  @Override
  public GeocodeLightInfoResponse fetchMapBoundsByRequest(LightRequest request) {
    GeocodeLightInfoResponse response = new GeocodeLightInfoResponse();
    InternalResultsResponse<GeocodeLightInfo> internalResponse =
        new InternalResultsResponse<GeocodeLightInfo>();

    try {
      ValidationContext context =
          new ValidationContext(
              LightRequest.class.getSimpleName(),
              request,
              MLCPersistanceActionEnum.getSlcActionName(),
              MLCPersistanceActionEnum.FETCH_LIGHT_MAP);

      if (getRequestValidationController().validate(context)) // Validate Tenant and UserContext
      {
        internalResponse = getLightBCL().fetchMapBoundsByRequest(request);
      }

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

    } catch (Exception ex) {

      handleException(LOG, response, ex, DEFAULT_LIGHT_BCF_EXCEPTION_MSG);
    }

    return response;
  }
  /*
   * (non-Javadoc)
   * @see com.sensus.mlc.light.bcf.ILightBCF#fetchAllByRequest(com.sensus.mlc.light.model.request.LightRequest)
   */
  @Override
  public FetchAllResponse fetchAllByRequest(LightRequest request) {
    InternalResultsResponse<Light> lightResponse = null;
    FetchAllResponse response = new FetchAllResponse();

    try {
      ValidationContext context =
          new ValidationContext(
              LightRequest.class.getSimpleName(),
              request,
              MLCPersistanceActionEnum.getSlcActionName(),
              MLCPersistanceActionEnum.FETCH);

      checkDefaultsFetchAll(request);
      if (getRequestValidationController().validate(context) // Validate Tenant and UserContext
          && getInquiryRequestValidationController().validate(context)) // Validate pagination
      {
        lightResponse = getLightBCL().fetchAllByRequest(request);
      }

      handleOperationStatusAndMessages(response, lightResponse, context.getMessages(), true);

    } catch (Exception ex) {

      handleException(LOG, response, ex, DEFAULT_LIGHT_BCF_EXCEPTION_MSG);
    }

    return response;
  }
  /*
   * (non-Javadoc)
   * @see com.sensus.mlc.light.bcf.ILightBCF#countAllByRequest(com.sensus.mlc.light.model.request.LightRequest)
   */
  @Override
  public CountResponse countAllByRequest(LightRequest request) {
    InternalResultsResponse<Integer> response = null;
    CountResponse countResponse = new CountResponse();

    try {
      ValidationContext context =
          new ValidationContext(
              LightRequest.class.getSimpleName(),
              request,
              MLCPersistanceActionEnum.getSlcActionName(),
              MLCPersistanceActionEnum.FETCH);

      if (getRequestValidationController().validate(context) // Validate Tenant and UserContext
          && getInquiryRequestValidationController().validate(context)) // Validate pagination
      {
        response = getLightBCL().countAllByRequest(request);

        if (!ValidationUtil.isNull(response) && !response.isInError()) {
          countResponse.setCount(response.getFirstResult());
        }
      }

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

    } catch (Exception ex) {

      handleException(LOG, countResponse, ex, DEFAULT_LIGHT_BCF_EXCEPTION_MSG);
    }

    return countResponse;
  }
  /*
   * (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#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;
  }
  /*
   * (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;
  }