Search in sources :

Example 6 with OrganisationUnit

use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.

the class DhisConvenienceTest method createOrganisationUnit.

/**
     * @param name The name, short name and code of the organisation unit.
     */
public static OrganisationUnit createOrganisationUnit(String name) {
    OrganisationUnit unit = new OrganisationUnit();
    unit.setAutoFields();
    unit.setUid(CodeGenerator.generateUid());
    unit.setName(name);
    unit.setShortName(name);
    unit.setCode(name);
    unit.setOpeningDate(date);
    unit.setComment("Comment " + name);
    return unit;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit)

Example 7 with OrganisationUnit

use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.

the class DhisConvenienceTest method createOrganisationUnit.

/**
     * @param uniqueCharacter A unique character to identify the object.
     * @param parent          The parent.
     */
public static OrganisationUnit createOrganisationUnit(char uniqueCharacter, OrganisationUnit parent) {
    OrganisationUnit unit = createOrganisationUnit(uniqueCharacter);
    unit.setParent(parent);
    parent.getChildren().add(unit);
    return unit;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit)

Example 8 with OrganisationUnit

use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.

the class DataSetReportController method getDataSetReport.

@RequestMapping(method = RequestMethod.GET)
public void getDataSetReport(HttpServletResponse response, @RequestParam String ds, @RequestParam String pe, @RequestParam String ou, @RequestParam(required = false) Set<String> dimension, @RequestParam(required = false) boolean selectedUnitOnly, @RequestParam(required = false) String type) throws Exception {
    OrganisationUnit selectedOrgunit = idObjectManager.get(OrganisationUnit.class, ou);
    DataSet selectedDataSet = dataSetService.getDataSet(ds);
    Period selectedPeriod = PeriodType.getPeriodFromIsoString(pe);
    if (selectedOrgunit == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal organisation unit identifier: " + ou));
    }
    if (selectedDataSet == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal data set identifier: " + ds));
    }
    if (selectedPeriod == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal period identifier: " + pe));
    }
    selectedPeriod = periodService.reloadPeriod(selectedPeriod);
    String customDataEntryFormCode = null;
    List<Grid> grids = new ArrayList<>();
    FormType formType = selectedDataSet.getFormType();
    // ---------------------------------------------------------------------
    // Configure response
    // ---------------------------------------------------------------------
    contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_HTML, CacheStrategy.RESPECT_SYSTEM_SETTING);
    if (formType.isCustom()) {
        if (type != null) {
            grids = dataSetReportService.getCustomDataSetReportAsGrid(selectedDataSet, selectedPeriod, selectedOrgunit, dimension, selectedUnitOnly, i18nManager.getI18nFormat());
        } else {
            customDataEntryFormCode = dataSetReportService.getCustomDataSetReport(selectedDataSet, selectedPeriod, selectedOrgunit, dimension, selectedUnitOnly, i18nManager.getI18nFormat());
        }
    } else if (formType.isSection()) {
        grids = dataSetReportService.getSectionDataSetReport(selectedDataSet, selectedPeriod, selectedOrgunit, dimension, selectedUnitOnly, i18nManager.getI18nFormat(), i18nManager.getI18n());
    } else {
        grids = dataSetReportService.getDefaultDataSetReport(selectedDataSet, selectedPeriod, selectedOrgunit, dimension, selectedUnitOnly, i18nManager.getI18nFormat(), i18nManager.getI18n());
    }
    // ---------------------------------------------------------------------
    // Write response
    // ---------------------------------------------------------------------
    Writer output = response.getWriter();
    if (formType.isCustom() && type == null) {
        IOUtils.write(customDataEntryFormCode, output);
    } else {
        for (Grid grid : grids) {
            GridUtils.toHtmlCss(grid, output);
        }
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) FormType(org.hisp.dhis.dataset.FormType) Period(org.hisp.dhis.period.Period) Writer(java.io.Writer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with OrganisationUnit

use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.

the class DataValueController method getAndValidateOrganisationUnit.

private OrganisationUnit getAndValidateOrganisationUnit(String ou) throws WebMessageException {
    OrganisationUnit organisationUnit = idObjectManager.get(OrganisationUnit.class, ou);
    if (organisationUnit == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal organisation unit identifier: " + ou));
    }
    boolean isInHierarchy = organisationUnitService.isInUserHierarchy(organisationUnit);
    if (!isInHierarchy) {
        throw new WebMessageException(WebMessageUtils.conflict("Organisation unit is not in the hierarchy of the current user: " + ou));
    }
    return organisationUnit;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException)

Example 10 with OrganisationUnit

use of org.hisp.dhis.organisationunit.OrganisationUnit 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);
    }
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataValue(org.hisp.dhis.datavalue.DataValue) FileResource(org.hisp.dhis.fileresource.FileResource) Period(org.hisp.dhis.period.Period) OptionSet(org.hisp.dhis.option.OptionSet) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Date(java.util.Date) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)321 Period (org.hisp.dhis.period.Period)74 Test (org.junit.Test)63 ArrayList (java.util.ArrayList)62 User (org.hisp.dhis.user.User)57 Date (java.util.Date)53 HashSet (java.util.HashSet)53 DataSet (org.hisp.dhis.dataset.DataSet)53 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)51 DataElement (org.hisp.dhis.dataelement.DataElement)50 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)39 DhisSpringTest (org.hisp.dhis.DhisSpringTest)37 List (java.util.List)36 CurrentUserService (org.hisp.dhis.user.CurrentUserService)29 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)28 MockCurrentUserService (org.hisp.dhis.mock.MockCurrentUserService)28 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)23 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)23 Program (org.hisp.dhis.program.Program)22 ClassPathResource (org.springframework.core.io.ClassPathResource)20