/* Copyright 2006 - 2010 Under Dusken Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package no.dusken.aranea.model; import org.compass.annotations.Searchable; import org.compass.annotations.SearchableConstant; import org.compass.annotations.SearchableProperty; import javax.persistence.*; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import java.util.Collection; import java.util.Date; import java.util.LinkedList; import static javax.persistence.FetchType.LAZY; /** * Represents a person or user. * * @author Erlend Hamnaberg<erlenha@underdusken.no> * @author Asbjørn L. Johansen<asbjorjo@underdusken.no> * @author Marvin B. Lillehaug<lillehau@underdusken.no> * @link http://penrose.org (NOT YET) */ @Entity @Table(name = "person", uniqueConstraints = {@UniqueConstraint(columnNames = "username")}) @SequenceGenerator(name = "person_seq", sequenceName = "person_id_seq") @NamedQueries({ @NamedQuery(name = "person_byname", query = "select p from Person p where p.surname = :surname and p.firstname = :firstname"), @NamedQuery(name = "person_byrole", query = "select p from Person p where :role member of p.roles"), @NamedQuery(name = "person_byusername", query = "select p from Person p where lower(p.username) = :username"), @NamedQuery(name = "person_persons", query = "select p from Person p order by p.surname"), @NamedQuery(name = "person_personsbyfirstname", query = "select p from Person p order by p.firstname"), @NamedQuery(name = "person_personsbyfirstnameandactive", query = "select p from Person p where p.active = :active order by p.firstname"), @NamedQuery(name = "person_by_active", query = "select p from Person p where p.active = :active order by p.surname"), @NamedQuery(name = "photographers_bypage", query = "select distinct p from Person p, " + "Image i, PageImage pi " + "where pi.page = :page " + "and pi.image = i " + "and i.photographer = p " + "and i.illustration = false " + "order by p.surname"), @NamedQuery(name = "person_byroleandstatus", query = "select distinct p from Person p " + "where :role member of p.roles and p.active = :active order by p.surname"), @NamedQuery(name = "drawers_bypage", query = "select distinct p from Person p, " + "Image i, PageImage pi " + "where pi.page = :page " + "and pi.image = i " + "and i.photographer = p " + "and i.illustration = true " + "order by p.surname")}) @Inheritance(strategy = InheritanceType.JOINED) @Searchable @SearchableConstant(name = "type", values = {"person"}) public class Person extends AraneaObject { /** * the person's first name */ @Column(nullable = false, length = 100) @SearchableProperty @Size(max = 100, min = 2) @NotNull private String firstname; /** * the person's surname */ @Column(nullable = false, length = 100) @SearchableProperty @Size(max = 100, min = 2) @NotNull private String surname; /** * the person's username */ @Column(nullable = false, length = 20) @SearchableProperty @Size(max = 20, min = 2) @NotNull private String username; /** * The date the person was born */ @Temporal(TemporalType.TIMESTAMP) private Date birthdate; /* * is the person currently active? */ @Column(nullable = false) private Boolean active = false; /* * Image taken of the person (ie portrait) */ @ManyToOne private Image portrait; @ManyToMany(fetch = LAZY) @JoinTable(name = "person_role") @OrderBy("ordering") private Collection<Role> roles = new LinkedList<Role>(); @ManyToMany(mappedBy = "authors", fetch = LAZY) private Collection<Page> pages = new LinkedList<Page>(); @OneToMany(cascade = CascadeType.ALL) private Collection<Image> images = new LinkedList<Image>(); @Column private String phoneNumber; @Column(nullable = false) private Boolean showPhoneNumber = false; @Column(nullable = false) private boolean showPortrait = false; /** * the user's email address */ @Column @Size(max = 255) @NotNull private String emailAddress; /** * Checks if the user has the given role * * @param role The role to check for. Must match the name of the role * @return true if the user has that role */ public boolean hasRole(String role) { if (role == null || role.equalsIgnoreCase("") || roles == null || roles.isEmpty()) { return false; } for (Role r : roles) { if (r.getName().equalsIgnoreCase(role)) { return true; } } return false; } @PreRemove public void preRemove(){ for(Page p: pages){ p.getAuthors().remove(this); } pages = null; } public String toString() { return getName(); } public String getName() { return new StringBuilder(firstname).append(" ").append(surname).toString(); } public Collection<Page> getPages() { return pages; } public void setPages(Collection<Page> pages) { this.pages = pages; } public Date getBirthdate() { return birthdate; } public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } public Collection<Image> getImages() { return images; } public void setImages(Collection<Image> images) { this.images = images; } public Boolean getActive() { return active; } public void setActive(Boolean active) { this.active = active; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Collection<Role> getRoles() { return roles; } public void setRoles(Collection<Role> roles) { this.roles = roles; } public Image getPortrait() { return portrait; } public void setPortrait(Image portrait) { this.portrait = portrait; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public boolean getShowPhoneNumber() { return showPhoneNumber; } public void setShowPhoneNumber(boolean showPhoneNumber) { this.showPhoneNumber = showPhoneNumber; } public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } public boolean getShowPortrait() { return showPortrait; } public void setShowPortrait(boolean showPortrait) { this.showPortrait = showPortrait; } }