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