/** * 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.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.core.RepositoryException; import com.openkm.dao.bean.Language; /** * LanguageDAO * * @author jllort * */ public class LanguageDAO { private static Logger log = LoggerFactory.getLogger(LanguageDAO.class); private LanguageDAO() {} /** * Find translations by pk */ public static Language findByPk(String id) throws DatabaseException { log.debug("findByPk({})", id); String qs = "from Language lg where lg.id=:id"; Session session = null; try { session = HibernateUtil.getSessionFactory().openSession(); Query q = session.createQuery(qs); q.setString("id", id); Language ret = (Language) q.setMaxResults(1).uniqueResult(); log.debug("findByPk: {}", ret); return ret; } catch (HibernateException e) { throw new DatabaseException(e.getMessage(), e); } finally { HibernateUtil.close(session); } } /** * Find all languages */ @SuppressWarnings("unchecked") public static List<Language> findAll() throws DatabaseException, RepositoryException { log.debug("findAll({})"); String qs = "from Language lg order by lg.name asc"; Session session = null; Transaction tx = null; try { session = HibernateUtil.getSessionFactory().openSession(); Query q = session.createQuery(qs); List<Language> ret = q.list(); log.debug("findAll: {}", ret); return ret; } catch (HibernateException e) { HibernateUtil.rollback(tx); throw new DatabaseException(e.getMessage(), e); } finally { HibernateUtil.close(session); } } /** * Delete */ public static void delete(String id) throws DatabaseException { log.debug("delete({})", id); Session session = null; Transaction tx = null; try { session = HibernateUtil.getSessionFactory().openSession(); tx = session.beginTransaction(); Language lang = (Language) session.load(Language.class, id); session.delete(lang); HibernateUtil.commit(tx); } catch (HibernateException e) { HibernateUtil.rollback(tx); throw new DatabaseException(e.getMessage(), e); } finally { HibernateUtil.close(session); } log.debug("delete: void"); } /** * Update language in database */ public static void update(Language lang) throws DatabaseException { log.debug("update({})", lang); Session session = null; Transaction tx = null; try { session = HibernateUtil.getSessionFactory().openSession(); tx = session.beginTransaction(); session.update(lang); HibernateUtil.commit(tx); } catch (HibernateException e) { HibernateUtil.rollback(tx); throw new DatabaseException(e.getMessage(), e); } finally { HibernateUtil.close(session); } log.debug("update: void"); } /** * Create language in database */ public static void create(Language lang) throws DatabaseException { log.debug("create({})", lang); Session session = null; Transaction tx = null; try { session = HibernateUtil.getSessionFactory().openSession(); tx = session.beginTransaction(); session.save(lang); HibernateUtil.commit(tx); } catch (HibernateException e) { HibernateUtil.rollback(tx); throw new DatabaseException(e.getMessage(), e); } finally { HibernateUtil.close(session); } log.debug("create: void"); } }