Salome HOME
Default document types mappings are moved from application settings (my.xml) to proje...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / technical / ProjectSettingsServiceImpl.java
index 49f84de4a7dcbc795925dfddb64ec6ab5655eb71..c9498f5135faf538d6b0270257892a4a192cda01 100644 (file)
@@ -53,6 +53,11 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
        protected final static Logger LOG = Logger
                        .getLogger(ProjectSettingsServiceImpl.class);
 
+       /**
+        * Type attribute name.
+        */
+       private final static String TYPE_ATTR = "type";
+
        // Non persistent configuration information
        /**
         * Repository settings.
@@ -77,11 +82,11 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
        /**
         * Configuration document validation cycles.
         */
-       private transient List<ProjectSettingsValidationCycle> _concycles;
+       private transient final List<ProjectSettingsValidationCycle> _concycles = new ArrayList<ProjectSettingsValidationCycle>();
        /**
         * Document type mappings to file formats which should be imported into SALOME during check-out.
         */
-       private transient Map<String, List<String>> _mapimport;
+       private transient final Map<String, List<String>> _mapimport = new HashMap<String, List<String>>();
 
        // Temporary attributes initialized from the configuration file for populating the database with object types
        /**
@@ -123,8 +128,27 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
         */
        private DocumentTypeService _documentTypeService;
 
+       /**
+        * Default document types structured by step.formats.
+        */
+       private transient final Map<String, DocumentType> _defdoctype = new LinkedHashMap<String, DocumentType>();
+
+       /**
+        * File naming strategy enumeration.
+        */
        public enum FileNaming {
-               title, encoded, asis
+               /**
+                * Name by document title.
+                */
+               title,
+               /**
+                * Generate encoded name.
+                */
+               encoded,
+               /**
+                * Keep original file name.
+                */
+               asis
        }
 
        /**
@@ -318,6 +342,44 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                                format));
        }
 
+       /**
+        * Get default document type for the given file format on the given study step.
+        * 
+        * @param step
+        *            the study step
+        * @param format
+        *            the file format (extension)
+        * @return document type
+        */
+       public DocumentType getDefaultDocumentType(final Step step,
+                       final String format) {
+               String[] table = format.split("\\x2E");
+               return _defdoctype
+                               .get(step.getNumber() + "." + table[table.length - 1]); // May be null
+       }
+
+       /**
+        * Get the list of default formats for the given study step.
+        * 
+        * @param step
+        *            the study step
+        * @return list of formats (file extensions)
+        */
+       public List<String> getDefaultFormats(final Step step) {
+               Integer stepNumber = step.getNumber();
+               List<String> result = new ArrayList<String>();
+
+               for (String i : _defdoctype.keySet()) {
+                       String[] key = i.split("\\x2E");
+                       // PDF is not an authoring format
+                       if (stepNumber.equals(Integer.valueOf(key[0]))
+                                       && (!key[1].equals("pdf"))) {
+                               result.add(key[1]); // Formats are unique
+                       }
+               }
+               return result;
+       }
+
        /**
         * Initialize the database: create all necessary default staff defined in the configuration file.
         */
@@ -394,10 +456,13 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                                                        resultype);
                                }
                        }
-                       // Validations tag
-                       _concycles = loadValidationCycles(children, resultype);
 
-                       _mapimport = loadFormatMappings(children);
+                       // Validations tag
+                       loadValidationCycles(children, resultype);
+                       // Load steps result document types mapped to file formats
+                       loadFormatMappings(children);
+                       // Load default mapping of file formats to document types for each step
+                       loadDefaultDocTypes(children);
 
                        if (!getDatabase().getCheckedDB().isInitialized()) {
                                // Load object type definitions
@@ -412,7 +477,8 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                                        child = nlist.item(i);
                                        if ("article".equals(child.getNodeName())) {
                                                natr = child.getAttributes();
-                                               String type = natr.getNamedItem("type").getNodeValue();
+                                               String type = natr.getNamedItem(TYPE_ATTR)
+                                                               .getNodeValue();
                                                String uses = null;
                                                child = natr.getNamedItem("uses");
                                                if (child != null) {
@@ -436,11 +502,9 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
         * 
         * @param children
         *            XML nodes
-        * @return map of document type names to lists of file formats
         */
-       private Map<String, List<String>> loadFormatMappings(
-                       final Map<String, Node> children) {
-               Map<String, List<String>> res = new HashMap<String, List<String>>();
+       private void loadFormatMappings(final Map<String, Node> children) {
+               _mapimport.clear();
                Element maps = (Element) children.get("mappings");
                Element doc, imp;
                String type, format;
@@ -451,7 +515,7 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                        docs = maps.getElementsByTagName("document");
                        for (int i = 0; i < docs.getLength(); i++) {
                                doc = (Element) docs.item(i);
-                               type = doc.getAttribute("type");
+                               type = doc.getAttribute(TYPE_ATTR);
                                if (!type.isEmpty()) {
                                        // Read file formats for the document type
                                        imports = doc.getElementsByTagName("import");
@@ -464,12 +528,50 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                                                }
                                        }
                                        if (!formats.isEmpty()) {
-                                               res.put(type, formats);
+                                               _mapimport.put(type, formats);
                                        }
                                }
                        }
                }
-               return res;
+       }
+
+       /**
+        * Load default document types from XML configuration.
+        * 
+        * @param children
+        *            XML nodes
+        */
+       private void loadDefaultDocTypes(final Map<String, Node> children) {
+               _defdoctype.clear();
+               Node child = children.get("default-doctypes");
+               NodeList nlist = child.getChildNodes();
+
+               List<DocumentType> listype = getDocumentTypeService().selectAllTypes();
+               Map<String, DocumentType> maptype = new HashMap<String, DocumentType>();
+               for (Iterator<DocumentType> i = listype.iterator(); i.hasNext();) {
+                       DocumentType type = i.next();
+                       maptype.put(type.getName(), type);
+               }
+               for (int i = 0; i < nlist.getLength(); i++) {
+                       child = nlist.item(i);
+                       if (!child.getNodeName().equals("step")) {
+                               continue;
+                       }
+
+                       String nstep = child.getAttributes().getNamedItem("number")
+                                       .getNodeValue();
+                       NodeList map = child.getChildNodes();
+                       for (int j = 0; j < map.getLength(); j++) {
+                               child = map.item(j);
+                               if (!child.getNodeName().equals("mapping")) {
+                                       continue;
+                               }
+                               NamedNodeMap natr = child.getAttributes();
+                               String dext = natr.getNamedItem("extension").getNodeValue();
+                               String type = natr.getNamedItem(TYPE_ATTR).getNodeValue();
+                               _defdoctype.put(nstep + "." + dext, maptype.get(type));
+                       }
+               }
        }
 
        /**
@@ -550,12 +652,11 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
         *            XML nodes
         * @param resultype
         *            list of result types
-        * @return return list of validation cycles
         */
-       private List<ProjectSettingsValidationCycle> loadValidationCycles(
-                       final Map<String, Node> children, final List<String> resultype) {
+       private void loadValidationCycles(final Map<String, Node> children,
+                       final List<String> resultype) {
+               _concycles.clear();
                Node child = children.get("validations");
-               List<ProjectSettingsValidationCycle> cycles = new ArrayList<ProjectSettingsValidationCycle>();
                Map<String, Node> datag = XDOM.getNamedChildNodes(child);
                NamedNodeMap natr;
 
@@ -574,11 +675,10 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                                                actor[j] = Actor.valueOf(child.getNodeValue()); // Validation step is required
                                        }
                                }
-                               cycles.add(new ProjectSettingsValidationCycle(name, actor));
+                               _concycles.add(new ProjectSettingsValidationCycle(name, actor));
                        }
                }
-               cycles.add(new ProjectSettingsValidationCycle()); // Adds the built-in validation cycle
-               return cycles;
+               _concycles.add(new ProjectSettingsValidationCycle()); // Adds the built-in validation cycle
        }
 
        /**
@@ -599,7 +699,7 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                for (int i = 0; i < nlist.getLength(); i++) {
                        child = nlist.item(i);
                        if (child.getNodeName().equals("article")) {
-                               articles.add(child.getAttributes().getNamedItem("type")
+                               articles.add(child.getAttributes().getNamedItem(TYPE_ATTR)
                                                .getNodeValue());
                        }
                }