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 org.apache.commons.lang.builder.CompareToBuilder;
22  import org.apache.commons.lang.builder.ToStringBuilder;
23  
24  import java.io.Serializable;
25  import java.util.Comparator;
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  
45  
46      public static final Comparator<IssueAttachment> SIZE_COMPARATOR = new SizeComparator();
47      
48  
49  
50      public static final Comparator<IssueAttachment> ORIGIINAL_FILENAME_COMPARATOR = new OriginalFilenameComparator();
51  
52      
53  
54  
55      private Issue issue;
56  
57      
58  
59  
60      private String originalFileName;
61  
62      
63  
64  
65  
66  
67  
68      private String fileName;
69  
70      
71  
72  
73      private String type;
74  
75      
76  
77  
78      private long size;
79  
80      
81  
82  
83      private String description;
84  
85      
86  
87  
88  
89      private byte[] fileData;
90  
91      
92  
93  
94      private User user;
95  
96      
97  
98  
99  
100 
101 
102 
103 
104 
105     public IssueAttachment() {
106     }
107 
108     public IssueAttachment(Issue issue, String originalFileName) {
109         setIssue(issue);
110         setOriginalFileName(originalFileName);
111     }
112 
113     
114 
115 
116     public IssueAttachment(Issue issue, String origFileName, String type,
117                            String description, long size) {
118         this(issue, origFileName);
119         this.setType(type);
120         this.setDescription(description);
121         this.setSize(size);
122     }
123 
124     
125 
126 
127     public IssueAttachment(Issue issue, String origFileName, String type,
128                            String description, long size, User user) {
129         this(issue, origFileName, type, description, size);
130         this.setUser(user);
131     }
132 
133     public Issue getIssue() {
134         return (issue);
135     }
136 
137     public void setIssue(Issue issue) {
138         if (issue == null) {
139             throw new IllegalArgumentException("null issue");
140         }
141         this.issue = issue;
142     }
143 
144     public String getOriginalFileName() {
145         return originalFileName;
146     }
147 
148     public void setOriginalFileName(String fileName) {
149         if (fileName == null) {
150             throw new IllegalArgumentException("null fileName");
151         }
152         this.originalFileName = fileName;
153     }
154 
155     public String getType() {
156         return type;
157     }
158 
159     public void setType(String mimeType) {
160         if (mimeType == null) {
161             throw new IllegalArgumentException("null mimeType");
162         }
163         this.type = mimeType;
164     }
165 
166     public String getFileName() {
167         return fileName;
168     }
169 
170     public void setFileName(String value) {
171         this.fileName = value;
172     }
173 
174     public String getFileExtension() {
175         final int lastIndex = this.getOriginalFileName().lastIndexOf('.');
176 
177         if (lastIndex > 0) {
178             return this.getOriginalFileName().substring(lastIndex);
179         }
180         return "";
181     }
182 
183     public byte[] getFileData() {
184         if (null == fileData)
185             return null;
186         return fileData.clone();
187     }
188 
189     public void setFileData(byte[] value) {
190         if (null == value)
191             throw new IllegalArgumentException("value must not be null");
192         fileData = value.clone();
193     }
194 
195     public String getDescription() {
196         return description;
197     }
198 
199     public void setDescription(String value) {
200         this.description = value;
201     }
202 
203     public long getSize() {
204         return size;
205     }
206 
207     public void setSize(long size) {
208         this.size = size;
209     }
210 
211     public User getUser() {
212         return user;
213     }
214 
215     public void setUser(User user) {
216         this.user = user;
217     }
218 
219     @Override
220     public String toString() {
221         return new ToStringBuilder(this).append("id", getId()).append("issue",
222                 getIssue()).append("originalfileName", getOriginalFileName())
223                 .toString();
224     }
225 
226     
227 
228 
229     public static class SizeComparator implements Comparator<IssueAttachment>,
230             Serializable {
231         
232 
233 
234         private static final long serialVersionUID = 1L;
235 
236         public int compare(IssueAttachment/../../org/itracker/model/IssueAttachment.html#IssueAttachment">IssueAttachment a, IssueAttachment b) {
237             return new CompareToBuilder()
238                     .append(a.getSize(), b.getSize())
239                     .append(a.getOriginalFileName(), b.getOriginalFileName())
240                     .append(a.getCreateDate(), b.getCreateDate())
241                     .toComparison();
242         }
243 
244     }
245 
246     
247 
248 
249 
250 
251     public static final class OriginalFilenameComparator implements
252             Comparator<IssueAttachment>, Serializable {
253         
254 
255 
256         private static final long serialVersionUID = 1L;
257 
258         public int compare(IssueAttachment../../org/itracker/model/IssueAttachment.html#IssueAttachment">IssueAttachment o1, IssueAttachment o2) {
259             return new CompareToBuilder()
260                     .append(o1.getOriginalFileName(), o1.getOriginalFileName())
261                     .append(o1.getCreateDate(), o2.getCreateDate())
262                     .toComparison();
263 
264         }
265 
266     }
267 
268 }