Salome HOME
ScenarioService.getScenarioInfo method is added.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / technical / ProjectSettingsServiceImpl.java
index 14e6e66d58e7a01d791636fe656a70450573b36e..cb7c193b2e1f7a435a4fb97a871c108a686819d5 100644 (file)
@@ -37,12 +37,13 @@ import org.splat.manox.XDOM;
 import org.splat.service.DocumentTypeService;
 import org.splat.service.KnowledgeElementTypeService;
 import org.splat.service.SimulationContextTypeService;
+import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 /**
- * SIMAN configuration data service.
+ * SIMAN workflow configuration data service.
  */
 public class ProjectSettingsServiceImpl implements ProjectSettingsService {
 
@@ -129,11 +130,11 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                /**
                 * Cycle (document) type name.
                 */
-               private final String _name;
+               private transient final String _name;
                /**
                 * Array of cycle actors positions in the organization. TODO: Must be replaced by Roles.
                 */
-               private final Actor[] _actor;
+               private transient final Actor[] _actor;
 
                /**
                 * Default constructor.
@@ -364,166 +365,184 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                        int snum = 1; // Base number of steps
                        for (int i = 0; i < nlist.getLength(); i++) {
                                child = nlist.item(i);
-                               if (child.getNodeName().equals("scenario")) {
+                               if ("scenario".equals(child.getNodeName())) {
                                        NodeList slist = child.getChildNodes();
                                        for (int j = 0; j < slist.getLength(); j++) {
-                                               child = slist.item(j);
-                                               if (!child.getNodeName().equals("step")) {
-                                                       continue;
-                                               }
-                                               HashMap<String, Node> tags = XDOM
-                                                               .getNamedChildNodes(child);
-
-                                               natr = tags.get("storage").getAttributes();
-                                               ProjectSettingsService.Step step = new ProjectSettingsService.Step(
-                                                               snum, Scenario.class, natr.getNamedItem("path")
-                                                                               .getNodeValue());
-
-                                               // Keeping flow and classification information for eventual later use
-                                               natr = tags.get("flow").getAttributes();
-                                               flist.add(natr);
-                                               child = natr.getNamedItem("result");
-                                               if (child != null) {
-                                                       resultype.add(child.getNodeValue());
-                                               }
-
-                                               child = tags.get("classification");
-                                               if (child == null) {
-                                                       clist.add(null);
-                                               } else {
-                                                       clist.add(child.getAttributes());
-                                               }
-
-                                               if (natr.getNamedItem("contents").getNodeValue()
-                                                               .equals("knowledge")) {
-                                                       // TODO In a given scenario, only one step must contain knowledges
-                                                       step._contents.add(KnowledgeElement.class);
-                                               } else {
-                                                       step._contents.add(Document.class);
-                                               }
-                                               _steps.add(step);
-                                               snum += 1;
+                                               snum = loadStep(slist.item(j), Scenario.class, snum,
+                                                               flist, clist, resultype);
                                        }
                                } else {
-                                       if (!child.getNodeName().equals("step")) {
-                                               continue;
-                                       }
-                                       HashMap<String, Node> tags = XDOM.getNamedChildNodes(child);
-
-                                       natr = tags.get("storage").getAttributes(); // Mandatory information
-                                       ProjectSettingsService.Step step = new ProjectSettingsService.Step(
-                                                       snum, Study.class, natr.getNamedItem("path")
-                                                                       .getNodeValue());
-
-                                       // Keeping flow and classification information for eventual later use
-                                       natr = tags.get("flow").getAttributes();
-                                       flist.add(natr);
-                                       child = natr.getNamedItem("result");
-                                       if (child != null) {
-                                               resultype.add(child.getNodeValue());
-                                       }
-
-                                       child = tags.get("classification"); // Optional information
-                                       if (child == null) {
-                                               clist.add(null);
-                                       } else {
-                                               clist.add(child.getAttributes());
-                                       }
-
-                                       if (natr.getNamedItem("contents").getNodeValue().equals(
-                                                       "knowledge")) {
-                                               // TODO Error: knowledges must be attached to scenarios
-                                               LOG.error("Error: knowledges must be attached to scenarios.");
-                                       } else {
-                                               step._contents.add(Document.class);
-                                       }
-                                       _steps.add(step);
-                                       snum += 1;
+                                       snum = loadStep(child, Study.class, snum, flist, clist,
+                                                       resultype);
                                }
                        }
                        // Validations tag
-                       child = children.get("validations");
-                       _concycles = new ArrayList<ProjectSettingsValidationCycle>();
-                       datag = XDOM.getNamedChildNodes(child);
-
-                       String[] step = { "review", "approval", "acceptance" };
-                       resultype.add("default");
-                       for (Iterator<String> i = resultype.iterator(); i.hasNext();) {
-                               Actor[] actor = { null, null, null };
-                               String name = i.next();
-                               child = datag.get(name);
-                               if (child == null) {
-                                       continue; // Document type not subject of any validation
-                               }
-                               natr = child.getAttributes();
-                               for (int j = 0; j < step.length; j++) {
-                                       child = natr.getNamedItem(step[j]);
-                                       if (child == null) {
-                                               continue; // Validation step not required
+                       _concycles = loadValidationCycles(children, resultype);
+
+                       if (!getDatabase().getCheckedDB().isInitialized()) {
+                               // Load object type definitions
+                               // Documents tag
+                               child = children.get("documents");
+                               nlist = child.getChildNodes();
+
+                               _flows = flist; // Kept for later use in document type definition
+                               _sclass = clist; // Kept for later use in simulation context type definition
+                               _mapuse = new LinkedHashMap<String, String>();
+                               for (int i = 0; i < nlist.getLength(); i++) {
+                                       child = nlist.item(i);
+                                       if ("article".equals(child.getNodeName())) {
+                                               natr = child.getAttributes();
+                                               String type = natr.getNamedItem("type").getNodeValue();
+                                               String uses = null;
+                                               child = natr.getNamedItem("uses");
+                                               if (child != null) {
+                                                       uses = child.getNodeValue();
+                                               }
+                                               _mapuse.put(type, uses); // Must be added to the map even if no (null) uses
                                        }
-                                       actor[j] = Actor.valueOf(child.getNodeValue());
                                }
-                               _concycles.add(new ProjectSettingsValidationCycle(name, actor));
+                               // Simulation Contexts tag
+                               _context = loadArticles(children, "contexts");
+                               // Knowledge Elements tag
+                               _kname = loadArticles(children, "knowledges");
                        }
-                       _concycles.add(new ProjectSettingsValidationCycle()); // Adds the built-in validation cycle
+               } catch (Exception error) {
+                       LOG.info("Error in customization", error);
+               }
+       }
 
-                       if (getDatabase().getCheckedDB().isInitialized()) {
-                               return; // No need to load object type definitions as they are already stored
+       /**
+        * Load a step from the given XML node. Return the next step's number.
+        * 
+        * @param node
+        *            XML node to parse
+        * @param ownerClass
+        *            the class of a step's owner project element - study or scenario
+        * @param snum
+        *            step's number
+        * @param flist
+        *            list of flows
+        * @param clist
+        *            list of classifications
+        * @param resultype
+        *            list of flow results
+        * @return the next step's number
+        */
+       private int loadStep(final Node node,
+                       final Class<? extends ProjectElement> ownerClass, final int snum,
+                       final List<NamedNodeMap> flist, final List<NamedNodeMap> clist,
+                       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());
+                       step.setKey(name);
+
+                       // Keeping flow and classification information for eventual later use
+                       natr = tags.get("flow").getAttributes();
+                       flist.add(natr);
+                       Node child = natr.getNamedItem("result");
+                       if (child != null) {
+                               resultype.add(child.getNodeValue());
                        }
 
-                       // Documents tag
-                       child = children.get("documents");
-                       nlist = child.getChildNodes();
-
-                       _flows = flist; // Kept for later use in document type definition
-                       _sclass = clist; // Kept for later use in simulation context type definition
-                       _mapuse = new LinkedHashMap<String, String>();
-                       for (int i = 0; i < nlist.getLength(); i++) {
-                               child = nlist.item(i);
-                               if (!child.getNodeName().equals("article")) {
-                                       continue;
-                               }
-
-                               natr = child.getAttributes();
-                               String type = natr.getNamedItem("type").getNodeValue();
-                               String uses = null;
-                               child = natr.getNamedItem("uses");
-                               if (child != null) {
-                                       uses = child.getNodeValue();
-                               }
-                               _mapuse.put(type, uses); // Must be added to the map even if no (null) uses
+                       child = tags.get("classification");
+                       if (child == null) {
+                               clist.add(null);
+                       } else {
+                               clist.add(child.getAttributes());
                        }
-                       // Simulation Contexts tag
-                       child = children.get("contexts");
-                       nlist = child.getChildNodes();
 
-                       _context = new ArrayList<String>();
-                       for (int i = 0; i < nlist.getLength(); i++) {
-                               child = nlist.item(i);
-                               if (!child.getNodeName().equals("article")) {
-                                       continue;
+                       if (natr.getNamedItem("contents").getNodeValue()
+                                       .equals("knowledge")) {
+                               if (Study.class.equals(ownerClass)) {
+                                       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);
                                }
+                       } else {
+                               step._contents.add(Document.class);
+                       }
 
-                               _context.add(child.getAttributes().getNamedItem("type")
-                                               .getNodeValue());
+                       Element module = (Element) tags.get("module");
+                       if (module != null) {
+                               step.setModule(module.getAttribute("name"));
                        }
-                       // Knowledge Elements tag
-                       child = children.get("knowledges");
-                       nlist = child.getChildNodes();
 
-                       _kname = new ArrayList<String>();
-                       for (int i = 0; i < nlist.getLength(); i++) {
-                               child = nlist.item(i);
-                               if (!child.getNodeName().equals("article")) {
-                                       continue;
+                       _steps.add(step);
+                       res += 1;
+               }
+               return res;
+       }
+
+       /**
+        * Get custom validation cycles.
+        * 
+        * @param children
+        *            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) {
+               Node child = children.get("validations");
+               List<ProjectSettingsValidationCycle> cycles = new ArrayList<ProjectSettingsValidationCycle>();
+               Map<String, Node> datag = XDOM.getNamedChildNodes(child);
+               NamedNodeMap natr;
+
+               String[] step = { "review", "approval", "acceptance" };
+               resultype.add("default");
+               for (Iterator<String> i = resultype.iterator(); i.hasNext();) {
+                       Actor[] actor = { null, null, null };
+                       String name = i.next();
+                       child = datag.get(name);
+                       if (child != null) {
+                               // Document type is the subject of a validation
+                               natr = child.getAttributes();
+                               for (int j = 0; j < step.length; j++) {
+                                       child = natr.getNamedItem(step[j]);
+                                       if (child != null) {
+                                               actor[j] = Actor.valueOf(child.getNodeValue()); // Validation step is required
+                                       }
                                }
+                               cycles.add(new ProjectSettingsValidationCycle(name, actor));
+                       }
+               }
+               cycles.add(new ProjectSettingsValidationCycle()); // Adds the built-in validation cycle
+               return cycles;
+       }
 
-                               _kname.add(child.getAttributes().getNamedItem("type")
+       /**
+        * Read list of articles types.
+        * 
+        * @param children
+        *            XML nodes containing articles
+        * @param listName
+        *            the name of the list of articles
+        * @return list of articles types
+        */
+       private List<String> loadArticles(final Map<String, Node> children,
+                       final String listName) {
+               Node child = children.get(listName);
+               NodeList nlist = child.getChildNodes();
+
+               List<String> articles = new ArrayList<String>();
+               for (int i = 0; i < nlist.getLength(); i++) {
+                       child = nlist.item(i);
+                       if (child.getNodeName().equals("article")) {
+                               articles.add(child.getAttributes().getNamedItem("type")
                                                .getNodeValue());
                        }
-               } catch (Exception error) {
-                       LOG.info("Error in customization", error);
                }
+               return articles;
        }
 
        /**
@@ -579,9 +598,7 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService {
                                if (uses != null) {
                                        tdoc = maptype.get(uses);
                                        if (tdoc == null) {
-                                               LOG
-                                                               .warn("Undefined \"" + uses
-                                                                               + "\" document type.");
+                                               LOG.warn("Undefined \"" + uses + "\" document type.");
                                        } else {
                                                tprop.setUses(tdoc);
                                        }
@@ -627,30 +644,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