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.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   * Class used for interactions between the controller and form when using the multifile upload widget.
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * {@inheritDoc}
60       */
61      @Override
62      public String getId() {
63          return id;
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public void setId(String id) {
71          this.id = id;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public String getName() {
79          return name;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public void setName(String name) {
87          this.name = name;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public String getContentType() {
95          return contentType;
96      }
97  
98      /**
99       * {@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 }