1 package org.splat.simer;
3 import java.util.Arrays;
6 import org.splat.dal.bo.som.ProjectElement;
7 import org.splat.dal.bo.som.SimulationContext;
8 import org.splat.dal.bo.som.SimulationContextType;
9 import org.splat.service.SimulationContextService;
10 import org.splat.service.SimulationContextTypeService;
11 import org.splat.service.StepService;
12 import org.splat.service.StudyService;
13 import org.splat.som.Step;
14 import org.splat.dal.bo.som.Study;
16 public class EditSimulationContextAction extends DisplayStudyStepAction {
18 private List<SimulationContextType> contype = null;
19 private List<SimulationContext> contelm = null;
20 private String selectype = null; // Context type, if selected
21 private String newtype = null; // Context type, if newed
22 private SimulationContextType type = null; // Corresponding context type object
23 private String value = null; // Context value
25 * Injected study service.
27 private StudyService _studyService;
29 * Injected step service.
31 private StepService _stepService;
33 * Injected simulation context service.
35 private SimulationContextService _simulationContextService;
37 * Injected simulation context type service.
39 private SimulationContextTypeService _simulationContextTypeService;
42 * Value of the menu property.
43 * It can be: none, create, open, study, knowledge, sysadmin, help.
45 private String _menuProperty;
48 * Value of the title bar property.
49 * It can be: study, knowledge.
51 private String _titleProperty;
54 * Value of the tool bar property.
55 * It can be: none, standard, study, back.
57 private String _toolProperty;
60 * Value of the left menu property.
61 * It can be: open, study, knowledge, scenario.
63 private String _leftMenuProperty;
66 * Property that indicates whether the current open study is editable or not.
67 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
68 * It is necessary for correct building the title bar.
70 private String _editDisabledProperty = "false";
72 private static final long serialVersionUID = -641719644024601042L;
74 // ==============================================================================================================================
76 // ==============================================================================================================================
78 public String doInitialize() {
80 mystudy = getOpenStudy();
81 contype = getInvolvedContexts();
83 setMenuProperty("study");
84 setTitleProperty("study");
85 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()) {
86 setToolProperty("study");
88 setToolProperty("standard");
91 setLeftMenuProperty("study");
92 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
94 if (contype.isEmpty())
100 public String doSelectContext() {
101 mystudy = getOpenStudy();
103 setMenuProperty("study");
104 setTitleProperty("study");
105 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()) {
106 setToolProperty("study");
108 setToolProperty("standard");
110 setLeftMenuProperty("study");
111 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
113 int typid = Integer.valueOf(selectype);
117 SimulationContext.Properties cprop = new SimulationContext.Properties();
118 type = getSimulationContextService().selectType(typid);
119 newtype = type.getName();
120 contype = getInvolvedContexts();
121 contelm = getSimulationContextService()
122 .selectSimulationContextsWhere(cprop.setType(type));
127 public String doCreateContext() {
128 // --------------------------------
130 mystudy = getOpenStudy();
132 setMenuProperty("study");
133 setTitleProperty("study");
134 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()) {
135 setToolProperty("study");
137 setToolProperty("standard");
139 setLeftMenuProperty("study");
140 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
142 if (newtype.length() == 0 || value.length() == 0)
145 Step step = mystudy.getSelectedStep();
146 ProjectElement owner = step.getOwner();
148 SimulationContext.Properties cprop = new SimulationContext.Properties();
149 SimulationContext contex = null;
150 type = getSimulationContextTypeService().createType(newtype,
152 cprop.setType(type).setValue(value);
153 if (owner instanceof Study)
154 contex = getStudyService().addProjectContext(((Study) owner),
155 cprop); // Re-indexes knowledges and the study
157 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
161 } catch (RuntimeException saverror) {
162 logger.error("Reason:", saverror);
164 } catch (Exception error) {
169 public String doDeleteContext() {
171 setMenuProperty("study");
172 setTitleProperty("study");
173 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()) {
174 setToolProperty("study");
176 setToolProperty("standard");
178 setLeftMenuProperty("study");
179 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
182 mystudy = getOpenStudy();
184 Step step = mystudy.getSelectedStep();
185 ProjectElement owner = step.getOwner();
186 SimulationContext context = step.getSimulationContext(Integer
188 if (owner instanceof Study)
190 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
192 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
194 mystudy.remove(context);
196 } catch (RuntimeException saverror) {
197 logger.error("Reason:", saverror);
202 public String doSetContext() {
203 // -----------------------------
204 String[] input = value.split(",");
205 // Session connex = Database.getCurSession();
206 // Transaction transax = connex.beginTransaction();
208 setMenuProperty("study");
209 setTitleProperty("study");
210 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()) {
211 setToolProperty("study");
213 setToolProperty("standard");
215 setLeftMenuProperty("study");
216 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
219 mystudy = getOpenStudy();
221 Step step = mystudy.getSelectedStep();
222 ProjectElement owner = step.getOwner();
223 SimulationContext contex = null;
225 if (input.length == 1
226 || (input.length == 2 && input[1].equals(" "))) {
227 // Setting an existing simulation context identified by value (input = rid," ")
228 int valid = Integer.valueOf(input[0]);
229 contex = getSimulationContextService().selectSimulationContext(
231 if (owner instanceof Study)
233 .addProjectContext(((Study) owner), contex);
235 getStepService().addSimulationContext(step, contex);
237 // Setting a new simulation context value (input = 0,"new context value")
238 int typid = Integer.valueOf(selectype);
239 SimulationContext.Properties cprop = new SimulationContext.Properties();
240 cprop.setType(getSimulationContextService().selectType(typid))
241 .setValue(input[1].trim());
242 if (owner instanceof Study)
243 contex = getStudyService().addProjectContext(
244 ((Study) owner), cprop); // Re-indexes knowledges and the study
246 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
249 contype = getInvolvedContexts();
253 } catch (RuntimeException saverror) {
254 logger.error("Reason:", saverror);
256 } catch (Exception error) {
262 // ==============================================================================================================================
263 // Getters and setters
264 // ==============================================================================================================================
266 public SimulationContextType getContextType() {
267 // ----------------------------------------------
271 public String getContextName() {
272 // -------------------------------
276 // public List<SimulationContextFacade> getSimulationContexts () {
277 // -------------------------------------------------------
278 // return mystudy.getSelectedStep().getAllSimulationContexts();
280 public List<SimulationContextType> getSimulationContextTypes() {
281 // ---------------------------------------------------------------
285 public List<SimulationContext> getSimulationContextValues() {
286 // ------------------------------------------------------------
290 public void setContextType(String type) {
291 // ----------------------------------------
292 this.selectype = type;
295 public void setContextValue(String value) {
296 // -----------------------------------------
300 public void setNewType(String name) {
301 // ------------------------------------
305 // ==============================================================================================================================
307 // ==============================================================================================================================
309 private List<SimulationContextType> getInvolvedContexts() {
310 // ----------------------------------------------------------
311 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
312 .setStep(mystudy.getSelectedStep().getStep());
313 List<SimulationContextType> contype = getSimulationContextService()
314 .selectTypesWhere(sprop);
316 if (!contype.isEmpty()) {
317 // Ordering by alphabetical order of localized context types
318 SimulationContextType[] types = contype
319 .toArray(new SimulationContextType[contype.size()]);
320 ContextTypeComparator compare = new ContextTypeComparator();
321 boolean state = types[0].isApproved();
324 while (to < types.length - 1) {
326 if (types[to].isApproved() == state)
330 Arrays.sort(types, from, to, compare);
335 Arrays.sort(types, from, to + 1, compare);
336 contype = Arrays.asList(types);
342 * Get the studyService.
344 * @return the studyService
346 public StudyService getStudyService() {
347 return _studyService;
351 * Set the studyService.
353 * @param studyService
354 * the studyService to set
356 public void setStudyService(StudyService studyService) {
357 _studyService = studyService;
361 * Get the simulationContextService.
363 * @return the simulationContextService
365 public SimulationContextService getSimulationContextService() {
366 return _simulationContextService;
370 * Set the simulationContextService.
372 * @param simulationContextService
373 * the simulationContextService to set
375 public void setSimulationContextService(
376 SimulationContextService simulationContextService) {
377 _simulationContextService = simulationContextService;
381 * Get the simulationContextTypeService.
382 * @return the simulationContextTypeService
384 public SimulationContextTypeService getSimulationContextTypeService() {
385 return _simulationContextTypeService;
389 * Set the simulationContextTypeService.
390 * @param simulationContextTypeService the simulationContextTypeService to set
392 public void setSimulationContextTypeService(
393 SimulationContextTypeService simulationContextTypeService) {
394 _simulationContextTypeService = simulationContextTypeService;
398 * Get the stepService.
400 * @return the stepService
402 public StepService getStepService() {
407 * Set the stepService.
410 * the stepService to set
412 public void setStepService(StepService stepService) {
413 _stepService = stepService;
417 * Get the menuProperty.
418 * @return the menuProperty
420 public String getMenuProperty() {
421 return _menuProperty;
425 * Set the menuProperty.
426 * @param menuProperty the menuProperty to set
428 public void setMenuProperty(String menuProperty) {
429 this._menuProperty = menuProperty;
433 * Get the _titleProperty.
434 * @return the _titleProperty
436 public String getTitleProperty() {
437 return _titleProperty;
441 * Set the _titleProperty.
442 * @param _titleProperty the titleProperty to set
444 public void setTitleProperty(String titleProperty) {
445 _titleProperty = titleProperty;
449 * Get the editDisabledProperty.
450 * @return the editDisabledProperty
452 public final String getEditDisabledProperty() {
453 return _editDisabledProperty;
457 * Set the editDisabledProperty.
458 * @param editDisabledProperty the editDisabledProperty to set
460 public final void setEditDisabledProperty(String editDisabledProperty) {
461 _editDisabledProperty = editDisabledProperty;
465 * Get the toolProperty.
466 * @return the toolProperty
468 public String getToolProperty() {
469 return _toolProperty;
473 * Set the toolProperty.
474 * @param toolProperty the toolProperty to set
476 public void setToolProperty(final String toolProperty) {
477 _toolProperty = toolProperty;
481 * Get the leftMenuProperty.
482 * @return the leftMenuProperty
484 public String getLeftMenuProperty() {
485 return _leftMenuProperty;
489 * Set the leftMenuProperty.
490 * @param leftMenuProperty the leftMenuProperty to set
492 public void setLeftMenuProperty(final String leftMenuProperty) {
493 _leftMenuProperty = leftMenuProperty;