/**
   * Recalculate calculated definitions by identifying definitions that use the supplied definition
   * in their calculation and calling the calculate function.
   *
   * @param definition the definition
   * @param kv the key value
   */
  private static void recalculateCalculatedDefinitions(
      final Definition definition, final KeyValue kv) {

    if (definition != null && kv != null) {
      List<Definition> defs = Definition.findDefinitionsWithVariable(definition);

      if (defs != null) {
        for (Definition def : defs) {
          try {
            calculateKeyValue(
                def, kv.getPrimaryRecordId(), kv.getSecondaryRecordId(), kv.getTertiaryRecordId());
          } catch (Exception e) {
            logger.error(
                "Error calculating calculated definition ("
                    + def.getSummaryDefinition().getId()
                    + ") for key value ("
                    + kv.getPrimaryRecordId()
                    + kv.getSecondaryRecordId()
                    + kv.getTertiaryRecordId()
                    + "): "
                    + e.getMessage());
          }
        }
      }
    }
  }
  /**
   * Recalculate any related definitions.
   *
   * @param def the definition
   * @param kv the key value
   */
  private static void recalculateRelatedDefinitions(final Definition def, final KeyValue kv) {

    if (def.getSummaryDefinition() != null) {
      // Recalculate the associated summary definition

      Definition summaryDef = def.getSummaryDefinition();
      logger.info("Summary definition: " + summaryDef.getId());

      try {
        calculateKeyValue(
            summaryDef,
            kv.getPrimaryRecordId(),
            kv.getSecondaryRecordId(),
            kv.getTertiaryRecordId());
      } catch (Exception e) {
        logger.error(
            "Error calculating summarised definition ("
                + def.getSummaryDefinition().getId()
                + ") for key value ("
                + kv.getPrimaryRecordId()
                + kv.getSecondaryRecordId()
                + kv.getTertiaryRecordId()
                + "): "
                + e.getMessage(),
            e);
      }
    }
    // Recalculate any associated calculated definitions
    recalculateCalculatedDefinitions(def, kv);
  }
  /**
   * Gets the calculated values as a map of doubles.
   *
   * @param definitions the definitions
   * @param keyValue the key value
   * @return the calculated values
   */
  private static HashMap<Long, Double> getCalculatedValues(
      final List<Definition> definitions, final KeyValue keyValue) {

    HashMap<Long, Double> values = new HashMap<Long, Double>();

    for (Definition def : definitions) {
      double value = 0;

      KeyValue kv =
          KeyValue.findKeyValue(
              def,
              keyValue.getPrimaryRecordId(),
              keyValue.getSecondaryRecordId(),
              keyValue.getTertiaryRecordId());

      if (kv != null) {
        Object kvValue = getKeyValueAsObject(def, kv);

        if (kvValue instanceof Double) {
          value = (Double) kvValue;
        }
      }
      values.put(def.getId(), value);
    }
    return values;
  }
  /**
   * Gets the summarised values as a list of objects.
   *
   * @param definitions the definitions
   * @param keyValue the key value
   * @return the summarised values
   */
  private static List<Object> getSummarisedValues(
      List<Definition> definitions, final KeyValue keyValue) {

    List<Object> values = new ArrayList<Object>();

    for (Definition def : definitions) {
      KeyValue kv =
          KeyValue.findKeyValue(
              def,
              keyValue.getPrimaryRecordId(),
              keyValue.getSecondaryRecordId(),
              keyValue.getTertiaryRecordId());

      if (kv != null) {
        values.add(getKeyValueAsObject(def, kv));
      }
    }
    return values;
  }
  /**
   * Calculate the key value for a standard definition.
   *
   * @param def the definition
   * @param kv the key value
   */
  private static void calculateStandardKeyValue(final Definition def, final KeyValue kv) {

    // Load all of the contributed values for this definition/record combination
    List<String> values = new ArrayList<String>();

    try {
      List<SubmittedField> fields =
          SubmittedField.findSubmittedFields(
              def, kv.getPrimaryRecordId(), kv.getSecondaryRecordId(), kv.getTertiaryRecordId());

      for (SubmittedField field : fields) {
        values.add(field.getValue());
      }
    } catch (Exception e) {
      logger.error(
          "Error loading submitted fields for record "
              + kv.getPrimaryRecordId()
              + "-"
              + kv.getSecondaryRecordId()
              + "-"
              + kv.getTertiaryRecordId()
              + ": "
              + e.getMessage());
    }

    logger.info("Number of values: " + values.size());
    logger.info("Values: " + values);
    logger.info("Key value id: " + kv.getId());
    logger.info("Primary record id: " + kv.getPrimaryRecordId());
    logger.info("Secondary record id: " + kv.getSecondaryRecordId());
    logger.info("Tertiary record id: " + kv.getTertiaryRecordId());

    kv.setSubmittedFieldCount(values.size());

    boolean refresh = false;

    if (values.size() == 0) {
      // No submitted values exist - delete the key value if one exists
      if (kv.getId() != null && kv.getId() > 0) {
        deleteKeyValue(kv);
        refresh = true;
      }
    } else {
      // Check to see if the definition is applicable to the key value
      boolean applicable = true;

      if (def.getApplicability() == Applicability.RECORD_SECONDARY
          && StringUtils.isBlank(kv.getSecondaryRecordId())) {
        // Not applicable as no secondary record id is defined
        applicable = false;
      }

      if (def.getApplicability() == Applicability.RECORD_SECONDARY
          && StringUtils.isBlank(kv.getTertiaryRecordId())) {
        // Not applicable as no tertiary record id is defined
        applicable = false;
      }

      if (applicable) {
        Object result = KeyValueGenerator.calculateFromRawStrings(def, values);

        refresh = checkChanged(kv, result);

        kv.setValue(result);
        logger.info("Calculated string value: " + kv.getStringValue());
        logger.info("Calculated double value: " + kv.getDoubleValue());

        // Save the key value
        saveKeyValue(kv);

      } else {
        logger.error(
            "This key value "
                + kv.getPrimaryRecordId()
                + ":"
                + kv.getSecondaryRecordId()
                + ":"
                + kv.getTertiaryRecordId()
                + " is not applicable to this definition: "
                + def.getName());
      }
    }

    if (refresh) {
      recalculateRelatedDefinitions(def, kv);
    }
  }