1 18 19 package cowsultants.itracker.ejb.client.models; 20 21 import java.util.Comparator ; 22 23 public class IssueAttachmentModel extends GenericModel implements Comparator { 24 private String type; 25 private String fileName; 26 private String originalFileName; 27 private String description; 28 private long size; 29 private String firstName; 30 private String lastName; 31 private Integer issueId; 32 private Integer userId; 33 34 private UserModel user; 35 36 public IssueAttachmentModel() { 37 } 38 39 public IssueAttachmentModel(String origFileName, String type, String 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 origFileName, String type, String description, long size, Integer issueId, Integer userId) { 47 this(origFileName, type, description, size); 48 this.setIssueId(issueId); 49 this.setUserId(userId); 50 } 51 52 public String getOriginalFileName() { 53 return (originalFileName == null ? "" : originalFileName); 54 } 55 56 public void setOriginalFileName(String value) { 57 originalFileName = value; 58 } 59 60 public String getType() { 61 return type; 62 } 63 64 public void setType(String value) { 65 type = value; 66 } 67 68 public String getFileName() { 69 return (fileName == null ? "attachment" + getId() : fileName); 70 } 71 72 public void setFileName(String value) { 73 fileName = value; 74 } 75 76 public String getDescription() { 77 return (description == null ? "" : description); 78 } 79 80 public void setDescription(String 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 getUserId() { 101 return (user == null ? userId : user.getId()); 102 } 103 104 public void setUserId(Integer value) { 105 userId = value; 106 } 107 108 public String getUserFirstName() { 109 return (user == null ? "" : user.getFirstName()); 110 } 111 112 public String getUserLastName() { 113 return (user == null ? "" : user.getLastName()); 114 } 115 116 public Integer getIssueId() { 117 return issueId; 118 } 119 120 public void setIssueId(Integer value) { 121 issueId = value; 122 } 123 124 public String getFileExtension() { 125 int lastIndex = getOriginalFileName().lastIndexOf('.'); 126 if(lastIndex > 0) { 127 return getOriginalFileName().substring(lastIndex); 128 } 129 130 return ""; 131 } 132 133 public String toString() { 134 return "Attachment [" + getId() + "] Description: " + getDescription() + " FileName: " + getFileName() + 135 " OrigFileName: " + getOriginalFileName() + " Size: " + getSize() + " Type: " + getType(); 136 } 137 138 public int compare(Object a, Object b) { 139 return this.new CompareByDate().compare(a, b); 140 } 141 142 public boolean equals(Object 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 { 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 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 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 a, Object b) { 191 if(! (a instanceof IssueAttachmentModel) || ! (b instanceof IssueAttachmentModel)) { 192 throw new ClassCastException (); 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 |