com.eldar.daos.mybatis.MyBatisCssClassDao.java Source code

Java tutorial

Introduction

Here is the source code for com.eldar.daos.mybatis.MyBatisCssClassDao.java

Source

/*******************************************************************************
 * This file is part of Eldar Works.
 *
 * Eldar Works is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Eldar Works 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Eldar Works.  If not, see <http://www.gnu.org/licenses/>.
 * Copyright (c) 2012. Phillip Sanderson
 ******************************************************************************/

package com.eldar.daos.mybatis;

import com.eldar.daos.ICssClassDao;
import com.eldar.daos.mybatis.mappers.CssClassMapper;
import com.eldar.models.CssClass;
import com.eldar.models.templates.Container;
import com.eldar.util.ListPage;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * DAO for the CssClass objects
 *
 * @author Phillip Sanderson
 *         Date: 13/05/12
 *         Time: 3:56 PM
 */
@Repository
public class MyBatisCssClassDao implements ICssClassDao {

    private static Logger logger = LoggerFactory.getLogger(MyBatisCssClassDao.class);

    @Autowired
    private CssClassMapper cssClassMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * List all the css classes.
     * TODO: Paging.
     *
     * @return List of all the css classes.
     */
    public List<CssClass> list(long start, long size) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            return cssClassMapper.list(new ListPage<CssClass>(start, size));
        } finally {
            session.close();
        }
    }

    public void save(CssClass cssClass) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            DateTime current = DateTime.now(DateTimeZone.UTC);
            cssClass.setModifiedDate(current);
            if (cssClass.getId() > 0) {
                cssClassMapper.update(cssClass);
            } else {
                cssClass.setCreatedDate(current);
                cssClassMapper.insert(cssClass);
            }
        } catch (Exception ex) {
            logger.error("Error saving css class", ex);
        } finally {
            session.commit();
            session.close();
        }
    }

    public void saveContainerRel(CssClass cssClass, Container container) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            cssClassMapper.insertContainerClasses(container.getId(), cssClass.getId());
        } finally {
            session.commit();
            session.close();
        }
    }

    public CssClass get(long id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            return cssClassMapper.select(id);
        } finally {
            session.close();
        }
    }

    public void delete(CssClass cssClass) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            cssClassMapper.delete(cssClass.getId());
        } finally {
            session.commit();
            session.close();
        }
    }

    public List<CssClass> findByContainer(long id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            return cssClassMapper.findByContainer();
        } finally {
            session.close();
        }
    }
}