User.java

  1. /*
  2.  * This software was designed and created by Jason Carroll.
  3.  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
  4.  * The author can be reached at jcarroll@cowsultants.com
  5.  * ITracker website: http://www.cowsultants.com
  6.  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it only under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */

  18. package org.itracker.model;

  19. import org.apache.commons.lang.StringUtils;
  20. import org.apache.commons.lang.builder.CompareToBuilder;
  21. import org.apache.commons.lang.builder.ToStringBuilder;
  22. import org.apache.log4j.Logger;
  23. import org.itracker.model.util.UserUtilities;

  24. import javax.mail.internet.AddressException;
  25. import javax.mail.internet.InternetAddress;
  26. import java.io.UnsupportedEncodingException;
  27. import java.util.*;

  28. /**
  29.  * A user.
  30.  *
  31.  * @author ready
  32.  */
  33. public class User extends AbstractEntity implements Comparable<Entity> {

  34.    /**
  35.     *
  36.     */
  37.    private static final long serialVersionUID = 1L;
  38.    private static final Logger log = Logger.getLogger(User.class);
  39.    public static final Comparator<User> NAME_COMPARATOR = new NameComparator();
  40.    public static final Comparator<User> LOGIN_COMPARATOR = new LoginComparator();

  41.    private String login;

  42.    private String password;

  43.    private String firstName;

  44.    private String lastName;

  45.    private String email;

  46.    private int status;

  47.    private boolean superUser;

  48.    private int registrationType;

  49.    /**
  50.     * The Permissions of this User on all Projects.
  51.     */
  52.    private Set<Permission> permissions = new TreeSet<Permission>(Permission.PERMISSION_PROPERTIES_COMPARATOR);

  53.    /**
  54.     * The Projects owned by this User.
  55.     */
  56.    private List<Project> projects = new ArrayList<Project>();
  57.    private UserPreferences preferences;

  58.    public UserPreferences getPreferences() {
  59.       return preferences;
  60.    }

  61.    public void setPreferences(UserPreferences preferences) {
  62.       this.preferences = preferences;
  63.    }

  64.    /**
  65.     * Default constructor (required by Hibernate).
  66.     * <p/>
  67.     * <p>
  68.     * PENDING: should be <code>private</code> so that it can only be used by
  69.     * Hibernate, to ensure that the fields which form an instance's identity
  70.     * are always initialized/never <tt>null</tt>.
  71.     * </p>
  72.     */
  73.    public User() {
  74.    }

  75.    public User(String login) {
  76.       setLogin(login);
  77.    }

  78.    public User(String login, String password, String firstName,
  79.                String lastName, String email, boolean superUser) {
  80.       this(login, password, firstName, lastName, email,
  81.               UserUtilities.REGISTRATION_TYPE_ADMIN, superUser);
  82.    }

  83.    public User(String login, String password, String firstName,
  84.                String lastName, String email, int registrationType,
  85.                boolean superUser) {
  86.       this(login);
  87.       this.password = password;
  88.       this.firstName = firstName;
  89.       this.lastName = lastName;
  90.       this.email = email;
  91.       this.registrationType = registrationType;
  92.       setSuperUser(superUser);
  93.    }

  94.    public String getLogin() {
  95.       return login;
  96.    }

  97.    public void setLogin(String login) {
  98.       if (login == null) {
  99.          throw new IllegalArgumentException("null login");
  100.       }
  101.       this.login = login;
  102.    }

  103.    public String getPassword() {
  104.       return password;
  105.    }

  106.    public void setPassword(String value) {
  107.       this.password = value;
  108.    }

  109.    public String getFirstName() {
  110.       return firstName;
  111.    }

  112.    public void setFirstName(String value) {
  113.       firstName = value;
  114.    }

  115.    public String getLastName() {
  116.       return lastName;
  117.    }

  118.    public void setLastName(String value) {
  119.       this.lastName = value;
  120.    }

  121.    public String getFullName() {
  122.       return StringUtils.defaultString(
  123.               StringUtils.trimToNull(
  124.                       (StringUtils.isNotBlank(getFirstName()) ? getFirstName() : "")
  125.                               + " " + (StringUtils.isNotBlank(getLastName()) ? getLastName() : "")
  126.               ), getLogin());
  127.    }

  128.    public String getEmail() {
  129.       return email;
  130.    }

  131.    public InternetAddress getEmailAddress() {

  132.       if (null == getEmail() || getEmail().trim().length() == 0) {
  133.          log.warn("getEmailAddress: failed to get eMail for user "
  134.                  + getLogin() + " (" + getId() + ")");
  135.          return null;
  136.       }
  137.       final InternetAddress adr;
  138.       try {
  139.          adr =  new InternetAddress(getEmail());
  140.          try {
  141.             adr.setPersonal(getFullName(), "utf-8");
  142.          } catch (UnsupportedEncodingException e) {
  143.             log.warn("getEmailAddress: unsupported encoding for setting personal: utf-8", e);
  144.          }
  145.          return adr;
  146.       } catch (AddressException e1) {
  147.          log.error("getEmailAddress: failed to parse email '"
  148.                  + getEmail() + "' for user " + getLogin() + " ("
  149.                  + getId() + "), returning null", e1);
  150.          return null;
  151.       }

  152.    }

  153.    public void setEmail(String email) {
  154.       this.email = email;
  155.    }

  156.    public Set<Permission> getPermissions() {
  157.       return permissions;
  158.    }

  159.    public void setPermissions(Set<Permission> getPermissions) {
  160.       this.permissions = getPermissions;
  161.    }


  162.    public int getRegistrationType() {
  163.       return registrationType;
  164.    }

  165.    public void setRegistrationType(int registrationType) {
  166.       this.registrationType = registrationType;
  167.    }

  168.    public int getStatus() {
  169.       return status;
  170.    }

  171.    public void setStatus(int status) {
  172.       this.status = status;
  173.    }

  174.    public boolean isSuperUser() {
  175.       return superUser;
  176.    }

  177.    public void setSuperUser(boolean superUser) {
  178.       this.superUser = superUser;
  179.    }

  180.    public String getFirstInitial() {
  181.       return (null != getFirstName() && getFirstName().length() > 0 ? getFirstName().substring(0, 1)
  182.               .toUpperCase()
  183.               + "." : "");
  184.    }

  185.    public boolean hasRequiredData() {
  186.       return hasRequiredData(true);
  187.    }

  188.    public boolean hasRequiredData(boolean passwordRequired) {
  189.       if (this.getLogin() == null || this.getLogin().equals("")
  190.               || this.getFirstName() == null
  191.               || this.getFirstName().equals("") || this.getLastName() == null
  192.               || this.getLastName().equals("") || this.getEmail() == null
  193.               || this.getEmail().equals("")) {
  194.          return false;
  195.       }
  196.       if (passwordRequired
  197.               && (this.getPassword() == null || this.getPassword().equals(""))) {
  198.          return false;
  199.       }
  200.       return true;
  201.    }

  202.    public List<Project> getProjects() {
  203.       return projects;
  204.    }

  205.    public void setProjects(List<Project> projects) {
  206.       this.projects = projects;
  207.    }

  208.    @Override
  209.    public String toString() {
  210.       return new ToStringBuilder(this).append("id", getId())
  211.               .append("login", getLogin()).toString();
  212.    }

  213.    /**
  214.     * Compares 2 users by last and first name.
  215.     */
  216.    private static class NameComparator implements Comparator<User> {

  217.       public int compare(User a, User b) {
  218.          return new CompareToBuilder().append(a.getLastName(), b.getLastName())
  219.                  .append(a.getFirstName(), b.getFirstName()).append(a, b, LOGIN_COMPARATOR).toComparison();
  220.       }

  221.    }

  222.    public static final class LoginComparator implements Comparator<User> {
  223.       public int compare(User o1, User o2) {
  224.          return new CompareToBuilder().append(o1.getLogin(), o2.getLogin())
  225.                  .toComparison();
  226.       }
  227.    }

  228. }