001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package sample.contact;
017:
018: import org.springframework.jdbc.core.SqlParameter;
019: import org.springframework.jdbc.core.support.JdbcDaoSupport;
020: import org.springframework.jdbc.object.MappingSqlQuery;
021: import org.springframework.jdbc.object.SqlUpdate;
022:
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025: import java.sql.Types;
026:
027: import java.util.List;
028:
029: import javax.sql.DataSource;
030:
031: /**
032: * Base implementation of {@link ContactDao} that uses Spring JDBC services.
033: *
034: * @author Ben Alex
035: * @version $Id: ContactDaoSpring.java 1496 2006-05-23 13:38:33Z benalex $
036: */
037: public class ContactDaoSpring extends JdbcDaoSupport implements
038: ContactDao {
039: //~ Instance fields ================================================================================================
040:
041: private ContactDelete contactDelete;
042: private ContactInsert contactInsert;
043: private ContactUpdate contactUpdate;
044: private ContactsAllQuery contactsAllQuery;
045: private ContactsByIdQuery contactsByIdQuery;
046: private PrincipalsAllQuery principalsAllQuery;
047: private RolesAllQuery rolesAllQuery;
048:
049: //~ Methods ========================================================================================================
050:
051: public void create(Contact contact) {
052: System.out.println("creating contact w/ id " + contact.getId()
053: + " " + contact.getEmail());
054: contactInsert.insert(contact);
055: }
056:
057: public void delete(Long contactId) {
058: contactDelete.delete(contactId);
059: }
060:
061: public List findAll() {
062: return contactsAllQuery.execute();
063: }
064:
065: public List findAllPrincipals() {
066: return principalsAllQuery.execute();
067: }
068:
069: public List findAllRoles() {
070: return rolesAllQuery.execute();
071: }
072:
073: public Contact getById(Long id) {
074: List list = contactsByIdQuery.execute(id.longValue());
075:
076: if (list.size() == 0) {
077: return null;
078: } else {
079: return (Contact) list.get(0);
080: }
081: }
082:
083: protected void initDao() throws Exception {
084: contactInsert = new ContactInsert(getDataSource());
085: contactUpdate = new ContactUpdate(getDataSource());
086: contactDelete = new ContactDelete(getDataSource());
087: contactsAllQuery = new ContactsAllQuery(getDataSource());
088: principalsAllQuery = new PrincipalsAllQuery(getDataSource());
089: rolesAllQuery = new RolesAllQuery(getDataSource());
090: contactsByIdQuery = new ContactsByIdQuery(getDataSource());
091: }
092:
093: private String makeObjectIdentity(Contact contact) {
094: return contact.getClass().getName() + ":" + contact.getId();
095: }
096:
097: public void update(Contact contact) {
098: contactUpdate.update(contact);
099: }
100:
101: //~ Inner Classes ==================================================================================================
102:
103: protected class AclObjectIdentityByObjectIdentityQuery extends
104: MappingSqlQuery {
105: protected AclObjectIdentityByObjectIdentityQuery(DataSource ds) {
106: super (ds,
107: "SELECT id FROM acl_object_identity WHERE object_identity = ?");
108: declareParameter(new SqlParameter(Types.VARCHAR));
109: compile();
110: }
111:
112: protected Object mapRow(ResultSet rs, int rownum)
113: throws SQLException {
114: return new Long(rs.getLong("id"));
115: }
116: }
117:
118: protected class AclObjectIdentityInsert extends SqlUpdate {
119: protected AclObjectIdentityInsert(DataSource ds) {
120: super (ds,
121: "INSERT INTO acl_object_identity VALUES (?, ?, ?, ?)");
122: declareParameter(new SqlParameter(Types.BIGINT));
123: declareParameter(new SqlParameter(Types.VARCHAR));
124: declareParameter(new SqlParameter(Types.INTEGER));
125: declareParameter(new SqlParameter(Types.VARCHAR));
126: compile();
127: }
128:
129: protected int insert(String objectIdentity,
130: Long parentAclObjectIdentity, String aclClass) {
131: Object[] objs = new Object[] { null, objectIdentity,
132: parentAclObjectIdentity, aclClass };
133: super .update(objs);
134:
135: return getJdbcTemplate().queryForInt("call identity()");
136: }
137: }
138:
139: protected class ContactDelete extends SqlUpdate {
140: protected ContactDelete(DataSource ds) {
141: super (ds, "DELETE FROM contacts WHERE id = ?");
142: declareParameter(new SqlParameter(Types.BIGINT));
143: compile();
144: }
145:
146: protected void delete(Long contactId) {
147: super .update(contactId.longValue());
148: }
149: }
150:
151: protected class ContactInsert extends SqlUpdate {
152: protected ContactInsert(DataSource ds) {
153: super (ds, "INSERT INTO contacts VALUES (?, ?, ?)");
154: declareParameter(new SqlParameter(Types.BIGINT));
155: declareParameter(new SqlParameter(Types.VARCHAR));
156: declareParameter(new SqlParameter(Types.VARCHAR));
157: compile();
158: }
159:
160: protected void insert(Contact contact) {
161: Object[] objs = new Object[] { contact.getId(),
162: contact.getName(), contact.getEmail() };
163: super .update(objs);
164: }
165: }
166:
167: protected class ContactUpdate extends SqlUpdate {
168: protected ContactUpdate(DataSource ds) {
169: super (ds,
170: "UPDATE contacts SET contact_name = ?, address = ? WHERE id = ?");
171: declareParameter(new SqlParameter(Types.VARCHAR));
172: declareParameter(new SqlParameter(Types.VARCHAR));
173: declareParameter(new SqlParameter(Types.BIGINT));
174: compile();
175: }
176:
177: protected void update(Contact contact) {
178: Object[] objs = new Object[] { contact.getName(),
179: contact.getEmail(), contact.getId() };
180: super .update(objs);
181: }
182: }
183:
184: protected class ContactsAllQuery extends MappingSqlQuery {
185: protected ContactsAllQuery(DataSource ds) {
186: super (ds,
187: "SELECT id, contact_name, email FROM contacts ORDER BY id");
188: compile();
189: }
190:
191: protected Object mapRow(ResultSet rs, int rownum)
192: throws SQLException {
193: Contact contact = new Contact();
194: contact.setId(new Long(rs.getLong("id")));
195: contact.setName(rs.getString("contact_name"));
196: contact.setEmail(rs.getString("email"));
197:
198: return contact;
199: }
200: }
201:
202: protected class ContactsByIdQuery extends MappingSqlQuery {
203: protected ContactsByIdQuery(DataSource ds) {
204: super (ds,
205: "SELECT id, contact_name, email FROM contacts WHERE id = ? ORDER BY id");
206: declareParameter(new SqlParameter(Types.BIGINT));
207: compile();
208: }
209:
210: protected Object mapRow(ResultSet rs, int rownum)
211: throws SQLException {
212: Contact contact = new Contact();
213: contact.setId(new Long(rs.getLong("id")));
214: contact.setName(rs.getString("contact_name"));
215: contact.setEmail(rs.getString("email"));
216:
217: return contact;
218: }
219: }
220:
221: protected class PermissionDelete extends SqlUpdate {
222: protected PermissionDelete(DataSource ds) {
223: super (ds,
224: "DELETE FROM acl_permission WHERE ACL_OBJECT_IDENTITY = ? AND RECIPIENT = ?");
225: declareParameter(new SqlParameter(Types.BIGINT));
226: declareParameter(new SqlParameter(Types.VARCHAR));
227: compile();
228: }
229:
230: protected void delete(Long aclObjectIdentity, String recipient) {
231: super .update(new Object[] { aclObjectIdentity, recipient });
232: }
233: }
234:
235: protected class PermissionInsert extends SqlUpdate {
236: protected PermissionInsert(DataSource ds) {
237: super (ds, "INSERT INTO acl_permission VALUES (?, ?, ?, ?);");
238: declareParameter(new SqlParameter(Types.BIGINT));
239: declareParameter(new SqlParameter(Types.BIGINT));
240: declareParameter(new SqlParameter(Types.VARCHAR));
241: declareParameter(new SqlParameter(Types.INTEGER));
242: compile();
243: }
244:
245: protected int insert(Long aclObjectIdentity, String recipient,
246: Integer mask) {
247: Object[] objs = new Object[] { null, aclObjectIdentity,
248: recipient, mask };
249: super .update(objs);
250:
251: return getJdbcTemplate().queryForInt("call identity()");
252: }
253: }
254:
255: protected class PrincipalsAllQuery extends MappingSqlQuery {
256: protected PrincipalsAllQuery(DataSource ds) {
257: super (ds, "SELECT username FROM users ORDER BY username");
258: compile();
259: }
260:
261: protected Object mapRow(ResultSet rs, int rownum)
262: throws SQLException {
263: return rs.getString("username");
264: }
265: }
266:
267: protected class RolesAllQuery extends MappingSqlQuery {
268: protected RolesAllQuery(DataSource ds) {
269: super (ds,
270: "SELECT DISTINCT authority FROM authorities ORDER BY authority");
271: compile();
272: }
273:
274: protected Object mapRow(ResultSet rs, int rownum)
275: throws SQLException {
276: return rs.getString("authority");
277: }
278: }
279: }
|