Source code: com/RuntimeCollective/webapps/bean/SimpleDBFile.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/bean/SimpleDBFile.java,v 1.6 2003/09/30 15:13:09 joe Exp $
2 * $Revision: 1.6 $
3 * $Date: 2003/09/30 15:13:09 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.bean;
31
32 import com.RuntimeCollective.webapps.bean.EntityBean;
33 import com.RuntimeCollective.webapps.RuntimeDataSource;
34 import com.RuntimeCollective.webapps.RuntimeParameters;
35 import java.io.InputStream;
36 import java.io.ByteArrayInputStream;
37 import java.io.ByteArrayOutputStream;
38
39 import java.io.IOException;
40 import java.sql.SQLException;
41
42 /** Database entry which manages and refers to an file in the database
43 *
44 * @version $Id: SimpleDBFile.java,v 1.6 2003/09/30 15:13:09 joe Exp $
45 */
46 public class SimpleDBFile implements DBFile {
47
48 /** The name of the database table for this bean type. */
49 public static final String DATABASE_TABLE = "rs_file";
50
51
52 // == Contstructors ====================================================
53
54 /** Construct a new blank SimpleDBFile, giving it a new unique ID.
55 *
56 * @exception SQLException if a new id cannot be allocated in the
57 * database.
58 */
59 public SimpleDBFile() throws SQLException {
60 setId(RuntimeDataSource.nextId());
61
62 }
63
64 /** Get an existing SimpleDBFile from the RuntimeDataSource, given an
65 * id.
66 *
67 * @param id ID of the SimpleDBFile.
68 * @exception SQLException if id cannot be retrieved.
69 */
70 public SimpleDBFile(int id) throws SQLException {
71 setId(id);
72
73 load();
74 }
75
76 // == Persistence methods ==============================================
77
78 public void save() throws SQLException {
79 // Save this SimpleDBFile
80 RuntimeDataSource.save(getId(), DATABASE_TABLE,
81 new String[] { "name",
82 "mime_type" },
83 new Object[] { getName(),
84 getMimeType() } );
85
86 try {
87 saveFile();
88
89 flushFileData();
90 } catch (IOException e) {
91 throw new SQLException(e.getMessage());
92 }
93 }
94
95
96 /** Delete this bean from the database. */
97 public void delete() throws SQLException {
98 // Delete this SimpleDBFile.
99 RuntimeDataSource.update(DELETE +
100 FROM_FILE +
101 WHERE_ID_EQUALS + getId());
102
103 }
104
105
106 // == Properties =======================================================
107
108 /** EntityBean id */
109 private int id;
110 /** get the unique id of this EntityBean. */
111 public int getId() { return id; }
112 /** set the id of this EntityBean. */
113 public void setId(int id) { this.id = id; }
114
115 /** file name */
116 private String name = null;
117 /** get the file name. */
118 public String getName() { return name; }
119 /** set the file name. */
120 public void setName(String name) { this.name = name; }
121
122 /** image mime type. */
123 private String mimeType = null;
124 /** get the mime type. */
125 public String getMimeType() { return mimeType; }
126 /** set the mime type. */
127 public void setMimeType(String mimeType) { this.mimeType = mimeType; }
128
129 /** image file data. */
130 private byte[] fileData = null;
131 /** get the file data. */
132 public byte[] getFileData() {
133 if (null == fileData) {
134 loadFileData();
135 }
136
137 return fileData;
138 }
139 /** set the file data. */
140 public void setFileData(byte[] fileData) { this.fileData = fileData; }
141
142
143 // == Utility methods ==================================================
144
145 protected void saveFile() throws IOException {
146
147 if (fileData != null) {
148
149 ByteArrayInputStream fileDataIS = new ByteArrayInputStream(fileData);
150
151 try {
152 RuntimeDataSource.updateBlob( DATABASE_TABLE,
153 WHERE_BLOB_ID_EQUALS + getId(),
154 BLOB_COLUMN,
155 fileDataIS,
156 fileData.length );
157 } catch (SQLException e) {
158 e.printStackTrace(System.out);
159 throw new IOException("Couldn't save BLOB "+id+" in the database: "+e);
160 }
161 }
162 }
163
164
165 protected void flushFileData() { fileData = null; }
166
167
168 // == Private methods ==================================================
169
170 /** Load the attributes of this File. */
171 private void load() throws SQLException {
172 Object[] result = RuntimeDataSource.queryRow(SELECT_ALL +
173 FROM_FILE +
174 WHERE_ID_EQUALS + getId());
175
176 if (result != null) {
177 setId(Integer.parseInt(result[0].toString()));
178 name = (String) result[1];
179 setMimeType((String) result[2]);
180 }
181 }
182
183 private void loadFileData() {
184
185 try {
186
187 ByteArrayOutputStream out = new ByteArrayOutputStream();
188 InputStream content = RuntimeDataSource.queryBlob(SELECT_BLOB_WHERE_ID_EQUALS + getId());
189
190 // FIXME: why 2k chunks? Why not bigger? What's the optimum? Can I use a buffered reader?
191 byte buf[] = new byte[2048];
192 int size = 0;
193 while ((size = content.read(buf)) > 0) {
194 out.write(buf, 0, size);
195 }
196 out.flush();
197
198 setFileData(out.toByteArray());
199
200 } catch (SQLException e) {
201 e.printStackTrace(System.out);
202 RuntimeParameters.logError(this, "failed to load file BLOB data "+id+" in the database: ", e);
203 setFileData(null);
204 } catch (IOException e) {
205 e.printStackTrace(System.out);
206 RuntimeParameters.logError(this, "failed to load file data from db", e);
207 setFileData(null);
208 }
209 }
210
211 // == Constants ========================================================
212
213 public static final String SELECT_ALL = "SELECT id,name,mime_type ";
214 public static final String SELECT_ID = "SELECT id ";
215
216 public static final String FROM_FILE = "FROM " + DATABASE_TABLE + " ";
217
218 public static final String WHERE_ID_EQUALS = "WHERE id = ";
219
220 public static final String DELETE = "DELETE ";
221
222 public static final String WHERE_BLOB_ID_EQUALS = "id = ";
223
224 public static final String BLOB_COLUMN = "content";
225
226 public static final String SELECT_BLOB_WHERE_ID_EQUALS = "SELECT "
227 + BLOB_COLUMN
228 + " FROM "
229 + DATABASE_TABLE
230 + " WHERE id = ";
231
232 }