package nl.thehyve.ocdu.validators.crossChecks;

import nl.thehyve.ocdu.models.OCEntities.ClinicalData;
import nl.thehyve.ocdu.models.OcDefinitions.CRFDefinition;
import nl.thehyve.ocdu.models.OcDefinitions.ItemDefinition;
import nl.thehyve.ocdu.models.OcDefinitions.MetaData;
import nl.thehyve.ocdu.models.OcDefinitions.RangeCheck;
import nl.thehyve.ocdu.models.errors.RangeCheckViolation;
import nl.thehyve.ocdu.models.errors.ValidationErrorMessage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.DoubleAccumulator;

/**
 * Created by piotrzakrzewski on 15/05/16.
 */
public class RangeChecks implements ClinicalDataCrossCheck {
    @Override
    public ValidationErrorMessage getCorrespondingError(List<ClinicalData> data, MetaData metaData) {
        RangeCheckViolation error = new RangeCheckViolation();

        data.forEach(clinicalData -> {
            ItemDefinition itemDefinition = getMatching(clinicalData, metaData);
            if (itemDefinition != null) { // Nonexistent item is a separate error
                List<RangeCheck> rangeCheckList = itemDefinition.getRangeCheckList();
                rangeCheckList.forEach(rangeCheck -> {
                    List<String> values = clinicalData.getValues();
                    for (String value : values) {
                        if (isFloat(value) || isInteger(value)) {
                            int intValue = (int) Double.parseDouble(value); // Do not attempt floating point comparison
                            if (!rangeCheck.isInRange(intValue)) {
                                String msg = clinicalData.getItem() + " " + rangeCheck.violationMessage()
                                        + " but was: " + intValue;
                                error.addOffendingValue(msg);
                            }
                        } // If item is not numeric but should be - there is a separate error for that
                    }
                });
            }
        });
        if (error.getOffendingValues().size() > 0) {
            return error;
        } else
            return null;
    }
}

@@ -467,60 +467,9 @@
 til.
-HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
+*
 ;%0Aim
@@ -812,16 +812,70 @@
 tion();%0A
+        Set%3CString%3E alreadyReported = new HashSet%3C%3E();
 %0A       
@@ -1790,37 +1790,208 @@
     
-error.addOffendingValue(msg);
+if (!alreadyReported.contains(msg)) %7B%0A                                    error.addOffendingValue(msg);%0A                                    alreadyReported.add(msg);%0A                                %7D%0A
 %0A   

Remove rendundat error messages generated by RangeCheck