View Javadoc

1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.model;
20  
21  import java.io.Serializable;
22  import java.util.Comparator;
23  
24  import org.apache.commons.lang.builder.CompareToBuilder;
25  import org.apache.commons.lang.builder.ToStringBuilder;
26  
27  /**
28   * A file attachment to an Issue.
29   * 
30   * <p>
31   * An IssueAttachment can only belong to 1 Issue (composition).
32   * </p>
33   * 
34   * @author ready
35   */
36  public class IssueAttachment extends AbstractEntity implements
37  		Comparable<Entity> {
38  
39  	/**
40  	 * 
41  	 */
42  	private static final long serialVersionUID = 1L;
43  	/** Compares 2 attachments by file size. */
44  	public static final Comparator<IssueAttachment> SIZE_COMPARATOR = new SizeComparator();
45  	/** Compares 2 attachments by original filename. */
46  	public static final Comparator<IssueAttachment> ORIGIINAL_FILENAME_COMPARATOR = new OriginalFilenameComparator();
47  
48  	/** The issue to which the file is attached. */
49  	private Issue issue;
50  
51  	/** The file name used to upload the attachment. */
52  	private String originalFileName;
53  
54  	/**
55  	 * Globally unique file name constructed from the concatenation of the issue
56  	 * id and original file name.
57  	 * 
58  	 * PENDING: remove this computed field.
59  	 */
60  	private String fileName;
61  
62  	/** MIME type. */
63  	private String type;
64  
65  	/** Byte size. */
66  	private long size;
67  
68  	/** Attachment description or comment. */
69  	private String description;
70  
71  	/**
72  	 * PENDING: this should probably not be saved in the DB nor be loaded in
73  	 * memory for good resource management.
74  	 */
75  	private byte[] fileData;
76  
77  	/** The User who created this attachment. */
78  	private User user;
79  
80  	/**
81  	 * Default constructor (required by Hibernate).
82  	 * 
83  	 * <p>
84  	 * PENDING: should be <code>private</code> so that it can only be used by
85  	 * Hibernate, to ensure that the fields which form an instance's identity
86  	 * are always initialized/never <tt>null</tt>.
87  	 * </p>
88  	 */
89  	public IssueAttachment() {
90  	}
91  
92  	public IssueAttachment(Issue issue, String originalFileName) {
93  		setIssue(issue);
94  		setOriginalFileName(originalFileName);
95  	}
96  
97  	/**
98  	 * Convenience constructor.
99  	 */
100 	public IssueAttachment(Issue issue, String origFileName, String type,
101 			String description, long size) {
102 		this(issue, origFileName);
103 		this.setType(type);
104 		this.setDescription(description);
105 		this.setSize(size);
106 	}
107 
108 	/**
109 	 * Convenience constructor.
110 	 */
111 	public IssueAttachment(Issue issue, String origFileName, String type,
112 			String description, long size, User user) {
113 		this(issue, origFileName, type, description, size);
114 		this.setUser(user);
115 	}
116 
117 	public Issue getIssue() {
118 		return (issue);
119 	}
120 
121 	public void setIssue(Issue issue) {
122 		if (issue == null) {
123 			throw new IllegalArgumentException("null issue");
124 		}
125 		this.issue = issue;
126 	}
127 
128 	public String getOriginalFileName() {
129 		return originalFileName;
130 	}
131 
132 	public void setOriginalFileName(String fileName) {
133 		if (fileName == null) {
134 			throw new IllegalArgumentException("null fileName");
135 		}
136 		this.originalFileName = fileName;
137 	}
138 
139 	public String getType() {
140 		return type;
141 	}
142 
143 	public void setType(String mimeType) {
144 		if (mimeType == null) {
145 			throw new IllegalArgumentException("null mimeType");
146 		}
147 		this.type = mimeType;
148 	}
149 
150 	public String getFileName() {
151 		return fileName;
152 	}
153 
154 	public void setFileName(String value) {
155 		this.fileName = value;
156 	}
157 
158 	public String getFileExtension() {
159 		final int lastIndex = this.getOriginalFileName().lastIndexOf('.');
160 
161 		if (lastIndex > 0) {
162 			return this.getOriginalFileName().substring(lastIndex);
163 		}
164 		return "";
165 	}
166 
167 	public byte[] getFileData() {
168 		if (null == fileData)
169 			return null;
170 		return fileData.clone();
171 	}
172 
173 	public void setFileData(byte[] value) {
174 		if (null == value)
175 			throw new IllegalArgumentException("value must not be null");
176 		fileData = value.clone();
177 	}
178 
179 	public String getDescription() {
180 		return description;
181 	}
182 
183 	public void setDescription(String value) {
184 		this.description = value;
185 	}
186 
187 	public long getSize() {
188 		return size;
189 	}
190 
191 	public void setSize(long size) {
192 		this.size = size;
193 	}
194 
195 	public User getUser() {
196 		return user;
197 	}
198 
199 	public void setUser(User user) {
200 		this.user = user;
201 	}
202 
203 	@Override
204 	public String toString() {
205 		return new ToStringBuilder(this).append("id", getId()).append("issue",
206 				getIssue()).append("originalfileName", getOriginalFileName())
207 				.toString();
208 	}
209 
210 	/**
211 	 * Compares 2 attachments by file size.
212 	 */
213 	public static class SizeComparator implements Comparator<IssueAttachment>,
214 			Serializable {
215 		/**
216 		 * 
217 		 */
218 		private static final long serialVersionUID = 1L;
219 
220 		public int compare(IssueAttachment a, IssueAttachment b) {
221 			return new CompareToBuilder()
222 					.append(a.getSize(), b.getSize())
223 					.append(a.getOriginalFileName(), b.getOriginalFileName())
224 					.append(a.getCreateDate(), b.getCreateDate())
225 					.toComparison();
226 		}
227 
228 	}
229 
230 	/**
231 	 * Compares 2 attachments by original filename
232 	 * 
233 	 * @author ranks
234 	 * 
235 	 */
236 	public static final class OriginalFilenameComparator implements
237 			Comparator<IssueAttachment>, Serializable {
238 		/**
239 		 * 
240 		 */
241 		private static final long serialVersionUID = 1L;
242 
243 		public int compare(IssueAttachment o1, IssueAttachment o2) {
244 			return new CompareToBuilder()
245 					.append(o1.getOriginalFileName(), o1.getOriginalFileName())
246 					.append(o1.getCreateDate(), o2.getCreateDate())
247 					.toComparison();
248 
249 		}
250 
251 	}
252 
253 }