1 | package eu.clarin.cmdi.virtualcollectionregistry.model; |
---|
2 | |
---|
3 | import java.io.Serializable; |
---|
4 | import java.security.Principal; |
---|
5 | import java.util.LinkedHashSet; |
---|
6 | import java.util.Set; |
---|
7 | |
---|
8 | import javax.persistence.CascadeType; |
---|
9 | import javax.persistence.Column; |
---|
10 | import javax.persistence.Entity; |
---|
11 | import javax.persistence.FetchType; |
---|
12 | import javax.persistence.GeneratedValue; |
---|
13 | import javax.persistence.GenerationType; |
---|
14 | import javax.persistence.Id; |
---|
15 | import javax.persistence.NamedQueries; |
---|
16 | import javax.persistence.NamedQuery; |
---|
17 | import javax.persistence.OneToMany; |
---|
18 | import javax.persistence.Table; |
---|
19 | |
---|
20 | import org.apache.commons.lang.builder.EqualsBuilder; |
---|
21 | import 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 | }) |
---|
29 | public 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 |
---|