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.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;
32 public class SaveDocumentAction extends Action {
37 private static final long serialVersionUID = -3364960833373200115L;
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
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 repository service.
64 private RepositoryService _repositoryService;
66 * Injected simulation context service.
68 private SimulationContextService _simulationContextService;
70 // ==============================================================================================================================
72 // ==============================================================================================================================
74 public String doSave() {
75 // -----------------------
76 Session connex = Database.getSession();
77 Transaction transax = connex.beginTransaction();
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
88 File upfile = new File(upath + filename);
89 String[] table = filename.split("\\x2E");
90 String format = table[table.length - 1];
92 // Creation of the document
93 getScenarioService().checkin((Scenario) step.getOwner()); // Modules necessarily save their data in a scenario step
96 Document.Properties dprop = new Document.Properties();
97 Publication credoc = getStepService().createDocument(
99 dprop.setName(docname).setType(type).setFormat(format)
101 // Writing the uploaded file into the created document
102 File target = credoc.getSourceFile().asFile();
105 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
106 // upfile.renameTo(target);
108 // Saving the document in given state
109 getPublicationService().saveAs(credoc, state);
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());
118 // Execution of module specific operations
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");
125 target = export.getTo().asFile();
128 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
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"));
141 getStepService().addSimulationContext(step, context);
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"));
150 getStepService().addSimulationContext(step, context);
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"));
159 getStepService().addSimulationContext(step, context);
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
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
174 } catch (HibernateException backerror) {
175 logger.debug("Error rolling back transaction", backerror);
183 * Get the publicationService.
185 * @return publicationService
187 private PublicationService getPublicationService() {
188 return _publicationService;
192 * Set the publicationService.
194 * @param publicationService
195 * the publicationService to set
197 public void setPublicationService(PublicationService publicationService) {
198 _publicationService = publicationService;
202 * Get the scenarioService.
204 * @return scenarioService
206 public ScenarioService getScenarioService() {
207 return _scenarioService;
211 * Set the scenarioService.
213 * @param scenarioService
214 * the scenarioService to set
216 public void setScenarioService(ScenarioService scenarioService) {
217 _scenarioService = scenarioService;
221 * Get the stepService.
223 * @return the stepService
225 public StepService getStepService() {
230 * Set the stepService.
233 * the stepService to set
235 public void setStepService(StepService stepService) {
236 _stepService = stepService;
239 public String doUpdate() {
240 // -------------------------
244 public String doVersion() {
245 // --------------------------
246 Session connex = Database.getSession();
247 Transaction transax = connex.beginTransaction();
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
257 File upfile = new File(upath + filename);
258 String[] table = filename.split("\\x2E");
259 String format = table[table.length - 1];
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);
268 Publication next = getStepService().versionDocument(step, current,
271 // Writing the uploaded file into the created document
272 File target = next.getSourceFile().asFile();
275 Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
276 // upfile.renameTo(target);
278 // Saving the document in given state
279 getPublicationService().saveAs(next, state);
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());
287 // TODO: Outdating impacted document
289 // Execution of module specific operations
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];
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());
303 // Update of the open study
304 // mystudy.setSelection(mystudy.getSelection()); // Rebuild the presentation
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
314 } catch (HibernateException backerror) {
315 logger.debug("Error rolling back transaction", backerror);
322 // ==============================================================================================================================
323 // Getters and setters
324 // ==============================================================================================================================
326 public String getDescription() {
327 // -------------------------------
331 public void setDescription(String summary) {
332 // -------------------------------------------
333 this.summary = summary;
336 public void setDocumentName(String name) {
337 // -----------------------------------------
341 public void setDocumentState(String state) {
342 // -------------------------------------------
343 this.state = ProgressState.valueOf(state);
346 public void setDocumentType(String value) {
347 // ------------------------------------------
348 this.doctype = Integer.valueOf(value);
351 public void setFileName(String name) {
352 // -------------------------------------
353 this.filename = name;
356 // ==============================================================================================================================
358 // ==============================================================================================================================
360 private void setupDefaultUses(DocumentType type) {
361 // -------------------------------------------------
362 Set<DocumentType> uses = type.getDefaultUses();
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);
370 defuses.addAll(usedoc);
375 * Get the repositoryService.
377 * @return the repositoryService
379 public RepositoryService getRepositoryService() {
380 return _repositoryService;
384 * Set the repositoryService.
386 * @param repositoryService
387 * the repositoryService to set
389 public void setRepositoryService(RepositoryService repositoryService) {
390 _repositoryService = repositoryService;
394 * Get the simulationContextService.
396 * @return the simulationContextService
398 public SimulationContextService getSimulationContextService() {
399 return _simulationContextService;
403 * Set the simulationContextService.
405 * @param simulationContextService
406 * the simulationContextService to set
408 public void setSimulationContextService(
409 SimulationContextService simulationContextService) {
410 _simulationContextService = simulationContextService;