Search in sources :

Example 6 with NotAllowedException

use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method updatePatient.

@Override
public Patient updatePatient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId, String programIdText) throws NotAllowedException {
    TrackedEntityInstance entityInstance = entityInstanceService.getTrackedEntityInstance(patient.getId());
    TrackedEntityInstance tempTEI = entityInstance;
    TrackedEntity trackedEntity = null;
    Program program = programService.getProgram(Integer.parseInt(programIdText));
    trackedEntity = program.getTrackedEntity();
    entityInstance.setTrackedEntity(trackedEntity);
    // get attributes to be saved/updated/deleted
    Collection<TrackedEntityAttribute> attributes = attributeService.getAllTrackedEntityAttributes();
    List<TrackedEntityAttributeValue> valuesForSave = new ArrayList<>();
    List<TrackedEntityAttributeValue> valuesForUpdate = new ArrayList<>();
    Collection<TrackedEntityAttributeValue> valuesForDelete = null;
    TrackedEntityAttributeValue attributeValue = null;
    Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
    if (attributes != null && attributes.size() > 0) {
        valuesForDelete = attValueService.getTrackedEntityAttributeValues(entityInstance);
        tempTEI.getTrackedEntityAttributeValues().clear();
        for (TrackedEntityAttribute attribute : attributes) {
            String value = getAttributeValue(attributesMobile, attribute.getName());
            if (value != null) {
                attributeValue = attValueService.getTrackedEntityAttributeValue(entityInstance, attribute);
                if (attributeValue == null) {
                    attributeValue = new TrackedEntityAttributeValue();
                    attributeValue.setEntityInstance(entityInstance);
                    attributeValue.setAttribute(attribute);
                    attributeValue.setValue(value.trim());
                    valuesForSave.add(attributeValue);
                } else {
                    attributeValue.setValue(value.trim());
                    valuesForUpdate.add(attributeValue);
                    valuesForDelete.remove(attributeValue);
                }
                tempTEI.getTrackedEntityAttributeValues().add(attributeValue);
            }
        }
    }
    // validate
    String[] errorCode = entityInstanceService.validateTrackedEntityInstance(tempTEI, program).split("_");
    int code = Integer.parseInt(errorCode[0]);
    if (code >= 1) {
        if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
            throw new NotAllowedException("Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName());
        } else {
            throw new NotAllowedException("Validation error");
        }
    }
    entityInstanceService.updateTrackedEntityInstance(entityInstance, null, null, valuesForSave, valuesForUpdate, valuesForDelete);
    enrollProgram(patient.getId() + "-" + programIdText, null, new Date());
    entityInstance = entityInstanceService.getTrackedEntityInstance(patient.getId());
    entityInstance.setTrackedEntity(trackedEntity);
    return getPatientModel(entityInstance);
}
Also used : Program(org.hisp.dhis.program.Program) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date)

Example 7 with NotAllowedException

use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method addRelationship.

@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient addRelationship(org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship enrollmentRelationship, int orgUnitId) throws NotAllowedException {
    TrackedEntityInstance patientB;
    if (enrollmentRelationship.getPersonBId() != 0) {
        patientB = entityInstanceService.getTrackedEntityInstance(enrollmentRelationship.getPersonBId());
    } else {
        String instanceInfo = findPatientInAdvanced(enrollmentRelationship.getPersonBName(), orgUnitId, 0);
        if (instanceInfo == null || instanceInfo.trim().length() == 0) {
            throw NotAllowedException.NO_BENEFICIARY_FOUND;
        } else {
            throw new NotAllowedException(instanceInfo);
        }
    }
    TrackedEntityInstance patientA = entityInstanceService.getTrackedEntityInstance(enrollmentRelationship.getPersonAId());
    RelationshipType relationshipType = relationshipTypeService.getRelationshipType(enrollmentRelationship.getId());
    Relationship relationship = new Relationship();
    relationship.setRelationshipType(relationshipType);
    if (enrollmentRelationship.getChosenRelationship().equals(relationshipType.getaIsToB())) {
        relationship.setEntityInstanceA(patientA);
        relationship.setEntityInstanceB(patientB);
    } else {
        relationship.setEntityInstanceA(patientB);
        relationship.setEntityInstanceB(patientA);
    }
    relationshipService.addRelationship(relationship);
    // return getPatientModel( orgUnitId, patientA );
    return getPatientModel(patientA);
}
Also used : NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) Relationship(org.hisp.dhis.relationship.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 8 with NotAllowedException

use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method findLostToFollowUp.

@Override
public String findLostToFollowUp(int orgUnitId, String searchEventInfos) throws NotAllowedException {
    String[] searchEventInfosArray = searchEventInfos.split("-");
    EventStatus eventStatus = EventStatus.ACTIVE;
    if (searchEventInfosArray[1].equalsIgnoreCase("Scheduled in future")) {
        eventStatus = EventStatus.SCHEDULE;
    } else if (searchEventInfosArray[1].equalsIgnoreCase("Overdue")) {
        eventStatus = EventStatus.OVERDUE;
    }
    String eventsInfo = "";
    Calendar toCalendar = new GregorianCalendar();
    toCalendar.add(Calendar.DATE, -1);
    toCalendar.add(Calendar.YEAR, 100);
    Date toDate = toCalendar.getTime();
    Calendar fromCalendar = new GregorianCalendar();
    fromCalendar.add(Calendar.DATE, -1);
    fromCalendar.add(Calendar.YEAR, -100);
    Date fromDate = fromCalendar.getTime();
    TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams();
    List<TrackedEntityAttribute> trackedEntityAttributeList = new ArrayList<>(attributeService.getTrackedEntityAttributesByDisplayOnVisitSchedule(true));
    for (TrackedEntityAttribute trackedEntityAttribute : trackedEntityAttributeList) {
        QueryItem queryItem = new QueryItem(trackedEntityAttribute);
        param.addAttribute(queryItem);
    }
    param.addOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
    param.setEventStatus(eventStatus);
    param.setEventStartDate(fromDate);
    param.setEventEndDate(toDate);
    Grid programStageInstanceGrid = entityInstanceService.getTrackedEntityInstancesGrid(param);
    List<List<Object>> rows = programStageInstanceGrid.getRows();
    if (rows.size() == 0) {
        throw NotAllowedException.NO_EVENT_FOUND;
    } else if (rows.size() > 0) {
        for (List<Object> row : rows) {
            for (int i = 5; i < row.size(); i++) {
                eventsInfo += row.get(i) + "/";
                if (i == row.size() - 1) {
                    eventsInfo += "$";
                }
            }
        }
        throw new NotAllowedException(eventsInfo);
    } else {
        return "";
    }
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) Grid(org.hisp.dhis.common.Grid) EventStatus(org.hisp.dhis.event.EventStatus) GregorianCalendar(java.util.GregorianCalendar) ArrayList(java.util.ArrayList) Date(java.util.Date) List(java.util.List) PatientList(org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList) ArrayList(java.util.ArrayList) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)

Aggregations

NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)8 Date (java.util.Date)5 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)5 ArrayList (java.util.ArrayList)4 Program (org.hisp.dhis.program.Program)4 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)4 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)4 HashSet (java.util.HashSet)3 PatientAttribute (org.hisp.dhis.api.mobile.model.PatientAttribute)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)2 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 List (java.util.List)1 Notification (org.hisp.dhis.api.mobile.model.LWUITmodel.Notification)1 PatientList (org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList)1 Grid (org.hisp.dhis.common.Grid)1 QueryItem (org.hisp.dhis.common.QueryItem)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 EventStatus (org.hisp.dhis.event.EventStatus)1