<commit_before>package org.esupportail.smsu.business;

import java.util.List;
import java.util.Set;

import org.esupportail.commons.services.i18n.I18nService;
import org.esupportail.commons.services.ldap.LdapUser;
import org.esupportail.commons.services.logging.Logger;
import org.esupportail.commons.services.logging.LoggerImpl;
import org.esupportail.smsu.exceptions.ldap.LdapUserNotFoundException;
import org.esupportail.smsu.services.client.ListPhoneNumbersInBlackListClient;
import org.esupportail.smsu.services.ldap.LdapUtils;
import org.esupportail.smsu.services.smtp.SmtpServiceUtils;

/**
 * Business layer concerning smsu notification invalid mail.
 *
 */
public class NotificationByMailForInvalidPhoneManager {
	
	
	/**
	 * {@link i18nService}.
	 */
	private I18nService i18nService;
	
	/**
	 *  {@link ldapUtils}.
	 */
	private LdapUtils ldapUtils;
	
	/**
	 * {@link smtpServiceUtils}.
	 */
	private SmtpServiceUtils smtpServiceUtils;
	
	/**
	 * {@link listPhoneNumbersInBlackListClient}.
	 */
	private ListPhoneNumbersInBlackListClient listPhoneNumbersInBlackListClient;

	/**
	 * Log4j logger.
	 */
	private final Logger logger = new LoggerImpl(getClass());
	
	///////////////////////////////////////
	//  constructor
	//////////////////////////////////////
	/**
	 * Bean constructor.
	 */
	public NotificationByMailForInvalidPhoneManager() {
		
	}
	
	///////////////////////////////////////
	//  Private method
	//////////////////////////////////////
	/**
	 * Get list of phone numbers in blacklist.
	 * @return return set of phone numbers 
	 */
	private Set<String> getListePhoneNumberInBlackList() {
		if (logger.isDebugEnabled()) {
			logger.debug("Request getListePhoneNumberInBlackList in " 
					+ "NotificationByMailForInvalidPhoneManager");
		}
		
		Set<String> retVal = null;
		try {
		 retVal = listPhoneNumbersInBlackListClient.getListPhoneNumbersInBlackList(); 
		 if (logger.isDebugEnabled()) {
				final StringBuilder sb = new StringBuilder(500);
				sb.append("Response for getListPhoneNumbersInBlackList request " 
						+ "in NotificationByMailForInvalidPhoneManager: ");
				for (String nb : retVal) {
				sb.append(" - phone number in blacklist = ").append(nb);	
				}
				logger.debug(sb.toString());
			}
		} catch (Exception e) {
			if (logger.isDebugEnabled()) {
				logger.debug("Probleme de connexion Web Service !!!");
			}
		}
		
		return retVal;
	}
	
	/**
	 * retrieve mail based on uid.
	 * @param uid
	 * @return
	 */
	private String getMail(final String uid) {
		if (logger.isDebugEnabled()) {
			logger.debug("Get the mail for user:  " + uid);
		}
		String mail = null;
		try {
			mail = ldapUtils.getUserEmailAdressByUid(uid);
			if (logger.isDebugEnabled()) {
				logger.debug("The mail is:  " + mail);
			}
		} catch (LdapUserNotFoundException e) {
			if (logger.isDebugEnabled()) {
				logger.debug(e.getMessage());
			}
		}
		
		return mail;
	}

	///////////////////////////////////////
	//  Public method
	//////////////////////////////////////
	/**
	 * sendMails method called by Quartz task.
	 * @return
	 */
	public void sendMails() {
		if (logger.isDebugEnabled()) {
			logger.debug("Enter to sendMails method");
		}
		// 1 - Retrieve the list of phone numbers in blacklist 
		Set<String> listPhones = getListePhoneNumberInBlackList();
		
			for (String phoneNumber : listPhones) {
				if (logger.isDebugEnabled()) {
					logger.debug("search ldapUser for phone number:" + phoneNumber);
				}
			// 2 - Retrieve the ldapUser 	
			List<LdapUser>  list = ldapUtils.searchLdapUsersByPhoneNumber(phoneNumber);
    		if (!list.isEmpty()) {
    		String uid = list.get(0).getId();
    		String mail = getMail(uid);
			    		if (mail != null) {
			    		// 3 - Send mail
			        	String subject = getI18nService().getString("MSG.SUBJECT.MAIL.TO.INVALIDPHONE", 
			        					 getI18nService().getDefaultLocale());
			        	
			    		String textBody = getI18nService().getString("MSG.TEXTBOX.MAIL.TO.INVALIDPHONE",
			    					  getI18nService().getDefaultLocale(), phoneNumber);
			        	
			        	smtpServiceUtils.sendOneMessage(mail, subject, textBody);
			    		} else {
			    		logger.error("no mail for blacklisted uid " + uid + " with phone number " + phoneNumber);
			    		}
    		} else {
    		logger.info("no user found for once-invalid-so-blacklisted phone number: " + phoneNumber);
    		}
			}	
	}<commit_msg>smsu: ensure we notify the good user (in case of invalid phone number "", it notified the first LDAP user)<commit_after>package org.esupportail.smsu.business;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import org.esupportail.commons.services.i18n.I18nService;
import org.esupportail.commons.services.ldap.LdapUser;
import org.esupportail.commons.services.logging.Logger;
import org.esupportail.commons.services.logging.LoggerImpl;
import org.esupportail.smsu.exceptions.ldap.LdapUserNotFoundException;
import org.esupportail.smsu.services.client.ListPhoneNumbersInBlackListClient;
import org.esupportail.smsu.services.ldap.LdapUtils;
import org.esupportail.smsu.services.smtp.SmtpServiceUtils;

/**
 * Business layer concerning smsu notification invalid mail.
 *
 */
public class NotificationByMailForInvalidPhoneManager {
	
	
	/**
	 * {@link i18nService}.
	 */
	private I18nService i18nService;
	
	/**
	 *  {@link ldapUtils}.
	 */
	private LdapUtils ldapUtils;
	
	/**
	 * {@link smtpServiceUtils}.
	 */
	private SmtpServiceUtils smtpServiceUtils;
	
	/**
	 * {@link listPhoneNumbersInBlackListClient}.
	 */
	private ListPhoneNumbersInBlackListClient listPhoneNumbersInBlackListClient;

	/**
	 * Log4j logger.
	 */
	private final Logger logger = new LoggerImpl(getClass());
	
	///////////////////////////////////////
	//  constructor
	//////////////////////////////////////
	/**
	 * Bean constructor.
	 */
	public NotificationByMailForInvalidPhoneManager() {
		
	}
	
	///////////////////////////////////////
	//  Private method
	//////////////////////////////////////
	/**
	 * Get list of phone numbers in blacklist.
	 * @return return set of phone numbers 
	 */
	private Set<String> getListePhoneNumberInBlackList() {
		if (logger.isDebugEnabled()) {
			logger.debug("Request getListePhoneNumberInBlackList in " 
					+ "NotificationByMailForInvalidPhoneManager");
		}
		
		Set<String> retVal = null;
		try {
		 retVal = listPhoneNumbersInBlackListClient.getListPhoneNumbersInBlackList(); 
		 if (logger.isDebugEnabled()) {
				final StringBuilder sb = new StringBuilder(500);
				sb.append("Response for getListPhoneNumbersInBlackList request " 
						+ "in NotificationByMailForInvalidPhoneManager: ");
				for (String nb : retVal) {
				sb.append(" - phone number in blacklist = ").append(nb);	
				}
				logger.debug(sb.toString());
			}
		} catch (Exception e) {
			if (logger.isDebugEnabled()) {
				logger.debug("Probleme de connexion Web Service !!!");
			}
		}
		
		return retVal;
	}
	
	/**
	 * retrieve mail based on uid.
	 * @param uid
	 * @return
	 */
	private String getMail(final String uid) {
		if (logger.isDebugEnabled()) {
			logger.debug("Get the mail for user:  " + uid);
		}
		String mail = null;
		try {
			mail = ldapUtils.getUserEmailAdressByUid(uid);
			if (logger.isDebugEnabled()) {
				logger.debug("The mail is:  " + mail);
			}
		} catch (LdapUserNotFoundException e) {
			if (logger.isDebugEnabled()) {
				logger.debug(e.getMessage());
			}
		}
		
		return mail;
	}

	///////////////////////////////////////
	//  Public method
	//////////////////////////////////////
	/**
	 * sendMails method called by Quartz task.
	 * @return
	 */
	public void sendMails() {
		if (logger.isDebugEnabled()) {
			logger.debug("Enter to sendMails method");
		}
		// 1 - Retrieve the list of phone numbers in blacklist 
		Set<String> listPhones = getListePhoneNumberInBlackList();
		
			for (String phoneNumber : listPhones) {
				if (StringUtils.isEmpty(phoneNumber)) // ensure weird numbers are discarded
					continue;

				if (logger.isDebugEnabled()) {
					logger.debug("search ldapUser for phone number:" + phoneNumber);
				}
			// 2 - Retrieve the ldapUser 	
			List<LdapUser>  list = ldapUtils.searchLdapUsersByPhoneNumber(phoneNumber);
		if (list.size() == 1) {
    		String uid = list.get(0).getId();
    		String mail = getMail(uid);
			    		if (mail != null) {
			    		// 3 - Send mail
			        	String subject = getI18nService().getString("MSG.SUBJECT.MAIL.TO.INVALIDPHONE", 
			        					 getI18nService().getDefaultLocale());
			        	
			    		String textBody = getI18nService().getString("MSG.TEXTBOX.MAIL.TO.INVALIDPHONE",
			    					  getI18nService().getDefaultLocale(), phoneNumber);
			        	
			        	smtpServiceUtils.sendOneMessage(mail, subject, textBody);
			    		} else {
			    		logger.error("no mail for blacklisted uid " + uid + " with phone number " + phoneNumber);
			    		}
    		} else {
    		logger.info("no user found for once-invalid-so-blacklisted phone number: " + phoneNumber);
    		}
			}	
	}
{'fraction_non_alphanumeric': 0.16209614744087933, 'fraction_numerical': 0.005884312201620961, 'mean_word_length': 2.6366572466693583, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '379210', 'n_tokens_mistral': 3143, 'n_tokens_neox': 2722, 'n_words': 563}