1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
29
30
31
32
33
34
35
36 public class IssueAttachment extends AbstractEntity implements
37 Comparable<Entity> {
38
39
40
41
42 private static final long serialVersionUID = 1L;
43
44 public static final Comparator<IssueAttachment> SIZE_COMPARATOR = new SizeComparator();
45
46 public static final Comparator<IssueAttachment> ORIGIINAL_FILENAME_COMPARATOR = new OriginalFilenameComparator();
47
48
49 private Issue issue;
50
51
52 private String originalFileName;
53
54
55
56
57
58
59
60 private String fileName;
61
62
63 private String type;
64
65
66 private long size;
67
68
69 private String description;
70
71
72
73
74
75 private byte[] fileData;
76
77
78 private User user;
79
80
81
82
83
84
85
86
87
88
89 public IssueAttachment() {
90 }
91
92 public IssueAttachment(Issue issue, String originalFileName) {
93 setIssue(issue);
94 setOriginalFileName(originalFileName);
95 }
96
97
98
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
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
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
232
233
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 }