All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.carleton.gcrc.couch.command.servlet.MailServiceRecipientsCouchDb Maven / Gradle / Ivy

There is a newer version: 2.2.7
Show newest version
package ca.carleton.gcrc.couch.command.servlet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import ca.carleton.gcrc.couch.user.UserDesignDocument;
import ca.carleton.gcrc.couch.user.UserDocument;
import ca.carleton.gcrc.mail.MailRecipient;
import ca.carleton.gcrc.mail.MailServiceRecipients;

public class MailServiceRecipientsCouchDb implements MailServiceRecipients {

	private String atlasName = null;
	private UserDesignDocument userDesignDocument;
	
	public MailServiceRecipientsCouchDb(
		String atlasName,
		UserDesignDocument userDesignDocument ){
		
		this.atlasName = atlasName;
		this.userDesignDocument = userDesignDocument;
	}
	
	@Override
	public List getDefaultRecipients() throws Exception {
		Collection usersWithRoles = new Vector();
		{
			List roles = new ArrayList(2);
			roles.add("administrator"); // global administrators
			roles.add(atlasName+"_administrator"); // atlas administrators
			
			usersWithRoles = userDesignDocument.getUsersWithRoles(roles);
		}
		
		List recipients = recipientsFromUser(usersWithRoles);
		return recipients;
	}

	@Override
	public List getRecipientsForDestination(String destination) throws Exception {
		Collection allUsers = userDesignDocument.getAllUsers();
		
		List usersForDestination = new ArrayList( allUsers.size() );
		for(UserDocument userDocument : allUsers){
			Set mailDestinations = userDocument.getMailDestinations();
			if( mailDestinations.contains(destination) ){
				usersForDestination.add(userDocument);
			}
		}
		
		List recipients = recipientsFromUser(usersForDestination);
		return recipients;
	}
	
	private List recipientsFromUser(Collection users){
		List recipients = new ArrayList(users.size());
		for(UserDocument user : users){
			String display = user.getDisplayName();
			Collection emails = user.getEmails();
			for(String email : emails){
				if( null == display ) {
					recipients.add( new MailRecipient(email) );
				} else {
					recipients.add( new MailRecipient(email,display) );
				}
			}
		}
		
		return recipients;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy