Search in sources :

Example 46 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class AbstractEventService method getQueryItem.

private QueryItem getQueryItem(String item) {
    String[] split = item.split(DimensionalObject.DIMENSION_NAME_SEP);
    if (split == null || split.length % 2 != 1) {
        throw new IllegalQueryException("Query item or filter is invalid: " + item);
    }
    QueryItem queryItem = getItem(split[0]);
    if (split.length > 1) {
        for (int i = 1; i < split.length; i += 2) {
            QueryOperator operator = QueryOperator.fromString(split[i]);
            queryItem.getFilters().add(new QueryFilter(operator, split[i + 1]));
        }
    }
    return queryItem;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) QueryFilter(org.hisp.dhis.common.QueryFilter) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) QueryOperator(org.hisp.dhis.common.QueryOperator)

Example 47 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class AbstractEventService method saveEvent.

private ImportSummary saveEvent(Program program, ProgramInstance programInstance, ProgramStage programStage, ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit, Event event, User user, ImportOptions importOptions) {
    Assert.notNull(program, "Program cannot be null");
    Assert.notNull(programInstance, "Program instance cannot be null");
    Assert.notNull(programStage, "Program stage cannot be null");
    ImportSummary importSummary = new ImportSummary(event.getEvent());
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    boolean existingEvent = programStageInstance != null;
    boolean dryRun = importOptions.isDryRun();
    // = new Date();
    Date executionDate = null;
    if (event.getEventDate() != null) {
        executionDate = DateUtils.parseDate(event.getEventDate());
    }
    Date dueDate = new Date();
    if (event.getDueDate() != null) {
        dueDate = DateUtils.parseDate(event.getDueDate());
    }
    String storedBy = getStoredBy(event, importSummary, user);
    String completedBy = getCompletedBy(event, importSummary, user);
    DataElementCategoryOptionCombo aoc = null;
    if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
        IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
        try {
            aoc = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
        } catch (IllegalQueryException ex) {
            importSummary.getConflicts().add(new ImportConflict(ex.getMessage(), event.getAttributeCategoryOptions()));
        }
    } else {
        aoc = categoryService.getDefaultDataElementCategoryOptionCombo();
    }
    if (!dryRun) {
        if (programStageInstance == null) {
            programStageInstance = createProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, event.getEvent(), aoc, importOptions);
        } else {
            updateProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, programStageInstance, aoc, importOptions);
        }
        updateTrackedEntityInstance(programStageInstance);
        saveTrackedEntityComment(programStageInstance, event, storedBy);
        importSummary.setReference(programStageInstance.getUid());
    }
    Map<String, TrackedEntityDataValue> dataElementValueMap = Maps.newHashMap();
    if (existingEvent) {
        dataElementValueMap = getDataElementDataValueMap(dataValueService.getTrackedEntityDataValues(programStageInstance));
    }
    for (DataValue dataValue : event.getDataValues()) {
        DataElement dataElement;
        if (dataElementValueMap.containsKey(dataValue.getDataElement())) {
            dataElement = dataElementValueMap.get(dataValue.getDataElement()).getDataElement();
        } else {
            dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), dataValue.getDataElement());
        }
        if (dataElement != null) {
            if (validateDataValue(dataElement, dataValue.getValue(), importSummary)) {
                String dataValueStoredBy = dataValue.getStoredBy() != null ? dataValue.getStoredBy() : storedBy;
                if (!dryRun) {
                    TrackedEntityDataValue existingDataValue = dataElementValueMap.get(dataValue.getDataElement());
                    saveDataValue(programStageInstance, dataValueStoredBy, dataElement, dataValue.getValue(), dataValue.getProvidedElsewhere(), existingDataValue, importSummary);
                }
            }
        } else {
            importSummary.getConflicts().add(new ImportConflict("dataElement", dataValue.getDataElement() + " is not a valid data element"));
            importSummary.getImportCount().incrementIgnored();
        }
    }
    importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
    return importSummary;
}
Also used : TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) IdScheme(org.hisp.dhis.common.IdScheme) Date(java.util.Date) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 48 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class DefaultProgramInstanceService method getFromUrl.

@Override
public ProgramInstanceQueryParams getFromUrl(Set<String> ou, OrganisationUnitSelectionMode ouMode, Date lastUpdated, String program, ProgramStatus programStatus, Date programStartDate, Date programEndDate, String trackedEntity, String trackedEntityInstance, Boolean followUp, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging) {
    ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
    if (ou != null) {
        for (String orgUnit : ou) {
            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnit);
            if (organisationUnit == null) {
                throw new IllegalQueryException("Organisation unit does not exist: " + orgUnit);
            }
            params.getOrganisationUnits().add(organisationUnit);
        }
    }
    Program pr = program != null ? programService.getProgram(program) : null;
    if (program != null && pr == null) {
        throw new IllegalQueryException("Program does not exist: " + program);
    }
    TrackedEntity te = trackedEntity != null ? trackedEntityService.getTrackedEntity(trackedEntity) : null;
    if (trackedEntity != null && te == null) {
        throw new IllegalQueryException("Tracked entity does not exist: " + program);
    }
    TrackedEntityInstance tei = trackedEntityInstance != null ? trackedEntityInstanceService.getTrackedEntityInstance(trackedEntityInstance) : null;
    if (trackedEntityInstance != null && tei == null) {
        throw new IllegalQueryException("Tracked entity instance does not exist: " + program);
    }
    params.setProgram(pr);
    params.setProgramStatus(programStatus);
    params.setFollowUp(followUp);
    params.setLastUpdated(lastUpdated);
    params.setProgramStartDate(programStartDate);
    params.setProgramEndDate(programEndDate);
    params.setTrackedEntity(te);
    params.setTrackedEntityInstance(tei);
    params.setOrganisationUnitMode(ouMode);
    params.setPage(page);
    params.setPageSize(pageSize);
    params.setTotalPages(totalPages);
    params.setSkipPaging(skipPaging);
    return params;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 49 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class InputUtils method getAttributeOptionCombo.

/**
     * Validates and retrieves the attribute option combo. 409 conflict as
     * status code along with a textual message will be set on the response in
     * case of invalid input.
     *
     * @param categoryCombo the category combo.
     * @param opts list of category option uid.
     * @param attributeOptionCombo the explicit attribute option combo
     *        identifier.
     * @return the attribute option combo identified from the given input, or
     *         null if the input was invalid.
     */
public DataElementCategoryOptionCombo getAttributeOptionCombo(DataElementCategoryCombo categoryCombo, Set<String> opts, String attributeOptionCombo, IdScheme idScheme) {
    if (categoryCombo == null) {
        throw new IllegalQueryException("Illegal category combo");
    }
    // ---------------------------------------------------------------------
    // Attribute category options validation
    // ---------------------------------------------------------------------
    DataElementCategoryOptionCombo attrOptCombo = null;
    if (opts != null) {
        Set<DataElementCategoryOption> categoryOptions = new HashSet<>();
        for (String uid : opts) {
            DataElementCategoryOption categoryOption = idObjectManager.getObject(DataElementCategoryOption.class, idScheme, uid);
            if (categoryOption == null) {
                throw new IllegalQueryException("Illegal category option identifier: " + uid);
            }
            categoryOptions.add(categoryOption);
        }
        attrOptCombo = categoryService.getDataElementCategoryOptionCombo(categoryCombo, categoryOptions);
        if (attrOptCombo == null) {
            throw new IllegalQueryException("Attribute option combo does not exist for given category combo and category options");
        }
    } else if (attributeOptionCombo != null) {
        attrOptCombo = categoryService.getDataElementCategoryOptionCombo(attributeOptionCombo);
    }
    if (attrOptCombo == null) {
        attrOptCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
    }
    if (attrOptCombo == null) {
        throw new IllegalQueryException("Default attribute option combo does not exist");
    }
    return attrOptCombo;
}
Also used : DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) HashSet(java.util.HashSet)

Example 50 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class DefaultResourceTableService method createAllSqlViews.

// -------------------------------------------------------------------------
// SQL Views. Each view is created/dropped in separate transactions so that
// process continues even if individual operations fail.
// -------------------------------------------------------------------------
@Override
public void createAllSqlViews() {
    List<SqlView> views = new ArrayList<>(sqlViewService.getAllSqlViewsNoAcl());
    Collections.sort(views);
    for (SqlView view : views) {
        if (!view.isQuery()) {
            try {
                sqlViewService.createViewTable(view);
            } catch (IllegalQueryException ex) {
                log.warn(String.format("Ignoring SQL view which failed validation: %s, %s, message: %s", view.getUid(), view.getName(), ex.getMessage()));
            }
        }
    }
}
Also used : SqlView(org.hisp.dhis.sqlview.SqlView) ArrayList(java.util.ArrayList) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException)

Aggregations

IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)98 Test (org.junit.jupiter.api.Test)26 ErrorMessage (org.hisp.dhis.feedback.ErrorMessage)22 HashSet (java.util.HashSet)17 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)17 User (org.hisp.dhis.user.User)14 QueryItem (org.hisp.dhis.common.QueryItem)13 ArrayList (java.util.ArrayList)12 Date (java.util.Date)11 Program (org.hisp.dhis.program.Program)11 QueryFilter (org.hisp.dhis.common.QueryFilter)10 TrackedEntityInstanceCriteria (org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria)10 Transactional (org.springframework.transaction.annotation.Transactional)10 QueryOperator (org.hisp.dhis.common.QueryOperator)9 List (java.util.List)8 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 DataElement (org.hisp.dhis.dataelement.DataElement)8 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)7 Map (java.util.Map)6 Set (java.util.Set)6