package at.gv.egovernment.moa.id.process.dao; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Restrictions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.egovernment.moa.id.commons.db.MOASessionDBUtils; import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException; /** * Database backed implementation of the {@link ProcessInstanceStoreDAO} * interface. */ public class ProcessInstanceStoreDAOImpl implements ProcessInstanceStoreDAO { private Logger log = LoggerFactory.getLogger(getClass()); private static ProcessInstanceStoreDAO instance = new ProcessInstanceStoreDAOImpl(); public static ProcessInstanceStoreDAO getInstance() { return instance; } @Override public void saveOrUpdate(ProcessInstanceStore pIStore) throws MOADatabaseException { try { MOASessionDBUtils.saveOrUpdate(pIStore); log.debug("Store process instance with='{}' in the database.", pIStore.getProcessInstanceId()); } catch (MOADatabaseException e) { log.warn("ProcessInstanceStore could not be persisted to the database."); throw e; } } @Override public ProcessInstanceStore load(String processInstanceId) throws MOADatabaseException { log.debug("Retrieve the ProcessInstanceStore for id='{}' from the database.", processInstanceId); Session session = MOASessionDBUtils.getCurrentSession(); ProcessInstanceStore result = null; Transaction tx = null; synchronized (session) { try { tx = session.beginTransaction(); // select all where processInstanceId equals processInstanceId Criteria criteria = session.createCriteria(ProcessInstanceStore.class); criteria.add(Restrictions.eq("processInstanceId", processInstanceId)); result = (ProcessInstanceStore) criteria.uniqueResult(); tx.commit(); } catch (Exception e) { log.error("There are multiple persisted processes with the same process instance id '{}'", processInstanceId); if (tx != null) { tx.rollback(); } throw e; } finally { MOASessionDBUtils.closeSession(); } } if (result != null) { log.debug("Found process instance store for instance '{}'.", processInstanceId); } else { log.debug("Unable to find process instance store for instance '{}'.", processInstanceId); } return result; } @Override public void remove(String processInstanceId) throws MOADatabaseException { log.debug("Delete the ProcessInstanceStore for id='{}' from the database.", processInstanceId); ProcessInstanceStore toBeDeleted = load(processInstanceId); if (toBeDeleted != null) { if (!MOASessionDBUtils.delete(toBeDeleted)) { log.warn("Could not delete the ProcessInstanceStore with process instance id '{}'", processInstanceId); throw new MOADatabaseException("Could not delete the ProcessInstanceStore with process instance id '" + processInstanceId + "'."); } } else log.trace("ProcessInstanceStore for id='{}' was not found and could therefore not be deleted.", processInstanceId); } }