gt:                 if (p.getId().equals(privilege)) {
context: /*
   * Copyright (C) 2010 - 2011 Interactive Media Management
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU 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 General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  package dk.i2m.converge.core.security;
  
  import dk.i2m.converge.core.Notification;
  import dk.i2m.converge.core.content.AssignmentType;
  import dk.i2m.converge.core.content.NewsItem;
  import dk.i2m.converge.core.content.catalogue.Catalogue;
  import dk.i2m.converge.core.newswire.NewswireBasket;
  import dk.i2m.converge.core.newswire.NewswireService;
  import dk.i2m.converge.core.utils.BeanComparator;
  import dk.i2m.converge.core.workflow.Outlet;
  import dk.i2m.converge.core.workflow.Section;
  import java.awt.ComponentOrientation;
  import java.io.Serializable;
  import java.util.*;
  import javax.persistence.*;
  
  /**
   * {@link UserAccount} with access to the system and relation news items,
   * outlets, photos, and so forth. The majority of the {@link UserAccount}
   * properties are not managed by the JPA persistence framework. Instead the are
   * retrieved from an LDAP directory upon fetching an account through the data
   * access object. The mapping of the LDAP user account to the
   * {@link UserAccount} object is defined in the application configuration.
   *
   * @author Allan Lykke Christensen
   */
  @Entity
  @Table(name = "user_account", uniqueConstraints = @UniqueConstraint(columnNames = {"username"}))
  @NamedQueries({
      @NamedQuery(name = UserAccount.FIND_BY_UID, query = "SELECT u FROM UserAccount u WHERE u.username=:username"),
      @NamedQuery(name = UserAccount.FIND_BY_USER_ROLE, query = "SELECT u FROM UserRole r JOIN r.userAccounts u WHERE r.name=:roleName"),
      @NamedQuery(name = UserAccount.FIND_USERS_WITH_PUBLICATIONS, query = "SELECT u FROM NewsItemActor n JOIN n.newsItem ni JOIN ni.placements p JOIN n.user u WHERE (n.role = :userRole AND p.edition.publicationDate >= :startDate AND p.edition.publicationDate <= :endDate) GROUP BY u"),
      @NamedQuery(name = UserAccount.FIND_ACTIVE_USERS_BY_ROLE, query = "SELECT DISTINCT u FROM NewsItem AS ni JOIN ni.actors AS a JOIN a.user AS u JOIN ni.history AS h WHERE (a.role = :userRole AND h.user = a.user AND h.timestamp >= :startDate AND h.timestamp <= :endDate AND h.submitted = true) ORDER BY a.user.username DESC"),
      @NamedQuery(name = UserAccount.FIND_PASSIVE_USERS_BY_ROLE, query = "SELECT DISTINCT u FROM NewsItem AS ni JOIN ni.actors AS a JOIN a.user AS u JOIN ni.history AS h WHERE (a.role = :userRole AND h.timestamp >= :startDate AND h.timestamp <= :endDate AND h.submitted = true) ORDER BY a.user.username DESC")
  })
  public class UserAccount implements Serializable {
      
      /** 
       * System account used for automated actions.
       * @since 1.1.14
       */
      public static final String SYSTEM_ACCOUNT = "converge";
  
      private static final long serialVersionUID = 2L;
  
      /** Query for finding a user account by its unique user identifier. */
      public static final String FIND_BY_UID = "UserAccount.findByUid";
  
      /** Query for finding a user accounts who are members of a given role. */
      public static final String FIND_BY_USER_ROLE = "UserAccount.findByUserRole";
  
      /** Query for generating the activity report. */
      public static final String FIND_USERS_WITH_PUBLICATIONS = "UserAccount.findUsersWithPublications";
  
      /** Query for generating the activity report for active users only. */
      public static final String FIND_ACTIVE_USERS_BY_ROLE = "UserAccount.findActiveUsersByRole";
      
      /** Query for generating the activity report - for passive and active users. */
      public static final String FIND_PASSIVE_USERS_BY_ROLE = "UserAccount.findPassiveUsersByRole";
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id")
      private Long id;
  
      @Column(name = "username", length = 255)
      private String username = "";
  
      @Column(name = "time_zone")
      private String timeZone;
  
      @Column(name = "user_status", length = 255)
      private String status = "";
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "default_outlet")
      private Outlet defaultOutlet;
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "default_section")
      private Section defaultSection;
  
      @Enumerated(EnumType.STRING)
      @Column(name = "default_assignment_type")
      private AssignmentType defaultAssignmentType;
  
      @ManyToOne
      @JoinColumn(name = "default_media_repository")
      private Catalogue defaultMediaRepository;
  
      @Column(name = "default_add_next_edition")
      private boolean defaultAddNextEdition;
  
      @Column(name = "default_search_engine_tags")
      private boolean defaultSearchEngineTags;
  
      @Column(name = "default_search_results_order_by")
      private String defaultSearchResultsSortBy;
  
      @Column(name = "default_search_results_order")
      private boolean defaultSearchResultsOrder;
      
      @Column(name = "default_work_day")
      private int defaultWorkDay;
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "clearance_level")
      private ClearanceLevel clearanceLevel;
  
      @ManyToMany(fetch = FetchType.LAZY)
      @JoinTable(name = "user_account_newswire_services",
      joinColumns = {@JoinColumn(referencedColumnName = "id", name = "user_account_id", nullable = false)},
      inverseJoinColumns = {@JoinColumn(referencedColumnName = "id", name = "newswire_service_id", nullable = false)})
      private List<NewswireService> newswireServices = new ArrayList<NewswireService>();
  
      @ManyToMany(mappedBy = "userAccounts")
      private List<UserRole> userRoles = new ArrayList<UserRole>();
  
      @Column(name = "dn") @Lob
      private String distinguishedName = "";
  
      @Column(name = "lang")
      private String preferredLanguage = "";
  
      @Enumerated(EnumType.STRING)
      @Column(name = "employment_type")
      private EmploymentType employmentType;
  
      @Enumerated(EnumType.STRING)
      @Column(name = "fee_type")
      private FeeType feeType;
  
      @Column(name = "full_name")
      private String fullName = "";
  
      @Column(name = "given_name")
      private String givenName = "";
  
      @Column(name = "surname")
      private String surname = "";
  
      @Column(name = "job_title")
      private String jobTitle = "";
  
      @Column(name = "organisation") @Lob
      private String organisation = "";
  
      @Column(name = "email") @Lob
      private String email = "";
  
      @Column(name = "phone")
      private String phone = "";
  
      @Column(name = "mobile")
      private String mobile = "";
  
      @OneToMany(mappedBy = "recipient", fetch = FetchType.LAZY)
      private List<Notification> notifications;
  
      @javax.persistence.Version
      @Column(name = "opt_lock")
      private int versionIdentifier;
  
      @OneToMany(mappedBy = "checkedOutBy", fetch = FetchType.LAZY)
      private List<NewsItem> checkedOut;
  
      @OneToMany(mappedBy = "owner", fetch = FetchType.LAZY)
      private List<NewswireBasket> newswireBaskets = new ArrayList<NewswireBasket>();
  
      /**
       * Creates a new instance of user.
       */
      public UserAccount() {
      }
  
      /**
       * Creates a new instance of {@link UserAccount}.
       * 
       * @param uid
       *      Username of the user
       */
      public UserAccount(String uid) {
          this.username = uid;
      }
  
      /**
       * Gets the unique identifier of the user. The unique identifier is automatically generated
       * upon the creation of the user.
       *
       * @return Unique identifier of the user
       */
      public Long getId() {
          return id;
      }
  
      /**
       * Sets the unique identifier of the user.
       *
       * @param id
       *          Unique identifier of the user
       */
      public void setId(Long id) {
          this.id = id;
      }
  
      public ClearanceLevel getClearanceLevel() {
          return clearanceLevel;
      }
  
      public void setClearanceLevel(ClearanceLevel clearanceLevel) {
          this.clearanceLevel = clearanceLevel;
      }
  
      /**
       * Gets the distinguished name of the user in the directory service.
       *
       * @return Distinguished name of the user in the directory service
       */
      public String getDistinguishedName() {
          return distinguishedName;
      }
  
      /**
       * Sets the distinguished name of the user in the directory service.
       *
       * @param distinguishedName
       *          Distinguished name of the user in the directory service
       */
      public void setDistinguishedName(String distinguishedName) {
          this.distinguishedName = distinguishedName;
      }
  
      /**
       * Gets the username of this {@link UserAccount}.
       * 
       * @return Username of this {@link UserAccount}.
       */
      public String getUsername() {
          return username;
      }
  
      /**
       * Sets the username of this {@link UserAccount}.
       * 
       * @param username Username of this {@link UserAccount}.
       */
      public void setUsername(String username) {
          this.username = username;
      }
  
      /**
       * Gets the full or common name of this {@link UserAccount}. This property is
       * supplied by the LDAP directory.
       * 
       * @return Full or common name of this {@link UserAccount}
       */
      public String getFullName() {
          return fullName;
      }
  
      /**
       * Sets the full or common name of this {@link UserAccount}.
       *
       * @param fullName
       *          Full or common name of this {@link UserAccount}
       */
      public void setFullName(String fullName) {
          this.fullName = fullName;
      }
  
      /**
       * Gets the given name of the person owning the {@link UserAccount}.
       *
       * @return Given name of the person owning the {@link UserAccount}
       */
      public String getGivenName() {
          return givenName;
      }
  
      /**
       * Sets the given name of the person owning the {@link UserAccount}.
       *
       * @param givenName
       *          Given name of the person owning the {@link UserAccount}
       */
      public void setGivenName(String givenName) {
          this.givenName = givenName;
      }
  
      /**
       * Gets the surname of the person owning the {@link UserAccount}.
       *
       * @return Surname of the person owning the {@link UserAccount}
       */
      public String getSurname() {
          return surname;
      }
  
      /**
       * Sets the surname of the person owning the {@link UserAccount}.
       *
       * @param surname
       *          Surname of the person owning the {@link UserAccount}
       */
      public void setSurname(String surname) {
          this.surname = surname;
      }
  
      public Outlet getDefaultOutlet() {
          return defaultOutlet;
      }
  
      public void setDefaultOutlet(Outlet defaultOutlet) {
          this.defaultOutlet = defaultOutlet;
      }
  
      public Section getDefaultSection() {
          return defaultSection;
      }
  
      public void setDefaultSection(Section defaultSection) {
          this.defaultSection = defaultSection;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getPhone() {
          return phone;
      }
  
      public void setPhone(String phone) {
          this.phone = phone;
      }
  
      public String getMobile() {
          return mobile;
      }
  
      public void setMobile(String mobile) {
          this.mobile = mobile;
      }
  
      public String getJobTitle() {
          return jobTitle;
      }
  
      public void setJobTitle(String jobTitle) {
          this.jobTitle = jobTitle;
      }
  
      public String getOrganisation() {
          return organisation;
      }
  
      public void setOrganisation(String organisation) {
          this.organisation = organisation;
      }
  
      /**
       * Gets the type of employment of the user.
       *
       * @return Employment type of the user
       */
      public EmploymentType getEmploymentType() {
          return employmentType;
      }
  
      /**
       * Sets the employment type of the user.
       *
       * @param employmentType
       *          Employment type of the user
       */
      public void setEmploymentType(EmploymentType employmentType) {
          this.employmentType = employmentType;
      }
  
      /**
       * Gets the type of fee paid to the user.
       *
       * @return Type of fee paid to the user
       */
      public FeeType getFeeType() {
          return feeType;
      }
  
      /**
       * Sets the type of fee paid to the user.
       *
       * @param feeType
       *          Type of fee paid to the user
       */
      public void setFeeType(FeeType feeType) {
          this.feeType = feeType;
      }
  
      /**
       * Gets the {@link TimeZone} of this {@link UserAccount}. Dates and times should be
       * adjusted to given {@link TimeZone} for this {@link UserAccount}.
       *
       * @return {@link TimeZone} of the {@link UserAccount}
       */
      public TimeZone getTimeZone() {
          if (this.timeZone == null) {
              return TimeZone.getDefault();
          } else {
              try {
                  return TimeZone.getTimeZone(this.timeZone);
              } catch (Exception ex) {
                  return TimeZone.getDefault();
              }
          }
      }
  
      /**
       * Sets the {@link TimeZone} of this {@link UserAccount}.
       *
       * @param timeZone
       *          {@link TimeZone} of this {@link UserAccount}
       */
      public void setTimeZone(TimeZone timeZone) {
          this.timeZone = timeZone.getID();
      }
  
      /**
       * Sets the {@link TimeZone} of this {@link UserAccount}.
       *
       * @param timeZoneId
       *          {@link String} identifier of the {@link TimeZone}
       */
      public void setTimeZoneAsString(String timeZoneId) {
          this.timeZone = timeZoneId;
      }
  
      /**
       * Gets the {@link TimeZone} as a {@link String} of this {@link UserAccount}.
       *
       * @return {@link String} identifier of the {@link TimeZone}
       */
      public String getTimeZoneAsString() {
          return this.timeZone;
      }
  
      /**
       * Gets the current status of the user. The status is free text set by the
       * user.
       * 
       * @return Current status of the user
       */
      public String getStatus() {
          return status;
      }
  
      /**
       * Sets the current status of the user.
       *
       * @param status
       *          Current status of the user
       */
      public void setStatus(String status) {
          this.status = status;
      }
  
      /**
       * Gets a {@link List} of the {@link UserRole}s where the user is a member.
       *
       * @return {@link List} of the {@link UserRole}s where the user is a member
       */
      public List<UserRole> getUserRoles() {
          return userRoles;
      }
  
      /**
       * Sets a {@link List} of the {@link UserRole}s where the user is a member.
       *
       * @param userRoles
       *          {@link List} of the {@link UserRole}s where the user is a member
       */
      public void setUserRoles(List<UserRole> userRoles) {
          this.userRoles = userRoles;
      }
  
      /**
       * Determines if the {@link UserAccount} has a given privilege.
       *
       * @param privilege
       *          Privilege to determine if the {@link UserAccount} has
       * @return <code>true</code> if the {@link UserAccount} has the given
       *         privilege, otherwise <code>false</code>
       */
      public boolean isPrivileged(SystemPrivilege privilege) {
          for (UserRole userRole : this.userRoles) {
              for (Privilege p : userRole.getPrivileges()) {
  