001: package sample.dms;
002:
003: import java.sql.ResultSet;
004: import java.sql.SQLException;
005: import java.util.List;
006:
007: import org.acegisecurity.util.FieldUtils;
008: import org.springframework.jdbc.core.RowMapper;
009: import org.springframework.jdbc.core.support.JdbcDaoSupport;
010: import org.springframework.transaction.support.TransactionSynchronizationManager;
011: import org.springframework.util.Assert;
012:
013: /**
014: * Basic JDBC implementation of {@link DocumentDao}.
015: *
016: * @author Ben Alex
017: * @version $Id: DocumentDaoImpl.java 1784 2007-02-24 21:00:24Z luke_t $
018: */
019: public class DocumentDaoImpl extends JdbcDaoSupport implements
020: DocumentDao {
021:
022: private static final String INSERT_INTO_DIRECTORY = "insert into directory(directory_name, parent_directory_id) values (?,?)";
023: private static final String INSERT_INTO_FILE = "insert into file(file_name, content, parent_directory_id) values (?,?,?)";
024: private static final String SELECT_FROM_DIRECTORY = "select id from directory where parent_directory_id = ?";
025: private static final String SELECT_FROM_DIRECTORY_NULL = "select id from directory where parent_directory_id is null";
026: private static final String SELECT_FROM_FILE = "select id, file_name, content, parent_directory_id from file where parent_directory_id = ?";
027: private static final String SELECT_FROM_DIRECTORY_SINGLE = "select id, directory_name, parent_directory_id from directory where id = ?";
028: private static final String DELETE_FROM_FILE = "delete from file where id = ?";
029: private static final String UPDATE_FILE = "update file set content = ? where id = ?";
030: private static final String SELECT_IDENTITY = "call identity()";
031:
032: private Long obtainPrimaryKey() {
033: Assert.isTrue(TransactionSynchronizationManager
034: .isSynchronizationActive(),
035: "Transaction must be running");
036: return new Long(getJdbcTemplate().queryForLong(SELECT_IDENTITY));
037: }
038:
039: public void create(AbstractElement element) {
040: Assert.notNull(element, "Element required");
041: Assert.isNull(element.getId(),
042: "Element has previously been saved");
043: if (element instanceof Directory) {
044: Directory directory = (Directory) element;
045: Long parentId = directory.getParent() == null ? null
046: : directory.getParent().getId();
047: getJdbcTemplate().update(INSERT_INTO_DIRECTORY,
048: new Object[] { directory.getName(), parentId });
049: FieldUtils.setProtectedFieldValue("id", directory,
050: obtainPrimaryKey());
051: } else if (element instanceof File) {
052: File file = (File) element;
053: Long parentId = file.getParent() == null ? null : file
054: .getParent().getId();
055: getJdbcTemplate().update(
056: INSERT_INTO_FILE,
057: new Object[] { file.getName(), file.getContent(),
058: parentId });
059: FieldUtils.setProtectedFieldValue("id", file,
060: obtainPrimaryKey());
061: } else {
062: throw new IllegalArgumentException(
063: "Unsupported AbstractElement");
064: }
065: }
066:
067: public void delete(File file) {
068: Assert.notNull(file, "File required");
069: Assert.notNull(file.getId(), "File ID required");
070: getJdbcTemplate().update(DELETE_FROM_FILE,
071: new Object[] { file.getId() });
072: }
073:
074: /** Executes recursive SQL as needed to build a full Directory hierarchy of objects */
075: private Directory getDirectoryWithImmediateParentPopulated(
076: final Long id) {
077: return (Directory) getJdbcTemplate().queryForObject(
078: SELECT_FROM_DIRECTORY_SINGLE, new Object[] { id },
079: new RowMapper() {
080: public Object mapRow(ResultSet rs, int rowNumber)
081: throws SQLException {
082: Long parentDirectoryId = new Long(rs
083: .getLong("parent_directory_id"));
084: Directory parentDirectory = Directory.ROOT_DIRECTORY;
085: if (parentDirectoryId != null
086: && !parentDirectoryId.equals(new Long(
087: -1))) {
088: // Need to go and lookup the parent, so do that first
089: parentDirectory = getDirectoryWithImmediateParentPopulated(parentDirectoryId);
090: }
091: Directory directory = new Directory(rs
092: .getString("directory_name"),
093: parentDirectory);
094: FieldUtils.setProtectedFieldValue("id",
095: directory, new Long(rs.getLong("id")));
096: return directory;
097: }
098: });
099: }
100:
101: public AbstractElement[] findElements(Directory directory) {
102: Assert
103: .notNull(directory,
104: "Directory required (the ID can be null to refer to root)");
105: if (directory.getId() == null) {
106: List directories = getJdbcTemplate().query(
107: SELECT_FROM_DIRECTORY_NULL, new RowMapper() {
108: public Object mapRow(ResultSet rs, int rowNumber)
109: throws SQLException {
110: return getDirectoryWithImmediateParentPopulated(new Long(
111: rs.getLong("id")));
112: }
113: });
114: return (AbstractElement[]) directories
115: .toArray(new AbstractElement[] {});
116: }
117: List directories = getJdbcTemplate().query(
118: SELECT_FROM_DIRECTORY,
119: new Object[] { directory.getId() }, new RowMapper() {
120: public Object mapRow(ResultSet rs, int rowNumber)
121: throws SQLException {
122: return getDirectoryWithImmediateParentPopulated(new Long(
123: rs.getLong("id")));
124: }
125: });
126: List files = getJdbcTemplate().query(SELECT_FROM_FILE,
127: new Object[] { directory.getId() }, new RowMapper() {
128: public Object mapRow(ResultSet rs, int rowNumber)
129: throws SQLException {
130: Long parentDirectoryId = new Long(rs
131: .getLong("parent_directory_id"));
132: Directory parentDirectory = null;
133: if (parentDirectoryId != null) {
134: parentDirectory = getDirectoryWithImmediateParentPopulated(parentDirectoryId);
135: }
136: File file = new File(rs.getString("file_name"),
137: parentDirectory);
138: FieldUtils.setProtectedFieldValue("id", file,
139: new Long(rs.getLong("id")));
140: return file;
141: }
142: });
143: // Add the File elements after the Directory elements
144: directories.addAll(files);
145: return (AbstractElement[]) directories
146: .toArray(new AbstractElement[] {});
147: }
148:
149: public void update(File file) {
150: Assert.notNull(file, "File required");
151: Assert.notNull(file.getId(), "File ID required");
152: getJdbcTemplate().update(UPDATE_FILE,
153: new Object[] { file.getContent(), file.getId() });
154: }
155:
156: }
|