1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.file;
17
18 import org.kuali.rice.core.api.CoreApiServiceLocator;
19 import org.springframework.web.multipart.MultipartFile;
20
21 import javax.sql.rowset.serial.SerialBlob;
22 import java.io.InputStream;
23 import java.io.Serializable;
24 import java.sql.Blob;
25 import java.text.DecimalFormat;
26 import java.util.Date;
27
28
29
30
31
32
33 public class FileMetaBlob implements Serializable, FileMeta {
34
35 private static final long serialVersionUID = 56328058337130228L;
36
37 private String id;
38 private String name;
39 private String contentType;
40 private Long size;
41 private Date dateUploaded;
42 private String url;
43
44 private MultipartFile multipartFile;
45 private Blob blob;
46
47 public FileMetaBlob() {
48 }
49
50 public void init(MultipartFile multipartFile) throws Exception {
51 this.name = multipartFile.getOriginalFilename();
52 this.contentType = multipartFile.getContentType();
53 this.size = multipartFile.getSize();
54 this.multipartFile = multipartFile;
55 blob = new SerialBlob(multipartFile.getBytes());
56 }
57
58
59
60
61 @Override
62 public String getId() {
63 return id;
64 }
65
66
67
68
69 @Override
70 public void setId(String id) {
71 this.id = id;
72 }
73
74
75
76
77 @Override
78 public String getName() {
79 return name;
80 }
81
82
83
84
85 @Override
86 public void setName(String name) {
87 this.name = name;
88 }
89
90
91
92
93 @Override
94 public String getContentType() {
95 return contentType;
96 }
97
98
99
100
101 @Override
102 public void setContentType(String contentType) {
103 this.contentType = contentType;
104 }
105
106
107
108
109 @Override
110 public Long getSize() {
111 return size;
112 }
113
114
115
116
117 @Override
118 public void setSize(Long size) {
119 this.size = size;
120 }
121
122
123
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
140
141 @Override
142 public Date getDateUploaded() {
143 return dateUploaded;
144 }
145
146
147
148
149 @Override
150 public void setDateUploaded(Date dateUploaded) {
151 this.dateUploaded = dateUploaded;
152 }
153
154
155
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
168
169 @Override
170 public String getUrl() {
171 return url;
172 }
173
174
175
176
177 @Override
178 public void setUrl(String url) {
179 this.url = url;
180 }
181
182
183
184
185
186
187 public MultipartFile getMultipartFile() {
188 return multipartFile;
189 }
190
191
192
193
194 public void setMultipartFile(MultipartFile multipartFile) {
195 this.multipartFile = multipartFile;
196 }
197
198
199
200
201
202
203 public Blob getBlob() {
204 return blob;
205 }
206
207
208
209
210 public void setBlob(Blob blob) {
211 this.blob = blob;
212 }
213
214
215
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 }