/**
   * Delete a custom KPI.
   *
   * @param kpiDefinitionId the KPI definition id
   */
  public Result delete(Long kpiDefinitionId) {

    // get the KPI
    KpiDefinition kpiDefinition = KpiDefinition.getById(kpiDefinitionId);
    Kpi kpi = new Kpi(kpiDefinition, getKpiService());

    if (!kpiDefinition.isStandard) {

      // cancel the scheduler
      kpi.cancel();

      // delete the values and data
      deleteKpiValueDefinition(kpiDefinition.mainKpiValueDefinition);
      deleteKpiValueDefinition(kpiDefinition.additional1KpiValueDefinition);
      deleteKpiValueDefinition(kpiDefinition.additional2KpiValueDefinition);

      // delete the colors
      if (kpiDefinition.kpiColorRules != null) {
        for (KpiColorRule kpiColorRule : kpiDefinition.kpiColorRules) {
          kpiColorRule.doDelete();
        }
      }

      kpiDefinition.deleted = true;
      kpiDefinition.save();

      Utilities.sendSuccessFlashMessage(Msg.get("admin.kpi.delete.successful"));

    } else {
      Utilities.sendSuccessFlashMessage(Msg.get("admin.kpi.delete.error"));
    }

    return redirect(controllers.admin.routes.KpiManagerController.index());
  }
  /**
   * Delete a KPI color rule.
   *
   * @param kpiColorRuleId the KPI color rule id
   */
  public Result deleteRule(Long kpiColorRuleId) {

    KpiColorRule kpiColorRule = KpiColorRule.getById(kpiColorRuleId);

    kpiColorRule.doDelete();

    reloadKpiDefinition(kpiColorRule.kpiDefinition.uid);

    Utilities.sendSuccessFlashMessage(Msg.get("admin.kpi.rule.delete"));

    return redirect(
        controllers.admin.routes.KpiManagerController.view(kpiColorRule.kpiDefinition.id));
  }