]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman/src/org/splat/simer/VersionDocumentAction.java
Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / VersionDocumentAction.java
index 1454d80a61009025e505c9a38a0470b371e8a4d6..c68b0ce61e735027dd20cf7053c2359a25fbc426 100644 (file)
@@ -14,247 +14,318 @@ import org.hibernate.HibernateException;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
 import org.splat.kernel.InvalidPropertyException;
-import org.splat.kernel.Relation;
-import org.splat.kernel.User;
+import org.splat.dal.bo.kernel.Relation;
+import org.splat.dal.bo.kernel.User;
 import org.splat.manox.Reader;
 import org.splat.manox.Toolbox;
-import org.splat.som.Database;
-import org.splat.som.Document;
-import org.splat.som.ProgressState;
-import org.splat.som.ProjectSettings;
-import org.splat.som.Publication;
+import org.splat.dal.dao.som.Database;
+import org.splat.dal.bo.som.Document;
+import org.splat.dal.bo.som.ProgressState;
+import org.splat.service.PublicationService;
+import org.splat.service.technical.ProjectSettingsService;
+import org.splat.dal.bo.som.Publication;
 import org.splat.som.Revision;
 import org.splat.som.Step;
-import org.splat.som.UsedByRelation;
-import org.splat.som.UsesRelation;
-
+import org.splat.dal.bo.som.UsedByRelation;
+import org.splat.dal.bo.som.UsesRelation;
 
 public class VersionDocumentAction extends UploadBaseNextAction {
 
-    private String            index     = null;   // Versioned document index
-    private List<Publication> usedby    = null;
-    private String            docusedby = null;
-    private String            summary   = null;   // Summary of changes in the new version
-    private String            docver    = "";     // Version number extracted from the imported file, if exist
-    private String            date      = "";     // Date extracted from the imported file, if exist
-
-    private static final long serialVersionUID = -5702264003232132168L;
-
-//  ==============================================================================================================================
-//  Action methods
-//  ==============================================================================================================================
-
-    public String doInitialize () {
-//  -----------------------------
-      Session      connex  = Database.getSession();
-      Transaction  transax = connex.beginTransaction();
-      User         user    = getConnectedUser();
-         File         updir   = Database.getDownloadDirectory(user);
-         File         upfile  = new File(updir.getPath() + "/" + filename);
-
-         mystudy  =  getOpenStudy();
-
-         Publication tag = mystudy.getSelectedStep().getDocument(Integer.valueOf(index));
-         Document    doc = tag.value();
-         deftype  =  doc.getType();
-         docname  =  doc.getTitle();
-      defuses  =  new Vector<Document>();
-      usedby   =  new Vector<Publication>();
-
-         Reader tool = Toolbox.getReader(upfile);
-         if (tool != null) {
-        String  fileref = tool.extractProperty("reference");
-        String  filever = tool.extractProperty("version");
-        if (fileref != null && !doc.getReference().equals(fileref)) {
-          setErrorCode("reference.mismatch");
-          return ERROR;
-        }
-        if (filever != null) try {
-          Revision.Format get    = new Revision.Format(ProjectSettings.getRevisionPattern());          
-          Revision        newver = get.parse(filever);
-          Revision        oldver = new Revision(doc.getVersion());
-          if (!newver.isGraterThan(oldver)) throw new InvalidPropertyException("version");
-          if ( newver.isMinor() ) state = ProgressState.inWORK;
-          else                    state = ProgressState.inDRAFT;
-          docver  = newver.toString();
-        } catch (Exception e) {
-          setErrorCode("version.mismatch");
-          return ERROR;
-        }
-        summary = tool.extractProperty("history");
-       date    = tool.extractProperty("date");
-       if (date != null) {
-         ResourceBundle   locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
-          SimpleDateFormat check  = new SimpleDateFormat(locale.getString("date.format"));
-          try {
-               check.parse(date);
-          } catch (ParseException e) {
-               setErrorCode("format.date");
-               return ERROR;
-          }
-       } else date = "";
-         }
-      setupDefaultUses(deftype);
-//    Add additional documents used by the current version
-         List<Relation> uses = doc.getRelations(UsesRelation.class);
-      for (Iterator<Relation> i=uses.iterator(); i.hasNext();) {
-       Document   used = (Document)i.next().getTo();
-       if (!defuses.contains(used)) defuses.add(used);
-      }
-//    Setup dependencies
-      List<Publication> relist = tag.getRelations(UsedByRelation.class);
-      for (Iterator<Publication> i=relist.iterator(); i.hasNext();) {
-               usedby.add(i.next());
-      }
-      transax.commit();
-      return SUCCESS;
-    }
-
-    public String doVersion () {
-//  -------------------------
-      if (action == ToDo.cancel) return "cancel";
-      
-      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);
-
-//      Versioning of the document
-        Document.Properties dprop   = new Document.Properties();
-        Publication         current = step.getDocument(Integer.valueOf(index));
-        Publication         next;
-
-        if (docver.length() == 0) {     // Importation of a foreign document
-          next  = step.versionDocument(current, dprop.setAuthor(user).setDescription(summary));
-          updir = next.getSourceFile().asFile();
-                 if (logger.isInfoEnabled()) logger.info("Moving \"" + upfile.getName() + "\" to \"" + updir.getPath() + "\".");
-          upfile.renameTo(updir);
-          try {
-            next.saveAs(state);        // May throw FileNotFound if rename was not done
-          } catch (FileNotFoundException saverror) {
-               Thread.sleep(1000);
-            logger.info("Waiting for the file.");
-            upfile.renameTo(updir);
-            next.saveAs(state);        // Forget it if throw again FileNotFound
-          }
-        } else {
-          if (date.length() > 0) {
-            ResourceBundle   locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
-            SimpleDateFormat get    = new SimpleDateFormat(locale.getString("date.format"));            
-            dprop.setDate(get.parse(date));
-          }
-          next  = step.versionDocument(current, dprop.setAuthor(user).setDescription(summary));
-          updir = next.getSourceFile().asFile();
-                 if (logger.isInfoEnabled()) logger.info("Moving \"" + upfile.getName() + "\" to \"" + updir.getPath() + "\".");
-          upfile.renameTo(updir);
-          try {
-            next.saveAs(new Revision(docver));
-          } catch (FileNotFoundException saverror) {
-            Thread.sleep(1000);
-            logger.info("Waiting for the file.");
-            upfile.renameTo(updir);
-            next.saveAs(state);
-          }
-        }
-//TODO: Remove current document details from the contents of open study
-
-//      Creation of uses relations
-        if (docuses != null) {
-          String[]     list = docuses.split(",");
-          for (int i=0; i<list.length; i++) {
-               Integer      index = Integer.valueOf(list[i].trim());
-               Publication  used  = getPublication(index);
-               next.addDependency(used);
-          }
-        }
-//      Outdating impacted document
-        HashSet<Integer> compatible = new HashSet<Integer>();
-        if (docusedby != null) {
-          String[] list = docusedby.split(",");
-          for (int i=0; i<list.length; i++) compatible.add(Integer.valueOf(list[i].trim()));
-        }
-        List<Publication> relist = current.getRelations(UsedByRelation.class);
-        for (Iterator<Publication> i=relist.iterator(); i.hasNext();) {
-          Publication  using = i.next();
-          if (!compatible.contains(using.getIndex())) using.outdate();
-        }        
-//      Update of the open study
-        mystudy.setSelection(mystudy.getSelection());   // Rebuilds the presentation
-//TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
-
-        transax.commit();
-        return SUCCESS;
-      }
-      catch (FileNotFoundException error) {
-        logger.error("Reason:", error);
-        setErrorCode("import.file");
-      }
-      catch (Exception error) {
-        logger.error("Reason:", error);
-        setErrorCode("internal");
-      }
-      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 getDate () {
-//  ------------------------
-      return date;
-    }
-    public List<Publication> getDependencies () {
-//  ------------------------------------------- 
-      return usedby;
-    }
-    public String getDescription () {
-//  -------------------------------
-      return summary;
-    }
-    public String getIndex () {
-//  -------------------------
-      return index;
-    }
-    public String getVersion () {
-//  ---------------------------
-      return docver;
-    }
-
-    public void setDate (String date) {
-//  ---------------------------------
-      this.date = date;
-    }
-    public void setDefaultDescription (String summary) {
-//  --------------------------------------------------
-      if (this.summary == null) this.summary = summary;
-    }
-    public void setDescription (String summary) {
-//  -------------------------------------------
-      this.summary = summary;
-    }
-    public void setIndex (String index) {
-//  -----------------------------------
-      this.index = index;
-    }
-    public void setUsedBy (String list) {
-//  -----------------------------------
-      this.docusedby = list;
-    }
-    public void setVersion (String value) {
-//  -------------------------------------
-      this.docver = value;
-    }
+       private String index = null; // Versioned document index
+       private List<Publication> usedby = null;
+       private String docusedby = null;
+       private String summary = null; // Summary of changes in the new version
+       private String docver = ""; // Version number extracted from the imported file, if exist
+       private String date = ""; // Date extracted from the imported file, if exist
+       private ProjectSettingsService _projectSettingsService;
+       private PublicationService _publicationService;
+
+       private static final long serialVersionUID = -5702264003232132168L;
+
+       // ==============================================================================================================================
+       // Action methods
+       // ==============================================================================================================================
+
+       public String doInitialize() {
+               // -----------------------------
+               Session connex = Database.getSession();
+               Transaction transax = connex.beginTransaction();
+               User user = getConnectedUser();
+               File updir = Database.getDownloadDirectory(user);
+               File upfile = new File(updir.getPath() + "/" + filename);
+
+               mystudy = getOpenStudy();
+
+               Publication tag = mystudy.getSelectedStep().getDocument(
+                               Integer.valueOf(index));
+               Document doc = tag.value();
+               deftype = doc.getType();
+               docname = doc.getTitle();
+               defuses = new Vector<Document>();
+               usedby = new Vector<Publication>();
+
+               Reader tool = Toolbox.getReader(upfile);
+               if (tool != null) {
+                       String fileref = tool.extractProperty("reference");
+                       String filever = tool.extractProperty("version");
+                       if (fileref != null && !doc.getReference().equals(fileref)) {
+                               setErrorCode("reference.mismatch");
+                               return ERROR;
+                       }
+                       if (filever != null)
+                               try {
+                                       Revision.Format get = new Revision.Format(
+                                                       getProjectSettings().getRevisionPattern());
+                                       Revision newver = get.parse(filever);
+                                       Revision oldver = new Revision(doc.getVersion());
+                                       if (!newver.isGraterThan(oldver))
+                                               throw new InvalidPropertyException("version");
+                                       if (newver.isMinor())
+                                               state = ProgressState.inWORK;
+                                       else
+                                               state = ProgressState.inDRAFT;
+                                       docver = newver.toString();
+                               } catch (Exception e) {
+                                       setErrorCode("version.mismatch");
+                                       return ERROR;
+                               }
+                       summary = tool.extractProperty("history");
+                       date = tool.extractProperty("date");
+                       if (date != null) {
+                               ResourceBundle locale = ResourceBundle.getBundle("som",
+                                               ApplicationSettings.getCurrentLocale());
+                               SimpleDateFormat check = new SimpleDateFormat(
+                                               locale.getString("date.format"));
+                               try {
+                                       check.parse(date);
+                               } catch (ParseException e) {
+                                       setErrorCode("format.date");
+                                       return ERROR;
+                               }
+                       } else
+                               date = "";
+               }
+               setupDefaultUses(deftype);
+               // Add additional documents used by the current version
+               List<Relation> uses = doc.getRelations(UsesRelation.class);
+               for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
+                       Document used = (Document) i.next().getTo();
+                       if (!defuses.contains(used))
+                               defuses.add(used);
+               }
+               // Setup dependencies
+               List<Publication> relist = tag.getRelations(UsedByRelation.class);
+               for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
+                       usedby.add(i.next());
+               }
+               transax.commit();
+               return SUCCESS;
+       }
+
+       public String doVersion() {
+               // -------------------------
+               if (action == ToDo.cancel)
+                       return "cancel";
+
+               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);
+
+                       // Versioning of the document
+                       Document.Properties dprop = new Document.Properties();
+                       Publication current = step.getDocument(Integer.valueOf(index));
+                       Publication next;
+
+                       if (docver.length() == 0) { // Importation of a foreign document
+                               next = step.versionDocument(current, dprop.setAuthor(user)
+                                               .setDescription(summary));
+                               updir = next.getSourceFile().asFile();
+                               if (logger.isInfoEnabled())
+                                       logger.info("Moving \"" + upfile.getName() + "\" to \""
+                                                       + updir.getPath() + "\".");
+                               upfile.renameTo(updir);
+                               try {
+                                       getPublicationService().saveAs(next, state); // May throw FileNotFound if rename was not done
+                               } catch (FileNotFoundException saverror) {
+                                       Thread.sleep(1000);
+                                       logger.info("Waiting for the file.");
+                                       upfile.renameTo(updir);
+                                       getPublicationService().saveAs(next, state); // Forget it if throw again FileNotFound
+                               }
+                       } else {
+                               if (date.length() > 0) {
+                                       ResourceBundle locale = ResourceBundle.getBundle("som",
+                                                       ApplicationSettings.getCurrentLocale());
+                                       SimpleDateFormat get = new SimpleDateFormat(
+                                                       locale.getString("date.format"));
+                                       dprop.setDate(get.parse(date));
+                               }
+                               next = step.versionDocument(current, dprop.setAuthor(user)
+                                               .setDescription(summary));
+                               updir = next.getSourceFile().asFile();
+                               if (logger.isInfoEnabled())
+                                       logger.info("Moving \"" + upfile.getName() + "\" to \""
+                                                       + updir.getPath() + "\".");
+                               upfile.renameTo(updir);
+                               try {
+                                       getPublicationService().saveAs(next, new Revision(docver));
+                               } catch (FileNotFoundException saverror) {
+                                       Thread.sleep(1000);
+                                       logger.info("Waiting for the file.");
+                                       upfile.renameTo(updir);
+                                       getPublicationService().saveAs(next, state);
+                               }
+                       }
+                       // TODO: Remove current document details from the contents of open study
+
+                       // Creation of uses relations
+                       if (docuses != null) {
+                               String[] list = docuses.split(",");
+                               for (int i = 0; i < list.length; i++) {
+                                       Integer index = Integer.valueOf(list[i].trim());
+                                       Publication used = getPublication(index);
+                                       next.addDependency(used);
+                               }
+                       }
+                       // Outdating impacted document
+                       HashSet<Integer> compatible = new HashSet<Integer>();
+                       if (docusedby != null) {
+                               String[] list = docusedby.split(",");
+                               for (int i = 0; i < list.length; i++)
+                                       compatible.add(Integer.valueOf(list[i].trim()));
+                       }
+                       List<Publication> relist = current
+                                       .getRelations(UsedByRelation.class);
+                       for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
+                               Publication using = i.next();
+                               if (!compatible.contains(using.getIndex()))
+                                       using.outdate();
+                       }
+                       // Update of the open study
+                       mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
+                       // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
+
+                       transax.commit();
+                       return SUCCESS;
+               } catch (FileNotFoundException error) {
+                       logger.error("Reason:", error);
+                       setErrorCode("import.file");
+               } catch (Exception error) {
+                       logger.error("Reason:", error);
+                       setErrorCode("internal");
+               }
+               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 getDate() {
+               // ------------------------
+               return date;
+       }
+
+       public List<Publication> getDependencies() {
+               // -------------------------------------------
+               return usedby;
+       }
+
+       public String getDescription() {
+               // -------------------------------
+               return summary;
+       }
+
+       public String getIndex() {
+               // -------------------------
+               return index;
+       }
+
+       public String getVersion() {
+               // ---------------------------
+               return docver;
+       }
+
+       public void setDate(String date) {
+               // ---------------------------------
+               this.date = date;
+       }
+
+       public void setDefaultDescription(String summary) {
+               // --------------------------------------------------
+               if (this.summary == null)
+                       this.summary = summary;
+       }
+
+       public void setDescription(String summary) {
+               // -------------------------------------------
+               this.summary = summary;
+       }
+
+       public void setIndex(String index) {
+               // -----------------------------------
+               this.index = index;
+       }
+
+       public void setUsedBy(String list) {
+               // -----------------------------------
+               this.docusedby = list;
+       }
+
+       public void setVersion(String value) {
+               // -------------------------------------
+               this.docver = value;
+       }
+
+       /**
+        * Get project settings.
+        * 
+        * @return Project settings service
+        */
+       private ProjectSettingsService getProjectSettings() {
+               return _projectSettingsService;
+       }
+
+       /**
+        * Set project settings service.
+        * 
+        * @param projectSettingsService
+        *            project settings service
+        */
+       public void setProjectSettings(ProjectSettingsService projectSettingsService) {
+               _projectSettingsService = projectSettingsService;
+       }
+
+       /**
+        * Get the publicationService.
+        * 
+        * @return the publicationService
+        */
+       public PublicationService getPublicationService() {
+               return _publicationService;
+       }
+
+       /**
+        * Set the publicationService.
+        * 
+        * @param publicationService
+        *            the publicationService to set
+        */
+       public void setPublicationService(PublicationService publicationService) {
+               _publicationService = publicationService;
+       }
 }
\ No newline at end of file