Salome HOME
Modifications to respect PMD rules.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditDocumentAction.java
1 package org.splat.simer;
2
3 import java.io.File;
4 import java.util.Calendar;
5
6 import org.splat.dal.bo.kernel.User;
7 import org.splat.dal.bo.som.ConvertsRelation;
8 import org.splat.dal.bo.som.Publication;
9 import org.splat.kernel.InvalidPropertyException;
10 import org.splat.service.PublicationService;
11 import org.splat.service.StepService;
12 import org.splat.service.technical.RepositoryService;
13 import org.splat.som.Step;
14
15 /**
16  * Document modification action.
17  */
18 public class EditDocumentAction extends DisplayStudyStepAction {
19
20         /**
21          * Serial version ID.
22          */
23         private static final long serialVersionUID = 4573036736137033679L;
24
25         private String index = null;
26         private String title = null;
27         private String filename = null;
28         /**
29          * Injected publication service.
30          */
31         private PublicationService _publicationService;
32         /**
33          * Injected step service.
34          */
35         private StepService _stepService;
36         /**
37          * Injected repository service.
38          */
39         private RepositoryService _repositoryService;
40         
41         /**
42          * Value of the menu property. 
43          * It can be: none, create, open, study, knowledge, sysadmin, help.
44          */
45         private String _menuProperty;
46         
47         /**
48          * Value of the title bar property. 
49          * It can be: study, knowledge.
50          */
51         private String _titleProperty;
52         
53         /**
54          * Value of the tool bar property. 
55          * It can be: none, standard, study, back.
56          */
57         private String _toolProperty;
58         
59         /**
60          * Value of the left menu property. 
61          * It can be: open, study, knowledge, scenario.
62          */
63         private String _leftMenuProperty;
64         
65         /**
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.
69          */
70         private String _editDisabledProperty = "false";
71
72         /**
73          * Operations enumeration.
74          */
75         private enum Execute {
76                 renameDocument, accept, promote, demote, review, invalidate, approve, disapprove
77         };
78
79         // ==============================================================================================================================
80         // Action methods
81         // ==============================================================================================================================
82
83         /**
84          * Open a study.
85          * 
86          * @return SUCCESS
87          */
88         public String doInitialize() {
89                 _openStudy = getOpenStudy();
90                 
91                 setMenuProperty("study");
92                 setTitleProperty("study");
93                 if ("true".equals(getWriteAccess()) &&  getUserRights().canCreateDocument()) {
94                         setToolProperty("study");
95                 } else {
96                         setToolProperty("standard");
97                 }
98                 setLeftMenuProperty("study");
99                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
100
101                 return SUCCESS;
102         }
103
104         public String doSetDocument() {
105                 
106                 setMenuProperty("study");
107                 setTitleProperty("study");
108                 if ("true".equals(getWriteAccess()) &&  getUserRights().canCreateDocument()) {
109                         setToolProperty("study");
110                 } else {
111                         setToolProperty("standard");
112                 }
113                 setLeftMenuProperty("study");
114                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
115
116                 try {
117                         _openStudy = getOpenStudy();
118
119                         Execute todo = Execute.valueOf(_action);
120                         Step step = _openStudy.getSelectedStep();
121                         Publication doc = step.getDocument(Integer.valueOf(index));
122
123                         if (todo == Execute.renameDocument) {
124                                 getPublicationService().rename(doc, title);
125                                 // Useless to update the document presentation
126                         } else if (todo == Execute.accept) {
127                                 getPublicationService().actualize(doc);
128                                 _openStudy.update(doc);
129                         } else if (todo == Execute.promote) {
130                                 getPublicationService().promote(doc,
131                                                 Calendar.getInstance().getTime());
132                                 _openStudy.update(doc);
133                         } else if (todo == Execute.demote) {
134                                 getPublicationService().demote(doc);
135                                 _openStudy.update(doc);
136                         } else if (todo == Execute.review) {
137                                 getPublicationService().review(doc,
138                                                 Calendar.getInstance().getTime());
139                                 _openStudy.update(doc);
140                         } else if (todo == Execute.invalidate) {
141                                 getPublicationService().invalidate(doc);
142                                 _openStudy.update(doc);
143                         } else if (todo == Execute.approve) {
144                                 getPublicationService().approve(doc,
145                                                 Calendar.getInstance().getTime());
146                                 _openStudy.update(doc);
147                                 _openStudy.getMenu().refreshSelectedItem(); // Updates the menu icon, in case of other documents in approved state
148                         }
149                         return SUCCESS;
150                 } catch (RuntimeException saverror) {
151                         LOG.error("Reason:", saverror);
152                         return ERROR;
153                 } catch (InvalidPropertyException error) {
154                         return INPUT;
155                 }
156         }
157
158         public String doAttach() {
159                 
160                 setMenuProperty("study");
161                 setTitleProperty("study");
162                 if ("true".equals(getWriteAccess()) &&  getUserRights().canCreateDocument()) {
163                         setToolProperty("study");
164                 } else {
165                         setToolProperty("standard");
166                 }
167                 setLeftMenuProperty("study");
168                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
169
170                 try {
171                         // Getting user inputs
172                         _openStudy = getOpenStudy();
173                         User user = getConnectedUser();
174                         Step step = _openStudy.getSelectedStep();
175                         File updir = getRepositoryService().getDownloadDirectory(user);
176                         File upfile = new File(updir.getPath() + "/" + filename);
177                         String[] parse = filename.split("\\x2E");
178
179                         Publication edited = step.getDocument(Integer.valueOf(index));
180                         ConvertsRelation export = getPublicationService().attach(edited, parse[parse.length - 1]);
181
182                         if (LOG.isInfoEnabled())
183                                 LOG.info("Moving \"" + upfile.getName() + "\" to \""
184                                                 + updir.getPath() + "\".");
185                         upfile.renameTo(export.getTo().asFile());
186
187                         _openStudy.update(edited);
188                         return SUCCESS;
189                 } catch (Exception error) {
190                         LOG.error("Reason:", error);
191                         return ERROR;
192                 }
193         }
194
195         public String doDeleteDocument() {
196                 
197                 setMenuProperty("study");
198                 setTitleProperty("study");
199                 if ("true".equals(getWriteAccess()) &&  getUserRights().canCreateDocument()) {
200                         setToolProperty("study");
201                 } else {
202                         setToolProperty("standard");
203                 }
204                 setLeftMenuProperty("study");
205                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
206
207                 try {
208                         _openStudy = getOpenStudy();
209
210                         Step step = _openStudy.getSelectedStep();
211                         Publication doctag = step.getDocument(Integer.valueOf(index));
212
213                         getStepService().removeDocument(step, doctag); // Updates the data structure
214
215                         _openStudy.remove(doctag); // Updates the presentation
216                         return SUCCESS;
217                 } catch (RuntimeException saverror) {
218                         LOG.error("Reason:", saverror);
219                         return ERROR;
220                 }
221         }
222
223         // ==============================================================================================================================
224         // Getters and setters
225         // ==============================================================================================================================
226
227         public void setDocumentTitle(String title) {
228                 // -------------------------------------------
229                 this.title = title;
230         }
231
232         public void setFileName(String filename) {
233                 // -----------------------------------------
234                 this.filename = filename;
235         }
236
237         public void setIndex(String index) {
238                 // -----------------------------------
239                 this.index = index;
240         }
241
242         /**
243          * Get the publicationService.
244          * 
245          * @return the publicationService
246          */
247         public PublicationService getPublicationService() {
248                 return _publicationService;
249         }
250
251         /**
252          * Set the publicationService.
253          * 
254          * @param publicationService
255          *            the publicationService to set
256          */
257         public void setPublicationService(PublicationService publicationService) {
258                 _publicationService = publicationService;
259         }
260
261         /**
262          * Get the repositoryService.
263          * 
264          * @return the repositoryService
265          */
266         public RepositoryService getRepositoryService() {
267                 return _repositoryService;
268         }
269
270         /**
271          * Set the repositoryService.
272          * 
273          * @param repositoryService
274          *            the repositoryService to set
275          */
276         public void setRepositoryService(RepositoryService repositoryService) {
277                 _repositoryService = repositoryService;
278         }
279
280         /**
281          * Get the stepService.
282          * @return the stepService
283          */
284         public StepService getStepService() {
285                 return _stepService;
286         }
287
288         /**
289          * Set the stepService.
290          * @param stepService the stepService to set
291          */
292         public void setStepService(StepService stepService) {
293                 _stepService = stepService;
294         }
295         
296         /**
297          * Get the menuProperty.
298          * @return the menuProperty
299          */
300         public String getMenuProperty() {
301                 return _menuProperty;
302         }
303
304         /**
305          * Set the menuProperty.
306          * @param menuProperty the menuProperty to set
307          */
308         public void setMenuProperty(String menuProperty) {
309                 this._menuProperty = menuProperty;
310         }
311         
312         /**
313          * Get the _titleProperty.
314          * @return the _titleProperty
315          */
316         public String getTitleProperty() {
317                 return _titleProperty;
318         }
319
320         /**
321          * Set the _titleProperty.
322          * @param _titleProperty the titleProperty to set
323          */
324         public void setTitleProperty(String titleProperty) {
325                 _titleProperty = titleProperty;
326         }
327
328         /**
329          * Get the editDisabledProperty.
330          * @return the editDisabledProperty
331          */
332         public String getEditDisabledProperty() {
333                 return _editDisabledProperty;
334         }
335
336         /**
337          * Set the editDisabledProperty.
338          * @param editDisabledProperty the editDisabledProperty to set
339          */
340         public void setEditDisabledProperty(String editDisabledProperty) {
341                 _editDisabledProperty = editDisabledProperty;
342         }
343
344
345         /**
346          * Get the toolProperty.
347          * @return the toolProperty
348          */
349         public String getToolProperty() {
350                 return _toolProperty;
351         }
352
353         /**
354          * Set the toolProperty.
355          * @param toolProperty the toolProperty to set
356          */
357         public void setToolProperty(final String toolProperty) {
358                 _toolProperty = toolProperty;
359         }
360
361         /**
362          * Get the leftMenuProperty.
363          * @return the leftMenuProperty
364          */
365         public String getLeftMenuProperty() {
366                 return _leftMenuProperty;
367         }
368
369         /**
370          * Set the leftMenuProperty.
371          * @param leftMenuProperty the leftMenuProperty to set
372          */
373         public void setLeftMenuProperty(final String leftMenuProperty) {
374                 _leftMenuProperty = leftMenuProperty;
375         }
376 }