Salome HOME
PMD plugin is refreshed. Some code is modified to respect PMD rules. Ant build proced...
[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.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.Scenario;
19 import org.splat.dal.bo.som.SimulationContext;
20 import org.splat.dal.bo.som.SimulationContextType;
21 import org.splat.dal.dao.som.Database;
22 import org.splat.kernel.Do;
23 import org.splat.service.DocumentTypeService;
24 import org.splat.service.PublicationService;
25 import org.splat.service.ScenarioService;
26 import org.splat.service.SimulationContextService;
27 import org.splat.service.StepService;
28 import org.splat.service.technical.RepositoryService;
29 import org.splat.simer.Action;
30 import org.splat.simer.OpenStudy;
31 import org.splat.som.Step;
32
33 public class SaveDocumentAction extends Action {
34
35         /**
36          * Serial version ID.
37          */
38         private static final long serialVersionUID = -3364960833373200115L;
39
40         /**
41          * Current open study.
42          */
43         private OpenStudy mystudy = null;
44         private int doctype = 0;
45         private String filename = null;
46         private String docname = null;
47         private ProgressState state = null;
48         private List<Document> defuses = null;
49         private String summary = null; // Summary of changes in the new version
50         /**
51          * Injected scenario service.
52          */
53         private ScenarioService _scenarioService;
54         /**
55          * Injected publication service.
56          */
57         private PublicationService _publicationService;
58         /**
59          * Injected step service.
60          */
61         private StepService _stepService;
62         /**
63          * Injected document type service.
64          */
65         private DocumentTypeService _documentTypeService;
66         /**
67          * Injected repository service.
68          */
69         private RepositoryService _repositoryService;
70         /**
71          * Injected simulation context service.
72          */
73         private SimulationContextService _simulationContextService;
74
75         // ==============================================================================================================================
76         // Action methods
77         // ==============================================================================================================================
78
79         public String doSave() {
80                 // -----------------------
81                 Session connex = Database.getCurSession();
82                 Transaction transax = connex.beginTransaction();
83                 try {
84                         // Getting user inputs
85                         mystudy = getOpenStudy();
86                         User user = getConnectedUser();
87                         Step step = mystudy.getSelectedStep();
88                         DocumentType type = getDocumentTypeService().selectType(doctype);
89                         // File updir = Database.getDownloadDirectory(user);
90                         // File upfile = new File(updir.getPath() + "/" + filename);
91                         String upath = getRepositoryService().getTemplatePath(); // Instead of DownloadDirectory for sharing the "uploaded" file
92                         // between users
93                         File upfile = new File(upath + filename);
94                         String[] table = filename.split("\\x2E");
95                         String format = table[table.length - 1];
96
97                         // Creation of the document
98                         getScenarioService().checkin((Scenario) step.getOwner()); // Modules necessarily save their data in a scenario step
99                         connex.flush();
100
101                         Document.Properties dprop = new Document.Properties();
102                         Publication credoc = getStepService().createDocument(
103                                         step,
104                                         dprop.setName(docname).setType(type).setFormat(format)
105                                                         .setAuthor(user));
106                         // Writing the uploaded file into the created document
107                         File target = credoc.getSourceFile().asFile();
108                         if (target.exists()) {
109                                 target.delete();
110                         }
111                         Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
112                         // upfile.renameTo(target);
113
114                         // Saving the document in given state
115                         getPublicationService().saveAs(credoc, state);
116
117                         // Creation of default uses relations
118                         defuses = new Vector<Document>();
119                         setupDefaultUses(type); // Recursive function
120                         for (Iterator<Document> i = defuses.iterator(); i.hasNext();) {
121                                 credoc.addDependency(i.next());
122                         }
123
124                         // Execution of module specific operations
125
126                         // 1. Conversion of the document to internal format, if required
127                         // TODO: The following code is temporary, waiting for the support of converters
128                         if (format.equals("part")) {
129                                 ConvertsRelation export = getPublicationService().attach(
130                                                 credoc, "brep");
131
132                                 target = export.getTo().asFile();
133                                 if (target.exists()) {
134                                         target.delete();
135                                 }
136                                 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
137                         }
138                         // 2. Addition of simulation contexts
139                         if (type.equals("model")) { // Set the characteristics of the mesh
140                                 SimulationContext.Properties cprop = new SimulationContext.Properties();
141                                 SimulationContextType ctype = getSimulationContextService()
142                                                 .selectType("model");
143                                 SimulationContext context = getSimulationContextService()
144                                                 .selectSimulationContext(ctype, "Éléments finis");
145                                 if (context == null) {
146                                         getStepService().addSimulationContext(step,
147                                                         cprop.setType(ctype).setValue("Éléments finis"));
148                                 } else {
149                                         getStepService().addSimulationContext(step, context);
150                                 }
151                                 ctype = getSimulationContextService().selectType("element");
152                                 context = getSimulationContextService()
153                                                 .selectSimulationContext(ctype, "Surfacique");
154                                 if (context == null) {
155                                         getStepService().addSimulationContext(step,
156                                                         cprop.setType(ctype).setValue("Surfacique"));
157                                 } else {
158                                         getStepService().addSimulationContext(step, context);
159                                 }
160                                 ctype = getSimulationContextService().selectType("shape");
161                                 context = getSimulationContextService()
162                                                 .selectSimulationContext(ctype, "Triangles");
163                                 if (context == null) {
164                                         getStepService().addSimulationContext(step,
165                                                         cprop.setType(ctype).setValue("Triangles"));
166                                 } else {
167                                         getStepService().addSimulationContext(step, context);
168                                 }
169                         }
170                         // Update of the open study
171                         // mystudy.add(credoc); // Useless while the SIMER page need to be refreshed manually
172                         getMenu("study").selects(mystudy.getSelection()); // Updates the menu icon, in case of first added document
173
174                         transax.commit();
175                         return SUCCESS;
176                 } catch (Exception saverror) {
177                         logger.error("Reason:", saverror);
178                         if (transax != null && transax.isActive()) {
179                                 // Second try-catch as the rollback could fail as well
180                                 try {
181                                         transax.rollback();
182                                 } catch (HibernateException backerror) {
183                                         logger.debug("Error rolling back transaction", backerror);
184                                 }
185                         }
186                         return ERROR;
187                 }
188         }
189
190         /**
191          * Get the publicationService.
192          * 
193          * @return publicationService
194          */
195         private PublicationService getPublicationService() {
196                 return _publicationService;
197         }
198
199         /**
200          * Set the publicationService.
201          * 
202          * @param publicationService
203          *            the publicationService to set
204          */
205         public void setPublicationService(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 (summary.length() > 0) {
274                                 dprop.setDescription(summary);
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 Vector<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 (format.equals("part")) {
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                         logger.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                                         logger.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 summary;
341         }
342
343         public void setDescription(final String summary) {
344                 // -------------------------------------------
345                 this.summary = 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(final DocumentTypeService documentTypeService) {
441                 _documentTypeService = documentTypeService;
442         }
443 }