<commit_before>/*
 *  Tanaguru - Automated webpage assessment
 *  Copyright (C) 2008-2012  Open-S Company
 * 
 *  This file is part of Tanaguru.
 * 
 *  Tanaguru is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 * 
 *  This program 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 Affero General Public License for more details.
 * 
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact us by mail: open-s AT open-s DOT com
 */
package org.opens.tgol.security.userdetails;

import java.util.Collection;
import org.apache.commons.lang3.StringUtils;
import org.opens.tgol.entity.user.User;
import org.springframework.security.core.GrantedAuthority;

/**
 *
 * @author jkowalczyk
 */
public class TgolUserDetails extends org.springframework.security.core.userdetails.User {

    private static final long serialVersionUID = 1L;
    private User user;

    public User getUser() {
        return user;
    }
    
    private String displayedUserName;
    public String getDisplayedUserName() {
        return displayedUserName;
    }

    /**
     *
     * @param username
     * @param password
     * @param enabled
     * @param accountNonExpired
     * @param credentialsNonExpired
     * @param accountNonLocked
     * @param authorities
     * @param displayName
     * @param user
     * @throws IllegalArgumentException
     */
    public TgolUserDetails(
            String username, 
            String password,
            boolean enabled, 
            boolean accountNonExpired,
            boolean credentialsNonExpired, 
            boolean accountNonLocked,
            Collection<GrantedAuthority> authorities, 
            User user)
            throws IllegalArgumentException {
        super(username, password, enabled, accountNonExpired,
                credentialsNonExpired, accountNonLocked, authorities);
        this.user = user;
        this.displayedUserName = computeDisplayedUserName(user, username);
    }

    public void updateUser(User user) {
        this.user=user;
        this.displayedUserName = computeDisplayedUserName(user, getUsername());
    }
    
    /**
     * 
     * @param user
     * @param userName
     * @return 
     */
    private String computeDisplayedUserName(
            User user,
            String userName) {
        if (user != null && !StringUtils.equals(user.getEmail1(), "guest")) {
            if (!StringUtils.isEmpty(user.getFirstName().trim()) && 
                    !StringUtils.isEmpty(user.getName().trim())) {
                return user.getFirstName() + " "+ user.getName();
            } else if (!StringUtils.isEmpty(user.getName().trim())) {
                return user.getName();
            }
        }
        return userName;
    }
    
}<commit_msg>tgol-security : update javadoc<commit_after>/*
 *  Tanaguru - Automated webpage assessment
 *  Copyright (C) 2008-2012  Open-S Company
 * 
 *  This file is part of Tanaguru.
 * 
 *  Tanaguru is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 * 
 *  This program 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 Affero General Public License for more details.
 * 
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact us by mail: open-s AT open-s DOT com
 */
package org.opens.tgol.security.userdetails;

import java.util.Collection;
import org.apache.commons.lang3.StringUtils;
import org.opens.tgol.entity.user.User;
import org.springframework.security.core.GrantedAuthority;

/**
 *
 * @author jkowalczyk
 */
public class TgolUserDetails extends org.springframework.security.core.userdetails.User {

    private static final long serialVersionUID = 1L;
    private User user;

    public User getUser() {
        return user;
    }
    
    private String displayedUserName;
    public String getDisplayedUserName() {
        return displayedUserName;
    }

    /**
     *
     * @param username
     * @param password
     * @param enabled
     * @param accountNonExpired
     * @param credentialsNonExpired
     * @param accountNonLocked
     * @param authorities
     * @param user
     * @throws IllegalArgumentException
     */
    public TgolUserDetails(
            String username, 
            String password,
            boolean enabled, 
            boolean accountNonExpired,
            boolean credentialsNonExpired, 
            boolean accountNonLocked,
            Collection<GrantedAuthority> authorities, 
            User user)
            throws IllegalArgumentException {
        super(username, password, enabled, accountNonExpired,
                credentialsNonExpired, accountNonLocked, authorities);
        this.user = user;
        this.displayedUserName = computeDisplayedUserName(user, username);
    }

    public void updateUser(User user) {
        this.user=user;
        this.displayedUserName = computeDisplayedUserName(user, getUsername());
    }
    
    /**
     * 
     * @param user
     * @param userName
     * @return 
     */
    private String computeDisplayedUserName(
            User user,
            String userName) {
        if (user != null && !StringUtils.equals(user.getEmail1(), "guest")) {
            if (!StringUtils.isEmpty(user.getFirstName().trim()) && 
                    !StringUtils.isEmpty(user.getName().trim())) {
                return user.getFirstName() + " "+ user.getName();
            } else if (!StringUtils.isEmpty(user.getName().trim())) {
                return user.getName();
            }
        }
        return userName;
    }
    
}
{'fraction_non_alphanumeric': 0.08612289800408612, 'fraction_numerical': 0.003771805752003772, 'mean_word_length': 2.434430652995143, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5532572', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1655, 'n_words': 510}