import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
-import org.splat.som.Database;
-import org.splat.som.ProjectElement;
-import org.splat.som.SimulationContext;
-import org.splat.som.SimulationContextType;
+import org.splat.dal.dao.som.Database;
+import org.splat.dal.bo.som.ProjectElement;
+import org.splat.dal.bo.som.SimulationContext;
+import org.splat.dal.bo.som.SimulationContextType;
+import org.splat.service.StepService;
+import org.splat.service.StudyService;
import org.splat.som.Step;
-import org.splat.som.Study;
-
+import org.splat.dal.bo.som.Study;
public class EditSimulationContextAction extends DisplayStudyStepAction {
- private List<SimulationContextType> contype = null;
- private List<SimulationContext> contelm = null;
- private String selectype = null; // Context type, if selected
- private String newtype = null; // Context type, if newed
- private SimulationContextType type = null; // Corresponding context type object
- private String value = null; // Context value
-
- private static final long serialVersionUID = -641719644024601042L;
-
-// ==============================================================================================================================
-// Action methods
-// ==============================================================================================================================
-
- public String doInitialize () {
-// -----------------------------
- Session connex = Database.getSession();
- Transaction transax = connex.beginTransaction();
-
- mystudy = getOpenStudy();
- contype = getInvolvedContexts();
-
- transax.commit();
- if (contype.isEmpty()) return "create";
- else return "select";
- }
-
- public String doSelectContext () {
-// --------------------------------
- Session connex = Database.getSession();
- Transaction transax = connex.beginTransaction();
- try {
- mystudy = getOpenStudy();
- int typid = Integer.valueOf(selectype);
- if (typid == 0) return "create";
-
- SimulationContext.Properties cprop = new SimulationContext.Properties();
- type = SimulationContext.selectType(typid);
- newtype = type.getName();
- contype = getInvolvedContexts();
- contelm = Database.selectSimulationContextsWhere(cprop.setType(type));
-
- return "set";
- }
- finally {
- transax.commit();
- }
- }
-
- public String doCreateContext () {
-// --------------------------------
- Session connex = Database.getSession();
- Transaction transax = connex.beginTransaction();
- try {
- mystudy = getOpenStudy();
- if (newtype.length() == 0 || value.length() == 0) return INPUT;
-
- Step step = mystudy.getSelectedStep();
- ProjectElement owner = step.getOwner();
-
- SimulationContext.Properties cprop = new SimulationContext.Properties();
- SimulationContext contex = null;
- type = SimulationContext.createType(newtype, step.getStep());
- cprop.setType(type).setValue(value);
- if (owner instanceof Study) contex = ((Study)owner).addProjectContext(cprop); // Re-indexes knowledges and the study
- else contex = step.addSimulationContext(cprop); // Re-indexes knowledges only
-
- mystudy.add(contex);
- transax.commit();
- return SUCCESS;
- }
- catch (RuntimeException saverror) {
- logger.error("Reason:", saverror);
- if (transax != null && transax.isActive()) {
-// Second try-catch as the rollback could fail as well
- try {
- transax.rollback();
- } catch (HibernateException backerror) {
- logger.debug("Error rolling back transaction", backerror);
- }
- }
- return ERROR;
- }
- catch (Exception error) {
- transax.commit();
- return INPUT;
- }
- }
-
- public String doDeleteContext () {
-// --------------------------------
- Session connex = Database.getSession();
- Transaction transax = connex.beginTransaction();
- try {
- mystudy = getOpenStudy();
-
- Step step = mystudy.getSelectedStep();
- ProjectElement owner = step.getOwner();
- SimulationContext context = step.getSimulationContext(Integer.valueOf(myindex));
- if (owner instanceof Study) ((Study)owner).removeProjectContext(context); // Re-indexes knowledges and the study
- else step.removeSimulationContext(context); // Re-indexes knowledges only
-
- mystudy.remove(context);
- transax.commit();
- return SUCCESS;
- }
- catch (RuntimeException saverror) {
- logger.error("Reason:", saverror);
- if (transax != null && transax.isActive()) {
-// Second try-catch as the rollback could fail as well
- try {
- transax.rollback();
- } catch (HibernateException backerror) {
- logger.debug("Error rolling back transaction", backerror);
- }
- }
- return ERROR;
- }
- }
-
- public String doSetContext () {
-// -----------------------------
- String[] input = value.split(",");
- Session connex = Database.getSession();
- Transaction transax = connex.beginTransaction();
- try {
+ private List<SimulationContextType> contype = null;
+ private List<SimulationContext> contelm = null;
+ private String selectype = null; // Context type, if selected
+ private String newtype = null; // Context type, if newed
+ private SimulationContextType type = null; // Corresponding context type object
+ private String value = null; // Context value
+ private StudyService _studyService;
+ private StepService _stepService;
+
+ /**
+ * Get the stepService.
+ *
+ * @return the stepService
+ */
+ public StepService getStepService() {
+ return _stepService;
+ }
+
+ /**
+ * Set the stepService.
+ *
+ * @param stepService
+ * the stepService to set
+ */
+ public void setStepService(StepService stepService) {
+ _stepService = stepService;
+ }
+
+ private static final long serialVersionUID = -641719644024601042L;
+
+ // ==============================================================================================================================
+ // Action methods
+ // ==============================================================================================================================
+
+ public String doInitialize() {
+ // -----------------------------
+ Session connex = Database.getSession();
+ Transaction transax = connex.beginTransaction();
+
mystudy = getOpenStudy();
+ contype = getInvolvedContexts();
+
+ transax.commit();
+ if (contype.isEmpty())
+ return "create";
+ else
+ return "select";
+ }
+
+ public String doSelectContext() {
+ // --------------------------------
+ Session connex = Database.getSession();
+ Transaction transax = connex.beginTransaction();
+ try {
+ mystudy = getOpenStudy();
+ int typid = Integer.valueOf(selectype);
+ if (typid == 0)
+ return "create";
+
+ SimulationContext.Properties cprop = new SimulationContext.Properties();
+ type = SimulationContext.selectType(typid);
+ newtype = type.getName();
+ contype = getInvolvedContexts();
+ contelm = Database.selectSimulationContextsWhere(cprop
+ .setType(type));
+
+ return "set";
+ } finally {
+ transax.commit();
+ }
+ }
+
+ public String doCreateContext() {
+ // --------------------------------
+ Session connex = Database.getSession();
+ Transaction transax = connex.beginTransaction();
+ try {
+ mystudy = getOpenStudy();
+ if (newtype.length() == 0 || value.length() == 0)
+ return INPUT;
+
+ Step step = mystudy.getSelectedStep();
+ ProjectElement owner = step.getOwner();
+
+ SimulationContext.Properties cprop = new SimulationContext.Properties();
+ SimulationContext contex = null;
+ type = SimulationContext.createType(newtype, step.getStep());
+ cprop.setType(type).setValue(value);
+ if (owner instanceof Study)
+ contex = getStudyService().addProjectContext(((Study) owner),
+ cprop); // Re-indexes knowledges and the study
+ else
+ contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
+
+ mystudy.add(contex);
+ transax.commit();
+ return SUCCESS;
+ } catch (RuntimeException saverror) {
+ logger.error("Reason:", saverror);
+ if (transax != null && transax.isActive()) {
+ // Second try-catch as the rollback could fail as well
+ try {
+ transax.rollback();
+ } catch (HibernateException backerror) {
+ logger.debug("Error rolling back transaction", backerror);
+ }
+ }
+ return ERROR;
+ } catch (Exception error) {
+ transax.commit();
+ return INPUT;
+ }
+ }
+
+ public String doDeleteContext() {
+ // --------------------------------
+ Session connex = Database.getSession();
+ Transaction transax = connex.beginTransaction();
+ try {
+ mystudy = getOpenStudy();
+
+ Step step = mystudy.getSelectedStep();
+ ProjectElement owner = step.getOwner();
+ SimulationContext context = step.getSimulationContext(Integer
+ .valueOf(myindex));
+ if (owner instanceof Study)
+ getStudyService()
+ .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
+ else
+ getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
+
+ mystudy.remove(context);
+ transax.commit();
+ return SUCCESS;
+ } catch (RuntimeException saverror) {
+ logger.error("Reason:", saverror);
+ if (transax != null && transax.isActive()) {
+ // Second try-catch as the rollback could fail as well
+ try {
+ transax.rollback();
+ } catch (HibernateException backerror) {
+ logger.debug("Error rolling back transaction", backerror);
+ }
+ }
+ return ERROR;
+ }
+ }
+
+ public String doSetContext() {
+ // -----------------------------
+ String[] input = value.split(",");
+ Session connex = Database.getSession();
+ Transaction transax = connex.beginTransaction();
+ try {
+ mystudy = getOpenStudy();
+
+ Step step = mystudy.getSelectedStep();
+ ProjectElement owner = step.getOwner();
+ SimulationContext contex = null;
+
+ if (input.length == 1
+ || (input.length == 2 && input[1].equals(" "))) {
+ // Setting an existing simulation context identified by value (input = rid," ")
+ int valid = Integer.valueOf(input[0]);
+ contex = Database.selectSimulationContext(valid);
+ if (owner instanceof Study)
+ getStudyService()
+ .addProjectContext(((Study) owner), contex);
+ else
+ getStepService().addSimulationContext(step, contex);
+ } else {
+ // Setting a new simulation context value (input = 0,"new context value")
+ int typid = Integer.valueOf(selectype);
+ SimulationContext.Properties cprop = new SimulationContext.Properties();
+ cprop.setType(SimulationContext.selectType(typid)).setValue(
+ input[1].trim());
+ if (owner instanceof Study)
+ contex = getStudyService().addProjectContext(
+ ((Study) owner), cprop); // Re-indexes knowledges and the study
+ else
+ contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
+ }
+ mystudy.add(contex);
+ contype = getInvolvedContexts();
+
+ transax.commit();
+ return SUCCESS;
+ } catch (RuntimeException saverror) {
+ logger.error("Reason:", saverror);
+ if (transax != null && transax.isActive()) {
+ // Second try-catch as the rollback could fail as well
+ try {
+ transax.rollback();
+ } catch (HibernateException backerror) {
+ logger.debug("Error rolling back transaction", backerror);
+ }
+ }
+ return ERROR;
+ } catch (Exception error) {
+ value = input[0];
+ transax.commit();
+ return INPUT;
+ }
+ }
+
+ // ==============================================================================================================================
+ // Getters and setters
+ // ==============================================================================================================================
+
+ public SimulationContextType getContextType() {
+ // ----------------------------------------------
+ return type;
+ }
+
+ public String getContextName() {
+ // -------------------------------
+ return newtype;
+ }
+
+ // public List<SimulationContextFacade> getSimulationContexts () {
+ // -------------------------------------------------------
+ // return mystudy.getSelectedStep().getAllSimulationContexts();
+ // }
+ public List<SimulationContextType> getSimulationContextTypes() {
+ // ---------------------------------------------------------------
+ return contype;
+ }
+
+ public List<SimulationContext> getSimulationContextValues() {
+ // ------------------------------------------------------------
+ return contelm;
+ }
+
+ public void setContextType(String type) {
+ // ----------------------------------------
+ this.selectype = type;
+ }
+
+ public void setContextValue(String value) {
+ // -----------------------------------------
+ this.value = value;
+ }
+
+ public void setNewType(String name) {
+ // ------------------------------------
+ this.newtype = name;
+ }
+
+ // ==============================================================================================================================
+ // Private service
+ // ==============================================================================================================================
+
+ private List<SimulationContextType> getInvolvedContexts() {
+ // ----------------------------------------------------------
+ SimulationContextType.Properties sprop = new SimulationContextType.Properties()
+ .setStep(mystudy.getSelectedStep().getStep());
+ List<SimulationContextType> contype = SimulationContext
+ .selectTypesWhere(sprop);
+
+ if (!contype.isEmpty()) {
+ // Ordering by alphabetical order of localized context types
+ SimulationContextType[] types = contype
+ .toArray(new SimulationContextType[contype.size()]);
+ ContextTypeComparator compare = new ContextTypeComparator();
+ boolean state = types[0].isApproved();
+ int from = 0;
+ int to = 0;
+ while (to < types.length - 1) {
+ to += 1;
+ if (types[to].isApproved() == state)
+ continue;
+
+ if (to > from + 1)
+ Arrays.sort(types, from, to, compare);
+ state = !state;
+ from = to;
+ }
+ if (to > from)
+ Arrays.sort(types, from, to + 1, compare);
+ contype = Arrays.asList(types);
+ }
+ return contype;
+ }
+
+ /**
+ * Get the studyService.
+ *
+ * @return the studyService
+ */
+ public StudyService getStudyService() {
+ return _studyService;
+ }
- Step step = mystudy.getSelectedStep();
- ProjectElement owner = step.getOwner();
- SimulationContext contex = null;
-
- if (input.length == 1 || (input.length == 2 && input[1].equals(" "))) {
-// Setting an existing simulation context identified by value (input = rid," ")
- int valid = Integer.valueOf(input[0]);
- contex = Database.selectSimulationContext(valid);
- if (owner instanceof Study) ((Study)owner).addProjectContext(contex);
- else step.addSimulationContext(contex);
- }
- else {
-// Setting a new simulation context value (input = 0,"new context value")
- int typid = Integer.valueOf(selectype);
- SimulationContext.Properties cprop = new SimulationContext.Properties();
- cprop.setType(SimulationContext.selectType(typid))
- .setValue(input[1].trim());
- if (owner instanceof Study) contex = ((Study)owner).addProjectContext(cprop); // Re-indexes knowledges and the study
- else contex = step.addSimulationContext(cprop); // Re-indexes knowledges only
- }
- mystudy.add(contex);
- contype = getInvolvedContexts();
-
- transax.commit();
- return SUCCESS;
- }
- catch (RuntimeException saverror) {
- logger.error("Reason:", saverror);
- if (transax != null && transax.isActive()) {
-// Second try-catch as the rollback could fail as well
- try {
- transax.rollback();
- } catch (HibernateException backerror) {
- logger.debug("Error rolling back transaction", backerror);
- }
- }
- return ERROR;
- }
- catch (Exception error) {
- value = input[0];
- transax.commit();
- return INPUT;
- }
- }
-
-// ==============================================================================================================================
-// Getters and setters
-// ==============================================================================================================================
-
- public SimulationContextType getContextType () {
-// ----------------------------------------------
- return type;
- }
- public String getContextName () {
-// -------------------------------
- return newtype;
- }
-// public List<SimulationContextFacade> getSimulationContexts () {
-// -------------------------------------------------------
-// return mystudy.getSelectedStep().getAllSimulationContexts();
-// }
- public List<SimulationContextType> getSimulationContextTypes () {
-// ---------------------------------------------------------------
- return contype;
- }
- public List<SimulationContext> getSimulationContextValues () {
-// ------------------------------------------------------------
- return contelm;
- }
-
- public void setContextType (String type) {
-// ----------------------------------------
- this.selectype = type;
- }
- public void setContextValue (String value) {
-// -----------------------------------------
- this.value = value;
- }
- public void setNewType (String name) {
-// ------------------------------------
- this.newtype = name;
- }
-
-// ==============================================================================================================================
-// Private service
-// ==============================================================================================================================
-
- private List<SimulationContextType> getInvolvedContexts () {
-// ----------------------------------------------------------
- SimulationContextType.Properties sprop = new SimulationContextType.Properties()
- .setStep(mystudy.getSelectedStep().getStep());
- List<SimulationContextType> contype = SimulationContext.selectTypesWhere(sprop);
-
- if (!contype.isEmpty()) {
-// Ordering by alphabetical order of localized context types
- SimulationContextType[] types = contype.toArray( new SimulationContextType[contype.size()] );
- ContextTypeComparator compare = new ContextTypeComparator();
- boolean state = types[0].isApproved();
- int from = 0;
- int to = 0;
- while (to < types.length-1) {
- to += 1;
- if (types[to].isApproved() == state) continue;
-
- if (to > from+1) Arrays.sort(types, from, to, compare);
- state = !state;
- from = to;
- }
- if (to > from) Arrays.sort(types, from, to+1, compare);
- contype = Arrays.asList(types);
- }
- return contype;
+ /**
+ * Set the studyService.
+ *
+ * @param studyService
+ * the studyService to set
+ */
+ public void setStudyService(StudyService studyService) {
+ _studyService = studyService;
}
}
\ No newline at end of file