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