use of org.hisp.dhis.category.CategoryOptionCombo in project dhis2-core by dhis2.
the class DataValueController method saveDataValueInternal.
private void saveDataValueInternal(String de, String co, String cc, String cp, String pe, String ou, String ds, String value, String comment, Boolean followUp, boolean force, User currentUser) throws WebMessageException {
boolean strictPeriods = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_PERIODS);
boolean strictCategoryOptionCombos = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_CATEGORY_OPTION_COMBOS);
boolean strictOrgUnits = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS);
boolean requireCategoryOptionCombo = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_REQUIRE_CATEGORY_OPTION_COMBO);
FileResourceRetentionStrategy retentionStrategy = systemSettingManager.getSystemSetting(SettingKey.FILE_RESOURCE_RETENTION_STRATEGY, FileResourceRetentionStrategy.class);
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = dataValueValidation.getAndValidateDataElement(de);
CategoryOptionCombo categoryOptionCombo = dataValueValidation.getAndValidateCategoryOptionCombo(co, requireCategoryOptionCombo);
CategoryOptionCombo attributeOptionCombo = dataValueValidation.getAndValidateAttributeOptionCombo(cc, cp);
Period period = dataValueValidation.getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = dataValueValidation.getAndValidateOrganisationUnit(ou);
dataValueValidation.validateOrganisationUnitPeriod(organisationUnit, period);
DataSet dataSet = dataValueValidation.getAndValidateOptionalDataSet(ds, dataElement);
dataValueValidation.validateInvalidFuturePeriod(period, dataElement);
dataValueValidation.validateAttributeOptionCombo(attributeOptionCombo, period, dataSet, dataElement);
value = dataValueValidation.validateAndNormalizeDataValue(value, dataElement);
dataValueValidation.validateComment(comment);
dataValueValidation.validateOptionSet(value, dataElement.getOptionSet(), dataElement);
dataValueValidation.checkCategoryOptionComboAccess(currentUser, categoryOptionCombo);
dataValueValidation.checkCategoryOptionComboAccess(currentUser, attributeOptionCombo);
if (strictPeriods && !dataElement.getPeriodTypes().contains(period.getPeriodType())) {
throw new WebMessageException(conflict("Period type of period: " + period.getIsoDate() + " not valid for data element: " + dataElement.getUid()));
}
if (strictCategoryOptionCombos && !dataElement.getCategoryOptionCombos().contains(categoryOptionCombo)) {
throw new WebMessageException(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(conflict("Data element: " + dataElement.getUid() + " must be assigned through data sets to organisation unit: " + organisationUnit.getUid()));
}
if (!inputUtils.canForceDataInput(currentUser, force)) {
dataValueValidation.validateDataSetNotLocked(currentUser, dataElement, period, dataSet, organisationUnit, attributeOptionCombo);
}
// ---------------------------------------------------------------------
// Period validation
// ---------------------------------------------------------------------
dataValueValidation.validateDataInputPeriodForDataElementAndPeriod(dataElement, dataSet, period);
// ---------------------------------------------------------------------
// Assemble and save data value
// ---------------------------------------------------------------------
String storedBy = currentUser.getUsername();
Date now = new Date();
DataValue persistedDataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
FileResource fileResource = null;
if (persistedDataValue == null) {
if (dataElement.getValueType().isFile()) {
fileResource = dataValueValidation.validateAndSetAssigned(value, dataElement.getValueType(), dataElement.getValueTypeOptions());
}
DataValue newValue = new DataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo, StringUtils.trimToNull(value), storedBy, now, StringUtils.trimToNull(comment));
newValue.setFollowup(followUp);
dataValueService.addDataValue(newValue);
} else {
if (value == null && comment == null && followUp == null && ValueType.TRUE_ONLY.equals(dataElement.getValueType())) {
dataValueService.deleteDataValue(persistedDataValue);
return;
}
if (dataElement.getValueType().isFile()) {
fileResource = dataValueValidation.validateAndSetAssigned(value, dataElement.getValueType(), dataElement.getValueTypeOptions());
}
if (dataElement.isFileType() && retentionStrategy == FileResourceRetentionStrategy.NONE) {
try {
fileResourceService.deleteFileResource(persistedDataValue.getValue());
} catch (AuthorizationException exception) {
// If we fail to delete the fileResource now, mark it as
// unassigned for removal later
fileResourceService.getFileResource(persistedDataValue.getValue()).setAssigned(false);
}
persistedDataValue.setValue(StringUtils.EMPTY);
}
if (value != null) {
persistedDataValue.setValue(StringUtils.trimToNull(value));
}
if (comment != null) {
persistedDataValue.setComment(StringUtils.trimToNull(comment));
}
if (followUp != null) {
persistedDataValue.toggleFollowUp();
}
persistedDataValue.setLastUpdated(now);
persistedDataValue.setStoredBy(storedBy);
dataValueService.updateDataValue(persistedDataValue);
}
if (fileResource != null) {
fileResourceService.updateFileResource(fileResource);
}
}
use of org.hisp.dhis.category.CategoryOptionCombo in project dhis2-core by dhis2.
the class DataValueController method deleteDataValue.
// ---------------------------------------------------------------------
// DELETE
// ---------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_DELETE')")
@DeleteMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteDataValue(@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 ds, @RequestParam(required = false) boolean force, @CurrentUser User currentUser, HttpServletResponse response) throws WebMessageException {
FileResourceRetentionStrategy retentionStrategy = systemSettingManager.getSystemSetting(SettingKey.FILE_RESOURCE_RETENTION_STRATEGY, FileResourceRetentionStrategy.class);
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = dataValueValidation.getAndValidateDataElement(de);
CategoryOptionCombo categoryOptionCombo = dataValueValidation.getAndValidateCategoryOptionCombo(co, false);
CategoryOptionCombo attributeOptionCombo = dataValueValidation.getAndValidateAttributeOptionCombo(cc, cp);
Period period = dataValueValidation.getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = dataValueValidation.getAndValidateOrganisationUnit(ou);
DataSet dataSet = dataValueValidation.getAndValidateOptionalDataSet(ds, dataElement);
if (!inputUtils.canForceDataInput(currentUser, force)) {
dataValueValidation.validateDataSetNotLocked(currentUser, dataElement, period, dataSet, organisationUnit, attributeOptionCombo);
}
// ---------------------------------------------------------------------
// Period validation
// ---------------------------------------------------------------------
dataValueValidation.validateDataInputPeriodForDataElementAndPeriod(dataElement, dataSet, period);
// ---------------------------------------------------------------------
// Delete data value
// ---------------------------------------------------------------------
DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
if (dataValue == null) {
throw new WebMessageException(conflict("Data value cannot be deleted because it does not exist"));
}
if (dataValue.getDataElement().isFileType() && retentionStrategy == FileResourceRetentionStrategy.NONE) {
fileResourceService.deleteFileResource(dataValue.getValue());
}
dataValueService.deleteDataValue(dataValue);
}
use of org.hisp.dhis.category.CategoryOptionCombo in project dhis2-core by dhis2.
the class RemoveMinMaxLimitsAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(organisationUnitId);
DataElement dataElement = dataElementService.getDataElement(dataElementId);
CategoryOptionCombo optionCombo = categoryService.getCategoryOptionCombo(categoryOptionComboId);
MinMaxDataElement minMaxDataElement = minMaxDataElementService.getMinMaxDataElement(organisationUnit, dataElement, optionCombo);
if (minMaxDataElement != null) {
minMaxDataElementService.deleteMinMaxDataElement(minMaxDataElement);
}
return SUCCESS;
}
use of org.hisp.dhis.category.CategoryOptionCombo in project dhis2-core by dhis2.
the class SaveMinMaxLimitsAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
minLimit = minLimit != null ? minLimit : 0;
maxLimit = maxLimit != null ? maxLimit : 0;
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(organisationUnitId);
DataElement dataElement = dataElementService.getDataElement(dataElementId);
CategoryOptionCombo optionCombo = categoryService.getCategoryOptionCombo(categoryOptionComboId);
MinMaxDataElement minMaxDataElement = minMaxDataElementService.getMinMaxDataElement(organisationUnit, dataElement, optionCombo);
if (minMaxDataElement == null) {
minMaxDataElement = new MinMaxDataElement(dataElement, organisationUnit, optionCombo, minLimit, maxLimit, false);
minMaxDataElementService.addMinMaxDataElement(minMaxDataElement);
} else {
minMaxDataElement.setMin(minLimit);
minMaxDataElement.setMax(maxLimit);
minMaxDataElement.setGenerated(false);
minMaxDataElementService.updateMinMaxDataElement(minMaxDataElement);
}
return SUCCESS;
}
use of org.hisp.dhis.category.CategoryOptionCombo in project dhis2-core by dhis2.
the class DefaultDataSetReportService method getSectionDataSetReport.
private List<Grid> getSectionDataSetReport(DataSet dataSet, List<Period> periods, OrganisationUnit unit, Set<String> filters, boolean selectedUnitOnly) {
I18nFormat format = i18nManager.getI18nFormat();
I18n i18n = i18nManager.getI18n();
List<Section> sections = new ArrayList<>(dataSet.getSections());
sections.sort(new SectionOrderComparator());
Map<String, Object> valueMap = dataSetReportStore.getAggregatedValues(dataSet, periods, unit, filters);
Map<String, Object> subTotalMap = dataSetReportStore.getAggregatedSubTotals(dataSet, periods, unit, filters);
Map<String, Object> totalMap = dataSetReportStore.getAggregatedTotals(dataSet, periods, unit, filters);
List<Grid> grids = new ArrayList<>();
for (Section section : sections) {
for (CategoryCombo categoryCombo : section.getCategoryCombos()) {
Grid grid = new ListGrid().setTitle(section.getName() + SPACE + categoryCombo.getName()).setSubtitle(unit.getName() + SPACE + formatPeriods(periods, format));
// -----------------------------------------------------------------
// Grid headers
// -----------------------------------------------------------------
grid.addHeader(new GridHeader(i18n.getString("dataelement"), false, true));
List<CategoryOptionCombo> optionCombos = categoryCombo.getSortedOptionCombos();
for (CategoryOptionCombo optionCombo : optionCombos) {
grid.addHeader(new GridHeader(optionCombo.isDefault() ? DEFAULT_HEADER : optionCombo.getName(), false, false));
}
if (// Sub-total
categoryCombo.doSubTotals() && !selectedUnitOnly) {
for (CategoryOption categoryOption : categoryCombo.getCategoryOptions()) {
grid.addHeader(new GridHeader(categoryOption.getName(), false, false));
}
}
if (// Total
categoryCombo.doTotal() && !selectedUnitOnly) {
grid.addHeader(new GridHeader(TOTAL_HEADER, false, false));
}
// -----------------------------------------------------------------
// Grid values
// -----------------------------------------------------------------
List<DataElement> dataElements = new ArrayList<>(section.getDataElementsByCategoryCombo(categoryCombo));
FilterUtils.filter(dataElements, AggregatableDataElementFilter.INSTANCE);
for (DataElement dataElement : dataElements) {
grid.addRow();
// Data
grid.addValue(new GridValue(dataElement.getFormNameFallback()));
for (// Values
CategoryOptionCombo optionCombo : // Values
optionCombos) {
Map<Object, Object> attributes = new HashMap<>();
attributes.put(ATTR_DE, dataElement.getUid());
attributes.put(ATTR_CO, optionCombo.getUid());
Object value;
if (selectedUnitOnly) {
value = getSelectedUnitValue(dataElement, periods, unit, optionCombo);
} else {
value = valueMap.get(dataElement.getUid() + SEPARATOR + optionCombo.getUid());
}
grid.addValue(new GridValue(value, attributes));
}
if (// Sub-total
categoryCombo.doSubTotals() && !selectedUnitOnly) {
for (CategoryOption categoryOption : categoryCombo.getCategoryOptions()) {
Object value = subTotalMap.get(dataElement.getUid() + SEPARATOR + categoryOption.getUid());
grid.addValue(new GridValue(value));
}
}
if (// Total
categoryCombo.doTotal() && !selectedUnitOnly) {
Object value = totalMap.get(String.valueOf(dataElement.getUid()));
grid.addValue(new GridValue(value));
}
}
grids.add(grid);
}
}
return grids;
}
Aggregations