]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/module/SaveDocumentAction.java
Salome HOME
Actions menu properties are refactored, unnecessary code is removed.
[tools/siman.git] / Workspace / Siman / src / org / splat / module / SaveDocumentAction.java
1 package org.splat.module;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Set;
8
9 import org.hibernate.HibernateException;
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.splat.dal.bo.kernel.User;
13 import org.splat.dal.bo.som.ConvertsRelation;
14 import org.splat.dal.bo.som.Document;
15 import org.splat.dal.bo.som.DocumentType;
16 import org.splat.dal.bo.som.ProgressState;
17 import org.splat.dal.bo.som.Publication;
18 import org.splat.dal.bo.som.SimulationContext;
19 import org.splat.dal.bo.som.SimulationContextType;
20 import org.splat.dal.dao.som.Database;
21 import org.splat.kernel.Do;
22 import org.splat.service.DocumentTypeService;
23 import org.splat.service.PublicationService;
24 import org.splat.service.ScenarioService;
25 import org.splat.service.SimulationContextService;
26 import org.splat.service.StepService;
27 import org.splat.service.technical.RepositoryService;
28 import org.splat.simer.Action;
29 import org.splat.simer.OpenStudy;
30 import org.splat.som.Step;
31
32 public class SaveDocumentAction extends Action {
33
34         /**
35          * Serial version ID.
36          */
37         private static final long serialVersionUID = -3364960833373200115L;
38
39         /**
40          * Current open study.
41          */
42         private transient OpenStudy _mystudy = null;
43         private transient int _doctype = 0;
44         private transient String _filename = null;
45         private transient String _docname = null;
46         private transient ProgressState _state = null;
47         private transient List<Document> _defuses = null;
48         private String _description = null; // Summary of changes in the new version
49         /**
50          * Injected scenario service.
51          */
52         private ScenarioService _scenarioService;
53         /**
54          * Injected publication service.
55          */
56         private PublicationService _publicationService;
57         /**
58          * Injected step service.
59          */
60         private StepService _stepService;
61         /**
62          * Injected document type service.
63          */
64         private DocumentTypeService _documentTypeService;
65         /**
66          * Injected repository service.
67          */
68         private RepositoryService _repositoryService;
69         /**
70          * Injected simulation context service.
71          */
72         private SimulationContextService _simulationContextService;
73
74         // ==============================================================================================================================
75         // Action methods
76         // ==============================================================================================================================
77
78         public String doSave() {
79                 // -----------------------
80                 Session connex = Database.getCurSession();
81                 Transaction transax = connex.beginTransaction();
82                 try {
83                         // Getting user inputs
84                         _mystudy = getOpenStudy();
85                         User user = getConnectedUser();
86                         Step step = _mystudy.getSelectedStep();
87                         DocumentType type = getDocumentTypeService().selectType(_doctype);
88                         // File updir = Database.getDownloadDirectory(user);
89                         // File upfile = new File(updir.getPath() + "/" + filename);
90                         String upath = getRepositoryService().getTemplatePath(); // Instead of DownloadDirectory for sharing the "uploaded" file
91                         // between users
92                         File upfile = new File(upath + _filename);
93                         String[] table = _filename.split("\\x2E");
94                         String format = table[table.length - 1];
95
96                         // Creation of the document
97                         getScenarioService().checkin(step.getOwner().getIndex()); // Modules necessarily save their data in a scenario step
98                         connex.flush();
99
100                         Document.Properties dprop = new Document.Properties();
101                         Publication credoc = getStepService().createDocument(
102                                         step,
103                                         dprop.setName(_docname).setType(type).setFormat(format)
104                                                         .setAuthor(user));
105                         // Writing the uploaded file into the created document
106                         File target = credoc.getSourceFile().asFile();
107                         if (target.exists()) {
108                                 target.delete();
109                         }
110                         Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
111                         // upfile.renameTo(target);
112
113                         // Saving the document in given state
114                         getPublicationService().saveAs(credoc, _state);
115
116                         // Creation of default uses relations
117                         _defuses = new ArrayList<Document>();
118                         setupDefaultUses(type); // Recursive function
119                         for (Iterator<Document> i = _defuses.iterator(); i.hasNext();) {
120                                 credoc.addDependency(i.next());
121                         }
122
123                         // Execution of module specific operations
124
125                         // 1. Conversion of the document to internal format, if required
126                         // TODO: The following code is temporary, waiting for the support of converters
127                         if ("part".equals(format)) {
128                                 ConvertsRelation export = getPublicationService().attach(
129                                                 credoc, "brep");
130
131                                 target = export.getTo().asFile();
132                                 if (target.exists()) {
133                                         target.delete();
134                                 }
135                                 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
136                         }
137                         // 2. Addition of simulation contexts
138                         if ("model".equals(type)) { // Set the characteristics of the mesh
139                                 SimulationContext.Properties cprop = new SimulationContext.Properties();
140                                 SimulationContextType ctype = getSimulationContextService()
141                                                 .selectType("model");
142                                 SimulationContext context = getSimulationContextService()
143                                                 .selectSimulationContext(ctype, "Éléments finis");
144                                 if (context == null) {
145                                         getStepService().addSimulationContext(step,
146                                                         cprop.setType(ctype).setValue("Éléments finis"));
147                                 } else {
148                                         getStepService().addSimulationContext(step, context);
149                                 }
150                                 ctype = getSimulationContextService().selectType("element");
151                                 context = getSimulationContextService()
152                                                 .selectSimulationContext(ctype, "Surfacique");
153                                 if (context == null) {
154                                         getStepService().addSimulationContext(step,
155                                                         cprop.setType(ctype).setValue("Surfacique"));
156                                 } else {
157                                         getStepService().addSimulationContext(step, context);
158                                 }
159                                 ctype = getSimulationContextService().selectType("shape");
160                                 context = getSimulationContextService()
161                                                 .selectSimulationContext(ctype, "Triangles");
162                                 if (context == null) {
163                                         getStepService().addSimulationContext(step,
164                                                         cprop.setType(ctype).setValue("Triangles"));
165                                 } else {
166                                         getStepService().addSimulationContext(step, context);
167                                 }
168                         }
169                         // Update of the open study
170                         // mystudy.add(credoc); // Useless while the SIMER page need to be refreshed manually
171                         getMenu("study").selects(_mystudy.getSelection()); // Updates the menu icon, in case of first added document
172
173                         transax.commit();
174                         return SUCCESS;
175                 } catch (Exception saverror) {
176                         LOG.error("Reason:", saverror);
177                         if (transax != null && transax.isActive()) {
178                                 // Second try-catch as the rollback could fail as well
179                                 try {
180                                         transax.rollback();
181                                 } catch (HibernateException backerror) {
182                                         LOG.debug("Error rolling back transaction", backerror);
183                                 }
184                         }
185                         return ERROR;
186                 }
187         }
188
189         /**
190          * Get the publicationService.
191          * 
192          * @return publicationService
193          */
194         private PublicationService getPublicationService() {
195                 return _publicationService;
196         }
197
198         /**
199          * Set the publicationService.
200          * 
201          * @param publicationService
202          *            the publicationService to set
203          */
204         public void setPublicationService(
205                         final PublicationService publicationService) {
206                 _publicationService = publicationService;
207         }
208
209         /**
210          * Get the scenarioService.
211          * 
212          * @return scenarioService
213          */
214         public ScenarioService getScenarioService() {
215                 return _scenarioService;
216         }
217
218         /**
219          * Set the scenarioService.
220          * 
221          * @param scenarioService
222          *            the scenarioService to set
223          */
224         public void setScenarioService(final ScenarioService scenarioService) {
225                 _scenarioService = scenarioService;
226         }
227
228         /**
229          * Get the stepService.
230          * 
231          * @return the stepService
232          */
233         public StepService getStepService() {
234                 return _stepService;
235         }
236
237         /**
238          * Set the stepService.
239          * 
240          * @param stepService
241          *            the stepService to set
242          */
243         public void setStepService(final StepService stepService) {
244                 _stepService = stepService;
245         }
246
247         public String doUpdate() {
248                 // -------------------------
249                 return SUCCESS;
250         }
251
252         public String doVersion() {
253                 // --------------------------
254                 Session connex = Database.getCurSession();
255                 Transaction transax = connex.beginTransaction();
256                 try {
257                         // Getting user inputs
258                         _mystudy = getOpenStudy();
259                         User user = getConnectedUser();
260                         Step step = _mystudy.getSelectedStep();
261                         // File updir = Database.getDownloadDirectory(user);
262                         // File upfile = new File(updir.getPath() + "/" + filename);
263                         String upath = getRepositoryService().getTemplatePath(); // Instead of DownloadDirectory for sharing the "uploaded" file
264                         // between users
265                         File upfile = new File(upath + _filename);
266                         String[] table = _filename.split("\\x2E");
267                         String format = table[table.length - 1];
268
269                         // Versioning of the document
270                         Publication current = _mystudy.getSelectedDocument();
271                         Document.Properties dprop = new Document.Properties();
272                         dprop.setAuthor(user);
273                         if (_description.length() > 0) {
274                                 dprop.setDescription(_description);
275                         }
276
277                         Publication next = getStepService().versionDocument(step, current,
278                                         dprop);
279
280                         // Writing the uploaded file into the created document
281                         File target = next.getSourceFile().asFile();
282                         if (target.exists()) {
283                                 target.delete();
284                         }
285                         Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
286                         // upfile.renameTo(target);
287
288                         // Saving the document in given state
289                         getPublicationService().saveAs(next, _state);
290
291                         // Creation of default uses relations
292                         _defuses = new ArrayList<Document>();
293                         setupDefaultUses(next.value().getType()); // Recursive function
294                         for (Iterator<Document> i = _defuses.iterator(); i.hasNext();) {
295                                 next.addDependency(i.next());
296                         }
297                         // TODO: Outdating impacted document
298
299                         // Execution of module specific operations
300
301                         // 1. Conversion of the document to internal format, if required
302                         // TODO: The following code is temporary, waiting for the support of converters
303                         if ("part".equals(format)) {
304                                 ConvertsRelation export = getPublicationService().attach(next,
305                                                 "brep");
306                                 String fname = table[0];
307
308                                 for (int i = 1; i < table.length - 1; i++) {
309                                         fname = fname + table[i];
310                                 }
311                                 upfile = new File(upath + fname + ".brep");
312                                 upfile.renameTo(export.getTo().asFile());
313                         }
314
315                         // Update of the open study
316                         // mystudy.setSelection(mystudy.getSelection()); // Rebuild the presentation
317
318                         transax.commit();
319                         return SUCCESS;
320                 } catch (Exception saverror) {
321                         LOG.error("Reason:", saverror);
322                         if (transax != null && transax.isActive()) {
323                                 // Second try-catch as the rollback could fail as well
324                                 try {
325                                         transax.rollback();
326                                 } catch (HibernateException backerror) {
327                                         LOG.debug("Error rolling back transaction", backerror);
328                                 }
329                         }
330                         return ERROR;
331                 }
332         }
333
334         // ==============================================================================================================================
335         // Getters and setters
336         // ==============================================================================================================================
337
338         public String getDescription() {
339                 // -------------------------------
340                 return _description;
341         }
342
343         public void setDescription(final String summary) {
344                 // -------------------------------------------
345                 this._description = summary;
346         }
347
348         public void setDocumentName(final String name) {
349                 // -----------------------------------------
350                 this._docname = name;
351         }
352
353         public void setDocumentState(final String state) {
354                 // -------------------------------------------
355                 this._state = ProgressState.valueOf(state);
356         }
357
358         public void setDocumentType(final String value) {
359                 // ------------------------------------------
360                 this._doctype = Integer.valueOf(value);
361         }
362
363         public void setFileName(final String name) {
364                 // -------------------------------------
365                 this._filename = name;
366         }
367
368         // ==============================================================================================================================
369         // Private service
370         // ==============================================================================================================================
371
372         private void setupDefaultUses(final DocumentType type) {
373                 Set<DocumentType> uses = type.getDefaultUses();
374
375                 for (Iterator<DocumentType> i = uses.iterator(); i.hasNext();) {
376                         DocumentType usetype = i.next();
377                         List<Document> usedoc = _mystudy.collectInvolvedDocuments(usetype);
378                         if (usedoc.isEmpty()) {
379                                 setupDefaultUses(usetype);
380                         } else {
381                                 _defuses.addAll(usedoc);
382                         }
383                 }
384         }
385
386         /**
387          * Get the repositoryService.
388          * 
389          * @return the repositoryService
390          */
391         public RepositoryService getRepositoryService() {
392                 return _repositoryService;
393         }
394
395         /**
396          * Set the repositoryService.
397          * 
398          * @param repositoryService
399          *            the repositoryService to set
400          */
401         public void setRepositoryService(final RepositoryService repositoryService) {
402                 _repositoryService = repositoryService;
403         }
404
405         /**
406          * Get the simulationContextService.
407          * 
408          * @return the simulationContextService
409          */
410         public SimulationContextService getSimulationContextService() {
411                 return _simulationContextService;
412         }
413
414         /**
415          * Set the simulationContextService.
416          * 
417          * @param simulationContextService
418          *            the simulationContextService to set
419          */
420         public void setSimulationContextService(
421                         final SimulationContextService simulationContextService) {
422                 _simulationContextService = simulationContextService;
423         }
424
425         /**
426          * Get the documentTypeService.
427          * 
428          * @return the documentTypeService
429          */
430         public DocumentTypeService getDocumentTypeService() {
431                 return _documentTypeService;
432         }
433
434         /**
435          * Set the documentTypeService.
436          * 
437          * @param documentTypeService
438          *            the documentTypeService to set
439          */
440         public void setDocumentTypeService(
441                         final DocumentTypeService documentTypeService) {
442                 _documentTypeService = documentTypeService;
443         }
444 }