]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsServiceImpl.java
Salome HOME
The checkin from SALOME operation is updated (ScenarioService.checkin()). Versioning...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / technical / ProjectSettingsServiceImpl.java
1 package org.splat.service.technical;
2
3 /**
4  * 
5  * @author    Daniel Brunier-Coulin
6  * @copyright OPEN CASCADE 2012
7  */
8
9 import java.io.File;
10 import java.io.FileNotFoundException;
11 import java.io.IOException;
12 import java.sql.SQLException;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.LinkedHashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Properties;
20 import java.util.Set;
21
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24
25 import org.apache.log4j.Logger;
26 import org.splat.dal.bo.som.Document;
27 import org.splat.dal.bo.som.DocumentType;
28 import org.splat.dal.bo.som.KnowledgeElement;
29 import org.splat.dal.bo.som.KnowledgeElementType;
30 import org.splat.dal.bo.som.ProjectElement;
31 import org.splat.dal.bo.som.Scenario;
32 import org.splat.dal.bo.som.SimulationContextType;
33 import org.splat.dal.bo.som.Study;
34 import org.splat.dal.bo.som.ValidationCycle.Actor;
35 import org.splat.dal.dao.som.Database;
36 import org.splat.manox.XDOM;
37 import org.splat.service.DocumentTypeService;
38 import org.splat.service.KnowledgeElementTypeService;
39 import org.splat.service.SimulationContextTypeService;
40 import org.w3c.dom.Element;
41 import org.w3c.dom.NamedNodeMap;
42 import org.w3c.dom.Node;
43 import org.w3c.dom.NodeList;
44
45 /**
46  * SIMAN workflow configuration data service.
47  */
48 public class ProjectSettingsServiceImpl implements ProjectSettingsService {
49
50         /**
51          * The logger for the service.
52          */
53         protected final static Logger LOG = Logger
54                         .getLogger(ProjectSettingsServiceImpl.class);
55
56         /**
57          * Type attribute name.
58          */
59         private final static String TYPE_ATTR = "type";
60
61         // Non persistent configuration information
62         /**
63          * Repository settings.
64          */
65         private transient final Properties _reprop = new Properties();
66         /**
67          * Pattern of study references.
68          */
69         private transient String _pattern;
70         /**
71          * Scheme of file names stored into the repository.
72          */
73         private transient FileNaming _naming;
74         /**
75          * Pattern of the presentation of version numbers.
76          */
77         private transient String _versioning;
78         /**
79          * Ordered list of (transient) study steps.
80          */
81         private transient final List<ProjectSettingsService.Step> _steps = new ArrayList<ProjectSettingsService.Step>();
82         /**
83          * Configuration document validation cycles.
84          */
85         private transient final List<ProjectSettingsValidationCycle> _concycles = new ArrayList<ProjectSettingsValidationCycle>();
86         /**
87          * Document type mappings to file formats which should be imported into SALOME during check-out.
88          */
89         private transient final Map<String, List<String>> _mapimport = new HashMap<String, List<String>>();
90         /**
91          * Default document types structured by step.formats.
92          */
93         private transient final Map<String, DocumentType> _defdoctype = new LinkedHashMap<String, DocumentType>();
94
95         // Temporary attributes initialized from the configuration file for populating the database with object types
96         /**
97          * Step.format keys structured by Default document types names. This map is used for loading config before document types are created in
98          * the database. When document types are created then _defdoctype is filled.
99          */
100         private transient final Map<String, List<String>> _defdoctypeKeys = new HashMap<String, List<String>>();
101
102         /**
103          * Document type names and uses mapping.
104          */
105         private transient Map<String, String> _mapuse;
106         /**
107          * Simulation Context type names.
108          */
109         private transient List<String> _context;
110         /**
111          * Knowledge Element type names.
112          */
113         private transient List<String> _kname;
114         /**
115          * Document flows.
116          */
117         private transient List<NamedNodeMap> _flows;
118         /**
119          * Study classifications.
120          */
121         private transient List<NamedNodeMap> _sclass;
122
123         // Other resources
124         /**
125          * Database service to check its version, etc.
126          */
127         private Database _database;
128         /**
129          * Injected simulation context type service.
130          */
131         private SimulationContextTypeService _simulationContextTypeService;
132         /**
133          * Injected knowledge element type service.
134          */
135         private KnowledgeElementTypeService _knowledgeElementTypeService;
136         /**
137          * Injected document type service.
138          */
139         private DocumentTypeService _documentTypeService;
140
141         /**
142          * File naming strategy enumeration.
143          */
144         public enum FileNaming {
145                 /**
146                  * Name by document title.
147                  */
148                 title,
149                 /**
150                  * Generate encoded name.
151                  */
152                 encoded,
153                 /**
154                  * Keep original file name.
155                  */
156                 asis
157         }
158
159         /**
160          * Validation cycle defined in the XML configuration.
161          */
162         public static class ProjectSettingsValidationCycle {
163                 /**
164                  * Cycle (document) type name.
165                  */
166                 private transient final String _name;
167                 /**
168                  * Array of cycle actors positions in the organization. TODO: Must be replaced by Roles.
169                  */
170                 private transient final Actor[] _actor;
171
172                 /**
173                  * Default constructor.
174                  */
175                 private ProjectSettingsValidationCycle() {
176                         this._name = "built-in";
177                         this._actor = new Actor[] { null, null, null };
178                 }
179
180                 /**
181                  * Create a validation cycle definition for the given document type name and actors positions.
182                  * 
183                  * @param name
184                  *            the document type name
185                  * @param actor
186                  *            the array of actors positions
187                  */
188                 private ProjectSettingsValidationCycle(final String name,
189                                 final Actor[] actor) {
190                         this._name = name;
191                         this._actor = actor;
192                 }
193
194                 /**
195                  * The processed document type name.
196                  * 
197                  * @return the document type name
198                  */
199                 public String getName() {
200                         return _name;
201                 }
202
203                 /**
204                  * Get an array of cycle actors positions.
205                  * 
206                  * @return the array of actors positions
207                  * @see org.splat.dal.bo.som.ValidationCycle.Actor
208                  */
209                 public Actor[] getActorTypes() {
210                         return _actor;
211                 }
212         }
213
214         // ==============================================================================================================================
215         // Public functions
216         // ==============================================================================================================================
217
218         /**
219          * Load workflow configuration from the given file. <br/> Create necessary default staff in the database if it is not initialized yet.
220          * 
221          * @param filename
222          *            the workflow configuration file
223          * @throws IOException
224          *             if there is a file reading or index creation problem
225          * @throws SQLException
226          *             if there is a database population problem
227          */
228         public void configure(final String filename) throws IOException,
229                         SQLException {
230                 if (!_steps.isEmpty()) {
231                         return; // Project already configured
232                 }
233
234                 Database base = getDatabase().getCheckedDB();
235                 File config = new File(filename);
236                 if (config.exists()) {
237                         loadCustomization(config);
238                 } else {
239                         LOG.fatal("Could not find the database configuration file \""
240                                         + config.getAbsolutePath() + "\"");
241                         throw new FileNotFoundException();
242                 }
243                 base.configure(_reprop);
244                 if (!base.isInitialized()) {
245                         base.initialize();
246                         initialize(); // Populates the database with all necessary stuff
247                 }
248         }
249
250         /**
251          * Get ordered list of (transient) study steps.
252          * 
253          * @return the list of steps from project settings
254          */
255         public List<ProjectSettingsService.Step> getAllSteps() {
256                 return _steps;
257         }
258
259         /**
260          * Return the validation cycles of result documents defined in the workflow, ordered by study activities and ending by the default
261          * validation cycle, if defined.
262          * 
263          * @return the validation cycles of the workflow
264          */
265         public List<ProjectSettingsValidationCycle> getAllValidationCycles() {
266                 return _concycles;
267         }
268
269         /**
270          * Get file naming scheme setting.
271          * 
272          * @return file naming scheme
273          * @see org.splat.service.technical.ProjectSettingsServiceImpl.FileNaming
274          */
275         public FileNaming getFileNamingScheme() {
276                 return _naming;
277         }
278
279         /**
280          * Get a pattern of study references.
281          * 
282          * @return the reference pattern
283          */
284         public String getReferencePattern() {
285                 return _pattern;
286         }
287
288         /**
289          * Get a pattern of the presentation of version numbers.
290          * 
291          * @return the version numbers presentation pattern
292          */
293         public String getRevisionPattern() {
294                 return _versioning;
295         }
296
297         /**
298          * Get a study step by its sequential number.
299          * 
300          * @param number
301          *            the step number
302          * @return the step
303          */
304         public ProjectSettingsService.Step getStep(final int number) {
305                 ProjectSettingsService.Step res = null;
306                 for (int i = 0; i < _steps.size(); i++) {
307                         ProjectSettingsService.Step step = _steps.get(i);
308                         if (step.getNumber() == number) {
309                                 res = step;
310                                 break;
311                         }
312                 }
313                 return res;
314         }
315
316         /**
317          * Get steps of the given project element (study or scenario).
318          * 
319          * @param level
320          *            the project element (study or scenario)
321          * @return the list of steps
322          */
323         public List<ProjectSettingsService.Step> getStepsOf(
324                         final Class<? extends ProjectElement> level) {
325                 List<ProjectSettingsService.Step> result = new ArrayList<ProjectSettingsService.Step>();
326
327                 for (int i = 0; i < _steps.size(); i++) {
328                         ProjectSettingsService.Step step = _steps.get(i);
329                         if (step.appliesTo(level)) {
330                                 result.add(step);
331                         }
332                 }
333                 return result;
334         }
335
336         /**
337          * Check if a file of the given format should be imported during check-in of a document of the given type.
338          * 
339          * @param type
340          *            document type
341          * @param format
342          *            file format
343          * @return true if file should be imported
344          */
345         public boolean doImport(final String type, final String format) {
346                 return (_mapimport.containsKey(type) && _mapimport.get(type).contains(
347                                 format));
348         }
349
350         /**
351          * Get default document type for the given file format on the given study step.
352          * 
353          * @param step
354          *            the study step
355          * @param format
356          *            the file format (extension)
357          * @return document type
358          */
359         public DocumentType getDefaultDocumentType(final Step step,
360                         final String format) {
361                 String[] table = format.split("\\x2E");
362                 if (LOG.isDebugEnabled()) {
363                         LOG.debug("Trying to get default type: " + step.getNumber() + "."
364                                         + table[table.length - 1]);
365                 }
366                 return _defdoctype
367                                 .get(step.getNumber() + "." + table[table.length - 1]); // May be null
368         }
369
370         /**
371          * Get the list of default formats for the given study step.
372          * 
373          * @param step
374          *            the study step
375          * @return list of formats (file extensions)
376          */
377         public List<String> getDefaultFormats(final Step step) {
378                 Integer stepNumber = step.getNumber();
379                 List<String> result = new ArrayList<String>();
380
381                 for (String i : _defdoctype.keySet()) {
382                         String[] key = i.split("\\x2E");
383                         // PDF is not an authoring format
384                         if (stepNumber.equals(Integer.valueOf(key[0]))
385                                         && (!key[1].equals("pdf"))) {
386                                 result.add(key[1]); // Formats are unique
387                         }
388                 }
389                 return result;
390         }
391
392         /**
393          * Initialize the database: create all necessary default staff defined in the configuration file.
394          */
395         protected void initialize() {
396                 createDocumentTypes();
397                 createSimulationContextTypes();
398                 createKnowledgeElementTypes();
399         }
400
401         // ==============================================================================================================================
402         // Private member function
403         // ==============================================================================================================================
404
405         /**
406          * Read the configuration file and fill transient project settings fields.
407          * 
408          * @param config
409          *            the configuration XML file
410          */
411         private void loadCustomization(final File config) {
412                 try {
413                         DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory
414                                         .newInstance();
415                         DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
416
417                         org.w3c.dom.Document conf = dBuilder.parse(config.getPath());
418                         HashMap<String, Node> children = XDOM.getNamedChildNodes(conf
419                                         .getDocumentElement());
420
421                         // Repository tag initializing the reprop attribute
422                         Node child = children.get("database");
423                         HashMap<String, Node> datag = XDOM.getNamedChildNodes(child);
424
425                         String disk = datag.get("repository").getAttributes().getNamedItem(
426                                         "disk").getNodeValue();
427                         if (!disk.endsWith("/")) {
428                                 disk = disk + "/";
429                         }
430                         LOG.info("Database root set to " + disk);
431                         _reprop.setProperty("repository", disk);
432
433                         // Formats tag initializing the reference pattern and date attributes
434                         child = children.get("formats");
435                         datag = XDOM.getNamedChildNodes(child);
436
437                         NamedNodeMap natr = datag.get("references").getAttributes();
438                         _pattern = natr.getNamedItem("study").getNodeValue();
439
440                         natr = datag.get("files").getAttributes();
441                         _naming = FileNaming.valueOf(natr.getNamedItem("name")
442                                         .getNodeValue());
443
444                         natr = datag.get("versions").getAttributes();
445                         _versioning = natr.getNamedItem("pattern").getNodeValue();
446
447                         // Activities tag initializing the steps and rex attributes
448                         child = children.get("activities");
449                         NodeList nlist = child.getChildNodes();
450                         List<NamedNodeMap> flist = new ArrayList<NamedNodeMap>();
451                         List<String> resultype = new ArrayList<String>();
452                         List<NamedNodeMap> clist = new ArrayList<NamedNodeMap>();
453
454                         int snum = 1; // Base number of steps
455                         for (int i = 0; i < nlist.getLength(); i++) {
456                                 child = nlist.item(i);
457                                 if ("scenario".equals(child.getNodeName())) {
458                                         NodeList slist = child.getChildNodes();
459                                         for (int j = 0; j < slist.getLength(); j++) {
460                                                 snum = loadStep(slist.item(j), Scenario.class, snum,
461                                                                 flist, clist, resultype);
462                                         }
463                                 } else {
464                                         snum = loadStep(child, Study.class, snum, flist, clist,
465                                                         resultype);
466                                 }
467                         }
468
469                         // Validations tag
470                         loadValidationCycles(children, resultype);
471                         // Load steps result document types mapped to file formats
472                         loadFormatMappings(children);
473                         // Load default mapping of file formats to document types for each step
474                         loadDefaultDocTypes(children);
475
476                         if (!getDatabase().getCheckedDB().isInitialized()) {
477                                 // Load object type definitions
478                                 // Documents tag
479                                 child = children.get("documents");
480                                 nlist = child.getChildNodes();
481
482                                 _flows = flist; // Kept for later use in document type definition
483                                 _sclass = clist; // Kept for later use in simulation context type definition
484                                 _mapuse = new LinkedHashMap<String, String>();
485                                 for (int i = 0; i < nlist.getLength(); i++) {
486                                         child = nlist.item(i);
487                                         if ("article".equals(child.getNodeName())) {
488                                                 natr = child.getAttributes();
489                                                 String type = natr.getNamedItem(TYPE_ATTR)
490                                                                 .getNodeValue();
491                                                 String uses = null;
492                                                 child = natr.getNamedItem("uses");
493                                                 if (child != null) {
494                                                         uses = child.getNodeValue();
495                                                 }
496                                                 _mapuse.put(type, uses); // Must be added to the map even if no (null) uses
497                                         }
498                                 }
499                                 // Simulation Contexts tag
500                                 _context = loadArticles(children, "contexts");
501                                 // Knowledge Elements tag
502                                 _kname = loadArticles(children, "knowledges");
503                         }
504                 } catch (Exception error) {
505                         LOG.info("Error in customization", error);
506                 }
507         }
508
509         /**
510          * Load mappings of document types to lists of importable file formats.
511          * 
512          * @param children
513          *            XML nodes
514          */
515         private void loadFormatMappings(final Map<String, Node> children) {
516                 _mapimport.clear();
517                 Element maps = (Element) children.get("mappings");
518                 Element doc, imp;
519                 String type, format;
520                 List<String> formats;
521                 NodeList docs, imports;
522                 if (maps != null) {
523                         // Read document types
524                         docs = maps.getElementsByTagName("document");
525                         for (int i = 0; i < docs.getLength(); i++) {
526                                 doc = (Element) docs.item(i);
527                                 type = doc.getAttribute(TYPE_ATTR);
528                                 if (!type.isEmpty()) {
529                                         // Read file formats for the document type
530                                         imports = doc.getElementsByTagName("import");
531                                         formats = new ArrayList<String>();
532                                         for (int j = 0; j < imports.getLength(); j++) {
533                                                 imp = (Element) imports.item(j);
534                                                 format = imp.getAttribute("format");
535                                                 if (!format.isEmpty()) {
536                                                         formats.add(format);
537                                                 }
538                                         }
539                                         if (!formats.isEmpty()) {
540                                                 _mapimport.put(type, formats);
541                                         }
542                                 }
543                         }
544                 }
545         }
546
547         /**
548          * Load default document types from XML configuration.
549          * 
550          * @param children
551          *            XML nodes
552          */
553         private void loadDefaultDocTypes(final Map<String, Node> children) {
554                 _defdoctype.clear();
555                 _defdoctypeKeys.clear();
556                 Node child = children.get("default-doctypes");
557                 if (child != null) {
558                         NodeList nlist = child.getChildNodes();
559
560                         List<DocumentType> listype = getDocumentTypeService()
561                                         .selectAllTypes();
562                         Map<String, DocumentType> maptype = new HashMap<String, DocumentType>();
563                         for (Iterator<DocumentType> i = listype.iterator(); i.hasNext();) {
564                                 DocumentType type = i.next();
565                                 maptype.put(type.getName(), type);
566                         }
567                         for (int i = 0; i < nlist.getLength(); i++) {
568                                 child = nlist.item(i);
569                                 if (!child.getNodeName().equals("step")) {
570                                         continue;
571                                 }
572
573                                 String nstep = child.getAttributes().getNamedItem("number")
574                                                 .getNodeValue();
575                                 NodeList map = child.getChildNodes();
576                                 for (int j = 0; j < map.getLength(); j++) {
577                                         child = map.item(j);
578                                         if (!child.getNodeName().equals("mapping")) {
579                                                 continue;
580                                         }
581                                         NamedNodeMap natr = child.getAttributes();
582                                         String dext = natr.getNamedItem("extension").getNodeValue();
583                                         String type = natr.getNamedItem(TYPE_ATTR).getNodeValue();
584                                         if (LOG.isDebugEnabled()) {
585                                                 LOG.debug("Map default type: " + nstep + "." + dext
586                                                                 + ": (type name = " + type + ")"
587                                                                 + maptype.get(type));
588                                         }
589                                         _defdoctype.put(nstep + "." + dext, maptype.get(type));
590                                         // Remember the key if type is not created yet
591                                         if (maptype.get(type) == null) {
592                                                 List<String> keys;
593                                                 if (_defdoctypeKeys.containsKey(type)) {
594                                                         keys = _defdoctypeKeys.get(type);
595                                                 } else {
596                                                         keys = new ArrayList<String>();
597                                                         _defdoctypeKeys.put(type, keys);
598                                                 }
599                                                 keys.add(nstep + "." + dext);
600                                         }
601                                 }
602                         }
603                 }
604         }
605
606         /**
607          * Load a step from the given XML node. Return the next step's number.
608          * 
609          * @param node
610          *            XML node to parse
611          * @param ownerClass
612          *            the class of a step's owner project element - study or scenario
613          * @param snum
614          *            step's number
615          * @param flist
616          *            list of flows
617          * @param clist
618          *            list of classifications
619          * @param resultype
620          *            list of flow results
621          * @return the next step's number
622          */
623         private int loadStep(final Node node,
624                         final Class<? extends ProjectElement> ownerClass, final int snum,
625                         final List<NamedNodeMap> flist, final List<NamedNodeMap> clist,
626                         final List<String> resultype) {
627                 int res = snum;
628                 if ("step".equals(node.getNodeName())) {
629
630                         String name = ((Element) node).getAttribute("name");
631                         HashMap<String, Node> tags = XDOM.getNamedChildNodes(node);
632
633                         NamedNodeMap natr = tags.get("storage").getAttributes();
634                         ProjectSettingsService.Step step = new ProjectSettingsService.Step(
635                                         snum, ownerClass, natr.getNamedItem("path").getNodeValue());
636                         step.setKey(name);
637
638                         // Keeping flow and classification information for eventual later use
639                         natr = tags.get("flow").getAttributes();
640                         flist.add(natr);
641                         Node child = natr.getNamedItem("result");
642                         if (child != null) {
643                                 resultype.add(child.getNodeValue());
644                         }
645
646                         child = tags.get("classification");
647                         if (child == null) {
648                                 clist.add(null);
649                         } else {
650                                 clist.add(child.getAttributes());
651                         }
652
653                         if (natr.getNamedItem("contents").getNodeValue()
654                                         .equals("knowledge")) {
655                                 if (Study.class.equals(ownerClass)) {
656                                         LOG
657                                                         .error("Error: knowledges must be attached to scenarios.");
658                                 } else {
659                                         // TODO In a given scenario, only one step must contain knowledges
660                                         step._contents.add(KnowledgeElement.class);
661                                 }
662                         } else {
663                                 step._contents.add(Document.class);
664                         }
665
666                         Element module = (Element) tags.get("module");
667                         if (module != null) {
668                                 step.setModule(module.getAttribute("name"));
669                         }
670
671                         _steps.add(step);
672                         res += 1;
673                 }
674                 return res;
675         }
676
677         /**
678          * Get custom validation cycles.
679          * 
680          * @param children
681          *            XML nodes
682          * @param resultype
683          *            list of result types
684          */
685         private void loadValidationCycles(final Map<String, Node> children,
686                         final List<String> resultype) {
687                 _concycles.clear();
688                 Node child = children.get("validations");
689                 Map<String, Node> datag = XDOM.getNamedChildNodes(child);
690                 NamedNodeMap natr;
691
692                 String[] step = { "review", "approval", "acceptance" };
693                 resultype.add("default");
694                 for (Iterator<String> i = resultype.iterator(); i.hasNext();) {
695                         Actor[] actor = { null, null, null };
696                         String name = i.next();
697                         child = datag.get(name);
698                         if (child != null) {
699                                 // Document type is the subject of a validation
700                                 natr = child.getAttributes();
701                                 for (int j = 0; j < step.length; j++) {
702                                         child = natr.getNamedItem(step[j]);
703                                         if (child != null) {
704                                                 actor[j] = Actor.valueOf(child.getNodeValue()); // Validation step is required
705                                         }
706                                 }
707                                 _concycles.add(new ProjectSettingsValidationCycle(name, actor));
708                         }
709                 }
710                 _concycles.add(new ProjectSettingsValidationCycle()); // Adds the built-in validation cycle
711         }
712
713         /**
714          * Read list of articles types.
715          * 
716          * @param children
717          *            XML nodes containing articles
718          * @param listName
719          *            the name of the list of articles
720          * @return list of articles types
721          */
722         private List<String> loadArticles(final Map<String, Node> children,
723                         final String listName) {
724                 Node child = children.get(listName);
725                 NodeList nlist = child.getChildNodes();
726
727                 List<String> articles = new ArrayList<String>();
728                 for (int i = 0; i < nlist.getLength(); i++) {
729                         child = nlist.item(i);
730                         if (child.getNodeName().equals("article")) {
731                                 articles.add(child.getAttributes().getNamedItem(TYPE_ATTR)
732                                                 .getNodeValue());
733                         }
734                 }
735                 return articles;
736         }
737
738         /**
739          * Create in the database document types defined in the custom configuration.
740          */
741         private void createDocumentTypes() {
742                 if (LOG.isDebugEnabled()) {
743                         LOG.debug("Creating documents types...");
744                 }
745                 DocumentType.Properties tprop = new DocumentType.Properties();
746                 Map<String, List<ProjectSettingsService.Step>> mapsteps = new HashMap<String, List<ProjectSettingsService.Step>>();
747                 Map<String, ProjectSettingsService.Step> mapresult = new HashMap<String, ProjectSettingsService.Step>();
748                 Map<String, DocumentType> maptype = new HashMap<String, DocumentType>();
749
750                 List<ProjectSettingsService.Step> slist = null; // List of Steps to which each document type is valid
751                 int snum = 0; // Step number
752                 String type = null;
753                 String uses = null;
754                 for (Iterator<NamedNodeMap> i = _flows.iterator(); i.hasNext(); snum++) {
755                         NamedNodeMap flow = i.next();
756                         ProjectSettingsService.Step step = _steps.get(snum);
757                         String[] contents = flow.getNamedItem("contents").getNodeValue()
758                                         .split(",");
759                         for (int j = 0; j < contents.length; j++) {
760                                 type = contents[j];
761                                 if (!_mapuse.containsKey(type)) {
762                                         LOG.warn("Undefined \"" + type + "\" document type.");
763                                         continue;
764                                 }
765                                 slist = mapsteps.get(type);
766                                 if (slist == null) {
767                                         slist = new ArrayList<ProjectSettingsService.Step>();
768                                 }
769                                 slist.add(step);
770                                 mapsteps.put(type, slist);
771                         }
772                         Node result = flow.getNamedItem("result");
773                         if (result != null) {
774                                 mapresult.put(result.getNodeValue(), step);
775                         }
776                 }
777                 try {
778                         DocumentType tdoc = null;
779                         Set<String> tset = _mapuse.keySet();
780                         ProjectSettingsService.Step step;
781                         for (Iterator<String> i = tset.iterator(); i.hasNext();) {
782                                 type = i.next();
783                                 slist = mapsteps.get(type);
784                                 if (slist != null) {
785                                         uses = _mapuse.get(type);
786                                         step = mapresult.get(type);
787
788                                         tprop.clear();
789                                         tprop.setName(type).setStep(
790                                                         slist.toArray(new ProjectSettingsService.Step[slist
791                                                                         .size()]));
792                                         if (uses != null) {
793                                                 tdoc = maptype.get(uses);
794                                                 if (tdoc == null) {
795                                                         LOG.warn("Undefined \"" + uses
796                                                                         + "\" document type.");
797                                                 } else {
798                                                         tprop.setUses(tdoc);
799                                                 }
800                                         }
801                                         if (step != null) {
802                                                 tprop.setResult(step);
803                                         }
804
805                                         tprop.disableCheck();
806                                         tdoc = getDocumentTypeService().createType(tprop); // Creation of Document Types
807                                         getDocumentTypeService().approve(tdoc);
808                                         maptype.put(type, tdoc);
809                                         // Remember default type if it is
810                                         if (_defdoctypeKeys.containsKey(type)) {
811                                                 for (String key : _defdoctypeKeys.get(type)) {
812                                                         if (LOG.isDebugEnabled()) {
813                                                                 LOG.debug("Put mapping for default type: "
814                                                                                 + key + ": " + tdoc);
815                                                         }
816                                                         _defdoctype.put(key, tdoc);
817                                                 }
818                                         }
819                                 }
820                         }
821                 } catch (Exception error) {
822                         LOG.warn("Error creating document types, reason:", error); // Should not happen
823                 }
824                 if (LOG.isDebugEnabled()) {
825                         LOG.debug("Documents types are created: " + maptype.size());
826                 }
827         }
828
829         /**
830          * Create in the database knowledge types defined in the custom configuration.
831          */
832         private void createKnowledgeElementTypes() {
833                 try {
834                         KnowledgeElementType ktype = getKnowledgeElementTypeService()
835                                         .createType("usecase"); // Internal reserved knowledge element type
836                         getKnowledgeElementTypeService().reserve(ktype);
837                         for (Iterator<String> i = _kname.iterator(); i.hasNext();) {
838                                 String type = i.next();
839
840                                 ktype = getKnowledgeElementTypeService().createType(type); // Knowledge Elements Types defined in the configuration
841                                 getKnowledgeElementTypeService().approve(ktype);
842                         }
843                 } catch (Exception error) {
844                         LOG.warn("Error creating knowledge types, reason:", error); // Should not happen
845                 }
846         }
847
848         /**
849          * Create in the database simulation contexts types defined in the custom configuration.
850          */
851         private void createSimulationContextTypes() {
852                 Map<String, ProjectSettingsService.Step> mapstep = new HashMap<String, ProjectSettingsService.Step>();
853                 int snum = 0;
854                 for (Iterator<NamedNodeMap> i = _sclass.iterator(); i.hasNext(); snum++) {
855                         NamedNodeMap clatr = i.next();
856                         if (clatr != null) {
857                                 String[] clist = clatr.getNamedItem("context").getNodeValue()
858                                                 .split(",");
859                                 for (int j = 0; j < clist.length; j++) {
860                                         mapstep.put(clist[j], _steps.get(snum));
861                                 }
862                         }
863                 }
864                 try {
865                         SimulationContextType tctex = null;
866                         for (Iterator<String> i = _context.iterator(); i.hasNext();) {
867                                 String type = i.next();
868                                 if (mapstep.containsKey(type)) {
869                                         tctex = getSimulationContextTypeService().createType(type,
870                                                         mapstep.get(type)); // Creation of Simulation Context Types
871                                         getSimulationContextTypeService().approve(tctex);
872                                 } else {
873                                         LOG
874                                                         .warn("Could not find \""
875                                                                         + type
876                                                                         + "\" classification. Simulation Context type ignored.");
877                                 }
878                         }
879                 } catch (Exception error) {
880                         LOG.warn("Error creating context types, reason:", error); // Should not happen
881                 }
882         }
883
884         /**
885          * Get the database.
886          * 
887          * @return the database
888          */
889         public Database getDatabase() {
890                 return _database;
891         }
892
893         /**
894          * Set the database.
895          * 
896          * @param database
897          *            the database to set
898          */
899         public void setDatabase(final Database database) {
900                 _database = database;
901         }
902
903         /**
904          * Get the simulationContextTypeService.
905          * 
906          * @return the simulationContextTypeService
907          */
908         public SimulationContextTypeService getSimulationContextTypeService() {
909                 return _simulationContextTypeService;
910         }
911
912         /**
913          * Set the simulationContextTypeService.
914          * 
915          * @param simulationContextTypeService
916          *            the simulationContextTypeService to set
917          */
918         public void setSimulationContextTypeService(
919                         final SimulationContextTypeService simulationContextTypeService) {
920                 _simulationContextTypeService = simulationContextTypeService;
921         }
922
923         /**
924          * Get the knowledgeElementTypeService.
925          * 
926          * @return the knowledgeElementTypeService
927          */
928         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
929                 return _knowledgeElementTypeService;
930         }
931
932         /**
933          * Set the knowledgeElementTypeService.
934          * 
935          * @param knowledgeElementTypeService
936          *            the knowledgeElementTypeService to set
937          */
938         public void setKnowledgeElementTypeService(
939                         final KnowledgeElementTypeService knowledgeElementTypeService) {
940                 _knowledgeElementTypeService = knowledgeElementTypeService;
941         }
942
943         /**
944          * Get the documentTypeService.
945          * 
946          * @return the documentTypeService
947          */
948         public DocumentTypeService getDocumentTypeService() {
949                 return _documentTypeService;
950         }
951
952         /**
953          * Set the documentTypeService.
954          * 
955          * @param documentTypeService
956          *            the documentTypeService to set
957          */
958         public void setDocumentTypeService(
959                         final DocumentTypeService documentTypeService) {
960                 _documentTypeService = documentTypeService;
961         }
962 }