view src/main/java/de/intevation/lada/util/data/AbstractRepository.java @ 438:f0ad10e0e1b1

Moved Repositories, etc. into data package.
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 06 Feb 2015 17:55:54 +0100
parents
children 808ea2091c1d
line wrap: on
line source
/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out
 * the documentation coming with IMIS-Labordaten-Application for details.
 */
package de.intevation.lada.util.data;

import javax.ejb.EJBTransactionRolledbackException;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TransactionRequiredException;


@Stateless
public abstract class AbstractRepository
implements Repository 
{
    protected EntityManagerProducer emp;

    protected String dataSource;

    protected String jndiPath;

    /**
     * Create or update an object in the database.
     * This operation can not be undone.
     *
     * @param object    The object to create or update
     *
     * @throws EntityExistsException
     * @throws IllegalArgumentException
     * @throws EJBTransactionRolledbackException
     * @throws TransactionRequiredException
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    protected void persistInDatabase(Object object)
    throws EntityExistsException,
        IllegalArgumentException,
        EJBTransactionRolledbackException,
        TransactionRequiredException
    {
        emp.entityManager(dataSource).merge(object);
    }

    /**
     * Remove an object from the datebase.
     * This operation can not be undone.
     *
     * @param object    The object to remove
     *
     * @throws IllegalArgumentException
     * @throws TransactionRequiredException
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    protected void removeFromDatabase(Object object)
    throws IllegalArgumentException,
        TransactionRequiredException
    {
        EntityManager em = emp.entityManager(dataSource);
        em.remove(
            em.contains(object) ?
                object : em.merge(object));
    }

    public Query queryFromString(String sql) {
        EntityManager em = emp.entityManager(dataSource);
        return em.createNativeQuery(sql);
    }

    @Override
    public void setDataSource(String dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public String getDataSource() {
        return this.dataSource;
    }

    public void setEntityManagerProducer(EntityManagerProducer emp) {
        this.emp = emp;
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)