use of org.hisp.dhis.sms.command.SMSSpecialCharacter in project dhis2-core by dhis2.
the class DataValueSMSListener method storeDataValue.
private boolean storeDataValue(String sender, OrganisationUnit orgunit, Map<String, String> parsedMessage, SMSCode code, SMSCommand command, Date date, DataSet dataSet) {
String storedBy = SmsUtils.getUser(sender, command, userService.getUsersByPhoneNumber(sender)).getUsername();
if (StringUtils.isBlank(storedBy)) {
storedBy = "[unknown] from [" + sender + "]";
}
DataElementCategoryOptionCombo optionCombo = dataElementCategoryService.getDataElementCategoryOptionCombo(code.getOptionId());
Period period = getPeriod(command, date);
DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgunit, optionCombo);
String value = parsedMessage.get(code.getCode());
Set<SMSSpecialCharacter> specialCharacters = command.getSpecialCharacters();
for (SMSSpecialCharacter each : specialCharacters) {
if (each.getName().equalsIgnoreCase(value)) {
value = each.getValue();
break;
}
}
if (!StringUtils.isEmpty(value)) {
boolean newDataValue = false;
if (dv == null) {
dv = new DataValue();
dv.setCategoryOptionCombo(optionCombo);
dv.setSource(orgunit);
dv.setDataElement(code.getDataElement());
dv.setPeriod(period);
dv.setComment("");
newDataValue = true;
}
if (ValueType.BOOLEAN == dv.getDataElement().getValueType()) {
if ("Y".equals(value.toUpperCase()) || "YES".equals(value.toUpperCase())) {
value = "true";
} else if ("N".equals(value.toUpperCase()) || "NO".equals(value.toUpperCase())) {
value = "false";
}
} else if (dv.getDataElement().getValueType().isInteger()) {
try {
Integer.parseInt(value);
} catch (NumberFormatException e) {
return false;
}
}
dv.setValue(value);
dv.setLastUpdated(new java.util.Date());
dv.setStoredBy(storedBy);
if (newDataValue) {
dataValueService.addDataValue(dv);
} else {
dataValueService.updateDataValue(dv);
}
}
if (code.getFormula() != null) {
try {
String formula = code.getFormula();
String targetDataElementId = formula.substring(1, formula.length());
String operation = String.valueOf(formula.charAt(0));
DataElement targetDataElement = dataElementService.getDataElement(Integer.parseInt(targetDataElementId));
if (targetDataElement == null) {
return false;
}
DataValue targetDataValue = dataValueService.getDataValue(targetDataElement, period, orgunit, dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
int targetValue = 0;
boolean newTargetDataValue = false;
if (targetDataValue == null) {
targetDataValue = new DataValue();
targetDataValue.setCategoryOptionCombo(dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
targetDataValue.setSource(orgunit);
targetDataValue.setDataElement(targetDataElement);
targetDataValue.setPeriod(period);
targetDataValue.setComment("");
newTargetDataValue = true;
} else {
targetValue = Integer.parseInt(targetDataValue.getValue());
}
if (operation.equals("+")) {
targetValue = targetValue + Integer.parseInt(value);
} else if (operation.equals("-")) {
targetValue = targetValue - Integer.parseInt(value);
}
targetDataValue.setValue(String.valueOf(targetValue));
targetDataValue.setLastUpdated(new java.util.Date());
targetDataValue.setStoredBy(storedBy);
if (newTargetDataValue) {
dataValueService.addDataValue(targetDataValue);
} else {
dataValueService.updateDataValue(targetDataValue);
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
use of org.hisp.dhis.sms.command.SMSSpecialCharacter in project dhis2-core by dhis2.
the class HibernateSMSCommandStore method delete.
@Override
@Transactional
public void delete(SMSCommand cmd) {
Session session = sessionFactory.getCurrentSession();
for (SMSCode x : cmd.getCodes()) {
session.delete(x);
}
for (SMSSpecialCharacter x : cmd.getSpecialCharacters()) {
session.delete(x);
}
session.delete(cmd);
}
use of org.hisp.dhis.sms.command.SMSSpecialCharacter in project dhis2-core by dhis2.
the class EditSMSCommandForm method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
Set<SMSCode> codeSet = new HashSet<>();
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readValue(codeDataelementOption, JsonNode.class);
JsonNode codes = root.get("codes");
if (codes != null && codes.size() > 0) {
codes.iterator().forEachRemaining(code -> {
if (code.get("code") != null && code.get("dataElementId") != null && code.get("optionId") != null && code.get("formula") != null) {
SMSCode c = new SMSCode();
c.setCode(code.get("code").asText());
c.setDataElement(dataElementService.getDataElement(code.get("dataElementId").asInt()));
c.setOptionId(code.get("optionId").asInt());
if (!StringUtils.isEmpty(code.get("formula").asText())) {
c.setFormula(code.get("formula").asText());
} else {
c.setFormula(null);
}
codeSet.add(c);
}
});
}
Set<SMSSpecialCharacter> specialCharacterSet = new HashSet<>();
root = mapper.readValue(specialCharactersInfo, JsonNode.class);
JsonNode specialChars = root.get("specialCharacters");
if (specialChars != null && StringUtils.isNoneEmpty(specialChars.toString())) {
specialCharacterSet = mapper.readValue(specialChars.toString(), new TypeReference<HashSet<SMSSpecialCharacter>>() {
});
smsCommandService.saveSpecialCharacterSet(specialCharacterSet);
}
SMSCommand command = getSMSCommand();
if (selectedDataSetID > -1 && command != null) {
if (command.getParserType() == ParserType.TRACKED_ENTITY_REGISTRATION_PARSER) {
root = mapper.readValue(trackedEntityAttributeCodes, JsonNode.class);
JsonNode regCodes = root.get("trackedEntityAttributeCodes");
if (regCodes != null && regCodes.size() > 0) {
regCodes.iterator().forEachRemaining(regCode -> {
if (regCode.get("code") != null && regCode.get("trackedEntityAttributeId") != null) {
SMSCode c = new SMSCode();
c.setCode(regCode.get("code").asText());
c.setTrackedEntityAttribute(trackedEntityAttributeService.getTrackedEntityAttribute(regCode.get("trackedEntityAttributeId").asInt()));
codeSet.add(c);
}
});
}
}
if (command.getParserType() == ParserType.EVENT_REGISTRATION_PARSER) {
root = mapper.readValue(programStageDataElementCodes, JsonNode.class);
JsonNode regCodes = root.get("programStageDataElementCodes");
if (regCodes != null && regCodes.size() > 0) {
regCodes.iterator().forEachRemaining(regCode -> {
if (regCode.get("code") != null && regCode.get("programStageDataElementId") != null) {
SMSCode c = new SMSCode();
c.setCode(regCode.get("code").asText());
c.setDataElement(dataElementService.getDataElement(regCode.get("programStageDataElementId").asInt()));
c.setCompulsory(regCode.get("compulsory").asBoolean());
codeSet.add(c);
}
});
}
}
}
if (codeSet.size() > 0) {
smsCommandService.save(codeSet);
}
if (selectedDataSetID > -1 && command != null) {
command.setCurrentPeriodUsedForReporting(currentPeriodUsedForReporting);
command.setName(name);
command.setSeparator(separator);
if (completenessMethod != null) {
command.setCompletenessMethod(completenessMethod);
}
// remove codes
Set<SMSCode> toRemoveCodes = command.getCodes();
smsCommandService.deleteCodeSet(toRemoveCodes);
// remove special characters
Set<SMSSpecialCharacter> toRemoveCharacters = command.getSpecialCharacters();
smsCommandService.deleteSpecialCharacterSet(toRemoveCharacters);
command.setCodes(codeSet);
// message
command.setDefaultMessage(defaultMessage);
command.setReceivedMessage(receivedMessage);
command.setMoreThanOneOrgUnitMessage(moreThanOneOrgUnitMessage);
command.setNoUserMessage(noUserMessage);
command.setWrongFormatMessage(wrongFormatMessage);
command.setSuccessMessage(successMessage);
if (userGroupID != null && userGroupID > -1) {
command.setUserGroup(userGroupService.getUserGroup(userGroupID));
}
command.setSpecialCharacters(specialCharacterSet);
smsCommandService.save(command);
}
return SUCCESS;
}
use of org.hisp.dhis.sms.command.SMSSpecialCharacter in project dhis2-core by dhis2.
the class DataValueListenerTest method setUpInstances.
private void setUpInstances() {
organisationUnitA = createOrganisationUnit('O');
organisationUnitB = createOrganisationUnit('P');
dataSet = createDataSet('D');
dataSetB = createDataSet('B');
dataSet.addOrganisationUnit(organisationUnitA);
dataSet.addOrganisationUnit(organisationUnitB);
period = createPeriod(new Date(), new Date());
user = createUser('U');
user.setPhoneNumber(ORIGINATOR);
user.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
userB = createUser('B');
userB.setPhoneNumber(ORIGINATOR);
userB.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
userC = createUser('C');
userC.setPhoneNumber(ORIGINATOR);
userC.setOrganisationUnits(Sets.newHashSet(organisationUnitA, organisationUnitB));
userWithNoOu = createUser('V');
userWithNoOu.setPhoneNumber(null);
userWithNoOu.setOrganisationUnits(null);
userwithMultipleOu = createUser('W');
userwithMultipleOu.setPhoneNumber(ORIGINATOR);
userwithMultipleOu.setOrganisationUnits(Sets.newHashSet(organisationUnitA, organisationUnitB));
dataElement = createDataElement('D');
dataElement.setValueType(ValueType.TEXT);
defaultCategoryOptionCombo = createCategoryOptionCombo('D');
categoryOptionCombo = createCategoryOptionCombo('C');
dataElementB = createDataElement('B');
dataElementB.setValueType(ValueType.TEXT);
fetchedDataValue = createDataValue(dataElement, period, organisationUnitA, FETCHED_DATA_VALUE, categoryOptionCombo);
fetchedCompleteDataSetRegistration = new CompleteDataSetRegistration(dataSet, period, organisationUnitA, categoryOptionCombo, new Date(), STORED_BY, new Date(), LAST_UPDATED_BY, true);
smsCode = new SMSCode();
smsCode.setCode("de");
smsCode.setDataElement(dataElement);
smsCode.setOptionId(defaultCategoryOptionCombo);
smsCodeForcompulsory = new SMSCode();
smsCodeForcompulsory.setCode("deb");
smsCodeForcompulsory.setDataElement(dataElementB);
smsCodeForcompulsory.setOptionId(categoryOptionCombo);
smsCodeForcompulsory.setCompulsory(true);
smsSpecialCharacter = new SMSSpecialCharacter();
smsSpecialCharacter.setName("special");
smsSpecialCharacter.setValue("spc1");
keyValueCommand = new SMSCommand();
keyValueCommand.setName(DATA_ENTRY_COMMAND);
keyValueCommand.setParserType(ParserType.KEY_VALUE_PARSER);
keyValueCommand.setDataset(dataSet);
keyValueCommand.setCodes(Sets.newHashSet(smsCode));
keyValueCommand.setSuccessMessage(SUCCESS_MESSAGE);
response = new OutboundMessageResponse();
response.setResponseObject(GatewayResponse.SUCCESS);
response.setOk(true);
incomingSms = new IncomingSms();
incomingSms.setText(SMS_TEXT);
incomingSms.setOriginator(ORIGINATOR);
incomingSms.setCreatedBy(user);
incomingSmsForCompulsoryCode = new IncomingSms();
incomingSmsForCompulsoryCode.setText(SMS_TEXT_FOR_COMPULSORY);
incomingSmsForCompulsoryCode.setOriginator(ORIGINATOR);
incomingSmsForCompulsoryCode.setCreatedBy(user);
incomingSmsForCustomSeparator = new IncomingSms();
incomingSmsForCustomSeparator.setText(SMS_TEXT_FOR_CUSTOM_SEPARATOR);
incomingSmsForCustomSeparator.setOriginator(ORIGINATOR);
incomingSmsForCustomSeparator.setCreatedBy(user);
}
use of org.hisp.dhis.sms.command.SMSSpecialCharacter in project dhis2-core by dhis2.
the class DataValueSMSListener method storeDataValue.
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private boolean storeDataValue(IncomingSms sms, OrganisationUnit orgunit, Map<String, String> parsedMessage, SMSCode code, SMSCommand command, Date date) {
String sender = sms.getOriginator();
String storedBy = SmsUtils.getUser(sender, command, Collections.singletonList(getUser(sms))).getUsername();
if (StringUtils.isBlank(storedBy)) {
storedBy = "[unknown] from [" + sender + "]";
}
CategoryOptionCombo optionCombo = dataElementCategoryService.getCategoryOptionCombo(code.getOptionId().getId());
Period period = getPeriod(command, date);
DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgunit, optionCombo);
String value = parsedMessage.get(code.getCode());
Set<SMSSpecialCharacter> specialCharacters = command.getSpecialCharacters();
for (SMSSpecialCharacter each : specialCharacters) {
if (each.getName().equalsIgnoreCase(value)) {
value = each.getValue();
break;
}
}
if (!StringUtils.isEmpty(value)) {
boolean newDataValue = false;
if (dv == null) {
dv = new DataValue();
dv.setCategoryOptionCombo(optionCombo);
dv.setSource(orgunit);
dv.setDataElement(code.getDataElement());
dv.setPeriod(period);
dv.setComment("");
newDataValue = true;
}
if (ValueType.BOOLEAN == dv.getDataElement().getValueType()) {
if ("Y".equals(value.toUpperCase()) || "YES".equals(value.toUpperCase())) {
value = "true";
} else if ("N".equals(value.toUpperCase()) || "NO".equals(value.toUpperCase())) {
value = "false";
}
} else if (dv.getDataElement().getValueType().isInteger()) {
try {
Integer.parseInt(value);
} catch (NumberFormatException e) {
return false;
}
}
dv.setValue(value);
dv.setLastUpdated(new java.util.Date());
dv.setStoredBy(storedBy);
if (newDataValue) {
dataValueService.addDataValue(dv);
} else {
dataValueService.updateDataValue(dv);
}
}
if (code.getFormula() != null) {
try {
String formula = code.getFormula();
String targetDataElementId = formula.substring(1);
String operation = String.valueOf(formula.charAt(0));
DataElement targetDataElement = dataElementService.getDataElement(Integer.parseInt(targetDataElementId));
if (targetDataElement == null) {
return false;
}
DataValue targetDataValue = dataValueService.getDataValue(targetDataElement, period, orgunit, dataElementCategoryService.getDefaultCategoryOptionCombo());
int targetValue = 0;
boolean newTargetDataValue = false;
if (targetDataValue == null) {
targetDataValue = new DataValue();
targetDataValue.setCategoryOptionCombo(dataElementCategoryService.getDefaultCategoryOptionCombo());
targetDataValue.setSource(orgunit);
targetDataValue.setDataElement(targetDataElement);
targetDataValue.setPeriod(period);
targetDataValue.setComment("");
newTargetDataValue = true;
} else {
targetValue = Integer.parseInt(targetDataValue.getValue());
}
if (operation.equals("+")) {
targetValue = targetValue + Integer.parseInt(value);
} else if (operation.equals("-")) {
targetValue = targetValue - Integer.parseInt(value);
}
targetDataValue.setValue(String.valueOf(targetValue));
targetDataValue.setLastUpdated(new java.util.Date());
targetDataValue.setStoredBy(storedBy);
if (newTargetDataValue) {
dataValueService.addDataValue(targetDataValue);
} else {
dataValueService.updateDataValue(targetDataValue);
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
Aggregations