use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class HibernateCategoryOptionComboStore method getCategoryOptionCombo.
@Override
public DataElementCategoryOptionCombo getCategoryOptionCombo(DataElementCategoryCombo categoryCombo, Set<DataElementCategoryOption> categoryOptions) {
String hql = "from DataElementCategoryOptionCombo co where co.categoryCombo = :categoryCombo";
for (DataElementCategoryOption option : categoryOptions) {
hql += " and :option" + option.getId() + " in elements (co.categoryOptions)";
}
Query query = getQuery(hql);
query.setEntity("categoryCombo", categoryCombo);
for (DataElementCategoryOption option : categoryOptions) {
query.setEntity("option" + option.getId(), option);
}
return (DataElementCategoryOptionCombo) query.uniqueResult();
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class HibernateCategoryOptionComboStore method updateNames.
@Override
@SuppressWarnings("unchecked")
public void updateNames() {
List<DataElementCategoryOptionCombo> categoryOptionCombos = getQuery("from DataElementCategoryOptionCombo co where co.name is null").list();
int counter = 0;
Session session = getSession();
for (DataElementCategoryOptionCombo coc : categoryOptionCombos) {
session.update(coc);
if ((counter % 400) == 0) {
dbmsManager.clearSession();
}
}
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo 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.DataElementCategoryOptionCombo 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.DataElementCategoryOptionCombo 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;
}
Aggregations