Search in sources :

Example 26 with IllegalQueryException

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

the class DefaultProgramInstanceService method prepareProgramInstance.

@Override
@Transactional
public ProgramInstance prepareProgramInstance(TrackedEntityInstance trackedEntityInstance, Program program, ProgramStatus programStatus, Date enrollmentDate, Date incidentDate, OrganisationUnit organisationUnit, String uid) {
    if (program.getTrackedEntityType() != null && !program.getTrackedEntityType().equals(trackedEntityInstance.getTrackedEntityType())) {
        throw new IllegalQueryException("Tracked entity instance must have same tracked entity as program: " + program.getUid());
    }
    ProgramInstance programInstance = new ProgramInstance();
    programInstance.setUid(CodeGenerator.isValidUid(uid) ? uid : CodeGenerator.generateUid());
    programInstance.setOrganisationUnit(organisationUnit);
    programInstance.enrollTrackedEntityInstance(trackedEntityInstance, program);
    if (enrollmentDate != null) {
        programInstance.setEnrollmentDate(enrollmentDate);
    } else {
        programInstance.setEnrollmentDate(new Date());
    }
    if (incidentDate != null) {
        programInstance.setIncidentDate(incidentDate);
    } else {
        programInstance.setIncidentDate(new Date());
    }
    programInstance.setStatus(programStatus);
    return programInstance;
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 27 with IllegalQueryException

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

the class FilteringHelper method extractValueTypeFromEqualFilter.

/**
 * This method will return the respective ValueType from the filter
 * provided.
 *
 * @param filter should have the format of "valueType:eq:NUMBER", where
 *        NUMBER represents the ValueType. It could be any value represented
 *        by {@link ValueType}
 * @return the respective value type associated with the given filter
 * @throws IllegalQueryException if the filter points to a non supported
 *         value type
 */
private static String extractValueTypeFromEqualFilter(final String filter) {
    final byte VALUE_TYPE = 2;
    String valueType = null;
    if (filterHasPrefix(filter, VALUE_TYPE_EQUAL.getCombination())) {
        final String[] array = filter.split(":");
        final boolean hasValueType = array.length == 3;
        if (hasValueType) {
            valueType = getValueTypeOrThrow(array[VALUE_TYPE]);
        } else {
            throw new IllegalQueryException(new ErrorMessage(E2014, filter));
        }
    }
    return valueType;
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ValueType.fromString(org.hisp.dhis.common.ValueType.fromString) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

Example 28 with IllegalQueryException

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

the class DataValidator method getAndValidateOrganisationUnit.

/**
 * Retrieves and verifies an organisation unit.
 *
 * @param uid the organisation unit identifier.
 * @return the {@link OrganisationUnit}.
 * @throws IllegalQueryException if the validation fails.
 */
public OrganisationUnit getAndValidateOrganisationUnit(final String uid) {
    final OrganisationUnit organisationUnit = idObjectManager.get(OrganisationUnit.class, uid);
    if (organisationUnit == null) {
        throw new IllegalQueryException(new ErrorMessage(ErrorCode.E1102, uid));
    }
    final boolean isInHierarchy = organisationUnitService.isInUserHierarchyCached(organisationUnit);
    if (!isInHierarchy) {
        throw new IllegalQueryException(new ErrorMessage(ErrorCode.E2020, uid));
    }
    return organisationUnit;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

Example 29 with IllegalQueryException

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

the class DataValidator method getAndValidateDataValue.

/**
 * Validates and retrieves a data value.
 *
 * @param dataValueRequest the {@link DataValueFollowUpRequest}.
 * @return a data value.
 * @throws IllegalQueryException if the validation fails.
 */
public DataValue getAndValidateDataValue(DataValueFollowUpRequest dataValueRequest) {
    DataElement dataElement = getAndValidateDataElement(dataValueRequest.getDataElement());
    Period period = PeriodType.getPeriodFromIsoString(dataValueRequest.getPeriod());
    OrganisationUnit orgUnit = getAndValidateOrganisationUnit(dataValueRequest.getOrgUnit());
    CategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(dataValueRequest.getCategoryOptionCombo(), false);
    CategoryOptionCombo attributeOptionCombo = getAndValidateCategoryOptionCombo(dataValueRequest.getAttributeOptionCombo(), false);
    DataValue dataValue = dataValueService.getDataValue(dataElement, period, orgUnit, categoryOptionCombo, attributeOptionCombo);
    if (dataValue == null) {
        throw new IllegalQueryException(ErrorCode.E2032);
    }
    return dataValue;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataValue(org.hisp.dhis.datavalue.DataValue) Period(org.hisp.dhis.period.Period) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 30 with IllegalQueryException

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

the class DataValidator method validateAndSetAssigned.

/**
 * Validates if the given file resource uid has a valid FileResource
 * associated with.
 *
 * @param fileResourceUid the uid of the FileResource.
 * @param valueType
 * @param valueTypeOptions
 * @return a valid FileResource.
 * @throws WebMessageException if any validation fails.
 */
public FileResource validateAndSetAssigned(final String fileResourceUid, ValueType valueType, ValueTypeOptions valueTypeOptions) throws WebMessageException {
    Preconditions.checkNotNull(fileResourceUid);
    final FileResource fileResource = fileResourceService.getFileResource(fileResourceUid);
    if (fileResource == null || fileResource.getDomain() != DATA_VALUE) {
        throw new WebMessageException(notFound(FileResource.class, fileResourceUid));
    }
    if (fileResource.isAssigned()) {
        throw new IllegalQueryException(ErrorCode.E2026);
    }
    if (valueType != null && valueTypeOptions != null) {
        String validationResult = dataValueIsValid(fileResource, valueType, valueTypeOptions);
        if (validationResult != null) {
            fileResourceService.deleteFileResource(fileResource);
            throw new IllegalQueryException(new ErrorMessage(ErrorCode.E2027, validationResult));
        }
    }
    fileResource.setAssigned(true);
    return fileResource;
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) FileResource(org.hisp.dhis.fileresource.FileResource) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

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