Search in sources :

Example 1 with UserGroup

use of org.hisp.dhis.user.UserGroup in project dhis2-core by dhis2.

the class MessageConversationController method postObject.

private void postObject(HttpServletResponse response, HttpServletRequest request, MessageConversation messageConversation) throws WebMessageException {
    List<User> users = new ArrayList<>(messageConversation.getUsers());
    messageConversation.getUsers().clear();
    for (OrganisationUnit ou : messageConversation.getOrganisationUnits()) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(ou.getUid());
        if (organisationUnit == null) {
            throw new WebMessageException(WebMessageUtils.conflict("Organisation Unit does not exist: " + ou.getUid()));
        }
        messageConversation.getUsers().addAll(organisationUnit.getUsers());
    }
    for (User u : users) {
        User user = userService.getUser(u.getUid());
        if (user == null) {
            throw new WebMessageException(WebMessageUtils.conflict("User does not exist: " + u.getUid()));
        }
        messageConversation.getUsers().add(user);
    }
    for (UserGroup ug : messageConversation.getUserGroups()) {
        UserGroup userGroup = userGroupService.getUserGroup(ug.getUid());
        if (userGroup == null) {
            throw new WebMessageException(WebMessageUtils.notFound("User Group does not exist: " + ug.getUid()));
        }
        messageConversation.getUsers().addAll(userGroup.getMembers());
    }
    if (messageConversation.getUsers().isEmpty()) {
        throw new WebMessageException(WebMessageUtils.conflict("No recipients selected."));
    }
    String metaData = MessageService.META_USER_AGENT + request.getHeader(ContextUtils.HEADER_USER_AGENT);
    int id = messageService.sendPrivateMessage(messageConversation.getSubject(), messageConversation.getText(), metaData, messageConversation.getUsers());
    org.hisp.dhis.message.MessageConversation conversation = messageService.getMessageConversation(id);
    response.addHeader("Location", MessageConversationSchemaDescriptor.API_ENDPOINT + "/" + conversation.getUid());
    webMessageService.send(WebMessageUtils.created("Message conversation created"), response, request);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ArrayList(java.util.ArrayList) UserGroup(org.hisp.dhis.user.UserGroup)

Example 2 with UserGroup

use of org.hisp.dhis.user.UserGroup in project dhis2-core by dhis2.

the class ConfigurationServiceTest method testConfiguration.

@Test
public void testConfiguration() {
    User userA = createUser('A');
    User userB = createUser('B');
    UserGroup group = new UserGroup("UserGroupA");
    group.getMembers().add(userA);
    group.getMembers().add(userB);
    userService.addUser(userA);
    userService.addUser(userB);
    userGroupService.addUserGroup(group);
    Configuration config = configurationService.getConfiguration();
    assertNull(config.getFeedbackRecipients());
    config.setFeedbackRecipients(group);
    configurationService.setConfiguration(config);
    config = configurationService.getConfiguration();
    assertNotNull(config.getFeedbackRecipients());
    assertEquals(group, config.getFeedbackRecipients());
}
Also used : User(org.hisp.dhis.user.User) Configuration(org.hisp.dhis.configuration.Configuration) UserGroup(org.hisp.dhis.user.UserGroup) DhisSpringTest(org.hisp.dhis.DhisSpringTest) Test(org.junit.Test)

Example 3 with UserGroup

use of org.hisp.dhis.user.UserGroup in project dhis2-core by dhis2.

the class DefaultPushAnalysisService method runPushAnalysis.

@Override
public void runPushAnalysis(int id, TaskId taskId) {
    //----------------------------------------------------------------------
    // Set up
    //----------------------------------------------------------------------
    PushAnalysis pushAnalysis = pushAnalysisStore.get(id);
    Set<User> receivingUsers = new HashSet<>();
    notifier.clear(taskId);
    //----------------------------------------------------------------------
    // Pre-check
    //----------------------------------------------------------------------
    log(taskId, NotificationLevel.INFO, "Starting pre-check on PushAnalysis", false, null);
    if (!setPushAnalysisIsRunningFlag(pushAnalysis, true)) {
        log(taskId, NotificationLevel.ERROR, "PushAnalysis with id '" + id + "' is already running. Terminating new PushAnalysis job.", true, null);
        return;
    }
    if (pushAnalysis == null) {
        log(taskId, NotificationLevel.ERROR, "PushAnalysis with id '" + id + "' was not found. Terminating PushAnalysis", true, null);
        return;
    }
    if (pushAnalysis.getRecipientUserGroups().size() == 0) {
        log(taskId, NotificationLevel.ERROR, "PushAnalysis with id '" + id + "' has no userGroups assigned. Terminating PushAnalysis.", true, null);
        return;
    }
    if (pushAnalysis.getDashboard() == null) {
        log(taskId, NotificationLevel.ERROR, "PushAnalysis with id '" + id + "' has no dashboard assigned. Terminating PushAnalysis.", true, null);
        return;
    }
    if (systemSettingManager.getInstanceBaseUrl() == null) {
        log(taskId, NotificationLevel.ERROR, "Missing system setting '" + SettingKey.INSTANCE_BASE_URL.getName() + "'. Terminating PushAnalysis.", true, null);
        return;
    }
    log(taskId, NotificationLevel.INFO, "pre-check completed successfully", false, null);
    //----------------------------------------------------------------------
    // Compose list of users that can receive PushAnalysis
    //----------------------------------------------------------------------
    log(taskId, NotificationLevel.INFO, "Composing list of receiving users", false, null);
    for (UserGroup userGroup : pushAnalysis.getRecipientUserGroups()) {
        for (User user : userGroup.getMembers()) {
            if (!user.hasEmail()) {
                log(taskId, NotificationLevel.WARN, "Skipping user: User '" + user.getUsername() + "' is missing a valid email.", false, null);
                continue;
            }
            receivingUsers.add(user);
        }
    }
    log(taskId, NotificationLevel.INFO, "List composed. " + receivingUsers.size() + " eligible users found.", false, null);
    //----------------------------------------------------------------------
    // Generating reports
    //----------------------------------------------------------------------
    log(taskId, NotificationLevel.INFO, "Generating and sending reports", false, null);
    for (User user : receivingUsers) {
        try {
            String title = pushAnalysis.getTitle();
            String html = generateHtmlReport(pushAnalysis, user, taskId);
            // TODO: Better handling of messageStatus; Might require refactoring of EmailMessageSender
            @SuppressWarnings("unused") OutboundMessageResponse status = messageSender.sendMessage(title, html, "", null, Sets.newHashSet(user), true);
        } catch (Exception e) {
            log(taskId, NotificationLevel.ERROR, "Could not create or send report for PushAnalysis '" + pushAnalysis.getName() + "' and User '" + user.getUsername() + "': " + e.getMessage(), false, e);
        }
    }
    // Update lastRun date
    pushAnalysis.setLastRun(new Date());
    pushAnalysisStore.update(pushAnalysis);
    setPushAnalysisIsRunningFlag(pushAnalysis, false);
}
Also used : User(org.hisp.dhis.user.User) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse) IOException(java.io.IOException) Date(java.util.Date) HashSet(java.util.HashSet) UserGroup(org.hisp.dhis.user.UserGroup)

Example 4 with UserGroup

use of org.hisp.dhis.user.UserGroup in project dhis2-core by dhis2.

the class DefaultConfigurationService method isUserInFeedbackRecipientUserGroup.

@Override
public boolean isUserInFeedbackRecipientUserGroup(User user) {
    user = (user == null ? currentUserService.getCurrentUser() : user);
    UserGroup feedbackRecipients = getConfiguration().getFeedbackRecipients();
    return feedbackRecipients != null && feedbackRecipients.getMembers().contains(user);
}
Also used : UserGroup(org.hisp.dhis.user.UserGroup)

Example 5 with UserGroup

use of org.hisp.dhis.user.UserGroup in project dhis2-core by dhis2.

the class DhisMessageAlertListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    String message = sms.getText();
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.ALERT_PARSER);
    UserGroup userGroup = smsCommand.getUserGroup();
    String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
    if (userGroup != null) {
        Collection<User> users = userService.getUsersByPhoneNumber(senderPhoneNumber);
        if (users != null && users.size() > 1) {
            String messageMoreThanOneUser = smsCommand.getMoreThanOneOrgUnitMessage();
            if (messageMoreThanOneUser.trim().equals("")) {
                messageMoreThanOneUser = SMSCommand.MORE_THAN_ONE_ORGUNIT_MESSAGE;
            }
            for (Iterator<User> i = users.iterator(); i.hasNext(); ) {
                User user = i.next();
                messageMoreThanOneUser += " " + user.getName();
                if (i.hasNext()) {
                    messageMoreThanOneUser += ",";
                }
            }
            throw new SMSParserException(messageMoreThanOneUser);
        } else if (users != null && users.size() == 1) {
            User sender = users.iterator().next();
            Set<User> receivers = new HashSet<>(userGroup.getMembers());
            messageService.sendMessage(smsCommand.getName(), message, null, receivers, sender, MessageType.SYSTEM, false);
            Set<User> feedbackList = new HashSet<>();
            feedbackList.add(sender);
            String confirmMessage = smsCommand.getReceivedMessage();
            if (confirmMessage == null) {
                confirmMessage = SMSCommand.ALERT_FEEDBACK;
            }
            if (smsSender.isConfigured()) {
                smsSender.sendMessage(smsCommand.getName(), confirmMessage, null, null, feedbackList, false);
            } else {
                Log.info("No sms configuration found.");
            }
            sms.setStatus(SmsMessageStatus.PROCESSED);
            sms.setParsed(true);
            incomingSmsService.update(sms);
        } else if (users == null || users.size() == 0) {
            throw new SMSParserException("No user associated with this phone number. Please contact your supervisor.");
        }
    }
}
Also used : User(org.hisp.dhis.user.User) Set(java.util.Set) HashSet(java.util.HashSet) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) UserGroup(org.hisp.dhis.user.UserGroup) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserGroup (org.hisp.dhis.user.UserGroup)76 User (org.hisp.dhis.user.User)50 Test (org.junit.jupiter.api.Test)32 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)23 UserGroupAccess (org.hisp.dhis.user.sharing.UserGroupAccess)22 DataElement (org.hisp.dhis.dataelement.DataElement)17 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)12 HashSet (java.util.HashSet)11 List (java.util.List)11 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)10 UserAccess (org.hisp.dhis.user.sharing.UserAccess)10 ClassPathResource (org.springframework.core.io.ClassPathResource)9 Program (org.hisp.dhis.program.Program)6 ArrayList (java.util.ArrayList)5 CategoryOption (org.hisp.dhis.category.CategoryOption)5 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)4 UserGroupAccess (org.hisp.dhis.user.UserGroupAccess)4