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.LinkedList;
24  import java.util.List;
25  
26  import javax.persistence.CascadeType;
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.NamedQueries;
33  import javax.persistence.NamedQuery;
34  import javax.persistence.OneToMany;
35  import javax.persistence.SequenceGenerator;
36  import javax.persistence.Table;
37  import javax.persistence.Transient;
38  
39  /**
40   * @author Elias Gomes [eliasgomes@users.sourceforge.net]
41   */
42  @Entity
43  @Table(name = "TB_PROJECT")
44  @SequenceGenerator(name = "projectIdSeq", sequenceName = "PROJECT_ID_SEQ", initialValue = 1, allocationSize = 1)
45  @NamedQueries( { @NamedQuery(name = "Project.findAll", query = "SELECT p FROM Project p ORDER BY p.name"),
46  		@NamedQuery(name = "Project.findByName", query = "SELECT p FROM Project p WHERE UPPER(p.name) = UPPER(:name)") })
47  // TODO: Project needs leaders
48  // TODO: Project needs developers
49  // TODO: Project needs description
50  public class Project {
51  
52  	@Id
53  	@GeneratedValue(generator = "projectIdSeq", strategy = GenerationType.SEQUENCE)
54  	@Column(name = "ID")
55  	private Long id;
56  
57  	@Column(name = "NAME", nullable = false, unique = true)
58  	private String name;
59  
60  	@OneToMany(mappedBy = "project")
61  	private List<Ticket> ticketsReported;
62  
63  	@OneToMany(mappedBy = "project", cascade = CascadeType.ALL)
64  	private List<Component> components;
65  
66  	@OneToMany(mappedBy = "project", cascade = CascadeType.ALL)
67  	private List<Milestone> milestones;
68  
69  	@OneToMany(mappedBy = "project", cascade = CascadeType.ALL)
70  	private List<Version> versions;
71  
72  	public Project() {
73  		setTicketsReported(new LinkedList<Ticket>());
74  		setComponents(new LinkedList<Component>());
75  		setMilestones(new LinkedList<Milestone>());
76  		setVersions(new LinkedList<Version>());
77  	}
78  
79  	@Transient
80  	public boolean isNew() {
81  		return (id == null);
82  	}
83  
84  	public void addComponent(Component component) {
85  		component.setProject(this);
86  		for (Component c : getComponents()) {
87  			if (c.getName().equalsIgnoreCase(component.getName())) {
88  				return;
89  			}
90  		}
91  		getComponents().add(component);
92  	}
93  
94  	public void addMilestone(Milestone milestone) {
95  		milestone.setProject(this);
96  		for (Milestone m : getMilestones()) {
97  			if (m.getName().equalsIgnoreCase(milestone.getName())) {
98  				return;
99  			}
100 		}
101 		getMilestones().add(milestone);
102 	}
103 
104 	public void addVersion(Version version) {
105 		version.setProject(this);
106 		for (Version v : getVersions()) {
107 			if (v.getNumber().equalsIgnoreCase(version.getNumber())) {
108 				return;
109 			}
110 		}
111 		getVersions().add(version);
112 	}
113 
114 	public Long getId() {
115 		return id;
116 	}
117 
118 	public void setId(Long id) {
119 		this.id = id;
120 	}
121 
122 	public String getName() {
123 		return name;
124 	}
125 
126 	public void setName(String name) {
127 		if (name != null) {
128 			this.name = name.trim();
129 		}
130 		this.name = name;
131 	}
132 
133 	public List<Ticket> getTicketsReported() {
134 		return ticketsReported;
135 	}
136 
137 	public void setTicketsReported(List<Ticket> ticketsReported) {
138 		this.ticketsReported = ticketsReported;
139 	}
140 
141 	public List<Component> getComponents() {
142 		return components;
143 	}
144 
145 	public void setComponents(List<Component> components) {
146 		this.components = components;
147 	}
148 
149 	public List<Milestone> getMilestones() {
150 		return milestones;
151 	}
152 
153 	public void setMilestones(List<Milestone> milestones) {
154 		this.milestones = milestones;
155 	}
156 
157 	public List<Version> getVersions() {
158 		return versions;
159 	}
160 
161 	public void setVersions(List<Version> versions) {
162 		this.versions = versions;
163 	}
164 
165 	@Override
166 	public int hashCode() {
167 		final int prime = 31;
168 		int result = 1;
169 		result = prime * result + ((id == null) ? 0 : id.hashCode());
170 		return result;
171 	}
172 
173 	@Override
174 	public boolean equals(Object obj) {
175 		if (this == obj)
176 			return true;
177 		if (obj == null)
178 			return false;
179 		if (getClass() != obj.getClass())
180 			return false;
181 		Project other = (Project) obj;
182 		if (id == null) {
183 			if (other.id != null)
184 				return false;
185 		} else if (!id.equals(other.id))
186 			return false;
187 		return true;
188 	}
189 
190 }