KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > imagedb > DefaultImageDatabase


1 package org.springframework.samples.imagedb;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.OutputStream JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.util.List JavaDoc;
10
11 import org.springframework.dao.DataAccessException;
12 import org.springframework.dao.IncorrectResultSizeDataAccessException;
13 import org.springframework.jdbc.LobRetrievalFailureException;
14 import org.springframework.jdbc.core.RowMapper;
15 import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
16 import org.springframework.jdbc.core.support.AbstractLobStreamingResultSetExtractor;
17 import org.springframework.jdbc.core.support.JdbcDaoSupport;
18 import org.springframework.jdbc.support.lob.LobCreator;
19 import org.springframework.jdbc.support.lob.LobHandler;
20 import org.springframework.util.FileCopyUtils;
21
22 /**
23  * Default implementation of the central image database business interface.
24  *
25  * <p>Uses JDBC with a LobHandler to retrieve and store image data.
26  * Illustrates direct use of the jdbc.core package, i.e. JdbcTemplate,
27  * rather than operation objects from the jdbc.object package.
28  *
29  * @author Juergen Hoeller
30  * @since 07.01.2004
31  * @see org.springframework.jdbc.core.JdbcTemplate
32  * @see org.springframework.jdbc.support.lob.LobHandler
33  */

34 public class DefaultImageDatabase extends JdbcDaoSupport implements ImageDatabase {
35
36     private LobHandler lobHandler;
37
38     /**
39      * Set the LobHandler to use for BLOB/CLOB access.
40      * Could use a DefaultLobHandler instance as default,
41      * but relies on a specified LobHandler here.
42      * @see org.springframework.jdbc.support.lob.DefaultLobHandler
43      */

44     public void setLobHandler(LobHandler lobHandler) {
45         this.lobHandler = lobHandler;
46     }
47
48     public List JavaDoc getImages() throws DataAccessException {
49         return getJdbcTemplate().query(
50             "SELECT image_name, description FROM imagedb",
51             new RowMapper() {
52                 public Object JavaDoc mapRow(ResultSet JavaDoc rs, int rowNum) throws SQLException JavaDoc {
53                     String JavaDoc name = rs.getString(1);
54                     String JavaDoc description = lobHandler.getClobAsString(rs, 2);
55                     return new ImageDescriptor(name, description);
56                 }
57             });
58     }
59
60     public void streamImage(final String JavaDoc name, final OutputStream JavaDoc contentStream) throws DataAccessException {
61         getJdbcTemplate().query(
62                 "SELECT content FROM imagedb WHERE image_name=?", new Object JavaDoc[] {name},
63                 new AbstractLobStreamingResultSetExtractor() {
64                     protected void handleNoRowFound() throws LobRetrievalFailureException {
65                         throw new IncorrectResultSizeDataAccessException(
66                             "Image with name '" + name + "' not found in database", 1, 0);
67                     }
68                     public void streamData(ResultSet JavaDoc rs) throws SQLException JavaDoc, IOException JavaDoc {
69                         InputStream JavaDoc is = lobHandler.getBlobAsBinaryStream(rs, 1);
70                         if (is != null) {
71                             FileCopyUtils.copy(is, contentStream);
72                         }
73                     }
74                 }
75         );
76     }
77
78     public void storeImage(
79         final String JavaDoc name, final InputStream JavaDoc contentStream, final int contentLength, final String JavaDoc description)
80         throws DataAccessException {
81
82         getJdbcTemplate().execute(
83                 "INSERT INTO imagedb (image_name, content, description) VALUES (?, ?, ?)",
84                 new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {
85                     protected void setValues(PreparedStatement JavaDoc ps, LobCreator lobCreator) throws SQLException JavaDoc {
86                         ps.setString(1, name);
87                         lobCreator.setBlobAsBinaryStream(ps, 2, contentStream, contentLength);
88                         lobCreator.setClobAsString(ps, 3, description);
89                     }
90                 }
91         );
92     }
93
94     public void checkImages() {
95         // Could implement consistency check here...
96
logger.info("Checking images: not implemented but invoked by scheduling");
97     }
98
99     public void clearDatabase() throws DataAccessException {
100         getJdbcTemplate().update("DELETE FROM imagedb");
101     }
102
103 }
104
Popular Tags