1 package org.devaki.nextobjects.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.util.HashSet;
22 import java.util.Set;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.devaki.nextobjects.workspace.models.PhysicalModel;
26 import org.devaki.nextobjects.workspace.models.columns.Column;
27 /***
28 * Verify a Physical Model (Experimental)
29 * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
30 */
31 public class PDMVerifier
32 {
33 /***
34 * The logger
35 */
36 private static Log logger = LogFactory.getLog(PDMVerifier.class.getName());
37 /***
38 * Verify the current PDM
39 * @param pDatabase the model
40 */
41 public static void verify(final PhysicalModel pDatabase)
42 {
43 int errors = 0;
44 int warnings = 0;
45 boolean tmpBoolean = false;
46 Set s;
47
48 if (pDatabase.getTables().size() < 1)
49 {
50 errors++;
51 logger.error("The PDM must have at least one table");
52 }
53
54 s = new HashSet();
55
56 for (int i = 0; i < pDatabase.getTables().size(); i++)
57 {
58 if (!s.add(pDatabase.getTableAt(i).getCode()))
59 {
60 errors++;
61 logger.error(
62 pDatabase.getTableAt(i).getCode() + " is not unique");
63 }
64 tmpBoolean = false;
65 Set s2 = new HashSet();
66
67 for (int j = 0; j < pDatabase.getTableAt(i).getData().size(); j++)
68 {
69 if (!s2
70 .add(
71 ((Column) pDatabase
72 .getTableAt(i)
73 .getData()
74 .elementAt(j))
75 .getCode()))
76 {
77 errors++;
78 logger.error(
79 ((Column) pDatabase
80 .getTableAt(i)
81 .getData()
82 .elementAt(j))
83 .getCode()
84 + " is not unique in "
85 + pDatabase.getTableAt(i));
86 }
87 if (((Column) pDatabase.getTableAt(i).getData().elementAt(j))
88 .isPk())
89 {
90 tmpBoolean = true;
91 }
92 }
93
94 if (pDatabase.getTables().size() < 1)
95 {
96 logger.error("A table must have at least on field");
97 }
98
99 if (!tmpBoolean)
100 {
101 logger.warn(
102 "Table "
103 + pDatabase.getTableAt(i).getName()
104 + " has no primary key");
105 warnings++;
106 }
107 }
108
109 logger.info(errors + " error(s), " + warnings + " warning(s).");
110 if (errors > 0)
111 {
112 logger.error(
113 "The Physical Data Model '"
114 + pDatabase.getName()
115 + "' is not correct.");
116 pDatabase.setVerified(false);
117 }
118 else
119 {
120 logger.info("The PDM '" + pDatabase.getName() + "' is correct.");
121 pDatabase.setVerified(true);
122 }
123 }
124 }