001/** 002 * Copyright 2005-2018 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.file; 017 018import org.kuali.rice.core.api.CoreApiServiceLocator; 019import org.springframework.web.multipart.MultipartFile; 020 021import javax.sql.rowset.serial.SerialBlob; 022import java.io.InputStream; 023import java.io.Serializable; 024import java.sql.Blob; 025import java.text.DecimalFormat; 026import java.util.Date; 027 028/** 029 * Class used for interactions between the controller and form when using the multifile upload widget. 030 * 031 * @author Kuali Rice Team (rice.collab@kuali.org) 032 */ 033public class FileMetaBlob implements Serializable, FileMeta { 034 035 private static final long serialVersionUID = 56328058337130228L; 036 037 private String id; 038 private String name; 039 private String contentType; 040 private Long size; 041 private Date dateUploaded; 042 private String url; 043 044 private MultipartFile multipartFile; 045 private Blob blob; 046 047 public FileMetaBlob() { 048 } 049 050 public void init(MultipartFile multipartFile) throws Exception { 051 this.name = multipartFile.getOriginalFilename(); 052 this.contentType = multipartFile.getContentType(); 053 this.size = multipartFile.getSize(); 054 this.multipartFile = multipartFile; 055 blob = new SerialBlob(multipartFile.getBytes()); 056 } 057 058 /** 059 * {@inheritDoc} 060 */ 061 @Override 062 public String getId() { 063 return id; 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 @Override 070 public void setId(String id) { 071 this.id = id; 072 } 073 074 /** 075 * {@inheritDoc} 076 */ 077 @Override 078 public String getName() { 079 return name; 080 } 081 082 /** 083 * {@inheritDoc} 084 */ 085 @Override 086 public void setName(String name) { 087 this.name = name; 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 @Override 094 public String getContentType() { 095 return contentType; 096 } 097 098 /** 099 * {@inheritDoc} 100 */ 101 @Override 102 public void setContentType(String contentType) { 103 this.contentType = contentType; 104 } 105 106 /** 107 * {@inheritDoc} 108 */ 109 @Override 110 public Long getSize() { 111 return size; 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public void setSize(Long size) { 119 this.size = size; 120 } 121 122 /** 123 * {@inheritDoc} 124 */ 125 @Override 126 public String getSizeFormatted() { 127 DecimalFormat format = new DecimalFormat("0.#"); 128 129 if (size >= 1000000000) { 130 return format.format((((double)size) / 1000000000)) + " GB"; 131 } else if (size >= 1000000) { 132 return format.format((((double)size) / 1000000)) + " MB"; 133 } else { 134 return format.format((((double)size) / 1000)) + " KB"; 135 } 136 } 137 138 /** 139 * {@inheritDoc} 140 */ 141 @Override 142 public Date getDateUploaded() { 143 return dateUploaded; 144 } 145 146 /** 147 * {@inheritDoc} 148 */ 149 @Override 150 public void setDateUploaded(Date dateUploaded) { 151 this.dateUploaded = dateUploaded; 152 } 153 154 /** 155 * {@inheritDoc} 156 */ 157 @Override 158 public String getDateUploadedFormatted() { 159 if (dateUploaded != null) { 160 return CoreApiServiceLocator.getDateTimeService().toDateTimeString(dateUploaded); 161 } else { 162 return ""; 163 } 164 } 165 166 /** 167 * {@inheritDoc} 168 */ 169 @Override 170 public String getUrl() { 171 return url; 172 } 173 174 /** 175 * {@inheritDoc} 176 */ 177 @Override 178 public void setUrl(String url) { 179 this.url = url; 180 } 181 182 /** 183 * Get the MultipartFile that is populated by the controller during the upload process. 184 * 185 * @return the MultipartFile object 186 */ 187 public MultipartFile getMultipartFile() { 188 return multipartFile; 189 } 190 191 /** 192 * @see #getMultipartFile() 193 */ 194 public void setMultipartFile(MultipartFile multipartFile) { 195 this.multipartFile = multipartFile; 196 } 197 198 /** 199 * Get the serialized blob data representing the file 200 * 201 * @return the blob data 202 */ 203 public Blob getBlob() { 204 return blob; 205 } 206 207 /** 208 * @see #getBlob() 209 */ 210 public void setBlob(Blob blob) { 211 this.blob = blob; 212 } 213 214 /** 215 * {@inheritDoc} 216 */ 217 @Override 218 public String toString() { 219 return "FileBase{" + 220 "id='" + id + '\'' + 221 ", name='" + name + '\'' + 222 ", contentType='" + contentType + '\'' + 223 ", size=" + size + 224 ", dateUploaded=" + dateUploaded + 225 ", url='" + url + '\'' + 226 '}'; 227 } 228}