View Javadoc

    1   /* Copyright 2004, 2005 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
    16  package sample.contact;
    17
    18  import org.springframework.jdbc.core.SqlParameter;
    19  import org.springframework.jdbc.core.support.JdbcDaoSupport;
    20  import org.springframework.jdbc.object.MappingSqlQuery;
    21  import org.springframework.jdbc.object.SqlUpdate;
    22
    23  import java.sql.ResultSet;
    24  import java.sql.SQLException;
    25  import java.sql.Types;
    26
    27  import java.util.List;
    28
    29  import javax.sql.DataSource;
    30
    31
    32  /***
    33   * Base implementation of {@link ContactDao} that uses Spring JDBC services.
    34   *
    35   * @author Ben Alex
    36   * @version $Id: ContactDaoSpring.java,v 1.3 2005/11/04 04:15:57 benalex Exp $
    37   */
    38  public class ContactDaoSpring extends JdbcDaoSupport implements ContactDao {
    39      //~ Instance fields ========================================================
    40
    41      private ContactDelete contactDelete;
    42      private ContactInsert contactInsert;
    43      private ContactUpdate contactUpdate;
    44      private ContactsAllQuery contactsAllQuery;
    45      private ContactsByIdQuery contactsByIdQuery;
    46      private PrincipalsAllQuery principalsAllQuery;
    47      private RolesAllQuery rolesAllQuery;
    48
    49      //~ Methods ================================================================
    50
    51      public Contact getById(Long id) {
    52          List list = contactsByIdQuery.execute(id.longValue());
    53
    54          if (list.size() == 0) {
    55              return null;
    56          } else {
    57              return (Contact) list.get(0);
    58          }
    59      }
    60
    61      public void create(Contact contact) {
    62          System.out.println("creating contact w/ id " + contact.getId() + " "
    63              + contact.getEmail());
    64          contactInsert.insert(contact);
    65      }
    66
    67      public void delete(Long contactId) {
    68          contactDelete.delete(contactId);
    69      }
    70
    71      public List findAll() {
    72          return contactsAllQuery.execute();
    73      }
    74
    75      public List findAllPrincipals() {
    76          return principalsAllQuery.execute();
    77      }
    78
    79      public List findAllRoles() {
    80          return rolesAllQuery.execute();
    81      }
    82
    83      public void update(Contact contact) {
    84          contactUpdate.update(contact);
    85      }
    86
    87      protected void initDao() throws Exception {
    88          contactInsert = new ContactInsert(getDataSource());
    89          contactUpdate = new ContactUpdate(getDataSource());
    90          contactDelete = new ContactDelete(getDataSource());
    91          contactsAllQuery = new ContactsAllQuery(getDataSource());
    92          principalsAllQuery = new PrincipalsAllQuery(getDataSource());
    93          rolesAllQuery = new RolesAllQuery(getDataSource());
    94          contactsByIdQuery = new ContactsByIdQuery(getDataSource());
    95      }
    96
    97      private String makeObjectIdentity(Contact contact) {
    98          return contact.getClass().getName() + ":" + contact.getId();
    99      }
    100
    101     //~ Inner Classes ==========================================================
    102
    103     protected class AclObjectIdentityByObjectIdentityQuery
    104         extends 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, "INSERT INTO acl_object_identity VALUES (?, ?, ?, ?)");
    121             declareParameter(new SqlParameter(Types.BIGINT));
    122             declareParameter(new SqlParameter(Types.VARCHAR));
    123             declareParameter(new SqlParameter(Types.INTEGER));
    124             declareParameter(new SqlParameter(Types.VARCHAR));
    125             compile();
    126         }
    127
    128         protected int insert(String objectIdentity,
    129             Long parentAclObjectIdentity, String aclClass) {
    130             Object[] objs = new Object[] {null, objectIdentity, parentAclObjectIdentity, aclClass};
    131             super.update(objs);
    132
    133             return getJdbcTemplate().queryForInt("call identity()");
    134         }
    135     }
    136
    137     protected class ContactDelete extends SqlUpdate {
    138         protected ContactDelete(DataSource ds) {
    139             super(ds, "DELETE FROM contacts WHERE id = ?");
    140             declareParameter(new SqlParameter(Types.BIGINT));
    141             compile();
    142         }
    143
    144         protected void delete(Long contactId) {
    145             super.update(contactId.longValue());
    146         }
    147     }
    148
    149     protected class ContactInsert extends SqlUpdate {
    150         protected ContactInsert(DataSource ds) {
    151             super(ds, "INSERT INTO contacts VALUES (?, ?, ?)");
    152             declareParameter(new SqlParameter(Types.BIGINT));
    153             declareParameter(new SqlParameter(Types.VARCHAR));
    154             declareParameter(new SqlParameter(Types.VARCHAR));
    155             compile();
    156         }
    157
    158         protected void insert(Contact contact) {
    159             Object[] objs = new Object[] {contact.getId(), contact.getName(), contact
    160                     .getEmail()};
    161             super.update(objs);
    162         }
    163     }
    164
    165     protected class ContactUpdate extends SqlUpdate {
    166         protected ContactUpdate(DataSource ds) {
    167             super(ds,
    168                 "UPDATE contacts SET contact_name = ?, address = ? WHERE id = ?");
    169             declareParameter(new SqlParameter(Types.VARCHAR));
    170             declareParameter(new SqlParameter(Types.VARCHAR));
    171             declareParameter(new SqlParameter(Types.BIGINT));
    172             compile();
    173         }
    174
    175         protected void update(Contact contact) {
    176             Object[] objs = new Object[] {contact.getName(), contact.getEmail(), contact
    177                     .getId()};
    178             super.update(objs);
    179         }
    180     }
    181
    182     protected class ContactsAllQuery extends MappingSqlQuery {
    183         protected ContactsAllQuery(DataSource ds) {
    184             super(ds, "SELECT id, contact_name, email FROM contacts ORDER BY id");
    185             compile();
    186         }
    187
    188         protected Object mapRow(ResultSet rs, int rownum)
    189             throws SQLException {
    190             Contact contact = new Contact();
    191             contact.setId(new Long(rs.getLong("id")));
    192             contact.setName(rs.getString("contact_name"));
    193             contact.setEmail(rs.getString("email"));
    194
    195             return contact;
    196         }
    197     }
    198
    199     protected class ContactsByIdQuery extends MappingSqlQuery {
    200         protected ContactsByIdQuery(DataSource ds) {
    201             super(ds,
    202                 "SELECT id, contact_name, email FROM contacts WHERE id = ? ORDER BY id");
    203             declareParameter(new SqlParameter(Types.BIGINT));
    204             compile();
    205         }
    206
    207         protected Object mapRow(ResultSet rs, int rownum)
    208             throws SQLException {
    209             Contact contact = new Contact();
    210             contact.setId(new Long(rs.getLong("id")));
    211             contact.setName(rs.getString("contact_name"));
    212             contact.setEmail(rs.getString("email"));
    213
    214             return contact;
    215         }
    216     }
    217
    218     protected class PermissionDelete extends SqlUpdate {
    219         protected PermissionDelete(DataSource ds) {
    220             super(ds,
    221                 "DELETE FROM acl_permission WHERE ACL_OBJECT_IDENTITY = ? AND RECIPIENT = ?");
    222             declareParameter(new SqlParameter(Types.BIGINT));
    223             declareParameter(new SqlParameter(Types.VARCHAR));
    224             compile();
    225         }
    226
    227         protected void delete(Long aclObjectIdentity, String recipient) {
    228             super.update(new Object[] {aclObjectIdentity, recipient});
    229         }
    230     }
    231
    232     protected class PermissionInsert extends SqlUpdate {
    233         protected PermissionInsert(DataSource ds) {
    234             super(ds, "INSERT INTO acl_permission VALUES (?, ?, ?, ?);");
    235             declareParameter(new SqlParameter(Types.BIGINT));
    236             declareParameter(new SqlParameter(Types.BIGINT));
    237             declareParameter(new SqlParameter(Types.VARCHAR));
    238             declareParameter(new SqlParameter(Types.INTEGER));
    239             compile();
    240         }
    241
    242         protected int insert(Long aclObjectIdentity, String recipient,
    243             Integer mask) {
    244             Object[] objs = new Object[] {null, aclObjectIdentity, recipient, mask};
    245             super.update(objs);
    246
    247             return getJdbcTemplate().queryForInt("call identity()");
    248         }
    249     }
    250
    251     protected class PrincipalsAllQuery extends MappingSqlQuery {
    252         protected PrincipalsAllQuery(DataSource ds) {
    253             super(ds, "SELECT username FROM users ORDER BY username");
    254             compile();
    255         }
    256
    257         protected Object mapRow(ResultSet rs, int rownum)
    258             throws SQLException {
    259             return rs.getString("username");
    260         }
    261     }
    262
    263     protected class RolesAllQuery extends MappingSqlQuery {
    264         protected RolesAllQuery(DataSource ds) {
    265             super(ds,
    266                 "SELECT DISTINCT authority FROM authorities ORDER BY authority");
    267             compile();
    268         }
    269
    270         protected Object mapRow(ResultSet rs, int rownum)
    271             throws SQLException {
    272             return rs.getString("authority");
    273         }
    274     }
    275 }