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