Source Code Cross Referenced for DocumentDaoImpl.java in  » Security » acegi-security » sample » dms » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Security » acegi security » sample.dms 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.