1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class QueryTranslatorTranslateMojo extends AbstractQueryTranslatorTranslateMojo {
52
53
54
55
56
57
58
59
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
104
105
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 }