1 package org.splat.module;
4 import java.util.Iterator;
7 import java.util.Vector;
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.DocumentTypeService;
15 import org.splat.service.PublicationService;
16 import org.splat.service.ScenarioService;
17 import org.splat.service.SimulationContextService;
18 import org.splat.service.StepService;
19 import org.splat.service.technical.RepositoryService;
20 import org.splat.simer.Action;
21 import org.splat.simer.OpenStudy;
22 import org.splat.dal.bo.som.ConvertsRelation;
23 import org.splat.dal.dao.som.Database;
24 import org.splat.dal.bo.som.Document;
25 import org.splat.dal.bo.som.DocumentType;
26 import org.splat.dal.bo.som.ProgressState;
27 import org.splat.dal.bo.som.Publication;
28 import org.splat.dal.bo.som.Scenario;
29 import org.splat.dal.bo.som.SimulationContext;
30 import org.splat.dal.bo.som.SimulationContextType;
31 import org.splat.som.Step;
33 public class SaveDocumentAction extends Action {
38 private static final long serialVersionUID = -3364960833373200115L;
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
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();
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 Vector<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 (format.equals("part")) {
128 ConvertsRelation export = getPublicationService().attach(
131 target = export.getTo().asFile();
134 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
136 // 2. Addition of simulation contexts
137 if (type.equals("model")) { // Set the characteristics of the mesh
138 SimulationContext.Properties cprop = new SimulationContext.Properties();
139 SimulationContextType ctype = getSimulationContextService()
140 .selectType("model");
141 SimulationContext context = getSimulationContextService()
142 .selectSimulationContext(ctype, "Éléments finis");
143 if (context == null) {
144 getStepService().addSimulationContext(step,
145 cprop.setType(ctype).setValue("Éléments finis"));
147 getStepService().addSimulationContext(step, context);
149 ctype = getSimulationContextService().selectType("element");
150 context = getSimulationContextService()
151 .selectSimulationContext(ctype, "Surfacique");
152 if (context == null) {
153 getStepService().addSimulationContext(step,
154 cprop.setType(ctype).setValue("Surfacique"));
156 getStepService().addSimulationContext(step, context);
158 ctype = getSimulationContextService().selectType("shape");
159 context = getSimulationContextService()
160 .selectSimulationContext(ctype, "Triangles");
161 if (context == null) {
162 getStepService().addSimulationContext(step,
163 cprop.setType(ctype).setValue("Triangles"));
165 getStepService().addSimulationContext(step, context);
168 // Update of the open study
169 // mystudy.add(credoc); // Useless while the SIMER page need to be refreshed manually
170 getMenu("study").selects(mystudy.getSelection()); // Updates the menu icon, in case of first added document
174 } catch (Exception saverror) {
175 logger.error("Reason:", saverror);
176 if (transax != null && transax.isActive()) {
177 // Second try-catch as the rollback could fail as well
180 } catch (HibernateException backerror) {
181 logger.debug("Error rolling back transaction", backerror);
189 * Get the publicationService.
191 * @return publicationService
193 private PublicationService getPublicationService() {
194 return _publicationService;
198 * Set the publicationService.
200 * @param publicationService
201 * the publicationService to set
203 public void setPublicationService(PublicationService publicationService) {
204 _publicationService = publicationService;
208 * Get the scenarioService.
210 * @return scenarioService
212 public ScenarioService getScenarioService() {
213 return _scenarioService;
217 * Set the scenarioService.
219 * @param scenarioService
220 * the scenarioService to set
222 public void setScenarioService(ScenarioService scenarioService) {
223 _scenarioService = scenarioService;
227 * Get the stepService.
229 * @return the stepService
231 public StepService getStepService() {
236 * Set the stepService.
239 * the stepService to set
241 public void setStepService(StepService stepService) {
242 _stepService = stepService;
245 public String doUpdate() {
246 // -------------------------
250 public String doVersion() {
251 // --------------------------
252 Session connex = Database.getCurSession();
253 Transaction transax = connex.beginTransaction();
255 // Getting user inputs
256 mystudy = getOpenStudy();
257 User user = getConnectedUser();
258 Step step = mystudy.getSelectedStep();
259 // File updir = Database.getDownloadDirectory(user);
260 // File upfile = new File(updir.getPath() + "/" + filename);
261 String upath = getRepositoryService().getTemplatePath(); // Instead of DownloadDirectory for sharing the "uploaded" file
263 File upfile = new File(upath + filename);
264 String[] table = filename.split("\\x2E");
265 String format = table[table.length - 1];
267 // Versioning of the document
268 Publication current = mystudy.getSelectedDocument();
269 Document.Properties dprop = new Document.Properties();
270 dprop.setAuthor(user);
271 if (summary.length() > 0)
272 dprop.setDescription(summary);
274 Publication next = getStepService().versionDocument(step, current,
277 // Writing the uploaded file into the created document
278 File target = next.getSourceFile().asFile();
281 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
282 // upfile.renameTo(target);
284 // Saving the document in given state
285 getPublicationService().saveAs(next, state);
287 // Creation of default uses relations
288 defuses = new Vector<Document>();
289 setupDefaultUses(next.value().getType()); // Recursive function
290 for (Iterator<Document> i = defuses.iterator(); i.hasNext();) {
291 next.addDependency(i.next());
293 // TODO: Outdating impacted document
295 // Execution of module specific operations
297 // 1. Conversion of the document to internal format, if required
298 // TODO: The following code is temporary, waiting for the support of converters
299 if (format.equals("part")) {
300 ConvertsRelation export = getPublicationService().attach(next,
302 String fname = table[0];
304 for (int i = 1; i < table.length - 1; i++)
305 fname = fname + table[i];
306 upfile = new File(upath + fname + ".brep");
307 upfile.renameTo(export.getTo().asFile());
310 // Update of the open study
311 // mystudy.setSelection(mystudy.getSelection()); // Rebuild the presentation
315 } catch (Exception saverror) {
316 logger.error("Reason:", saverror);
317 if (transax != null && transax.isActive()) {
318 // Second try-catch as the rollback could fail as well
321 } catch (HibernateException backerror) {
322 logger.debug("Error rolling back transaction", backerror);
329 // ==============================================================================================================================
330 // Getters and setters
331 // ==============================================================================================================================
333 public String getDescription() {
334 // -------------------------------
338 public void setDescription(String summary) {
339 // -------------------------------------------
340 this.summary = summary;
343 public void setDocumentName(String name) {
344 // -----------------------------------------
348 public void setDocumentState(String state) {
349 // -------------------------------------------
350 this.state = ProgressState.valueOf(state);
353 public void setDocumentType(String value) {
354 // ------------------------------------------
355 this.doctype = Integer.valueOf(value);
358 public void setFileName(String name) {
359 // -------------------------------------
360 this.filename = name;
363 // ==============================================================================================================================
365 // ==============================================================================================================================
367 private void setupDefaultUses(DocumentType type) {
368 Set<DocumentType> uses = type.getDefaultUses();
370 for (Iterator<DocumentType> i = uses.iterator(); i.hasNext();) {
371 DocumentType usetype = i.next();
372 List<Document> usedoc = mystudy.collectInvolvedDocuments(usetype);
373 if (usedoc.isEmpty())
374 setupDefaultUses(usetype);
376 defuses.addAll(usedoc);
381 * Get the repositoryService.
383 * @return the repositoryService
385 public RepositoryService getRepositoryService() {
386 return _repositoryService;
390 * Set the repositoryService.
392 * @param repositoryService
393 * the repositoryService to set
395 public void setRepositoryService(RepositoryService repositoryService) {
396 _repositoryService = repositoryService;
400 * Get the simulationContextService.
402 * @return the simulationContextService
404 public SimulationContextService getSimulationContextService() {
405 return _simulationContextService;
409 * Set the simulationContextService.
411 * @param simulationContextService
412 * the simulationContextService to set
414 public void setSimulationContextService(
415 SimulationContextService simulationContextService) {
416 _simulationContextService = simulationContextService;
420 * Get the documentTypeService.
422 * @return the documentTypeService
424 public DocumentTypeService getDocumentTypeService() {
425 return _documentTypeService;
429 * Set the documentTypeService.
431 * @param documentTypeService
432 * the documentTypeService to set
434 public void setDocumentTypeService(DocumentTypeService documentTypeService) {
435 _documentTypeService = documentTypeService;