001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.bo; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.core.api.util.RiceUtilities; 020import org.kuali.rice.krad.service.KRADServiceLocator; 021 022import javax.persistence.Column; 023import javax.persistence.Entity; 024import javax.persistence.FetchType; 025import javax.persistence.Id; 026import javax.persistence.JoinColumn; 027import javax.persistence.OneToOne; 028import javax.persistence.Table; 029import java.io.IOException; 030import java.io.InputStream; 031 032/** 033 * @author Kuali Rice Team (rice.collab@kuali.org) 034 */ 035@Entity 036@Table(name="KRNS_ATT_T") 037public class Attachment extends PersistableBusinessObjectBase { 038 private static final long serialVersionUID = 402432724949441326L; 039 040 @Id 041 @Column(name="NTE_ID") 042 private Long noteIdentifier; 043 @Column(name="MIME_TYP") 044 private String attachmentMimeTypeCode; 045 @Column(name="FILE_NM") 046 private String attachmentFileName; 047 @Column(name="ATT_ID") 048 private String attachmentIdentifier; 049 @Column(name="FILE_SZ") 050 private Long attachmentFileSize; 051 @Column(name="ATT_TYP_CD") 052 private String attachmentTypeCode; 053 054 @OneToOne(fetch=FetchType.EAGER) 055 @JoinColumn(name="NTE_ID") 056 private Note note; 057 058 /** 059 * Default constructor. 060 */ 061 public Attachment() { 062 063 } 064 065 /** 066 * Gets the noteIdentifier attribute. 067 * 068 * @return Returns the noteIdentifier 069 * 070 */ 071 public Long getNoteIdentifier() { 072 return noteIdentifier; 073 } 074 075 /** 076 * Sets the noteIdentifier attribute. 077 * 078 * @param noteIdentifier The noteIdentifier to set. 079 * 080 */ 081 public void setNoteIdentifier(Long noteIdentifier) { 082 this.noteIdentifier = noteIdentifier; 083 } 084 085 086 /** 087 * Gets the attachmentMimeTypeCode attribute. 088 * 089 * @return Returns the attachmentMimeTypeCode 090 * 091 */ 092 public String getAttachmentMimeTypeCode() { 093 return attachmentMimeTypeCode; 094 } 095 096 /** 097 * Sets the attachmentMimeTypeCode attribute. 098 * 099 * @param attachmentMimeTypeCode The attachmentMimeTypeCode to set. 100 * 101 */ 102 public void setAttachmentMimeTypeCode(String attachmentMimeTypeCode) { 103 this.attachmentMimeTypeCode = attachmentMimeTypeCode; 104 } 105 106 107 /** 108 * Gets the attachmentFileName attribute. 109 * 110 * @return Returns the attachmentFileName 111 * 112 */ 113 public String getAttachmentFileName() { 114 return attachmentFileName; 115 } 116 117 /** 118 * Sets the attachmentFileName attribute. 119 * 120 * @param attachmentFileName The attachmentFileName to set. 121 * 122 */ 123 public void setAttachmentFileName(String attachmentFileName) { 124 this.attachmentFileName = attachmentFileName; 125 } 126 127 128 /** 129 * Gets the attachmentIdentifier attribute. 130 * 131 * @return Returns the attachmentIdentifier 132 * 133 */ 134 public String getAttachmentIdentifier() { 135 return attachmentIdentifier; 136 } 137 138 /** 139 * Sets the attachmentIdentifier attribute. 140 * 141 * @param attachmentIdentifier The attachmentIdentifier to set. 142 * 143 */ 144 public void setAttachmentIdentifier(String attachmentIdentifier) { 145 this.attachmentIdentifier = attachmentIdentifier; 146 } 147 148 149 /** 150 * Gets the attachmentFileSize attribute. 151 * 152 * @return Returns the attachmentFileSize 153 * 154 */ 155 public Long getAttachmentFileSize() { 156 return attachmentFileSize; 157 } 158 159 /** 160 * Sets the attachmentFileSize attribute. 161 * 162 * @param attachmentFileSize The attachmentFileSize to set. 163 * 164 */ 165 public void setAttachmentFileSize(Long attachmentFileSize) { 166 this.attachmentFileSize = attachmentFileSize; 167 } 168 169 /** 170 * Returns the size of the attachment with units (byte, kilobyte, ...) 171 * 172 * @return String attachment file size 173 */ 174 public String getAttachmentFileSizeWithUnits() { 175 if (attachmentFileSize != null) { 176 return RiceUtilities.getFileSizeUnits(attachmentFileSize); 177 } 178 179 return ""; 180 } 181 182 /** 183 * Gets the attachmentTypeCode attribute. 184 * 185 * @return Returns the attachmentTypeCode 186 * 187 */ 188 public String getAttachmentTypeCode() { 189 return attachmentTypeCode; 190 } 191 192 /** 193 * Sets the attachmentTypeCode attribute. 194 * 195 * @param attachmentTypeCode The attachmentTypeCode to set. 196 * 197 */ 198 public void setAttachmentTypeCode(String attachmentTypeCode) { 199 this.attachmentTypeCode = attachmentTypeCode; 200 } 201 202 /** 203 * Gets the note attribute. 204 * @return Returns the note. 205 */ 206 public Note getNote() { 207 return note; 208 } 209 210 /** 211 * Sets the note attribute value. 212 * @param note The note to set. 213 */ 214 public void setNote(Note note) { 215 this.note = note; 216 } 217 /** 218 * @return false if any of the required fields (attachmentId, fileName, fileSize, and mimeType) are blank 219 */ 220 public boolean isComplete() { 221 return (StringUtils.isNotBlank(attachmentIdentifier) && StringUtils.isNotBlank(attachmentFileName) && (attachmentFileSize != null) && StringUtils.isNotBlank(attachmentMimeTypeCode)); 222 } 223 224 /** 225 * 226 * 227 */ 228 public InputStream getAttachmentContents() throws IOException { 229 return KRADServiceLocator.getAttachmentService().retrieveAttachmentContents(this); 230 } 231} 232