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