com.balero.models.BlogDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.balero.models.BlogDAO.java

Source

/**
 * <pre>
 * Balero CMS Enterprise Edition is free and open source software under MIT License.
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013-2014 <Balero CMS All Rights Reserved>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * <a href="http://www.balerocms.com">BaleroCMS.com</a>
 * </pre>
 *
 * @author      Anibal Gomez
 * @version     1.0
 * @since       1.0
 */

package com.balero.models;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

/**
 * Get, Insert and Update data from database
 */
@Repository
@SuppressWarnings({ "unchecked", "rawtypes" })
public class BlogDAO {
    @Autowired
    private SessionFactory sessionFactory;

    /**
     * SELECT * FROM 'Blog'
     *
     * @return rows
     */
    @Transactional
    public List<Blog> findAllAdmin() {
        Session session = sessionFactory.getCurrentSession();
        List rows = session.createQuery("from Blog").list();
        return rows;
    }

    /**
     * SELECT * FROM 'Blog' where lang
     *
     * @return rows
     */
    @Transactional
    public List<Blog> findAllUser(Locale locale) {
        Session session = sessionFactory.getCurrentSession();
        List rows = session.createQuery("from Blog").list();
        return rows;
    }

    @Transactional
    public List<Blog> findAllAuthor(String author) {
        Session session = sessionFactory.getCurrentSession();
        List rows = session.createQuery("from Blog where authorpost = '" + author + "'").list();
        return rows;
    }

    /**
     * Find by id (More)
     *
     * @param id
     * @return
     */
    @Transactional
    public List<Blog> findBlog(int id) {
        Session session = sessionFactory.getCurrentSession();
        List rows = session.createQuery("from Blog where id = '" + id + "'").list();
        return rows;
    }

    @Transactional
    public void addPost(String postitle, String postbody, String postslug, Locale postlocale, String postauthor,
            String poststatus) {
        Session session = sessionFactory.openSession();
        Blog Blog = new Blog();
        Blog.setPosttitle(postitle);
        Blog.setPostbody(postbody);
        Blog.setPostslug(postslug);
        Blog.setPostlocale(postlocale.toString());
        Blog.setPostauthor(postauthor);
        Blog.setPoststatus(poststatus);
        session.save(Blog);
        session.flush();
        session.close();
    }

    @Transactional
    public void updatePost(int id, String posttitle, String postbody, String postslug, String postlocale,
            String postauhtor, String poststatus) {
        Session session = sessionFactory.openSession();
        Blog Blog = new Blog();
        Blog.setId(id);
        Blog.setPosttitle(posttitle);
        Blog.setPostbody(postbody);
        Blog.setPostslug(postslug);
        Blog.setPostlocale(postlocale);
        Blog.setPostauthor(postauhtor);
        Blog.setPoststatus(poststatus);
        session.update(Blog);
        session.flush();
        session.close();
    }

    @Transactional
    public void deletePost(int id) {
        Session session = sessionFactory.openSession();
        Blog Blog = new Blog();
        Blog.setId(id);
        session.delete(Blog);
        session.flush();
        session.close();
    }

    @Transactional
    public String postTitle(int id) {
        String postitle = null;
        Session session = sessionFactory.getCurrentSession();
        List<Blog> Blog = session.createQuery("from Blog where id = '" + id + "'").list();
        for (Blog obj : Blog) {
            postitle = obj.getPosttitle();
        }
        String title = null;
        title = StringUtils.substringBetween(postitle, "<h3>", "</h3>");
        if (title == null) {
            title = StringUtils.substringBetween(postitle, "<p>", "</p>");
        }
        return StringUtils.abbreviate(title + "...", 150);
    }

    @Transactional
    public void make(Locale locale) {
        Session session = sessionFactory.getCurrentSession();
        Blog Blog = new Blog();
        Blog.setId(1);
        Blog.setPosttitle("<h3>Lorem ipsum</h3>\n" + "\n"
                + "<p>Lorem Ipsum is text that is commonly used in graphic design typefaces demonstrations or draft design to test the visual design before inserting the final text.</p>\n"
                + "\n"
                + "<p>Although currently has no sources to justify tesis hip, professor of classical philology Richard McClintock says its use dates back to the early printers XVI.1 century Its use in some text editors well known in today has given the new popularity lorem ipsum.</p>\n"
                + "\n"
                + "<p>The text itself no sense, although it is not completely random, but derives from a text by Cicero in Latin language, whose words have been removed them syllables or letters. The meaning of the text does not matter, since it is just a test demostracino,</p>\n");
        Blog.setPostbody("Read more content...");
        Blog.setPostslug("welcome-test-post");
        Blog.setPostlocale(locale.toString());
        Blog.setPostauthor("admin");
        Blog.setPoststatus("published");
        session.save(Blog);
        session.flush();
    }

}