From 0ee2ca9df4723c0c0547f387fd01dd5b43f10d8b Mon Sep 17 00:00:00 2001 From: rkv Date: Thu, 25 Oct 2012 10:55:45 +0000 Subject: [PATCH] NewStudyAction is improved. Specific business method for creation of a new study is added to the ScenarioService. --- .../src/conf/log-messages.properties | 3 +- .../org/splat/dal/bo/kernel/Persistent.java | 273 ++++++++++-------- .../org/splat/service/ScenarioService.java | 22 ++ .../splat/service/ScenarioServiceImpl.java | 62 +++- .../org/splat/service/StudyServiceImpl.java | 26 +- .../src/spring/businessServiceContext.xml | 2 + .../simer/EditKnowledgeElementAction.java | 27 +- .../src/org/splat/simer/NewStudyAction.java | 108 ++++--- 8 files changed, 335 insertions(+), 188 deletions(-) diff --git a/Workspace/Siman-Common/src/conf/log-messages.properties b/Workspace/Siman-Common/src/conf/log-messages.properties index 974cf9e..708f15d 100644 --- a/Workspace/Siman-Common/src/conf/log-messages.properties +++ b/Workspace/Siman-Common/src/conf/log-messages.properties @@ -2,4 +2,5 @@ LCK-000001=Lock reference already exists for user {2}. LCK-000002=Lock reference does not exists. LCK-000003=Lock reference protected and can be only deleted or updated by user {2}. -LCK-000004=Lock reference is timeout and could have been modified by user {2}. \ No newline at end of file +LCK-000004=Lock reference is timeout and could have been modified by user {2}. +STD-000001="Unable to re-index the study #{1}, reason: {2}" \ No newline at end of file diff --git a/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Persistent.java b/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Persistent.java index 42629f2..f9f01d6 100644 --- a/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Persistent.java +++ b/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Persistent.java @@ -6,152 +6,187 @@ import org.splat.kernel.MultiplyDefinedException; import org.splat.kernel.ObjectProperties; /** - * Base implementation of the API Design Pattern of objects which persistence is based on Hibernate.
- * This Design Pattern supports the following features: + * Base implementation of the API Design Pattern of objects which persistence is based on Hibernate.
This Design Pattern supports the + * following features: * - * The API is based on intermediate properties objects used for collecting arguments and checking their validity. - * These properties objects are passed to persistent object constructors and database select functions for execution. - * For example, as based on this Design Pattern, a User object could be created that way: + * The API is based on intermediate properties objects used for collecting arguments and checking their validity. These properties objects + * are passed to persistent object constructors and database select functions for execution. For example, as based on this Design Pattern, a + * User object could be created that way: + * *
- *   User.Properties  args = new User.Properties();
- *   User  user = new User( args.setUsername("mypseudo").setMailAddress("me@provider.domain") );
+ * User.Properties args = new User.Properties();
+ * User user = new User(args.setUsername("mypseudo").setMailAddress(
+ * 		"me@provider.domain"));
  * 
- * Classes implementing this Design Pattern must inherit from Persistent and implement their Properties class as a nested - * subclass of Persistent.Properties.
- *
- * Naturally, as usual with Hibernate, any class can be persistent (you don't need to inherit from Persistent for being - * persistent). Inheriting from Persistent is only a matter of enabling the API supported by the above Design Pattern. + * + * Classes implementing this Design Pattern must inherit from Persistent and implement their Properties class as a nested subclass of + * Persistent.Properties.

Naturally, as usual with Hibernate, any class can be persistent (you don't need to inherit from + * Persistent for being persistent). Inheriting from Persistent is only a matter of enabling the API supported by the above Design Pattern. * * @see ObjectProperties * @see Persistent.Properties - * @author Daniel Brunier-Coulin + * @author Daniel Brunier-Coulin * @copyright OPEN CASCADE 2012 */ - public abstract class Persistent { - private long rid; // Primary key of persistent objects - - protected abstract static class Properties implements ObjectProperties { -// ---------------------------------------------------------------------- - private boolean tobechecked = true; // Property validity check flag - - public void disableCheck () { - tobechecked = false; - } - public boolean mustBeChecked () { - return tobechecked; - } - public void clear () { - tobechecked = true; - } - } - -// ============================================================================================================================== -// Constructors -// ============================================================================================================================== -/** - * Database fetch constructor. - */ - protected Persistent () { -// ----------------------- - rid = 0; // Set when loading the object - } + private long rid; // Primary key of persistent objects + + protected abstract static class Properties implements ObjectProperties { + private long rid; // Primary key of persistent objects + + private boolean tobechecked = true; // Property validity check flag + + public void disableCheck() { + tobechecked = false; + } + + public boolean mustBeChecked() { + return tobechecked; + } + + public void clear() { + tobechecked = true; + } + + /** + * Get the rid. + * + * @return the rid + */ + public long getIndex() { + return rid; + } + + /** + * Set the rid. + * + * @param rid + * the rid to set + */ + public void setIndex(long rid) { + this.rid = rid; + } + } -/** - * Checks the mutual compatibility of arguments previously set in the given properties object, if the validity check is enabled. - * As this validity depends on concrete classes, the check is delegated to subclasses of the given Persistent.Properties. - */ - protected Persistent (ObjectProperties oprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException { -// --------------------------------------------- - if (oprop.mustBeChecked()) oprop.checkValidity(); // Throws one of the above exception if not valid - rid = 0; // Set when saving the object - } - -// ============================================================================================================================== -// Public member functions -// ============================================================================================================================== - - public boolean equals(Object entity) { -// ------------------------------------ - if (entity == null) return false; - if (entity.getClass().equals(this.getClass())) { - Persistent object = (Persistent)entity; - long he = object.getIndex(); // getIndex() is supposed fetching the index if not yet done - long me = this.getIndex(); // getIndex() is supposed fetching the index if not yet done - if (me*he != 0) return (he == me); - if (me+he == 0) return (this == object); - } - return false; - } + // ============================================================================================================================== + // Constructors + // ============================================================================================================================== + /** + * Database fetch constructor. + */ + protected Persistent() { + // ----------------------- + rid = 0; // Set when loading the object + } -/** - * Returns the Persistent ID of this object. The PID is set when saving this object. It is unique in the scope of the class - * of this object only. - * - * @return the PID of this, or 0 if this is not saved. - * @see isSaved() - */ - public long getIndex () { -// ---------------------- - return rid; - } + /** + * Checks the mutual compatibility of arguments previously set in the given properties object, if the validity check is enabled. As this + * validity depends on concrete classes, the check is delegated to subclasses of the given Persistent.Properties. + */ + protected Persistent(ObjectProperties oprop) + throws MissedPropertyException, InvalidPropertyException, + MultiplyDefinedException { + // --------------------------------------------- + if (oprop.mustBeChecked()) + oprop.checkValidity(); // Throws one of the above exception if not valid + rid = 0; // Set when saving the object + } + + // ============================================================================================================================== + // Public member functions + // ============================================================================================================================== + + public boolean equals(Object entity) { + // ------------------------------------ + if (entity == null) + return false; + if (entity.getClass().equals(this.getClass())) { + Persistent object = (Persistent) entity; + long he = object.getIndex(); // getIndex() is supposed fetching the index if not yet done + long me = this.getIndex(); // getIndex() is supposed fetching the index if not yet done + if (me * he != 0) + return (he == me); + if (me + he == 0) + return (this == object); + } + return false; + } + + /** + * Returns the Persistent ID of this object. The PID is set when saving this object. It is unique in the scope of the class of this + * object only. + * + * @return the PID of this, or 0 if this is not saved. + * @see isSaved() + */ + public long getIndex() { + // ---------------------- + return rid; + } /** * Set an id for the object. - * @param anId id to set + * + * @param anId + * id to set */ public void setIndex(long anId) { rid = anId; } - - public int hashCode () { -// ---------------------- - return toString().hashCode(); - } -/** - * Returns true if this object is saved. - * - * @return true if this is saved. - * @see getIndex() - */ - public boolean isSaved () { -// ------------------------- - return (getIndex() != 0); // getIndex() is supposed fetching the index if not yet done - } + public int hashCode() { + // ---------------------- + return toString().hashCode(); + } -/** - * Return a string representing uniquely this object. - * - * @return the unique string representation of this object. - */ - public String toString () { -// ------------------------- - long oid = getIndex(); // getIndex() is supposed fetching the index if not yet done - if (oid == 0) oid = super.hashCode(); //WARNING: Must not call super.toString() as it goes back here (this.toString()) - return new StringBuffer("object ").append(getClass().getName()).append("@").append(oid).toString(); - } + /** + * Returns true if this object is saved. + * + * @return true if this is saved. + * @see getIndex() + */ + public boolean isSaved() { + // ------------------------- + return (getIndex() != 0); // getIndex() is supposed fetching the index if not yet done + } -/** - * Get the rid. - * @return the rid - */ -public long getRid() { - return rid; -} + /** + * Return a string representing uniquely this object. + * + * @return the unique string representation of this object. + */ + public String toString() { + // ------------------------- + long oid = getIndex(); // getIndex() is supposed fetching the index if not yet done + if (oid == 0) + oid = super.hashCode(); // WARNING: Must not call super.toString() as it goes back here (this.toString()) + return new StringBuffer("object ").append(getClass().getName()).append( + "@").append(oid).toString(); + } -/** - * Set the rid. - * @param rid the rid to set - */ -public void setRid(long rid) { - this.rid = rid; -} + /** + * Get the rid. + * + * @return the rid + */ + public long getRid() { + return rid; + } + + /** + * Set the rid. + * + * @param rid + * the rid to set + */ + public void setRid(long rid) { + this.rid = rid; + } } \ No newline at end of file diff --git a/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java b/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java index b5903e2..025a6cb 100644 --- a/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java +++ b/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java @@ -12,6 +12,7 @@ package org.splat.service; import org.splat.dal.bo.kernel.User; import org.splat.dal.bo.som.KnowledgeElement; import org.splat.dal.bo.som.Scenario; +import org.splat.dal.bo.som.SimulationContext; import org.splat.dal.bo.som.Study; import org.splat.kernel.InvalidPropertyException; import org.splat.kernel.MissedPropertyException; @@ -25,6 +26,27 @@ import org.splat.som.Step; */ public interface ScenarioService { + /** + * Create a new study with one scenario and "product" simulation context. + * + * @param sprop + * the study properties + * @param oprop + * the scenario properties + * @param cprop + * the "product" simulation context properties + * @return the created study + * @throws MissedPropertyException + * if a mandatory property is missed + * @throws InvalidPropertyException + * if a property is invalid + * @throws MultiplyDefinedException + * if some property occurs several times + */ + public Study createStudy(Study.Properties sprop, Scenario.Properties oprop, + SimulationContext.Properties cprop) throws MissedPropertyException, + InvalidPropertyException, MultiplyDefinedException; + /** * Add a new scenario to the study. * diff --git a/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java index 0f8ea31..464eff5 100644 --- a/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java @@ -101,6 +101,11 @@ public class ScenarioServiceImpl implements ScenarioService { */ private KnowledgeElementTypeDAO _knowledgeElementTypeDAO; + /** + * Injected simulation context service. + */ + private SimulationContextService _simulationContextService; + /** * Get the projectElementService. * @@ -159,6 +164,41 @@ public class ScenarioServiceImpl implements ScenarioService { _stepService = stepService; } + /** + * Create a new study with one scenario and "product" simulation context. + * + * @param sprop + * the study properties + * @param oprop + * the scenario properties + * @param cprop + * the "product" simulation context properties + * @return the created study + * @throws MissedPropertyException + * if a mandatory property is missed + * @throws InvalidPropertyException + * if a property is invalid + * @throws MultiplyDefinedException + * if some property occurs several times + */ + @Transactional + public Study createStudy(Study.Properties sprop, Scenario.Properties oprop, + SimulationContext.Properties cprop) throws MissedPropertyException, + InvalidPropertyException, MultiplyDefinedException { + Study study = getStudyService().createStudy(sprop); + addScenario(study, oprop); + if (cprop.getIndex() == 0) { // Input of new project context + cprop.setType(getSimulationContextService().selectType("product")) + .setValue(cprop.getValue()); + getStudyService().addProjectContext(study, cprop); + } else { // Selection of existing project context + SimulationContext context = getSimulationContextService() + .selectSimulationContext(cprop.getIndex()); + getStudyService().addProjectContext(study, context); + } + return study; + } + /** * {@inheritDoc} * @@ -179,7 +219,7 @@ public class ScenarioServiceImpl implements ScenarioService { // Get the persistent scenario. Scenario aScenario = getScenarioDAO().get(aScenarioId); // Get persistent objects for creating a new knowledge. - //TODO: Actions must use DTO instead of persistent objects. + // TODO: Actions must use DTO instead of persistent objects. getUserDAO().merge(kprop.getAuthor()); getKnowledgeElementTypeDAO().merge(kprop.getType()); // Create a transient knowledge element related to the given scenario. @@ -559,4 +599,24 @@ public class ScenarioServiceImpl implements ScenarioService { _knowledgeElementTypeDAO = knowledgeElementTypeDAO; } + /** + * Get the simulationContextService. + * + * @return the simulationContextService + */ + public SimulationContextService getSimulationContextService() { + return _simulationContextService; + } + + /** + * Set the simulationContextService. + * + * @param simulationContextService + * the simulationContextService to set + */ + public void setSimulationContextService( + SimulationContextService simulationContextService) { + _simulationContextService = simulationContextService; + } + } diff --git a/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java index aaf0249..e6a6191 100644 --- a/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java @@ -176,6 +176,7 @@ public class StudyServiceImpl implements StudyService { * * @see org.splat.service.StudyService#addProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext.Properties) */ + @Transactional public SimulationContext addProjectContext(Study aStudy, SimulationContext.Properties cprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException { @@ -190,6 +191,7 @@ public class StudyServiceImpl implements StudyService { * * @see org.splat.service.StudyService#addProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext) */ + @Transactional public SimulationContext addProjectContext(Study aStudy, SimulationContext context) { SimulationContext added = getStepService().addSimulationContext( @@ -445,9 +447,8 @@ public class StudyServiceImpl implements StudyService { setShortCuts(aStudy); // RKV: initialize transient actors set getIndex().update(aStudy); // Update of Lucene index isOk = true; - } catch (Exception error) { - logger.error("Unable to re-index the study '" + aStudy.getIndex() - + "', reason:", error); + } catch (Exception e) { + logger.error("STD-000001", e, aStudy.getIndex(), e.getMessage()); } return isOk; } @@ -517,8 +518,8 @@ public class StudyServiceImpl implements StudyService { if (i == format.length) break; } - DecimalFormat tostring = new DecimalFormat( - pattern.substring(n, i)); + DecimalFormat tostring = new DecimalFormat(pattern + .substring(n, i)); String number = tostring.format(next); for (int j = 0; j < number.length(); j++) { ref[count] = number.charAt(j); @@ -649,15 +650,13 @@ public class StudyServiceImpl implements StudyService { if (actype[i] == Actor.manager) { actor = from.getAuthor(); } else if (actype[i] == Actor.Nx1) { - List manager = getUserService() - .selectUsersWhere(uprop - .setOrganizationName("Nx1")); + List manager = getUserService().selectUsersWhere( + uprop.setOrganizationName("Nx1")); if (manager.size() == 1) actor = manager.get(0); } else if (actype[i] == Actor.Nx2) { - List manager = getUserService() - .selectUsersWhere(uprop - .setOrganizationName("Nx2")); + List manager = getUserService().selectUsersWhere( + uprop.setOrganizationName("Nx2")); if (manager.size() == 1) actor = manager.get(0); } else { /* Actor.customer */ @@ -1111,6 +1110,7 @@ public class StudyServiceImpl implements StudyService { /** * Get the userService. + * * @return the userService */ public UserService getUserService() { @@ -1119,7 +1119,9 @@ public class StudyServiceImpl implements StudyService { /** * Set the userService. - * @param userService the userService to set + * + * @param userService + * the userService to set */ public void setUserService(UserService userService) { _userService = userService; diff --git a/Workspace/Siman-Common/src/spring/businessServiceContext.xml b/Workspace/Siman-Common/src/spring/businessServiceContext.xml index f0ad06d..2bb17ee 100644 --- a/Workspace/Siman-Common/src/spring/businessServiceContext.xml +++ b/Workspace/Siman-Common/src/spring/businessServiceContext.xml @@ -83,6 +83,8 @@ http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> + contelm = null; - private String context = null; - - private static int number = 0; /** * Serial version ID. */ private static final long serialVersionUID = 693943641800113782L; + /** + * Sequential number of the new study for appending to its default title. + */ + private static int number = 0; + + /** + * Title of the new study. + */ + private String title = null; + /** + * List of available project contexts for selection for the new study. + */ + private List contelm = null; + /** + * Project context. + */ + private String context = null; /** * The injected Study service. */ private StudyService _studyService; + /** + * Injected simulation context service. + */ private SimulationContextService _simulationContextService; /** * Injected scenario service. */ private ScenarioService _scenarioService; - + /** - * Value of the menu property. - * It can be: none, create, open, study, knowledge, sysadmin, help. + * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help. */ private String _menuProperty; @@ -43,7 +60,11 @@ public class NewStudyAction extends Action { // Action methods // ============================================================================================================================== - // Fill the values of the drop-down list. + /** + * Fill the values of the drop-down list, and initialize a menu. + * + * @return SUCCESS + */ public String doInitialize() { // get the list of the simulation contexts of the study @@ -54,13 +75,20 @@ public class NewStudyAction extends Action { ApplicationSettings.getCurrentLocale()); title = locale.getString("label.study") + " " + String.valueOf(number + 1); - + setMenuProperty("create"); initializationScreenContext(_menuProperty); return SUCCESS; } + /** + * Create a new study. + * + * @return SUCCESS if the new study is created, INPUT if project context is not defined, ERROR if failed + * @throws Exception + * if properties of the new study are invalid + */ public String doCreate() throws Exception { String[] input = context.split(","); int valid = Integer.valueOf(input[0]); @@ -69,63 +97,49 @@ public class NewStudyAction extends Action { Study.Properties sprop = new Study.Properties(); // Check arguments and creation of the study - try { - if (valid == -1) - throw new Exception(); - if (valid == 0) { - value = input[1].trim(); - if (value.length() == 0) { - - setMenuProperty("create"); - initializationScreenContext(_menuProperty); - - return INPUT; // No need to reinitialize the list of existing products - } - } - sprop.setTitle(title).setManager(getConnectedUser()); - sprop.checkValidity(); - sprop.disableCheck(); - } catch (Exception error) { + if (valid == -1) { SimulationContext.Properties cprop = new SimulationContext.Properties(); SimulationContextType product = getSimulationContextService() .selectType("product"); contelm = getSimulationContextService() .selectSimulationContextsWhere(cprop.setType(product)); - - setMenuProperty("create"); - initializationScreenContext(_menuProperty); - return INPUT; // Title empty, simply wait for input without error message } + if (valid == 0) { + value = input[1].trim(); + if (value.length() == 0) { + setMenuProperty("create"); + initializationScreenContext(_menuProperty); + return INPUT; // No need to reinitialize the list of existing products + } + } + sprop.setTitle(title).setManager(getConnectedUser()); + sprop.checkValidity(); + sprop.disableCheck(); try { - Study study = getStudyService().createStudy(sprop); - // Addition of a default scenario ResourceBundle locale = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale()); Scenario.Properties oprop = new Scenario.Properties(); oprop.setTitle(locale.getString("label.scenario") + " 1"); - getScenarioService().addScenario(study, oprop); // Addition of the entered project context + SimulationContext.Properties cprop = new SimulationContext.Properties(); if (valid == 0) { // Input of new project context - SimulationContext.Properties cprop = new SimulationContext.Properties(); cprop.setType( getSimulationContextService().selectType("product")) .setValue(value); - getStudyService().addProjectContext(study, cprop); } else { // Selection of existing project context - SimulationContext context = getSimulationContextService() - .selectSimulationContext(valid); - getStudyService().addProjectContext(study, context); + cprop.setIndex(valid); } + Study study = getScenarioService().createStudy(sprop, oprop, cprop); // Update of the session number += 1; open(study); // Opens the study, - + setMenuProperty("study"); initializationScreenContext(_menuProperty); - + return SUCCESS; } catch (Exception error) { logger.error("Unable to save the study, reason:", error); @@ -205,6 +219,7 @@ public class NewStudyAction extends Action { /** * Get the scenarioService. + * * @return the scenarioService */ public ScenarioService getScenarioService() { @@ -213,14 +228,17 @@ public class NewStudyAction extends Action { /** * Set the scenarioService. - * @param scenarioService the scenarioService to set + * + * @param scenarioService + * the scenarioService to set */ public void setScenarioService(ScenarioService scenarioService) { _scenarioService = scenarioService; } - + /** * Get the menuProperty. + * * @return the menuProperty */ public String getMenuProperty() { @@ -229,7 +247,9 @@ public class NewStudyAction extends Action { /** * Set the menuProperty. - * @param menuProperty the menuProperty to set + * + * @param menuProperty + * the menuProperty to set */ public void setMenuProperty(String menuProperty) { this._menuProperty = menuProperty; -- 2.30.2