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