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  package org.nuiton.topia.generator;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.codehaus.plexus.component.annotations.Component;
30  import org.nuiton.eugene.Template;
31  import org.nuiton.eugene.java.ObjectModelTransformerToJava;
32  import org.nuiton.eugene.models.object.ObjectModelClass;
33  import org.nuiton.eugene.models.object.ObjectModelClassifier;
34  import org.nuiton.eugene.models.object.ObjectModelDependency;
35  import org.nuiton.eugene.models.object.ObjectModelInterface;
36  import org.nuiton.eugene.models.object.ObjectModelOperation;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  /**
42   * Created: 14 déc. 2009
43   *
44   * @author tchemit &lt;chemit@codelutin.com&gt;
45   * @version $Id$
46   * @since 2.3.0
47   * @deprecated 2.5.4, prefer use the transformer {@link EntityDAOTransformer}
48   */
49  @Deprecated
50  @Component(role = Template.class, hint = "org.nuiton.topia.generator.DAOImplTransformer")
51  public class DAOImplTransformer extends ObjectModelTransformerToJava {
52  
53      /** Logger. */
54      static Log log = LogFactory.getLog(DAOImplTransformer.class);
55  
56      /**
57       * Collection used to identify entities full qualified name that have
58       * a dependency for DAO extra operations. So no need to generate the
59       * DAOImpl in this case, it will be created by the developper. 
60       */
61      List<String> noGenerationNeeded = new ArrayList<String>();
62  
63      @Override
64      public void transformFromInterface(ObjectModelInterface interfacez) {
65          if (!TopiaGeneratorUtil.hasDaoStereotype(interfacez)) {
66              return;
67          }
68  
69          /**
70           * EVO #636 : Manage extra operations for DAO from "dao" dependency
71           * between an interface with stereotype &lt;&lt;dao&gt;&gt; (dependency client) and
72           * a class with stereotype &lt;&lt;entity&gt;&gt; (dependency supplier).
73           */
74  
75          ObjectModelDependency dependency =
76                  interfacez.getDependency(TopiaGeneratorUtil.DEPENDENCIES_DAO);
77  
78          if (dependency == null) {
79              if (log.isWarnEnabled()) {
80                  log.warn("Could not find dependency " +
81                           TopiaGeneratorUtil.DEPENDENCIES_DAO +
82                           " but DAO stereotype was placed on the interface " +
83                           interfacez.getName());
84  
85              }
86              return;
87          }
88          ObjectModelClassifier classifier = dependency.getSupplier();
89  
90          if (TopiaGeneratorUtil.isEntity(classifier)) {
91              noGenerationNeeded.add(classifier.getQualifiedName());
92          }
93      }
94  
95      @Override
96      public void transformFromClass(ObjectModelClass clazz) {
97          if (!TopiaGeneratorUtil.isEntity(clazz) || hasDAOOperations(clazz)) {
98              return;
99          }
100         String clazzName = clazz.getName();
101         String clazzFQN = clazz.getQualifiedName();
102         ObjectModelClass result = createClass(clazzName + "DAOImpl<E extends " + clazzName + ">", clazz.getPackageName());
103         setDocumentation(result, "/**\n" +
104                 " Implantation du DAO pour l'entité " + clazzName + ".\n" +
105                 " * L'utilisateur peut remplacer cette classe par la sienne en la mettant \n" +
106                 " * simplement dans ces sources. Cette classe générée sera alors simplement\n" +
107                 " * écrasée\n" +
108                 " */");
109         setSuperClass(result, clazzFQN + "DAOAbstract<E>");
110     }
111 
112     /**
113      * Detect if the class has DAO operations identified with &lt;&lt;dao&gt;&gt; stereotype.
114      *
115      * @param clazz The ObjectModelClass with operations (Corresponding to the Entity)
116      * @return true if the class has some dao operations, false if not
117      */
118     public boolean hasDAOOperations(ObjectModelClass clazz) {
119         // This code will be deprecated
120         for (ObjectModelOperation op : clazz.getOperations()) {
121             if (TopiaGeneratorUtil.hasDaoStereotype(op)) {
122                 return true;
123             }
124         }
125         // New method : interface dependency
126         return noGenerationNeeded.contains(clazz.getQualifiedName());
127     }
128 }