View Javadoc

1   /**
2    * Copyright (c) 2009-2010, Elias Gomes. All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5    * following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8    * disclaimer.
9    * 
10   * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided with the distribution.
12   * 
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
14   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
16   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
18   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20   */
21  package net.sourceforge.mystique.domain.entity;
22  
23  import java.util.Date;
24  import java.util.LinkedList;
25  import java.util.List;
26  
27  import javax.persistence.Column;
28  import javax.persistence.Entity;
29  import javax.persistence.GeneratedValue;
30  import javax.persistence.GenerationType;
31  import javax.persistence.Id;
32  import javax.persistence.JoinColumn;
33  import javax.persistence.JoinTable;
34  import javax.persistence.ManyToMany;
35  import javax.persistence.NamedQueries;
36  import javax.persistence.NamedQuery;
37  import javax.persistence.OneToMany;
38  import javax.persistence.SequenceGenerator;
39  import javax.persistence.Table;
40  import javax.persistence.Temporal;
41  import javax.persistence.TemporalType;
42  import javax.persistence.Transient;
43  
44  /**
45   * @author Elias Gomes [eliasgomes@users.sourceforge.net]
46   */
47  @Entity
48  @Table(name = "TB_USER")
49  @SequenceGenerator(name = "userIdSeq", sequenceName = "USER_ID_SEQ", initialValue = 1, allocationSize = 1)
50  @NamedQueries( { @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u ORDER BY u.name"),
51  		@NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE UPPER(u.email) = UPPER(:email)") })
52  public class User {
53  
54  	@Id
55  	@GeneratedValue(generator = "userIdSeq", strategy = GenerationType.SEQUENCE)
56  	@Column(name = "ID")
57  	private Long id;
58  
59  	@Column(name = "NAME", nullable = false)
60  	private String name;
61  
62  	@Column(name = "EMAIL", nullable = false, unique = true)
63  	private String email;
64  
65  	@Column(name = "PASSWORD", nullable = false)
66  	private String password;
67  
68  	@Temporal(TemporalType.TIMESTAMP)
69  	@Column(name = "CREATED", nullable = false)
70  	private Date created;
71  
72  	@OneToMany(mappedBy = "reporter")
73  	private List<Ticket> ticketsReported;
74  
75  	@ManyToMany(mappedBy = "usersAssignedTo")
76  	private List<Ticket> ticketsAssigned;
77  
78  	@ManyToMany
79  	@JoinTable(name = "TB_USER_HAS_TICKET_TO_APPROVE", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "TICKET_ID"))
80  	private List<Ticket> ticketsToApprove;
81  
82  	public User() {
83  		setCreated(new Date());
84  		setTicketsReported(new LinkedList<Ticket>());
85  		setTicketsAssigned(new LinkedList<Ticket>());
86  		setTicketsToApprove(new LinkedList<Ticket>());
87  	}
88  
89  	@Transient
90  	public boolean isNew() {
91  		return (id == null);
92  	}
93  
94  	public Long getId() {
95  		return id;
96  	}
97  
98  	public void setId(Long id) {
99  		this.id = id;
100 	}
101 
102 	public String getName() {
103 		return name;
104 	}
105 
106 	public void setName(String name) {
107 		if (name != null) {
108 			this.name = name.trim();
109 		}
110 		this.name = name;
111 	}
112 
113 	public String getEmail() {
114 		return email;
115 	}
116 
117 	public void setEmail(String email) {
118 		if (email != null) {
119 			this.email = email.trim();
120 		}
121 		this.email = email;
122 	}
123 
124 	public String getPassword() {
125 		return password;
126 	}
127 
128 	public void setPassword(String password) {
129 		if (password != null) {
130 			this.password = password.trim();
131 		}
132 		this.password = password;
133 	}
134 
135 	public Date getCreated() {
136 		return created;
137 	}
138 
139 	public void setCreated(Date created) {
140 		this.created = created;
141 	}
142 
143 	public List<Ticket> getTicketsReported() {
144 		return ticketsReported;
145 	}
146 
147 	public void setTicketsReported(List<Ticket> ticketsReported) {
148 		this.ticketsReported = ticketsReported;
149 	}
150 
151 	public List<Ticket> getTicketsAssigned() {
152 		return ticketsAssigned;
153 	}
154 
155 	public void setTicketsAssigned(List<Ticket> ticketsAssigned) {
156 		this.ticketsAssigned = ticketsAssigned;
157 	}
158 
159 	public List<Ticket> getTicketsToApprove() {
160 		return ticketsToApprove;
161 	}
162 
163 	public void setTicketsToApprove(List<Ticket> ticketsToApprove) {
164 		this.ticketsToApprove = ticketsToApprove;
165 	}
166 
167 	@Override
168 	public int hashCode() {
169 		final int prime = 31;
170 		int result = 1;
171 		result = prime * result + ((id == null) ? 0 : id.hashCode());
172 		return result;
173 	}
174 
175 	@Override
176 	public boolean equals(Object obj) {
177 		if (this == obj)
178 			return true;
179 		if (obj == null)
180 			return false;
181 		if (getClass() != obj.getClass())
182 			return false;
183 		User other = (User) obj;
184 		if (id == null) {
185 			if (other.id != null)
186 				return false;
187 		} else if (!id.equals(other.id))
188 			return false;
189 		return true;
190 	}
191 
192 }