Salome HOME
NewStudyAction is improved. Specific business method for creation of a new study...
[tools/siman.git] / Workspace / Siman / src / org / splat / module / SaveDocumentAction.java
index 7d04e46e25c193c82d1065bdc6350d803b1e6aec..878975b33dfe3db0c9905bf78078d31624482284 100644 (file)
@@ -10,257 +10,428 @@ import org.hibernate.HibernateException;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
 import org.splat.kernel.Do;
-import org.splat.kernel.User;
+import org.splat.dal.bo.kernel.User;
+import org.splat.service.DocumentTypeService;
+import org.splat.service.PublicationService;
+import org.splat.service.ScenarioService;
+import org.splat.service.SimulationContextService;
+import org.splat.service.StepService;
+import org.splat.service.technical.RepositoryService;
 import org.splat.simer.Action;
 import org.splat.simer.OpenStudy;
-import org.splat.som.ConvertsRelation;
-import org.splat.som.Database;
-import org.splat.som.Document;
-import org.splat.som.DocumentType;
-import org.splat.som.ProgressState;
-import org.splat.som.Publication;
-import org.splat.som.Scenario;
-import org.splat.som.SimulationContext;
-import org.splat.som.SimulationContextType;
+import org.splat.dal.bo.som.ConvertsRelation;
+import org.splat.dal.dao.som.Database;
+import org.splat.dal.bo.som.Document;
+import org.splat.dal.bo.som.DocumentType;
+import org.splat.dal.bo.som.ProgressState;
+import org.splat.dal.bo.som.Publication;
+import org.splat.dal.bo.som.Scenario;
+import org.splat.dal.bo.som.SimulationContext;
+import org.splat.dal.bo.som.SimulationContextType;
 import org.splat.som.Step;
 
+public class SaveDocumentAction extends Action {
 
-public class SaveDocumentAction extends Action { 
-
-    private OpenStudy      mystudy  = null;
-    private int            doctype  = 0;
-    private String         filename = null;
-    private String         docname  = null;
-    private ProgressState  state    = null;
-    private List<Document> defuses  = null;
-    private String         summary  = null;   // Summary of changes in the new version
-
+       /**
+        * Serial version ID.
+        */
        private static final long serialVersionUID = -3364960833373200115L;
 
-//  ==============================================================================================================================
-//  Action methods
-//  ==============================================================================================================================
-
-    public String doSave () {
-//  -----------------------
-      Session      connex  = Database.getSession();
-      Transaction  transax = connex.beginTransaction();
-      try {
-//      Getting user inputs
-                      mystudy = getOpenStudy();
-        User          user    = getConnectedUser();
-        Step          step    = mystudy.getSelectedStep();
-           DocumentType  type    = Document.selectType(doctype);
-//             File          updir   = Database.getDownloadDirectory(user);
-//             File          upfile  = new File(updir.getPath() + "/" + filename);
-           String        upath   = Database.getTemplatePath();      // Instead of DownloadDirectory for sharing the "uploaded" file between users
-           File          upfile  = new File(upath + filename);
-        String[]      table   = filename.split("\\x2E");
-        String        format  = table[table.length-1];
-
-//      Creation of the document
-        ((Scenario)step.getOwner()).checkin();                   // Modules necessarily save their data in a scenario step
-        connex.flush();
-
-        Document.Properties dprop  = new Document.Properties();
-        Publication         credoc = step.createDocument(dprop.setName(docname)
-                                                              .setType(type)
-                                                              .setFormat(format)
-                                                                 .setAuthor(user));
-//      Writing the uploaded file into the created document
-        File target = credoc.getSourceFile().asFile();
-        if  (target.exists()) target.delete();
-        Do.copy(upfile, target);                                 // Instead of rename for keeping the "uploaded" file for further use
-//      upfile.renameTo(target);
-
-//      Saving the document in given state
-        credoc.saveAs(state);
-
-//      Creation of default uses relations
-       defuses = new Vector<Document>();
-        setupDefaultUses(type);             // Recursive function
-        for (Iterator<Document> i=defuses.iterator(); i.hasNext(); ) {
-          credoc.addDependency(i.next());
-        }
-
-//      Execution of module specific operations
-
-//      1. Conversion of the document to internal format, if required
-//TODO: The following code is temporary, waiting for the support of converters
-        if (format.equals("part")) {
-          ConvertsRelation  export = credoc.attach("brep");
-
-          target  = export.getTo().asFile();
-          if  (target.exists()) target.delete();
-          Do.copy(upfile, target);                              // Instead of rename for keeping the "uploaded" file for further use
-        }
-//      2. Addition of simulation contexts
-        if (type.equals("model")) {         // Set the characteristics of the mesh
-          SimulationContext.Properties  cprop   = new SimulationContext.Properties();        
-          SimulationContextType         ctype   = SimulationContext.selectType("model");
-          SimulationContext             context = Database.selectSimulationContext(ctype, "Éléments finis");
-          if (context == null) {
-               step.addSimulationContext(cprop.setType(ctype).setValue("Éléments finis"));
-          } else {
-               step.addSimulationContext(context);
-          }
-          ctype   = SimulationContext.selectType("element");
-          context = Database.selectSimulationContext(ctype, "Surfacique");
-          if (context == null) {
-            step.addSimulationContext(cprop.setType(ctype).setValue("Surfacique"));
-          } else {
-            step.addSimulationContext(context);
-          }
-          ctype   = SimulationContext.selectType("shape");
-          context = Database.selectSimulationContext(ctype, "Triangles");
-          if (context == null) {
-            step.addSimulationContext(cprop.setType(ctype).setValue("Triangles"));
-          } else {
-            step.addSimulationContext(context);
-          }
-        }
-//      Update of the open study
-//      mystudy.add(credoc);                                // Useless while the SIMER page need to be refreshed manually
-        getMenu("study").selects(mystudy.getSelection());   // Updates the menu icon, in case of first added document
-
-        transax.commit();
-        return SUCCESS;
-      }
-      catch (Exception saverror) {
-        logger.error("Reason:", saverror);
-        if (transax != null && transax.isActive()) {
-//            Second try-catch as the rollback could fail as well
-          try {
-            transax.rollback();
-          } catch (HibernateException backerror) {
-            logger.debug("Error rolling back transaction", backerror);
-          }
-        }
-        return ERROR;
-      }
-    }
-
-    public String doUpdate () {
-//  -------------------------
-      return SUCCESS;
-    }
-
-    public String doVersion () {
-//  --------------------------
-      Session      connex  = Database.getSession();
-      Transaction  transax = connex.beginTransaction();
-      try {
-//      Getting user inputs
-                   mystudy = getOpenStudy();
-        User       user    = getConnectedUser();
-        Step       step    = mystudy.getSelectedStep();
-//             File       updir   = Database.getDownloadDirectory(user);
-//             File       upfile  = new File(updir.getPath() + "/" + filename);
-           String     upath   = Database.getTemplatePath();   // Instead of DownloadDirectory for sharing the "uploaded" file between users
-           File       upfile  = new File(upath + filename);
-        String[]   table   = filename.split("\\x2E");
-        String     format  = table[table.length-1];
-
-//      Versioning of the document
-        Publication          current = mystudy.getSelectedDocument();
-        Document.Properties  dprop   = new Document.Properties();
-        dprop.setAuthor(user);
-        if (summary.length() > 0) dprop.setDescription(summary);
-
-        Publication  next = step.versionDocument(current, dprop);
-
-//      Writing the uploaded file into the created document
-        File target = next.getSourceFile().asFile();
-        if  (target.exists()) target.delete();
-        Do.copy(upfile, target);    // Instead of rename for keeping the "uploaded" file for further use
-//      upfile.renameTo(target);
-
-//      Saving the document in given state
-        next.saveAs(state);
-
-//      Creation of default uses relations
-       defuses = new Vector<Document>();
-        setupDefaultUses(next.value().getType());         // Recursive function
-        for (Iterator<Document> i=defuses.iterator(); i.hasNext(); ) {
-          next.addDependency(i.next());
-        }
-//TODO: Outdating impacted document
-
-//      Execution of module specific operations
-
-//      1. Conversion of the document to internal format, if required
-//TODO: The following code is temporary, waiting for the support of converters
-        if (format.equals("part")) {
-          ConvertsRelation  export = next.attach("brep");
-          String            fname  = table[0];
-
-          for (int i=1; i<table.length-1; i++) fname = fname + table[i];
-          upfile = new File(upath + fname + ".brep");
-          upfile.renameTo(export.getTo().asFile());
-        }
-
-//      Update of the open study
-//      mystudy.setSelection(mystudy.getSelection());   // Rebuild the presentation
-
-        transax.commit();
-        return SUCCESS;
-      }
-      catch (Exception saverror) {
-        logger.error("Reason:", saverror);
-        if (transax != null && transax.isActive()) {
-//        Second try-catch as the rollback could fail as well
-          try {
-               transax.rollback();
-          } catch (HibernateException backerror) {
-            logger.debug("Error rolling back transaction", backerror);
-          }
-        }
-        return ERROR;
-      }
-    }
-
-//  ==============================================================================================================================
-//  Getters and setters
-//  ==============================================================================================================================
-
-    public String getDescription () {
-//  -------------------------------
-      return summary;
-    }
-
-    public void setDescription (String summary) {
-//  -------------------------------------------
-      this.summary = summary;
-    }
-    public void setDocumentName (String name) {
-//  -----------------------------------------
-      this.docname = name;
-    }
-    public void setDocumentState (String state) {
-//  -------------------------------------------
-      this.state = ProgressState.valueOf(state);
-    }
-    public void setDocumentType (String value) {
-//  ------------------------------------------
-      this.doctype = Integer.valueOf(value);
-    }
-    public void setFileName (String name) {
-//  -------------------------------------
-      this.filename = name;
-    }
-//  ==============================================================================================================================
-//  Private service
-//  ==============================================================================================================================
-
-    private void setupDefaultUses (DocumentType type) {
-//  -------------------------------------------------
-      Set<DocumentType> uses = type.getDefaultUses();
-      
-      for (Iterator<DocumentType> i=uses.iterator(); i.hasNext();) {
-       DocumentType   usetype = i.next();
-       List<Document> usedoc  = mystudy.collectInvolvedDocuments(usetype);
-       if (usedoc.isEmpty()) setupDefaultUses(usetype);
-       else                  defuses.addAll(usedoc);
-      }
-    }
+       /**
+        * Current open study.
+        */
+       private OpenStudy mystudy = null;
+       private int doctype = 0;
+       private String filename = null;
+       private String docname = null;
+       private ProgressState state = null;
+       private List<Document> defuses = null;
+       private String summary = null; // Summary of changes in the new version
+       /**
+        * Injected scenario service.
+        */
+       private ScenarioService _scenarioService;
+       /**
+        * Injected publication service.
+        */
+       private PublicationService _publicationService;
+       /**
+        * Injected step service.
+        */
+       private StepService _stepService;
+       /**
+        * Injected document type service.
+        */
+       private DocumentTypeService _documentTypeService;
+       /**
+        * Injected repository service.
+        */
+       private RepositoryService _repositoryService;
+       /**
+        * Injected simulation context service.
+        */
+       private SimulationContextService _simulationContextService;
+
+       // ==============================================================================================================================
+       // Action methods
+       // ==============================================================================================================================
+
+       public String doSave() {
+               // -----------------------
+               Session connex = Database.getCurSession();
+               Transaction transax = connex.beginTransaction();
+               try {
+                       // Getting user inputs
+                       mystudy = getOpenStudy();
+                       User user = getConnectedUser();
+                       Step step = mystudy.getSelectedStep();
+                       DocumentType type = getDocumentTypeService().selectType(doctype);
+                       // File updir = Database.getDownloadDirectory(user);
+                       // File upfile = new File(updir.getPath() + "/" + filename);
+                       String upath = getRepositoryService().getTemplatePath(); // Instead of DownloadDirectory for sharing the "uploaded" file
+                       // between users
+                       File upfile = new File(upath + filename);
+                       String[] table = filename.split("\\x2E");
+                       String format = table[table.length - 1];
+
+                       // Creation of the document
+                       getScenarioService().checkin((Scenario) step.getOwner()); // Modules necessarily save their data in a scenario step
+                       connex.flush();
+
+                       Document.Properties dprop = new Document.Properties();
+                       Publication credoc = getStepService().createDocument(
+                                       step,
+                                       dprop.setName(docname).setType(type).setFormat(format)
+                                                       .setAuthor(user));
+                       // Writing the uploaded file into the created document
+                       File target = credoc.getSourceFile().asFile();
+                       if (target.exists())
+                               target.delete();
+                       Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
+                       // upfile.renameTo(target);
+
+                       // Saving the document in given state
+                       getPublicationService().saveAs(credoc, state);
+
+                       // Creation of default uses relations
+                       defuses = new Vector<Document>();
+                       setupDefaultUses(type); // Recursive function
+                       for (Iterator<Document> i = defuses.iterator(); i.hasNext();) {
+                               credoc.addDependency(i.next());
+                       }
+
+                       // Execution of module specific operations
+
+                       // 1. Conversion of the document to internal format, if required
+                       // TODO: The following code is temporary, waiting for the support of converters
+                       if (format.equals("part")) {
+                               ConvertsRelation export = getPublicationService().attach(
+                                               credoc, "brep");
+
+                               target = export.getTo().asFile();
+                               if (target.exists())
+                                       target.delete();
+                               Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
+                       }
+                       // 2. Addition of simulation contexts
+                       if (type.equals("model")) { // Set the characteristics of the mesh
+                               SimulationContext.Properties cprop = new SimulationContext.Properties();
+                               SimulationContextType ctype = getSimulationContextService()
+                                               .selectType("model");
+                               SimulationContext context = getSimulationContextService()
+                                               .selectSimulationContext(ctype, "Éléments finis");
+                               if (context == null) {
+                                       getStepService().addSimulationContext(step,
+                                                       cprop.setType(ctype).setValue("Éléments finis"));
+                               } else {
+                                       getStepService().addSimulationContext(step, context);
+                               }
+                               ctype = getSimulationContextService().selectType("element");
+                               context = getSimulationContextService()
+                                               .selectSimulationContext(ctype, "Surfacique");
+                               if (context == null) {
+                                       getStepService().addSimulationContext(step,
+                                                       cprop.setType(ctype).setValue("Surfacique"));
+                               } else {
+                                       getStepService().addSimulationContext(step, context);
+                               }
+                               ctype = getSimulationContextService().selectType("shape");
+                               context = getSimulationContextService()
+                                               .selectSimulationContext(ctype, "Triangles");
+                               if (context == null) {
+                                       getStepService().addSimulationContext(step,
+                                                       cprop.setType(ctype).setValue("Triangles"));
+                               } else {
+                                       getStepService().addSimulationContext(step, context);
+                               }
+                       }
+                       // Update of the open study
+                       // mystudy.add(credoc); // Useless while the SIMER page need to be refreshed manually
+                       getMenu("study").selects(mystudy.getSelection()); // Updates the menu icon, in case of first added document
+
+                       transax.commit();
+                       return SUCCESS;
+               } catch (Exception saverror) {
+                       logger.error("Reason:", saverror);
+                       if (transax != null && transax.isActive()) {
+                               // Second try-catch as the rollback could fail as well
+                               try {
+                                       transax.rollback();
+                               } catch (HibernateException backerror) {
+                                       logger.debug("Error rolling back transaction", backerror);
+                               }
+                       }
+                       return ERROR;
+               }
+       }
+
+       /**
+        * Get the publicationService.
+        * 
+        * @return publicationService
+        */
+       private PublicationService getPublicationService() {
+               return _publicationService;
+       }
+
+       /**
+        * Set the publicationService.
+        * 
+        * @param publicationService
+        *            the publicationService to set
+        */
+       public void setPublicationService(PublicationService publicationService) {
+               _publicationService = publicationService;
+       }
+
+       /**
+        * Get the scenarioService.
+        * 
+        * @return scenarioService
+        */
+       public ScenarioService getScenarioService() {
+               return _scenarioService;
+       }
+
+       /**
+        * Set the scenarioService.
+        * 
+        * @param scenarioService
+        *            the scenarioService to set
+        */
+       public void setScenarioService(ScenarioService scenarioService) {
+               _scenarioService = scenarioService;
+       }
+
+       /**
+        * Get the stepService.
+        * 
+        * @return the stepService
+        */
+       public StepService getStepService() {
+               return _stepService;
+       }
+
+       /**
+        * Set the stepService.
+        * 
+        * @param stepService
+        *            the stepService to set
+        */
+       public void setStepService(StepService stepService) {
+               _stepService = stepService;
+       }
+
+       public String doUpdate() {
+               // -------------------------
+               return SUCCESS;
+       }
+
+       public String doVersion() {
+               // --------------------------
+               Session connex = Database.getCurSession();
+               Transaction transax = connex.beginTransaction();
+               try {
+                       // Getting user inputs
+                       mystudy = getOpenStudy();
+                       User user = getConnectedUser();
+                       Step step = mystudy.getSelectedStep();
+                       // File updir = Database.getDownloadDirectory(user);
+                       // File upfile = new File(updir.getPath() + "/" + filename);
+                       String upath = getRepositoryService().getTemplatePath(); // Instead of DownloadDirectory for sharing the "uploaded" file
+                       // between users
+                       File upfile = new File(upath + filename);
+                       String[] table = filename.split("\\x2E");
+                       String format = table[table.length - 1];
+
+                       // Versioning of the document
+                       Publication current = mystudy.getSelectedDocument();
+                       Document.Properties dprop = new Document.Properties();
+                       dprop.setAuthor(user);
+                       if (summary.length() > 0)
+                               dprop.setDescription(summary);
+
+                       Publication next = getStepService().versionDocument(step, current,
+                                       dprop);
+
+                       // Writing the uploaded file into the created document
+                       File target = next.getSourceFile().asFile();
+                       if (target.exists())
+                               target.delete();
+                       Do.copy(upfile, target); // Instead of rename for keeping the "uploaded" file for further use
+                       // upfile.renameTo(target);
+
+                       // Saving the document in given state
+                       getPublicationService().saveAs(next, state);
+
+                       // Creation of default uses relations
+                       defuses = new Vector<Document>();
+                       setupDefaultUses(next.value().getType()); // Recursive function
+                       for (Iterator<Document> i = defuses.iterator(); i.hasNext();) {
+                               next.addDependency(i.next());
+                       }
+                       // TODO: Outdating impacted document
+
+                       // Execution of module specific operations
+
+                       // 1. Conversion of the document to internal format, if required
+                       // TODO: The following code is temporary, waiting for the support of converters
+                       if (format.equals("part")) {
+                               ConvertsRelation export = getPublicationService().attach(next,
+                                               "brep");
+                               String fname = table[0];
+
+                               for (int i = 1; i < table.length - 1; i++)
+                                       fname = fname + table[i];
+                               upfile = new File(upath + fname + ".brep");
+                               upfile.renameTo(export.getTo().asFile());
+                       }
+
+                       // Update of the open study
+                       // mystudy.setSelection(mystudy.getSelection()); // Rebuild the presentation
+
+                       transax.commit();
+                       return SUCCESS;
+               } catch (Exception saverror) {
+                       logger.error("Reason:", saverror);
+                       if (transax != null && transax.isActive()) {
+                               // Second try-catch as the rollback could fail as well
+                               try {
+                                       transax.rollback();
+                               } catch (HibernateException backerror) {
+                                       logger.debug("Error rolling back transaction", backerror);
+                               }
+                       }
+                       return ERROR;
+               }
+       }
+
+       // ==============================================================================================================================
+       // Getters and setters
+       // ==============================================================================================================================
+
+       public String getDescription() {
+               // -------------------------------
+               return summary;
+       }
+
+       public void setDescription(String summary) {
+               // -------------------------------------------
+               this.summary = summary;
+       }
+
+       public void setDocumentName(String name) {
+               // -----------------------------------------
+               this.docname = name;
+       }
+
+       public void setDocumentState(String state) {
+               // -------------------------------------------
+               this.state = ProgressState.valueOf(state);
+       }
+
+       public void setDocumentType(String value) {
+               // ------------------------------------------
+               this.doctype = Integer.valueOf(value);
+       }
+
+       public void setFileName(String name) {
+               // -------------------------------------
+               this.filename = name;
+       }
+
+       // ==============================================================================================================================
+       // Private service
+       // ==============================================================================================================================
+
+       private void setupDefaultUses(DocumentType type) {
+               Set<DocumentType> uses = type.getDefaultUses();
+
+               for (Iterator<DocumentType> i = uses.iterator(); i.hasNext();) {
+                       DocumentType usetype = i.next();
+                       List<Document> usedoc = mystudy.collectInvolvedDocuments(usetype);
+                       if (usedoc.isEmpty())
+                               setupDefaultUses(usetype);
+                       else
+                               defuses.addAll(usedoc);
+               }
+       }
+
+       /**
+        * Get the repositoryService.
+        * 
+        * @return the repositoryService
+        */
+       public RepositoryService getRepositoryService() {
+               return _repositoryService;
+       }
+
+       /**
+        * Set the repositoryService.
+        * 
+        * @param repositoryService
+        *            the repositoryService to set
+        */
+       public void setRepositoryService(RepositoryService repositoryService) {
+               _repositoryService = repositoryService;
+       }
+
+       /**
+        * Get the simulationContextService.
+        * 
+        * @return the simulationContextService
+        */
+       public SimulationContextService getSimulationContextService() {
+               return _simulationContextService;
+       }
+
+       /**
+        * Set the simulationContextService.
+        * 
+        * @param simulationContextService
+        *            the simulationContextService to set
+        */
+       public void setSimulationContextService(
+                       SimulationContextService simulationContextService) {
+               _simulationContextService = simulationContextService;
+       }
+
+       /**
+        * Get the documentTypeService.
+        * 
+        * @return the documentTypeService
+        */
+       public DocumentTypeService getDocumentTypeService() {
+               return _documentTypeService;
+       }
+
+       /**
+        * Set the documentTypeService.
+        * 
+        * @param documentTypeService
+        *            the documentTypeService to set
+        */
+       public void setDocumentTypeService(DocumentTypeService documentTypeService) {
+               _documentTypeService = documentTypeService;
+       }
 }
\ No newline at end of file