use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class MinMaxOutlierAnalysisService method generateMinMaxValues.
@Override
public void generateMinMaxValues(Collection<OrganisationUnit> parents, Collection<DataElement> dataElements, Double stdDevFactor) {
log.info("Starting min-max value generation, no of data elements: " + dataElements.size() + ", no of org units: " + parents.size());
//Set<Integer> orgUnitIds = new HashSet<>( IdentifiableObjectUtils.getIdentifiers( organisationUnits ) );
Date from = new DateTime(1, 1, 1, 1, 1).toDate();
minMaxDataElementService.removeMinMaxDataElements(dataElements, parents);
log.debug("Deleted existing min-max values");
BatchHandler<MinMaxDataElement> batchHandler = batchHandlerFactory.createBatchHandler(MinMaxDataElementBatchHandler.class).init();
for (DataElement dataElement : dataElements) {
ValueType valueType = dataElement.getValueType();
if (valueType.isNumeric()) {
Collection<DataElementCategoryOptionCombo> categoryOptionCombos = dataElement.getCategoryOptionCombos();
for (DataElementCategoryOptionCombo categoryOptionCombo : categoryOptionCombos) {
Map<Integer, Double> standardDeviations = dataAnalysisStore.getStandardDeviation(dataElement, categoryOptionCombo, parents, from);
Map<Integer, Double> averages = dataAnalysisStore.getAverage(dataElement, categoryOptionCombo, parents, from);
for (Integer unit : averages.keySet()) {
Double stdDev = standardDeviations.get(unit);
Double avg = averages.get(unit);
if (stdDev != null && avg != null) {
int min = (int) MathUtils.getLowBound(stdDev, stdDevFactor, avg);
int max = (int) MathUtils.getHighBound(stdDev, stdDevFactor, avg);
if (ValueType.INTEGER_POSITIVE == valueType || ValueType.INTEGER_ZERO_OR_POSITIVE == valueType) {
// Cannot be < 0
min = Math.max(0, min);
}
if (ValueType.INTEGER_NEGATIVE == valueType) {
// Cannot be > 0
max = Math.min(0, max);
}
OrganisationUnit source = new OrganisationUnit();
source.setId(unit);
batchHandler.addObject(new MinMaxDataElement(source, dataElement, categoryOptionCombo, min, max, true));
}
}
}
}
}
log.info("Min-max value generation done");
batchHandler.flush();
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class MinMaxOutlierAnalysisService method analyse.
// -------------------------------------------------------------------------
// DataAnalysisService implementation
// -------------------------------------------------------------------------
@Override
public List<DeflatedDataValue> analyse(Collection<OrganisationUnit> parents, Collection<DataElement> dataElements, Collection<Period> periods, Double stdDevFactor, Date from) {
Set<DataElement> elements = new HashSet<>(dataElements);
FilterUtils.filter(elements, DE_NUMERIC_FILTER);
Set<DataElementCategoryOptionCombo> categoryOptionCombos = new HashSet<>();
for (DataElement dataElement : elements) {
categoryOptionCombos.addAll(dataElement.getCategoryOptionCombos());
}
log.debug("Starting min-max analysis, no of data elements: " + elements.size() + ", no of parent org units: " + parents.size());
return dataAnalysisStore.getMinMaxViolations(elements, categoryOptionCombos, periods, parents, MAX_OUTLIERS);
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DefaultDataEntryFormService method getDataElementsInDataEntryForm.
@Override
public Set<DataElement> getDataElementsInDataEntryForm(DataSet dataSet) {
if (dataSet == null || !dataSet.hasDataEntryForm()) {
return null;
}
Map<String, DataElement> dataElementMap = Maps.uniqueIndex(dataSet.getDataElements(), de -> de.getUid());
Set<DataElement> dataElements = new HashSet<>();
Matcher inputMatcher = INPUT_PATTERN.matcher(dataSet.getDataEntryForm().getHtmlCode());
while (inputMatcher.find()) {
String inputHtml = inputMatcher.group();
Matcher identifierMatcher = IDENTIFIER_PATTERN.matcher(inputHtml);
Matcher dataElementTotalMatcher = DATAELEMENT_TOTAL_PATTERN.matcher(inputHtml);
DataElement dataElement = null;
if (identifierMatcher.find() && identifierMatcher.groupCount() > 0) {
String dataElementId = identifierMatcher.group(1);
dataElement = dataElementMap.get(dataElementId);
} else if (dataElementTotalMatcher.find() && dataElementTotalMatcher.groupCount() > 0) {
String dataElementId = dataElementTotalMatcher.group(1);
dataElement = dataElementMap.get(dataElementId);
}
if (dataElement != null) {
dataElements.add(dataElement);
}
}
return dataElements;
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DataValueSMSListener method storeDataValue.
private boolean storeDataValue(String sender, OrganisationUnit orgunit, Map<String, String> parsedMessage, SMSCode code, SMSCommand command, Date date, DataSet dataSet) {
String storedBy = SmsUtils.getUser(sender, command, userService.getUsersByPhoneNumber(sender)).getUsername();
if (StringUtils.isBlank(storedBy)) {
storedBy = "[unknown] from [" + sender + "]";
}
DataElementCategoryOptionCombo optionCombo = dataElementCategoryService.getDataElementCategoryOptionCombo(code.getOptionId());
Period period = getPeriod(command, date);
DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgunit, optionCombo);
String value = parsedMessage.get(code.getCode());
Set<SMSSpecialCharacter> specialCharacters = command.getSpecialCharacters();
for (SMSSpecialCharacter each : specialCharacters) {
if (each.getName().equalsIgnoreCase(value)) {
value = each.getValue();
break;
}
}
if (!StringUtils.isEmpty(value)) {
boolean newDataValue = false;
if (dv == null) {
dv = new DataValue();
dv.setCategoryOptionCombo(optionCombo);
dv.setSource(orgunit);
dv.setDataElement(code.getDataElement());
dv.setPeriod(period);
dv.setComment("");
newDataValue = true;
}
if (ValueType.BOOLEAN == dv.getDataElement().getValueType()) {
if ("Y".equals(value.toUpperCase()) || "YES".equals(value.toUpperCase())) {
value = "true";
} else if ("N".equals(value.toUpperCase()) || "NO".equals(value.toUpperCase())) {
value = "false";
}
} else if (dv.getDataElement().getValueType().isInteger()) {
try {
Integer.parseInt(value);
} catch (NumberFormatException e) {
return false;
}
}
dv.setValue(value);
dv.setLastUpdated(new java.util.Date());
dv.setStoredBy(storedBy);
if (newDataValue) {
dataValueService.addDataValue(dv);
} else {
dataValueService.updateDataValue(dv);
}
}
if (code.getFormula() != null) {
try {
String formula = code.getFormula();
String targetDataElementId = formula.substring(1, formula.length());
String operation = String.valueOf(formula.charAt(0));
DataElement targetDataElement = dataElementService.getDataElement(Integer.parseInt(targetDataElementId));
if (targetDataElement == null) {
return false;
}
DataValue targetDataValue = dataValueService.getDataValue(targetDataElement, period, orgunit, dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
int targetValue = 0;
boolean newTargetDataValue = false;
if (targetDataValue == null) {
targetDataValue = new DataValue();
targetDataValue.setCategoryOptionCombo(dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
targetDataValue.setSource(orgunit);
targetDataValue.setDataElement(targetDataElement);
targetDataValue.setPeriod(period);
targetDataValue.setComment("");
newTargetDataValue = true;
} else {
targetValue = Integer.parseInt(targetDataValue.getValue());
}
if (operation.equals("+")) {
targetValue = targetValue + Integer.parseInt(value);
} else if (operation.equals("-")) {
targetValue = targetValue - Integer.parseInt(value);
}
targetDataValue.setValue(String.valueOf(targetValue));
targetDataValue.setLastUpdated(new java.util.Date());
targetDataValue.setStoredBy(storedBy);
if (newTargetDataValue) {
dataValueService.addDataValue(targetDataValue);
} else {
dataValueService.updateDataValue(targetDataValue);
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DefaultCsvImportService method dataElementsFromCsv.
private List<DataElement> dataElementsFromCsv(CsvReader reader) throws IOException {
DataElementCategoryCombo categoryCombo = categoryService.getDefaultDataElementCategoryCombo();
List<DataElement> list = new ArrayList<>();
while (reader.readRecord()) {
String[] values = reader.getValues();
if (values != null && values.length > 0) {
DataElement object = new DataElement();
setIdentifiableObject(object, values);
object.setShortName(getSafe(values, 3, object.getName(), 50));
object.setDescription(getSafe(values, 4, null, null));
object.setFormName(getSafe(values, 5, null, 230));
String domainType = getSafe(values, 6, DataElementDomain.AGGREGATE.getValue(), 16);
object.setDomainType(DataElementDomain.fromValue(domainType));
object.setValueType(ValueType.valueOf(getSafe(values, 7, ValueType.INTEGER.toString(), 50)));
object.setAggregationType(AggregationType.valueOf(getSafe(values, 8, AggregationType.SUM.toString(), 50)));
String categoryComboUid = getSafe(values, 9, null, 11);
object.setUrl(getSafe(values, 10, null, 255));
object.setZeroIsSignificant(Boolean.valueOf(getSafe(values, 11, "false", null)));
String optionSetUid = getSafe(values, 12, null, 11);
String commentOptionSetUid = getSafe(values, 13, null, 11);
object.setAutoFields();
DataElementCategoryCombo cc = new DataElementCategoryCombo();
cc.setUid(categoryComboUid);
cc.setAutoFields();
if (categoryComboUid == null) {
cc.setUid(categoryCombo.getUid());
}
object.setDataElementCategoryCombo(cc);
if (optionSetUid != null) {
OptionSet optionSet = new OptionSet();
optionSet.setUid(optionSetUid);
optionSet.setAutoFields();
object.setOptionSet(optionSet);
}
if (commentOptionSetUid != null) {
OptionSet optionSet = new OptionSet();
optionSet.setUid(commentOptionSetUid);
optionSet.setAutoFields();
object.setCommentOptionSet(optionSet);
}
list.add(object);
}
}
return list;
}
Aggregations