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 java.util.Vector;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.devaki.nextobjects.workspace.models.ConceptualModel;
27  import org.devaki.nextobjects.workspace.models.columns.Column;
28  /***
29   *  EXPERIMENTAL CDMVerifier
30   * @todo check required fields against DTD, lot of other checks
31   * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
32   */
33  public class CDMVerifier
34  {
35      /*** logger */
36      private static Log logger = LogFactory.getLog(CDMVerifier.class.getName());
37      /***
38          * Default constructor do nothing
39          *
40          */
41      public CDMVerifier()
42      {
43          logger.warn(" -- Using EXPERIMENTAL CDMVerifier -- ");
44      }
45      /***
46       * Verify the current CDM
47       * @param pMerise the model to verify
48       * @return isVerified
49       */
50      public static boolean verify(final ConceptualModel pMerise)
51      {
52          int errors = 0;
53          int warnings = 0;
54          boolean tmpBoolean = false;
55          Vector fieldList = new Vector();
56          logger.warn("Using EXPERIMENTAL CDMVerifier");
57          /*** Checking entities **/
58          if (pMerise.getEntities().size() < 1)
59          {
60              errors++;
61              logger.error("The CDM must have at least one entity");
62          }
63          Set s = new HashSet();
64          // loop for entities
65          for (int i = 0; i < pMerise.getEntities().size(); i++)
66          {
67              if (!s.add(pMerise.getEntityAt(i).getCode()))
68              {
69                  errors++;
70                  logger.error(
71                      "Entity code "
72                          + pMerise.getEntityAt(i).getCode()
73                          + " is not unique");
74              }
75              //for entity identifier check
76              tmpBoolean = false;
77              // loop for columns
78              for (int j = 0; j < pMerise.getEntityAt(i).getData().size(); j++)
79              {
80                  fieldList.addElement(
81                      new String[] {
82                          pMerise.getEntityAt(i).getCode(),
83                          ((Column) pMerise
84                              .getEntityAt(i)
85                              .getData()
86                              .elementAt(j))
87                              .getName(),
88                          ((Column) pMerise
89                              .getEntityAt(i)
90                              .getData()
91                              .elementAt(j))
92                              .getCode()});
93                  if (((Column) pMerise.getEntityAt(i).getData().elementAt(j))
94                      .isPk())
95                  {
96                      tmpBoolean = true;
97                  }
98              } // end of columns loop
99              // entity must have at least one column
100             if (pMerise.getEntities().size() < 1)
101             {
102                 logger.error("An entity must have at least on field");
103             }
104             // entities must have at least one identifier
105             if (!tmpBoolean)
106             {
107                 logger.error(
108                     "Entity "
109                         + pMerise.getEntityAt(i).getName()
110                         + " has no identifier");
111                 errors++;
112             }
113         } // end of entities loop
114         /*** Checking associations **/
115         // loop for associations
116         for (int i = 0; i < pMerise.getAssociations().size(); i++)
117         {
118             if (!s.add(pMerise.getAssociationAt(i).getCode()))
119             {
120                 errors++;
121                 logger.error(
122                     pMerise.getAssociationAt(i).getCode() + " is not unique");
123             }
124             // loop for columns
125             for (int j = 0;
126                 j < pMerise.getAssociationAt(i).getData().size();
127                 j++)
128             {
129                 fieldList.addElement(
130                     new String[] {
131                         pMerise.getAssociationAt(i).getCode(),
132                         ((Column) pMerise
133                             .getAssociationAt(i)
134                             .getData()
135                             .elementAt(j))
136                             .getName(),
137                         ((Column) pMerise
138                             .getAssociationAt(i)
139                             .getData()
140                             .elementAt(j))
141                             .getCode()});
142             }
143         }
144         /*** Checking uniqueness **/
145         s = new HashSet();
146         for (int i = 0; i < fieldList.size(); i++)
147         {
148             if (!s.add(((String[]) fieldList.elementAt(i))[1]))
149             {
150                 warnings++;
151                 logger.warn(
152                     ((String[]) fieldList.elementAt(i))[1]
153                         + " is not unique in "
154                         + ((String[]) fieldList.elementAt(i))[0]);
155             }
156         }
157         /*** Display results **/
158         logger.info(errors + " error(s), " + warnings + " warning(s).");
159         if (errors > 0)
160         {
161             logger.error("I can't continue cause the model is wrong");
162             pMerise.setVerified(false);
163         }
164         else
165         {
166             logger.info("CDM " + pMerise.getName() + " is correct");
167             pMerise.setVerified(true);
168         }
169         return pMerise.isVerified();
170     }
171 }