KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > models > IssueAttachmentModel


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 cowsultants.itracker.ejb.client.models;
20
21 import java.util.Comparator JavaDoc;
22
23 public class IssueAttachmentModel extends GenericModel implements Comparator JavaDoc {
24     private String JavaDoc type;
25     private String JavaDoc fileName;
26     private String JavaDoc originalFileName;
27     private String JavaDoc description;
28     private long size;
29     private String JavaDoc firstName;
30     private String JavaDoc lastName;
31     private Integer JavaDoc issueId;
32     private Integer JavaDoc userId;
33
34     private UserModel user;
35
36     public IssueAttachmentModel() {
37     }
38
39     public IssueAttachmentModel(String JavaDoc origFileName, String JavaDoc type, String JavaDoc description, long size) {
40         this.setOriginalFileName(origFileName);
41         this.setType(type);
42         this.setDescription(description);
43         this.setSize(size);
44     }
45
46     public IssueAttachmentModel(String JavaDoc origFileName, String JavaDoc type, String JavaDoc description, long size, Integer JavaDoc issueId, Integer JavaDoc userId) {
47         this(origFileName, type, description, size);
48         this.setIssueId(issueId);
49         this.setUserId(userId);
50     }
51
52     public String JavaDoc getOriginalFileName() {
53         return (originalFileName == null ? "" : originalFileName);
54     }
55
56     public void setOriginalFileName(String JavaDoc value) {
57         originalFileName = value;
58     }
59
60     public String JavaDoc getType() {
61         return type;
62     }
63
64     public void setType(String JavaDoc value) {
65         type = value;
66     }
67
68     public String JavaDoc getFileName() {
69         return (fileName == null ? "attachment" + getId() : fileName);
70     }
71
72     public void setFileName(String JavaDoc value) {
73         fileName = value;
74     }
75
76     public String JavaDoc getDescription() {
77         return (description == null ? "" : description);
78     }
79
80     public void setDescription(String JavaDoc value) {
81         description = value;
82     }
83
84     public long getSize() {
85         return size;
86     }
87
88     public void setSize(long value) {
89         size = value;
90     }
91
92     public UserModel getUser() {
93         return user;
94     }
95
96     public void setUser(UserModel value) {
97         user = value;
98     }
99
100     public Integer JavaDoc getUserId() {
101         return (user == null ? userId : user.getId());
102     }
103
104     public void setUserId(Integer JavaDoc value) {
105         userId = value;
106     }
107
108     public String JavaDoc getUserFirstName() {
109         return (user == null ? "" : user.getFirstName());
110     }
111
112     public String JavaDoc getUserLastName() {
113         return (user == null ? "" : user.getLastName());
114     }
115
116     public Integer JavaDoc getIssueId() {
117         return issueId;
118     }
119
120     public void setIssueId(Integer JavaDoc value) {
121         issueId = value;
122     }
123
124     public String JavaDoc getFileExtension() {
125         int lastIndex = getOriginalFileName().lastIndexOf('.');
126         if(lastIndex > 0) {
127            return getOriginalFileName().substring(lastIndex);
128         }
129
130         return "";
131     }
132
133     public String JavaDoc toString() {
134         return "Attachment [" + getId() + "] Description: " + getDescription() + " FileName: " + getFileName() +
135                " OrigFileName: " + getOriginalFileName() + " Size: " + getSize() + " Type: " + getType();
136     }
137
138     public int compare(Object JavaDoc a, Object JavaDoc b) {
139         return this.new CompareByDate().compare(a, b);
140     }
141
142     public boolean equals(Object JavaDoc obj) {
143         return this.new CompareByDate().equals(obj);
144     }
145
146     public int hashCode() {
147         return this.new CompareByDate().hashCode();
148     }
149
150     public abstract class IssueAttachmentModelComparator implements Comparator JavaDoc {
151         protected boolean isAscending = true;
152
153         public IssueAttachmentModelComparator() {
154         }
155
156         public IssueAttachmentModelComparator(boolean isAscending) {
157             setAscending(isAscending);
158         }
159
160         public void setAscending(boolean value) {
161             this.isAscending = value;
162         }
163
164         protected abstract int doComparison(IssueAttachmentModel ma, IssueAttachmentModel mb);
165
166         public final boolean equals(Object JavaDoc obj) {
167             if(! (obj instanceof IssueAttachmentModel)) {
168                 return false;
169             }
170
171             try {
172                 IssueAttachmentModel mo = (IssueAttachmentModel) obj;
173                 if(IssueAttachmentModel.this.getCreateDate().equals(mo.getCreateDate()) &&
174                    IssueAttachmentModel.this.getFileName().equals(mo.getFileName()) &&
175                    IssueAttachmentModel.this.getDescription().equals(mo.getDescription())) {
176                     return true;
177                 }
178             } catch(ClassCastException JavaDoc cce) {
179             }
180
181             return false;
182         }
183
184         public final int hashCode() {
185             return ((IssueAttachmentModel.this.getCreateDate() == null ? 1 : IssueAttachmentModel.this.getCreateDate().hashCode()) ^
186                     IssueAttachmentModel.this.getFileName().hashCode() ^
187                     (IssueAttachmentModel.this.getDescription() == null ? 1 : IssueAttachmentModel.this.getDescription().hashCode()));
188         }
189
190         public final int compare(Object JavaDoc a, Object JavaDoc b) {
191             if(! (a instanceof IssueAttachmentModel) || ! (b instanceof IssueAttachmentModel)) {
192                 throw new ClassCastException JavaDoc();
193             }
194
195             IssueAttachmentModel ma = (IssueAttachmentModel) a;
196             IssueAttachmentModel mb = (IssueAttachmentModel) b;
197
198             int result = doComparison(ma, mb);
199             if(! isAscending) {
200                 result = result * -1;
201             }
202             return result;
203         }
204     }
205
206     public class CompareByDate extends IssueAttachmentModelComparator {
207         public CompareByDate(){
208           super();
209         }
210
211         public CompareByDate(boolean isAscending) {
212           super(isAscending);
213         }
214
215         protected int doComparison(IssueAttachmentModel ma, IssueAttachmentModel mb) {
216             if(ma.getCreateDate() == null && mb.getCreateDate() == null) {
217                 return 0;
218             } else if(ma.getCreateDate() == null) {
219                 return 1;
220             } else if(mb.getCreateDate() == null) {
221                 return -1;
222             }
223
224             if(ma.getCreateDate().equals(mb.getCreateDate())) {
225                 return 0;
226             } else if(ma.getCreateDate().before(mb.getCreateDate())) {
227                 return -1;
228             } else {
229                 return 1;
230             }
231         }
232     }
233
234     public class CompareByIssueId extends IssueAttachmentModelComparator {
235         public CompareByIssueId(){
236           super();
237         }
238
239         public CompareByIssueId(boolean isAscending) {
240           super(isAscending);
241         }
242
243         protected int doComparison(IssueAttachmentModel ma, IssueAttachmentModel mb) {
244             if(ma.getIssueId() == null && mb.getIssueId() == null) {
245                 return 0;
246             } else if(ma.getIssueId() == null) {
247                 return 1;
248             } else if(mb.getIssueId() == null) {
249                 return -1;
250             }
251
252             if(ma.getIssueId().equals(mb.getIssueId())) {
253                 return ma.getId().compareTo(mb.getId());
254             } else {
255                 return ma.getIssueId().compareTo(mb.getIssueId());
256             }
257         }
258     }
259
260     public class CompareBySize extends IssueAttachmentModelComparator {
261         public CompareBySize(){
262           super();
263         }
264
265         public CompareBySize(boolean isAscending) {
266           super(isAscending);
267         }
268
269         protected int doComparison(IssueAttachmentModel ma, IssueAttachmentModel mb) {
270             if(ma.getIssueId() == null && mb.getIssueId() == null) {
271                 return 0;
272             } else if(ma.getIssueId() == null) {
273                 return 1;
274             } else if(mb.getIssueId() == null) {
275                 return -1;
276             }
277
278             if(ma.getSize() == mb.getSize()) {
279                 return ma.getIssueId().compareTo(mb.getIssueId());
280             } else if(ma.getSize() > mb.getSize()) {
281                 return 1;
282             } else {
283                 return -1;
284             }
285         }
286     }
287 }
Popular Tags