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.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;
33 public class SaveDocumentAction extends Action {
38 private static final long serialVersionUID = -3364960833373200115L;
43 private transient OpenStudy _mystudy = null;
44 private transient int _doctype = 0;
45 private transient String _filename = null;
46 private transient String _docname = null;
47 private transient ProgressState _state = null;
48 private transient List<Document> _defuses = null;
49 private String _description = null; // Summary of changes in the new version
51 * Injected scenario service.
53 private ScenarioService _scenarioService;
55 * Injected publication service.
57 private PublicationService _publicationService;
59 * Injected step service.
61 private StepService _stepService;
63 * Injected document type service.
65 private DocumentTypeService _documentTypeService;
67 * Injected repository service.
69 private RepositoryService _repositoryService;
71 * Injected simulation context service.
73 private SimulationContextService _simulationContextService;
75 // ==============================================================================================================================
77 // ==============================================================================================================================
79 public String doSave() {
80 // -----------------------
81 Session connex = Database.getCurSession();
82 Transaction transax = connex.beginTransaction();
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
93 File upfile = new File(upath + _filename);
94 String[] table = _filename.split("\\x2E");
95 String format = table[table.length - 1];
97 // Creation of the document
98 getScenarioService().checkin((Scenario) step.getOwner()); // Modules necessarily save their data in a scenario step
101 Document.Properties dprop = new Document.Properties();
102 Publication credoc = getStepService().createDocument(
104 dprop.setName(_docname).setType(type).setFormat(format)
106 // Writing the uploaded file into the created document
107 File target = credoc.getSourceFile().asFile();
108 if (target.exists()) {
111 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
112 // upfile.renameTo(target);
114 // Saving the document in given state
115 getPublicationService().saveAs(credoc, _state);
117 // Creation of default uses relations
118 _defuses = new ArrayList<Document>();
119 setupDefaultUses(type); // Recursive function
120 for (Iterator<Document> i = _defuses.iterator(); i.hasNext();) {
121 credoc.addDependency(i.next());
124 // Execution of module specific operations
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 ("part".equals(format)) {
129 ConvertsRelation export = getPublicationService().attach(
132 target = export.getTo().asFile();
133 if (target.exists()) {
136 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
138 // 2. Addition of simulation contexts
139 if ("model".equals(type)) { // 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"));
149 getStepService().addSimulationContext(step, context);
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"));
158 getStepService().addSimulationContext(step, context);
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"));
167 getStepService().addSimulationContext(step, context);
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
176 } catch (Exception saverror) {
177 LOG.error("Reason:", saverror);
178 if (transax != null && transax.isActive()) {
179 // Second try-catch as the rollback could fail as well
182 } catch (HibernateException backerror) {
183 LOG.debug("Error rolling back transaction", backerror);
191 * Get the publicationService.
193 * @return publicationService
195 private PublicationService getPublicationService() {
196 return _publicationService;
200 * Set the publicationService.
202 * @param publicationService
203 * the publicationService to set
205 public void setPublicationService(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(final DocumentTypeService documentTypeService) {
441 _documentTypeService = documentTypeService;