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 * Property that indicates whether the current open study is editable or not.
61 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
62 * It is necessary for correct building the title bar.
64 private String _editDisabledProperty = "false";
66 private static final long serialVersionUID = -641719644024601042L;
68 // ==============================================================================================================================
70 // ==============================================================================================================================
72 public String doInitialize() {
74 mystudy = getOpenStudy();
75 contype = getInvolvedContexts();
77 setMenuProperty("study");
78 setTitleProperty("study");
79 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()) {
80 setToolProperty("study");
82 setToolProperty("standard");
84 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
86 if (contype.isEmpty())
92 public String doSelectContext() {
93 mystudy = getOpenStudy();
95 setMenuProperty("study");
96 setTitleProperty("study");
97 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()) {
98 setToolProperty("study");
100 setToolProperty("standard");
102 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
104 int typid = Integer.valueOf(selectype);
108 SimulationContext.Properties cprop = new SimulationContext.Properties();
109 type = getSimulationContextService().selectType(typid);
110 newtype = type.getName();
111 contype = getInvolvedContexts();
112 contelm = getSimulationContextService()
113 .selectSimulationContextsWhere(cprop.setType(type));
118 public String doCreateContext() {
119 // --------------------------------
121 mystudy = getOpenStudy();
123 setMenuProperty("study");
124 setTitleProperty("study");
125 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()) {
126 setToolProperty("study");
128 setToolProperty("standard");
130 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
132 if (newtype.length() == 0 || value.length() == 0)
135 Step step = mystudy.getSelectedStep();
136 ProjectElement owner = step.getOwner();
138 SimulationContext.Properties cprop = new SimulationContext.Properties();
139 SimulationContext contex = null;
140 type = getSimulationContextTypeService().createType(newtype,
142 cprop.setType(type).setValue(value);
143 if (owner instanceof Study)
144 contex = getStudyService().addProjectContext(((Study) owner),
145 cprop); // Re-indexes knowledges and the study
147 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
151 } catch (RuntimeException saverror) {
152 logger.error("Reason:", saverror);
154 } catch (Exception error) {
159 public String doDeleteContext() {
161 setMenuProperty("study");
162 setTitleProperty("study");
163 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()) {
164 setToolProperty("study");
166 setToolProperty("standard");
168 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
171 mystudy = getOpenStudy();
173 Step step = mystudy.getSelectedStep();
174 ProjectElement owner = step.getOwner();
175 SimulationContext context = step.getSimulationContext(Integer
177 if (owner instanceof Study)
179 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
181 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
183 mystudy.remove(context);
185 } catch (RuntimeException saverror) {
186 logger.error("Reason:", saverror);
191 public String doSetContext() {
192 // -----------------------------
193 String[] input = value.split(",");
194 // Session connex = Database.getCurSession();
195 // Transaction transax = connex.beginTransaction();
197 setMenuProperty("study");
198 setTitleProperty("study");
199 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()) {
200 setToolProperty("study");
202 setToolProperty("standard");
204 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
207 mystudy = getOpenStudy();
209 Step step = mystudy.getSelectedStep();
210 ProjectElement owner = step.getOwner();
211 SimulationContext contex = null;
213 if (input.length == 1
214 || (input.length == 2 && input[1].equals(" "))) {
215 // Setting an existing simulation context identified by value (input = rid," ")
216 int valid = Integer.valueOf(input[0]);
217 contex = getSimulationContextService().selectSimulationContext(
219 if (owner instanceof Study)
221 .addProjectContext(((Study) owner), contex);
223 getStepService().addSimulationContext(step, contex);
225 // Setting a new simulation context value (input = 0,"new context value")
226 int typid = Integer.valueOf(selectype);
227 SimulationContext.Properties cprop = new SimulationContext.Properties();
228 cprop.setType(getSimulationContextService().selectType(typid))
229 .setValue(input[1].trim());
230 if (owner instanceof Study)
231 contex = getStudyService().addProjectContext(
232 ((Study) owner), cprop); // Re-indexes knowledges and the study
234 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
237 contype = getInvolvedContexts();
241 } catch (RuntimeException saverror) {
242 logger.error("Reason:", saverror);
244 } catch (Exception error) {
250 // ==============================================================================================================================
251 // Getters and setters
252 // ==============================================================================================================================
254 public SimulationContextType getContextType() {
255 // ----------------------------------------------
259 public String getContextName() {
260 // -------------------------------
264 // public List<SimulationContextFacade> getSimulationContexts () {
265 // -------------------------------------------------------
266 // return mystudy.getSelectedStep().getAllSimulationContexts();
268 public List<SimulationContextType> getSimulationContextTypes() {
269 // ---------------------------------------------------------------
273 public List<SimulationContext> getSimulationContextValues() {
274 // ------------------------------------------------------------
278 public void setContextType(String type) {
279 // ----------------------------------------
280 this.selectype = type;
283 public void setContextValue(String value) {
284 // -----------------------------------------
288 public void setNewType(String name) {
289 // ------------------------------------
293 // ==============================================================================================================================
295 // ==============================================================================================================================
297 private List<SimulationContextType> getInvolvedContexts() {
298 // ----------------------------------------------------------
299 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
300 .setStep(mystudy.getSelectedStep().getStep());
301 List<SimulationContextType> contype = getSimulationContextService()
302 .selectTypesWhere(sprop);
304 if (!contype.isEmpty()) {
305 // Ordering by alphabetical order of localized context types
306 SimulationContextType[] types = contype
307 .toArray(new SimulationContextType[contype.size()]);
308 ContextTypeComparator compare = new ContextTypeComparator();
309 boolean state = types[0].isApproved();
312 while (to < types.length - 1) {
314 if (types[to].isApproved() == state)
318 Arrays.sort(types, from, to, compare);
323 Arrays.sort(types, from, to + 1, compare);
324 contype = Arrays.asList(types);
330 * Get the studyService.
332 * @return the studyService
334 public StudyService getStudyService() {
335 return _studyService;
339 * Set the studyService.
341 * @param studyService
342 * the studyService to set
344 public void setStudyService(StudyService studyService) {
345 _studyService = studyService;
349 * Get the simulationContextService.
351 * @return the simulationContextService
353 public SimulationContextService getSimulationContextService() {
354 return _simulationContextService;
358 * Set the simulationContextService.
360 * @param simulationContextService
361 * the simulationContextService to set
363 public void setSimulationContextService(
364 SimulationContextService simulationContextService) {
365 _simulationContextService = simulationContextService;
369 * Get the simulationContextTypeService.
370 * @return the simulationContextTypeService
372 public SimulationContextTypeService getSimulationContextTypeService() {
373 return _simulationContextTypeService;
377 * Set the simulationContextTypeService.
378 * @param simulationContextTypeService the simulationContextTypeService to set
380 public void setSimulationContextTypeService(
381 SimulationContextTypeService simulationContextTypeService) {
382 _simulationContextTypeService = simulationContextTypeService;
386 * Get the stepService.
388 * @return the stepService
390 public StepService getStepService() {
395 * Set the stepService.
398 * the stepService to set
400 public void setStepService(StepService stepService) {
401 _stepService = stepService;
405 * Get the menuProperty.
406 * @return the menuProperty
408 public String getMenuProperty() {
409 return _menuProperty;
413 * Set the menuProperty.
414 * @param menuProperty the menuProperty to set
416 public void setMenuProperty(String menuProperty) {
417 this._menuProperty = menuProperty;
421 * Get the _titleProperty.
422 * @return the _titleProperty
424 public String getTitleProperty() {
425 return _titleProperty;
429 * Set the _titleProperty.
430 * @param _titleProperty the titleProperty to set
432 public void setTitleProperty(String titleProperty) {
433 _titleProperty = titleProperty;
437 * Get the editDisabledProperty.
438 * @return the editDisabledProperty
440 public final String getEditDisabledProperty() {
441 return _editDisabledProperty;
445 * Set the editDisabledProperty.
446 * @param editDisabledProperty the editDisabledProperty to set
448 public final void setEditDisabledProperty(String editDisabledProperty) {
449 _editDisabledProperty = editDisabledProperty;
453 * Get the toolProperty.
454 * @return the toolProperty
456 public String getToolProperty() {
457 return _toolProperty;
461 * Set the toolProperty.
462 * @param toolProperty the toolProperty to set
464 public void setToolProperty(final String toolProperty) {
465 _toolProperty = toolProperty;