package org.pegadi.server.publication; import org.pegadi.model.Article; import org.pegadi.model.DispPage; import org.pegadi.model.DispSection; import org.pegadi.server.ArticleServer; import org.pegadi.server.DispPageServer; 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; import java.util.List; public class DispPageServerImpl implements DispPageServer{ private JdbcTemplate template; private DispPageRowMapper mapper; private DispSectionRowMapper sectionRowMapper; private ArticleServer articleServer; public DispPageServerImpl() { mapper = new DispPageRowMapper(); sectionRowMapper = new DispSectionRowMapper(); } public void setDataSource(DataSource dataSource) { template = new JdbcTemplate(dataSource); } public void setArticleServer(ArticleServer articleServer) { this.articleServer = articleServer; } public List<DispPage> getPagesByDispId(int dispId) { return template.query("SELECT * FROM disppage WHERE dispid=? ORDER BY pagenumber ASC", mapper, dispId); } public DispPage createDispPage(DispPage dispPage) { template.update("INSERT INTO disppage (dispid, pagenumber, sectionid, adonpage, pagetext, photostatus) VALUES(?, ?, ?, ?, ?, ?)", dispPage.getDispId(), dispPage.getPageNumber(), dispPage.getSection()==null?1:dispPage.getSection().getId(), dispPage.isAdOnPage(), dispPage.getText(), dispPage.getPhotoStatus()); return template.queryForObject("SELECT * FROM disppage WHERE dispid=? AND pagenumber=?", mapper, dispPage.getDispId(), dispPage.getPageNumber()); } public void saveDispPage(DispPage dispPage) { template.update("UPDATE disppage SET pagenumber=?, sectionid=?, adonpage=?, pagetext=?, photostatus=? WHERE id=?", dispPage.getPageNumber(), dispPage.getSection().getId(), dispPage.isAdOnPage(), dispPage.getText(), dispPage.getPhotoStatus(), dispPage.getId()); } public void saveDispPages(List<DispPage> dispPages) { for(DispPage dispPage : dispPages) { saveDispPage(dispPage); } } public void deleteDispPage(DispPage dispPage) { template.update("DELETE FROM disppage WHERE id=?", dispPage.getId()); } private class DispPageRowMapper implements ParameterizedRowMapper<DispPage> { public DispPage mapRow(ResultSet rs, int rowNum) throws SQLException { DispPage dispPage = new DispPage(); dispPage.setId(rs.getInt("id")); dispPage.setDispId(rs.getInt("dispid")); dispPage.setPageNumber(rs.getInt("pagenumber")); dispPage.setSection(getDispSectionById(rs.getInt("sectionid"))); dispPage.setAdOnPage(rs.getBoolean("adonpage")); dispPage.setText(rs.getString("pagetext")); dispPage.setPhotoSatus(rs.getString("photostatus")); dispPage.setArticles(articleServer.getArticlesByPageID(dispPage.getId())); return dispPage; } } public List<DispSection> getDispSections() { return template.query("SELECT * FROM dispsection", sectionRowMapper); } public List<DispSection> getActiveDispSections() { return template.query("SELECT * FROM dispsection WHERE active=true", sectionRowMapper); } public DispSection getDispSectionById(int sectionId) { return template.queryForObject("SELECT * FROM dispsection WHERE id=?", sectionRowMapper, sectionId); } public void createDispSection(DispSection dispSection) { template.update("INSERT INTO dispsection (active, sectionname) VALUES(?, ?)", dispSection.isActive(), dispSection.getSectionName()); } public void saveDispSection(DispSection dispSection) { template.update("UPDATE dispsection SET active=?, sectionname=? WHERE id=?", dispSection.isActive(), dispSection.getSectionName(), dispSection.getId()); } public void addArticleToPage(DispPage page, Article article) { template.update("INSERT INTO disppagearticles (pageid, articleid) VALUES (?, ?)", page.getId(), article.getId()); } public void removeArticleFromPage(DispPage page, Article article) { template.update("DELETE FROM disppagearticles WHERE pageid=? AND articleid=?", page.getId(), article.getId()); } private class DispSectionRowMapper implements ParameterizedRowMapper<DispSection> { public DispSection mapRow(ResultSet rs, int rowNum) throws SQLException { DispSection dispSection = new DispSection(); dispSection.setId(rs.getInt("id")); dispSection.setActive(rs.getBoolean("active")); dispSection.setSectionName(rs.getString("sectionname")); return dispSection; } } }