001: package org.outerj.daisy.sync.dao;
002:
003: import java.sql.ResultSet;
004: import java.sql.SQLException;
005: import java.util.ArrayList;
006: import java.util.List;
007: import java.util.logging.Logger;
008:
009: import org.outerj.daisy.sync.Attribute;
010: import org.outerj.daisy.sync.AttributeImpl;
011: import org.outerj.daisy.sync.Entity;
012: import org.outerj.daisy.sync.EntityImpl;
013: import org.springframework.dao.EmptyResultDataAccessException;
014: import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
015: import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
016:
017: public class ExternalDBEntityDao implements ExternalEntityDao {
018: private final String entitySql = "select last_modified from entity where entity_name = ? and ext_id = ?";
019:
020: private final String attributeSql = "select attribute_name, value from entity join entity_attribute on entity.id = entity_attribute.entity_id where entity_name = ? and ext_id = ?";
021:
022: private final String getAssociatedEntitiesSql = "select entity.ext_id from entity join entity_attribute as attr on entity.id = attr.entity_id where entity_name = ? and attr.attribute_name = ? and attr.value = ?";
023:
024: private SimpleJdbcTemplate jdbcTemplate;
025:
026: private Logger logger;
027:
028: public ExternalDBEntityDao(SimpleJdbcTemplate jdbcTemplate) {
029: this .jdbcTemplate = jdbcTemplate;
030: }
031:
032: public List<Long> getEntityIds(final String entityName) {
033: String sql = "select ext_id from entity where entity_name = ?";
034:
035: ParameterizedRowMapper<Long> rowMapper = new ParameterizedRowMapper<Long>() {
036:
037: public Long mapRow(ResultSet rs, int rowNum)
038: throws SQLException {
039: return rs.getLong("ext_id");
040: }
041:
042: };
043:
044: return jdbcTemplate.query(sql, rowMapper,
045: new Object[] { entityName });
046: }
047:
048: public List<Entity> getEntity(final String entityName,
049: final long externalId) {
050: List<Entity> entities = new ArrayList<Entity>();
051:
052: ParameterizedRowMapper<Entity> entityMapper = new ParameterizedRowMapper<Entity>() {
053: public Entity mapRow(ResultSet rs, int rowNum)
054: throws SQLException {
055: Entity entity = new EntityImpl();
056: entity.setName(entityName);
057: entity.setExternalId(externalId);
058: entity.setExternalLastModified(rs
059: .getDate("last_modified"));
060: return entity;
061: }
062: };
063:
064: Object[] args = new Object[] { entityName, externalId };
065: Entity entity = null;
066: try {
067: entity = jdbcTemplate.queryForObject(entitySql,
068: entityMapper, args);
069: } catch (EmptyResultDataAccessException e) {
070: // do nothing just return null
071: }
072: if (entity != null) {
073: entity.setAttributes(jdbcTemplate.query(attributeSql,
074: new AttributeMapper(entity), args));
075: entities.add(entity);
076: }
077: return entities;
078: }
079:
080: public List<Entity> getAssociatedEntities(long externalId,
081: final String associatedEntityName, String joinKey) {
082: ParameterizedRowMapper<Entity> entityMapper = new ParameterizedRowMapper<Entity>() {
083: public Entity mapRow(ResultSet rs, int rowNum)
084: throws SQLException {
085: long extId = rs.getLong("entity.ext_id");
086: List<Entity> entities = getEntity(associatedEntityName,
087: extId);
088: if (entities.size() > 0)
089: return entities.get(0);
090: else
091: return null;
092: }
093: };
094: return jdbcTemplate.query(getAssociatedEntitiesSql,
095: entityMapper, new Object[] { associatedEntityName,
096: joinKey, Long.toString(externalId) });
097: }
098:
099: private class AttributeMapper implements
100: ParameterizedRowMapper<Attribute> {
101: private Entity entity;
102:
103: public AttributeMapper(Entity entity) {
104: this .entity = entity;
105: }
106:
107: public Attribute mapRow(ResultSet rs, int rowNum)
108: throws SQLException {
109: Attribute attribute = new AttributeImpl(rs
110: .getString("attribute_name"), rs.getString("value"));
111: entity.addAttribute(attribute);
112: return attribute;
113: }
114: }
115:
116: public Logger getLogger() {
117: return logger;
118: }
119:
120: public void setLogger(Logger logger) {
121: this.logger = logger;
122: }
123:
124: }
|