]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/EditDocumentAction.java
Salome HOME
Rename the scenario functionality is implemented
[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.exception.DocumentIsUsedException;
10 import org.splat.kernel.InvalidPropertyException;
11 import org.splat.service.PublicationService;
12 import org.splat.service.StepService;
13 import org.splat.service.technical.RepositoryService;
14 import org.splat.som.Step;
15
16 /**
17  * Document modification action.
18  */
19 public class EditDocumentAction extends DisplayStudyStepAction {
20
21         /**
22          * Serial version ID.
23          */
24         private static final long serialVersionUID = 4573036736137033679L;
25
26         private transient String _index = null;
27         private transient String _title = null;
28         private transient String _filename = null;
29         /**
30          * Injected publication service.
31          */
32         private PublicationService _publicationService;
33         /**
34          * Injected step service.
35          */
36         private StepService _stepService;
37         /**
38          * Injected repository service.
39          */
40         private RepositoryService _repositoryService;
41
42         /**
43          * Operations enumeration.
44          */
45         private enum Execute {
46                 renameDocument, accept, promote, demote, review, invalidate, approve, disapprove
47         };
48
49         // ==============================================================================================================================
50         // Action methods
51         // ==============================================================================================================================
52
53         /**
54          * Open a study.
55          * 
56          * @return SUCCESS
57          */
58         public String doInitialize() {
59                 _openStudy = getOpenStudy();
60                 setMenu();
61                 return SUCCESS;
62         }
63
64         public String doSetDocument() {
65
66                 setMenu();
67
68                 try {
69                         _openStudy = getOpenStudy();
70
71                         Execute todo = Execute.valueOf(_action);
72                         Step step = _openStudy.getSelectedStep();
73                         Publication doc = step.getDocument(Integer.valueOf(_index));
74
75                         if (todo == Execute.renameDocument) {
76                                 getPublicationService().rename(doc, _title);
77                                 // Useless to update the document presentation
78                         } else if (todo == Execute.accept) {
79                                 getPublicationService().actualize(doc);
80                                 _openStudy.update(doc);
81                         } else if (todo == Execute.promote) {
82                                 getPublicationService().promote(doc,
83                                                 Calendar.getInstance().getTime());
84                                 _openStudy.update(doc);
85                         } else if (todo == Execute.demote) {
86                                 getPublicationService().demote(doc);
87                                 _openStudy.update(doc);
88                         } else if (todo == Execute.review) {
89                                 getPublicationService().review(doc,
90                                                 Calendar.getInstance().getTime());
91                                 _openStudy.update(doc);
92                         } else if (todo == Execute.invalidate) {
93                                 getPublicationService().invalidate(doc);
94                                 _openStudy.update(doc);
95                         } else if (todo == Execute.approve) {
96                                 getPublicationService().approve(doc,
97                                                 Calendar.getInstance().getTime());
98                                 _openStudy.update(doc);
99                                 _openStudy.getMenu().refreshSelectedItem(); // Updates the menu icon, in case of other documents in approved state
100                         }
101                         return SUCCESS;
102                 } catch (RuntimeException saverror) {
103                         LOG.error("Reason:", saverror);
104                         return ERROR;
105                 } catch (InvalidPropertyException error) {
106                         return INPUT;
107                 }
108         }
109
110         public String doAttach() {
111
112                 setMenu();
113
114                 try {
115                         // Getting user inputs
116                         _openStudy = getOpenStudy();
117                         User user = getConnectedUser();
118                         Step step = _openStudy.getSelectedStep();
119                         File updir = getRepositoryService().getDownloadDirectory(user);
120                         File upfile = new File(updir.getPath() + "/" + _filename);
121                         String[] parse = _filename.split("\\x2E");
122
123                         Publication edited = step.getDocument(Integer.valueOf(_index));
124                         ConvertsRelation export = getPublicationService().attach(edited,
125                                         parse[parse.length - 1]);
126
127                         if (LOG.isInfoEnabled()) {
128                                 LOG.info("Moving \"" + upfile.getName() + "\" to \""
129                                                 + updir.getPath() + "\".");
130                         }
131                         upfile.renameTo(export.getTo().asFile());
132
133                         _openStudy.update(edited);
134                         return SUCCESS;
135                 } catch (Exception error) {
136                         LOG.error("Reason:", error);
137                         return ERROR;
138                 }
139         }
140
141         /**
142          * Remove the selected document from the current activity.
143          * 
144          * @return SUCCESS
145          * @throws DocumentIsUsedException
146          *             if the document is used by other document(s)
147          * @throws NumberFormatException
148          *             if the document id can not be transformed to number
149          */
150         public String doDeleteDocument() throws DocumentIsUsedException,
151                         NumberFormatException {
152
153                 setMenu();
154                 _openStudy = getOpenStudy();
155                 Step step = _openStudy.getSelectedStep();
156                 Publication doctag = step.getDocument(Integer.valueOf(_index));
157
158                 if (getStepService().removeDocument(step, Long.valueOf(_index))) { // Updates the data structure
159                         _openStudy.remove(doctag); // Updates the presentation
160                 }
161                 return SUCCESS;
162         }
163
164         // ==============================================================================================================================
165         // Getters and setters
166         // ==============================================================================================================================
167
168         public void setDocumentTitle(final String title) {
169                 this._title = title;
170         }
171
172         public void setFileName(final String filename) {
173                 this._filename = filename;
174         }
175
176         @Override
177         public void setIndex(final String index) {
178                 this._index = index;
179         }
180
181         /**
182          * Get the publicationService.
183          * 
184          * @return the publicationService
185          */
186         public PublicationService getPublicationService() {
187                 return _publicationService;
188         }
189
190         /**
191          * Set the publicationService.
192          * 
193          * @param publicationService
194          *            the publicationService to set
195          */
196         public void setPublicationService(
197                         final PublicationService publicationService) {
198                 _publicationService = publicationService;
199         }
200
201         /**
202          * Get the repositoryService.
203          * 
204          * @return the repositoryService
205          */
206         public RepositoryService getRepositoryService() {
207                 return _repositoryService;
208         }
209
210         /**
211          * Set the repositoryService.
212          * 
213          * @param repositoryService
214          *            the repositoryService to set
215          */
216         public void setRepositoryService(final RepositoryService repositoryService) {
217                 _repositoryService = repositoryService;
218         }
219
220         /**
221          * Get the stepService.
222          * 
223          * @return the stepService
224          */
225         public StepService getStepService() {
226                 return _stepService;
227         }
228
229         /**
230          * Set the stepService.
231          * 
232          * @param stepService
233          *            the stepService to set
234          */
235         public void setStepService(final StepService stepService) {
236                 _stepService = stepService;
237         }
238 }