Search in sources :

Example 6 with SMSCommand

use of org.hisp.dhis.sms.command.SMSCommand in project dhis2-core by dhis2.

the class SmsController method getSmsCommandTypes.

@PreAuthorize("hasRole('ALL') or hasRole('F_MOBILE_SENDSMS')")
@RequestMapping(value = "/commands/{commandName}", method = RequestMethod.GET, produces = "application/json")
public void getSmsCommandTypes(@PathVariable("commandName") String commandName, @RequestParam ParserType type, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
    SMSCommand command = smsCommandService.getSMSCommand(commandName, type);
    if (command == null) {
        throw new WebMessageException(WebMessageUtils.notFound("No SMS command found"));
    }
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), command);
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with SMSCommand

use of org.hisp.dhis.sms.command.SMSCommand in project dhis2-core by dhis2.

the class CreateSMSCommandForm method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    SMSCommand command = new SMSCommand();
    command.setName(name);
    command.setParserType(parserType);
    if (parserType.equals(ParserType.KEY_VALUE_PARSER) || parserType.equals(ParserType.J2ME_PARSER)) {
        DataSet dataset = dataSetService.getDataSet(selectedDataSetID);
        command.setDataset(dataset);
    } else if (parserType.equals(ParserType.ALERT_PARSER) || parserType.equals(ParserType.UNREGISTERED_PARSER)) {
        UserGroup userGroup = new UserGroup();
        userGroup = userGroupService.getUserGroup(userGroupID);
        command.setUserGroup(userGroup);
    } else if (parserType.equals(ParserType.TRACKED_ENTITY_REGISTRATION_PARSER)) {
        Program program = programService.getProgram(selectedProgramId);
        command.setProgram(program);
    } else if (parserType.equals(ParserType.EVENT_REGISTRATION_PARSER)) {
        Program program = programService.getProgram(selectedProgramIdWithoutRegistration);
        command.setProgram(program);
        command.setProgramStage(program.getProgramStages().iterator().next());
    }
    smsCommandService.save(command);
    return SUCCESS;
}
Also used : Program(org.hisp.dhis.program.Program) DataSet(org.hisp.dhis.dataset.DataSet) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) UserGroup(org.hisp.dhis.user.UserGroup)

Example 8 with SMSCommand

use of org.hisp.dhis.sms.command.SMSCommand 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;
}
Also used : SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SMSSpecialCharacter(org.hisp.dhis.sms.command.SMSSpecialCharacter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Example 9 with SMSCommand

use of org.hisp.dhis.sms.command.SMSCommand in project dhis2-core by dhis2.

the class HibernateSMSCommandStore method getSMSCommand.

@Override
public SMSCommand getSMSCommand(String commandName, ParserType parserType) {
    CriteriaBuilder builder = getCriteriaBuilder();
    List<SMSCommand> list = getList(builder, newJpaParameters().addPredicate(root -> builder.equal(root.get("parserType"), parserType)).addPredicate(root -> JpaQueryUtils.stringPredicateIgnoreCase(builder, root.get("name"), commandName, JpaQueryUtils.StringSearchMode.ANYWHERE)));
    if (list != null && !list.isEmpty()) {
        return list.get(0);
    }
    return null;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) DataSet(org.hisp.dhis.dataset.DataSet) JpaQueryUtils(org.hisp.dhis.query.JpaQueryUtils) SessionFactory(org.hibernate.SessionFactory) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) List(java.util.List) HibernateIdentifiableObjectStore(org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Query(org.hibernate.query.Query) AclService(org.hisp.dhis.security.acl.AclService) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Repository(org.springframework.stereotype.Repository) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) ParserType(org.hisp.dhis.sms.parse.ParserType) SMSCommand(org.hisp.dhis.sms.command.SMSCommand)

Example 10 with SMSCommand

use of org.hisp.dhis.sms.command.SMSCommand in project dhis2-core by dhis2.

the class SMSCommandServiceTest method testGetCommandById.

@Test
void testGetCommandById() {
    smsCommandService.save(keyValueCommandA);
    smsCommandService.save(keyValueCommandB);
    List<SMSCommand> commands = smsCommandService.getSMSCommands();
    long id = commands.iterator().next().getId();
    SMSCommand fetched = smsCommandService.getSMSCommand(id);
    assertNotNull(fetched);
}
Also used : SMSCommand(org.hisp.dhis.sms.command.SMSCommand) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

SMSCommand (org.hisp.dhis.sms.command.SMSCommand)41 Test (org.junit.jupiter.api.Test)17 DhisSpringTest (org.hisp.dhis.DhisSpringTest)14 SMSCode (org.hisp.dhis.sms.command.code.SMSCode)14 User (org.hisp.dhis.user.User)9 Transactional (org.springframework.transaction.annotation.Transactional)8 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 SMSParserException (org.hisp.dhis.sms.parse.SMSParserException)7 SMSSpecialCharacter (org.hisp.dhis.sms.command.SMSSpecialCharacter)6 UserGroup (org.hisp.dhis.user.UserGroup)6 List (java.util.List)5 Set (java.util.Set)5 DataSet (org.hisp.dhis.dataset.DataSet)5 Program (org.hisp.dhis.program.Program)5 ParserType (org.hisp.dhis.sms.parse.ParserType)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 ProgramService (org.hisp.dhis.program.ProgramService)4 SMSCommandService (org.hisp.dhis.sms.command.SMSCommandService)4 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)4