use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method saveProgramStage.
@Override
public String saveProgramStage(org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStage mobileProgramStage, int patientId, int orgUnitId) throws NotAllowedException {
if (mobileProgramStage.isSingleEvent()) {
TrackedEntityInstance patient = entityInstanceService.getTrackedEntityInstance(patientId);
ProgramStageInstance prStageInstance = programStageInstanceService.getProgramStageInstance(mobileProgramStage.getId());
ProgramStage programStage = programStageService.getProgramStage(prStageInstance.getProgramStage().getId());
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
// ---------------------------------------------------------------------
// Add a new program-instance
//
// ---------------------------------------------------------------------
ProgramInstance programInstance = new ProgramInstance();
programInstance.setEnrollmentDate(new Date());
programInstance.setIncidentDate(new Date());
programInstance.setProgram(programStage.getProgram());
programInstance.setStatus(ProgramStatus.COMPLETED);
programInstance.setEntityInstance(patient);
programInstanceService.addProgramInstance(programInstance);
// ---------------------------------------------------------------------
// Add a new program-stage-instance
//
// ---------------------------------------------------------------------
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setProgramStage(programStage);
programStageInstance.setDueDate(new Date());
programStageInstance.setExecutionDate(new Date());
programStageInstance.setOrganisationUnit(organisationUnit);
programStageInstance.setStatus(EventStatus.COMPLETED);
programStageInstanceService.addProgramStageInstance(programStageInstance);
// ---------------------------------------------------------------------
// Save value
//
// ---------------------------------------------------------------------
List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement> dataElements = mobileProgramStage.getDataElements();
for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement dataElement1 : dataElements) {
DataElement dataElement = dataElementService.getDataElement(dataElement1.getId());
String value = dataElement1.getValue();
if (ValueType.DATE == dataElement.getValueType() && !value.trim().equals("")) {
value = PeriodUtil.convertDateFormat(value);
}
TrackedEntityDataValue patientDataValue = new TrackedEntityDataValue();
patientDataValue.setDataElement(dataElement);
patientDataValue.setValue(value);
patientDataValue.setProgramStageInstance(programStageInstance);
patientDataValue.setLastUpdated(new Date());
dataValueService.saveTrackedEntityDataValue(patientDataValue);
}
return SINGLE_EVENT_UPLOADED;
} else {
ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(mobileProgramStage.getId());
List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement> dataElements = mobileProgramStage.getDataElements();
try {
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
programStageInstance.setOrganisationUnit(organisationUnit);
} catch (Exception e) {
programStageInstance.setOrganisationUnit(null);
}
for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement dataElement1 : dataElements) {
DataElement dataElement = dataElementService.getDataElement(dataElement1.getId());
String value = dataElement1.getValue();
if (value != null) {
if (ValueType.DATE == dataElement.getValueType() && !value.trim().equals("")) {
value = PeriodUtil.convertDateFormat(value);
}
TrackedEntityDataValue previousPatientDataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
if (previousPatientDataValue == null) {
TrackedEntityDataValue patientDataValue = new TrackedEntityDataValue(programStageInstance, dataElement, value);
dataValueService.saveTrackedEntityDataValue(patientDataValue);
} else {
previousPatientDataValue.setValue(value);
previousPatientDataValue.setLastUpdated(new Date());
previousPatientDataValue.setProvidedElsewhere(false);
dataValueService.updateTrackedEntityDataValue(previousPatientDataValue);
}
}
}
if (DateUtils.getMediumDate(mobileProgramStage.getReportDate()) != null) {
programStageInstance.setExecutionDate(DateUtils.getMediumDate(mobileProgramStage.getReportDate()));
} else {
programStageInstance.setExecutionDate(new Date());
}
if (programStageInstance.getProgramStage().getProgramStageDataElements().size() > dataElements.size()) {
programStageInstanceService.updateProgramStageInstance(programStageInstance);
return PROGRAM_STAGE_SECTION_UPLOADED;
} else {
if (mobileProgramStage.isCompleted()) {
programStageInstance.setStatus(EventStatus.COMPLETED);
}
programStageInstanceService.updateProgramStageInstance(programStageInstance);
// check if all belonged program stage are completed
if (!mobileProgramStage.isRepeatable() && isAllProgramStageFinished(programStageInstance) == true) {
ProgramInstance programInstance = programStageInstance.getProgramInstance();
programInstance.setStatus(ProgramStatus.COMPLETED);
programInstanceService.updateProgramInstance(programInstance);
}
if (mobileProgramStage.isRepeatable()) {
Date nextDate = DateUtils.getDateAfterAddition(new Date(), mobileProgramStage.getStandardInterval());
return PROGRAM_STAGE_UPLOADED + "$" + DateUtils.getMediumDateString(nextDate);
} else {
return PROGRAM_STAGE_UPLOADED;
}
}
}
}
use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method handleLostToFollowUp.
@SuppressWarnings("finally")
@Override
public Notification handleLostToFollowUp(LostEvent lostEvent) throws NotAllowedException {
Notification notification = new Notification();
try {
ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(lostEvent.getId());
programStageInstance.setDueDate(DateUtils.getMediumDate(lostEvent.getDueDate()));
programStageInstance.setStatus(EventStatus.fromInt(lostEvent.getStatus()));
if (lostEvent.getComment() != null) {
List<MessageConversation> conversationList = new ArrayList<>();
MessageConversation conversation = new MessageConversation(lostEvent.getName(), currentUserService.getCurrentUser(), MessageType.PRIVATE);
conversation.addMessage(new Message(lostEvent.getComment(), null, currentUserService.getCurrentUser()));
conversation.setRead(true);
conversationList.add(conversation);
programStageInstance.setMessageConversations(conversationList);
messageService.saveMessageConversation(conversation);
}
programStageInstanceService.updateProgramStageInstance(programStageInstance);
// send SMS
if (programStageInstance.getProgramInstance().getEntityInstance().getTrackedEntityAttributeValues() != null && lostEvent.getSMS() != null) {
List<User> recipientsList = new ArrayList<>();
for (TrackedEntityAttributeValue attrValue : programStageInstance.getProgramInstance().getEntityInstance().getTrackedEntityAttributeValues()) {
if (ValueType.PHONE_NUMBER == attrValue.getAttribute().getValueType()) {
User user = new User();
user.setPhoneNumber(attrValue.getValue());
recipientsList.add(user);
}
}
Set<User> recipients = new HashSet<>();
recipients.addAll(recipientsList);
smsSender.sendMessage(lostEvent.getName(), lostEvent.getSMS(), null, currentUserService.getCurrentUser(), recipients, false);
}
notification.setMessage("Success");
} catch (Exception e) {
e.printStackTrace();
notification.setMessage("Fail");
} finally {
return notification;
}
}
use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method savePatient.
@Override
public Patient savePatient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId, String programIdText) throws NotAllowedException {
TrackedEntityInstance patientWeb = new TrackedEntityInstance();
patientWeb.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
Set<TrackedEntityAttribute> patientAttributeSet = new HashSet<>();
Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
if (attributesMobile != null) {
for (org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : attributesMobile) {
TrackedEntityAttribute patientAttribute = attributeService.getTrackedEntityAttributeByName(paAtt.getName());
patientAttributeSet.add(patientAttribute);
TrackedEntityAttributeValue patientAttributeValue = new TrackedEntityAttributeValue();
patientAttributeValue.setEntityInstance(patientWeb);
patientAttributeValue.setAttribute(patientAttribute);
patientAttributeValue.setValue(paAtt.getValue());
patientAttributeValues.add(patientAttributeValue);
}
}
patientWeb.setTrackedEntity(trackedEntityService.getTrackedEntityByName(patient.getTrackedEntityName()));
patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, null, null, patientAttributeValues);
TrackedEntityInstance newTrackedEntityInstance = entityInstanceService.getTrackedEntityInstance(this.patientId);
String errorMsg = null;
try {
for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance mobileProgramInstance : patient.getEnrollmentPrograms()) {
Date incidentDate = DateUtils.getMediumDate(mobileProgramInstance.getDateOfIncident());
enrollProgram(patientId + "-" + mobileProgramInstance.getProgramId(), mobileProgramInstance.getProgramStageInstances(), incidentDate);
}
Program program = programService.getProgram(Integer.parseInt(programIdText));
String[] errorCode = entityInstanceService.validateTrackedEntityInstance(newTrackedEntityInstance, program).split("_");
int code = Integer.parseInt(errorCode[0]);
if (code >= 1) {
entityInstanceService.deleteTrackedEntityInstance(newTrackedEntityInstance);
if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
errorMsg = "Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName();
} else {
errorMsg = "Validation error";
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (errorMsg != null) {
throw new NotAllowedException(errorMsg);
}
return getPatientModel(newTrackedEntityInstance);
}
use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method registerRelative.
@Override
public Patient registerRelative(Patient patient, int orgUnitId, String programId) throws NotAllowedException {
TrackedEntityInstance patientWeb = new TrackedEntityInstance();
patientWeb.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
Set<TrackedEntityAttribute> patientAttributeSet = new HashSet<>();
Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
if (attributesMobile != null) {
for (org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : attributesMobile) {
TrackedEntityAttribute patientAttribute = attributeService.getTrackedEntityAttributeByName(paAtt.getName());
patientAttributeSet.add(patientAttribute);
TrackedEntityAttributeValue patientAttributeValue = new TrackedEntityAttributeValue();
patientAttributeValue.setEntityInstance(patientWeb);
patientAttributeValue.setAttribute(patientAttribute);
patientAttributeValue.setValue(paAtt.getValue());
patientAttributeValues.add(patientAttributeValue);
}
}
patientWeb.setTrackedEntity(trackedEntityService.getTrackedEntityByName(patient.getTrackedEntityName()));
if (patient.getIdToAddRelative() != 0) {
TrackedEntityInstance relative = entityInstanceService.getTrackedEntityInstance(patient.getIdToAddRelative());
if (relative == null) {
throw new NotAllowedException("relative does not exist");
}
patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, relative.getUid(), patient.getRelTypeIdToAdd(), patientAttributeValues);
} else {
patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, null, null, patientAttributeValues);
}
TrackedEntityInstance newTrackedEntityInstance = entityInstanceService.getTrackedEntityInstance(this.patientId);
String errorMsg = null;
try {
for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance mobileProgramInstance : patient.getEnrollmentPrograms()) {
Date incidentDate = DateUtils.getMediumDate(mobileProgramInstance.getDateOfIncident());
enrollProgram(patientId + "-" + mobileProgramInstance.getProgramId(), mobileProgramInstance.getProgramStageInstances(), incidentDate);
}
Program program = programService.getProgram(Integer.parseInt(programId));
String[] errorCode = entityInstanceService.validateTrackedEntityInstance(newTrackedEntityInstance, program).split("_");
int code = Integer.parseInt(errorCode[0]);
if (code >= 1) {
entityInstanceService.deleteTrackedEntityInstance(newTrackedEntityInstance);
if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
errorMsg = "Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName();
} else {
errorMsg = "Validation error";
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (errorMsg != null) {
throw new NotAllowedException(errorMsg);
}
if (patient.getEnrollmentRelationship() != null) {
org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship enrollmentRelationship = patient.getEnrollmentRelationship();
enrollmentRelationship.setPersonBId(newTrackedEntityInstance.getId());
addRelationship(enrollmentRelationship, orgUnitId);
}
return getPatientModel(newTrackedEntityInstance);
}
use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method getAllProgramByOrgUnit.
@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Program getAllProgramByOrgUnit(int orgUnitId, String type) throws NotAllowedException {
String programsInfo = "";
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
Set<Program> tempPrograms = null;
ProgramType programType = ProgramType.fromValue(type);
if (programType == ProgramType.WITHOUT_REGISTRATION) {
tempPrograms = programService.getUserPrograms(ProgramType.WITHOUT_REGISTRATION);
} else // ProgramType.WITH_REGISTRATION
{
tempPrograms = programService.getUserPrograms(ProgramType.WITH_REGISTRATION);
}
List<Program> programs = new ArrayList<>();
for (Program program : tempPrograms) {
if (program.getOrganisationUnits().contains(organisationUnit)) {
programs.add(program);
}
}
if (programs.size() != 0) {
if (programs.size() == 1) {
Program program = programs.get(0);
return getMobileProgramWithoutData(program);
} else {
for (Program program : programs) {
if (program.getOrganisationUnits().contains(organisationUnit)) {
programsInfo += program.getId() + "/" + program.getName() + "$";
}
}
throw new NotAllowedException(programsInfo);
}
} else {
throw NotAllowedException.NO_PROGRAM_FOUND;
}
}
Aggregations