<commit_before>/**
 * Copyright (C) 2011  JTalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.jtalks.jcommune.service.nontransactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jtalks.jcommune.model.dao.UserDao;
import org.jtalks.jcommune.model.entity.JCUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 
 * @author Anuar_Nurmakanov
 *
 */
public class UserMentionService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserMentionService.class);
    private MailService mailService;
    private UserDao userDao;
    
    
    /**
     * @param mailService
     * @param userDao
     */
    public UserMentionService(MailService mailService, UserDao userDao) {
        this.mailService = mailService;
        this.userDao = userDao;
    }

    /**
     * Extract all mentioned users and notify them.
     * 
     * @param canContainMentionedUsers can contains mentioned users
     * @param post where user was mentioned
     */
    public void notifyAllMentionedUsers(String canContainMentionedUsers, long mentioningPostId) {
        List<String> mentionedUsersNames = extractMentionedUsers(canContainMentionedUsers);
        sendNotificationToMentionedUsers(mentionedUsersNames, mentioningPostId);
    }
    
    /**
     * Extract all user names that were mentioned in passed text.
     * 
     * @return extracted user names
     */
    public List<String> extractMentionedUsers(String canContainMentionedUsers) {
        if (!StringUtils.isEmpty(canContainMentionedUsers)) {
            Pattern pattern = Pattern.compile("\\[user\\].*?\\[/user\\]");
            Matcher matcher = pattern.matcher(canContainMentionedUsers);
            List<String> mentionedUsernames = new ArrayList<String>();
            while (matcher.find()) {
                String userBBCode = matcher.group();
                String mentionedUser = userBBCode.replaceAll("\\[.*?\\]", StringUtils.EMPTY);
                mentionedUsernames.add(mentionedUser);
            }
            return mentionedUsernames;
        } 
        return Collections.emptyList();
    }
    
    /**
     * Send notification for all mentioned users.
     * 
     * @param mentionedUsernames the list of names of mentioned users
     * @param mentioningPostId an identifier of post where users where mentioned
     */
    private void sendNotificationToMentionedUsers(List<String> mentionedUsernames, long mentioningPostId) {
        List<JCUser> mentionedUsers = userDao.getByUsernames(mentionedUsernames);
        for (JCUser mentionedUser: mentionedUsers) {
            if (mentionedUser.isMentioningNotificationsEnabled()) {
                LOGGER.debug(mentionedUser.getUsername() + " was mentioned, email was sent to - " + mentionedUser.getEmail());
                mailService.sendUserMentionedNotification(mentionedUser, mentioningPostId);
            }
        }
    }
}
<commit_msg>#JC-1428 Problems with creation posts without users mentioning were removed.<commit_after>/**
 * Copyright (C) 2011  JTalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.jtalks.jcommune.service.nontransactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jtalks.jcommune.model.dao.UserDao;
import org.jtalks.jcommune.model.entity.JCUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 
 * @author Anuar_Nurmakanov
 *
 */
public class UserMentionService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserMentionService.class);
    private MailService mailService;
    private UserDao userDao;
    
    
    /**
     * @param mailService
     * @param userDao
     */
    public UserMentionService(MailService mailService, UserDao userDao) {
        this.mailService = mailService;
        this.userDao = userDao;
    }

    /**
     * Extract all mentioned users and notify them.
     * 
     * @param canContainMentionedUsers can contains mentioned users
     * @param post where user was mentioned
     */
    public void notifyAllMentionedUsers(String canContainMentionedUsers, long mentioningPostId) {
        List<String> mentionedUsersNames = extractMentionedUsers(canContainMentionedUsers);
        if (!CollectionUtils.isEmpty(mentionedUsersNames)) {
            sendNotificationToMentionedUsers(mentionedUsersNames, mentioningPostId);
        }
    }
    
    /**
     * Extract all user names that were mentioned in passed text.
     * 
     * @return extracted user names
     */
    public List<String> extractMentionedUsers(String canContainMentionedUsers) {
        if (!StringUtils.isEmpty(canContainMentionedUsers)) {
            Pattern pattern = Pattern.compile("\\[user\\].*?\\[/user\\]");
            Matcher matcher = pattern.matcher(canContainMentionedUsers);
            List<String> mentionedUsernames = new ArrayList<String>();
            while (matcher.find()) {
                String userBBCode = matcher.group();
                String mentionedUser = userBBCode.replaceAll("\\[.*?\\]", StringUtils.EMPTY);
                mentionedUsernames.add(mentionedUser);
            }
            return mentionedUsernames;
        } 
        return Collections.emptyList();
    }
    
    /**
     * Send notification for all mentioned users.
     * 
     * @param mentionedUsernames the list of names of mentioned users
     * @param mentioningPostId an identifier of post where users where mentioned
     */
    private void sendNotificationToMentionedUsers(List<String> mentionedUsernames, long mentioningPostId) {
        List<JCUser> mentionedUsers = userDao.getByUsernames(mentionedUsernames);
        for (JCUser mentionedUser: mentionedUsers) {
            if (mentionedUser.isMentioningNotificationsEnabled()) {
                LOGGER.debug(mentionedUser.getUsername() + " was mentioned, email was sent to - " + mentionedUser.getEmail());
                mailService.sendUserMentionedNotification(mentionedUser, mentioningPostId);
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.0830637857420106, 'fraction_numerical': 0.005434079441066115, 'mean_word_length': 3.13148049171566, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '3528839', 'n_tokens_mistral': 2100, 'n_tokens_neox': 2048, 'n_words': 609}