Salome HOME
Loading of mappings of document types to lists of file formats is implemented. Projec...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / technical / ProjectSettingsServiceImpl.java
index d8da9723f442678c5e58fa4279b058051b7ef128..49f84de4a7dcbc795925dfddb64ec6ab5655eb71 100644 (file)
@@ -78,6 +78,10 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
         * Configuration document validation cycles.
         */
        private transient List<ProjectSettingsValidationCycle> _concycles;
+       /**
+        * Document type mappings to file formats which should be imported into SALOME during check-out.
+        */
+       private transient Map<String, List<String>> _mapimport;
 
        // Temporary attributes initialized from the configuration file for populating the database with object types
        /**
@@ -300,6 +304,20 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                return result;
        }
 
+       /**
+        * Check if a file of the given format should be imported during check-in of a document of the given type.
+        * 
+        * @param type
+        *            document type
+        * @param format
+        *            file format
+        * @return true if file should be imported
+        */
+       public boolean doImport(final String type, final String format) {
+               return (_mapimport.containsKey(type) && _mapimport.get(type).contains(
+                               format));
+       }
+
        /**
         * Initialize the database: create all necessary default staff defined in the configuration file.
         */
@@ -379,6 +397,8 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                        // Validations tag
                        _concycles = loadValidationCycles(children, resultype);
 
+                       _mapimport = loadFormatMappings(children);
+
                        if (!getDatabase().getCheckedDB().isInitialized()) {
                                // Load object type definitions
                                // Documents tag
@@ -411,6 +431,47 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                }
        }
 
+       /**
+        * Load mappings of document types to lists of importable file formats.
+        * 
+        * @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>>();
+               Element maps = (Element) children.get("mappings");
+               Element doc, imp;
+               String type, format;
+               List<String> formats;
+               NodeList docs, imports;
+               if (maps != null) {
+                       // Read document types
+                       docs = maps.getElementsByTagName("document");
+                       for (int i = 0; i < docs.getLength(); i++) {
+                               doc = (Element) docs.item(i);
+                               type = doc.getAttribute("type");
+                               if (!type.isEmpty()) {
+                                       // Read file formats for the document type
+                                       imports = doc.getElementsByTagName("import");
+                                       formats = new ArrayList<String>();
+                                       for (int j = 0; j < imports.getLength(); j++) {
+                                               imp = (Element) imports.item(j);
+                                               format = imp.getAttribute("format");
+                                               if (!format.isEmpty()) {
+                                                       formats.add(format);
+                                               }
+                                       }
+                                       if (!formats.isEmpty()) {
+                                               res.put(type, formats);
+                                       }
+                               }
+                       }
+               }
+               return res;
+       }
+
        /**
         * Load a step from the given XML node. Return the next step's number.
         * 
@@ -434,12 +495,14 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                        final List<String> resultype) {
                int res = snum;
                if ("step".equals(node.getNodeName())) {
+
+                       String name = ((Element) node).getAttribute("name");
                        HashMap<String, Node> tags = XDOM.getNamedChildNodes(node);
 
                        NamedNodeMap natr = tags.get("storage").getAttributes();
                        ProjectSettingsService.Step step = new ProjectSettingsService.Step(
-                                       snum, ownerClass, natr.getNamedItem("path")
-                                                       .getNodeValue());
+                                       snum, ownerClass, natr.getNamedItem("path").getNodeValue());
+                       step.setKey(name);
 
                        // Keeping flow and classification information for eventual later use
                        natr = tags.get("flow").getAttributes();
@@ -459,7 +522,8 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                        if (natr.getNamedItem("contents").getNodeValue()
                                        .equals("knowledge")) {
                                if (Study.class.equals(ownerClass)) {
-                                       LOG.error("Error: knowledges must be attached to scenarios.");
+                                       LOG
+                                                       .error("Error: knowledges must be attached to scenarios.");
                                } else {
                                        // TODO In a given scenario, only one step must contain knowledges
                                        step._contents.add(KnowledgeElement.class);
@@ -467,8 +531,8 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                        } else {
                                step._contents.add(Document.class);
                        }
-                       
-                       Element module = (Element)tags.get("module");
+
+                       Element module = (Element) tags.get("module");
                        if (module != null) {
                                step.setModule(module.getAttribute("name"));
                        }
@@ -546,6 +610,9 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
         * Create in the database document types defined in the custom configuration.
         */
        private void createDocumentTypes() {
+               if (LOG.isDebugEnabled()) {
+                       LOG.debug("Creating documents types...");
+               }
                DocumentType.Properties tprop = new DocumentType.Properties();
                Map<String, List<ProjectSettingsService.Step>> mapsteps = new HashMap<String, List<ProjectSettingsService.Step>>();
                Map<String, ProjectSettingsService.Step> mapresult = new HashMap<String, ProjectSettingsService.Step>();
@@ -585,33 +652,39 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                        for (Iterator<String> i = tset.iterator(); i.hasNext();) {
                                type = i.next();
                                slist = mapsteps.get(type);
-                               uses = _mapuse.get(type);
-                               step = mapresult.get(type);
-
-                               tprop.clear();
-                               tprop.setName(type).setStep(
-                                               slist.toArray(new ProjectSettingsService.Step[slist
-                                                               .size()]));
-                               if (uses != null) {
-                                       tdoc = maptype.get(uses);
-                                       if (tdoc == null) {
-                                               LOG.warn("Undefined \"" + uses + "\" document type.");
-                                       } else {
-                                               tprop.setUses(tdoc);
+                               if (slist != null) {
+                                       uses = _mapuse.get(type);
+                                       step = mapresult.get(type);
+
+                                       tprop.clear();
+                                       tprop.setName(type).setStep(
+                                                       slist.toArray(new ProjectSettingsService.Step[slist
+                                                                       .size()]));
+                                       if (uses != null) {
+                                               tdoc = maptype.get(uses);
+                                               if (tdoc == null) {
+                                                       LOG.warn("Undefined \"" + uses
+                                                                       + "\" document type.");
+                                               } else {
+                                                       tprop.setUses(tdoc);
+                                               }
+                                       }
+                                       if (step != null) {
+                                               tprop.setResult(step);
                                        }
-                               }
-                               if (step != null) {
-                                       tprop.setResult(step);
-                               }
 
-                               tprop.disableCheck();
-                               tdoc = getDocumentTypeService().createType(tprop); // Creation of Document Types
-                               getDocumentTypeService().approve(tdoc);
-                               maptype.put(type, tdoc);
+                                       tprop.disableCheck();
+                                       tdoc = getDocumentTypeService().createType(tprop); // Creation of Document Types
+                                       getDocumentTypeService().approve(tdoc);
+                                       maptype.put(type, tdoc);
+                               }
                        }
                } catch (Exception error) {
                        LOG.warn("Error creating document types, reason:", error); // Should not happen
                }
+               if (LOG.isDebugEnabled()) {
+                       LOG.debug("Documents types are created: " + maptype.size());
+               }
        }
 
        /**
@@ -641,30 +714,28 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                int snum = 0;
                for (Iterator<NamedNodeMap> i = _sclass.iterator(); i.hasNext(); snum++) {
                        NamedNodeMap clatr = i.next();
-                       if (clatr == null) {
-                               continue;
-                       }
-
-                       String[] clist = clatr.getNamedItem("context").getNodeValue()
-                                       .split(",");
-                       for (int j = 0; j < clist.length; j++) {
-                               mapstep.put(clist[j], _steps.get(snum));
+                       if (clatr != null) {
+                               String[] clist = clatr.getNamedItem("context").getNodeValue()
+                                               .split(",");
+                               for (int j = 0; j < clist.length; j++) {
+                                       mapstep.put(clist[j], _steps.get(snum));
+                               }
                        }
                }
                try {
                        SimulationContextType tctex = null;
                        for (Iterator<String> i = _context.iterator(); i.hasNext();) {
                                String type = i.next();
-                               if (!mapstep.containsKey(type)) {
+                               if (mapstep.containsKey(type)) {
+                                       tctex = getSimulationContextTypeService().createType(type,
+                                                       mapstep.get(type)); // Creation of Simulation Context Types
+                                       getSimulationContextTypeService().approve(tctex);
+                               } else {
                                        LOG
                                                        .warn("Could not find \""
                                                                        + type
                                                                        + "\" classification. Simulation Context type ignored.");
-                                       continue;
                                }
-                               tctex = getSimulationContextTypeService().createType(type,
-                                               mapstep.get(type)); // Creation of Simulation Context Types
-                               getSimulationContextTypeService().approve(tctex);
                        }
                } catch (Exception error) {
                        LOG.warn("Error creating context types, reason:", error); // Should not happen