Java tutorial
/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.book.service.persistence.impl; import aQute.bnd.annotation.ProviderType; import com.liferay.book.exception.NoSuchBookException; import com.liferay.book.model.Book; import com.liferay.book.model.impl.BookImpl; import com.liferay.book.model.impl.BookModelImpl; import com.liferay.book.service.persistence.BookPersistence; import com.liferay.portal.kernel.dao.orm.EntityCache; import com.liferay.portal.kernel.dao.orm.FinderCache; import com.liferay.portal.kernel.dao.orm.FinderPath; import com.liferay.portal.kernel.dao.orm.Query; import com.liferay.portal.kernel.dao.orm.QueryPos; import com.liferay.portal.kernel.dao.orm.QueryUtil; import com.liferay.portal.kernel.dao.orm.SQLQuery; import com.liferay.portal.kernel.dao.orm.Session; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.security.permission.InlineSQLHelperUtil; import com.liferay.portal.kernel.service.ServiceContext; import com.liferay.portal.kernel.service.ServiceContextThreadLocal; import com.liferay.portal.kernel.service.persistence.CompanyProvider; import com.liferay.portal.kernel.service.persistence.CompanyProviderWrapper; import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl; import com.liferay.portal.kernel.util.OrderByComparator; import com.liferay.portal.kernel.util.StringBundler; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.spring.extender.service.ServiceReference; import java.io.Serializable; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; /** * The persistence implementation for the book service. * * <p> * Caching information and settings can be found in <code>portal.properties</code> * </p> * * @author terry * @see BookPersistence * @see com.liferay.book.service.persistence.BookUtil * @generated */ @ProviderType public class BookPersistenceImpl extends BasePersistenceImpl<Book> implements BookPersistence { /* * NOTE FOR DEVELOPERS: * * Never modify or reference this class directly. Always use {@link BookUtil} to access the book persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class. */ public static final String FINDER_CLASS_NAME_ENTITY = BookImpl.class.getName(); public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY + ".List1"; public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY + ".List2"; public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath( BookModelImpl.ENTITY_CACHE_ENABLED, BookModelImpl.FINDER_CACHE_ENABLED, BookImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]); public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath( BookModelImpl.ENTITY_CACHE_ENABLED, BookModelImpl.FINDER_CACHE_ENABLED, BookImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]); public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(BookModelImpl.ENTITY_CACHE_ENABLED, BookModelImpl.FINDER_CACHE_ENABLED, Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]); public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_AUTHOR = new FinderPath( BookModelImpl.ENTITY_CACHE_ENABLED, BookModelImpl.FINDER_CACHE_ENABLED, BookImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByAuthor", new String[] { String.class.getName(), Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() }); public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_AUTHOR = new FinderPath( BookModelImpl.ENTITY_CACHE_ENABLED, BookModelImpl.FINDER_CACHE_ENABLED, BookImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByAuthor", new String[] { String.class.getName() }, BookModelImpl.AUTHOR_COLUMN_BITMASK); public static final FinderPath FINDER_PATH_COUNT_BY_AUTHOR = new FinderPath(BookModelImpl.ENTITY_CACHE_ENABLED, BookModelImpl.FINDER_CACHE_ENABLED, Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByAuthor", new String[] { String.class.getName() }); /** * Returns all the books where author = ?. * * @param author the author * @return the matching books */ @Override public List<Book> findByAuthor(String author) { return findByAuthor(author, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null); } /** * Returns a range of all the books where author = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param author the author * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @return the range of matching books */ @Override public List<Book> findByAuthor(String author, int start, int end) { return findByAuthor(author, start, end, null); } /** * Returns an ordered range of all the books where author = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param author the author * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @return the ordered range of matching books */ @Override public List<Book> findByAuthor(String author, int start, int end, OrderByComparator<Book> orderByComparator) { return findByAuthor(author, start, end, orderByComparator, true); } /** * Returns an ordered range of all the books where author = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param author the author * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @param retrieveFromCache whether to retrieve from the finder cache * @return the ordered range of matching books */ @Override public List<Book> findByAuthor(String author, int start, int end, OrderByComparator<Book> orderByComparator, boolean retrieveFromCache) { boolean pagination = true; FinderPath finderPath = null; Object[] finderArgs = null; if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) { pagination = false; finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_AUTHOR; finderArgs = new Object[] { author }; } else { finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_AUTHOR; finderArgs = new Object[] { author, start, end, orderByComparator }; } List<Book> list = null; if (retrieveFromCache) { list = (List<Book>) finderCache.getResult(finderPath, finderArgs, this); if ((list != null) && !list.isEmpty()) { for (Book book : list) { if (!Objects.equals(author, book.getAuthor())) { list = null; break; } } } } if (list == null) { StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2)); } else { query = new StringBundler(3); } query.append(_SQL_SELECT_BOOK_WHERE); boolean bindAuthor = false; if (author == null) { query.append(_FINDER_COLUMN_AUTHOR_AUTHOR_1); } else if (author.equals(StringPool.BLANK)) { query.append(_FINDER_COLUMN_AUTHOR_AUTHOR_3); } else { bindAuthor = true; query.append(_FINDER_COLUMN_AUTHOR_AUTHOR_2); } if (orderByComparator != null) { appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator); } else if (pagination) { query.append(BookModelImpl.ORDER_BY_JPQL); } String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); QueryPos qPos = QueryPos.getInstance(q); if (bindAuthor) { qPos.add(author); } if (!pagination) { list = (List<Book>) QueryUtil.list(q, getDialect(), start, end, false); Collections.sort(list); list = Collections.unmodifiableList(list); } else { list = (List<Book>) QueryUtil.list(q, getDialect(), start, end); } cacheResult(list); finderCache.putResult(finderPath, finderArgs, list); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return list; } /** * Returns the first book in the ordered set where author = ?. * * @param author the author * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the first matching book * @throws NoSuchBookException if a matching book could not be found */ @Override public Book findByAuthor_First(String author, OrderByComparator<Book> orderByComparator) throws NoSuchBookException { Book book = fetchByAuthor_First(author, orderByComparator); if (book != null) { return book; } StringBundler msg = new StringBundler(4); msg.append(_NO_SUCH_ENTITY_WITH_KEY); msg.append("author="); msg.append(author); msg.append(StringPool.CLOSE_CURLY_BRACE); throw new NoSuchBookException(msg.toString()); } /** * Returns the first book in the ordered set where author = ?. * * @param author the author * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the first matching book, or <code>null</code> if a matching book could not be found */ @Override public Book fetchByAuthor_First(String author, OrderByComparator<Book> orderByComparator) { List<Book> list = findByAuthor(author, 0, 1, orderByComparator); if (!list.isEmpty()) { return list.get(0); } return null; } /** * Returns the last book in the ordered set where author = ?. * * @param author the author * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the last matching book * @throws NoSuchBookException if a matching book could not be found */ @Override public Book findByAuthor_Last(String author, OrderByComparator<Book> orderByComparator) throws NoSuchBookException { Book book = fetchByAuthor_Last(author, orderByComparator); if (book != null) { return book; } StringBundler msg = new StringBundler(4); msg.append(_NO_SUCH_ENTITY_WITH_KEY); msg.append("author="); msg.append(author); msg.append(StringPool.CLOSE_CURLY_BRACE); throw new NoSuchBookException(msg.toString()); } /** * Returns the last book in the ordered set where author = ?. * * @param author the author * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the last matching book, or <code>null</code> if a matching book could not be found */ @Override public Book fetchByAuthor_Last(String author, OrderByComparator<Book> orderByComparator) { int count = countByAuthor(author); if (count == 0) { return null; } List<Book> list = findByAuthor(author, count - 1, count, orderByComparator); if (!list.isEmpty()) { return list.get(0); } return null; } /** * Returns the books before and after the current book in the ordered set where author = ?. * * @param bookId the primary key of the current book * @param author the author * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the previous, current, and next book * @throws NoSuchBookException if a book with the primary key could not be found */ @Override public Book[] findByAuthor_PrevAndNext(long bookId, String author, OrderByComparator<Book> orderByComparator) throws NoSuchBookException { Book book = findByPrimaryKey(bookId); Session session = null; try { session = openSession(); Book[] array = new BookImpl[3]; array[0] = getByAuthor_PrevAndNext(session, book, author, orderByComparator, true); array[1] = book; array[2] = getByAuthor_PrevAndNext(session, book, author, orderByComparator, false); return array; } catch (Exception e) { throw processException(e); } finally { closeSession(session); } } protected Book getByAuthor_PrevAndNext(Session session, Book book, String author, OrderByComparator<Book> orderByComparator, boolean previous) { StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3) + (orderByComparator.getOrderByFields().length * 3)); } else { query = new StringBundler(3); } query.append(_SQL_SELECT_BOOK_WHERE); boolean bindAuthor = false; if (author == null) { query.append(_FINDER_COLUMN_AUTHOR_AUTHOR_1); } else if (author.equals(StringPool.BLANK)) { query.append(_FINDER_COLUMN_AUTHOR_AUTHOR_3); } else { bindAuthor = true; query.append(_FINDER_COLUMN_AUTHOR_AUTHOR_2); } if (orderByComparator != null) { String[] orderByConditionFields = orderByComparator.getOrderByConditionFields(); if (orderByConditionFields.length > 0) { query.append(WHERE_AND); } for (int i = 0; i < orderByConditionFields.length; i++) { query.append(_ORDER_BY_ENTITY_ALIAS); query.append(orderByConditionFields[i]); if ((i + 1) < orderByConditionFields.length) { if (orderByComparator.isAscending() ^ previous) { query.append(WHERE_GREATER_THAN_HAS_NEXT); } else { query.append(WHERE_LESSER_THAN_HAS_NEXT); } } else { if (orderByComparator.isAscending() ^ previous) { query.append(WHERE_GREATER_THAN); } else { query.append(WHERE_LESSER_THAN); } } } query.append(ORDER_BY_CLAUSE); String[] orderByFields = orderByComparator.getOrderByFields(); for (int i = 0; i < orderByFields.length; i++) { query.append(_ORDER_BY_ENTITY_ALIAS); query.append(orderByFields[i]); if ((i + 1) < orderByFields.length) { if (orderByComparator.isAscending() ^ previous) { query.append(ORDER_BY_ASC_HAS_NEXT); } else { query.append(ORDER_BY_DESC_HAS_NEXT); } } else { if (orderByComparator.isAscending() ^ previous) { query.append(ORDER_BY_ASC); } else { query.append(ORDER_BY_DESC); } } } } else { query.append(BookModelImpl.ORDER_BY_JPQL); } String sql = query.toString(); Query q = session.createQuery(sql); q.setFirstResult(0); q.setMaxResults(2); QueryPos qPos = QueryPos.getInstance(q); if (bindAuthor) { qPos.add(author); } if (orderByComparator != null) { Object[] values = orderByComparator.getOrderByConditionValues(book); for (Object value : values) { qPos.add(value); } } List<Book> list = q.list(); if (list.size() == 2) { return list.get(1); } else { return null; } } /** * Removes all the books where author = ? from the database. * * @param author the author */ @Override public void removeByAuthor(String author) { for (Book book : findByAuthor(author, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) { remove(book); } } /** * Returns the number of books where author = ?. * * @param author the author * @return the number of matching books */ @Override public int countByAuthor(String author) { FinderPath finderPath = FINDER_PATH_COUNT_BY_AUTHOR; Object[] finderArgs = new Object[] { author }; Long count = (Long) finderCache.getResult(finderPath, finderArgs, this); if (count == null) { StringBundler query = new StringBundler(2); query.append(_SQL_COUNT_BOOK_WHERE); boolean bindAuthor = false; if (author == null) { query.append(_FINDER_COLUMN_AUTHOR_AUTHOR_1); } else if (author.equals(StringPool.BLANK)) { query.append(_FINDER_COLUMN_AUTHOR_AUTHOR_3); } else { bindAuthor = true; query.append(_FINDER_COLUMN_AUTHOR_AUTHOR_2); } String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); QueryPos qPos = QueryPos.getInstance(q); if (bindAuthor) { qPos.add(author); } count = (Long) q.uniqueResult(); finderCache.putResult(finderPath, finderArgs, count); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return count.intValue(); } private static final String _FINDER_COLUMN_AUTHOR_AUTHOR_1 = "book.author IS NULL"; private static final String _FINDER_COLUMN_AUTHOR_AUTHOR_2 = "book.author = ?"; private static final String _FINDER_COLUMN_AUTHOR_AUTHOR_3 = "(book.author IS NULL OR book.author = '')"; public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUP = new FinderPath( BookModelImpl.ENTITY_CACHE_ENABLED, BookModelImpl.FINDER_CACHE_ENABLED, BookImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByGroup", new String[] { Long.class.getName(), Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() }); public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUP = new FinderPath( BookModelImpl.ENTITY_CACHE_ENABLED, BookModelImpl.FINDER_CACHE_ENABLED, BookImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByGroup", new String[] { Long.class.getName() }, BookModelImpl.GROUPID_COLUMN_BITMASK | BookModelImpl.AUTHOR_COLUMN_BITMASK); public static final FinderPath FINDER_PATH_COUNT_BY_GROUP = new FinderPath(BookModelImpl.ENTITY_CACHE_ENABLED, BookModelImpl.FINDER_CACHE_ENABLED, Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByGroup", new String[] { Long.class.getName() }); /** * Returns all the books where groupId = ?. * * @param groupId the group ID * @return the matching books */ @Override public List<Book> findByGroup(long groupId) { return findByGroup(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null); } /** * Returns a range of all the books where groupId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param groupId the group ID * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @return the range of matching books */ @Override public List<Book> findByGroup(long groupId, int start, int end) { return findByGroup(groupId, start, end, null); } /** * Returns an ordered range of all the books where groupId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param groupId the group ID * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @return the ordered range of matching books */ @Override public List<Book> findByGroup(long groupId, int start, int end, OrderByComparator<Book> orderByComparator) { return findByGroup(groupId, start, end, orderByComparator, true); } /** * Returns an ordered range of all the books where groupId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param groupId the group ID * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @param retrieveFromCache whether to retrieve from the finder cache * @return the ordered range of matching books */ @Override public List<Book> findByGroup(long groupId, int start, int end, OrderByComparator<Book> orderByComparator, boolean retrieveFromCache) { boolean pagination = true; FinderPath finderPath = null; Object[] finderArgs = null; if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) { pagination = false; finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUP; finderArgs = new Object[] { groupId }; } else { finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUP; finderArgs = new Object[] { groupId, start, end, orderByComparator }; } List<Book> list = null; if (retrieveFromCache) { list = (List<Book>) finderCache.getResult(finderPath, finderArgs, this); if ((list != null) && !list.isEmpty()) { for (Book book : list) { if ((groupId != book.getGroupId())) { list = null; break; } } } } if (list == null) { StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2)); } else { query = new StringBundler(3); } query.append(_SQL_SELECT_BOOK_WHERE); query.append(_FINDER_COLUMN_GROUP_GROUPID_2); if (orderByComparator != null) { appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator); } else if (pagination) { query.append(BookModelImpl.ORDER_BY_JPQL); } String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); QueryPos qPos = QueryPos.getInstance(q); qPos.add(groupId); if (!pagination) { list = (List<Book>) QueryUtil.list(q, getDialect(), start, end, false); Collections.sort(list); list = Collections.unmodifiableList(list); } else { list = (List<Book>) QueryUtil.list(q, getDialect(), start, end); } cacheResult(list); finderCache.putResult(finderPath, finderArgs, list); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return list; } /** * Returns the first book in the ordered set where groupId = ?. * * @param groupId the group ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the first matching book * @throws NoSuchBookException if a matching book could not be found */ @Override public Book findByGroup_First(long groupId, OrderByComparator<Book> orderByComparator) throws NoSuchBookException { Book book = fetchByGroup_First(groupId, orderByComparator); if (book != null) { return book; } StringBundler msg = new StringBundler(4); msg.append(_NO_SUCH_ENTITY_WITH_KEY); msg.append("groupId="); msg.append(groupId); msg.append(StringPool.CLOSE_CURLY_BRACE); throw new NoSuchBookException(msg.toString()); } /** * Returns the first book in the ordered set where groupId = ?. * * @param groupId the group ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the first matching book, or <code>null</code> if a matching book could not be found */ @Override public Book fetchByGroup_First(long groupId, OrderByComparator<Book> orderByComparator) { List<Book> list = findByGroup(groupId, 0, 1, orderByComparator); if (!list.isEmpty()) { return list.get(0); } return null; } /** * Returns the last book in the ordered set where groupId = ?. * * @param groupId the group ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the last matching book * @throws NoSuchBookException if a matching book could not be found */ @Override public Book findByGroup_Last(long groupId, OrderByComparator<Book> orderByComparator) throws NoSuchBookException { Book book = fetchByGroup_Last(groupId, orderByComparator); if (book != null) { return book; } StringBundler msg = new StringBundler(4); msg.append(_NO_SUCH_ENTITY_WITH_KEY); msg.append("groupId="); msg.append(groupId); msg.append(StringPool.CLOSE_CURLY_BRACE); throw new NoSuchBookException(msg.toString()); } /** * Returns the last book in the ordered set where groupId = ?. * * @param groupId the group ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the last matching book, or <code>null</code> if a matching book could not be found */ @Override public Book fetchByGroup_Last(long groupId, OrderByComparator<Book> orderByComparator) { int count = countByGroup(groupId); if (count == 0) { return null; } List<Book> list = findByGroup(groupId, count - 1, count, orderByComparator); if (!list.isEmpty()) { return list.get(0); } return null; } /** * Returns the books before and after the current book in the ordered set where groupId = ?. * * @param bookId the primary key of the current book * @param groupId the group ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the previous, current, and next book * @throws NoSuchBookException if a book with the primary key could not be found */ @Override public Book[] findByGroup_PrevAndNext(long bookId, long groupId, OrderByComparator<Book> orderByComparator) throws NoSuchBookException { Book book = findByPrimaryKey(bookId); Session session = null; try { session = openSession(); Book[] array = new BookImpl[3]; array[0] = getByGroup_PrevAndNext(session, book, groupId, orderByComparator, true); array[1] = book; array[2] = getByGroup_PrevAndNext(session, book, groupId, orderByComparator, false); return array; } catch (Exception e) { throw processException(e); } finally { closeSession(session); } } protected Book getByGroup_PrevAndNext(Session session, Book book, long groupId, OrderByComparator<Book> orderByComparator, boolean previous) { StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3) + (orderByComparator.getOrderByFields().length * 3)); } else { query = new StringBundler(3); } query.append(_SQL_SELECT_BOOK_WHERE); query.append(_FINDER_COLUMN_GROUP_GROUPID_2); if (orderByComparator != null) { String[] orderByConditionFields = orderByComparator.getOrderByConditionFields(); if (orderByConditionFields.length > 0) { query.append(WHERE_AND); } for (int i = 0; i < orderByConditionFields.length; i++) { query.append(_ORDER_BY_ENTITY_ALIAS); query.append(orderByConditionFields[i]); if ((i + 1) < orderByConditionFields.length) { if (orderByComparator.isAscending() ^ previous) { query.append(WHERE_GREATER_THAN_HAS_NEXT); } else { query.append(WHERE_LESSER_THAN_HAS_NEXT); } } else { if (orderByComparator.isAscending() ^ previous) { query.append(WHERE_GREATER_THAN); } else { query.append(WHERE_LESSER_THAN); } } } query.append(ORDER_BY_CLAUSE); String[] orderByFields = orderByComparator.getOrderByFields(); for (int i = 0; i < orderByFields.length; i++) { query.append(_ORDER_BY_ENTITY_ALIAS); query.append(orderByFields[i]); if ((i + 1) < orderByFields.length) { if (orderByComparator.isAscending() ^ previous) { query.append(ORDER_BY_ASC_HAS_NEXT); } else { query.append(ORDER_BY_DESC_HAS_NEXT); } } else { if (orderByComparator.isAscending() ^ previous) { query.append(ORDER_BY_ASC); } else { query.append(ORDER_BY_DESC); } } } } else { query.append(BookModelImpl.ORDER_BY_JPQL); } String sql = query.toString(); Query q = session.createQuery(sql); q.setFirstResult(0); q.setMaxResults(2); QueryPos qPos = QueryPos.getInstance(q); qPos.add(groupId); if (orderByComparator != null) { Object[] values = orderByComparator.getOrderByConditionValues(book); for (Object value : values) { qPos.add(value); } } List<Book> list = q.list(); if (list.size() == 2) { return list.get(1); } else { return null; } } /** * Returns all the books that the user has permission to view where groupId = ?. * * @param groupId the group ID * @return the matching books that the user has permission to view */ @Override public List<Book> filterFindByGroup(long groupId) { return filterFindByGroup(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null); } /** * Returns a range of all the books that the user has permission to view where groupId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param groupId the group ID * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @return the range of matching books that the user has permission to view */ @Override public List<Book> filterFindByGroup(long groupId, int start, int end) { return filterFindByGroup(groupId, start, end, null); } /** * Returns an ordered range of all the books that the user has permissions to view where groupId = ?. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param groupId the group ID * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @return the ordered range of matching books that the user has permission to view */ @Override public List<Book> filterFindByGroup(long groupId, int start, int end, OrderByComparator<Book> orderByComparator) { if (!InlineSQLHelperUtil.isEnabled(groupId)) { return findByGroup(groupId, start, end, orderByComparator); } StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2)); } else { query = new StringBundler(4); } if (getDB().isSupportsInlineDistinct()) { query.append(_FILTER_SQL_SELECT_BOOK_WHERE); } else { query.append(_FILTER_SQL_SELECT_BOOK_NO_INLINE_DISTINCT_WHERE_1); } query.append(_FINDER_COLUMN_GROUP_GROUPID_2); if (!getDB().isSupportsInlineDistinct()) { query.append(_FILTER_SQL_SELECT_BOOK_NO_INLINE_DISTINCT_WHERE_2); } if (orderByComparator != null) { if (getDB().isSupportsInlineDistinct()) { appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator, true); } else { appendOrderByComparator(query, _ORDER_BY_ENTITY_TABLE, orderByComparator, true); } } else { if (getDB().isSupportsInlineDistinct()) { query.append(BookModelImpl.ORDER_BY_JPQL); } else { query.append(BookModelImpl.ORDER_BY_SQL); } } String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), Book.class.getName(), _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId); Session session = null; try { session = openSession(); SQLQuery q = session.createSynchronizedSQLQuery(sql); if (getDB().isSupportsInlineDistinct()) { q.addEntity(_FILTER_ENTITY_ALIAS, BookImpl.class); } else { q.addEntity(_FILTER_ENTITY_TABLE, BookImpl.class); } QueryPos qPos = QueryPos.getInstance(q); qPos.add(groupId); return (List<Book>) QueryUtil.list(q, getDialect(), start, end); } catch (Exception e) { throw processException(e); } finally { closeSession(session); } } /** * Returns the books before and after the current book in the ordered set of books that the user has permission to view where groupId = ?. * * @param bookId the primary key of the current book * @param groupId the group ID * @param orderByComparator the comparator to order the set by (optionally <code>null</code>) * @return the previous, current, and next book * @throws NoSuchBookException if a book with the primary key could not be found */ @Override public Book[] filterFindByGroup_PrevAndNext(long bookId, long groupId, OrderByComparator<Book> orderByComparator) throws NoSuchBookException { if (!InlineSQLHelperUtil.isEnabled(groupId)) { return findByGroup_PrevAndNext(bookId, groupId, orderByComparator); } Book book = findByPrimaryKey(bookId); Session session = null; try { session = openSession(); Book[] array = new BookImpl[3]; array[0] = filterGetByGroup_PrevAndNext(session, book, groupId, orderByComparator, true); array[1] = book; array[2] = filterGetByGroup_PrevAndNext(session, book, groupId, orderByComparator, false); return array; } catch (Exception e) { throw processException(e); } finally { closeSession(session); } } protected Book filterGetByGroup_PrevAndNext(Session session, Book book, long groupId, OrderByComparator<Book> orderByComparator, boolean previous) { StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(5 + (orderByComparator.getOrderByConditionFields().length * 3) + (orderByComparator.getOrderByFields().length * 3)); } else { query = new StringBundler(4); } if (getDB().isSupportsInlineDistinct()) { query.append(_FILTER_SQL_SELECT_BOOK_WHERE); } else { query.append(_FILTER_SQL_SELECT_BOOK_NO_INLINE_DISTINCT_WHERE_1); } query.append(_FINDER_COLUMN_GROUP_GROUPID_2); if (!getDB().isSupportsInlineDistinct()) { query.append(_FILTER_SQL_SELECT_BOOK_NO_INLINE_DISTINCT_WHERE_2); } if (orderByComparator != null) { String[] orderByConditionFields = orderByComparator.getOrderByConditionFields(); if (orderByConditionFields.length > 0) { query.append(WHERE_AND); } for (int i = 0; i < orderByConditionFields.length; i++) { if (getDB().isSupportsInlineDistinct()) { query.append(_ORDER_BY_ENTITY_ALIAS); } else { query.append(_ORDER_BY_ENTITY_TABLE); } query.append(orderByConditionFields[i]); if ((i + 1) < orderByConditionFields.length) { if (orderByComparator.isAscending() ^ previous) { query.append(WHERE_GREATER_THAN_HAS_NEXT); } else { query.append(WHERE_LESSER_THAN_HAS_NEXT); } } else { if (orderByComparator.isAscending() ^ previous) { query.append(WHERE_GREATER_THAN); } else { query.append(WHERE_LESSER_THAN); } } } query.append(ORDER_BY_CLAUSE); String[] orderByFields = orderByComparator.getOrderByFields(); for (int i = 0; i < orderByFields.length; i++) { if (getDB().isSupportsInlineDistinct()) { query.append(_ORDER_BY_ENTITY_ALIAS); } else { query.append(_ORDER_BY_ENTITY_TABLE); } query.append(orderByFields[i]); if ((i + 1) < orderByFields.length) { if (orderByComparator.isAscending() ^ previous) { query.append(ORDER_BY_ASC_HAS_NEXT); } else { query.append(ORDER_BY_DESC_HAS_NEXT); } } else { if (orderByComparator.isAscending() ^ previous) { query.append(ORDER_BY_ASC); } else { query.append(ORDER_BY_DESC); } } } } else { if (getDB().isSupportsInlineDistinct()) { query.append(BookModelImpl.ORDER_BY_JPQL); } else { query.append(BookModelImpl.ORDER_BY_SQL); } } String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), Book.class.getName(), _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId); SQLQuery q = session.createSynchronizedSQLQuery(sql); q.setFirstResult(0); q.setMaxResults(2); if (getDB().isSupportsInlineDistinct()) { q.addEntity(_FILTER_ENTITY_ALIAS, BookImpl.class); } else { q.addEntity(_FILTER_ENTITY_TABLE, BookImpl.class); } QueryPos qPos = QueryPos.getInstance(q); qPos.add(groupId); if (orderByComparator != null) { Object[] values = orderByComparator.getOrderByConditionValues(book); for (Object value : values) { qPos.add(value); } } List<Book> list = q.list(); if (list.size() == 2) { return list.get(1); } else { return null; } } /** * Removes all the books where groupId = ? from the database. * * @param groupId the group ID */ @Override public void removeByGroup(long groupId) { for (Book book : findByGroup(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) { remove(book); } } /** * Returns the number of books where groupId = ?. * * @param groupId the group ID * @return the number of matching books */ @Override public int countByGroup(long groupId) { FinderPath finderPath = FINDER_PATH_COUNT_BY_GROUP; Object[] finderArgs = new Object[] { groupId }; Long count = (Long) finderCache.getResult(finderPath, finderArgs, this); if (count == null) { StringBundler query = new StringBundler(2); query.append(_SQL_COUNT_BOOK_WHERE); query.append(_FINDER_COLUMN_GROUP_GROUPID_2); String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); QueryPos qPos = QueryPos.getInstance(q); qPos.add(groupId); count = (Long) q.uniqueResult(); finderCache.putResult(finderPath, finderArgs, count); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return count.intValue(); } /** * Returns the number of books that the user has permission to view where groupId = ?. * * @param groupId the group ID * @return the number of matching books that the user has permission to view */ @Override public int filterCountByGroup(long groupId) { if (!InlineSQLHelperUtil.isEnabled(groupId)) { return countByGroup(groupId); } StringBundler query = new StringBundler(2); query.append(_FILTER_SQL_COUNT_BOOK_WHERE); query.append(_FINDER_COLUMN_GROUP_GROUPID_2); String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), Book.class.getName(), _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId); Session session = null; try { session = openSession(); SQLQuery q = session.createSynchronizedSQLQuery(sql); q.addScalar(COUNT_COLUMN_NAME, com.liferay.portal.kernel.dao.orm.Type.LONG); QueryPos qPos = QueryPos.getInstance(q); qPos.add(groupId); Long count = (Long) q.uniqueResult(); return count.intValue(); } catch (Exception e) { throw processException(e); } finally { closeSession(session); } } private static final String _FINDER_COLUMN_GROUP_GROUPID_2 = "book.groupId = ?"; public BookPersistenceImpl() { setModelClass(Book.class); } /** * Caches the book in the entity cache if it is enabled. * * @param book the book */ @Override public void cacheResult(Book book) { entityCache.putResult(BookModelImpl.ENTITY_CACHE_ENABLED, BookImpl.class, book.getPrimaryKey(), book); book.resetOriginalValues(); } /** * Caches the books in the entity cache if it is enabled. * * @param books the books */ @Override public void cacheResult(List<Book> books) { for (Book book : books) { if (entityCache.getResult(BookModelImpl.ENTITY_CACHE_ENABLED, BookImpl.class, book.getPrimaryKey()) == null) { cacheResult(book); } else { book.resetOriginalValues(); } } } /** * Clears the cache for all books. * * <p> * The {@link EntityCache} and {@link FinderCache} are both cleared by this method. * </p> */ @Override public void clearCache() { entityCache.clearCache(BookImpl.class); finderCache.clearCache(FINDER_CLASS_NAME_ENTITY); finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); } /** * Clears the cache for the book. * * <p> * The {@link EntityCache} and {@link FinderCache} are both cleared by this method. * </p> */ @Override public void clearCache(Book book) { entityCache.removeResult(BookModelImpl.ENTITY_CACHE_ENABLED, BookImpl.class, book.getPrimaryKey()); finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); } @Override public void clearCache(List<Book> books) { finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); for (Book book : books) { entityCache.removeResult(BookModelImpl.ENTITY_CACHE_ENABLED, BookImpl.class, book.getPrimaryKey()); } } /** * Creates a new book with the primary key. Does not add the book to the database. * * @param bookId the primary key for the new book * @return the new book */ @Override public Book create(long bookId) { Book book = new BookImpl(); book.setNew(true); book.setPrimaryKey(bookId); book.setCompanyId(companyProvider.getCompanyId()); return book; } /** * Removes the book with the primary key from the database. Also notifies the appropriate model listeners. * * @param bookId the primary key of the book * @return the book that was removed * @throws NoSuchBookException if a book with the primary key could not be found */ @Override public Book remove(long bookId) throws NoSuchBookException { return remove((Serializable) bookId); } /** * Removes the book with the primary key from the database. Also notifies the appropriate model listeners. * * @param primaryKey the primary key of the book * @return the book that was removed * @throws NoSuchBookException if a book with the primary key could not be found */ @Override public Book remove(Serializable primaryKey) throws NoSuchBookException { Session session = null; try { session = openSession(); Book book = (Book) session.get(BookImpl.class, primaryKey); if (book == null) { if (_log.isDebugEnabled()) { _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey); } throw new NoSuchBookException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey); } return remove(book); } catch (NoSuchBookException nsee) { throw nsee; } catch (Exception e) { throw processException(e); } finally { closeSession(session); } } @Override protected Book removeImpl(Book book) { book = toUnwrappedModel(book); Session session = null; try { session = openSession(); if (!session.contains(book)) { book = (Book) session.get(BookImpl.class, book.getPrimaryKeyObj()); } if (book != null) { session.delete(book); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } if (book != null) { clearCache(book); } return book; } @Override public Book updateImpl(Book book) { book = toUnwrappedModel(book); boolean isNew = book.isNew(); BookModelImpl bookModelImpl = (BookModelImpl) book; ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext(); Date now = new Date(); if (isNew && (book.getCreateDate() == null)) { if (serviceContext == null) { book.setCreateDate(now); } else { book.setCreateDate(serviceContext.getCreateDate(now)); } } if (!bookModelImpl.hasSetModifiedDate()) { if (serviceContext == null) { book.setModifiedDate(now); } else { book.setModifiedDate(serviceContext.getModifiedDate(now)); } } Session session = null; try { session = openSession(); if (book.isNew()) { session.save(book); book.setNew(false); } else { book = (Book) session.merge(book); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); if (isNew || !BookModelImpl.COLUMN_BITMASK_ENABLED) { finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); } else { if ((bookModelImpl.getColumnBitmask() & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_AUTHOR.getColumnBitmask()) != 0) { Object[] args = new Object[] { bookModelImpl.getOriginalAuthor() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_AUTHOR, args); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_AUTHOR, args); args = new Object[] { bookModelImpl.getAuthor() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_AUTHOR, args); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_AUTHOR, args); } if ((bookModelImpl.getColumnBitmask() & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUP.getColumnBitmask()) != 0) { Object[] args = new Object[] { bookModelImpl.getOriginalGroupId() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_GROUP, args); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUP, args); args = new Object[] { bookModelImpl.getGroupId() }; finderCache.removeResult(FINDER_PATH_COUNT_BY_GROUP, args); finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUP, args); } } entityCache.putResult(BookModelImpl.ENTITY_CACHE_ENABLED, BookImpl.class, book.getPrimaryKey(), book, false); book.resetOriginalValues(); return book; } protected Book toUnwrappedModel(Book book) { if (book instanceof BookImpl) { return book; } BookImpl bookImpl = new BookImpl(); bookImpl.setNew(book.isNew()); bookImpl.setPrimaryKey(book.getPrimaryKey()); bookImpl.setGroupId(book.getGroupId()); bookImpl.setCompanyId(book.getCompanyId()); bookImpl.setUserId(book.getUserId()); bookImpl.setUserName(book.getUserName()); bookImpl.setCreateDate(book.getCreateDate()); bookImpl.setModifiedDate(book.getModifiedDate()); bookImpl.setBookId(book.getBookId()); bookImpl.setName(book.getName()); bookImpl.setAuthor(book.getAuthor()); bookImpl.setDescription(book.getDescription()); bookImpl.setPrice(book.getPrice()); return bookImpl; } /** * Returns the book with the primary key or throws a {@link com.liferay.portal.kernel.exception.NoSuchModelException} if it could not be found. * * @param primaryKey the primary key of the book * @return the book * @throws NoSuchBookException if a book with the primary key could not be found */ @Override public Book findByPrimaryKey(Serializable primaryKey) throws NoSuchBookException { Book book = fetchByPrimaryKey(primaryKey); if (book == null) { if (_log.isDebugEnabled()) { _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey); } throw new NoSuchBookException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey); } return book; } /** * Returns the book with the primary key or throws a {@link NoSuchBookException} if it could not be found. * * @param bookId the primary key of the book * @return the book * @throws NoSuchBookException if a book with the primary key could not be found */ @Override public Book findByPrimaryKey(long bookId) throws NoSuchBookException { return findByPrimaryKey((Serializable) bookId); } /** * Returns the book with the primary key or returns <code>null</code> if it could not be found. * * @param primaryKey the primary key of the book * @return the book, or <code>null</code> if a book with the primary key could not be found */ @Override public Book fetchByPrimaryKey(Serializable primaryKey) { Serializable serializable = entityCache.getResult(BookModelImpl.ENTITY_CACHE_ENABLED, BookImpl.class, primaryKey); if (serializable == nullModel) { return null; } Book book = (Book) serializable; if (book == null) { Session session = null; try { session = openSession(); book = (Book) session.get(BookImpl.class, primaryKey); if (book != null) { cacheResult(book); } else { entityCache.putResult(BookModelImpl.ENTITY_CACHE_ENABLED, BookImpl.class, primaryKey, nullModel); } } catch (Exception e) { entityCache.removeResult(BookModelImpl.ENTITY_CACHE_ENABLED, BookImpl.class, primaryKey); throw processException(e); } finally { closeSession(session); } } return book; } /** * Returns the book with the primary key or returns <code>null</code> if it could not be found. * * @param bookId the primary key of the book * @return the book, or <code>null</code> if a book with the primary key could not be found */ @Override public Book fetchByPrimaryKey(long bookId) { return fetchByPrimaryKey((Serializable) bookId); } @Override public Map<Serializable, Book> fetchByPrimaryKeys(Set<Serializable> primaryKeys) { if (primaryKeys.isEmpty()) { return Collections.emptyMap(); } Map<Serializable, Book> map = new HashMap<Serializable, Book>(); if (primaryKeys.size() == 1) { Iterator<Serializable> iterator = primaryKeys.iterator(); Serializable primaryKey = iterator.next(); Book book = fetchByPrimaryKey(primaryKey); if (book != null) { map.put(primaryKey, book); } return map; } Set<Serializable> uncachedPrimaryKeys = null; for (Serializable primaryKey : primaryKeys) { Serializable serializable = entityCache.getResult(BookModelImpl.ENTITY_CACHE_ENABLED, BookImpl.class, primaryKey); if (serializable != nullModel) { if (serializable == null) { if (uncachedPrimaryKeys == null) { uncachedPrimaryKeys = new HashSet<Serializable>(); } uncachedPrimaryKeys.add(primaryKey); } else { map.put(primaryKey, (Book) serializable); } } } if (uncachedPrimaryKeys == null) { return map; } StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1); query.append(_SQL_SELECT_BOOK_WHERE_PKS_IN); for (Serializable primaryKey : uncachedPrimaryKeys) { query.append(String.valueOf(primaryKey)); query.append(StringPool.COMMA); } query.setIndex(query.index() - 1); query.append(StringPool.CLOSE_PARENTHESIS); String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); for (Book book : (List<Book>) q.list()) { map.put(book.getPrimaryKeyObj(), book); cacheResult(book); uncachedPrimaryKeys.remove(book.getPrimaryKeyObj()); } for (Serializable primaryKey : uncachedPrimaryKeys) { entityCache.putResult(BookModelImpl.ENTITY_CACHE_ENABLED, BookImpl.class, primaryKey, nullModel); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } return map; } /** * Returns all the books. * * @return the books */ @Override public List<Book> findAll() { return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null); } /** * Returns a range of all the books. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @return the range of books */ @Override public List<Book> findAll(int start, int end) { return findAll(start, end, null); } /** * Returns an ordered range of all the books. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @return the ordered range of books */ @Override public List<Book> findAll(int start, int end, OrderByComparator<Book> orderByComparator) { return findAll(start, end, orderByComparator, true); } /** * Returns an ordered range of all the books. * * <p> * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link BookModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order. * </p> * * @param start the lower bound of the range of books * @param end the upper bound of the range of books (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @param retrieveFromCache whether to retrieve from the finder cache * @return the ordered range of books */ @Override public List<Book> findAll(int start, int end, OrderByComparator<Book> orderByComparator, boolean retrieveFromCache) { boolean pagination = true; FinderPath finderPath = null; Object[] finderArgs = null; if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) { pagination = false; finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL; finderArgs = FINDER_ARGS_EMPTY; } else { finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL; finderArgs = new Object[] { start, end, orderByComparator }; } List<Book> list = null; if (retrieveFromCache) { list = (List<Book>) finderCache.getResult(finderPath, finderArgs, this); } if (list == null) { StringBundler query = null; String sql = null; if (orderByComparator != null) { query = new StringBundler(2 + (orderByComparator.getOrderByFields().length * 2)); query.append(_SQL_SELECT_BOOK); appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator); sql = query.toString(); } else { sql = _SQL_SELECT_BOOK; if (pagination) { sql = sql.concat(BookModelImpl.ORDER_BY_JPQL); } } Session session = null; try { session = openSession(); Query q = session.createQuery(sql); if (!pagination) { list = (List<Book>) QueryUtil.list(q, getDialect(), start, end, false); Collections.sort(list); list = Collections.unmodifiableList(list); } else { list = (List<Book>) QueryUtil.list(q, getDialect(), start, end); } cacheResult(list); finderCache.putResult(finderPath, finderArgs, list); } catch (Exception e) { finderCache.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return list; } /** * Removes all the books from the database. * */ @Override public void removeAll() { for (Book book : findAll()) { remove(book); } } /** * Returns the number of books. * * @return the number of books */ @Override public int countAll() { Long count = (Long) finderCache.getResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, this); if (count == null) { Session session = null; try { session = openSession(); Query q = session.createQuery(_SQL_COUNT_BOOK); count = (Long) q.uniqueResult(); finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, count); } catch (Exception e) { finderCache.removeResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY); throw processException(e); } finally { closeSession(session); } } return count.intValue(); } @Override protected Map<String, Integer> getTableColumnsMap() { return BookModelImpl.TABLE_COLUMNS_MAP; } /** * Initializes the book persistence. */ public void afterPropertiesSet() { } public void destroy() { entityCache.removeCache(BookImpl.class.getName()); finderCache.removeCache(FINDER_CLASS_NAME_ENTITY); finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); } @ServiceReference(type = CompanyProviderWrapper.class) protected CompanyProvider companyProvider; @ServiceReference(type = EntityCache.class) protected EntityCache entityCache; @ServiceReference(type = FinderCache.class) protected FinderCache finderCache; private static final String _SQL_SELECT_BOOK = "SELECT book FROM Book book"; private static final String _SQL_SELECT_BOOK_WHERE_PKS_IN = "SELECT book FROM Book book WHERE bookId IN ("; private static final String _SQL_SELECT_BOOK_WHERE = "SELECT book FROM Book book WHERE "; private static final String _SQL_COUNT_BOOK = "SELECT COUNT(book) FROM Book book"; private static final String _SQL_COUNT_BOOK_WHERE = "SELECT COUNT(book) FROM Book book WHERE "; private static final String _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN = "book.bookId"; private static final String _FILTER_SQL_SELECT_BOOK_WHERE = "SELECT DISTINCT {book.*} FROM BOOK_Book book WHERE "; private static final String _FILTER_SQL_SELECT_BOOK_NO_INLINE_DISTINCT_WHERE_1 = "SELECT {BOOK_Book.*} FROM (SELECT DISTINCT book.bookId FROM BOOK_Book book WHERE "; private static final String _FILTER_SQL_SELECT_BOOK_NO_INLINE_DISTINCT_WHERE_2 = ") TEMP_TABLE INNER JOIN BOOK_Book ON TEMP_TABLE.bookId = BOOK_Book.bookId"; private static final String _FILTER_SQL_COUNT_BOOK_WHERE = "SELECT COUNT(DISTINCT book.bookId) AS COUNT_VALUE FROM BOOK_Book book WHERE "; private static final String _FILTER_ENTITY_ALIAS = "book"; private static final String _FILTER_ENTITY_TABLE = "BOOK_Book"; private static final String _ORDER_BY_ENTITY_ALIAS = "book."; private static final String _ORDER_BY_ENTITY_TABLE = "BOOK_Book."; private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Book exists with the primary key "; private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Book exists with the key {"; private static final Log _log = LogFactoryUtil.getLog(BookPersistenceImpl.class); }