ContactDaoSpring.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package sample.contact;
  16. import org.springframework.jdbc.core.SqlParameter;
  17. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  18. import org.springframework.jdbc.object.MappingSqlQuery;
  19. import org.springframework.jdbc.object.SqlUpdate;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.sql.Types;
  23. import java.util.List;
  24. import javax.sql.DataSource;
  25. /**
  26. * Base implementation of {@link ContactDao} that uses Spring JDBC services.
  27. *
  28. * @author Ben Alex
  29. * @version $Id$
  30. */
  31. public class ContactDaoSpring extends JdbcDaoSupport implements ContactDao {
  32. //~ Instance fields ================================================================================================
  33. private ContactDelete contactDelete;
  34. private ContactInsert contactInsert;
  35. private ContactUpdate contactUpdate;
  36. private ContactsAllQuery contactsAllQuery;
  37. private ContactsByIdQuery contactsByIdQuery;
  38. private PrincipalsAllQuery principalsAllQuery;
  39. private RolesAllQuery rolesAllQuery;
  40. //~ Methods ========================================================================================================
  41. public void create(Contact contact) {
  42. contactInsert.insert(contact);
  43. }
  44. public void delete(Long contactId) {
  45. contactDelete.delete(contactId);
  46. }
  47. public List<Contact> findAll() {
  48. return contactsAllQuery.execute();
  49. }
  50. public List<String> findAllPrincipals() {
  51. return principalsAllQuery.execute();
  52. }
  53. public List<String> findAllRoles() {
  54. return rolesAllQuery.execute();
  55. }
  56. public Contact getById(Long id) {
  57. List<Contact> list = contactsByIdQuery.execute(id.longValue());
  58. if (list.size() == 0) {
  59. return null;
  60. } else {
  61. return (Contact) list.get(0);
  62. }
  63. }
  64. protected void initDao() throws Exception {
  65. contactInsert = new ContactInsert(getDataSource());
  66. contactUpdate = new ContactUpdate(getDataSource());
  67. contactDelete = new ContactDelete(getDataSource());
  68. contactsAllQuery = new ContactsAllQuery(getDataSource());
  69. principalsAllQuery = new PrincipalsAllQuery(getDataSource());
  70. rolesAllQuery = new RolesAllQuery(getDataSource());
  71. contactsByIdQuery = new ContactsByIdQuery(getDataSource());
  72. }
  73. public void update(Contact contact) {
  74. contactUpdate.update(contact);
  75. }
  76. //~ Inner Classes ==================================================================================================
  77. protected class AclObjectIdentityByObjectIdentityQuery extends MappingSqlQuery<Long> {
  78. protected AclObjectIdentityByObjectIdentityQuery(DataSource ds) {
  79. super(ds, "SELECT id FROM acl_object_identity WHERE object_identity = ?");
  80. declareParameter(new SqlParameter(Types.VARCHAR));
  81. compile();
  82. }
  83. protected Long mapRow(ResultSet rs, int rownum)
  84. throws SQLException {
  85. return new Long(rs.getLong("id"));
  86. }
  87. }
  88. protected class AclObjectIdentityInsert extends SqlUpdate {
  89. protected AclObjectIdentityInsert(DataSource ds) {
  90. super(ds, "INSERT INTO acl_object_identity VALUES (?, ?, ?, ?)");
  91. declareParameter(new SqlParameter(Types.BIGINT));
  92. declareParameter(new SqlParameter(Types.VARCHAR));
  93. declareParameter(new SqlParameter(Types.INTEGER));
  94. declareParameter(new SqlParameter(Types.VARCHAR));
  95. compile();
  96. }
  97. protected int insert(String objectIdentity, Long parentAclObjectIdentity, String aclClass) {
  98. Object[] objs = new Object[] {null, objectIdentity, parentAclObjectIdentity, aclClass};
  99. super.update(objs);
  100. return getJdbcTemplate().queryForInt("call identity()");
  101. }
  102. }
  103. protected class ContactDelete extends SqlUpdate {
  104. protected ContactDelete(DataSource ds) {
  105. super(ds, "DELETE FROM contacts WHERE id = ?");
  106. declareParameter(new SqlParameter(Types.BIGINT));
  107. compile();
  108. }
  109. protected void delete(Long contactId) {
  110. super.update(contactId.longValue());
  111. }
  112. }
  113. protected class ContactInsert extends SqlUpdate {
  114. protected ContactInsert(DataSource ds) {
  115. super(ds, "INSERT INTO contacts VALUES (?, ?, ?)");
  116. declareParameter(new SqlParameter(Types.BIGINT));
  117. declareParameter(new SqlParameter(Types.VARCHAR));
  118. declareParameter(new SqlParameter(Types.VARCHAR));
  119. compile();
  120. }
  121. protected void insert(Contact contact) {
  122. Object[] objs = new Object[] {contact.getId(), contact.getName(), contact.getEmail()};
  123. super.update(objs);
  124. }
  125. }
  126. protected class ContactUpdate extends SqlUpdate {
  127. protected ContactUpdate(DataSource ds) {
  128. super(ds, "UPDATE contacts SET contact_name = ?, address = ? WHERE id = ?");
  129. declareParameter(new SqlParameter(Types.VARCHAR));
  130. declareParameter(new SqlParameter(Types.VARCHAR));
  131. declareParameter(new SqlParameter(Types.BIGINT));
  132. compile();
  133. }
  134. protected void update(Contact contact) {
  135. Object[] objs = new Object[] {contact.getName(), contact.getEmail(), contact.getId()};
  136. super.update(objs);
  137. }
  138. }
  139. protected class ContactsAllQuery extends MappingSqlQuery<Contact> {
  140. protected ContactsAllQuery(DataSource ds) {
  141. super(ds, "SELECT id, contact_name, email FROM contacts ORDER BY id");
  142. compile();
  143. }
  144. protected Contact mapRow(ResultSet rs, int rownum) throws SQLException {
  145. Contact contact = new Contact();
  146. contact.setId(new Long(rs.getLong("id")));
  147. contact.setName(rs.getString("contact_name"));
  148. contact.setEmail(rs.getString("email"));
  149. return contact;
  150. }
  151. }
  152. protected class ContactsByIdQuery extends MappingSqlQuery<Contact> {
  153. protected ContactsByIdQuery(DataSource ds) {
  154. super(ds, "SELECT id, contact_name, email FROM contacts WHERE id = ? ORDER BY id");
  155. declareParameter(new SqlParameter(Types.BIGINT));
  156. compile();
  157. }
  158. protected Contact mapRow(ResultSet rs, int rownum) throws SQLException {
  159. Contact contact = new Contact();
  160. contact.setId(new Long(rs.getLong("id")));
  161. contact.setName(rs.getString("contact_name"));
  162. contact.setEmail(rs.getString("email"));
  163. return contact;
  164. }
  165. }
  166. protected class PermissionDelete extends SqlUpdate {
  167. protected PermissionDelete(DataSource ds) {
  168. super(ds, "DELETE FROM acl_permission WHERE ACL_OBJECT_IDENTITY = ? AND RECIPIENT = ?");
  169. declareParameter(new SqlParameter(Types.BIGINT));
  170. declareParameter(new SqlParameter(Types.VARCHAR));
  171. compile();
  172. }
  173. protected void delete(Long aclObjectIdentity, String recipient) {
  174. super.update(new Object[] {aclObjectIdentity, recipient});
  175. }
  176. }
  177. protected class PermissionInsert extends SqlUpdate {
  178. protected PermissionInsert(DataSource ds) {
  179. super(ds, "INSERT INTO acl_permission VALUES (?, ?, ?, ?);");
  180. declareParameter(new SqlParameter(Types.BIGINT));
  181. declareParameter(new SqlParameter(Types.BIGINT));
  182. declareParameter(new SqlParameter(Types.VARCHAR));
  183. declareParameter(new SqlParameter(Types.INTEGER));
  184. compile();
  185. }
  186. protected int insert(Long aclObjectIdentity, String recipient, Integer mask) {
  187. Object[] objs = new Object[] {null, aclObjectIdentity, recipient, mask};
  188. super.update(objs);
  189. return getJdbcTemplate().queryForInt("call identity()");
  190. }
  191. }
  192. protected class PrincipalsAllQuery extends MappingSqlQuery<String> {
  193. protected PrincipalsAllQuery(DataSource ds) {
  194. super(ds, "SELECT username FROM users ORDER BY username");
  195. compile();
  196. }
  197. protected String mapRow(ResultSet rs, int rownum) throws SQLException {
  198. return rs.getString("username");
  199. }
  200. }
  201. protected class RolesAllQuery extends MappingSqlQuery<String> {
  202. protected RolesAllQuery(DataSource ds) {
  203. super(ds, "SELECT DISTINCT authority FROM authorities ORDER BY authority");
  204. compile();
  205. }
  206. protected String mapRow(ResultSet rs, int rownum) throws SQLException {
  207. return rs.getString("authority");
  208. }
  209. }
  210. }