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.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21
22 import javax.persistence.Entity;
23 import javax.persistence.EntityManager;
24
25 import net.sf.oqt.core.CoreFactory;
26 import net.sf.oqt.core.CoreProperties;
27 import net.sf.oqt.model.EntityVO;
28 import net.sf.oqt.model.PackageVO;
29 import net.sf.oqt.model.QueryVO;
30 import net.sf.oqt.model.ResultVO;
31
32 import org.apache.maven.plugin.AbstractMojo;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.MojoFailureException;
35 import org.apache.maven.project.MavenProject;
36 import org.apache.openjpa.persistence.EntityManagerImpl;
37 import org.apache.openjpa.persistence.OpenJPAQuery;
38
39
40 public abstract class AbstractQueryTranslatorTranslateMojo extends AbstractMojo {
41
42
43
44
45
46
47 protected MavenProject project;
48
49
50
51
52
53
54
55
56
57
58 private String url = "";
59
60
61
62
63
64 private String username = "";
65
66
67
68
69
70 private String password = "";
71
72
73
74
75
76
77
78
79 private String driverName = "";
80
81
82
83
84
85
86 private String dictionary = "";
87
88
89
90
91
92
93
94
95
96
97
98 private final List<String> packageNames = new ArrayList<String>();
99
100
101
102
103
104
105
106
107
108
109
110
111
112 private final List<String> reportTypes = new ArrayList<String>();
113
114 public final String getUrl() {
115 return url;
116 }
117
118 public final void setUrl(final String url) {
119 this.url = url;
120 }
121
122 public final String getUsername() {
123 return username;
124 }
125
126 public final void setUsername(final String username) {
127 this.username = username;
128 }
129
130 public final String getPassword() {
131 return password;
132 }
133
134 public final void setPassword(final String password) {
135 this.password = password;
136 }
137
138 public final String getDriverName() {
139 return driverName;
140 }
141
142 public final void setDriverName(final String driverName) {
143 this.driverName = driverName;
144 }
145
146 public final String getDictionary() {
147 return dictionary;
148 }
149
150 public final void setDictionary(final String dictionary) {
151 this.dictionary = dictionary;
152 }
153
154 public final List<String> getPackageNames() {
155 return packageNames;
156 }
157
158
159
160
161 public final List<String> getReportTypes() {
162 return reportTypes;
163 }
164
165 void printOutputToDebug(final ResultVO result) {
166 if (getLog().isDebugEnabled()) {
167 final Collection<PackageVO> packages = result.getPackages();
168 for (final PackageVO packageVO : packages) {
169 getLog().debug("Package: " + packageVO.getName());
170 final Collection<EntityVO> entities = packageVO.getEntities();
171 for (final EntityVO entityVO : entities) {
172 getLog().debug(">Entity: " + entityVO.getName());
173 final Collection<QueryVO> queries = entityVO.getQueries();
174 for (final QueryVO queryVO : queries) {
175 getLog().debug(">>NamedQuery: " + queryVO.getName());
176 getLog().debug(">>> Number of SQL queries: " + queryVO.getSQLQuery().size());
177 }
178 }
179 }
180 }
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 void validateClassPath() throws MojoExecutionException {
199 final List<String> missing = new ArrayList<String>();
200
201 try {
202 Class.forName(EntityManager.class.getName());
203 Class.forName(Entity.class.getName());
204 } catch (final ClassNotFoundException e) {
205 missing.add("Ensure that javaee-api is on the classpath");
206 }
207
208 try {
209 Class.forName(EntityManagerImpl.class.getName());
210 Class.forName(OpenJPAQuery.class.getName());
211 Class.forName(org.apache.openjpa.persistence.PersistenceProviderImpl.class.getName());
212 } catch (final ClassNotFoundException e) {
213 missing.add("Ensure that openjpa is on the classpath");
214 }
215
216 try {
217 Class.forName(getDriverName());
218 } catch (final ClassNotFoundException e) {
219 missing.add("Failed to find the driver" + getDriverName() + " on the classpath. Ensure that you provided it as a dependency");
220 }
221 if (!missing.isEmpty()) {
222 for (final String string : missing) {
223 getLog().error(string);
224 }
225 throw new MojoExecutionException("Failed to find all dependencies, see log output for more info");
226 }
227
228 }
229
230
231
232
233 @Override
234 public void execute() throws MojoExecutionException, MojoFailureException {
235
236 CoreFactory.getProperties().setUrl(getUrl());
237 CoreFactory.getProperties().setUsername(getUsername());
238 CoreFactory.getProperties().setPassword(getPassword());
239 CoreFactory.getProperties().setDriverName(getDriverName());
240 CoreFactory.getProperties().setDictionary(getDictionary());
241 CoreFactory.getProperties().getFqns().clear();
242 CoreFactory.getProperties().getPackageNames().addAll(getPackageNames());
243 validateClassPath();
244 }
245
246 void setProject(final MavenProject project) {
247 this.project = project;
248 }
249
250 }