package org.pegadi.server.stylesheet; import org.pegadi.model.PublishingMediaEnum; import org.pegadi.model.Section; import org.pegadi.model.Stylesheet; import org.pegadi.server.ArticleServer; import org.pegadi.server.ArticleTypeServer; import org.pegadi.server.StylesheetServer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.simple.ParameterizedRowMapper; import javax.sql.DataSource; import java.sql.ResultSet; import java.sql.SQLException; /** * User: jan-preben * Date: Sep 26, 2010 */ public class StylesheetServerImpl implements StylesheetServer { private final Logger log = LoggerFactory.getLogger(getClass()); private JdbcTemplate template; private StylesheetRowMapper stylesheetRowMapper = new StylesheetRowMapper(); private ArticleTypeServer articleTypeServer; private ArticleServer articleServer; public void setDataSource(DataSource dataSource) { template = new JdbcTemplate(dataSource); } public void setArticleTypeServer(ArticleTypeServer articleTypeServer) { this.articleTypeServer = articleTypeServer; } public void setArticleServer(ArticleServer articleServer) { this.articleServer = articleServer; } /* public Stylesheet getStylesheetByMediaId(Integer publishingMediaId) { try { return template.queryForObject("SELECT * FROM Stylesheets Where ref_publishingMedia=?", stylesheetRowMapper, publishingMediaId); } catch(EmptyResultDataAccessException e) { log.info("No stylesheet found for media with id:" + publishingMediaId); return null; } }*/ public Stylesheet getStylesheet(PublishingMediaEnum publishingMedia, Section section) { Stylesheet stylesheet; try { stylesheet = template.queryForObject("SELECT * FROM Stylesheets Where publishingMedia=? AND ref_articleDepartment=?", stylesheetRowMapper, publishingMedia.name(), section.getId()); } catch(EmptyResultDataAccessException e) { log.info("No stylesheet found for media with name:" + publishingMedia.name()); try { stylesheet = template.queryForObject("SELECT * FROM Stylesheets Where publishingMedia=? AND defaultForPublishingMedia = 1", stylesheetRowMapper, publishingMedia.name()); } catch (EmptyResultDataAccessException e1) { String message = "No default stylesheet found for media with name:" + publishingMedia.name(); log.error(message); throw new IllegalStateException(message); } } stylesheet.setArticleType(section); return stylesheet; } private class StylesheetRowMapper implements ParameterizedRowMapper<Stylesheet> { public Stylesheet mapRow(ResultSet rs, int rowNum) throws SQLException { Stylesheet stylesheet = new Stylesheet(); stylesheet.setId(rs.getInt("ID")); stylesheet.setPublishingMedia(PublishingMediaEnum.valueOf(rs.getString("publishingMedia"))); stylesheet.setDescription(rs.getString("description")); stylesheet.setName(rs.getString("name")); stylesheet.setStylesheetURL(rs.getString("stylesheetURL")); return stylesheet; } } }