use of org.hisp.dhis.sms.command.code.SMSCode in project dhis2-core by dhis2.
the class SMSCommandAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
if (selectedCommandID > -1) {
smsCommand = smsCommandService.getSMSCommand(selectedCommandID);
}
if (smsCommand != null && smsCommand.getCodes() != null) {
for (SMSCode smsCode : smsCommand.getCodes()) {
if (smsCommand.getParserType() == ParserType.TRACKED_ENTITY_REGISTRATION_PARSER && smsCode.getTrackedEntityAttribute() != null) {
codes.put("" + smsCode.getTrackedEntityAttribute().getId(), smsCode.getCode());
} else if (smsCode.getDataElement() != null) {
codes.put("" + smsCode.getDataElement().getId() + smsCode.getOptionId(), smsCode.getCode());
}
if (smsCode.getFormula() != null) {
formulas.put("" + smsCode.getDataElement().getId() + smsCode.getOptionId(), smsCode.getFormula());
}
if (smsCommand.getParserType().equals(ParserType.EVENT_REGISTRATION_PARSER)) {
codes.put("" + smsCode.getDataElement().getId(), smsCode.getCode());
}
}
}
userGroupList = new ArrayList<>(userGroupService.getAllUserGroups());
programList = new ArrayList<>(programService.getPrograms(ProgramType.WITH_REGISTRATION));
programWithoutRegistration = new ArrayList<>(programService.getPrograms(ProgramType.WITHOUT_REGISTRATION));
return SUCCESS;
}
use of org.hisp.dhis.sms.command.code.SMSCode in project dhis2-core by dhis2.
the class DataValueSMSListener method markCompleteDataSet.
private void markCompleteDataSet(String sender, OrganisationUnit orgunit, Map<String, String> parsedMessage, SMSCommand command, Date date) {
Period period = null;
int numberOfEmptyValue = 0;
for (SMSCode code : command.getCodes()) {
DataElementCategoryOptionCombo optionCombo = dataElementCategoryService.getDataElementCategoryOptionCombo(code.getOptionId());
period = getPeriod(command, date);
DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgunit, optionCombo);
if (dv == null && !StringUtils.isEmpty(code.getCode())) {
numberOfEmptyValue++;
}
}
// Check completeness method
if (command.getCompletenessMethod() == SMSCommand.RECEIVE_ALL_DATAVALUE) {
if (numberOfEmptyValue > 0) {
return;
}
} else if (command.getCompletenessMethod() == SMSCommand.RECEIVE_AT_LEAST_ONE_DATAVALUE) {
if (numberOfEmptyValue == command.getCodes().size()) {
return;
}
} else if (command.getCompletenessMethod() == SMSCommand.DO_NOT_MARK_COMPLETE) {
return;
}
// Go through the complete process
String storedBy = SmsUtils.getUser(sender, command, userService.getUsersByPhoneNumber(sender)).getUsername();
if (StringUtils.isBlank(storedBy)) {
storedBy = "[unknown] from [" + sender + "]";
}
// If new values are submitted re-register as complete
deregisterCompleteDataSet(command.getDataset(), period, orgunit);
registerCompleteDataSet(command.getDataset(), period, orgunit, storedBy);
}
use of org.hisp.dhis.sms.command.code.SMSCode in project dhis2-core by dhis2.
the class SingleEventListener method registerEvent.
// -------------------------------------------------------------------------
// Supportive Methods
// -------------------------------------------------------------------------
private void registerEvent(Map<String, String> commandValuePairs, SMSCommand smsCommand, IncomingSms sms) {
OrganisationUnit orgUnit = getOrganisationUnits(sms).iterator().next();
List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(smsCommand.getProgram(), ProgramStatus.ACTIVE));
if (programInstances.isEmpty()) {
ProgramInstance pi = new ProgramInstance();
pi.setEnrollmentDate(new Date());
pi.setIncidentDate(new Date());
pi.setProgram(smsCommand.getProgram());
pi.setStatus(ProgramStatus.ACTIVE);
programInstanceService.addProgramInstance(pi);
programInstances.add(pi);
} else if (programInstances.size() > 1) {
update(sms, SmsMessageStatus.FAILED, false);
sendFeedback("Multiple active program instances exists for program: " + smsCommand.getProgram().getUid(), sms.getOriginator(), ERROR);
return;
}
ProgramInstance programInstance = null;
programInstance = programInstances.get(0);
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setOrganisationUnit(orgUnit);
programStageInstance.setProgramStage(smsCommand.getProgramStage());
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setExecutionDate(sms.getSentDate());
programStageInstance.setDueDate(sms.getSentDate());
programStageInstance.setAttributeOptionCombo(dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
programStageInstance.setCompletedBy("DHIS 2");
programStageInstanceService.addProgramStageInstance(programStageInstance);
for (SMSCode smsCode : smsCommand.getCodes()) {
TrackedEntityDataValue dataValue = new TrackedEntityDataValue();
dataValue.setAutoFields();
dataValue.setDataElement(smsCode.getDataElement());
dataValue.setProgramStageInstance(programStageInstance);
dataValue.setValue(commandValuePairs.get(smsCode.getCode()));
trackedEntityDataValueService.saveTrackedEntityDataValue(dataValue);
}
update(sms, SmsMessageStatus.PROCESSED, true);
sendFeedback(StringUtils.defaultIfEmpty(smsCommand.getSuccessMessage(), EVENT_REGISTERED), sms.getOriginator(), INFO);
}
use of org.hisp.dhis.sms.command.code.SMSCode in project dhis2-core by dhis2.
the class TrackedEntityRegistrationSMSListener method receive.
@Transactional
@Override
public void receive(IncomingSms sms) {
String message = sms.getText();
SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.TRACKED_ENTITY_REGISTRATION_PARSER);
Map<String, String> parsedMessage = this.parse(message, smsCommand);
Date date = SmsUtils.lookForDate(message);
String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
Collection<OrganisationUnit> orgUnits = SmsUtils.getOrganisationUnitsByPhoneNumber(senderPhoneNumber, userService.getUsersByPhoneNumber(senderPhoneNumber));
if (orgUnits == null || orgUnits.size() == 0) {
if (StringUtils.isEmpty(smsCommand.getNoUserMessage())) {
throw new SMSParserException(SMSCommand.NO_USER_MESSAGE);
} else {
throw new SMSParserException(smsCommand.getNoUserMessage());
}
}
OrganisationUnit orgUnit = SmsUtils.selectOrganisationUnit(orgUnits, parsedMessage, smsCommand);
TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
trackedEntityInstance.setOrganisationUnit(orgUnit);
trackedEntityInstance.setTrackedEntity(trackedEntityService.getTrackedEntityByName("Person"));
Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
for (SMSCode code : smsCommand.getCodes()) {
if (parsedMessage.containsKey(code.getCode().toUpperCase())) {
TrackedEntityAttributeValue trackedEntityAttributeValue = this.createTrackedEntityAttributeValue(parsedMessage, code, smsCommand, trackedEntityInstance);
patientAttributeValues.add(trackedEntityAttributeValue);
}
}
int trackedEntityInstanceId = 0;
if (patientAttributeValues.size() > 0) {
trackedEntityInstanceId = trackedEntityInstanceService.createTrackedEntityInstance(trackedEntityInstance, null, null, patientAttributeValues);
}
programInstanceService.enrollTrackedEntityInstance(trackedEntityInstanceService.getTrackedEntityInstance(trackedEntityInstanceId), smsCommand.getProgram(), new Date(), date, orgUnit);
smsSender.sendMessage(null, "Entity Registered Successfully ", senderPhoneNumber);
sms.setStatus(SmsMessageStatus.PROCESSED);
sms.setParsed(true);
incomingSmsService.update(sms);
}
use of org.hisp.dhis.sms.command.code.SMSCode in project dhis2-core by dhis2.
the class DataValueSMSListener method sendSuccessFeedback.
protected void sendSuccessFeedback(String sender, SMSCommand command, Map<String, String> parsedMessage, Date date, OrganisationUnit orgunit) {
String reportBack = "Thank you! Values entered: ";
String notInReport = "Missing values for: ";
Period period = null;
Map<String, DataValue> codesWithDataValues = new TreeMap<>();
List<String> codesWithoutDataValues = new ArrayList<>();
for (SMSCode code : command.getCodes()) {
DataElementCategoryOptionCombo optionCombo = dataElementCategoryService.getDataElementCategoryOptionCombo(code.getOptionId());
period = getPeriod(command, date);
DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgunit, optionCombo);
if (dv == null && !StringUtils.isEmpty(code.getCode())) {
codesWithoutDataValues.add(code.getCode());
} else if (dv != null) {
codesWithDataValues.put(code.getCode(), dv);
}
}
for (String key : codesWithDataValues.keySet()) {
DataValue dv = codesWithDataValues.get(key);
String value = dv.getValue();
if (ValueType.BOOLEAN == dv.getDataElement().getValueType()) {
if ("true".equals(value)) {
value = "Yes";
} else if ("false".equals(value)) {
value = "No";
}
}
reportBack += key + "=" + value + " ";
}
Collections.sort(codesWithoutDataValues);
for (String key : codesWithoutDataValues) {
notInReport += key + ",";
}
notInReport = notInReport.substring(0, notInReport.length() - 1);
if (smsSender.isConfigured()) {
if (command.getSuccessMessage() != null && !StringUtils.isEmpty(command.getSuccessMessage())) {
smsSender.sendMessage(null, command.getSuccessMessage(), sender);
} else {
smsSender.sendMessage(null, reportBack, sender);
}
} else {
Log.info("No sms configuration found.");
}
}
Aggregations