コード例 #1
0
  /**
   * 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;
  }
コード例 #2
0
  /**
   * 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);
    }
  }
コード例 #3
0
  /**
   * Calculate the key value for a calculated definition.
   *
   * @param def the definition
   * @param kv the key value
   */
  private static void calculateCalculatedKeyValue(final Definition def, final KeyValue kv) {

    // Load all of the variable values for this definition/record combination
    HashMap<Long, Double> values = getCalculatedValues(def.getCalculatedDefinitions(), kv);

    logger.info("Number of values: " + values.size());
    logger.info("Key value id: " + kv.getId());

    // No submitted fields associated with a calculated definition
    kv.setSubmittedFieldCount(0);

    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 {
      double result = 0;
      try {
        result = CalculationParser.performCalculation(def.getPlainTextCalculation(), values);
      } catch (Exception e) {
        logger.error("Error calculating value: " + e.getMessage());
      }

      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);
    }
    if (refresh) {
      recalculateRelatedDefinitions(def, kv);
    }
  }
コード例 #4
0
  /**
   * Calculate the key value for a summary definition.
   *
   * @param def the definition
   * @param kv the key value
   */
  private static void calculateSummarisedKeyValue(final Definition def, final KeyValue kv) {

    // Load all of the contributed values for this definition/record combination

    List<Definition> definitions = Definition.findSummarisedDefinitions(def);

    List<Object> values = getSummarisedValues(definitions, kv);

    logger.info("Number of values: " + values.size());
    logger.info("Key value id: " + kv.getId());

    // No submitted fields associated with a summary definition
    kv.setSubmittedFieldCount(0);

    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 {
      Object result = KeyValueGenerator.calculateFromObjects(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);
    }

    if (refresh) {
      recalculateRelatedDefinitions(def, kv);
    }
  }
コード例 #5
0
  /**
   * Check to see if the value has changed.
   *
   * @param kv the kv
   * @param newValue the new value
   * @return true, if successful
   */
  private static boolean checkChanged(final KeyValue kv, Object newValue) {

    boolean refresh = false;

    if (kv.getId() == null) {
      refresh = true;
    } else {
      if (newValue == null) {
        refresh = true;
      } else {
        if (newValue instanceof String) {
          if (!StringUtils.equalsIgnoreCase((String) newValue, kv.getStringValue())) {
            refresh = true;
          }
        }
        if (newValue instanceof Double) {
          if (kv.getDoubleValue() != null) {
            if ((Double) newValue != kv.getDoubleValue()) {
              refresh = true;
            }
          } else {
            refresh = true;
          }
        }
        if (newValue instanceof KeyValueBoolean) {
          if (kv.getBooleanValue() != null) {
            if ((KeyValueBoolean) newValue != kv.getBooleanValue()) {
              refresh = true;
            }
          } else {
            refresh = true;
          }
        }
      }
    }
    return refresh;
  }
コード例 #6
0
  /**
   * 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);
    }
  }