View Javadoc
1   /*
2    * #%L
3    * ToPIA :: Persistence
4    * $Id$
5    * $HeadURL$
6    * %%
7    * Copyright (C) 2004 - 2014 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as 
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  
26  /* *
27  * TopiaRelationValidator.java
28  *
29  * Created: 31 mars 2006
30  *
31  * @author Arnaud Thimel &lt;thimel@codelutin.com&gt;
32  * @version $Revision$
33  *
34  * Mise a jour: $Date$
35  * par : $Author$
36  */
37  
38  package org.nuiton.topia.generator;
39  
40  import org.nuiton.eugene.models.object.ObjectModel;
41  import org.nuiton.eugene.models.object.ObjectModelAttribute;
42  import org.nuiton.eugene.models.object.validator.ObjectModelValidator;
43  
44  /**
45   * Validateur pour les relations du modèle.
46   * Vérifie que :
47   * <ul>
48   *   <li>Toutes les relations ont au moins une navigabilité</li>
49   *   <li>Une relation 1-n unidirectionnelle pointant sur une classe ayant des
50   *   sous-classes dans le modèle est incompatibe avec Hibernate</li>
51   *   <li>Toutes les relations ont des reverseAttribute</li>
52   * </ul>
53   *
54   * @version $Id$
55   */
56  public class TopiaRelationValidator extends ObjectModelValidator {
57  
58      /**
59       * Constructeur de TopiaRelationValidator.
60       * 
61       * @param model le modèle à valider
62       */
63      public TopiaRelationValidator(ObjectModel model) {
64          super(model);
65      }
66  
67      /* (non-Javadoc)
68       * @see org.nuiton.eugene.models.object.validator.ObjectModelValidator#validateAttribute(org.nuiton.eugene.models.object.ObjectModelAttribute)
69       */
70      @Override
71      protected boolean validateAttribute(ObjectModelAttribute attr) {
72          boolean isValid = true;
73          ObjectModelAttribute reverse = attr.getReverseAttribute();
74  
75          /* Relation navigabilité */
76          //Pour ne pas avoir de doublons, on ne vérifie que sur le premier
77          //attribut par ordre alphabétique
78          if (TopiaGeneratorUtil.isFirstAttribute(attr)) {
79              if (!attr.isNavigable() && !reverse.isNavigable()) {
80                  addError(attr, "La relation entre " + "\"" + reverse.getType()
81                          + "\"[" + attr.getName() + "] et " + "\""
82                          + attr.getType() + "\"[" + reverse.getName() + "] "
83                          + "n'est navigable dans aucun sens");
84                  isValid = false;
85              }
86          }
87  
88          /* Relation héritage */
89          if (TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType(attr, model)) {
90              isValid = false;
91              addError(
92                      attr,
93                      "La relation entre "
94                              + "\""
95                              + reverse.getType()
96                              + "\"["
97                              + attr.getName()
98                              + "] et "
99                              + "\""
100                             + attr.getType()
101                             + "\"["
102                             + reverse.getName()
103                             + "] "
104                             + "n'est navigable que dans un sens et "
105                             + "la classe \""
106                             + attr.getType()
107                             + "\" a des sous-classes. "
108                             + "Des accesseurs doivent donc etre generes pour Hibernate.");
109         }
110 
111         /* Pas d'inverse */
112         if (reverse == null && model.hasClass(attr.getType())) {
113             isValid = false;
114             addError(attr, "Cet attribut n'a pas d'inverse.");
115         }
116 
117         return isValid;
118     }
119 
120 } //TopiaRelationValidator