package org.sakaiproject.sitemanage.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.email.api.EmailService;
import org.sakaiproject.sitemanage.api.UserNotificationProvider;
import org.sakaiproject.user.api.User;
import org.sakaiproject.user.api.UserDirectoryService;
import org.sakaiproject.util.ResourceLoader;




public class UserNotificationProviderImpl implements UserNotificationProvider {
	
	private static Log M_log = LogFactory.getLog(UserNotificationProviderImpl.class);
	private EmailService emailService; 
	
	public void setEmailService(EmailService es) {
		emailService = es;
	}
	
	private ServerConfigurationService serverConfigurationService;
	public void setServerConfigurationService(ServerConfigurationService scs) {
		serverConfigurationService = scs;
	}
	
	private UserDirectoryService userDirectoryService;
	public void setUserDirectoryService(UserDirectoryService uds) {
		userDirectoryService = uds;
	}
	
	/** portlet configuration parameter values* */
	/** Resource bundle using current language locale */
	private static ResourceLoader rb = new ResourceLoader("UserNotificationProvider");

	public void init() {
		//nothing realy to do
		M_log.info("init()");
	}
	
	public void notifyAddedParticipant(boolean newNonOfficialAccount,
			User user, String siteTitle) {
		rb = new ResourceLoader(user.getId(), "UserNotificationProvider");
		
		String from = getSetupRequestEmailAddress();
		if (from != null) {
			String productionSiteName = serverConfigurationService.getString(
					"ui.service", "");
			String productionSiteUrl = serverConfigurationService
					.getPortalUrl();
			String nonOfficialAccountUrl = serverConfigurationService.getString(
					"nonOfficialAccount.url", null);
			String emailId = user.getEmail();
			String to = emailId;
			String headerTo = emailId;
			String replyTo = emailId;
			String message_subject = productionSiteName + " "
					+ rb.getString("java.sitenoti");
			String content = "";
			StringBuilder buf = new StringBuilder();
			buf.setLength(0);

			// email bnonOfficialAccounteen newly added nonOfficialAccount account
			// and other users
			buf.append(user.getDisplayName() + ":\n\n");
			buf.append(rb.getString("java.following") + " "
					+ productionSiteName + " "
					+ rb.getString("java.simplesite") + "\n");
			buf.append(siteTitle + "\n");
			buf.append(rb.getString("java.simpleby") + " ");
			buf.append(userDirectoryService.getCurrentUser().getDisplayName()
					+ ". \n\n");
			if (newNonOfficialAccount) {
				buf.append(serverConfigurationService.getString(
						"nonOfficialAccountInstru", "")
						+ "\n");

				if (nonOfficialAccountUrl != null) {
					buf.append(rb.getString("java.togeta1") + "\n"
							+ nonOfficialAccountUrl + "\n");
					buf.append(rb.getString("java.togeta2") + "\n\n");
				}
				buf.append(rb.getString("java.once") + " " + productionSiteName
						+ ": \n");
				buf.append(rb.getString("java.loginhow1") + " "
						+ productionSiteName + ": " + productionSiteUrl + "\n");
				buf.append(rb.getString("java.loginhow2") + "\n");
				buf.append(rb.getString("java.loginhow3") + "\n");
			} else {
				buf.append(rb.getString("java.tolog") + "\n");
				buf.append(rb.getString("java.loginhow1") + " "
						+ productionSiteName + ": " + productionSiteUrl + "\n");
				buf.append(rb.getString("java.loginhow2") + "\n");
				buf.append(rb.getString("java.loginhow3u") + "\n");
			}
			buf.append(rb.getString("java.tabscreen"));
			content = buf.toString();
			emailService.send(from, to, message_subject, content, headerTo,
					replyTo, null);

		} // if

	}

	public void notifyNewUserEmail(User user, String newUserPassword,
			String siteTitle) {
		rb = new ResourceLoader("UserNotificationProvider");
		// set the locale to individual receipient's setting
		rb.setContextLocale(rb.getLocale(user.getId()));
		
		String from = getSetupRequestEmailAddress();
		String productionSiteName = serverConfigurationService.getString(
				"ui.service", "");
		String productionSiteUrl = serverConfigurationService.getPortalUrl();
		
		String newUserEmail = user.getEmail();
		String to = newUserEmail;
		String headerTo = newUserEmail;
		String replyTo = newUserEmail;
		String message_subject = productionSiteName + " "
				+ rb.getString("java.newusernoti");
		String content = "";

		if (from != null && newUserEmail != null) {
			StringBuilder buf = new StringBuilder();
			buf.setLength(0);

			// email body
			buf.append(user.getDisplayName() + ":\n\n");

			buf.append(rb.getString("java.addedto") + " " + productionSiteName
					+ " (" + productionSiteUrl + ") ");
			buf.append(rb.getString("java.simpleby") + " ");
			buf.append(userDirectoryService.getCurrentUser().getDisplayName()
					+ ". \n\n");
			buf.append(rb.getString("java.passwordis1") + "\n"
					+ newUserPassword + "\n\n");
			buf.append(rb.getString("java.passwordis2") + "\n\n");

			content = buf.toString();
			emailService.send(from, to, message_subject, content, headerTo,
					replyTo, null);
		}
	}

	/*
	 *  Private methods
	 */
	
	private String getSetupRequestEmailAddress() {
		String from = serverConfigurationService.getString("setup.request",
				null);
		if (from == null) {
			M_log.warn(this + " - no 'setup.request' in configuration");
			from = "postmaster@".concat(serverConfigurationService
					.getServerName());
		}
		return from;
	}
}

@@ -5313,72 +5313,8 @@
 ) %7B%0A
-%09%09%09M_log.warn(this + %22 - no 'setup.request' in configuration%22);%0A
 %09%09%09f
@@ -5391,16 +5391,95 @@
 ame());%0A
+%09%09%09M_log.warn(this + %22 - no 'setup.request' in configuration, using: %22+ from);%0A
 %09%09%7D%0A%09%09re

fix to SAK-14507:Better log message when no setup.request from site setup 2