1 package org.splat.module;
4 import java.util.ArrayList;
5 import java.util.Iterator;
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;
32 public class SaveDocumentAction extends Action {
37 private static final long serialVersionUID = -3364960833373200115L;
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
50 * Injected scenario service.
52 private ScenarioService _scenarioService;
54 * Injected publication service.
56 private PublicationService _publicationService;
58 * Injected step service.
60 private StepService _stepService;
62 * Injected document type service.
64 private DocumentTypeService _documentTypeService;
66 * Injected repository service.
68 private RepositoryService _repositoryService;
70 * Injected simulation context service.
72 private SimulationContextService _simulationContextService;
74 // ==============================================================================================================================
76 // ==============================================================================================================================
78 public String doSave() {
79 // -----------------------
80 Session connex = Database.getCurSession();
81 Transaction transax = connex.beginTransaction();
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
92 File upfile = new File(upath + _filename);
93 String[] table = _filename.split("\\x2E");
94 String format = table[table.length - 1];
96 // Creation of the document
97 getScenarioService().checkin(step.getOwner().getIndex()); // Modules necessarily save their data in a scenario step
100 Document.Properties dprop = new Document.Properties();
101 Publication credoc = getStepService().createDocument(
103 dprop.setName(_docname).setType(type).setFormat(format)
105 // Writing the uploaded file into the created document
106 File target = credoc.getSourceFile().asFile();
107 if (target.exists()) {
110 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
111 // upfile.renameTo(target);
113 // Saving the document in given state
114 getPublicationService().saveAs(credoc, _state);
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());
123 // Execution of module specific operations
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(
131 target = export.getTo().asFile();
132 if (target.exists()) {
135 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
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"));
148 getStepService().addSimulationContext(step, context);
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"));
157 getStepService().addSimulationContext(step, context);
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"));
166 getStepService().addSimulationContext(step, context);
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
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
181 } catch (HibernateException backerror) {
182 LOG.debug("Error rolling back transaction", backerror);
190 * Get the publicationService.
192 * @return publicationService
194 private PublicationService getPublicationService() {
195 return _publicationService;
199 * Set the publicationService.
201 * @param publicationService
202 * the publicationService to set
204 public void setPublicationService(
205 final PublicationService publicationService) {
206 _publicationService = publicationService;
210 * Get the scenarioService.
212 * @return scenarioService
214 public ScenarioService getScenarioService() {
215 return _scenarioService;
219 * Set the scenarioService.
221 * @param scenarioService
222 * the scenarioService to set
224 public void setScenarioService(final ScenarioService scenarioService) {
225 _scenarioService = scenarioService;
229 * Get the stepService.
231 * @return the stepService
233 public StepService getStepService() {
238 * Set the stepService.
241 * the stepService to set
243 public void setStepService(final StepService stepService) {
244 _stepService = stepService;
247 public String doUpdate() {
248 // -------------------------
252 public String doVersion() {
253 // --------------------------
254 Session connex = Database.getCurSession();
255 Transaction transax = connex.beginTransaction();
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
265 File upfile = new File(upath + _filename);
266 String[] table = _filename.split("\\x2E");
267 String format = table[table.length - 1];
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);
277 Publication next = getStepService().versionDocument(step, current,
280 // Writing the uploaded file into the created document
281 File target = next.getSourceFile().asFile();
282 if (target.exists()) {
285 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
286 // upfile.renameTo(target);
288 // Saving the document in given state
289 getPublicationService().saveAs(next, _state);
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());
297 // TODO: Outdating impacted document
299 // Execution of module specific operations
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,
306 String fname = table[0];
308 for (int i = 1; i < table.length - 1; i++) {
309 fname = fname + table[i];
311 upfile = new File(upath + fname + ".brep");
312 upfile.renameTo(export.getTo().asFile());
315 // Update of the open study
316 // mystudy.setSelection(mystudy.getSelection()); // Rebuild the presentation
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
326 } catch (HibernateException backerror) {
327 LOG.debug("Error rolling back transaction", backerror);
334 // ==============================================================================================================================
335 // Getters and setters
336 // ==============================================================================================================================
338 public String getDescription() {
339 // -------------------------------
343 public void setDescription(final String summary) {
344 // -------------------------------------------
345 this._description = summary;
348 public void setDocumentName(final String name) {
349 // -----------------------------------------
350 this._docname = name;
353 public void setDocumentState(final String state) {
354 // -------------------------------------------
355 this._state = ProgressState.valueOf(state);
358 public void setDocumentType(final String value) {
359 // ------------------------------------------
360 this._doctype = Integer.valueOf(value);
363 public void setFileName(final String name) {
364 // -------------------------------------
365 this._filename = name;
368 // ==============================================================================================================================
370 // ==============================================================================================================================
372 private void setupDefaultUses(final DocumentType type) {
373 Set<DocumentType> uses = type.getDefaultUses();
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);
381 _defuses.addAll(usedoc);
387 * Get the repositoryService.
389 * @return the repositoryService
391 public RepositoryService getRepositoryService() {
392 return _repositoryService;
396 * Set the repositoryService.
398 * @param repositoryService
399 * the repositoryService to set
401 public void setRepositoryService(final RepositoryService repositoryService) {
402 _repositoryService = repositoryService;
406 * Get the simulationContextService.
408 * @return the simulationContextService
410 public SimulationContextService getSimulationContextService() {
411 return _simulationContextService;
415 * Set the simulationContextService.
417 * @param simulationContextService
418 * the simulationContextService to set
420 public void setSimulationContextService(
421 final SimulationContextService simulationContextService) {
422 _simulationContextService = simulationContextService;
426 * Get the documentTypeService.
428 * @return the documentTypeService
430 public DocumentTypeService getDocumentTypeService() {
431 return _documentTypeService;
435 * Set the documentTypeService.
437 * @param documentTypeService
438 * the documentTypeService to set
440 public void setDocumentTypeService(
441 final DocumentTypeService documentTypeService) {
442 _documentTypeService = documentTypeService;