Salome HOME
Fix for some problems with definition uses.
[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                 // updating current step and doc relations
211                 _openStudy.updateCurrentStep();
212                 Step step = _openStudy.getSelectedStep();
213
214                 if (getStepService().removeDocument(step, Long.valueOf(_index))) { // Updates the data structure
215                         // Re-opening (refreshing) the currently open study
216                         if (_selection == null) { // Opening a study just newed
217                                 _selection = _openStudy.getSelection(); // Default selection
218                         }
219                         _openStudy = open(getStudyService().selectStudy(
220                                         _openStudy.getIndex())); // Closes the previously open study
221                         //updating relations and pop-up menu
222                         _openStudy.updateCurrentStep();
223                         _openStudy.setSelection(_selection);
224                 }
225                 return SUCCESS;
226         }
227
228         // =========================================================================
229         // Getters and setters
230         // =========================================================================
231
232         /**
233          * Set the document title.
234          * 
235          * @param title
236          *            the document title to set
237          */
238         public void setDocumentTitle(final String title) {
239                 this._title = title;
240         }
241
242         /**
243          * Set the uploaded file name.
244          * 
245          * @param filename
246          *            the file name to set
247          */
248         public void setFileName(final String filename) {
249                 this._filename = filename;
250         }
251
252         /**
253          * {@inheritDoc}
254          * 
255          * @see org.splat.simer.AbstractDisplayAction#setIndex(java.lang.String)
256          */
257         @Override
258         public void setIndex(final String index) {
259                 this._index = index;
260         }
261
262         /**
263          * Get the index.
264          * 
265          * @return the index
266          */
267         @Override
268         public String getIndex() {
269                 return _index;
270         }
271
272         /**
273          * Get the publicationService.
274          * 
275          * @return the publicationService
276          */
277         public PublicationService getPublicationService() {
278                 return _publicationService;
279         }
280
281         /**
282          * Set the publicationService.
283          * 
284          * @param publicationService
285          *            the publicationService to set
286          */
287         public void setPublicationService(
288                         final PublicationService publicationService) {
289                 _publicationService = publicationService;
290         }
291
292         /**
293          * Get the repositoryService.
294          * 
295          * @return the repositoryService
296          */
297         public RepositoryService getRepositoryService() {
298                 return _repositoryService;
299         }
300
301         /**
302          * Set the repositoryService.
303          * 
304          * @param repositoryService
305          *            the repositoryService to set
306          */
307         public void setRepositoryService(final RepositoryService repositoryService) {
308                 _repositoryService = repositoryService;
309         }
310
311         /**
312          * Get the stepService.
313          * 
314          * @return the stepService
315          */
316         public StepService getStepService() {
317                 return _stepService;
318         }
319
320         /**
321          * Set the stepService.
322          * 
323          * @param stepService
324          *            the stepService to set
325          */
326         public void setStepService(final StepService stepService) {
327                 _stepService = stepService;
328         }
329 }