Search in sources :

Example 1 with UnrecoverableImportException

use of org.hisp.dhis.dxf2.events.event.UnrecoverableImportException in project dhis2-core by dhis2.

the class OrganisationUnitSupplier method fetchOu.

private Map<String, OrganisationUnit> fetchOu(IdScheme idScheme, Set<String> orgUnitUids, Multimap<String, String> orgUnitToEvent) {
    String sql = "select ou.organisationunitid, ou.uid, ou.code, ou.name, ou.path, ou.hierarchylevel ";
    if (idScheme.isAttribute()) {
        // 
        // Attribute IdScheme handling: use Postgres JSONB custom clauses to
        // query the
        // "attributvalues" column
        // 
        // The column is expected to contain a JSON structure like so:
        // 
        // {"ie9wfkGw8GX": {"value": "Some value", "attribute": {"id":
        // "ie9wfkGw8GX"}}}
        // 
        // The 'ie9wfkGw8GX' uid is the attribute identifier
        // 
        final String attribute = idScheme.getAttribute();
        sql += ",attributevalues->'" + attribute + "'->>'value' as " + ATTRIBUTESCHEME_COL + " from organisationunit ou where ou.attributevalues#>>'{" + attribute + ",value}' in (:ids)";
    } else {
        sql += "from organisationunit ou where ou." + IdSchemeUtils.getColumnNameByScheme(idScheme, "organisationunitid") + " in (:ids)";
    }
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("ids", orgUnitUids);
    return jdbcTemplate.query(sql, parameters, rs -> {
        Map<String, OrganisationUnit> results = new HashMap<>();
        while (rs.next()) {
            OrganisationUnit ou = mapFromResultSet(rs);
            try {
                for (String event : orgUnitToEvent.get(idScheme.isAttribute() ? rs.getString(ATTRIBUTESCHEME_COL) : getIdentifierBasedOnIdScheme(ou, idScheme))) {
                    results.put(event, ou);
                }
            } catch (Exception e) {
                throw new UnrecoverableImportException(e);
            }
        }
        return results;
    });
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) UnrecoverableImportException(org.hisp.dhis.dxf2.events.event.UnrecoverableImportException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) HashMap(java.util.HashMap) UnrecoverableImportException(org.hisp.dhis.dxf2.events.event.UnrecoverableImportException) SQLException(java.sql.SQLException)

Aggregations

SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 UnrecoverableImportException (org.hisp.dhis.dxf2.events.event.UnrecoverableImportException)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1