IssueAttachment.java

  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. package org.itracker.model;

  19. import org.apache.commons.lang.builder.CompareToBuilder;
  20. import org.apache.commons.lang.builder.ToStringBuilder;

  21. import java.io.Serializable;
  22. import java.util.Comparator;

  23. /**
  24.  * A file attachment to an Issue.
  25.  * <p/>
  26.  * <p>
  27.  * An IssueAttachment can only belong to 1 Issue (composition).
  28.  * </p>
  29.  *
  30.  * @author ready
  31.  */
  32. public class IssueAttachment extends AbstractEntity implements
  33.         Comparable<Entity> {

  34.     /**
  35.      *
  36.      */
  37.     private static final long serialVersionUID = 1L;
  38.     /**
  39.      * Compares 2 attachments by file size.
  40.      */
  41.     public static final Comparator<IssueAttachment> SIZE_COMPARATOR = new SizeComparator();
  42.     /**
  43.      * Compares 2 attachments by original filename.
  44.      */
  45.     public static final Comparator<IssueAttachment> ORIGIINAL_FILENAME_COMPARATOR = new OriginalFilenameComparator();

  46.     /**
  47.      * The issue to which the file is attached.
  48.      */
  49.     private Issue issue;

  50.     /**
  51.      * The file name used to upload the attachment.
  52.      */
  53.     private String originalFileName;

  54.     /**
  55.      * Globally unique file name constructed from the concatenation of the issue
  56.      * id and original file name.
  57.      * <p/>
  58.      * PENDING: remove this computed field.
  59.      */
  60.     private String fileName;

  61.     /**
  62.      * MIME type.
  63.      */
  64.     private String type;

  65.     /**
  66.      * Byte size.
  67.      */
  68.     private long size;

  69.     /**
  70.      * Attachment description or comment.
  71.      */
  72.     private String description;

  73.     /**
  74.      * PENDING: this should probably not be saved in the DB nor be loaded in
  75.      * memory for good resource management.
  76.      */
  77.     private byte[] fileData;

  78.     /**
  79.      * The User who created this attachment.
  80.      */
  81.     private User user;

  82.     /**
  83.      * Default constructor (required by Hibernate).
  84.      * <p/>
  85.      * <p>
  86.      * PENDING: should be <code>private</code> so that it can only be used by
  87.      * Hibernate, to ensure that the fields which form an instance's identity
  88.      * are always initialized/never <tt>null</tt>.
  89.      * </p>
  90.      */
  91.     public IssueAttachment() {
  92.     }

  93.     public IssueAttachment(Issue issue, String originalFileName) {
  94.         setIssue(issue);
  95.         setOriginalFileName(originalFileName);
  96.     }

  97.     /**
  98.      * Convenience constructor.
  99.      */
  100.     public IssueAttachment(Issue issue, String origFileName, String type,
  101.                            String description, long size) {
  102.         this(issue, origFileName);
  103.         this.setType(type);
  104.         this.setDescription(description);
  105.         this.setSize(size);
  106.     }

  107.     /**
  108.      * Convenience constructor.
  109.      */
  110.     public IssueAttachment(Issue issue, String origFileName, String type,
  111.                            String description, long size, User user) {
  112.         this(issue, origFileName, type, description, size);
  113.         this.setUser(user);
  114.     }

  115.     public Issue getIssue() {
  116.         return (issue);
  117.     }

  118.     public void setIssue(Issue issue) {
  119.         if (issue == null) {
  120.             throw new IllegalArgumentException("null issue");
  121.         }
  122.         this.issue = issue;
  123.     }

  124.     public String getOriginalFileName() {
  125.         return originalFileName;
  126.     }

  127.     public void setOriginalFileName(String fileName) {
  128.         if (fileName == null) {
  129.             throw new IllegalArgumentException("null fileName");
  130.         }
  131.         this.originalFileName = fileName;
  132.     }

  133.     public String getType() {
  134.         return type;
  135.     }

  136.     public void setType(String mimeType) {
  137.         if (mimeType == null) {
  138.             throw new IllegalArgumentException("null mimeType");
  139.         }
  140.         this.type = mimeType;
  141.     }

  142.     public String getFileName() {
  143.         return fileName;
  144.     }

  145.     public void setFileName(String value) {
  146.         this.fileName = value;
  147.     }

  148.     public String getFileExtension() {
  149.         final int lastIndex = this.getOriginalFileName().lastIndexOf('.');

  150.         if (lastIndex > 0) {
  151.             return this.getOriginalFileName().substring(lastIndex);
  152.         }
  153.         return "";
  154.     }

  155.     public byte[] getFileData() {
  156.         if (null == fileData)
  157.             return null;
  158.         return fileData.clone();
  159.     }

  160.     public void setFileData(byte[] value) {
  161.         if (null == value)
  162.             throw new IllegalArgumentException("value must not be null");
  163.         fileData = value.clone();
  164.     }

  165.     public String getDescription() {
  166.         return description;
  167.     }

  168.     public void setDescription(String value) {
  169.         this.description = value;
  170.     }

  171.     public long getSize() {
  172.         return size;
  173.     }

  174.     public void setSize(long size) {
  175.         this.size = size;
  176.     }

  177.     public User getUser() {
  178.         return user;
  179.     }

  180.     public void setUser(User user) {
  181.         this.user = user;
  182.     }

  183.     @Override
  184.     public String toString() {
  185.         return new ToStringBuilder(this).append("id", getId()).append("issue",
  186.                 getIssue()).append("originalfileName", getOriginalFileName())
  187.                 .toString();
  188.     }

  189.     /**
  190.      * Compares 2 attachments by file size.
  191.      */
  192.     public static class SizeComparator implements Comparator<IssueAttachment>,
  193.             Serializable {
  194.         /**
  195.          *
  196.          */
  197.         private static final long serialVersionUID = 1L;

  198.         public int compare(IssueAttachment a, IssueAttachment b) {
  199.             return new CompareToBuilder()
  200.                     .append(a.getSize(), b.getSize())
  201.                     .append(a.getOriginalFileName(), b.getOriginalFileName())
  202.                     .append(a.getCreateDate(), b.getCreateDate())
  203.                     .toComparison();
  204.         }

  205.     }

  206.     /**
  207.      * Compares 2 attachments by original filename
  208.      *
  209.      * @author ranks
  210.      */
  211.     public static final class OriginalFilenameComparator implements
  212.             Comparator<IssueAttachment>, Serializable {
  213.         /**
  214.          *
  215.          */
  216.         private static final long serialVersionUID = 1L;

  217.         public int compare(IssueAttachment o1, IssueAttachment o2) {
  218.             return new CompareToBuilder()
  219.                     .append(o1.getOriginalFileName(), o1.getOriginalFileName())
  220.                     .append(o1.getCreateDate(), o2.getCreateDate())
  221.                     .toComparison();

  222.         }

  223.     }

  224. }