/**
* This file is part of Faktotum.
*
*
* Faktotum is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* Faktotum 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Faktotum.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
package de.romankreisel.faktotum.dao;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import de.romankreisel.faktotum.FaktotumObject;
import de.romankreisel.faktotum.datamodel.FaktotumEntity;
/**
* The logic to persist and remove entities to/from the database.
*
* @author Roman Kreisel <mail@romankreisel.de>
*/
@Stateless
public class StorageService extends FaktotumObject {
@PersistenceContext(unitName = "Faktotum")
private EntityManager entityManager;
public CriteriaBuilder getCriteriaBuilder() {
return this.getEntityManager().getCriteriaBuilder();
}
public EntityManager getEntityManager() {
if (this.entityManager == null) {
throw new NullPointerException("EntityManager not initialized!");
}
return this.entityManager;
}
/**
* Persists an entity
*
* @param entity
* the entity to be persisted
*/
public void persist(FaktotumEntity entity) {
this.getEntityManager().persist(entity);
}
/**
* Removes an entity
*
* @param entity
* the entity to be removed
*/
public void remove(FaktotumEntity entity) {
this.getEntityManager().remove(entity);
}
/**
* Normally the entity manager is injected, but this method can be used e.G.
* for JUnit tests.
*
* @param entityManager
*/
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}