source: VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/model/User.java @ 5540

Last change on this file since 5540 was 5540, checked in by Twan Goosen, 10 years ago

Enabled 'edit' option with confirmation dialogue for published collections (works for private collections and owner) and allowed access to edit view for non-private
Refs #600

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1package eu.clarin.cmdi.virtualcollectionregistry.model;
2
3import java.io.Serializable;
4import java.security.Principal;
5import java.util.LinkedHashSet;
6import java.util.Set;
7
8import javax.persistence.CascadeType;
9import javax.persistence.Column;
10import javax.persistence.Entity;
11import javax.persistence.FetchType;
12import javax.persistence.GeneratedValue;
13import javax.persistence.GenerationType;
14import javax.persistence.Id;
15import javax.persistence.NamedQueries;
16import javax.persistence.NamedQuery;
17import javax.persistence.OneToMany;
18import javax.persistence.Table;
19
20import org.apache.commons.lang.builder.EqualsBuilder;
21import org.apache.commons.lang.builder.HashCodeBuilder;
22
23@Entity
24@Table(name = "user")
25@NamedQueries({
26    @NamedQuery(name = "User.findByName",
27                query = "SELECT u FROM User u WHERE u.name = :name")
28})
29public class User implements Serializable, IdentifiedEntity {
30    private static final long serialVersionUID = 1L;
31
32    @Id
33    @GeneratedValue(strategy = GenerationType.AUTO)
34    @Column(name = "id")
35    private Long id;
36
37    @Column(name = "name", nullable = false, unique = true, length = 255)
38    private String name;
39
40    @Column(name = "display_name", length = 255)
41    private String displayName;
42
43    @OneToMany(cascade = CascadeType.ALL,
44               fetch = FetchType.LAZY,
45               mappedBy = "owner",
46               orphanRemoval = true)
47    private Set<VirtualCollection> collections =
48        new LinkedHashSet<VirtualCollection>();
49
50
51    protected User() {
52    }
53
54    public User(String name, String displayName) {
55        super();
56        this.setName(name);
57        this.setDisplayName(displayName);
58    }
59
60    public User(String name) {
61        this(name, null);
62    }
63
64    public Long getId() {
65        return id;
66    }
67
68    public String getName() {
69        return name;
70    }
71
72    public void setName(String name) {
73        if (name == null) {
74            throw new NullPointerException("name == null");
75        }
76        name = name.trim();
77        if (name.isEmpty()) {
78            throw new IllegalArgumentException("name is empty");
79        }
80        this.name = name;
81    }
82
83    public String getDisplayName() {
84        return displayName;
85    }
86
87    public void setDisplayName(String displayName) {
88        if (displayName != null) {
89            displayName = displayName.trim();
90            if (displayName.isEmpty()) {
91                displayName = null;
92            }
93        }
94        this.displayName = displayName;
95    }
96
97    public Set<VirtualCollection> getVirtualCollections() {
98        return collections;
99    }
100
101    public boolean equalsPrincipal(Principal principal) {
102        if (principal == null) {
103            throw new NullPointerException("principal == null");
104    }
105        return name.equals(principal.getName());
106    }
107
108    @Override
109    public boolean equals(Object obj) {
110        if (obj == null) {
111            return false;
112        }
113        if (obj == this) {
114            return true;
115        }
116        if (obj instanceof User) {
117            final User rhs = (User) obj;
118            return new EqualsBuilder()
119                .append(this.getName(), rhs.getName())
120                .isEquals();
121        }
122        return false;
123    }
124
125    @Override
126    public int hashCode() {
127        return new HashCodeBuilder(22391, 295)
128            .append(this.getName())
129            .toHashCode();
130    }
131
132} // class User
Note: See TracBrowser for help on using the repository browser.