Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
41   253   30   1.71
12   132   0.73   8
24     1.25  
3    
 
 
  IssueAttachment       Line # 36 39 28 23.3% 0.23287672
  IssueAttachment.SizeComparator       Line # 213 1 1 0% 0.0
  IssueAttachment.OriginalFilenameComparator       Line # 236 1 1 0% 0.0
 
No Tests
 
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  2 toggle public IssueAttachment() {
90    }
91   
 
92  0 toggle public IssueAttachment(Issue issue, String originalFileName) {
93  0 setIssue(issue);
94  0 setOriginalFileName(originalFileName);
95    }
96   
97    /**
98    * Convenience constructor.
99    */
 
100  0 toggle public IssueAttachment(Issue issue, String origFileName, String type,
101    String description, long size) {
102  0 this(issue, origFileName);
103  0 this.setType(type);
104  0 this.setDescription(description);
105  0 this.setSize(size);
106    }
107   
108    /**
109    * Convenience constructor.
110    */
 
111  0 toggle public IssueAttachment(Issue issue, String origFileName, String type,
112    String description, long size, User user) {
113  0 this(issue, origFileName, type, description, size);
114  0 this.setUser(user);
115    }
116   
 
117  0 toggle public Issue getIssue() {
118  0 return (issue);
119    }
120   
 
121  0 toggle public void setIssue(Issue issue) {
122  0 if (issue == null) {
123  0 throw new IllegalArgumentException("null issue");
124    }
125  0 this.issue = issue;
126    }
127   
 
128  0 toggle public String getOriginalFileName() {
129  0 return originalFileName;
130    }
131   
 
132  1 toggle public void setOriginalFileName(String fileName) {
133  1 if (fileName == null) {
134  0 throw new IllegalArgumentException("null fileName");
135    }
136  1 this.originalFileName = fileName;
137    }
138   
 
139  0 toggle public String getType() {
140  0 return type;
141    }
142   
 
143  1 toggle public void setType(String mimeType) {
144  1 if (mimeType == null) {
145  0 throw new IllegalArgumentException("null mimeType");
146    }
147  1 this.type = mimeType;
148    }
149   
 
150  0 toggle public String getFileName() {
151  0 return fileName;
152    }
153   
 
154  2 toggle public void setFileName(String value) {
155  2 this.fileName = value;
156    }
157   
 
158  0 toggle public String getFileExtension() {
159  0 final int lastIndex = this.getOriginalFileName().lastIndexOf('.');
160   
161  0 if (lastIndex > 0) {
162  0 return this.getOriginalFileName().substring(lastIndex);
163    }
164  0 return "";
165    }
166   
 
167  0 toggle public byte[] getFileData() {
168  0 if (null == fileData)
169  0 return null;
170  0 return fileData.clone();
171    }
172   
 
173  0 toggle public void setFileData(byte[] value) {
174  0 if (null == value)
175  0 throw new IllegalArgumentException("value must not be null");
176  0 fileData = value.clone();
177    }
178   
 
179  0 toggle public String getDescription() {
180  0 return description;
181    }
182   
 
183  1 toggle public void setDescription(String value) {
184  1 this.description = value;
185    }
186   
 
187  0 toggle public long getSize() {
188  0 return size;
189    }
190   
 
191  1 toggle public void setSize(long size) {
192  1 this.size = size;
193    }
194   
 
195  0 toggle public User getUser() {
196  0 return user;
197    }
198   
 
199  1 toggle public void setUser(User user) {
200  1 this.user = user;
201    }
202   
 
203  0 toggle @Override
204    public String toString() {
205  0 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  0 toggle public int compare(IssueAttachment a, IssueAttachment b) {
221  0 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  0 toggle public int compare(IssueAttachment o1, IssueAttachment o2) {
244  0 return new CompareToBuilder()
245    .append(o1.getOriginalFileName(), o1.getOriginalFileName())
246    .append(o1.getCreateDate(), o2.getCreateDate())
247    .toComparison();
248   
249    }
250   
251    }
252   
253    }