Salome HOME
Show and Edit comments functionalities are implemented
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / DisplayStudyStepAction.java
1 package org.splat.simer;
2
3 import java.util.List;
4
5 import org.splat.dal.bo.som.ProjectElement;
6 import org.splat.dal.bo.som.Scenario;
7 import org.splat.exception.InvalidParameterException;
8 import org.splat.service.StepService;
9 import org.splat.service.StudyService;
10 import org.splat.service.dto.StepCommentDTO;
11 import org.splat.som.StepRights;
12 import org.splat.wapp.Constants;
13 import org.splat.wapp.PopupMenu;
14 import org.splat.wapp.SimpleMenu;
15
16 /**
17  * Presents the current open study information.
18  * 
19  * @author Daniel Brunier-Coulin.
20  * 
21  */
22 public class DisplayStudyStepAction extends AbstractDisplayAction {
23
24         /**
25          * Serial version ID.
26          */
27         private static final long serialVersionUID = 6467920934724352021L;
28
29         /**
30          * Presented study.
31          */
32         protected transient OpenStudy _openStudy = null;
33
34         /**
35          * Injected study service.
36          */
37         private StudyService _studyService;
38         
39         /**
40          * Step comments.
41          */
42         private List<StepCommentDTO> _comments = null;
43
44         /**
45          * Injected Step Service.
46          */
47         private StepService _stepService;
48
49         // ==============================================================================================================================
50         // Action methods
51         // ==============================================================================================================================
52
53         /**
54          * Open the selected study.
55          * 
56          * @return SUCCESS if succeeded or ERROR if can't open
57          */
58         public String doOpen() {
59                 String res = SUCCESS;
60                 _openStudy = getOpenStudy();
61                 if (_myindex == null) {
62                         if (_selection == null) { // Opening a study just newed
63                                 _selection = _openStudy.getSelection(); // Default selection
64                         } else { // Re-opening (refreshing) the currently open study
65                                 _openStudy = open(getStudyService().selectStudy(
66                                                 _openStudy.getIndex())); // Closes the previously open study
67                                 _openStudy.setSelection(_selection);
68                         }
69                 } else {
70                         try { // Opening a study from the search result
71                                 int index = Integer.valueOf(_myindex);
72                                 if (_openStudy != null && _openStudy.getStudyObject() != null
73                                                 && _openStudy.getIndex() == index) { // - The selected study is currently open
74                                         _selection = _openStudy.getSelection(); // Current selection
75                                         // RKV:BEGIN: put in session if necessary
76                                         if (!getSession().containsKey("study.open")) {
77                                                 open(_openStudy.getStudyObject());
78                                         }
79                                         // RKV:END
80                                 } else { // - The selected study is new
81                                         _openStudy = open(getStudyService().selectStudy(index));
82                                         _selection = _openStudy.getSelection(); // Default selection
83                                 }
84                         } catch (Exception error) {
85                                 LOG.error("Reason:", error);
86                                 res = ERROR;
87                         }
88                 }
89                 if (!ERROR.equals(res)) {
90                         // Initialization of menus
91                         ProjectElement owner = _openStudy.getSelectedStep().getOwner();
92                         SimpleMenu menu = getApplicationSettings().getMenu("configuration");
93                         if (owner instanceof Scenario) {
94                                 menu.enables(Constants.PROP_SCENARIO);
95                                 menu.selects(Constants.PROP_SCENARIO);
96                         } else {
97                                 menu.disables(Constants.PROP_SCENARIO);
98                                 menu.selects("prop-general");
99                         }
100                         getSession().put("menu.study", _openStudy.getMenu());
101
102                         setMenu();
103                 }
104                 loadComments();
105                 return res;
106         }
107
108         /**
109          * Set context menu.
110          */
111         protected void setMenu() {
112                 if (Constants.TRUE.equals(getWriteAccess())
113                                 && getUserRights().canCreateDocument()) {
114                         setToolProperty(Constants.STUDY_MENU);
115                 } else {
116                         setToolProperty(Constants.STANDARD_MENU);
117                 }
118
119                 initializationFullScreenContext(Constants.STUDY_MENU,
120                                 Constants.STUDY_MENU, "false", getToolProperty(),
121                                 Constants.STUDY_MENU);
122         }
123
124         public String doSelectStep() {
125
126                 _openStudy = getOpenStudy();
127                 if (_selection == null) { // Switch back to the current study
128                         _selection = _openStudy.getSelection();
129                 } else { // Selection of a step of current study
130                         _openStudy.setSelection(_selection);
131                 }
132                 // Re-initialization of the properties menu according to the selected step
133                 ProjectElement owner = _openStudy.getSelectedStep().getOwner();
134                 SimpleMenu menu = getApplicationSettings().getMenu("configuration");
135                 if (owner instanceof Scenario) {
136                         menu.enables(Constants.PROP_SCENARIO);
137                         menu.selects(Constants.PROP_SCENARIO);
138                 } else {
139                         menu.disables(Constants.PROP_SCENARIO);
140                         menu.selects("prop-general");
141                 }
142
143                 setMenu();
144
145                 return SUCCESS;
146         }
147
148         public String doSelectDocument() {
149                 _openStudy = getOpenStudy();
150                 Execute todo = Execute.valueOf(_action);
151                 if (todo == Execute.develop) {
152                         _openStudy.developDocument(_myindex);
153                 } else if (todo == Execute.reduce) {
154                         _openStudy.reduceHistory(_myindex);
155                 } else if (todo == Execute.reduceall) {
156                         _openStudy.reduceDocument(_myindex);
157                 }
158
159                 setMenu();
160
161                 return SUCCESS;
162         }
163
164         public String doSelectKnowledge() {
165                 _openStudy = getOpenStudy();
166                 Execute todo = Execute.valueOf(_action);
167                 if (todo == Execute.develop) {
168                         _openStudy.developKnowledge(_myindex);
169                 } else if (todo == Execute.reduce) {
170                         _openStudy.reduceKnowledge(_myindex);
171                 }
172
173                 setMenu();
174
175                 return SUCCESS;
176         }
177
178         /**
179          * Close the currently open study.
180          * 
181          * @return SUCCESS
182          */
183         public String doClose() {
184                 closeStudy();
185                 initializationScreenContext("none");
186                 return SUCCESS;
187         }
188
189         /**
190          * Loads step comments (if already loaded - reloads).
191          */
192         public void loadComments() {
193                 try {
194                         _comments = _stepService.getStepComments(_openStudy.getSelectedStep());
195                 } catch(InvalidParameterException exception) {
196                         _comments = null;
197                         LOG.debug("Error while trying to add comment: " + exception.getMessage());
198                 }
199         }
200         
201         // ==============================================================================================================================
202         // Getters
203         // ==============================================================================================================================
204
205         public String getAction() {
206                 return _action;
207         }
208
209         public List<DocumentFacade> getDocuments() {
210                 return _openStudy.getDisplayedDocuments();
211         }
212
213         public List<AbstractOpenObject.KnowledgeIterator> getKnowledges() {
214                 return _openStudy.getDisplayedKnowledges();
215         }
216
217         public List<SimulationContextFacade> getSimulationContexts() {
218                 return _openStudy.getDisplayedSimulationContexts();
219         }
220
221         public PopupMenu getPopup() {
222                 return _openStudy.getPopup();
223         }
224
225         public int getStepNumber() {
226                 return _openStudy.getSelectedStep().getNumber();
227         }
228
229         public String getStepEnabled() {
230                 return String.valueOf(_openStudy.isStepEnabled());
231         }
232
233         public StepRights getUserRights() {
234                 return _openStudy.getSelectedStepRights();
235         }
236
237         @Override
238         public String getWriteAccess() {
239                 return String.valueOf(_openStudy.isOpenForWriting());
240         }
241
242         /**
243          * Get the studyService.
244          * 
245          * @return the studyService
246          */
247         public StudyService getStudyService() {
248                 return _studyService;
249         }
250
251         /**
252          * Set the studyService.
253          * 
254          * @param studyService
255          *            the studyService to set
256          */
257         public void setStudyService(final StudyService studyService) {
258                 _studyService = studyService;
259         }
260
261         /**
262          * {@inheritDoc}
263          * 
264          * @see org.splat.simer.Action#setOpenStudy(org.splat.simer.OpenStudy)
265          */
266         @Override
267         public void setOpenStudy(final OpenStudy study) {
268                 super.setOpenStudy(study);
269                 _openStudy = study;
270         }
271         
272         /**
273          * Get the comments.
274          * @return the comments
275          */
276         public List<StepCommentDTO> getComments() {
277                 return _comments;
278         }
279
280         /**
281          * Set the comments.
282          * @param comments the comments to set
283          */
284         public void setComments(final List<StepCommentDTO> comments) {
285                 _comments = comments;
286         }
287
288         /**
289          * Get the stepService.
290          * @return the stepService
291          */
292         public StepService getStepService() {
293                 return _stepService;
294         }
295
296         /**
297          * Set the stepService.
298          * @param stepService the stepService to set
299          */
300         public void setStepService(final StepService stepService) {
301                 _stepService = stepService;
302         }
303 }