view src/main/java/de/intevation/lada/util/data/DataTransaction.java @ 1290:14876c62f692

Push down refreshing of persisted objects deeper into the stack. There are more places besides creation of Probe objects where it is useful to return within the response what has been really written to the database (including modifications by the database itself) instead of merely the request data, e.g. creation of Ort objects, which includes database generated ort_ids.
author Tom Gottfried <tom@intevation.de>
date Wed, 08 Feb 2017 18:02:05 +0100
parents 7683c4162746
children
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.inject.Inject;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TransactionRequiredException;

/**
 * Abstract class implementing low level data operations.
 *
 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
 */
@Stateless
public class DataTransaction
{
    @Inject
    protected EntityManagerProducer emp;

    protected String jndiPath;

    /**
     * Create object in the database.
     * This operation can not be undone.
     *
     * @param object    The object to create
     *
     * @throws EntityExistsException
     * @throws IllegalArgumentException
     * @throws EJBTransactionRolledbackException
     * @throws TransactionRequiredException
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void persistInDatabase(Object object, String dataSource)
    throws EntityExistsException,
        IllegalArgumentException,
        EJBTransactionRolledbackException,
        TransactionRequiredException
    {
        EntityManager manager = emp.entityManager(dataSource);
        manager.persist(object);

        /* Refreshing the object is necessary because some objects use
           dynamic-insert, meaning null-valued columns are not INSERTed
           to the DB to take advantage of DB DEFAULT values, or triggers
           modify the object during INSERT. */
        manager.refresh(object);
    }

    /**
     * Create object in the database.
     * This operation can not be undone.
     *
     * @param object    The object to create
     *
     * @throws EntityExistsException
     * @throws IllegalArgumentException
     * @throws EJBTransactionRolledbackException
     * @throws TransactionRequiredException
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void updateInDatabase(Object object, String dataSource)
    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)
    public void removeFromDatabase(Object object, String dataSource)
    throws IllegalArgumentException,
        TransactionRequiredException,
        EJBTransactionRolledbackException
    {
        EntityManager em = emp.entityManager(dataSource);
        em.remove(
            em.contains(object) ?
                object : em.merge(object));
        em.flush();
    }

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

    public EntityManager entityManager(String dataSource) {
        return emp.entityManager(dataSource);
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)