]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsServiceImpl.java
Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[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.HashMap;
14 import java.util.Iterator;
15 import java.util.LinkedHashMap;
16 import java.util.List;
17 import java.util.Properties;
18 import java.util.Set;
19 import java.util.Vector;
20
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23
24 import org.apache.log4j.Logger;
25 import org.w3c.dom.NamedNodeMap;
26 import org.w3c.dom.Node;
27 import org.w3c.dom.NodeList;
28
29 import org.splat.dal.bo.som.Document;
30 import org.splat.dal.bo.som.DocumentType;
31 import org.splat.dal.bo.som.KnowledgeElement;
32 import org.splat.dal.bo.som.KnowledgeElementType;
33 import org.splat.dal.bo.som.ProjectElement;
34 import org.splat.dal.bo.som.Scenario;
35 import org.splat.dal.bo.som.SimulationContext;
36 import org.splat.dal.bo.som.SimulationContextType;
37 import org.splat.dal.bo.som.Study;
38 import org.splat.dal.bo.som.ValidationCycle.Actor;
39 import org.splat.dal.dao.som.Database;
40 import org.splat.manox.XDOM;
41 import org.splat.service.SimulationContextService;
42
43 public class ProjectSettingsServiceImpl implements ProjectSettingsService {
44
45         // Non persistent configuration information
46         private Properties reprop; // Repository settings
47         private String pattern; // Pattern of study references
48         private FileNaming naming; // Scheme of file names stored into the repository
49         private String versioning; // Pattern of the presentation of version numbers
50         private Vector<ProjectSettingsService.Step> steps; // Ordered list of (transient) study steps
51         private Vector<ProjectSettingsValidationCycle> concycles; // Configuration document validation cycles
52
53         // Temporary attributes initialized from the configuration file for populating the database with object types
54         private LinkedHashMap<String, String> mapuse; // Document type names and uses mapping
55         private Vector<String> context; // Simulation Context type names
56         private Vector<String> kname; // Knowledge Element type names
57         private Vector<NamedNodeMap> flows; // Document flows
58         private Vector<NamedNodeMap> sclass; // Study classifications
59
60         // Other resources
61         private static ProjectSettingsServiceImpl my = null; // Singleton instance
62         private Database _database;
63         /**
64          * Injected simulation context service.
65          */
66         private SimulationContextService _simulationContextService;
67         protected final static Logger logger = Logger
68                         .getLogger(ProjectSettingsServiceImpl.class);
69
70         public enum FileNaming {
71                 title, encoded, asis
72         }
73
74         public static class ProjectSettingsValidationCycle {
75                 // -----------------------------------
76                 private String name;
77                 private Actor[] actor;
78
79                 private ProjectSettingsValidationCycle() {
80                         this.name = "built-in";
81                         this.actor = new Actor[] { null, null, null };
82                 }
83
84                 private ProjectSettingsValidationCycle(String name, Actor[] actor) {
85                         this.name = name;
86                         this.actor = actor;
87                 }
88
89                 public String getName() {
90                         return name;
91                 }
92
93                 public Actor[] getActorTypes() {
94                         return actor;
95                 }
96         }
97
98         // ==============================================================================================================================
99         // Construction
100         // ==============================================================================================================================
101         protected ProjectSettingsServiceImpl() {
102                 // ----------------------------
103                 reprop = new Properties();
104                 steps = new Vector<ProjectSettingsService.Step>();
105                 my = this;
106         }
107
108         // ==============================================================================================================================
109         // Public functions
110         // ==============================================================================================================================
111
112         public void configure(String filename) throws IOException, SQLException {
113                 // ---------------------------------------
114                 if (!steps.isEmpty())
115                         return; // Project already configured
116
117                 Database base = getDatabase().getMe();
118                 File config = new File(filename);
119                 if (config.exists()) {
120                         loadCustomization(config);
121                 } else {
122                         logger.fatal("Could not find the database configuration file \""
123                                         + config.getAbsolutePath() + "\"");
124                         throw new FileNotFoundException();
125                 }
126                 base.configure(reprop);
127                 if (!base.isInitialized()) {
128                         base.initialize();
129                         initialize(); // Populates the database with all necessary stuff
130                 }
131         }
132
133         public List<ProjectSettingsService.Step> getAllSteps() {
134                 // ---------------------------------------
135                 return steps;
136         }
137
138         /**
139          * Return the validation cycles of result documents defined in the workflow, ordered by study activities and ending by the default
140          * validation cycle, if defined.
141          * 
142          * @return the validation cycles of the workflow
143          */
144         public static List<ProjectSettingsValidationCycle> getAllValidationCycles() {
145                 // -------------------------------------------------------------
146                 return my.concycles;
147         }
148
149         public FileNaming getFileNamingScheme() {
150                 // -----------------------------------------------
151                 return naming;
152         }
153
154         public static ProjectSettingsValidationCycle getNewValidationCycle() {
155                 // ------------------------------------------------------
156                 return new ProjectSettingsValidationCycle();
157         }
158
159         public String getReferencePattern() {
160                 return pattern;
161         }
162
163         public String getRevisionPattern() {
164                 // ------------------------------------------
165                 return versioning;
166         }
167
168         public static ProjectSettingsService.Step getStep(int number) {
169                 // ---------------------------------------
170                 for (int i = 0; i < my.steps.size(); i++) {
171                         ProjectSettingsService.Step step = my.steps.get(i);
172                         if (step.number == number)
173                                 return step;
174                 }
175                 return null;
176         }
177
178         public List<ProjectSettingsService.Step> getStepsOf(
179                         Class<? extends ProjectElement> level) {
180                 // ---------------------------------------------------------------------------
181                 Vector<ProjectSettingsService.Step> result = new Vector<ProjectSettingsService.Step>();
182
183                 for (int i = 0; i < steps.size(); i++) {
184                         ProjectSettingsService.Step step = steps.get(i);
185                         if (step.appliesTo(level))
186                                 result.add(step);
187                 }
188                 return result;
189         }
190
191         // ==============================================================================================================================
192         // Protected member function
193         // ==============================================================================================================================
194
195         public void initialize() {
196                 // ----------------------------
197                 createDocumentTypes();
198                 createSimulationContextTypes();
199                 createKnowledgeElementTypes();
200         }
201
202         // ==============================================================================================================================
203         // Private member function
204         // ==============================================================================================================================
205
206         private void loadCustomization(File config) {
207                 // --------------------------------------------
208                 try {
209                         DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory
210                                         .newInstance();
211                         DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
212
213                         org.w3c.dom.Document conf = dBuilder.parse(config.getPath());
214                         HashMap<String, Node> children = XDOM.getNamedChildNodes(conf
215                                         .getDocumentElement());
216
217                         // Repository tag initializing the reprop attribute
218                         Node child = children.get("database");
219                         HashMap<String, Node> datag = XDOM.getNamedChildNodes(child);
220
221                         String disk = datag.get("repository").getAttributes().getNamedItem(
222                                         "disk").getNodeValue();
223                         if (!disk.endsWith("/"))
224                                 disk = disk + "/";
225                         logger.info("Database root set to " + disk);
226                         reprop.setProperty("repository", disk);
227
228                         // Formats tag initializing the reference pattern and date attributes
229                         child = children.get("formats");
230                         datag = XDOM.getNamedChildNodes(child);
231
232                         NamedNodeMap natr = datag.get("references").getAttributes();
233                         pattern = natr.getNamedItem("study").getNodeValue();
234
235                         natr = datag.get("files").getAttributes();
236                         naming = FileNaming.valueOf(natr.getNamedItem("name")
237                                         .getNodeValue());
238
239                         natr = datag.get("versions").getAttributes();
240                         versioning = natr.getNamedItem("pattern").getNodeValue();
241
242                         // Activities tag initializing the steps and rex attributes
243                         child = children.get("activities");
244                         NodeList nlist = child.getChildNodes();
245                         Vector<NamedNodeMap> flist = new Vector<NamedNodeMap>();
246                         Vector<String> resultype = new Vector<String>();
247                         Vector<NamedNodeMap> clist = new Vector<NamedNodeMap>();
248
249                         int snum = 1; // Base number of steps
250                         for (int i = 0; i < nlist.getLength(); i++) {
251                                 child = nlist.item(i);
252                                 if (child.getNodeName().equals("scenario")) {
253                                         NodeList slist = child.getChildNodes();
254                                         for (int j = 0; j < slist.getLength(); j++) {
255                                                 child = slist.item(j);
256                                                 if (!child.getNodeName().equals("step"))
257                                                         continue;
258                                                 HashMap<String, Node> tags = XDOM
259                                                                 .getNamedChildNodes(child);
260
261                                                 natr = tags.get("storage").getAttributes();
262                                                 ProjectSettingsService.Step step = new ProjectSettingsService.Step(
263                                                                 snum, Scenario.class, natr.getNamedItem("path")
264                                                                                 .getNodeValue());
265
266                                                 // Keeping flow and classification information for eventual later use
267                                                 natr = tags.get("flow").getAttributes();
268                                                 flist.add(natr);
269                                                 child = natr.getNamedItem("result");
270                                                 if (child != null)
271                                                         resultype.add(child.getNodeValue());
272
273                                                 child = tags.get("classification");
274                                                 if (child != null)
275                                                         clist.add(child.getAttributes());
276                                                 else
277                                                         clist.add(null);
278
279                                                 if (natr.getNamedItem("contents").getNodeValue()
280                                                                 .equals("knowledge")) {
281                                                         // TODO In a given scenario, only one step must contain knowledges
282                                                         step.contents.add(KnowledgeElement.class);
283                                                 } else {
284                                                         step.contents.add(Document.class);
285                                                 }
286                                                 steps.add(step);
287                                                 snum += 1;
288                                         }
289                                 } else {
290                                         if (!child.getNodeName().equals("step"))
291                                                 continue;
292                                         HashMap<String, Node> tags = XDOM.getNamedChildNodes(child);
293
294                                         natr = tags.get("storage").getAttributes(); // Mandatory information
295                                         ProjectSettingsService.Step step = new ProjectSettingsService.Step(
296                                                         snum, Study.class, natr.getNamedItem("path")
297                                                                         .getNodeValue());
298
299                                         // Keeping flow and classification information for eventual later use
300                                         natr = tags.get("flow").getAttributes();
301                                         flist.add(natr);
302                                         child = natr.getNamedItem("result");
303                                         if (child != null)
304                                                 resultype.add(child.getNodeValue());
305
306                                         child = tags.get("classification"); // Optional information
307                                         if (child != null)
308                                                 clist.add(child.getAttributes());
309                                         else
310                                                 clist.add(null);
311
312                                         if (natr.getNamedItem("contents").getNodeValue().equals(
313                                                         "knowledge")) {
314                                                 // TODO Error: knowledges must be attached to scenarios
315                                         } else {
316                                                 step.contents.add(Document.class);
317                                         }
318                                         steps.add(step);
319                                         snum += 1;
320                                 }
321                         }
322                         // Validations tag
323                         child = children.get("validations");
324                         concycles = new Vector<ProjectSettingsValidationCycle>();
325                         datag = XDOM.getNamedChildNodes(child);
326
327                         String[] step = { "review", "approval", "acceptance" };
328                         resultype.add("default");
329                         for (Iterator<String> i = resultype.iterator(); i.hasNext();) {
330                                 Actor[] actor = { null, null, null };
331                                 String name = i.next();
332                                 child = datag.get(name);
333                                 if (child == null)
334                                         continue; // Document type not subject of any validation
335                                 natr = child.getAttributes();
336                                 for (int j = 0; j < step.length; j++) {
337                                         child = natr.getNamedItem(step[j]);
338                                         if (child == null)
339                                                 continue; // Validation step not required
340                                         actor[j] = Actor.valueOf(child.getNodeValue());
341                                 }
342                                 concycles.add(new ProjectSettingsValidationCycle(name, actor));
343                         }
344                         concycles.add(new ProjectSettingsValidationCycle()); // Adds the built-in validation cycle
345
346                         if (getDatabase().getMe().isInitialized())
347                                 return; // No need to load object type definitions as they are already stored
348
349                         // Documents tag
350                         child = children.get("documents");
351                         nlist = child.getChildNodes();
352
353                         flows = flist; // Kept for later use in document type definition
354                         sclass = clist; // Kept for later use in simulation context type definition
355                         mapuse = new LinkedHashMap<String, String>();
356                         for (int i = 0; i < nlist.getLength(); i++) {
357                                 child = nlist.item(i);
358                                 if (!child.getNodeName().equals("article"))
359                                         continue;
360
361                                 natr = child.getAttributes();
362                                 String type = natr.getNamedItem("type").getNodeValue();
363                                 String uses = null;
364                                 child = natr.getNamedItem("uses");
365                                 if (child != null)
366                                         uses = child.getNodeValue();
367                                 mapuse.put(type, uses); // Must be added to the map even if no (null) uses
368                         }
369                         // Simulation Contexts tag
370                         child = children.get("contexts");
371                         nlist = child.getChildNodes();
372
373                         context = new Vector<String>();
374                         for (int i = 0; i < nlist.getLength(); i++) {
375                                 child = nlist.item(i);
376                                 if (!child.getNodeName().equals("article"))
377                                         continue;
378
379                                 context.add(child.getAttributes().getNamedItem("type")
380                                                 .getNodeValue());
381                         }
382                         // Knowledge Elements tag
383                         child = children.get("knowledges");
384                         nlist = child.getChildNodes();
385
386                         kname = new Vector<String>();
387                         for (int i = 0; i < nlist.getLength(); i++) {
388                                 child = nlist.item(i);
389                                 if (!child.getNodeName().equals("article"))
390                                         continue;
391
392                                 kname.add(child.getAttributes().getNamedItem("type")
393                                                 .getNodeValue());
394                         }
395                 } catch (Exception error) {
396                         logger.info("Error in customization", error);
397                 }
398         }
399
400         private void createDocumentTypes() {
401                 // -----------------------------------
402                 DocumentType.Properties tprop = new DocumentType.Properties();
403                 HashMap<String, Vector<ProjectSettingsService.Step>> mapsteps = new HashMap<String, Vector<ProjectSettingsService.Step>>();
404                 HashMap<String, ProjectSettingsService.Step> mapresult = new HashMap<String, ProjectSettingsService.Step>();
405                 HashMap<String, DocumentType> maptype = new HashMap<String, DocumentType>();
406
407                 Vector<ProjectSettingsService.Step> slist = null; // List of Steps to which each document type is valid
408                 int snum = 0; // Step number
409                 String type = null;
410                 String uses = null;
411                 for (Iterator<NamedNodeMap> i = flows.iterator(); i.hasNext(); snum++) {
412                         NamedNodeMap flow = i.next();
413                         ProjectSettingsService.Step step = steps.get(snum);
414                         String[] contents = flow.getNamedItem("contents").getNodeValue()
415                                         .split(",");
416                         for (int j = 0; j < contents.length; j++) {
417                                 type = contents[j];
418                                 if (!mapuse.containsKey(type)) {
419                                         logger.warn("Undefined \"" + type + "\" document type.");
420                                         continue;
421                                 }
422                                 slist = mapsteps.get(type);
423                                 if (slist == null)
424                                         slist = new Vector<ProjectSettingsService.Step>();
425                                 slist.add(step);
426                                 mapsteps.put(type, slist);
427                         }
428                         Node result = flow.getNamedItem("result");
429                         if (result != null)
430                                 mapresult.put(result.getNodeValue(), step);
431                 }
432                 try {
433                         DocumentType tdoc = null;
434                         Set<String> tset = mapuse.keySet();
435                         ProjectSettingsService.Step step;
436                         for (Iterator<String> i = tset.iterator(); i.hasNext();) {
437                                 type = i.next();
438                                 slist = mapsteps.get(type);
439                                 uses = mapuse.get(type);
440                                 step = mapresult.get(type);
441
442                                 tprop.clear();
443                                 tprop.setName(type).setStep(
444                                                 slist.toArray(new ProjectSettingsService.Step[slist
445                                                                 .size()]));
446                                 if (uses != null) {
447                                         tdoc = maptype.get(uses);
448                                         if (tdoc == null)
449                                                 logger
450                                                                 .warn("Undefined \"" + uses
451                                                                                 + "\" document type.");
452                                         else
453                                                 tprop.setUses(tdoc);
454                                 }
455                                 if (step != null)
456                                         tprop.setResult(step);
457
458                                 tprop.disableCheck();
459                                 tdoc = Document.createType(tprop); // Creation of Document Types
460                                 tdoc.approve();
461                                 maptype.put(type, tdoc);
462                         }
463                 } catch (Exception error) {
464                         logger.warn("Error creating document types, reason:", error); // Should not happen
465                 }
466         }
467
468         private void createKnowledgeElementTypes() {
469                 // -------------------------------------------
470                 try {
471                         KnowledgeElementType ktype = KnowledgeElement.createType("usecase"); // Internal reserved knowledge element type
472                         ktype.reserve();
473                         for (Iterator<String> i = kname.iterator(); i.hasNext();) {
474                                 String type = i.next();
475
476                                 ktype = KnowledgeElement.createType(type); // Knowledge Elements Types defined in the configuration
477                                 ktype.approve();
478                         }
479                 } catch (Exception error) {
480                         logger.warn("Error creating knowledge types, reason:", error); // Should not happen
481                 }
482         }
483
484         private void createSimulationContextTypes() {
485                 // --------------------------------------------
486                 HashMap<String, ProjectSettingsService.Step> mapstep = new HashMap<String, ProjectSettingsService.Step>();
487                 int snum = 0;
488                 for (Iterator<NamedNodeMap> i = sclass.iterator(); i.hasNext(); snum++) {
489                         NamedNodeMap clatr = i.next();
490                         if (clatr == null)
491                                 continue;
492
493                         String[] clist = clatr.getNamedItem("context").getNodeValue()
494                                         .split(",");
495                         for (int j = 0; j < clist.length; j++) {
496                                 mapstep.put(clist[j], steps.get(snum));
497                         }
498                 }
499                 try {
500                         SimulationContextType tctex = null;
501                         for (Iterator<String> i = context.iterator(); i.hasNext();) {
502                                 String type = i.next();
503                                 if (!mapstep.containsKey(type)) {
504                                         logger
505                                                         .warn("Could not find \""
506                                                                         + type
507                                                                         + "\" classification. Simulation Context type ignored.");
508                                         continue;
509                                 }
510                                 tctex = getSimulationContextService().createType(type,
511                                                 mapstep.get(type)); // Creation of Simulation Context Types
512                                 tctex.approve();
513                         }
514                 } catch (Exception error) {
515                         logger.warn("Error creating context types, reason:", error); // Should not happen
516                 }
517         }
518
519         /**
520          * Get the database.
521          * 
522          * @return the database
523          */
524         public Database getDatabase() {
525                 return _database;
526         }
527
528         /**
529          * Set the database.
530          * 
531          * @param database
532          *            the database to set
533          */
534         public void setDatabase(Database database) {
535                 _database = database;
536         }
537
538         /**
539          * Get the simulationContextService.
540          * 
541          * @return the simulationContextService
542          */
543         public SimulationContextService getSimulationContextService() {
544                 return _simulationContextService;
545         }
546
547         /**
548          * Set the simulationContextService.
549          * 
550          * @param simulationContextService
551          *            the simulationContextService to set
552          */
553         public void setSimulationContextService(
554                         SimulationContextService simulationContextService) {
555                 _simulationContextService = simulationContextService;
556         }
557 }