protected void addDataValue(
      OrganisationUnit unit,
      Period period,
      String expression,
      String value,
      Set<DataValue> oldList,
      Set<DataValue> newList) {
    // value = value.replaceAll( "\\.", "" ).replace( ",", "." );

    DataElementOperand operand =
        expressionService.getOperandsInExpression(expression).iterator().next();

    DataElement dataElement = dataElementService.getDataElement(operand.getDataElementId());

    DataElementCategoryOptionCombo optionCombo =
        categoryService.getDataElementCategoryOptionCombo(operand.getOptionComboId());

    String storedBy = currentUserService.getCurrentUsername();

    DataValue dataValue = dataValueService.getDataValue(unit, dataElement, period, optionCombo);

    if (dataValue == null) {
      dataValue =
          new DataValue(dataElement, period, unit, value, storedBy, new Date(), null, optionCombo);
      dataValueService.addDataValue(dataValue);

      newList.add(dataValue);
    } else {
      DataValue backedUpDataValue =
          new DataValue(dataElement, period, unit, dataValue.getValue(), optionCombo);

      oldList.add(backedUpDataValue);

      dataValue.setValue(value);
      dataValue.setTimestamp(new Date());
      dataValue.setStoredBy(storedBy);

      dataValueService.updateDataValue(dataValue);
    }
  }
  public String execute() {
    DataElement dataElement = dataElementService.getDataElement(dataElementId);
    DataElementCategoryOptionCombo categoryOptionCombo =
        categoryService.getDataElementCategoryOptionCombo(categoryOptionComboId);
    Period period = PeriodType.createPeriodExternalId(periodId);
    OrganisationUnit source = organisationUnitService.getOrganisationUnit(organisationUnitId);

    DataValue dataValue =
        dataValueService.getDataValue(source, dataElement, period, categoryOptionCombo);

    boolean isMarked = dataValue.isFollowup();

    dataValue.setFollowup(!isMarked);

    dataValueService.updateDataValue(dataValue);

    message = !isMarked ? "marked" : "unmarked";

    log.info(!isMarked ? "Data value marked for follow-up" : "Data value unmarked for follow-up");

    return SUCCESS;
  }
  @RequestMapping(method = RequestMethod.POST, produces = "text/plain")
  public void saveDataValue(
      @RequestParam String de,
      @RequestParam String cc,
      @RequestParam String pe,
      @RequestParam String ou,
      @RequestParam String value,
      HttpServletResponse response) {
    DataElement dataElement = dataElementService.getDataElement(de);

    if (dataElement == null) {
      ContextUtils.conflictResponse(response, "Illegal data element identifier: " + de);
      return;
    }

    DataElementCategoryOptionCombo categoryOptionCombo =
        categoryService.getDataElementCategoryOptionCombo(cc);

    if (categoryOptionCombo == null) {
      ContextUtils.conflictResponse(response, "Illegal category option combo identifier: " + cc);
      return;
    }

    Period period = PeriodType.getPeriodFromIsoString(pe);

    if (period == null) {
      ContextUtils.conflictResponse(response, "Illegal period identifier: " + pe);
      return;
    }

    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(ou);

    if (organisationUnit == null) {
      ContextUtils.conflictResponse(response, "Illegal organisation unit identifier: " + ou);
      return;
    }

    if (dataSetService.isLocked(dataElement, period, organisationUnit, null)) {
      ContextUtils.conflictResponse(response, "Data set is locked");
      return;
    }

    value = StringUtils.trimToNull(value);

    String storedBy = currentUserService.getCurrentUsername();

    Date now = new Date();

    DataValue dataValue =
        dataValueService.getDataValue(organisationUnit, dataElement, period, categoryOptionCombo);

    if (dataValue == null) {
      if (value != null) {
        dataValue =
            new DataValue(
                dataElement,
                period,
                organisationUnit,
                value,
                storedBy,
                now,
                null,
                categoryOptionCombo);
        dataValueService.addDataValue(dataValue);
      }
    } else {
      dataValue.setValue(value);
      dataValue.setTimestamp(now);
      dataValue.setStoredBy(storedBy);

      dataValueService.updateDataValue(dataValue);
    }
  }
  @Override
  public String execute() {
    optionCombo = categoryService.getDataElementCategoryOptionCombo(id);

    return SUCCESS;
  }
  @Override
  public String execute() throws Exception {
    for (String aggregateValue : aggregateValues) {
      // -----------------------------------------------------------------
      // Get params
      // -----------------------------------------------------------------

      String[] info = aggregateValue.split(SEPERATE_SIGN);

      int dataElementId = Integer.parseInt(info[0]);
      int optionComboId = Integer.parseInt(info[1]);
      String periodIsoId = info[2];
      int orgunitId = Integer.parseInt(info[3]);
      String resultValue = info[4];

      // -----------------------------------------------------------------
      // Create objects
      // -----------------------------------------------------------------

      DataElement dataElement = dataElementService.getDataElement(dataElementId);
      DataElementCategoryOptionCombo optionCombo =
          categoryService.getDataElementCategoryOptionCombo(optionComboId);

      DataElementCategoryOptionCombo attributeOptioncombo =
          categoryService.getDefaultDataElementCategoryOptionCombo();

      Period period = PeriodType.getPeriodFromIsoString(periodIsoId);

      OrganisationUnit orgunit = organisationUnitService.getOrganisationUnit(orgunitId);

      DataValue dataValue =
          dataValueService.getDataValue(
              dataElement, period, orgunit, optionCombo, attributeOptioncombo);

      // -----------------------------------------------------------------
      // Save/Update/Delete data-values
      // -----------------------------------------------------------------

      if (resultValue != "0.0") {
        if (dataValue == null) {
          dataValue =
              new DataValue(
                  dataElement,
                  period,
                  orgunit,
                  optionCombo,
                  attributeOptioncombo,
                  "" + resultValue,
                  CaseAggregationCondition.AUTO_STORED_BY,
                  new Date(),
                  null);
          dataValueService.addDataValue(dataValue);
        } else {
          dataValue.setValue(resultValue);
          dataValue.setLastUpdated(new Date());
          dataValue.setStoredBy(CaseAggregationCondition.AUTO_STORED_BY);

          dataValueService.updateDataValue(dataValue);
        }
      } else if (dataValue != null) {
        dataValueService.deleteDataValue(dataValue);
      }
    }

    return SUCCESS;
  }