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.io.BufferedInputStream;
24  import java.io.BufferedOutputStream;
25  import java.io.FileInputStream;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.OutputStream;
30  import java.util.Date;
31  
32  import javax.persistence.Column;
33  import javax.persistence.Entity;
34  import javax.persistence.GeneratedValue;
35  import javax.persistence.GenerationType;
36  import javax.persistence.Id;
37  import javax.persistence.JoinColumn;
38  import javax.persistence.ManyToOne;
39  import javax.persistence.NamedQueries;
40  import javax.persistence.NamedQuery;
41  import javax.persistence.SequenceGenerator;
42  import javax.persistence.Table;
43  import javax.persistence.Temporal;
44  import javax.persistence.TemporalType;
45  import javax.persistence.Transient;
46  
47  /**
48   * @author Elias Gomes [eliasgomes@users.sourceforge.net]
49   */
50  @Entity
51  @Table(name = "TB_ATTACHMENT")
52  @SequenceGenerator(name = "attachmentIdSeq", sequenceName = "ATTACHMENT_ID_SEQ", initialValue = 1, allocationSize = 1)
53  @NamedQueries(@NamedQuery(name = "Attachment.findByTicket", query = "SELECT a FROM Attachment a WHERE a.ticket = :ticket ORDER BY a.created"))
54  public class Attachment {
55  
56  	@Id
57  	@GeneratedValue(generator = "attachmentIdSeq", strategy = GenerationType.SEQUENCE)
58  	@Column(name = "ID")
59  	private Long id;
60  
61  	@Column(name = "NAME", nullable = false)
62  	private String name;
63  
64  	@Column(name = "DESCRIPTION")
65  	private String description;
66  
67  	@Column(name = "MIME_TYPE", nullable = false)
68  	private String mimeType;
69  
70  	@Column(name = "LENGTH", nullable = false)
71  	private long length;
72  
73  	@Temporal(TemporalType.TIMESTAMP)
74  	@Column(name = "CREATED", nullable = false)
75  	private Date created;
76  
77  	@ManyToOne
78  	@JoinColumn(name = "OWNER_USER_ID", nullable = false)
79  	private User owner;
80  
81  	@ManyToOne
82  	@JoinColumn(name = "TICKET_ID", nullable = false)
83  	private Ticket ticket;
84  
85  	public Attachment() {
86  		setCreated(new Date());
87  	}
88  
89  	@Transient
90  	public boolean isNew() {
91  		return (id == null);
92  	}
93  
94  	public void storeStream(String path, InputStream inputStream) throws IOException {
95  
96  		String fs = System.getProperty("file.separator");
97  		String fileName = path + fs + id;
98  
99  		BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(fileName));
100 
101 		int read = inputStream.read();
102 		while (read != -1) {
103 			outputStream.write(read);
104 			read = inputStream.read();
105 		}
106 
107 		outputStream.close();
108 
109 	}
110 
111 	public void loadStream(String path, OutputStream outputStream) throws IOException {
112 
113 		String fs = System.getProperty("file.separator");
114 		String fileName = path + fs + id;
115 
116 		BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(fileName));
117 
118 		int read = inputStream.read();
119 		while (read != -1) {
120 			outputStream.write(read);
121 			read = inputStream.read();
122 		}
123 
124 		inputStream.close();
125 
126 	}
127 
128 	public Long getId() {
129 		return id;
130 	}
131 
132 	public void setId(Long id) {
133 		this.id = id;
134 	}
135 
136 	public String getName() {
137 		return name;
138 	}
139 
140 	public void setName(String name) {
141 		if (name != null) {
142 			this.name = name.trim();
143 		}
144 		this.name = name;
145 	}
146 
147 	public String getDescription() {
148 		return description;
149 	}
150 
151 	public void setDescription(String description) {
152 		if (description != null) {
153 			this.description = description.trim();
154 		}
155 		this.description = description;
156 	}
157 
158 	public String getMimeType() {
159 		return mimeType;
160 	}
161 
162 	public void setMimeType(String mimeType) {
163 		if (mimeType != null) {
164 			this.mimeType = mimeType.trim();
165 		}
166 		this.mimeType = mimeType;
167 	}
168 
169 	public long getLength() {
170 		return length;
171 	}
172 
173 	public void setLength(long length) {
174 		this.length = length;
175 	}
176 
177 	public Date getCreated() {
178 		return created;
179 	}
180 
181 	public void setCreated(Date created) {
182 		this.created = created;
183 	}
184 
185 	public User getOwner() {
186 		return owner;
187 	}
188 
189 	public void setOwner(User owner) {
190 		this.owner = owner;
191 	}
192 
193 	public Ticket getTicket() {
194 		return ticket;
195 	}
196 
197 	public void setTicket(Ticket ticket) {
198 		this.ticket = ticket;
199 	}
200 
201 	@Override
202 	public int hashCode() {
203 		final int prime = 31;
204 		int result = 1;
205 		result = prime * result + ((id == null) ? 0 : id.hashCode());
206 		return result;
207 	}
208 
209 	@Override
210 	public boolean equals(Object obj) {
211 		if (this == obj)
212 			return true;
213 		if (obj == null)
214 			return false;
215 		if (getClass() != obj.getClass())
216 			return false;
217 		Attachment other = (Attachment) obj;
218 		if (id == null) {
219 			if (other.id != null)
220 				return false;
221 		} else if (!id.equals(other.id))
222 			return false;
223 		return true;
224 	}
225 
226 }