Salome HOME
Promote/Review/Validate functionality for author of the study is improved.
[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 import java.util.Date;
6
7 import org.splat.dal.bo.kernel.User;
8 import org.splat.dal.bo.som.ConvertsRelation;
9 import org.splat.dal.bo.som.Publication;
10 import org.splat.exception.DocumentIsUsedException;
11 import org.splat.exception.InvalidParameterException;
12 import org.splat.exception.UserRightsException;
13 import org.splat.kernel.InvalidPropertyException;
14 import org.splat.service.PublicationService;
15 import org.splat.service.StepService;
16 import org.splat.service.technical.RepositoryService;
17 import org.splat.som.Step;
18
19 /**
20  * Document modification action.
21  */
22 public class EditDocumentAction extends DisplayStudyStepAction {
23
24         /**
25          * Serial version ID.
26          */
27         private static final long serialVersionUID = 4573036736137033679L;
28
29         /**
30          * The selected document id.
31          */
32         private transient String _index = null;
33         /**
34          * Document title.
35          */
36         private transient String _title = null;
37         /**
38          * Uploaded filename.
39          */
40         private transient String _filename = null;
41         /**
42          * Injected publication service.
43          */
44         private PublicationService _publicationService;
45         /**
46          * Injected step service.
47          */
48         private StepService _stepService;
49         /**
50          * Injected repository service.
51          */
52         private RepositoryService _repositoryService;
53
54         /**
55          * Operations enumeration.
56          */
57         private enum Execute {
58                 /**
59                  * Possible document operations.
60                  */
61                 renameDocument, accept, promote, demote, review, invalidate, approve, disapprove
62         };
63
64         // =========================================================================
65         // Action methods
66         // =========================================================================
67
68         /**
69          * Open a study.
70          * 
71          * @return SUCCESS
72          */
73         public String doInitialize() {
74                 _openStudy = getOpenStudy();
75                 setMenu();
76                 return SUCCESS;
77         }
78
79         /**
80          * Perform the selected operation on the document.
81          * 
82          * @return SUCCESS if succeeded, INPUT if input data are invalid, ERROR if failed
83          */
84         public String doSetDocument() throws InvalidParameterException, UserRightsException {
85                 setMenu();
86                 String res = ERROR;
87                 try {
88                         _openStudy = getOpenStudy();
89
90                         Execute todo = Execute.valueOf(_action);
91                         Step step = _openStudy.getSelectedStep();
92                         Publication doc = step.getDocument(Long.valueOf(_index));
93
94                         if (todo == Execute.renameDocument) {
95                                 getPublicationService().rename(doc, _title);
96                                 // Useless to update the document presentation
97                                 setAction(null);
98                         } else if (todo == Execute.accept) {
99                                 getPublicationService().actualize(doc);
100                                 doOpen();       //so pop-up menus of documents depending on this one will be updated
101                         } else if (todo == Execute.promote) {
102                                 getPublicationService().promote(doc,
103                                                 Calendar.getInstance().getTime(), getConnectedUser());
104                                 _openStudy.update(doc);
105                         } else if (todo == Execute.demote) {
106                                 getPublicationService().demote(doc);
107                                 _openStudy.update(doc);
108                         } else if (todo == Execute.review) {
109                                 getPublicationService().review(doc,
110                                                 Calendar.getInstance().getTime(), getConnectedUser());
111                                 _openStudy.update(doc);
112                         } else if (todo == Execute.invalidate) {
113                                 getPublicationService().invalidate(doc);
114                                 _openStudy.update(doc);
115                         } else if (todo == Execute.approve) {
116                                 getPublicationService().approve(doc,
117                                                 Calendar.getInstance().getTime(), getConnectedUser());
118                                 _openStudy.update(doc);
119                                 // Update the menu icon, in case of other documents in approved state
120                                 _openStudy.getMenu().refreshSelectedItem();
121                         }
122                         res = SUCCESS;
123                 } catch (RuntimeException saverror) {
124                         LOG.error("Reason:", saverror);
125                 } catch (InvalidPropertyException error) {
126                         res = INPUT;
127                 }
128                 return res;
129         }
130
131         /**
132          * Attach an uploaded result file to the selected document.
133          * 
134          * @return SUCCESS if succeeded, otherwise return ERROR
135          */
136         public String doAttach() {
137                 setMenu();
138                 String res = ERROR;
139                 try {
140                         // Getting user inputs
141                         _openStudy = getOpenStudy();
142                         User user = getConnectedUser();
143                         Step step = _openStudy.getSelectedStep();
144                         File updir = getRepositoryService().getDownloadDirectory(user);
145                         File upfile = new File(updir.getPath() + "/" + _filename);
146                         String[] parse = _filename.split("\\x2E");
147
148                         Publication edited = step.getDocument(Long.valueOf(_index));
149                         ConvertsRelation export = getPublicationService().attach(edited,
150                                         parse[parse.length - 1]);
151
152                         if (LOG.isInfoEnabled()) {
153                                 LOG.info("Moving \"" + upfile.getName() + "\" to \""
154                                                 + export.getTo().asFile().getAbsolutePath() + "\".");
155                         }
156                         upfile.renameTo(export.getTo().asFile());
157
158                         _openStudy.update(edited);
159                         res = SUCCESS;
160                 } catch (Exception error) {
161                         LOG.error("Reason:", error);
162                 }
163                 return res;
164         }
165
166         /**
167          * Replace the selected document source file by the uploaded file.
168          * 
169          * @return SUCCESS if succeeded, otherwise return ERROR
170          */
171         public String doReplace() {
172                 setMenu();
173                 String res = ERROR;
174                 try {
175                         // Getting user inputs
176                         User user = getConnectedUser();
177                         File updir = getRepositoryService().getDownloadDirectory(user);
178                         File upfile = new File(updir.getPath() + "/" + _filename);
179
180                         _openStudy = getOpenStudy();
181                         Step step = _openStudy.getSelectedStep();
182                         Publication edited = step.getDocument(Long.valueOf(_index));
183                         Date modifTime = Calendar.getInstance().getTime();
184
185                         // Replace the document source file
186                         getPublicationService().replace(edited, upfile, modifTime);
187
188                         _openStudy.update(edited);
189                         res = SUCCESS;
190                 } catch (Exception error) {
191                         LOG.error("Reason:", error);
192                 }
193                 return res;
194         }
195
196         /**
197          * Remove the selected document from the current activity.
198          * 
199          * @return SUCCESS
200          * @throws DocumentIsUsedException
201          *             if the document is used by other document(s)
202          * @throws NumberFormatException
203          *             if the document id can not be transformed to number
204          */
205         public String doDeleteDocument() throws DocumentIsUsedException,
206                         NumberFormatException {
207
208                 setMenu();
209                 _openStudy = getOpenStudy();
210                 Step step = _openStudy.getSelectedStep();
211
212                 if (getStepService().removeDocument(step, Long.valueOf(_index))) { // Updates the data structure
213                         // Re-opening (refreshing) the currently open study
214                         if (_selection == null) { // Opening a study just newed
215                                 _selection = _openStudy.getSelection(); // Default selection
216                         }
217                         _openStudy = open(getStudyService().selectStudy(
218                                         _openStudy.getIndex())); // Closes the previously open study
219                         _openStudy.setSelection(_selection);
220                 }
221                 return SUCCESS;
222         }
223
224         // =========================================================================
225         // Getters and setters
226         // =========================================================================
227
228         /**
229          * Set the document title.
230          * 
231          * @param title
232          *            the document title to set
233          */
234         public void setDocumentTitle(final String title) {
235                 this._title = title;
236         }
237
238         /**
239          * Set the uploaded file name.
240          * 
241          * @param filename
242          *            the file name to set
243          */
244         public void setFileName(final String filename) {
245                 this._filename = filename;
246         }
247
248         /**
249          * {@inheritDoc}
250          * 
251          * @see org.splat.simer.AbstractDisplayAction#setIndex(java.lang.String)
252          */
253         @Override
254         public void setIndex(final String index) {
255                 this._index = index;
256         }
257
258         /**
259          * Get the index.
260          * 
261          * @return the index
262          */
263         @Override
264         public String getIndex() {
265                 return _index;
266         }
267
268         /**
269          * Get the publicationService.
270          * 
271          * @return the publicationService
272          */
273         public PublicationService getPublicationService() {
274                 return _publicationService;
275         }
276
277         /**
278          * Set the publicationService.
279          * 
280          * @param publicationService
281          *            the publicationService to set
282          */
283         public void setPublicationService(
284                         final PublicationService publicationService) {
285                 _publicationService = publicationService;
286         }
287
288         /**
289          * Get the repositoryService.
290          * 
291          * @return the repositoryService
292          */
293         public RepositoryService getRepositoryService() {
294                 return _repositoryService;
295         }
296
297         /**
298          * Set the repositoryService.
299          * 
300          * @param repositoryService
301          *            the repositoryService to set
302          */
303         public void setRepositoryService(final RepositoryService repositoryService) {
304                 _repositoryService = repositoryService;
305         }
306
307         /**
308          * Get the stepService.
309          * 
310          * @return the stepService
311          */
312         public StepService getStepService() {
313                 return _stepService;
314         }
315
316         /**
317          * Set the stepService.
318          * 
319          * @param stepService
320          *            the stepService to set
321          */
322         public void setStepService(final StepService stepService) {
323                 _stepService = stepService;
324         }
325 }