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