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 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   * A file attachment to an Issue.
29   * <p/>
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      /**
44       * Compares 2 attachments by file size.
45       */
46      public static final Comparator<IssueAttachment> SIZE_COMPARATOR = new SizeComparator();
47      /**
48       * Compares 2 attachments by original filename.
49       */
50      public static final Comparator<IssueAttachment> ORIGIINAL_FILENAME_COMPARATOR = new OriginalFilenameComparator();
51  
52      /**
53       * The issue to which the file is attached.
54       */
55      private Issue issue;
56  
57      /**
58       * The file name used to upload the attachment.
59       */
60      private String originalFileName;
61  
62      /**
63       * Globally unique file name constructed from the concatenation of the issue
64       * id and original file name.
65       * <p/>
66       * PENDING: remove this computed field.
67       */
68      private String fileName;
69  
70      /**
71       * MIME type.
72       */
73      private String type;
74  
75      /**
76       * Byte size.
77       */
78      private long size;
79  
80      /**
81       * Attachment description or comment.
82       */
83      private String description;
84  
85      /**
86       * PENDING: this should probably not be saved in the DB nor be loaded in
87       * memory for good resource management.
88       */
89      private byte[] fileData;
90  
91      /**
92       * The User who created this attachment.
93       */
94      private User user;
95  
96      /**
97       * Default constructor (required by Hibernate).
98       * <p/>
99       * <p>
100      * PENDING: should be <code>private</code> so that it can only be used by
101      * Hibernate, to ensure that the fields which form an instance's identity
102      * are always initialized/never <tt>null</tt>.
103      * </p>
104      */
105     public IssueAttachment() {
106     }
107 
108     public IssueAttachment(Issue issue, String originalFileName) {
109         setIssue(issue);
110         setOriginalFileName(originalFileName);
111     }
112 
113     /**
114      * Convenience constructor.
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      * Convenience constructor.
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      * Compares 2 attachments by file size.
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      * Compares 2 attachments by original filename
248      *
249      * @author ranks
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 }