Salome HOME
69edf20eb76effd0ec37d21ef2e534a47685a1fe
[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.hibernate.HibernateException;
7 import org.hibernate.Session;
8 import org.hibernate.Transaction;
9 import org.splat.kernel.InvalidPropertyException;
10 import org.splat.dal.bo.kernel.User;
11 import org.splat.dal.dao.som.Database;
12 import org.splat.dal.bo.som.Publication;
13 import org.splat.dal.bo.som.ConvertsRelation;
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         private String index = null;
30         private String title = null;
31         private String filename = null;
32         /**
33          * Injected publication service.
34          */
35         private PublicationService _publicationService;
36         /**
37          * Injected step service.
38          */
39         private StepService _stepService;
40         /**
41          * Injected repository service.
42          */
43         private RepositoryService _repositoryService;
44
45         /**
46          * Operations enumeration.
47          */
48         private enum Execute {
49                 renameDocument, accept, promote, demote, review, invalidate, approve, disapprove
50         };
51
52         // ==============================================================================================================================
53         // Action methods
54         // ==============================================================================================================================
55
56         /**
57          * Open a study.
58          * 
59          * @return SUCCESS
60          */
61         public String doInitialize() {
62                 mystudy = getOpenStudy();
63                 return SUCCESS;
64         }
65
66         public String doSetDocument() {
67                 // ------------------------------
68                 Session connex = Database.getSession();
69                 Transaction transax = connex.beginTransaction();
70                 try {
71                         mystudy = getOpenStudy();
72
73                         Execute todo = Execute.valueOf(action);
74                         Step step = mystudy.getSelectedStep();
75                         Publication doc = step.getDocument(Integer.valueOf(index));
76
77                         if (todo == Execute.renameDocument) {
78                                 getPublicationService().rename(doc, title);
79                                 // Useless to update the document presentation
80                         } else if (todo == Execute.accept) {
81                                 doc.actualize();
82                                 mystudy.update(doc);
83                         } else if (todo == Execute.promote) {
84                                 getPublicationService().promote(doc,
85                                                 Calendar.getInstance().getTime());
86                                 mystudy.update(doc);
87                         } else if (todo == Execute.demote) {
88                                 getPublicationService().demote(doc);
89                                 mystudy.update(doc);
90                         } else if (todo == Execute.review) {
91                                 getPublicationService().review(doc,
92                                                 Calendar.getInstance().getTime());
93                                 mystudy.update(doc);
94                         } else if (todo == Execute.invalidate) {
95                                 getPublicationService().invalidate(doc);
96                                 mystudy.update(doc);
97                         } else if (todo == Execute.approve) {
98                                 getPublicationService().approve(doc,
99                                                 Calendar.getInstance().getTime());
100                                 mystudy.update(doc);
101                                 mystudy.getMenu().refreshSelectedItem(); // Updates the menu icon, in case of other documents in approved state
102                         }
103                         transax.commit();
104                         return SUCCESS;
105                 } catch (RuntimeException saverror) {
106                         logger.error("Reason:", saverror);
107                         if (transax != null && transax.isActive()) {
108                                 // Second try-catch as the rollback could fail as well
109                                 try {
110                                         transax.rollback();
111                                 } catch (HibernateException backerror) {
112                                         logger.debug("Error rolling back transaction", backerror);
113                                 }
114                         }
115                         return ERROR;
116                 } catch (InvalidPropertyException error) {
117                         transax.commit();
118                         return INPUT;
119                 }
120         }
121
122         public String doAttach() {
123                 // -------------------------
124                 Session connex = Database.getSession();
125                 Transaction transax = connex.beginTransaction();
126                 try {
127                         // Getting user inputs
128                         mystudy = getOpenStudy();
129                         User user = getConnectedUser();
130                         Step step = mystudy.getSelectedStep();
131                         File updir = getRepositoryService().getDownloadDirectory(user);
132                         File upfile = new File(updir.getPath() + "/" + filename);
133                         String[] parse = filename.split("\\x2E");
134
135                         Publication edited = step.getDocument(Integer.valueOf(index));
136                         ConvertsRelation export = getPublicationService().attach(edited, parse[parse.length - 1]);
137
138                         if (logger.isInfoEnabled())
139                                 logger.info("Moving \"" + upfile.getName() + "\" to \""
140                                                 + updir.getPath() + "\".");
141                         upfile.renameTo(export.getTo().asFile());
142
143                         mystudy.update(edited);
144                         transax.commit();
145                         return SUCCESS;
146                 } catch (Exception error) {
147                         logger.error("Reason:", error);
148                         return ERROR;
149                 }
150         }
151
152         public String doDeleteDocument() {
153                 // ---------------------------------
154                 Session connex = Database.getSession();
155                 Transaction transax = connex.beginTransaction();
156                 try {
157                         mystudy = getOpenStudy();
158
159                         Step step = mystudy.getSelectedStep();
160                         Publication doctag = step.getDocument(Integer.valueOf(index));
161
162                         getStepService().removeDocument(step, doctag); // Updates the data structure
163                         transax.commit();
164
165                         mystudy.remove(doctag); // Updates the presentation
166                         return SUCCESS;
167                 } catch (RuntimeException saverror) {
168                         logger.error("Reason:", saverror);
169                         if (transax != null && transax.isActive()) {
170                                 // Second try-catch as the rollback could fail as well
171                                 try {
172                                         transax.rollback();
173                                 } catch (HibernateException backerror) {
174                                         logger.debug("Error rolling back transaction", backerror);
175                                 }
176                         }
177                         return ERROR;
178                 }
179         }
180
181         // ==============================================================================================================================
182         // Getters and setters
183         // ==============================================================================================================================
184
185         public void setDocumentTitle(String title) {
186                 // -------------------------------------------
187                 this.title = title;
188         }
189
190         public void setFileName(String filename) {
191                 // -----------------------------------------
192                 this.filename = filename;
193         }
194
195         public void setIndex(String index) {
196                 // -----------------------------------
197                 this.index = index;
198         }
199
200         /**
201          * Get the publicationService.
202          * 
203          * @return the publicationService
204          */
205         public PublicationService getPublicationService() {
206                 return _publicationService;
207         }
208
209         /**
210          * Set the publicationService.
211          * 
212          * @param publicationService
213          *            the publicationService to set
214          */
215         public void setPublicationService(PublicationService publicationService) {
216                 _publicationService = publicationService;
217         }
218
219         /**
220          * Get the repositoryService.
221          * 
222          * @return the repositoryService
223          */
224         public RepositoryService getRepositoryService() {
225                 return _repositoryService;
226         }
227
228         /**
229          * Set the repositoryService.
230          * 
231          * @param repositoryService
232          *            the repositoryService to set
233          */
234         public void setRepositoryService(RepositoryService repositoryService) {
235                 _repositoryService = repositoryService;
236         }
237
238         /**
239          * Get the stepService.
240          * @return the stepService
241          */
242         public StepService getStepService() {
243                 return _stepService;
244         }
245
246         /**
247          * Set the stepService.
248          * @param stepService the stepService to set
249          */
250         public void setStepService(StepService stepService) {
251                 _stepService = stepService;
252         }
253 }