use of org.hisp.dhis.datavalue.DataValue in project dhis2-core by dhis2.
the class DataValueSetServiceExportTest method testExportLastUpdatedWithDeletedValues.
@Test
public void testExportLastUpdatedWithDeletedValues() {
DataValue dvA = new DataValue(deC, peA, ouA, cocA, cocA, "1");
DataValue dvB = new DataValue(deC, peB, ouA, cocA, cocA, "2");
dataValueService.addDataValue(dvA);
dataValueService.addDataValue(dvB);
dbmsManager.flushSession();
Date lastUpdated = getDate(1970, 1, 1);
ByteArrayOutputStream out = new ByteArrayOutputStream();
dataValueSetService.writeDataValueSetJson(lastUpdated, out, new IdSchemes());
DataValueSet dvs = JacksonUtils.fromJson(out.toByteArray(), DataValueSet.class);
assertNotNull(dvs);
assertEquals(14, dvs.getDataValues().size());
dataValueService.deleteDataValue(dvA);
dataValueService.deleteDataValue(dvB);
dbmsManager.flushSession();
out = new ByteArrayOutputStream();
dataValueSetService.writeDataValueSetJson(lastUpdated, out, new IdSchemes());
dvs = JacksonUtils.fromJson(out.toByteArray(), DataValueSet.class);
assertNotNull(dvs);
assertEquals(14, dvs.getDataValues().size());
}
use of org.hisp.dhis.datavalue.DataValue in project dhis2-core by dhis2.
the class DhisConvenienceTest method createDataValue.
/**
* @param dataElement The data element.
* @param period The period.
* @param source The source.
* @param value The value.
* @param categoryOptionCombo The category option combo.
* @param attributeOptionCombo The attribute option combo.
*/
public static DataValue createDataValue(DataElement dataElement, Period period, OrganisationUnit source, String value, DataElementCategoryOptionCombo categoryOptionCombo, DataElementCategoryOptionCombo attributeOptionCombo) {
DataValue dataValue = new DataValue();
dataValue.setDataElement(dataElement);
dataValue.setPeriod(period);
dataValue.setSource(source);
dataValue.setCategoryOptionCombo(categoryOptionCombo);
dataValue.setAttributeOptionCombo(attributeOptionCombo);
dataValue.setValue(value);
dataValue.setComment("Comment");
dataValue.setStoredBy("StoredBy");
return dataValue;
}
use of org.hisp.dhis.datavalue.DataValue in project dhis2-core by dhis2.
the class DataValueController method saveDataValue.
// ---------------------------------------------------------------------
// POST
// ---------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_ADD')")
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void saveDataValue(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, @RequestParam(required = false) String value, @RequestParam(required = false) String comment, @RequestParam(required = false) boolean followUp, HttpServletResponse response) throws WebMessageException {
boolean strictPeriods = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_PERIODS);
boolean strictCategoryOptionCombos = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_CATEGORY_OPTION_COMBOS);
boolean strictOrgUnits = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS);
boolean requireCategoryOptionCombo = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_REQUIRE_CATEGORY_OPTION_COMBO);
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = getAndValidateDataElement(de);
DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, requireCategoryOptionCombo);
DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
Period period = getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
validateInvalidFuturePeriod(period, dataElement);
validateAttributeOptionComboWithOrgUnitAndPeriod(attributeOptionCombo, organisationUnit, period);
String valueValid = ValidationUtils.dataValueIsValid(value, dataElement);
if (valueValid != null) {
throw new WebMessageException(WebMessageUtils.conflict("Invalid value: " + value + ", must match data element type: " + dataElement.getValueType()));
}
String commentValid = ValidationUtils.commentIsValid(comment);
if (commentValid != null) {
throw new WebMessageException(WebMessageUtils.conflict("Invalid comment: " + comment));
}
OptionSet optionSet = dataElement.getOptionSet();
if (!Strings.isNullOrEmpty(value) && optionSet != null && !optionSet.getOptionCodesAsSet().contains(value)) {
throw new WebMessageException(WebMessageUtils.conflict("Data value is not a valid option of the data element option set: " + dataElement.getUid()));
}
if (strictPeriods && !dataElement.getPeriodTypes().contains(period.getPeriodType())) {
throw new WebMessageException(WebMessageUtils.conflict("Period type of period: " + period.getIsoDate() + " not valid for data element: " + dataElement.getUid()));
}
if (strictCategoryOptionCombos && !dataElement.getCategoryOptionCombos().contains(categoryOptionCombo)) {
throw new WebMessageException(WebMessageUtils.conflict("Category option combo: " + categoryOptionCombo.getUid() + " must be part of category combo of data element: " + dataElement.getUid()));
}
if (strictOrgUnits && !organisationUnit.hasDataElement(dataElement)) {
throw new WebMessageException(WebMessageUtils.conflict("Data element: " + dataElement.getUid() + " must be assigned through data sets to organisation unit: " + organisationUnit.getUid()));
}
// ---------------------------------------------------------------------
// Locking validation
// ---------------------------------------------------------------------
validateDataSetNotLocked(dataElement, period, organisationUnit, attributeOptionCombo);
// ---------------------------------------------------------------------
// Period validation
// ---------------------------------------------------------------------
validateDataInputPeriodForDataElementAndPeriod(dataElement, period);
// ---------------------------------------------------------------------
// Assemble and save data value
// ---------------------------------------------------------------------
String storedBy = currentUserService.getCurrentUsername();
Date now = new Date();
DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
FileResource fileResource = null;
if (dataValue == null) {
if (dataElement.getValueType() == ValueType.FILE_RESOURCE) {
if (value != null) {
fileResource = fileResourceService.getFileResource(value);
if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
throw new WebMessageException(WebMessageUtils.notFound(FileResource.class, value));
}
if (fileResource.isAssigned()) {
throw new WebMessageException(WebMessageUtils.conflict("File resource already assigned or linked to another data value"));
}
fileResource.setAssigned(true);
} else {
throw new WebMessageException(WebMessageUtils.conflict("Missing parameter 'value'"));
}
}
dataValue = new DataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo, StringUtils.trimToNull(value), storedBy, now, StringUtils.trimToNull(comment));
dataValueService.addDataValue(dataValue);
} else {
if (value == null && ValueType.TRUE_ONLY.equals(dataElement.getValueType())) {
if (comment == null) {
dataValueService.deleteDataValue(dataValue);
return;
} else {
value = "false";
}
}
if (dataElement.isFileType()) {
fileResourceService.deleteFileResource(dataValue.getValue());
}
if (value != null) {
dataValue.setValue(StringUtils.trimToNull(value));
}
if (comment != null) {
dataValue.setComment(StringUtils.trimToNull(comment));
}
if (followUp) {
dataValue.toggleFollowUp();
}
dataValue.setLastUpdated(now);
dataValue.setStoredBy(storedBy);
dataValueService.updateDataValue(dataValue);
}
if (fileResource != null) {
fileResourceService.updateFileResource(fileResource);
}
}
use of org.hisp.dhis.datavalue.DataValue in project dhis2-core by dhis2.
the class DefaultPredictorService method writeDataValue.
/**
* Writes (adds or updates) a predicted data value to the database.
*
* @param dataElement the data element.
* @param period the period.
* @param orgUnit the organisation unit.
* @param categoryOptionCombo the category option combo.
* @param attributeOptionCombo the attribute option combo.
* @param value the value.
* @param storedBy the user that will store this data value.
*/
private void writeDataValue(DataElement dataElement, Period period, OrganisationUnit orgUnit, DataElementCategoryOptionCombo categoryOptionCombo, DataElementCategoryOptionCombo attributeOptionCombo, String value, String storedBy) {
DataValue existingValue = dataValueService.getDataValue(dataElement, period, orgUnit, categoryOptionCombo, attributeOptionCombo);
if (existingValue != null) {
existingValue.setValue(value);
existingValue.setStoredBy(storedBy);
dataValueService.updateDataValue(existingValue);
} else {
DataValue dv = new DataValue(dataElement, period, orgUnit, categoryOptionCombo, attributeOptionCombo, value, storedBy, null, null);
dataValueService.addDataValue(dv);
}
}
use of org.hisp.dhis.datavalue.DataValue in project dhis2-core by dhis2.
the class HibernateDataValueStore method getDataValue.
@Override
public DataValue getDataValue(DataElement dataElement, Period period, OrganisationUnit source, DataElementCategoryOptionCombo categoryOptionCombo, DataElementCategoryOptionCombo attributeOptionCombo) {
Session session = sessionFactory.getCurrentSession();
Period storedPeriod = periodStore.reloadPeriod(period);
if (storedPeriod == null) {
return null;
}
return (DataValue) session.createCriteria(DataValue.class).add(Restrictions.eq("dataElement", dataElement)).add(Restrictions.eq("period", storedPeriod)).add(Restrictions.eq("source", source)).add(Restrictions.eq("categoryOptionCombo", categoryOptionCombo)).add(Restrictions.eq("attributeOptionCombo", attributeOptionCombo)).add(Restrictions.eq("deleted", false)).uniqueResult();
}
Aggregations