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