View Javadoc

1   package org.devaki.nextobjects.util;
2   /*
3   
4   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
5   
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; either version 2 of the License, or (at your
9   option) any later version.
10  
11  This program is distributed in the hope that it will
12  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc., 59
18  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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          // Checking entities
48          if (pDatabase.getTables().size() < 1)
49          {
50              errors++;
51              logger.error("The PDM must have at least one table");
52          }
53          // for table name uniquenesss
54          s = new HashSet();
55          // loop for tables
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              // loop for columns
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              } // end of columns loop
93              // table must have at least one column
94              if (pDatabase.getTables().size() < 1)
95              {
96                  logger.error("A table must have at least on field");
97              }
98              // tables must have at least one PK
99              if (!tmpBoolean)
100             {
101                 logger.warn(
102                     "Table "
103                         + pDatabase.getTableAt(i).getName()
104                         + " has no primary key");
105                 warnings++;
106             }
107         } // end of tables loop
108         // Display results
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 }