use of org.hisp.dhis.fileresource.FileResourceRetentionStrategy 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.fileresource.FileResourceRetentionStrategy 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);
}
Aggregations