Open the BookLocalServiceImpl.java file, which is available in /docroot/WEB-INF/src/com/sample/portlet/library/service/impl/. We will be adding the database interaction methods to this service layer class. Add the following method to the BookLocalServiceImpl class.
public Book addBook(long userId, String title) throws PortalException, SystemException { User user = UserUtil.findByPrimaryKey(userId); Date now = new Date(); long bookId = CounterLocalServiceUtil.increment(Book.class.getName()); Book book = bookPersistence.create(bookId); book.setTitle(title); book.setCompanyId(user.getCompanyId()); book.setUserId(user.getUserId()); book.setUserName(user.getFullName()); book.setCreateDate(now); book.setModifiedDate(now); book.setTitle(title); return bookPersistence.update(book); } |
Before you can use the code, you must regenerate the service layer class. Navigate to the root folder of your portlet, and type the ant build-service command. You can now add a new book to the database by making the following call.
BookLocalServiceUtil.addBook(userId, ?A new title?); |