From 4b8f2b481636ef71146d382f386f9c297da7a64b Mon Sep 17 00:00:00 2001 From: Gerwin Gsenger Date: Fri, 30 Jan 2015 11:11:40 +0100 Subject: implement DAO methods, add exeptions to ProcessInstanceStoreDAO interface --- .../id/config/auth/AuthConfigurationProvider.java | 3 +- .../id/process/dao/ProcessInstanceStoreDAO.java | 35 ++++----- .../process/dao/ProcessInstanceStoreDAOImpl.java | 91 ++++++++++++++++++---- 3 files changed, 95 insertions(+), 34 deletions(-) diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/AuthConfigurationProvider.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/AuthConfigurationProvider.java index a67badf49..0dfd2236f 100644 --- a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/AuthConfigurationProvider.java +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/AuthConfigurationProvider.java @@ -110,10 +110,10 @@ import at.gv.egovernment.moa.id.config.auth.data.ProtocolAllowed; import at.gv.egovernment.moa.id.config.legacy.BuildFromLegacyConfig; import at.gv.egovernment.moa.id.config.stork.STORKConfig; import at.gv.egovernment.moa.id.data.IssuerAndSerial; +import at.gv.egovernment.moa.id.process.dao.ProcessInstanceStore; import at.gv.egovernment.moa.id.protocols.pvp2x.config.MOADefaultBootstrap; import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.util.MiscUtil; -import at.gv.util.config.EgovUtilConfiguration; import at.gv.util.config.EgovUtilPropertiesConfiguration; /** @@ -344,6 +344,7 @@ public class AuthConfigurationProvider extends ConfigurationProvider { config.addAnnotatedClass(OldSSOSessionIDStore.class); config.addAnnotatedClass(ExceptionStore.class); config.addAnnotatedClass(InterfederationSessionStore.class); + config.addAnnotatedClass(ProcessInstanceStore.class); config.addProperties(moaSessionProp); MOASessionDBUtils.initHibernate(config, moaSessionProp); diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAO.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAO.java index 57489c33e..0aa6f80cd 100644 --- a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAO.java +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAO.java @@ -2,6 +2,7 @@ package at.gv.egovernment.moa.id.process.dao; import java.util.List; +import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException; import at.gv.egovernment.moa.id.process.ProcessInstance; public interface ProcessInstanceStoreDAO { @@ -12,18 +13,23 @@ public interface ProcessInstanceStoreDAO { * * @param pIStore * the {@link ProcessInstanceStore} to persist. + * @throws MOADatabaseException + * is thrown if a problem occurs while accessing the database. */ - void save(ProcessInstanceStore pIStore); + void save(ProcessInstanceStore pIStore) throws MOADatabaseException; /** - * Loads a {@link ProcessInstanceStore}, defined by - * {@code processInstanceID} from the database. + * Returns a {@link ProcessInstanceStore}, defined by + * {@code processInstanceID} from the database, or {@code null} if the + * object could not be found. * * @param processInstanceID * the id of the {@code ProcessInstanceStore} to retrieve. - * @return a ProcessInstanceStore. + * @return a ProcessInstanceStore, or {@code null}. + * @throws MOADatabaseException + * is thrown if a problem occurs while accessing the database. */ - ProcessInstanceStore load(String processInstanceId); + ProcessInstanceStore load(String processInstanceId) throws MOADatabaseException; /** * Deletes the {@link ProcessInstance} corresponding with the @@ -31,26 +37,19 @@ public interface ProcessInstanceStoreDAO { * * @param processInstanceID * the id of the {@code ProcessInstance} to be deleted. + * @throws MOADatabaseException + * is thrown if a problem occurs while accessing the database. */ - void remove(String processInstanceId); + void remove(String processInstanceId) throws MOADatabaseException; /** * Returns all {@link ProcessInstanceStore} objects stored in the database. * The returned list may be empty, but never {@code null}. * * @return a list of {@link ProcessInstanceStore} (never {@code null}). + * @throws MOADatabaseException + * is thrown if a problem occurs while accessing the database. */ - List getAllProcessInstanceStores(); - - /** - * Returns the specific {@link ProcessInstanceStore} object corresponding to - * the given {@code processInstanceId}, or {@code null} if the object could - * not be found. - * - * @param processInstanceId - * the processInstanceId to search. - * @return the ProcessInstanceStore for the given id, or {@code null}. - */ - ProcessInstanceStore getProcessInstance(String processInstanceId); + List getAllProcessInstanceStores() throws MOADatabaseException; } diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAOImpl.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAOImpl.java index cde34acd1..f5fb71145 100644 --- a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAOImpl.java +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAOImpl.java @@ -1,10 +1,18 @@ package at.gv.egovernment.moa.id.process.dao; +import java.util.Collections; import java.util.List; +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. @@ -20,32 +28,85 @@ public class ProcessInstanceStoreDAOImpl implements ProcessInstanceStoreDAO { } @Override - public void save(ProcessInstanceStore pIStore) { - // TODO Auto-generated method stub + public void save(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) { - // TODO Auto-generated method stub - return null; - } + public ProcessInstanceStore load(String processInstanceId) throws MOADatabaseException { - @Override - public void remove(String processInstanceId) { - // TODO Auto-generated method stub + log.debug("Retrieve the ProcessInstanceStore for id='{}' from the database.", processInstanceId); + Session session = MOASessionDBUtils.getCurrentSession(); + ProcessInstanceStore result = null; + Transaction tx = null; + try { + synchronized (session) { + 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(); + } + return result; } @Override - public List getAllProcessInstanceStores() { - // TODO Auto-generated method stub - return null; + public void remove(String processInstanceId) throws MOADatabaseException { + + log.debug("Delete the ProcessInstanceStore for id='{}' from the database.", processInstanceId); + + ProcessInstanceStore toBeDeleted = load(processInstanceId); + boolean result = MOASessionDBUtils.delete(toBeDeleted); + if (result == false) { + 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 + "'."); + } } + @SuppressWarnings("unchecked") @Override - public ProcessInstanceStore getProcessInstance(String processInstanceId) { - // TODO Auto-generated method stub - return null; + public List getAllProcessInstanceStores() throws MOADatabaseException { + log.debug("Retrieve a list with all ProcessInstanceStores from the database."); + Session session = MOASessionDBUtils.getCurrentSession(); + + List result = Collections.emptyList(); + Transaction tx = null; + try { + synchronized (session) { + tx = session.beginTransaction(); + // select all + result = session.createCriteria(ProcessInstanceStore.class).list(); + tx.commit(); + } + } catch (Exception e) { + log.error("A problem occured while retrieving all stored ProcessInstanceStores."); + if (tx != null) { + tx.rollback(); + } + throw e; + } finally { + MOASessionDBUtils.closeSession(); + } + return result; } } -- cgit v1.2.3 From 78a3caea21bdd5475007b958d59b23b1d3deccfb Mon Sep 17 00:00:00 2001 From: Gerwin Gsenger Date: Fri, 30 Jan 2015 12:27:45 +0100 Subject: small DAO code review from Thomas --- .../process/dao/ProcessInstanceStoreDAOImpl.java | 50 ++++++++++++---------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAOImpl.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAOImpl.java index f5fb71145..4b7468013 100644 --- a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAOImpl.java +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/process/dao/ProcessInstanceStoreDAOImpl.java @@ -46,24 +46,26 @@ public class ProcessInstanceStoreDAOImpl implements ProcessInstanceStoreDAO { ProcessInstanceStore result = null; Transaction tx = null; - try { - synchronized (session) { + 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(); } - } 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(); } return result; } @@ -74,8 +76,8 @@ public class ProcessInstanceStoreDAOImpl implements ProcessInstanceStoreDAO { log.debug("Delete the ProcessInstanceStore for id='{}' from the database.", processInstanceId); ProcessInstanceStore toBeDeleted = load(processInstanceId); - boolean result = MOASessionDBUtils.delete(toBeDeleted); - if (result == false) { + boolean deleted = MOASessionDBUtils.delete(toBeDeleted); + if (!deleted) { 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 + "'."); @@ -90,21 +92,23 @@ public class ProcessInstanceStoreDAOImpl implements ProcessInstanceStoreDAO { List result = Collections.emptyList(); Transaction tx = null; - try { - synchronized (session) { + synchronized (session) { + try { + tx = session.beginTransaction(); // select all result = session.createCriteria(ProcessInstanceStore.class).list(); tx.commit(); + + } catch (Exception e) { + log.error("A problem occured while retrieving all stored ProcessInstanceStores."); + if (tx != null) { + tx.rollback(); + } + throw e; + } finally { + MOASessionDBUtils.closeSession(); } - } catch (Exception e) { - log.error("A problem occured while retrieving all stored ProcessInstanceStores."); - if (tx != null) { - tx.rollback(); - } - throw e; - } finally { - MOASessionDBUtils.closeSession(); } return result; } -- cgit v1.2.3