From 3fe025d3509ccccf6e9ef9e8d4bfd0ee51fb1c7b Mon Sep 17 00:00:00 2001 From: rkv Date: Wed, 17 Oct 2012 08:00:33 +0000 Subject: [PATCH] Creation of a new study is fixed. Database.getSessoin is not used now during creation of a new study. --- .../org/splat/service/ScenarioService.java | 40 ++- .../splat/service/ScenarioServiceImpl.java | 289 ++++++++++++------ .../service/SimulationContextServiceImpl.java | 54 ++-- .../org/splat/service/StudyServiceImpl.java | 20 +- .../src/spring/businessServiceContext.xml | 10 +- 5 files changed, 272 insertions(+), 141 deletions(-) diff --git a/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java b/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java index 44bdc43..498f6eb 100644 --- a/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java +++ b/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java @@ -17,18 +17,54 @@ import org.splat.kernel.MultiplyDefinedException; import org.splat.som.Step; /** - * @author RKV - * + * Scenario service interface. + * @author Roman Kozlov (RKV) */ public interface ScenarioService { + /** + * Add a new knowledge element to the scenario. + * + * @param aScenario + * the scenario + * @param kprop + * knowledge element properties + * @return the created knowledge element + * @throws MissedPropertyException + * if a mandatory property is missed + * @throws InvalidPropertyException + * if some property doesn't exist + * @throws MultiplyDefinedException + * if some property is defined several times + */ public KnowledgeElement addKnowledgeElement(Scenario aScenario, KnowledgeElement.Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException; + /** + * Check in the scenario. + * + * @param aScenario + * the scenario to check in + */ public void checkin(Scenario aScenario); + /** + * Copy contents from other scenario up to its given step into the given scenario. + * + * @param scenario + * the target scenario + * @param lastep + * the last processed step of the source scenario + */ public void copyContentsUpTo(Scenario scenario, Step lastep); + /** + * Check if the scenario is empty, i.d. no one of its steps doesn't contain any knowledge elements or documents. + * + * @param scenario + * the scenario to check + * @return true if the scenario is empty + */ public boolean isEmpty(Scenario scenario); } diff --git a/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java index 0b6ba12..3b0e02d 100644 --- a/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java @@ -20,34 +20,60 @@ import org.hibernate.Session; import org.hibernate.Transaction; import org.splat.dal.bo.kernel.User; import org.splat.dal.bo.som.KnowledgeElement; -import org.splat.dal.bo.som.ProjectElement; import org.splat.dal.bo.som.Publication; import org.splat.dal.bo.som.Scenario; import org.splat.dal.bo.som.SimulationContext; -import org.splat.dal.bo.som.Study; import org.splat.dal.dao.som.Database; +import org.splat.dal.dao.som.KnowledgeElementDAO; +import org.splat.dal.dao.som.ScenarioDAO; import org.splat.kernel.InvalidPropertyException; import org.splat.kernel.MissedPropertyException; import org.splat.kernel.MultiplyDefinedException; import org.splat.service.technical.IndexService; import org.splat.som.Step; +import org.springframework.transaction.annotation.Transactional; /** - * @author RKV + * Scenario service implementation. * + * @author Roman Kozlov (RKV) */ public class ScenarioServiceImpl implements ScenarioService { + /** + * Logger for this class. + */ protected final static Logger logger = Logger .getLogger(ScenarioServiceImpl.class); + /** + * Injected index service. + */ private IndexService _indexService; + /** + * Injected step service. + */ private StepService _stepService; + /** + * Injected publication service. + */ private PublicationService _publicationService; + /** + * Injected project element service. + */ private ProjectElementService _projectElementService; + /** + * Injected knowledge element DAO. + */ + private KnowledgeElementDAO _knowledgeElementDAO; + /** + * Injected scenario DAO. + */ + private ScenarioDAO _scenarioDAO; /** * Get the projectElementService. + * * @return the projectElementService */ public ProjectElementService getProjectElementService() { @@ -56,14 +82,18 @@ public class ScenarioServiceImpl implements ScenarioService { /** * Set the projectElementService. - * @param projectElementService the projectElementService to set + * + * @param projectElementService + * the projectElementService to set */ - public void setProjectElementService(ProjectElementService projectElementService) { + public void setProjectElementService( + ProjectElementService projectElementService) { _projectElementService = projectElementService; } /** * Get the publicationService. + * * @return the publicationService */ public PublicationService getPublicationService() { @@ -72,7 +102,9 @@ public class ScenarioServiceImpl implements ScenarioService { /** * Set the publicationService. - * @param publicationService the publicationService to set + * + * @param publicationService + * the publicationService to set */ public void setPublicationService(PublicationService publicationService) { _publicationService = publicationService; @@ -80,6 +112,7 @@ public class ScenarioServiceImpl implements ScenarioService { /** * Get the stepService. + * * @return the stepService */ public StepService getStepService() { @@ -88,26 +121,32 @@ public class ScenarioServiceImpl implements ScenarioService { /** * Set the stepService. - * @param stepService the stepService to set + * + * @param stepService + * the stepService to set */ public void setStepService(StepService stepService) { _stepService = stepService; } + /** + * {@inheritDoc} + * + * @see org.splat.service.ScenarioService#addKnowledgeElement(org.splat.dal.bo.som.Scenario, + * org.splat.dal.bo.som.KnowledgeElement.Properties) + */ + @Transactional public KnowledgeElement addKnowledgeElement(Scenario aScenario, KnowledgeElement.Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException { - // ------------------------------------------------------------------------------- - KnowledgeElement kelm = new KnowledgeElement( - kprop.setOwnerScenario(aScenario)); - Session session = Database.getSession(); - Transaction transax = session.getTransaction(); + KnowledgeElement kelm = new KnowledgeElement(kprop + .setOwnerScenario(aScenario)); try { - session.save(kelm); - session.flush(); //RKV + getKnowledgeElementDAO().create(kelm); + // RKV: commented because of BatchUpdateException during creation of a new study: session.flush(); //RKV // Update of my persistent data aScenario.getKnowledgeElements().add(kelm); - session.merge(aScenario); //RKV + // RKV: commented because of NullPointerException during creation of a new study: session.merge(aScenario); //RKV // Update of my transient data List known = aScenario .getKnowledgeElementsOf(kelm.getType()); // Initializes this.known, if not yet done @@ -120,59 +159,53 @@ public class ScenarioServiceImpl implements ScenarioService { // Update of the index of Knowledge Elements getIndexService().add(kelm); update(aScenario); - return kelm; - } catch (RuntimeException e) { - if (transax != null && transax.isActive()) { - // Second try-catch as the rollback could fail as well - try { - transax.rollback(); - } catch (HibernateException error) { - logger.debug("Error rolling back transaction", error); - } - // Throw again the first exception - throw e; - } - return null; + } catch (IOException error) { - logger.error( - "Unable to index the knowedge element '" + kelm.getIndex() - + "', reason:", error); - return null; + logger.error("Unable to index the knowedge element '" + + kelm.getIndex() + "', reason:", error); + kelm = null; } + + return kelm; } + /** + * Update the scenario in the database. + * + * @param aScenario + * the scenario to update + * @return true if updating succeeded + */ + @Transactional private boolean update(Scenario aScenario) { - // --------------------------- + boolean isOk = false; try { - Database.getSession().update(aScenario); // Update of relational base - return true; + getScenarioDAO().update(aScenario); // Update of relational base + isOk = true; } catch (Exception error) { logger.error("Unable to re-index the knowledge element '" + aScenario.getIndex() + "', reason:", error); - return false; } + return isOk; } /** - * @return + * {@inheritDoc} + * + * @see org.splat.service.ScenarioService#checkin(org.splat.dal.bo.som.Scenario) */ - public IndexService getIndexService() { - return _indexService; - } - - public void setIndexService(IndexService indexService) { - _indexService = indexService; - } - public void checkin(Scenario aScenario) { - // ---------------------- aScenario.setUser(null); aScenario.setLastModificationDate(Calendar.getInstance().getTime()); Database.getSession().update(aScenario); } + /** + * @param aScenario + * @param user + * @return + */ public boolean checkout(Scenario aScenario, User user) { - // ----------------------------------- if (!aScenario.getOwnerStudy().isStaffedBy(user)) return false; @@ -182,49 +215,123 @@ public class ScenarioServiceImpl implements ScenarioService { return true; } - // ============================================================================================================================== - // Private services - // ============================================================================================================================== - - public void copyContentsUpTo (Scenario scenario, Step lastep) { - // ------------------------------------------- - Scenario base = (Scenario)lastep.getOwner(); - Step[] from = getProjectElementService().getSteps(base); - Step[] to = getProjectElementService().getSteps(scenario); - for (int i=0; i lastep.getNumber()) break; - - List docs = step.getAllDocuments(); - for (Iterator j=docs.iterator(); j.hasNext(); ) { - Publication doc = getPublicationService().copy(j.next(), scenario); // Creation of a new reference to the document - // Database.getSession().save(doc); Publications MUST be saved later through cascading when saving the scenario - to[i].add(doc); - } - List ctex = step.getAllSimulationContexts(); - for (Iterator j=ctex.iterator(); j.hasNext(); ) { - getStepService().addSimulationContext(to[i], j.next()); - } - } - } - - public boolean isEmpty (Scenario scenario) { - // ------------------------- - Step[] mystep = getProjectElementService().getSteps(scenario); - for (int i=0; i lastep.getNumber()) + break; + + List docs = step.getAllDocuments(); + for (Iterator j = docs.iterator(); j.hasNext();) { + Publication doc = getPublicationService().copy(j.next(), + scenario); // Creation of a new reference to the document + // Database.getSession().save(doc); Publications MUST be saved later through cascading when saving the scenario + to[i].add(doc); + } + List ctex = step.getAllSimulationContexts(); + for (Iterator j = ctex.iterator(); j.hasNext();) { + getStepService().addSimulationContext(to[i], j.next()); + } + } + } + + /** + * {@inheritDoc} + * + * @see org.splat.service.ScenarioService#isEmpty(org.splat.dal.bo.som.Scenario) + */ + public boolean isEmpty(Scenario scenario) { + Step[] mystep = getProjectElementService().getSteps(scenario); + boolean isEmp = true; + for (int i = 0; i < mystep.length; i++) { + if (mystep[i].isStarted()) { + isEmp = false; + break; + } + } + return isEmp; + } + + /** + * @param scenario + * @return + */ + public boolean isFinished(Scenario scenario) { + Step[] mystep = getProjectElementService().getSteps(scenario); + boolean notempty = false; // If this is empty, this is not finished + for (int i = 0; i < mystep.length; i++) { + if (!mystep[i].isStarted()) + continue; + if (!mystep[i].isFinished()) + return false; + notempty = true; + } + return notempty; + } + + /** + * Get the knowledgeElementDAO. + * + * @return the knowledgeElementDAO + */ + public KnowledgeElementDAO getKnowledgeElementDAO() { + return _knowledgeElementDAO; + } + + /** + * Set the knowledgeElementDAO. + * + * @param knowledgeElementDAO + * the knowledgeElementDAO to set + */ + public void setKnowledgeElementDAO(KnowledgeElementDAO knowledgeElementDAO) { + _knowledgeElementDAO = knowledgeElementDAO; + } + + /** + * Get the indexService. + * + * @return the indexService + */ + public IndexService getIndexService() { + return _indexService; + } + + /** + * Set the indexService. + * + * @param indexService + * the indexService to set + */ + public void setIndexService(IndexService indexService) { + _indexService = indexService; + } + + /** + * Get the scenarioDAO. + * + * @return the scenarioDAO + */ + public ScenarioDAO getScenarioDAO() { + return _scenarioDAO; + } + + /** + * Set the scenarioDAO. + * + * @param scenarioDAO + * the scenarioDAO to set + */ + public void setScenarioDAO(ScenarioDAO scenarioDAO) { + _scenarioDAO = scenarioDAO; + } } diff --git a/Workspace/Siman-Common/src/org/splat/service/SimulationContextServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/SimulationContextServiceImpl.java index 729e7d4..decbcde 100644 --- a/Workspace/Siman-Common/src/org/splat/service/SimulationContextServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/SimulationContextServiceImpl.java @@ -11,11 +11,8 @@ package org.splat.service; import java.util.List; -import org.hibernate.Criteria; import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Example; import org.hibernate.criterion.Order; -import org.hibernate.criterion.PropertyExpression; import org.hibernate.criterion.Restrictions; import org.splat.dal.bo.som.ProgressState; import org.splat.dal.bo.som.SimulationContext; @@ -86,45 +83,47 @@ public class SimulationContextServiceImpl implements SimulationContextService { * * @see org.splat.service.SimulationContextService#selectSimulationContextsWhere(org.splat.dal.bo.som.SimulationContext.Properties) */ - @Transactional(readOnly=true) + @Transactional(readOnly = true) public List selectSimulationContextsWhere( SimulationContext.Properties cprop) { -// StringBuffer query = new StringBuffer("from SimulationContext"); -// String separator = " where"; + // StringBuffer query = new StringBuffer("from SimulationContext"); + // String separator = " where"; SimulationContextType celt = cprop.getType(); String value = cprop.getValue(); ProgressState state = cprop.getProgressState(); -// String order = ""; + // String order = ""; Criterion aCondition = null; Order anOrder = null; if (celt != null) { -// query = query.append(separator).append(" type='").append( -// celt.getIndex()).append("'"); -// separator = " and"; -// order = " order by value asc"; + // query = query.append(separator).append(" type='").append( + // celt.getIndex()).append("'"); + // separator = " and"; + // order = " order by value asc"; aCondition = Restrictions.eq("type", celt); anOrder = Order.asc("value"); } if (value != null) { -// query = query.append(separator).append(" value='").append(value) -// .append("'"); -// separator = " and"; - - aCondition = Restrictions.and(aCondition, Restrictions.eq("value", value)); + // query = query.append(separator).append(" value='").append(value) + // .append("'"); + // separator = " and"; + + aCondition = Restrictions.and(aCondition, Restrictions.eq("value", + value)); } if (state != null) { -// query = query.append(separator).append(" state='").append(state) -// .append("'"); + // query = query.append(separator).append(" state='").append(state) + // .append("'"); if (celt == null) { -// order = " order by type asc"; + // order = " order by type asc"; anOrder = Order.asc("type"); } - aCondition = Restrictions.and(aCondition, Restrictions.eq("state", state)); + aCondition = Restrictions.and(aCondition, Restrictions.eq("state", + state)); } -// query.append(order); -// return (List) Database.getSession().createQuery( -// query.toString()).list(); + // query.append(order); + // return (List) Database.getSession().createQuery( + // query.toString()).list(); return getSimulationContextDAO().getFilteredList(aCondition, anOrder); } @@ -164,11 +163,10 @@ public class SimulationContextServiceImpl implements SimulationContextService { */ @SuppressWarnings("unchecked") public List selectAllTypes() { - // ----------------------------------------------------------- - StringBuffer query = new StringBuffer("from SimulationContextType"); // Useless to order by names as the result mixes localized + // Useless to order by names as the result mixes localized // and non localized types - query = query.append(" order by step asc"); - return Database.getSession().createQuery(query.toString()).list(); + return getSimulationContextTypeDAO().getFilteredList(null, + Order.asc("step")); } /** @@ -231,7 +229,7 @@ public class SimulationContextServiceImpl implements SimulationContextService { * * @see org.splat.service.SimulationContextService#selectType(java.lang.String) */ - @Transactional(readOnly=true) + @Transactional(readOnly = true) public SimulationContextType selectType(String name) { return getSimulationContextTypeDAO().findByCriteria( Restrictions.eq("name", name)); diff --git a/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java index 6a86110..a917ac6 100644 --- a/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java @@ -499,22 +499,10 @@ public class StudyServiceImpl implements StudyService { * @return found id builder */ private IDBuilder selectIDBuilder(Date date) { - SimpleDateFormat year = new SimpleDateFormat("yyyy"); - String cycle = year.format(date); - // StringBuffer buffer = new StringBuffer("from IDBuilder where cycle='") - // .append(cycle).append("'"); - // String qstring = buffer.toString(); - // Query query = Database.getSession().createQuery(qstring); - // IDBuilder result = (IDBuilder) query.uniqueResult(); - - // return result; - - // Calendar aDate = Calendar.getInstance(); - // aDate.setTime(date); - // return getIDBuilderDAO().findByCriteria( - // Restrictions.eq("cycle", aDate.get(Calendar.YEAR))); - return getIDBuilderDAO() - .findByCriteria(Restrictions.eq("cycle", cycle)); + Calendar aDate = Calendar.getInstance(); + aDate.setTime(date); + return getIDBuilderDAO().findByCriteria( + Restrictions.eq("cycle", aDate.get(Calendar.YEAR))); } /** diff --git a/Workspace/Siman-Common/src/spring/businessServiceContext.xml b/Workspace/Siman-Common/src/spring/businessServiceContext.xml index a5a441c..5ada1f4 100644 --- a/Workspace/Siman-Common/src/spring/businessServiceContext.xml +++ b/Workspace/Siman-Common/src/spring/businessServiceContext.xml @@ -50,6 +50,8 @@ http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> ref="projectElementService" /> + + ref="projectElementService" /> - - - - + + + + \ No newline at end of file -- 2.39.2