]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/EditDocumentAction.java
Salome HOME
Rename the document functionality is improved. Now if several documents are added...
[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                                 setAction(null);
79                         } else if (todo == Execute.accept) {
80                                 getPublicationService().actualize(doc);
81                                 _openStudy.update(doc);
82                         } else if (todo == Execute.promote) {
83                                 getPublicationService().promote(doc,
84                                                 Calendar.getInstance().getTime());
85                                 _openStudy.update(doc);
86                         } else if (todo == Execute.demote) {
87                                 getPublicationService().demote(doc);
88                                 _openStudy.update(doc);
89                         } else if (todo == Execute.review) {
90                                 getPublicationService().review(doc,
91                                                 Calendar.getInstance().getTime());
92                                 _openStudy.update(doc);
93                         } else if (todo == Execute.invalidate) {
94                                 getPublicationService().invalidate(doc);
95                                 _openStudy.update(doc);
96                         } else if (todo == Execute.approve) {
97                                 getPublicationService().approve(doc,
98                                                 Calendar.getInstance().getTime());
99                                 _openStudy.update(doc);
100                                 _openStudy.getMenu().refreshSelectedItem(); // Updates the menu icon, in case of other documents in approved state
101                         }
102                         return SUCCESS;
103                 } catch (RuntimeException saverror) {
104                         LOG.error("Reason:", saverror);
105                         return ERROR;
106                 } catch (InvalidPropertyException error) {
107                         return INPUT;
108                 }
109         }
110
111         public String doAttach() {
112
113                 setMenu();
114
115                 try {
116                         // Getting user inputs
117                         _openStudy = getOpenStudy();
118                         User user = getConnectedUser();
119                         Step step = _openStudy.getSelectedStep();
120                         File updir = getRepositoryService().getDownloadDirectory(user);
121                         File upfile = new File(updir.getPath() + "/" + _filename);
122                         String[] parse = _filename.split("\\x2E");
123
124                         Publication edited = step.getDocument(Integer.valueOf(_index));
125                         ConvertsRelation export = getPublicationService().attach(edited,
126                                         parse[parse.length - 1]);
127
128                         if (LOG.isInfoEnabled()) {
129                                 LOG.info("Moving \"" + upfile.getName() + "\" to \""
130                                                 + updir.getPath() + "\".");
131                         }
132                         upfile.renameTo(export.getTo().asFile());
133
134                         _openStudy.update(edited);
135                         return SUCCESS;
136                 } catch (Exception error) {
137                         LOG.error("Reason:", error);
138                         return ERROR;
139                 }
140         }
141
142         /**
143          * Remove the selected document from the current activity.
144          * 
145          * @return SUCCESS
146          * @throws DocumentIsUsedException
147          *             if the document is used by other document(s)
148          * @throws NumberFormatException
149          *             if the document id can not be transformed to number
150          */
151         public String doDeleteDocument() throws DocumentIsUsedException,
152                         NumberFormatException {
153
154                 setMenu();
155                 _openStudy = getOpenStudy();
156                 Step step = _openStudy.getSelectedStep();
157                 Publication doctag = step.getDocument(Integer.valueOf(_index));
158
159                 if (getStepService().removeDocument(step, Long.valueOf(_index))) { // Updates the data structure
160                         _openStudy.remove(doctag); // Updates the presentation
161                 }
162                 return SUCCESS;
163         }
164
165         // ==============================================================================================================================
166         // Getters and setters
167         // ==============================================================================================================================
168
169         public void setDocumentTitle(final String title) {
170                 this._title = title;
171         }
172
173         public void setFileName(final String filename) {
174                 this._filename = filename;
175         }
176
177         @Override
178         public void setIndex(final String index) {
179                 this._index = index;
180         }
181
182         /**
183          * Get the index.
184          * @return the index
185          */
186         @Override
187         public String getIndex() {
188                 return _index;
189         }
190         
191         /**
192          * Get the publicationService.
193          * 
194          * @return the publicationService
195          */
196         public PublicationService getPublicationService() {
197                 return _publicationService;
198         }
199
200         /**
201          * Set the publicationService.
202          * 
203          * @param publicationService
204          *            the publicationService to set
205          */
206         public void setPublicationService(
207                         final PublicationService publicationService) {
208                 _publicationService = publicationService;
209         }
210
211         /**
212          * Get the repositoryService.
213          * 
214          * @return the repositoryService
215          */
216         public RepositoryService getRepositoryService() {
217                 return _repositoryService;
218         }
219
220         /**
221          * Set the repositoryService.
222          * 
223          * @param repositoryService
224          *            the repositoryService to set
225          */
226         public void setRepositoryService(final RepositoryService repositoryService) {
227                 _repositoryService = repositoryService;
228         }
229
230         /**
231          * Get the stepService.
232          * 
233          * @return the stepService
234          */
235         public StepService getStepService() {
236                 return _stepService;
237         }
238
239         /**
240          * Set the stepService.
241          * 
242          * @param stepService
243          *            the stepService to set
244          */
245         public void setStepService(final StepService stepService) {
246                 _stepService = stepService;
247         }
248 }