Salome HOME
SIMAN Eclipse workspace first version
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewStudyAction.java
1 package org.splat.simer;
2
3 import java.util.List;
4 import java.util.ResourceBundle;
5
6 import org.hibernate.HibernateException;
7 import org.hibernate.Session;
8 import org.hibernate.Transaction;
9 import org.splat.som.Database;
10 import org.splat.som.Scenario;
11 import org.splat.som.SimulationContext;
12 import org.splat.som.SimulationContextType;
13 import org.splat.som.Study;
14
15
16 public class NewStudyAction extends Action {
17
18     private String                  title   = null;
19         private List<SimulationContext> contelm = null;
20     private String                  context = null;
21     
22     private static       int        number           = 0;
23         private static final long       serialVersionUID = 693943641800113782L;
24
25 //  ==============================================================================================================================
26 //  Action methods
27 //  ==============================================================================================================================
28
29     public String doInitialize () {
30 //  -----------------------------      
31       Session      connex  = Database.getSession();
32           Transaction  transax = connex.beginTransaction();       
33
34           SimulationContext.Properties cprop   = new SimulationContext.Properties();
35       SimulationContextType        product = SimulationContext.selectType("product");
36       ResourceBundle               locale  = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
37           
38       contelm = Database.selectSimulationContextsWhere(cprop.setType(product));
39       title   = locale.getString("label.study") + " " + String.valueOf(number + 1);
40           transax.commit();
41       return SUCCESS;
42     }
43
44         public String doCreate () throws Exception {
45 //  -------------------------
46       String[] input = context.split(",");
47       int      valid = Integer.valueOf(input[0]);
48       String   value = "";   // input[1] if exists
49
50       Session          session = Database.getSession();
51           Transaction      transax = session.beginTransaction();        
52       Study.Properties sprop   = new Study.Properties();
53
54 //    Check arguments and creation of the study
55           try {
56         if (valid == -1) throw new Exception();
57         if (valid == 0) {
58           value = input[1].trim();
59           if (value.length() == 0) return INPUT;  // No need to reinitialize the list of existing products
60         }
61         sprop.setTitle(title).setManager(getConnectedUser());
62         sprop.checkValidity();
63         sprop.disableCheck();
64           }
65           catch (Exception error) {
66         SimulationContext.Properties cprop   = new SimulationContext.Properties();
67         SimulationContextType        product = SimulationContext.selectType("product");
68         contelm = Database.selectSimulationContextsWhere(cprop.setType(product));
69         transax.commit();
70                 return INPUT;        // Title empty, simply wait for input without error message
71           }
72           try {
73         Study study = Database.createStudy(sprop);
74
75 //      Addition of a default scenario
76         ResourceBundle      locale = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
77                 Scenario.Properties oprop  = new Scenario.Properties();
78                 oprop.setTitle(locale.getString("label.scenario") + " 1");
79                 study.addScenario(oprop);
80                 
81 //      Addition of the entered project context
82                 if (valid == 0) {     // Input of new project context
83               SimulationContext.Properties cprop = new SimulationContext.Properties();
84                   cprop.setType(SimulationContext.selectType("product")).setValue(value);
85           study.addProjectContext(cprop);
86                 }
87                 else {                // Selection of existing project context
88           SimulationContext context = Database.selectSimulationContext(valid);
89           study.addProjectContext(context);
90                 }
91 //      Update of the session
92             number += 1;
93             open(study);          // Opens the study,
94             transax.commit();
95             return SUCCESS;
96           }
97       catch (Exception error) {
98         logger.error("Unable to save the study, reason:", error);
99         if (transax != null && transax.isActive()) {
100 //        Second try-catch as the rollback could fail as well
101           try {
102                 transax.rollback();
103           } catch (HibernateException backerror) {
104             logger.debug("Error rolling back transaction", backerror);
105           }
106         }
107         return ERROR;
108       }
109         }
110
111 //  ==============================================================================================================================
112 //  Getters and setters
113 //  ==============================================================================================================================
114
115     public String getProjectContext () {
116 //  ----------------------------------
117       return context;
118     }
119     public List<SimulationContext> getProjectContextValues () {
120 //  ---------------------------------------------------------
121       return contelm;
122     }
123     public String getTitle () {
124 //  ----------------------------        
125       return title;
126     }
127
128     public void setProjectContext (String value) {
129 //  --------------------------------------------
130       this.context = value;
131     }
132     public void setTitle (String value) {
133 //  -----------------------------------         
134       this.title = value;
135     }
136 }