Advertisement
purluno

purluno.springrain.book.BookService

Apr 24th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.51 KB | None | 0 0
  1. package purluno.springrain.book
  2.  
  3. import javax.annotation.Resource
  4.  
  5. import org.hibernate.Session
  6. import org.springframework.stereotype.Service
  7.  
  8. import purluno.springrain.HibernateHelper
  9.  
  10. @Service
  11. class BookService {
  12.     static final int DEFAULT_PAGE_SIZE = 3
  13.  
  14.     int pageSize = DEFAULT_PAGE_SIZE
  15.  
  16.     /**
  17.      * Hibernate 지원 객체
  18.      */
  19.     @Resource
  20.     HibernateHelper hib
  21.  
  22.     /**
  23.      * 특정 id의 책 정보를 가져온다.
  24.      *
  25.      * @param id
  26.      *     책 정보 ID   
  27.      * @return
  28.      *     책 정보 객체. 또는, 책이 없을 경우 null.
  29.      */
  30.     Book getById(long id) {
  31.         hib.transactional { Session session ->
  32.             session.get(Book, id)
  33.         }
  34.     }
  35.  
  36.     /**
  37.      * 특정 ISBN을 가진 책 정보를 가져온다.
  38.      *
  39.      * @param isbn
  40.      *     ISBN
  41.      * @return
  42.      *     책 정보 객체. 또는, 책이 없을 경우 null.
  43.      */
  44.     Book getByIsbn(String isbn) {
  45.         assert isbn != null, "ISBN은 null이 아니어야 합니다."
  46.         assert !isbn.empty, "ISBN은 빈 문자열이 아니어야 합니다."
  47.         hib.transactional { Session session ->
  48.             session.bySimpleNaturalId(Book).load(isbn)
  49.         }
  50.     }
  51.  
  52.     /**
  53.      * 책 정보의 총 개수를 구한다.
  54.      */
  55.     int count() {
  56.         hib.transactional { Session session ->
  57.             def hql = "select count(*) from Book"
  58.             session.createQuery(hql).uniqueResult()
  59.         }
  60.     }
  61.  
  62.     /**
  63.      * 책 정보 목록을 가져온다.
  64.      * 인자로 받는 params로부터 요청 페이지(page)를 입력받으며,
  65.      * params에 개수(count), 페이지 수(numPages), 현재 페이지(page), 페이지 크기(pageSize)를
  66.      * 지정한다.
  67.      */
  68.     List<Book> list(Map params) {
  69.         hib.transactional { Session session ->
  70.             def c = count()
  71.             def numPages = (c - 1).intdiv(pageSize) + 1
  72.             def page = Math.min(Math.max(1, params["page"]), numPages)
  73.             def first = (page - 1) * pageSize
  74.             params["count"] = c
  75.             params["numPages"] = numPages
  76.             params["page"] = page
  77.             params["pageSize"] = pageSize
  78.             params["startPage"] = Math.max(1, page - 5)
  79.             params["endPage"] = Math.min(page + 5, numPages)
  80.             params["first"] = first
  81.             def hql = "from Book order by dateModified desc"
  82.             session.createQuery(hql)
  83.                 .setFirstResult(first)
  84.                 .setMaxResults(pageSize)
  85.                 .list()
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * 책 정보를 생성한다.
  91.      *
  92.      * @param book
  93.      */
  94.     void save(Book book) {
  95.         if (book.isbn?.empty) {  // ISBN이 비어있으면
  96.             book.isbn = null     // 중복될 수 있도록 null 처리해준다.
  97.         }
  98.         hib.transactional { Session session ->
  99.             def now = new Date()
  100.             book.dateCreated = now
  101.             book.dateModified = now
  102.             session.save(book)
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * 책 정보를 갱신한다.
  108.      *
  109.      * @param input
  110.      */
  111.     void update(Book input) {
  112.         assert input.id != null, "ID는 null이 아니어야 합니다."
  113.         if (input.isbn?.empty) {  // ISBN이 비어있으면
  114.             input.isbn = null     // 중복될 수 있도록 null 처리해준다.
  115.         }
  116.         hib.transactional { Session session ->
  117.             Book book = session.get(Book, input.id)
  118.             book.dateModified = new Date()
  119.             book.isbn = input.isbn
  120.             book.title = input.title
  121.             book.author = input.author
  122.             book.year = input.year
  123.             book.intro = input.intro
  124.             book.authorIntro = input.authorIntro
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * 책 정보를 삭제한다.
  130.      *
  131.      * @param id
  132.      *     삭제할 책 정보의 ID
  133.      */
  134.     void delete(long id) {
  135.         hib.transactional { Session session ->
  136.             session.delete(new Book(id: id))
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement