Java tutorial
/* * Copyright 2014 asikprad. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.bisen.blog.service; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; import org.bisen.blog.model.Blog; import org.bisen.blog.model.BlogUser; import org.bisen.blog.model.SolrBlog; import org.bisen.blog.repository.springdatajpa.BlogRepository; import org.bisen.blog.repository.springdatajpa.UserRepository; import org.bisen.blog.repository.springdatasolr.SolrBlogRepository; import org.jooq.DSLContext; import org.jooq.impl.DSL; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.dao.DataAccessException; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.solr.core.query.result.HighlightPage; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * * @author asikprad */ @Service("blogService") public class BlogService { @Autowired @Qualifier("blogRepository") private BlogRepository blogRepository; @Autowired @Qualifier("userRepository") private UserRepository userRepository; @Autowired @Qualifier("solrBlogRepository") private SolrBlogRepository solrBlogRepository; @Autowired DSLContext create; @Autowired @Qualifier("dataSource") private BasicDataSource dataSource; @Transactional(readOnly = true) public Blog find(int blogId) throws DataAccessException { return blogRepository.findOne(blogId); } @Transactional private Blog saveToDatabase(Blog blog) { return blogRepository.save(blog); } @Transactional private SolrBlog saveToSolr(Blog blog) { return solrBlogRepository.save(new SolrBlog.Builder(blog).build()); } public SolrBlog save(Blog blog) throws DataAccessException { Blog databaseBlog = saveToDatabase(blog); SolrBlog solrBlog = saveToSolr(databaseBlog); return solrBlog; } @Transactional public BlogUser saveUser(BlogUser user) throws DataAccessException { return userRepository.save(user); } @Transactional(readOnly = true) public BlogUser findUser(String email) throws DataAccessException { String sql = create.select().from(DSL.tableByName("user")).where(DSL.fieldByName("email").eq(email)) .getSQL(); JdbcTemplate template = new JdbcTemplate(dataSource); BlogUser user = template.query(sql, new Object[] { email }, new ResultSetExtractor<BlogUser>() { @Override public BlogUser extractData(ResultSet rs) throws SQLException, DataAccessException { return rs.next() ? new BlogUser(rs.getString("email"), rs.getString("full_name")) : null; } }); return user; } @Transactional(readOnly = true) public Page<SolrBlog> findAll(Pageable pageable) throws DataAccessException { return solrBlogRepository.findAll(pageable); } @Transactional(readOnly = true) public Page<SolrBlog> search(String query, Pageable pageable) throws DataAccessException { return solrBlogRepository.search(query, pageable); } @Transactional(readOnly = true) public HighlightPage<SolrBlog> highlight(String query, Pageable pageable) throws DataAccessException { return solrBlogRepository.highlight(query, pageable); } }