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 <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
*/
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() {
/**
* 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() {
/**
* Set the publicationService.
- * @param publicationService the publicationService to set
+ *
+ * @param publicationService
+ * the publicationService to set
*/
public void setPublicationService(PublicationService publicationService) {
_publicationService = publicationService;
/**
* Get the stepService.
+ *
* @return the stepService
*/
public StepService getStepService() {
/**
* 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<KnowledgeElement> known = aScenario
.getKnowledgeElementsOf(kelm.getType()); // Initializes this.known, if not yet done
// 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;
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<from.length; i++) {
- Step step = from[i];
- if (step.getNumber() > lastep.getNumber()) break;
-
- List<Publication> docs = step.getAllDocuments();
- for (Iterator<Publication> 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<SimulationContext> ctex = step.getAllSimulationContexts();
- for (Iterator<SimulationContext> 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<mystep.length; i++) if (mystep[i].isStarted()) return false;
- return true;
- }
-
- 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;
- }
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.ScenarioService#copyContentsUpTo(org.splat.dal.bo.som.Scenario, org.splat.som.Step)
+ */
+ 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 < from.length; i++) {
+ Step step = from[i];
+ if (step.getNumber() > lastep.getNumber())
+ break;
+
+ List<Publication> docs = step.getAllDocuments();
+ for (Iterator<Publication> 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<SimulationContext> ctex = step.getAllSimulationContexts();
+ for (Iterator<SimulationContext> 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;
+ }
}