View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.bo;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  
21  import javax.persistence.CascadeType;
22  import javax.persistence.Column;
23  import javax.persistence.Entity;
24  import javax.persistence.FetchType;
25  import javax.persistence.Id;
26  import javax.persistence.JoinColumn;
27  import javax.persistence.OneToOne;
28  import javax.persistence.Table;
29  import javax.persistence.UniqueConstraint;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.kuali.rice.core.api.util.RiceUtilities;
33  import org.kuali.rice.krad.service.KRADServiceLocator;
34  
35  /**
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  @Entity
39  @Table(name = "KRNS_ATT_T", uniqueConstraints = {@UniqueConstraint(name = "KRNS_ATT_TC0", columnNames = "OBJ_ID")})
40  public class Attachment extends PersistableBusinessObjectBaseAdapter {
41      private static final long serialVersionUID = 402432724949441326L;
42  
43      @Id @Column(name = "NTE_ID", length = 14, precision = 0, updatable = false)
44      private Long noteIdentifier;
45      @Column(name = "MIME_TYP", length = 255)
46      private String attachmentMimeTypeCode;
47      @Column(name = "FILE_NM", length = 250)
48      private String attachmentFileName;
49      @Column(name = "ATT_ID", length = 36)
50      private String attachmentIdentifier;
51      @Column(name = "FILE_SZ", length = 14, precision = 0)
52      private Long attachmentFileSize;
53      @Column(name = "ATT_TYP_CD", length = 40)
54      private String attachmentTypeCode;
55  
56      @OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH}) @JoinColumn(name = "NTE_ID",
57              updatable = false, insertable = false)
58      private Note note;
59  
60      /**
61       * Default constructor.
62       */
63      public Attachment() {
64  
65      }
66  
67      /**
68       * Gets the noteIdentifier attribute.
69       *
70       * @return Returns the noteIdentifier
71       */
72      public Long getNoteIdentifier() {
73          return noteIdentifier;
74      }
75  
76      /**
77       * Sets the noteIdentifier attribute.
78       *
79       * @param noteIdentifier The noteIdentifier to set.
80       */
81      public void setNoteIdentifier(Long noteIdentifier) {
82          this.noteIdentifier = noteIdentifier;
83      }
84  
85      /**
86       * Gets the attachmentMimeTypeCode attribute.
87       *
88       * @return Returns the attachmentMimeTypeCode
89       */
90      public String getAttachmentMimeTypeCode() {
91          return attachmentMimeTypeCode;
92      }
93  
94      /**
95       * Sets the attachmentMimeTypeCode attribute.
96       *
97       * @param attachmentMimeTypeCode The attachmentMimeTypeCode to set.
98       */
99      public void setAttachmentMimeTypeCode(String attachmentMimeTypeCode) {
100         this.attachmentMimeTypeCode = attachmentMimeTypeCode;
101     }
102 
103     /**
104      * Gets the attachmentFileName attribute.
105      *
106      * @return Returns the attachmentFileName
107      */
108     public String getAttachmentFileName() {
109         return attachmentFileName;
110     }
111 
112     /**
113      * Sets the attachmentFileName attribute.
114      *
115      * @param attachmentFileName The attachmentFileName to set.
116      */
117     public void setAttachmentFileName(String attachmentFileName) {
118         this.attachmentFileName = attachmentFileName;
119     }
120 
121     /**
122      * Gets the attachmentIdentifier attribute.
123      *
124      * @return Returns the attachmentIdentifier
125      */
126     public String getAttachmentIdentifier() {
127         return attachmentIdentifier;
128     }
129 
130     /**
131      * Sets the attachmentIdentifier attribute.
132      *
133      * @param attachmentIdentifier The attachmentIdentifier to set.
134      */
135     public void setAttachmentIdentifier(String attachmentIdentifier) {
136         this.attachmentIdentifier = attachmentIdentifier;
137     }
138 
139     /**
140      * Gets the attachmentFileSize attribute.
141      *
142      * @return Returns the attachmentFileSize
143      */
144     public Long getAttachmentFileSize() {
145         return attachmentFileSize;
146     }
147 
148     /**
149      * Sets the attachmentFileSize attribute.
150      *
151      * @param attachmentFileSize The attachmentFileSize to set.
152      */
153     public void setAttachmentFileSize(Long attachmentFileSize) {
154         this.attachmentFileSize = attachmentFileSize;
155     }
156 
157     /**
158      * Returns the size of the attachment with units (byte, kilobyte, ...)
159      *
160      * @return String attachment file size
161      */
162     public String getAttachmentFileSizeWithUnits() {
163         if (attachmentFileSize != null) {
164             return RiceUtilities.getFileSizeUnits(attachmentFileSize);
165         }
166 
167         return "";
168     }
169 
170     /**
171      * Gets the attachmentTypeCode attribute.
172      *
173      * @return Returns the attachmentTypeCode
174      */
175     public String getAttachmentTypeCode() {
176         return attachmentTypeCode;
177     }
178 
179     /**
180      * Sets the attachmentTypeCode attribute.
181      *
182      * @param attachmentTypeCode The attachmentTypeCode to set.
183      */
184     public void setAttachmentTypeCode(String attachmentTypeCode) {
185         this.attachmentTypeCode = attachmentTypeCode;
186     }
187 
188     /**
189      * Gets the note attribute.
190      *
191      * @return Returns the note.
192      */
193     public Note getNote() {
194         return note;
195     }
196 
197     /**
198      * Sets the note attribute value.
199      *
200      * @param note The note to set.
201      */
202     public void setNote(Note note) {
203         this.note = note;
204     }
205 
206     /**
207      * @return false if any of the required fields (attachmentId, fileName, fileSize, and mimeType) are blank
208      */
209     public boolean isComplete() {
210         return (StringUtils.isNotBlank(attachmentIdentifier) && StringUtils.isNotBlank(attachmentFileName) && (
211                 attachmentFileSize
212                         != null) && StringUtils.isNotBlank(attachmentMimeTypeCode));
213     }
214 
215     public InputStream getAttachmentContents() throws IOException {
216         return KRADServiceLocator.getAttachmentService().retrieveAttachmentContents(this);
217     }
218 }
219