1 package org.springframework.samples.imagedb; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.OutputStream ; 6 import java.sql.PreparedStatement ; 7 import java.sql.ResultSet ; 8 import java.sql.SQLException ; 9 import java.util.List ; 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 34 public class DefaultImageDatabase extends JdbcDaoSupport implements ImageDatabase { 35 36 private LobHandler lobHandler; 37 38 44 public void setLobHandler(LobHandler lobHandler) { 45 this.lobHandler = lobHandler; 46 } 47 48 public List getImages() throws DataAccessException { 49 return getJdbcTemplate().query( 50 "SELECT image_name, description FROM imagedb", 51 new RowMapper() { 52 public Object mapRow(ResultSet rs, int rowNum) throws SQLException { 53 String name = rs.getString(1); 54 String description = lobHandler.getClobAsString(rs, 2); 55 return new ImageDescriptor(name, description); 56 } 57 }); 58 } 59 60 public void streamImage(final String name, final OutputStream contentStream) throws DataAccessException { 61 getJdbcTemplate().query( 62 "SELECT content FROM imagedb WHERE image_name=?", new Object [] {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 rs) throws SQLException , IOException { 69 InputStream 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 name, final InputStream contentStream, final int contentLength, final String 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 ps, LobCreator lobCreator) throws SQLException { 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 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 |