/**
   * 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());
  }
  /**
   * Display the details of a KPI definition.
   *
   * @param kpiDefinitionId the KPI definition id
   */
  public Result view(Long kpiDefinitionId) {

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

    // create the table values
    List<KpiValueDefinitionListView> kpiValueDefinitionListView =
        new ArrayList<KpiValueDefinitionListView>();
    kpiValueDefinitionListView.add(
        new KpiValueDefinitionListView(kpiDefinition.mainKpiValueDefinition, DataType.MAIN));
    if (kpi.hasBoxDisplay()) {
      kpiValueDefinitionListView.add(
          new KpiValueDefinitionListView(
              kpiDefinition.additional1KpiValueDefinition, DataType.ADDITIONAL1));
      kpiValueDefinitionListView.add(
          new KpiValueDefinitionListView(
              kpiDefinition.additional2KpiValueDefinition, DataType.ADDITIONAL2));
    }
    Table<KpiValueDefinitionListView> valuesTable =
        this.getTableProvider()
            .get()
            .kpiValueDefinition
            .templateTable
            .fill(kpiValueDefinitionListView);

    // create the color rules table
    List<KpiColorRuleListView> kpiColorRuleListView = new ArrayList<KpiColorRuleListView>();
    for (KpiColorRule kpiColorRule : kpiDefinition.kpiColorRules) {
      kpiColorRuleListView.add(new KpiColorRuleListView(kpiColorRule, getI18nMessagesPlugin()));
    }
    Table<KpiColorRuleListView> rulesTable =
        this.getTableProvider().get().kpiColorRule.templateTable.fill(kpiColorRuleListView);

    return ok(views.html.admin.kpi.view.render(kpiDefinition, kpi, valuesTable, rulesTable));
  }