Changeset 1300


Ignore:
Timestamp:
05/13/11 14:21:24 (13 years ago)
Author:
twagoo
Message:

Dao's: abstracted getPublicDescriptions for Profile/Component?.
DbImpl?: implemented getMDProfile(), register() now works for components

testRegisterComponent() now (should) succeed

Location:
ComponentRegistry/trunk/ComponentRegistry/src
Files:
7 edited

Legend:

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

    r1298 r1300  
    11package clarin.cmdi.componentregistry.impl.database;
    22
     3import org.slf4j.LoggerFactory;
     4import org.slf4j.Logger;
     5import java.sql.SQLException;
     6import java.sql.ResultSet;
     7import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
     8import java.util.List;
    39import java.util.Collections;
    410import java.util.Map;
     
    612import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
    713import clarin.cmdi.componentregistry.model.AbstractDescription;
     14import org.springframework.jdbc.core.SingleColumnRowMapper;
     15import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper;
    816import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
    917import static clarin.cmdi.componentregistry.impl.database.ComponentDescriptionDatabase.*;
     
    1422 */
    1523public abstract class AbstractDescriptionDao extends SimpleJdbcDaoSupport {
     24
     25    private final static Logger LOG = LoggerFactory.getLogger(AbstractDescriptionDao.class);
    1626
    1727    protected abstract String getTableName();
     
    2838                + " join " + getTableName() + " on " + TABLE_XML_CONTENT + "." + COLUMN_ID + " = " + getTableName() + ".content_id"
    2939                + " where " + getTableName() + "." + getCMDIdColumn() + " = :id";
    30         return getSimpleJdbcTemplate().queryForObject(select, String.class, cmdId);
     40
     41
     42        List<String> result = getSimpleJdbcTemplate().query(select, new ParameterizedSingleColumnRowMapper<String>(), cmdId);
     43        if (result.size() > 0) {
     44            return result.get(0);
     45        } else {
     46            return null;
     47        }
    3148    }
    3249
     
    4562        params.put("is_public", Boolean.TRUE);
    4663        params.put("is_deleted", Boolean.FALSE);
    47         params.put(getCMDIdColumn(), "clarin_" + contentId);
     64        params.put(getCMDIdColumn(), description.getId());
    4865        params.put("name", description.getName());
    4966        params.put("description", description.getDescription());
    5067        return insertDescription.executeAndReturnKey(params);
    5168    }
     69
     70    protected <T extends AbstractDescription> List<T> getPublicDescriptions(final Class<T> _class) {
     71        String select = "select name, description, " + getCMDIdColumn()  + " from " + getTableName();
     72
     73        ParameterizedRowMapper<T> rowMapper = new ParameterizedRowMapper<T>() {
     74
     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;
     89            }
     90        };
     91
     92        return getSimpleJdbcTemplate().query(select, rowMapper);
     93    }
    5294}
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentDescriptionDao.java

    r1297 r1300  
    2525
    2626    public List<ComponentDescription> getPublicComponentDescriptions() {
    27         String select = "select name, description from " + TABLE_COMPONENT_DESCRIPTION;
    28 
    29         ParameterizedRowMapper<ComponentDescription> rowMapper = new ParameterizedRowMapper<ComponentDescription>() {
    30 
    31             @Override
    32             public ComponentDescription mapRow(ResultSet rs, int rowNumber) throws SQLException {
    33                 ComponentDescription cd = new ComponentDescription();
    34                 cd.setName(rs.getString("name"));
    35                 cd.setDescription(rs.getString("description"));
    36                 return cd;
    37             }
    38         };
    39 
    40         return getSimpleJdbcTemplate().query(select, rowMapper);
     27        return getPublicDescriptions(ComponentDescription.class);
    4128    }
    4229}
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java

    r1297 r1300  
    22
    33import clarin.cmdi.componentregistry.ComponentRegistry;
     4import clarin.cmdi.componentregistry.ComponentRegistryUtils;
    45import clarin.cmdi.componentregistry.DeleteFailedException;
    56import clarin.cmdi.componentregistry.MDMarshaller;
     
    3031
    3132    private String user;
    32    
    3333    @Autowired
    3434    private ProfileDescriptionDao profileDescriptionDao;
     
    4141     * @see setUser
    4242     */
    43     public ComponentRegistryDbImpl(){
    44 
     43    public ComponentRegistryDbImpl() {
    4544    }
    4645
     
    8382    @Override
    8483    public CMDComponentSpec getMDProfile(String id) {
    85         throw new UnsupportedOperationException("Not supported yet.");
     84        return getMDComponent(id, profileDescriptionDao);
    8685    }
    8786
    8887    @Override
    8988    public CMDComponentSpec getMDComponent(String id) {
    90         try {
    91             String xml = componentDescriptionDao.getContent(id);
    92             InputStream is = new ByteArrayInputStream(xml.getBytes());
    93             return MDMarshaller.unmarshal(CMDComponentSpec.class, is, MDMarshaller.getCMDComponentSchema());
    94         } catch (JAXBException ex) {
    95             Logger.getLogger(ComponentRegistryDbImpl.class.getName()).log(Level.SEVERE, null, ex);
     89        return getMDComponent(id, componentDescriptionDao);
     90    }
     91
     92    private CMDComponentSpec getMDComponent(String id, AbstractDescriptionDao dao) {
     93        String xml = dao.getContent(id);
     94        if (xml != null) {
     95            try {
     96                InputStream is = new ByteArrayInputStream(xml.getBytes());
     97                return MDMarshaller.unmarshal(CMDComponentSpec.class, is, MDMarshaller.getCMDComponentSchema());
     98            } catch (JAXBException ex) {
     99                Logger.getLogger(ComponentRegistryDbImpl.class.getName()).log(Level.SEVERE, null, ex);
     100            }
    96101        }
    97102        return null;
     
    99104
    100105    @Override
    101     public int register(AbstractDescription desc, CMDComponentSpec spec) {
     106    public int register(AbstractDescription description, CMDComponentSpec spec) {
     107        ComponentRegistryUtils.enrichSpecHeader(spec, description);
    102108        try {
    103109            OutputStream os = new ByteArrayOutputStream();
    104110            MDMarshaller.marshal(spec, os);
    105111            String xml = os.toString();
    106             if (!desc.isProfile()) {
    107                 componentDescriptionDao.insertComponent(desc, xml);
     112            if (!description.isProfile()) {
     113                componentDescriptionDao.insertComponent(description, xml);
    108114            }
    109115            return 0;
     
    171177    }
    172178
    173     public void setPublic(){
     179    public void setPublic() {
    174180        this.user = null;
    175181    }
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ProfileDescriptionDao.java

    r1297 r1300  
    33import clarin.cmdi.componentregistry.model.ProfileDescription;
    44import java.util.List;
    5 import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
    6 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
    75import static clarin.cmdi.componentregistry.impl.database.ComponentDescriptionDatabase.*;
    86
     
    2321    }
    2422
    25     List<ProfileDescription> getPublicProfileDescriptions() {
    26         String select = "select name, description from profile_description";
    27 
    28         ParameterizedRowMapper<ProfileDescription> rowMapper = new ParameterizedBeanPropertyRowMapper<ProfileDescription>();
    29 
    30         return getSimpleJdbcTemplate().query(select, rowMapper);
     23    public List<ProfileDescription> getPublicProfileDescriptions() {
     24        return getPublicDescriptions(ProfileDescription.class);
    3125    }
    3226}
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/model/AbstractDescription.java

    r1087 r1300  
    9595        return href;
    9696    }
    97    
     97
    9898    public void setGroupName(String groupName) {
    9999        this.groupName = groupName;
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDatabase.java

    r1298 r1300  
    3838                + "  is_public boolean NOT NULL,"
    3939                + "  is_deleted boolean DEFAULT false NOT NULL,"
    40                 + "  component_id character varying NOT NULL,"
     40                + "  profile_id character varying NOT NULL,"
    4141                + "  name character varying NOT NULL,"
    4242                + "  description character varying NOT NULL,"
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImplTest.java

    r1298 r1300  
    1313import static org.junit.Assert.assertNull;
    1414import static org.junit.Assert.assertNotNull;
    15 import org.junit.Ignore;
    16 
    1715import org.springframework.beans.factory.annotation.Autowired;
    1816import org.springframework.jdbc.core.JdbcTemplate;
Note: See TracChangeset for help on using the changeset viewer.