com.openkm.extension.dao.StampImageDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.openkm.extension.dao.StampImageDAO.java

Source

/**
 *  OpenKM, Open Document Management System (http://www.openkm.com)
 *  Copyright (c) 2006-2011  Paco Avila & Josep Llort
 *
 *  No bytes were intentionally harmed during the development of this application.
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.openkm.extension.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openkm.core.DatabaseException;
import com.openkm.dao.HibernateUtil;
import com.openkm.extension.dao.bean.StampImage;

public class StampImageDAO {
    private static Logger log = LoggerFactory.getLogger(StampImageDAO.class);

    private StampImageDAO() {
    }

    /**
     * Create
     */
    public static int create(StampImage si) throws DatabaseException {
        log.debug("create({})", si);
        Session session = null;
        Transaction tx = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            Integer id = (Integer) session.save(si);
            HibernateUtil.commit(tx);
            log.debug("create: {}", id);
            return id.intValue();
        } catch (HibernateException e) {
            HibernateUtil.rollback(tx);
            throw new DatabaseException(e.getMessage(), e);
        } finally {
            HibernateUtil.close(session);
        }
    }

    /**
     * Update
     */
    public static void update(StampImage si) throws DatabaseException {
        log.debug("update({})", si);
        String qs = "select si.imageContent, si.imageMime from StampImage si where si.id=:id";
        Session session = null;
        Transaction tx = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();

            if (si.getImageContent() == null || si.getImageContent().length() == 0) {
                Query q = session.createQuery(qs);
                q.setParameter("id", si.getId());
                Object[] data = (Object[]) q.setMaxResults(1).uniqueResult();
                si.setImageContent((String) data[0]);
                si.setImageMime((String) data[1]);
            }

            session.update(si);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            HibernateUtil.rollback(tx);
            throw new DatabaseException(e.getMessage(), e);
        } finally {
            HibernateUtil.close(session);
        }

        log.debug("update: void");
    }

    /**
     * Active
     */
    public static void active(int siId, boolean active) throws DatabaseException {
        log.debug("active({}, {})", siId, active);
        String qs = "update StampImage si set si.active=:active where si.id=:id";
        Session session = null;
        Transaction tx = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            Query q = session.createQuery(qs);
            q.setBoolean("active", active);
            q.setInteger("id", siId);
            q.executeUpdate();
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            HibernateUtil.rollback(tx);
            throw new DatabaseException(e.getMessage(), e);
        } finally {
            HibernateUtil.close(session);
        }

        log.debug("active: void");
    }

    /**
     * Delete
     */
    public static void delete(int siId) throws DatabaseException {
        log.debug("delete({})", siId);
        Session session = null;
        Transaction tx = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            StampImage si = (StampImage) session.load(StampImage.class, siId);
            session.delete(si);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            HibernateUtil.rollback(tx);
            throw new DatabaseException(e.getMessage(), e);
        } finally {
            HibernateUtil.close(session);
        }

        log.debug("delete: void");
    }

    /**
     * Find by pk
     */
    public static StampImage findByPk(int siId) throws DatabaseException {
        log.debug("findByPk({})", siId);
        String qs = "from StampImage si where si.id=:id";
        Session session = null;
        Transaction tx = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            Query q = session.createQuery(qs);
            q.setInteger("id", siId);
            StampImage ret = (StampImage) q.setMaxResults(1).uniqueResult();
            HibernateUtil.commit(tx);
            log.debug("findByPk: {}", ret);
            return ret;
        } catch (HibernateException e) {
            HibernateUtil.rollback(tx);
            throw new DatabaseException(e.getMessage(), e);
        } finally {
            HibernateUtil.close(session);
        }
    }

    /**
     * Find by pk
     */
    @SuppressWarnings("unchecked")
    public static List<StampImage> findAll() throws DatabaseException {
        log.debug("findAll()");
        String qs = "from StampImage si order by si.id";
        Session session = null;
        Transaction tx = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            Query q = session.createQuery(qs);
            List<StampImage> ret = q.list();
            HibernateUtil.commit(tx);
            log.debug("findAll: {}", ret);
            return ret;
        } catch (HibernateException e) {
            HibernateUtil.rollback(tx);
            throw new DatabaseException(e.getMessage(), e);
        } finally {
            HibernateUtil.close(session);
        }
    }

    /**
     * Find by user
     */
    @SuppressWarnings("unchecked")
    public static List<StampImage> findByUser(String usrId) throws DatabaseException {
        log.debug("findByUser({})", usrId);
        String qs = "from StampImage si where :user in elements(si.users)" + "and si.active=:active order by si.id";
        Session session = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query q = session.createQuery(qs);
            q.setString("user", usrId);
            q.setBoolean("active", true);
            List<StampImage> ret = q.list();
            log.debug("findByUser: {}", ret);
            return ret;
        } catch (HibernateException e) {
            throw new DatabaseException(e.getMessage(), e);
        } finally {
            HibernateUtil.close(session);
        }
    }
}