Changeset 1303


Ignore:
Timestamp:
05/16/11 07:32:54 (13 years ago)
Author:
twagoo
Message:

Made AbstractDescriptionDao? generic, RowMapper? inner class

Added some more tests

Location:
ComponentRegistry/trunk/ComponentRegistry/src
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/AbstractDescriptionDao.java

    r1300 r1303  
    1212import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
    1313import clarin.cmdi.componentregistry.model.AbstractDescription;
    14 import org.springframework.jdbc.core.SingleColumnRowMapper;
    1514import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper;
    1615import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
     
    2120 * @author Twan Goosen <twan.goosen@mpi.nl>
    2221 */
    23 public abstract class AbstractDescriptionDao extends SimpleJdbcDaoSupport {
     22public abstract class AbstractDescriptionDao<T extends AbstractDescription> extends SimpleJdbcDaoSupport {
    2423
    2524    private final static Logger LOG = LoggerFactory.getLogger(AbstractDescriptionDao.class);
    2625
    2726    protected abstract String getTableName();
    28 
    2927    protected abstract String getCMDIdColumn();
    3028
     29    /**
     30     * Class object required to instantiate new description domain objects
     31     */
     32    private Class<T> _class;
     33
     34    protected AbstractDescriptionDao(Class<T> _class) {
     35        this._class = _class;
     36    }
    3137    /**
    3238     *
     
    6874    }
    6975
    70     protected <T extends AbstractDescription> List<T> getPublicDescriptions(final Class<T> _class) {
    71         String select = "select name, description, " + getCMDIdColumn()  + " from " + getTableName();
     76    /**
     77     *
     78     * @return All descriptions in the public space
     79     */
     80    protected List<T> getPublicDescriptions() {
     81        String select = "select name, description, " + getCMDIdColumn() + " from " + getTableName() + " where is_public = TRUE";
     82        return getSimpleJdbcTemplate().query(select, new AbstractDescriptionRowMapper());
     83    }
    7284
    73         ParameterizedRowMapper<T> rowMapper = new ParameterizedRowMapper<T>() {
     85    private class AbstractDescriptionRowMapper<T> implements ParameterizedRowMapper<T> {
    7486
    75             @Override
    76             public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
    77                 try {
    78                     AbstractDescription cd = (AbstractDescription) _class.newInstance();
    79                     cd.setName(rs.getString("name"));
    80                     cd.setDescription(rs.getString("description"));
    81                     cd.setId(rs.getString(getCMDIdColumn()));
    82                     return (T) cd;
    83                 } catch (InstantiationException ex) {
    84                     LOG.error("Error in row mapping", ex);
    85                 } catch (IllegalAccessException ex) {
    86                     LOG.error("Error in row mapping", ex);
    87                 }
    88                 return null;
     87        @Override
     88        public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
     89            try {
     90                AbstractDescription newDescription = (AbstractDescription) _class.newInstance();
     91                newDescription.setName(rs.getString("name"));
     92                newDescription.setDescription(rs.getString("description"));
     93                newDescription.setId(rs.getString(getCMDIdColumn()));
     94                return (T) newDescription;
     95            } catch (InstantiationException ex) {
     96                LOG.error("Error in row mapping", ex);
     97            } catch (IllegalAccessException ex) {
     98                LOG.error("Error in row mapping", ex);
    8999            }
    90         };
    91 
    92         return getSimpleJdbcTemplate().query(select, rowMapper);
    93     }
     100            return null;
     101        }
     102    };
    94103}
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentDescriptionDao.java

    r1300 r1303  
    22
    33import clarin.cmdi.componentregistry.model.ComponentDescription;
    4 import java.sql.ResultSet;
    5 import java.sql.SQLException;
    64import java.util.List;
    7 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
    85import static clarin.cmdi.componentregistry.impl.database.ComponentDescriptionDatabase.*;
    96
     
    129 * @author Twan Goosen <twan.goosen@mpi.nl>
    1310 */
    14 public class ComponentDescriptionDao extends AbstractDescriptionDao {
     11public class ComponentDescriptionDao extends AbstractDescriptionDao<ComponentDescription> {
     12
     13    public ComponentDescriptionDao() {
     14        super(ComponentDescription.class);
     15    }
    1516
    1617    @Override
     
    2526
    2627    public List<ComponentDescription> getPublicComponentDescriptions() {
    27         return getPublicDescriptions(ComponentDescription.class);
     28        return getPublicDescriptions();
    2829    }
    2930}
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ProfileDescriptionDao.java

    r1300 r1303  
    99 * @author Twan Goosen <twan.goosen@mpi.nl>
    1010 */
    11 public class ProfileDescriptionDao extends AbstractDescriptionDao {
     11public class ProfileDescriptionDao extends AbstractDescriptionDao<ProfileDescription> {
     12
     13    public ProfileDescriptionDao() {
     14        super(ProfileDescription.class);
     15    }
    1216
    1317    @Override
     
    2226
    2327    public List<ProfileDescription> getPublicProfileDescriptions() {
    24         return getPublicDescriptions(ProfileDescription.class);
     28        return getPublicDescriptions();
    2529    }
    2630}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/AbstractDescriptionDaoTest.java

    r1298 r1303  
    11package clarin.cmdi.componentregistry.impl.database;
    22
    3 import clarin.cmdi.componentregistry.components.CMDComponentSpec;
     3import clarin.cmdi.componentregistry.model.AbstractDescription;
    44import clarin.cmdi.componentregistry.model.ComponentDescription;
    55import clarin.cmdi.componentregistry.rest.RegistryTestHelper;
     6import java.util.List;
    67import org.junit.runner.RunWith;
    78import org.junit.Test;
     
    4142        getDao().insertComponent(description, testComponent);
    4243    }
     44
     45    @Test
     46    public void testGetPublicComponents() throws Exception {
     47        List<AbstractDescription> descriptions =  getDao().getPublicDescriptions();
     48        assertNotNull(descriptions);
     49    }
    4350}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentDescriptionDaoTest.java

    r1298 r1303  
    1919        resetDatabase(jdbcTemplate);
    2020        createTableComponentDescription(jdbcTemplate);
    21         createTableProfileDescription(jdbcTemplate);
    2221        createTableXmlContent(jdbcTemplate);
    2322    }
Note: See TracChangeset for help on using the changeset viewer.