lab1/server/src/main/java/com/amazon/aws/partners/saasfactory/repository/CategoryDaoImpl.java [36:112]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Repository
public class CategoryDaoImpl implements CategoryDao {

    private final static Logger logger = LoggerFactory.getLogger(CategoryDaoImpl.class);

    @Autowired
    private JdbcTemplate jdbc;

    @Override
    public Category getCategory(Integer categoryId) throws Exception {
        logger.info("CategoryDao::getCategory " + categoryId);
        return jdbc.queryForObject("SELECT category_id, category FROM category WHERE category_id = ?", new Object[]{categoryId}, new CategoryRowMapper());
    }

    @Override
    public Category getCategoryByName(String name) throws Exception {
        logger.info("CategoryDao::getCategoryByName " + name);
        return jdbc.queryForObject("SELECT category_id, category FROM category WHERE category = ?", new Object[]{name}, new CategoryRowMapper());
    }

    @Override
    public List<Category> getCategories() throws Exception {
        logger.info("CategoryDao::getCategories");
        List<Category> categories = jdbc.query("SELECT category_id, category FROM category", new CategoryRowMapper());
        if (categories == null) {
            categories = Collections.emptyList();
        }
        logger.info("CategoryDao::getCategories returning " + categories.size() + " categories");
        return categories;
    }

    @Override
    public Category saveCategory(Category category) throws Exception {
        logger.info("CategoryDao::saveCategory " + category);
        if (category.getId() != null && category.getId() > 0) {
            return updateCategory(category);
        } else {
            return insertCategory(category);
        }
    }

    private Category insertCategory(Category category) throws Exception {
        logger.info("CategoryDao::insertCategory " + category);
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbc.update(connection -> {
            PreparedStatement ps = connection.prepareStatement("INSERT INTO category (category) VALUES (?)", Statement.RETURN_GENERATED_KEYS );
            ps.setString(1, category.getName());
            return ps;
        }, keyHolder);
        if (!keyHolder.getKeys().isEmpty()) {
            category.setId((Integer) keyHolder.getKeys().get("category_id"));
        } else {
            category.setId(keyHolder.getKey().intValue());
        }
        return category;
    }

    private Category updateCategory(Category category) throws Exception {
        logger.info("CategoryDao::updateCategory " + category);
        jdbc.update("UPDATE category SET category = ? WHERE category_id = ?", new Object[]{category.getName(), category.getId()});
        return category;
    }

    @Override
    public Category deleteCategory(Category category) throws Exception {
        logger.info("CategoryDao::deleteCategory " + category);
        int affectedRows = jdbc.update("DELETE FROM category WHERE category_id = ?", new Object[]{category.getId()});
        if (affectedRows != 1) {
            throw new RuntimeException("Delete failed for category " + category.getId());
        }
        return category;
    }

    class CategoryRowMapper implements RowMapper<Category> {
        @Override
        public Category mapRow(ResultSet result, int rowMapper) throws SQLException {
            return new Category(result.getInt("category_id"), result.getString("category"));
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



lab2/server/src/main/java/com/amazon/aws/partners/saasfactory/repository/CategoryDaoImpl.java [36:112]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Repository
public class CategoryDaoImpl implements CategoryDao {

    private final static Logger logger = LoggerFactory.getLogger(CategoryDaoImpl.class);

    @Autowired
    private JdbcTemplate jdbc;

    @Override
    public Category getCategory(Integer categoryId) throws Exception {
        logger.info("CategoryDao::getCategory " + categoryId);
        return jdbc.queryForObject("SELECT category_id, category FROM category WHERE category_id = ?", new Object[]{categoryId}, new CategoryRowMapper());
    }

    @Override
    public Category getCategoryByName(String name) throws Exception {
        logger.info("CategoryDao::getCategoryByName " + name);
        return jdbc.queryForObject("SELECT category_id, category FROM category WHERE category = ?", new Object[]{name}, new CategoryRowMapper());
    }

    @Override
    public List<Category> getCategories() throws Exception {
        logger.info("CategoryDao::getCategories");
        List<Category> categories = jdbc.query("SELECT category_id, category FROM category", new CategoryRowMapper());
        if (categories == null) {
            categories = Collections.emptyList();
        }
        logger.info("CategoryDao::getCategories returning " + categories.size() + " categories");
        return categories;
    }

    @Override
    public Category saveCategory(Category category) throws Exception {
        logger.info("CategoryDao::saveCategory " + category);
        if (category.getId() != null && category.getId() > 0) {
            return updateCategory(category);
        } else {
            return insertCategory(category);
        }
    }

    private Category insertCategory(Category category) throws Exception {
        logger.info("CategoryDao::insertCategory " + category);
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbc.update(connection -> {
            PreparedStatement ps = connection.prepareStatement("INSERT INTO category (category) VALUES (?)", Statement.RETURN_GENERATED_KEYS );
            ps.setString(1, category.getName());
            return ps;
        }, keyHolder);
        if (!keyHolder.getKeys().isEmpty()) {
            category.setId((Integer) keyHolder.getKeys().get("category_id"));
        } else {
            category.setId(keyHolder.getKey().intValue());
        }
        return category;
    }

    private Category updateCategory(Category category) throws Exception {
        logger.info("CategoryDao::updateCategory " + category);
        jdbc.update("UPDATE category SET category = ? WHERE category_id = ?", new Object[]{category.getName(), category.getId()});
        return category;
    }

    @Override
    public Category deleteCategory(Category category) throws Exception {
        logger.info("CategoryDao::deleteCategory " + category);
        int affectedRows = jdbc.update("DELETE FROM category WHERE category_id = ?", new Object[]{category.getId()});
        if (affectedRows != 1) {
            throw new RuntimeException("Delete failed for category " + category.getId());
        }
        return category;
    }

    class CategoryRowMapper implements RowMapper<Category> {
        @Override
        public Category mapRow(ResultSet result, int rowMapper) throws SQLException {
            return new Category(result.getInt("category_id"), result.getString("category"));
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



