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