View Javadoc
1   /*******************************************************************************
2    * Copyright 2012 pw999
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   ******************************************************************************/
16  package net.sf.oqt.maven;
17  
18  import java.sql.SQLException;
19  import java.util.Collection;
20  
21  import net.sf.oqt.core.CoreFactory;
22  import net.sf.oqt.core.Serializer;
23  import net.sf.oqt.jpa.EntityFinder;
24  import net.sf.oqt.jpa.JPATransformer;
25  import net.sf.oqt.model.EntityVO;
26  import net.sf.oqt.model.PackageVO;
27  import net.sf.oqt.model.QueryVO;
28  import net.sf.oqt.model.ResultVO;
29  
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.MojoFailureException;
32  
33  
34  /**
35   * The QueryTranslatorBuildMojo is the mojo which will convert all the known JPQL queries to SQL queries.
36   * <p>
37   * This plugin will gather the following information from your ORM:
38   * <ul>
39   * <li>The list of entities per package
40   * <li>The list of named queries per entity
41   * <li>The list of SQL queries per named query
42   * <li>The list of erroneous queries.
43   * </ul>
44   * 
45   * @goal translate
46   * @phase site
47   * @executionStrategy once-per-session
48   * @author phillip
49   * 
50   */
51  public class QueryTranslatorTranslateMojo extends AbstractQueryTranslatorTranslateMojo {
52  
53      /**
54       * Finds all the known entities, named queries and translates all the information.
55       * <p>
56       * The gathered information is saved to disk using the {@link Serializer} class.
57       * 
58       * @throws MojoFailureException if anything goes wrong
59       * @throws MojoExecutionException if the ClassPath is not correct or if closing the database connection failed.
60       */
61      @Override
62      public void execute() throws MojoExecutionException, MojoFailureException {
63          getLog().info("Starting Translate Mojo");
64          super.execute();
65          try {
66              final ResultVO result = new ResultVO();
67              result.getPackages().addAll(EntityFinder.findAllEntities(CoreFactory.getProperties().getPackageNames()));
68              if (result.getPackages().isEmpty()) {
69                  getLog().warn("Nothing found, are the packages correctly configured ? ");
70                  return;
71              }
72              CoreFactory.getProperties().setFqnsFromResult(result);
73              JPATransformer.transformAllQueries(result.getPackages());
74              printOutputToDebug(result);
75              final Serializer s = new Serializer(project);
76              s.serialize(result);
77  
78              for (String type : getReportTypes()) {
79                  String output = getOutput(type, result);
80                  if (output != null) {
81                      s.textOutput(type + ".txt", output);
82                  }
83              }
84              
85              getLog().info("Finished Translate Mojo");
86  
87          } catch (Exception e) {
88              getLog().error(e);
89              throw new MojoFailureException(e.getMessage());
90          } finally {
91              try {
92                  CoreFactory.shutdown();
93              } catch (final SQLException e) {
94                  getLog().error("Failed to shutdown database connection", e);
95                  throw new MojoExecutionException(e.getMessage());
96              }
97          }
98  
99      }
100     
101     
102     /**
103      * Handles the different textual outputs.
104      * @param type the type of output
105      * @param r the resultset
106      */
107     String getOutput(String type, ResultVO r) {
108         String output = "";
109         if (type.equalsIgnoreCase("sql")) {
110             output = getOutputSQL(r);
111         } else if (type.equalsIgnoreCase("jpql")) {
112             output = getOutputJPQL(r);
113         }
114         return output.isEmpty() ? null : output;
115     }
116     
117     String getOutputSQL(ResultVO r) {
118         StringBuilder b = new StringBuilder();
119         for (final PackageVO packageVO : r.getPackages()) {
120             for (final EntityVO entityVO : packageVO.getEntities()) {
121                 for (final QueryVO queryVO : entityVO.getQueries()) {
122                     for (final String query : queryVO.getSQLQuery()) {
123                         b.append(query).append("@").append("\r\n");
124                     }
125                 }
126             }
127         }
128         return b.toString();
129     }
130     
131     String getOutputJPQL(ResultVO r) {
132         StringBuilder b = new StringBuilder();
133         for (final PackageVO packageVO : r.getPackages()) {
134             for (final EntityVO entityVO : packageVO.getEntities()) {
135                 for (final QueryVO queryVO : entityVO.getQueries()) {
136                     b.append(queryVO.getJPQLQuery()).append("\r\n");
137                 }
138             }
139         }
140         return b.toString();
141     }
142     
143 
144 }