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);
}
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());
}
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);
}
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);
}
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.");
}
}
}
Aggregations