daily pastebin goal
67%
SHARE
TWEET

Untitled

a guest May 16th, 2018 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package cz.muni.fi.books;
  2.  
  3. import org.springframework.jdbc.core.JdbcTemplate;
  4. import org.springframework.jdbc.core.RowMapper;
  5. import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
  6. import org.springframework.jdbc.core.namedparam.SqlParameterSource;
  7. import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
  8. import org.springframework.transaction.annotation.Transactional;
  9.  
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12.  
  13. import javax.sql.DataSource;
  14. import java.util.List;
  15.  
  16. /**
  17.  * cz.muni.fi.books.Book manager class
  18.  *
  19.  * @author Martin Kacenga
  20.  */
  21. public class BookManagerImpl implements BookManager {
  22.     private JdbcTemplate jdbc;
  23.  
  24.     final static Logger log = LoggerFactory.getLogger(BookManagerImpl.class);
  25.     public BookManagerImpl(DataSource dataSource) {
  26.         this.jdbc = new JdbcTemplate(dataSource);
  27.     }
  28.  
  29.     @Override
  30.     public void createBook(Book book) {
  31.         if (book == null) {
  32.             log.error("Null cannot he passed as argument.");
  33.             throw new NullPointerException();
  34.         }
  35.  
  36.         if (book.getName().equals("") || book.getAuthorName().equals("")) {
  37.             log.error("Essential arguments from book are missing.");
  38.             throw new IllegalArgumentException();
  39.         }
  40.  
  41.         SimpleJdbcInsert insertBook = new SimpleJdbcInsert(jdbc)
  42.                 .withTableName("books").usingGeneratedKeyColumns("id");
  43.  
  44.         SqlParameterSource parameters = new MapSqlParameterSource()
  45.                 .addValue("name", book.getName())
  46.                 .addValue("author", book.getAuthorName())
  47.                 .addValue("publisher", book.getPublisher());
  48.         log.debug("Creating book {}", book.getName());
  49.         Number id = insertBook.executeAndReturnKey(parameters);
  50.         book.setId(id.longValue());
  51.     }
  52.  
  53.     @Override
  54.     public Book getBookById(Long id) {
  55.         if (id == null) {
  56.             log.error("Book has no ID.");
  57.             throw new NullPointerException();
  58.         }
  59.         log.debug("Getting book with {} ID", id);
  60.         return jdbc.queryForObject("SELECT * FROM books WHERE id=?", bookMapper, id);
  61.     }
  62.  
  63.  
  64.     private RowMapper<Book> bookMapper = (rs, rowNum) ->
  65.             new Book(rs.getLong("id"), rs.getString("name"), rs.getString("author"), rs.getString("publisher"));
  66.  
  67.     @Transactional
  68.     @Override
  69.     public List<Book> getAllBooks()  {
  70.         log.debug("getting all books");
  71.         return jdbc.query("SELECT * FROM books", bookMapper);
  72.     }
  73.  
  74.     @Override
  75.     public void updateBook(Book book) {
  76.         throwPossibleExceptions(book);
  77.         log.debug("Updating {0} book with {1}ID.", book.getName(), book.getId());
  78.         jdbc.update("UPDATE books set name=?,author=? where id=?",
  79.                 book.getName(),
  80.                 book.getAuthorName(),
  81.                 book.getId());
  82.     }
  83.  
  84.     @Override
  85.     public void deleteBook(Book book) {
  86.         throwPossibleExceptions(book);
  87.         log.debug("Deleting {0} book with {1}ID.", book.getName(), book.getId());
  88.         jdbc.update("DELETE FROM books WHERE id=?", book.getId());
  89.     }
  90.  
  91.     private void throwPossibleExceptions(Book book) {
  92.         if (book == null){
  93.             log.error("Cannot do this operation with null argument.");
  94.             throw new NullPointerException();
  95.         }
  96.         if (book.getId() == null) {
  97.             log.error("ID cannot be set to NULL.");
  98.             throw new IllegalArgumentException();
  99.         }
  100.     }
  101. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top