Salome HOME
9725ecc78ececb22fbdc9de1df65e799a5b1985a
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / admin / SimulationContextAction.java
1 package org.splat.simer.admin;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Comparator;
6 import java.util.HashSet;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Locale;
10 import java.util.ResourceBundle;
11 import java.util.Set;
12 import java.util.Vector;
13
14 import org.hibernate.Session;
15 import org.hibernate.Transaction;
16
17 import org.splat.simer.Action;
18 import org.splat.simer.ApplicationSettings;
19 import org.splat.som.Database;
20 import org.splat.som.KnowledgeElement;
21 import org.splat.som.ProgressState;
22 import org.splat.som.ProjectSettings;
23 import org.splat.som.Proxy;
24 import org.splat.som.SimulationContext;
25 import org.splat.som.SimulationContextType;
26 import org.splat.som.Study;
27
28
29 public class SimulationContextAction extends Action {
30
31     private List<SimulationContextFacade> tocheck;     // Simulation contexts to be approved
32     private int                           selection;   // Index of the selected simulation context presented in the approval form
33     private SimulationContext             edition;     // Corresponding simulation context object
34     private ProjectSettings.Step          step;        // Study step to which the selected simulation context is attached
35     private Set<ProjectElementFacade>     owner;       // Study scenarios indexed by this simulation context
36     private List<LocalizedContextTypes>   existype;    // Existing approved simulation context types ordered by localized names
37     private List<SimulationContextType>   exisname;    // Existing approved simulation context types ordered by internal codes
38     private List<SimulationContext>       existing;    // Existing simulation contexts of selected type
39
40         private static final long serialVersionUID = 7083323229359094699L;
41
42     private class ContextNameComparator  implements Comparator<SimulationContextType> {
43 //  ---------------------------------------------------------------------------------
44       public int compare(SimulationContextType t1, SimulationContextType t2)
45       {
46         String  name1  = t1.getName();
47         String  name2  = t2.getName();
48
49         return name1.compareToIgnoreCase(name2);
50       }
51     }
52
53     public class LocalizedContextTypes {
54 //  ----------------------------------
55       private Locale       locale;
56       private List<String> sortype;    // Existing approved simulation context types ordered by localized names
57
58       public LocalizedContextTypes (List<SimulationContextType> types, Locale lang)
59       {
60         ResourceBundle          bundle = ResourceBundle.getBundle("som", lang);
61         SimulationContextType[] tosort = types.toArray( new SimulationContextType[types.size()] );
62         String[]                name   = new String[types.size()];
63
64         for (int i=0; i<name.length; i++) name[i] = bundle.getString("type.context." + tosort[i].getName());
65         Arrays.sort(name);
66         sortype = Arrays.asList(name);
67         locale  = lang;
68       }
69       public String getLocale ()
70       {
71         return locale.toString();
72       }
73       public List<String> getTypeNames ()
74       {
75         return sortype;
76       }
77       public boolean isCurrent ()
78       {
79         return (locale.equals(ApplicationSettings.getCurrentLocale()));
80       }
81     }
82
83 //  ==============================================================================================================================
84 //  Action methods
85 //  ==============================================================================================================================
86
87     public String doInitialize () {
88 //  -----------------------------
89       Session      connex  = Database.getSession();
90       Transaction  transax = connex.beginTransaction();
91       try {
92         SimulationContext.Properties  cprop   = new SimulationContext.Properties();
93         List<SimulationContext>       context = Database.selectSimulationContextsWhere(cprop.setState(ProgressState.inCHECK));
94
95         tocheck = new Vector<SimulationContextFacade>(context.size());
96         for (Iterator<SimulationContext> i=context.iterator(); i.hasNext(); ) {
97           tocheck.add( new SimulationContextFacade(i.next()) );
98         }
99         selection = 0;
100         edition   = null;
101         owner     = null;
102
103         transax.commit();
104         return SUCCESS;
105       }
106       catch (Exception error) {
107         logger.error("Reason:", error);
108         return ERROR;     // No need to roll-back the transaction as it is read-only
109       }
110     }
111
112     public String doSelect () {
113 //  -------------------------
114       Session      connex  = Database.getSession();
115       Transaction  transax = connex.beginTransaction();
116       try {
117         SimulationContext.Properties  cprop    = new SimulationContext.Properties();
118         List<SimulationContext>       context  = Database.selectSimulationContextsWhere(cprop.setState(ProgressState.inCHECK));
119         List<SimulationContext>       selected = new ArrayList<SimulationContext>(1);
120
121         tocheck = new Vector<SimulationContextFacade>(context.size());
122         for (Iterator<SimulationContext> i=context.iterator(); i.hasNext(); ) {
123           SimulationContext next = i.next();
124           if (next.getIndex() == selection) {
125                 edition = next;
126                 selected.add(edition);
127           }
128           tocheck.add( new SimulationContextFacade(next) );
129         }
130         KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
131         List<Proxy>                 kelm  = Database.selectKnowledgeElementsWhere(kprop.setSimulationContexts(selected).setState(ProgressState.inWORK));
132
133         step  = edition.getType().getAttachedStep();
134         owner = new HashSet<ProjectElementFacade>();
135         for (Iterator<Proxy> i=kelm.iterator(); i.hasNext(); ) {
136           KnowledgeElement      next = Database.selectKnowledgeElement(i.next().getIndex());
137           ProjectElementFacade  facade;
138           if (step.appliesTo(Study.class)) facade = new ProjectElementFacade(next.getOwnerScenario().getOwnerStudy(), step);
139           else                             facade = new ProjectElementFacade(next.getOwnerScenario(), step);
140           owner.add(facade);
141         }
142         SimulationContextType.Properties  sprop    = new SimulationContextType.Properties();
143         List<SimulationContextType>       types    = SimulationContext.selectTypesWhere(sprop.setState(ProgressState.APPROVED));
144         Locale[]                          support  = ApplicationSettings.getSupportedLocales();
145
146 //      Sort localized type names
147         existype = new ArrayList<LocalizedContextTypes>(support.length);
148         for (int i=0; i<support.length; i++) {
149           existype.add( new LocalizedContextTypes(types, support[i]) );
150         }
151         SimulationContextType[] names = types.toArray( new SimulationContextType[types.size()] );
152         
153         Arrays.sort(names, new ContextNameComparator() );
154         exisname = Arrays.asList(names);
155         
156         existing = Database.selectSimulationContextsWhere(cprop.setType(edition.getType()).setState(ProgressState.APPROVED));
157
158         transax.commit();
159         return SUCCESS;
160       }
161       catch (Exception error) {
162         logger.error("Reason:", error);
163         return ERROR;     // No need to roll-back the transaction as it is read-only
164       }
165     }
166
167 //  ==============================================================================================================================
168 //  Getters and setters
169 //  ==============================================================================================================================
170
171     public List<ProjectSettings.Step> getAllStudySteps () {
172 //  -----------------------------------------------------
173       return  ProjectSettings.getAllSteps();
174     }
175     public ProjectSettings.Step getAttachedStep () {
176 //  ----------------------------------------------
177       return  step;
178     }
179     public List<SimulationContextFacade> getContextsToBeApproved () {
180 //  ---------------------------------------------------------------
181       return  tocheck;
182     }
183     public SimulationContext getEdited () {
184 //  -------------------------------------
185       return  edition;
186     }
187     public List<SimulationContext> getExistingContexts () {
188 //  -----------------------------------------------------
189       return  existing;
190     }
191     public List<SimulationContextType> getExistingNames () {
192 //  ------------------------------------------------------
193       return exisname;
194     }
195     public List<LocalizedContextTypes> getSupportedLocales () {
196 //  ---------------------------------------------------------
197       return existype;
198     }
199     public Set<ProjectElementFacade> getIndexedElements () {
200 //  ------------------------------------------------------
201       return  owner;
202     }
203     public int getSelection () {
204 //  --------------------------
205       return  selection;
206     }
207
208     public void setSelection (String index) {
209 //  ---------------------------------------
210       selection = Integer.valueOf(index);
211     }
212 }