]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java
Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / StudyServiceImpl.java
index ef71552cc0e829f95739f0626cbd1b3ebedd8376..99e22cd67750051956e9e8aa195dc651dab4fd84 100644 (file)
 package org.splat.service;
 
 import java.io.IOException;
+import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 
-import org.apache.log4j.Logger;
+import org.hibernate.Query;
 import org.hibernate.Session;
-import org.hibernate.Transaction;
 import org.splat.dal.bo.kernel.Relation;
 import org.splat.dal.bo.kernel.User;
 import org.splat.dal.bo.som.ActorRelation;
@@ -32,13 +32,13 @@ import org.splat.dal.bo.som.ProgressState;
 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.SimulationContextType;
 import org.splat.dal.bo.som.Study;
 import org.splat.dal.bo.som.ValidationCycle;
 import org.splat.dal.bo.som.ValidationCycleRelation;
 import org.splat.dal.bo.som.Visibility;
 import org.splat.dal.bo.som.Study.Properties;
 import org.splat.dal.dao.som.Database;
+import org.splat.dal.dao.som.StudyDAO;
 import org.splat.kernel.InvalidPropertyException;
 import org.splat.kernel.MissedPropertyException;
 import org.splat.kernel.MultiplyDefinedException;
@@ -47,56 +47,59 @@ import org.splat.log.AppLogger;
 import org.splat.service.technical.IndexService;
 import org.splat.service.technical.ProjectSettingsService;
 import org.splat.som.Revision;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * This class defines all methods for creation, modification the study.
+ * 
  * @author Maria KRUCHININA
- *
+ * 
  */
 public class StudyServiceImpl implements StudyService {
 
        /**
         * logger for the service.
         */
-       public final static AppLogger logger = AppLogger.getLogger(StudyServiceImpl.class);
+       public final static AppLogger logger = AppLogger
+                       .getLogger(StudyServiceImpl.class);
 
+       /**
+        * Injected index service.
+        */
        private IndexService _indexService;
 
+       /**
+        * Injected step service.
+        */
        private StepService _stepService;
 
+       /**
+        * Injected scenario service.
+        */
        private ScenarioService _scenarioService;
 
+       /**
+        * Injected project service.
+        */
        private ProjectSettingsService _projectSettingsService;
 
+       /**
+        * Injected project element service.
+        */
        private ProjectElementService _projectElementService;
-       
+
        /**
-        * Get the simulation context list for displaying drop-down list values populating
-        * on the "Create new study" screen.
+        * Injected study DAO.
+        */
+       private StudyDAO _studyDAO;
+
+       /** 
         * {@inheritDoc}
-        * @see org.splat.service.StudyService#getSimulationContextList()
+        * @see org.splat.service.StudyService#selectStudy(long)
         */
-       public List<SimulationContext> getSimulationContextList() {
-               //TODO: remove the commit transaction ...
-               Session      connex  = Database.getSession();
-               Transaction  transax = connex.beginTransaction();
-               
-               SimulationContext.Properties cprop   = new SimulationContext.Properties();
-           SimulationContextType        product = SimulationContext.selectType("product");
-           List<SimulationContext> resList = Database.selectSimulationContextsWhere(cprop.setType(product));
-           
-           transax.commit();
-                 
-           return resList;
-       }
-       
+       @Transactional
        public Study selectStudy(long index) {
-               // -------------------------------------------
-               StringBuffer query = new StringBuffer("from Study where rid='").append(
-                               index).append("'");
-               Study result = (Study) Database.getSession().createQuery(
-                               query.toString()).uniqueResult();
-
+               Study result = getStudyDAO().get(index);
                result.loadWorkflow();
                return result;
        }
@@ -112,6 +115,7 @@ public class StudyServiceImpl implements StudyService {
                return result;
        }
 
+       @Transactional
        public Study createStudy(Study.Properties sprop)
                        throws MissedPropertyException, InvalidPropertyException,
                        MultiplyDefinedException, RuntimeException {
@@ -119,7 +123,7 @@ public class StudyServiceImpl implements StudyService {
                Study study = new Study(sprop);
 
                buildReference(study);
-               Database.getSession().save(study); // TODO: use StudyDAO and throw out Database.
+               getStudyDAO().create(study);
                try {
                        IndexService lucin = getIndex();
                        lucin.add(study);
@@ -211,7 +215,7 @@ public class StudyServiceImpl implements StudyService {
 
        public boolean buildReference(Study aStudy) {
                String pattern = aStudy.getReference(); // The study being supposed just created, its reference is the reference pattern
-               IDBuilder tool = Database.selectIDBuilder(aStudy.getDate());
+               IDBuilder tool = selectIDBuilder(aStudy.getDate());
                if (tool == null) {
                        tool = new IDBuilder(aStudy.getDate());
                        Database.getSession().save(tool);
@@ -220,6 +224,19 @@ public class StudyServiceImpl implements StudyService {
                return true;
        }
 
+       public 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;
+       }
+
        public boolean publishes(Study aStudy, Document doc) {
                // ---------------------------------------
                if (!aStudy.publishes(doc)) {
@@ -367,9 +384,9 @@ public class StudyServiceImpl implements StudyService {
                KnowledgeElementType ucase = KnowledgeElement.selectType("usecase");
                KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
                User admin = UserDirectory.selectUser(1); // First user created when creating the database
-               kprop.setType(ucase).setTitle(aStudy.getTitle())
-                               .setValue(scenario.getTitle()).setAuthor(admin); // Internal Knowledge Element required by the validation process of
-                                                                                                                                       // knowledges
+               kprop.setType(ucase).setTitle(aStudy.getTitle()).setValue(
+                               scenario.getTitle()).setAuthor(admin); // Internal Knowledge Element required by the validation process of
+               // knowledges
                getScenarioService().addKnowledgeElement(scenario, kprop);
                return scenario;
        }
@@ -462,17 +479,6 @@ public class StudyServiceImpl implements StudyService {
                }
        }
 
-       /**
-        * @return
-        */
-       public IndexService getIndexService() {
-               return _indexService;
-       }
-
-       public void setIndexService(IndexService indexService) {
-               _indexService = indexService;
-       }
-
        /**
         * Get project settings.
         * 
@@ -514,6 +520,7 @@ public class StudyServiceImpl implements StudyService {
 
        /**
         * Get the stepService.
+        * 
         * @return the stepService
         */
        public StepService getStepService() {
@@ -522,9 +529,43 @@ public class StudyServiceImpl implements StudyService {
 
        /**
         * Set the stepService.
-        * @param stepService the stepService to set
+        * 
+        * @param stepService
+        *            the stepService to set
         */
        public void setStepService(StepService stepService) {
                _stepService = stepService;
        }
+
+       /**
+        * 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 studyDAO.
+        * @return the studyDAO
+        */
+       public StudyDAO getStudyDAO() {
+               return _studyDAO;
+       }
+
+       /**
+        * Set the studyDAO.
+        * @param studyDAO the studyDAO to set
+        */
+       public void setStudyDAO(StudyDAO studyDAO) {
+               _studyDAO = studyDAO;
+       }
 }