/**
   * Get the existing key value, or prepare a new key value for calculating.
   *
   * @param def the def
   * @param primaryRecordId the primary record id
   * @param secondaryRecordId the secondary record id
   * @param tertiaryRecordId the tertiary record id
   * @return the key value
   */
  private static KeyValue prepareKeyValue(
      final Definition def,
      final String primaryRecordId,
      final String secondaryRecordId,
      final String tertiaryRecordId) {

    MetahivePreferences preferences = MetahivePreferences.load();

    String primaryId = primaryRecordId;
    String secondaryId = "";
    String tertiaryId = "";

    if (StringUtils.isNotBlank(secondaryRecordId)) {
      secondaryId = parseRecordId(secondaryRecordId);
    }
    if (StringUtils.isNotBlank(tertiaryRecordId)) {
      tertiaryId = parseRecordId(tertiaryRecordId);
    }

    if (StringUtils.isBlank(secondaryId)
        && StringUtils.isNotBlank(preferences.getSecondaryRecordDefault())) {
      secondaryId = preferences.getSecondaryRecordDefault();
    }

    if (StringUtils.isBlank(tertiaryId)
        && StringUtils.isNotBlank(preferences.getTertiaryRecordDefault())) {
      tertiaryId = preferences.getTertiaryRecordDefault();
    }

    Record recd = Record.findRecordByRecordIdEquals(primaryId);

    if (recd == null) {
      throw new IllegalArgumentException("A valid primaryRecordId is required");
    }

    KeyValue kv = KeyValue.findKeyValue(def, primaryId, secondaryId, tertiaryId);

    if (kv != null) {
      logger.info("Key value id: " + kv.getId());
    } else {
      logger.info("Key value is null");
    }

    // If the key value is still null then this is a new record
    if (kv == null) {
      kv = new KeyValue();
      kv.setDefinition(def);
      kv.setRecord(recd);
      kv.setKeyValueType(KeyValueType.CALCULATED);
      kv.setPrimaryRecordId(primaryId);
      kv.setSecondaryRecordId(secondaryId);
      kv.setTertiaryRecordId(tertiaryId);
    }

    return kv;
  }
  /**
   * Calculate the key value.
   *
   * @param def the def
   * @param primaryId the primary id
   * @param secondaryId the secondary id
   * @param tertiaryId the tertiary id
   */
  public static void calculateKeyValue(
      final Definition def,
      final String primaryId,
      final String secondaryId,
      final String tertiaryId) {

    if (def == null) {
      throw new IllegalArgumentException("A valid definition is required");
    }
    if (StringUtils.isBlank(primaryId)) {
      throw new IllegalArgumentException("A valid primaryId is required");
    }
    // Check that the record exists
    Record record = Record.findRecordByRecordIdEquals(primaryId);
    if (record == null) {
      throw new IllegalArgumentException(
          "A valid primaryId is required," + " no record exists in the Metahive");
    }

    KeyValue kv = prepareKeyValue(def, primaryId, secondaryId, tertiaryId);

    if (kv.getId() != null) {
      logger.info("Key value id: " + kv.getId());
      logger.info("Key value type: " + kv.getKeyValueType());
    }

    // If the key value is overridden then there's no need to recalculate it
    if (kv.getKeyValueType() != KeyValueType.OVERRIDDEN) {
      if (def.getDefinitionType() == DefinitionType.STANDARD) {
        calculateStandardKeyValue(def, kv);
      }
      if (def.getDefinitionType() == DefinitionType.SUMMARY) {
        calculateSummarisedKeyValue(def, kv);
      }
      if (def.getDefinitionType() == DefinitionType.CALCULATED) {
        calculateCalculatedKeyValue(def, kv);
      }
    } else {
      // There's no need to recalculate the value,
      // but the related definitions need updating.
      recalculateRelatedDefinitions(def, kv);
    }
  }