1
19
20 package com.liferay.portal.tools.servicebuilder;
21
22 import com.liferay.portal.freemarker.FreeMarkerUtil;
23 import com.liferay.portal.kernel.util.ArrayUtil;
24 import com.liferay.portal.kernel.util.ArrayUtil_IW;
25 import com.liferay.portal.kernel.util.FileUtil;
26 import com.liferay.portal.kernel.util.GetterUtil;
27 import com.liferay.portal.kernel.util.PropertiesUtil;
28 import com.liferay.portal.kernel.util.SetUtil;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.StringUtil;
31 import com.liferay.portal.kernel.util.StringUtil_IW;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.model.ModelHintsUtil;
34 import com.liferay.portal.tools.SourceFormatter;
35 import com.liferay.portal.util.InitUtil;
36 import com.liferay.portal.util.PropsValues;
37 import com.liferay.util.TextFormatter;
38 import com.liferay.portal.kernel.util.Time;
39 import com.liferay.portal.kernel.xml.Document;
40 import com.liferay.portal.kernel.xml.Element;
41 import com.liferay.portal.kernel.xml.SAXReaderUtil;
42 import com.liferay.util.log4j.Log4JUtil;
43 import com.liferay.util.xml.XMLFormatter;
44
45 import com.thoughtworks.qdox.JavaDocBuilder;
46 import com.thoughtworks.qdox.model.JavaClass;
47 import com.thoughtworks.qdox.model.JavaMethod;
48 import com.thoughtworks.qdox.model.JavaParameter;
49 import com.thoughtworks.qdox.model.Type;
50
51 import de.hunsicker.io.FileFormat;
52 import de.hunsicker.jalopy.Jalopy;
53 import de.hunsicker.jalopy.storage.Convention;
54 import de.hunsicker.jalopy.storage.ConventionKeys;
55 import de.hunsicker.jalopy.storage.Environment;
56
57 import freemarker.ext.beans.BeansWrapper;
58 import freemarker.template.TemplateHashModel;
59 import freemarker.template.TemplateModelException;
60
61 import java.io.BufferedReader;
62 import java.io.File;
63 import java.io.FileNotFoundException;
64 import java.io.FileReader;
65 import java.io.IOException;
66 import java.io.StringReader;
67
68 import java.util.ArrayList;
69 import java.util.Arrays;
70 import java.util.HashMap;
71 import java.util.Iterator;
72 import java.util.LinkedHashSet;
73 import java.util.List;
74 import java.util.Map;
75 import java.util.Properties;
76 import java.util.Set;
77 import java.util.TreeMap;
78 import java.util.TreeSet;
79
80 import org.dom4j.DocumentException;
81
82
93 public class ServiceBuilder {
94
95 public static void main(String[] args) {
96 InitUtil.initWithSpring();
97
98 ServiceBuilder serviceBuilder = null;
99
100 if (args.length == 7) {
101 String fileName = args[0];
102 String hbmFileName = args[1];
103 String modelHintsFileName = args[2];
104 String springFileName = args[3];
105 String springBaseFileName = "";
106 String springDynamicDataSourceFileName = "";
107 String springHibernateFileName = "";
108 String springInfrastructureFileName = "";
109 String apiDir = args[5];
110 String implDir = "src";
111 String jsonFileName = args[6];
112 String remotingFileName = "../tunnel-web/docroot/WEB-INF/remoting-servlet.xml";
113 String sqlDir = "../sql";
114 String sqlFileName = "portal-tables.sql";
115 String sqlIndexesFileName = "indexes.sql";
116 String sqlIndexesPropertiesFileName = "indexes.properties";
117 String sqlSequencesFileName = "sequences.sql";
118 boolean autoNamespaceTables = false;
119 String beanLocatorUtil = "com.liferay.portal.kernel.bean.BeanLocatorUtil";
120 String propsUtil = "com.liferay.portal.util.PropsUtil";
121 String pluginName = "";
122 String testDir = "";
123
124 serviceBuilder = new ServiceBuilder(
125 fileName, hbmFileName, modelHintsFileName, springFileName,
126 springBaseFileName, springDynamicDataSourceFileName,
127 springHibernateFileName, springInfrastructureFileName, apiDir,
128 implDir, jsonFileName, remotingFileName, sqlDir, sqlFileName,
129 sqlIndexesFileName, sqlIndexesPropertiesFileName,
130 sqlSequencesFileName, autoNamespaceTables, beanLocatorUtil,
131 propsUtil, pluginName, testDir);
132 }
133 else if (args.length == 0) {
134 String fileName = System.getProperty("service.input.file");
135 String hbmFileName = System.getProperty("service.hbm.file");
136 String modelHintsFileName = System.getProperty("service.model.hints.file");
137 String springFileName = System.getProperty("service.spring.file");
138 String springBaseFileName = System.getProperty("service.spring.base.file");
139 String springDynamicDataSourceFileName = System.getProperty("service.spring.dynamic.data.source.file");
140 String springHibernateFileName = System.getProperty("service.spring.hibernate.file");
141 String springInfrastructureFileName = System.getProperty("service.spring.infrastructure.file");
142 String apiDir = System.getProperty("service.api.dir");
143 String implDir = System.getProperty("service.impl.dir");
144 String jsonFileName = System.getProperty("service.json.file");
145 String remotingFileName = System.getProperty("service.remoting.file");
146 String sqlDir = System.getProperty("service.sql.dir");
147 String sqlFileName = System.getProperty("service.sql.file");
148 String sqlIndexesFileName = System.getProperty("service.sql.indexes.file");
149 String sqlIndexesPropertiesFileName = System.getProperty("service.sql.indexes.properties.file");
150 String sqlSequencesFileName = System.getProperty("service.sql.sequences.file");
151 boolean autoNamespaceTables = GetterUtil.getBoolean(System.getProperty("service.auto.namespace.tables"));
152 String beanLocatorUtil = System.getProperty("service.bean.locator.util");
153 String propsUtil = System.getProperty("service.props.util");
154 String pluginName = System.getProperty("service.plugin.name");
155 String testDir = System.getProperty("service.test.dir");
156
157 serviceBuilder = new ServiceBuilder(
158 fileName, hbmFileName, modelHintsFileName, springFileName,
159 springBaseFileName, springDynamicDataSourceFileName,
160 springHibernateFileName, springInfrastructureFileName, apiDir,
161 implDir, jsonFileName, remotingFileName, sqlDir, sqlFileName,
162 sqlIndexesFileName, sqlIndexesPropertiesFileName,
163 sqlSequencesFileName, autoNamespaceTables, beanLocatorUtil,
164 propsUtil, pluginName, testDir);
165 }
166
167 if (serviceBuilder == null) {
168 System.out.println(
169 "Please set these required system properties. Sample values are:\n" +
170 "\n" +
171 "\t-Dservice.input.file=${service.file}\n" +
172 "\t-Dservice.hbm.file=src/META-INF/portal-hbm.xml\n" +
173 "\t-Dservice.model.hints.file=src/META-INF/portal-model-hints.xml\n" +
174 "\t-Dservice.spring.file=src/META-INF/portal-spring.xml\n" +
175 "\t-Dservice.api.dir=${project.dir}/portal-service/src\n" +
176 "\t-Dservice.impl.dir=src\n" +
177 "\t-Dservice.json.file=${project.dir}/portal-web/docroot/html/js/liferay/service_unpacked.js\n" +
178 "\t-Dservice.remoting.file=${project.dir}/tunnel-web/docroot/WEB-INF/remoting-servlet.xml\n" +
179 "\t-Dservice.sql.dir=../sql\n" +
180 "\t-Dservice.sql.file=portal-tables.sql\n" +
181 "\t-Dservice.sql.indexes.file=indexes.sql\n" +
182 "\t-Dservice.sql.indexes.properties.file=indexes.properties\n" +
183 "\t-Dservice.sql.sequences.file=sequences.sql\n" +
184 "\t-Dservice.bean.locator.util.package=com.liferay.portal.kernel.bean\n" +
185 "\t-Dservice.props.util.package=com.liferay.portal.util\n" +
186 "\n" +
187 "You can also customize the generated code by overriding the default templates with these optional properties:\n" +
188 "\n" +
189 "\t-Dservice.tpl.bad_column_names=" + _TPL_ROOT + "bad_column_names.txt\n"+
190 "\t-Dservice.tpl.bad_table_names=" + _TPL_ROOT + "bad_table_names.txt\n"+
191 "\t-Dservice.tpl.base_mode_impl=" + _TPL_ROOT + "base_mode_impl.ftl\n"+
192 "\t-Dservice.tpl.copyright.txt=copyright.txt\n"+
193 "\t-Dservice.tpl.ejb_pk=" + _TPL_ROOT + "ejb_pk.ftl\n"+
194 "\t-Dservice.tpl.exception=" + _TPL_ROOT + "exception.ftl\n"+
195 "\t-Dservice.tpl.extended_model=" + _TPL_ROOT + "extended_model.ftl\n"+
196 "\t-Dservice.tpl.extended_model_impl=" + _TPL_ROOT + "extended_model_impl.ftl\n"+
197 "\t-Dservice.tpl.finder=" + _TPL_ROOT + "finder.ftl\n"+
198 "\t-Dservice.tpl.finder_util=" + _TPL_ROOT + "finder_util.ftl\n"+
199 "\t-Dservice.tpl.hbm_xml=" + _TPL_ROOT + "hbm_xml.ftl\n"+
200 "\t-Dservice.tpl.json_js=" + _TPL_ROOT + "json_js.ftl\n"+
201 "\t-Dservice.tpl.json_js_method=" + _TPL_ROOT + "json_js_method.ftl\n"+
202 "\t-Dservice.tpl.model=" + _TPL_ROOT + "model.ftl\n"+
203 "\t-Dservice.tpl.model_hints_xml=" + _TPL_ROOT + "model_hints_xml.ftl\n"+
204 "\t-Dservice.tpl.model_impl=" + _TPL_ROOT + "model_impl.ftl\n"+
205 "\t-Dservice.tpl.model_soap=" + _TPL_ROOT + "model_soap.ftl\n"+
206 "\t-Dservice.tpl.persistence=" + _TPL_ROOT + "persistence.ftl\n"+
207 "\t-Dservice.tpl.persistence_impl=" + _TPL_ROOT + "persistence_impl.ftl\n"+
208 "\t-Dservice.tpl.persistence_util=" + _TPL_ROOT + "persistence_util.ftl\n"+
209 "\t-Dservice.tpl.props=" + _TPL_ROOT + "props.ftl\n"+
210 "\t-Dservice.tpl.remoting_xml=" + _TPL_ROOT + "remoting_xml.ftl\n"+
211 "\t-Dservice.tpl.service=" + _TPL_ROOT + "service.ftl\n"+
212 "\t-Dservice.tpl.service_base_impl=" + _TPL_ROOT + "service_base_impl.ftl\n"+
213 "\t-Dservice.tpl.service_factory=" + _TPL_ROOT + "service_factory.ftl\n"+
214 "\t-Dservice.tpl.service_http=" + _TPL_ROOT + "service_http.ftl\n"+
215 "\t-Dservice.tpl.service_impl=" + _TPL_ROOT + "service_impl.ftl\n"+
216 "\t-Dservice.tpl.service_json=" + _TPL_ROOT + "service_json.ftl\n"+
217 "\t-Dservice.tpl.service_json_serializer=" + _TPL_ROOT + "service_json_serializer.ftl\n"+
218 "\t-Dservice.tpl.service_soap=" + _TPL_ROOT + "service_soap.ftl\n"+
219 "\t-Dservice.tpl.service_util=" + _TPL_ROOT + "service_util.ftl\n"+
220 "\t-Dservice.tpl.spring_base_xml=" + _TPL_ROOT + "spring_base_xml.ftl\n"+
221 "\t-Dservice.tpl.spring_dynamic_data_source_xml=" + _TPL_ROOT + "spring_dynamic_data_source_xml.ftl\n"+
222 "\t-Dservice.tpl.spring_hibernate_xml=" + _TPL_ROOT + "spring_hibernate_xml.ftl\n"+
223 "\t-Dservice.tpl.spring_infrastructure_xml=" + _TPL_ROOT + "spring_infrastructure_xml.ftl\n"+
224 "\t-Dservice.tpl.spring_xml=" + _TPL_ROOT + "spring_xml.ftl\n"+
225 "\t-Dservice.tpl.spring_xml_session=" + _TPL_ROOT + "spring_xml_session.ftl");
226 }
227 }
228
229 public static void writeFile(File file, String content)
230 throws IOException {
231
232 writeFile(file, content, _AUTHOR);
233 }
234
235 public static void writeFile(File file, String content, String author)
236 throws IOException {
237
238 writeFile(file, content, author, null);
239 }
240
241 public static void writeFile(
242 File file, String content, String author,
243 Map<String, Object> jalopySettings)
244 throws IOException {
245
246 String packagePath = _getPackagePath(file);
247
248 String className = file.getName();
249
250 className = className.substring(0, className.length() - 5);
251
252 content = SourceFormatter.stripImports(content, packagePath, className);
253
254 File tempFile = new File("ServiceBuilder.temp");
255
256 FileUtil.write(tempFile, content);
257
258
260 StringBuffer sb = new StringBuffer();
261
262 Jalopy jalopy = new Jalopy();
263
264 jalopy.setFileFormat(FileFormat.UNIX);
265 jalopy.setInput(tempFile);
266 jalopy.setOutput(sb);
267
268 try {
269 Jalopy.setConvention("../tools/jalopy.xml");
270 }
271 catch (FileNotFoundException fnne) {
272 }
273
274 try {
275 Jalopy.setConvention("../../misc/jalopy.xml");
276 }
277 catch (FileNotFoundException fnne) {
278 }
279
280 if (jalopySettings == null) {
281 jalopySettings = new HashMap<String, Object>();
282 }
283
284 Environment env = Environment.getInstance();
285
286
288 author = GetterUtil.getString(
289 (String)jalopySettings.get("author"), author);
290
291 env.set("author", author);
292
293
295 env.set("fileName", file.getName());
296
297 Convention convention = Convention.getInstance();
298
299 String classMask =
300 "/**\n" +
301 " * <a href=\"$fileName$.html\"><b><i>View Source</i></b></a>\n" +
302 " *\n" +
303 " * @author $author$\n" +
304 " *\n" +
305 "*/";
306
307 convention.put(
308 ConventionKeys.COMMENT_JAVADOC_TEMPLATE_CLASS,
309 env.interpolate(classMask));
310
311 convention.put(
312 ConventionKeys.COMMENT_JAVADOC_TEMPLATE_INTERFACE,
313 env.interpolate(classMask));
314
315 jalopy.format();
316
317 String newContent = sb.toString();
318
319
336
337
339 String oldContent = null;
340
341 if (file.exists()) {
342
343
345 oldContent = FileUtil.read(file);
346
347
349 int x = oldContent.indexOf("@version $Revision:");
350
351 if (x != -1) {
352 int y = oldContent.indexOf("$", x);
353 y = oldContent.indexOf("$", y + 1);
354
355 String oldVersion = oldContent.substring(x, y + 1);
356
357 newContent = StringUtil.replace(
358 newContent, "@version $Rev: $", oldVersion);
359 }
360 }
361 else {
362 newContent = StringUtil.replace(
363 newContent, "@version $Rev: $", "@version $Revision: 1.183 $");
364 }
365
366 if (oldContent == null || !oldContent.equals(newContent)) {
367 FileUtil.write(file, newContent);
368
369 System.out.println("Writing " + file);
370
371
373 file.setLastModified(
374 System.currentTimeMillis() - (Time.SECOND * 5));
375 }
376
377 tempFile.deleteOnExit();
378 }
379
380 public ServiceBuilder(
381 String fileName, String hbmFileName, String modelHintsFileName,
382 String springFileName, String springBaseFileName,
383 String springDynamicDataSourceFileName, String springHibernateFileName,
384 String springInfrastructureFileName, String apiDir, String implDir,
385 String jsonFileName, String remotingFileName, String sqlDir,
386 String sqlFileName, String sqlIndexesFileName,
387 String sqlIndexesPropertiesFileName, String sqlSequencesFileName,
388 boolean autoNamespaceTables, String beanLocatorUtil, String propsUtil,
389 String pluginName, String testDir) {
390
391 new ServiceBuilder(
392 fileName, hbmFileName, modelHintsFileName, springFileName,
393 springBaseFileName, springDynamicDataSourceFileName,
394 springHibernateFileName, springInfrastructureFileName, apiDir,
395 implDir, jsonFileName, remotingFileName, sqlDir, sqlFileName,
396 sqlIndexesFileName, sqlIndexesPropertiesFileName,
397 sqlSequencesFileName, autoNamespaceTables, beanLocatorUtil,
398 propsUtil, pluginName, testDir, true);
399 }
400
401 public ServiceBuilder(
402 String fileName, String hbmFileName, String modelHintsFileName,
403 String springFileName, String springBaseFileName,
404 String springDynamicDataSourceFileName, String springHibernateFileName,
405 String springInfrastructureFileName, String apiDir, String implDir,
406 String jsonFileName, String remotingFileName, String sqlDir,
407 String sqlFileName, String sqlIndexesFileName,
408 String sqlIndexesPropertiesFileName, String sqlSequencesFileName,
409 boolean autoNamespaceTables, String beanLocatorUtil, String propsUtil,
410 String pluginName, String testDir, boolean build) {
411
412 _tplBadColumnNames = _getTplProperty(
413 "bad_column_names", _tplBadColumnNames);
414 _tplBadTableNames = _getTplProperty(
415 "bad_table_names", _tplBadTableNames);
416 _tplEjbPk = _getTplProperty("ejb_pk", _tplEjbPk);
417 _tplException = _getTplProperty("exception", _tplException);
418 _tplExtendedModel = _getTplProperty(
419 "extended_model", _tplExtendedModel);
420 _tplExtendedModelImpl = _getTplProperty(
421 "extended_model_impl", _tplExtendedModelImpl);
422 _tplFinder = _getTplProperty("finder", _tplFinder);
423 _tplFinderUtil = _getTplProperty("finder_util", _tplFinderUtil);
424 _tplHbmXml = _getTplProperty("hbm_xml", _tplHbmXml);
425 _tplJsonJs = _getTplProperty("json_js", _tplJsonJs);
426 _tplJsonJsMethod = _getTplProperty("json_js_method", _tplJsonJsMethod);
427 _tplModel = _getTplProperty("model", _tplModel);
428 _tplModelClp = _getTplProperty("model", _tplModelClp);
429 _tplModelHintsXml = _getTplProperty(
430 "model_hints_xml", _tplModelHintsXml);
431 _tplModelImpl = _getTplProperty("model_impl", _tplModelImpl);
432 _tplModelSoap = _getTplProperty("model_soap", _tplModelSoap);
433 _tplPersistence = _getTplProperty("persistence", _tplPersistence);
434 _tplPersistenceImpl = _getTplProperty(
435 "persistence_impl", _tplPersistenceImpl);
436 _tplPersistenceUtil = _getTplProperty(
437 "persistence_util", _tplPersistenceUtil);
438 _tplProps = _getTplProperty("props", _tplProps);
439 _tplRemotingXml = _getTplProperty("remoting_xml", _tplRemotingXml);
440 _tplService = _getTplProperty("service", _tplService);
441 _tplServiceBaseImpl = _getTplProperty(
442 "service_base_impl", _tplServiceBaseImpl);
443 _tplServiceClp = _getTplProperty("service_clp", _tplServiceClp);
444 _tplServiceClpSerializer = _getTplProperty(
445 "service_clp_serializer", _tplServiceClpSerializer);
446 _tplServiceFactory = _getTplProperty(
447 "service_factory", _tplServiceFactory);
448 _tplServiceHttp = _getTplProperty("service_http", _tplServiceHttp);
449 _tplServiceImpl = _getTplProperty("service_impl", _tplServiceImpl);
450 _tplServiceJson = _getTplProperty("service_json", _tplServiceJson);
451 _tplServiceJsonSerializer = _getTplProperty(
452 "service_json_serializer", _tplServiceJsonSerializer);
453 _tplServiceSoap = _getTplProperty("service_soap", _tplServiceSoap);
454 _tplServiceUtil = _getTplProperty("service_util", _tplServiceUtil);
455 _tplSpringBaseXml = _getTplProperty(
456 "spring_base_xml", _tplSpringBaseXml);
457 _tplSpringDynamicDataSourceXml = _getTplProperty(
458 "spring_dynamic_data_source_xml", _tplSpringDynamicDataSourceXml);
459 _tplSpringHibernateXml = _getTplProperty(
460 "spring_hibernate_xml", _tplSpringHibernateXml);
461 _tplSpringInfrastructureXml = _getTplProperty(
462 "spring_infrastructure_xml", _tplSpringInfrastructureXml);
463 _tplSpringXml = _getTplProperty("spring_xml", _tplSpringXml);
464
465 try {
466 _badTableNames = SetUtil.fromString(StringUtil.read(
467 getClass().getClassLoader(), _tplBadTableNames));
468 _badColumnNames = SetUtil.fromString(StringUtil.read(
469 getClass().getClassLoader(), _tplBadColumnNames));
470 _hbmFileName = hbmFileName;
471 _modelHintsFileName = modelHintsFileName;
472 _springFileName = springFileName;
473 _springBaseFileName = springBaseFileName;
474 _springDynamicDataSourceFileName = springDynamicDataSourceFileName;
475 _springHibernateFileName = springHibernateFileName;
476 _springInfrastructureFileName = springInfrastructureFileName;
477 _apiDir = apiDir;
478 _implDir = implDir;
479 _jsonFileName = jsonFileName;
480 _remotingFileName = remotingFileName;
481 _sqlDir = sqlDir;
482 _sqlFileName = sqlFileName;
483 _sqlIndexesFileName = sqlIndexesFileName;
484 _sqlIndexesPropertiesFileName = sqlIndexesPropertiesFileName;
485 _sqlSequencesFileName = sqlSequencesFileName;
486 _autoNamespaceTables = autoNamespaceTables;
487 _beanLocatorUtil = beanLocatorUtil;
488 _beanLocatorUtilShortName = _beanLocatorUtil.substring(
489 _beanLocatorUtil.lastIndexOf(".") + 1);
490 _propsUtil = propsUtil;
491 _pluginName = GetterUtil.getString(pluginName);
492 _testDir = testDir;
493
494 Document doc = SAXReaderUtil.read(new File(fileName), true);
495
496 Element root = doc.getRootElement();
497
498 String packagePath = root.attributeValue("package-path");
499
500 _outputPath =
501 _implDir + "/" + StringUtil.replace(packagePath, ".", "/");
502
503 _serviceOutputPath =
504 _apiDir + "/" + StringUtil.replace(packagePath, ".", "/");
505
506 if (Validator.isNotNull(_testDir)) {
507 _testOutputPath =
508 _testDir + "/" + StringUtil.replace(packagePath, ".", "/");
509 }
510
511 _packagePath = packagePath;
512
513 Element author = root.element("author");
514
515 if (author != null) {
516 _author = author.getText();
517 }
518 else {
519 _author = _AUTHOR;
520 }
521
522 Element portlet = root.element("portlet");
523 Element namespace = root.element("namespace");
524
525 if (portlet != null) {
526 _portletName = portlet.attributeValue("name");
527
528 _portletShortName = portlet.attributeValue("short-name");
529
530 _portletPackageName =
531 TextFormatter.format(_portletName, TextFormatter.B);
532
533 _outputPath += "/" + _portletPackageName;
534
535 _serviceOutputPath += "/" + _portletPackageName;
536
537 _testOutputPath += "/" + _portletPackageName;
538
539 _packagePath += "." + _portletPackageName;
540 }
541 else {
542 _portletShortName = namespace.getText();
543 }
544
545 _portletShortName = _portletShortName.trim();
546
547 if (!Validator.isChar(_portletShortName)) {
548 throw new RuntimeException(
549 "The namespace element must be a valid keyword");
550 }
551
552 _ejbList = new ArrayList<Entity>();
553 _entityMappings = new HashMap<String, EntityMapping>();
554
555 List<Element> entities = root.elements("entity");
556
557 Iterator<Element> itr1 = entities.iterator();
558
559 while (itr1.hasNext()) {
560 Element entityEl = itr1.next();
561
562 String ejbName = entityEl.attributeValue("name");
563
564 String table = entityEl.attributeValue("table");
565
566 if (Validator.isNull(table)) {
567 table = ejbName;
568
569 if (_badTableNames.contains(ejbName)) {
570 table += StringPool.UNDERLINE;
571 }
572
573 if (_autoNamespaceTables) {
574 table =
575 _portletShortName + StringPool.UNDERLINE + ejbName;
576 }
577 }
578
579 boolean uuid = GetterUtil.getBoolean(
580 entityEl.attributeValue("uuid"), false);
581 boolean localService = GetterUtil.getBoolean(
582 entityEl.attributeValue("local-service"), false);
583 boolean remoteService = GetterUtil.getBoolean(
584 entityEl.attributeValue("remote-service"), true);
585 String persistenceClass = GetterUtil.getString(
586 entityEl.attributeValue("persistence-class"),
587 _packagePath + ".service.persistence." + ejbName +
588 "PersistenceImpl");
589
590 String finderClass = "";
591
592 if (FileUtil.exists(
593 _outputPath + "/service/persistence/" + ejbName +
594 "FinderImpl.java")) {
595
596 finderClass =
597 _packagePath + ".service.persistence." + ejbName +
598 "FinderImpl";
599 }
600
601 String dataSource = entityEl.attributeValue("data-source");
602 String sessionFactory = entityEl.attributeValue(
603 "session-factory");
604 String txManager = entityEl.attributeValue(
605 "tx-manager");
606 boolean cacheEnabled = GetterUtil.getBoolean(
607 entityEl.attributeValue("cache-enabled"), true);
608
609 List<EntityColumn> pkList = new ArrayList<EntityColumn>();
610 List<EntityColumn> regularColList =
611 new ArrayList<EntityColumn>();
612 List<EntityColumn> collectionList =
613 new ArrayList<EntityColumn>();
614 List<EntityColumn> columnList = new ArrayList<EntityColumn>();
615
616 List<Element> columns = entityEl.elements("column");
617
618 if (uuid) {
619 Element column = SAXReaderUtil.createElement("column");
620
621 column.addAttribute("name", "uuid");
622 column.addAttribute("type", "String");
623
624 columns.add(0, column);
625 }
626
627 Iterator<Element> itr2 = columns.iterator();
628
629 while (itr2.hasNext()) {
630 Element column = itr2.next();
631
632 String columnName = column.attributeValue("name");
633
634 String columnDBName = column.attributeValue("db-name");
635
636 if (Validator.isNull(columnDBName)) {
637 columnDBName = columnName;
638
639 if (_badColumnNames.contains(columnName)) {
640 columnDBName += StringPool.UNDERLINE;
641 }
642 }
643
644 String columnType = column.attributeValue("type");
645 boolean primary = GetterUtil.getBoolean(
646 column.attributeValue("primary"), false);
647 String collectionEntity = column.attributeValue("entity");
648 String mappingKey = column.attributeValue("mapping-key");
649
650 String mappingTable = column.attributeValue(
651 "mapping-table");
652
653 if (Validator.isNotNull(mappingTable)) {
654 if (_badTableNames.contains(mappingTable)) {
655 mappingTable += StringPool.UNDERLINE;
656 }
657
658 if (_autoNamespaceTables) {
659 mappingTable =
660 _portletShortName + StringPool.UNDERLINE +
661 mappingTable;
662 }
663 }
664
665 String idType = column.attributeValue("id-type");
666 String idParam = column.attributeValue("id-param");
667 boolean convertNull = GetterUtil.getBoolean(
668 column.attributeValue("convert-null"), true);
669
670 EntityColumn col = new EntityColumn(
671 columnName, columnDBName, columnType, primary,
672 collectionEntity, mappingKey, mappingTable, idType,
673 idParam, convertNull);
674
675 if (primary) {
676 pkList.add(col);
677 }
678
679 if (columnType.equals("Collection")) {
680 collectionList.add(col);
681 }
682 else {
683 regularColList.add(col);
684 }
685
686 columnList.add(col);
687
688 if (Validator.isNotNull(collectionEntity) &&
689 Validator.isNotNull(mappingTable)) {
690
691 EntityMapping entityMapping = new EntityMapping(
692 mappingTable, ejbName, collectionEntity);
693
694 int ejbNameWeight = StringUtil.startsWithWeight(
695 mappingTable, ejbName);
696 int collectionEntityWeight =
697 StringUtil.startsWithWeight(
698 mappingTable, collectionEntity);
699
700 if ((ejbNameWeight > collectionEntityWeight) ||
701 ((ejbNameWeight == collectionEntityWeight) &&
702 (ejbName.compareTo(collectionEntity) > 0))) {
703
704 _entityMappings.put(mappingTable, entityMapping);
705 }
706 }
707 }
708
709 EntityOrder order = null;
710
711 Element orderEl = entityEl.element("order");
712
713 if (orderEl != null) {
714 boolean asc = true;
715
716 if ((orderEl.attribute("by") != null) &&
717 (orderEl.attributeValue("by").equals("desc"))) {
718
719 asc = false;
720 }
721
722 List<EntityColumn> orderColsList =
723 new ArrayList<EntityColumn>();
724
725 order = new EntityOrder(asc, orderColsList);
726
727 List<Element> orderCols = orderEl.elements("order-column");
728
729 Iterator<Element> itr3 = orderCols.iterator();
730
731 while (itr3.hasNext()) {
732 Element orderColEl = itr3.next();
733
734 String orderColName =
735 orderColEl.attributeValue("name");
736 boolean orderColCaseSensitive = GetterUtil.getBoolean(
737 orderColEl.attributeValue("case-sensitive"),
738 true);
739
740 boolean orderColByAscending = asc;
741
742 String orderColBy = GetterUtil.getString(
743 orderColEl.attributeValue("order-by"));
744
745 if (orderColBy.equals("asc")) {
746 orderColByAscending = true;
747 }
748 else if (orderColBy.equals("desc")) {
749 orderColByAscending = false;
750 }
751
752 EntityColumn col = Entity.getColumn(
753 orderColName, columnList);
754
755 col = (EntityColumn)col.clone();
756
757 col.setCaseSensitive(orderColCaseSensitive);
758 col.setOrderByAscending(orderColByAscending);
759
760 orderColsList.add(col);
761 }
762 }
763
764 List<EntityFinder> finderList = new ArrayList<EntityFinder>();
765
766 List<Element> finders = entityEl.elements("finder");
767
768 if (uuid) {
769 Element finderEl = SAXReaderUtil.createElement("finder");
770
771 finderEl.addAttribute("name", "Uuid");
772 finderEl.addAttribute("return-type", "Collection");
773
774 Element finderColEl = finderEl.addElement("finder-column");
775
776 finderColEl.addAttribute("name", "uuid");
777
778 finders.add(0, finderEl);
779
780 if (columnList.contains(new EntityColumn("groupId"))) {
781 finderEl = SAXReaderUtil.createElement("finder");
782
783 finderEl.addAttribute("name", "UUID_G");
784 finderEl.addAttribute("return-type", ejbName);
785
786 finderColEl = finderEl.addElement("finder-column");
787
788 finderColEl.addAttribute("name", "uuid");
789
790 finderColEl = finderEl.addElement("finder-column");
791
792 finderColEl.addAttribute("name", "groupId");
793
794 finders.add(1, finderEl);
795 }
796 }
797
798 itr2 = finders.iterator();
799
800 while (itr2.hasNext()) {
801 Element finderEl = itr2.next();
802
803 String finderName = finderEl.attributeValue("name");
804 String finderReturn =
805 finderEl.attributeValue("return-type");
806 String finderWhere =
807 finderEl.attributeValue("where");
808 boolean finderDBIndex = GetterUtil.getBoolean(
809 finderEl.attributeValue("db-index"), true);
810
811 List<EntityColumn> finderColsList =
812 new ArrayList<EntityColumn>();
813
814 List<Element> finderCols = finderEl.elements(
815 "finder-column");
816
817 Iterator<Element> itr3 = finderCols.iterator();
818
819 while (itr3.hasNext()) {
820 Element finderColEl = itr3.next();
821
822 String finderColName =
823 finderColEl.attributeValue("name");
824
825 boolean finderColCaseSensitive = GetterUtil.getBoolean(
826 finderColEl.attributeValue("case-sensitive"),
827 true);
828
829 String finderColComparator = GetterUtil.getString(
830 finderColEl.attributeValue("comparator"), "=");
831
832 EntityColumn col = Entity.getColumn(
833 finderColName, columnList);
834
835 col = (EntityColumn)col.clone();
836
837 col.setCaseSensitive(finderColCaseSensitive);
838 col.setComparator(finderColComparator);
839
840 finderColsList.add(col);
841 }
842
843 finderList.add(
844 new EntityFinder(
845 finderName, finderReturn, finderColsList,
846 finderWhere, finderDBIndex));
847 }
848
849 List<Entity> referenceList = new ArrayList<Entity>();
850
851 if (build) {
852 List<Element> references = entityEl.elements("reference");
853
854 itr2 = references.iterator();
855
856 while (itr2.hasNext()) {
857 Element reference = itr2.next();
858
859 String refPackage =
860 reference.attributeValue("package-path");
861 String refEntity = reference.attributeValue("entity");
862
863 referenceList.add(
864 getEntity(refPackage + "." + refEntity));
865 }
866 }
867
868 List<String> txRequiredList = new ArrayList<String>();
869
870 itr2 = entityEl.elements("tx-required").iterator();
871
872 while (itr2.hasNext()) {
873 Element txRequiredEl = itr2.next();
874
875 String txRequired = txRequiredEl.getText();
876
877 txRequiredList.add(txRequired);
878 }
879
880 _ejbList.add(
881 new Entity(
882 _packagePath, _portletName, _portletShortName, ejbName,
883 table, uuid, localService, remoteService,
884 persistenceClass, finderClass, dataSource,
885 sessionFactory, txManager, cacheEnabled, pkList,
886 regularColList, collectionList, columnList, order,
887 finderList, referenceList, txRequiredList));
888 }
889
890 List<String> exceptionList = new ArrayList<String>();
891
892 if (root.element("exceptions") != null) {
893 List<Element> exceptions =
894 root.element("exceptions").elements("exception");
895
896 itr1 = exceptions.iterator();
897
898 while (itr1.hasNext()) {
899 Element exception = itr1.next();
900
901 exceptionList.add(exception.getText());
902 }
903 }
904
905 if (build) {
906 for (int x = 0; x < _ejbList.size(); x++) {
907 Entity entity = _ejbList.get(x);
908
909 System.out.println("Building " + entity.getName());
910
911 if (true ||
912 entity.getName().equals("EmailAddress") ||
913 entity.getName().equals("User")) {
914
915 if (entity.hasColumns()) {
916 _createHbm(entity);
917 _createHbmUtil(entity);
918
919 _createPersistenceImpl(entity);
920 _createPersistence(entity);
921 _createPersistenceUtil(entity);
922
923 if (Validator.isNotNull(_testDir)) {
924 _createPersistenceTest(entity);
925 }
926
927 _createModelImpl(entity);
928 _createExtendedModelImpl(entity);
929
930 _createModel(entity);
931 _createExtendedModel(entity);
932
933 _createModelSoap(entity);
934
935 _createModelClp(entity);
936
937 _createPool(entity);
938
939 if (entity.getPKList().size() > 1) {
940 _createEJBPK(entity);
941 }
942 }
943
944 _createFinder(entity);
945 _createFinderUtil(entity);
946
947 if (entity.hasLocalService()) {
948 _createServiceBaseImpl(entity, _SESSION_TYPE_LOCAL);
949 _createServiceImpl(entity, _SESSION_TYPE_LOCAL);
950 _createService(entity, _SESSION_TYPE_LOCAL);
951 _createServiceFactory(entity, _SESSION_TYPE_LOCAL);
952 _createServiceUtil(entity, _SESSION_TYPE_LOCAL);
953
954 _createServiceClp(entity, _SESSION_TYPE_LOCAL);
955 }
956
957 if (entity.hasRemoteService()) {
958 _createServiceBaseImpl(
959 entity, _SESSION_TYPE_REMOTE);
960 _createServiceImpl(entity, _SESSION_TYPE_REMOTE);
961 _createService(entity, _SESSION_TYPE_REMOTE);
962 _createServiceFactory(entity, _SESSION_TYPE_REMOTE);
963 _createServiceUtil(entity, _SESSION_TYPE_REMOTE);
964
965 _createServiceClp(entity, _SESSION_TYPE_REMOTE);
966
967 if (Validator.isNotNull(_jsonFileName)) {
968 _createServiceHttp(entity);
969 _createServiceJson(entity);
970
971 if (entity.hasColumns()) {
972 _createServiceJsonSerializer(entity);
973 }
974
975 _createServiceSoap(entity);
976 }
977 }
978 }
979 }
980
981 _createHbmXml();
982 _createModelHintsXml();
983 _createSpringXml();
984
985 _createServiceClpSerializer();
986
987 if (Validator.isNotNull(_jsonFileName)) {
988 _createJsonJs();
989 }
990
991 if (Validator.isNotNull(_remotingFileName)) {
992 _createRemotingXml();
993 }
994
995 _createSQLIndexes();
996 _createSQLTables();
997 _createSQLSequences();
998
999 _createExceptions(exceptionList);
1000
1001 _createProps();
1002 _createSpringBaseXml();
1003 _createSpringDynamicDataSourceXml();
1004 _createSpringHibernateXml();
1005 _createSpringInfrastructureXml();
1006 }
1007 }
1008 catch (Exception e) {
1009 e.printStackTrace();
1010 }
1011 }
1012
1013 public String getClassName(Type type) {
1014 int dimensions = type.getDimensions();
1015 String name = type.getValue();
1016
1017 if (dimensions > 0) {
1018 StringBuilder sb = new StringBuilder();
1019
1020 for (int i = 0; i < dimensions; i++) {
1021 sb.append("[");
1022 }
1023
1024 if (name.equals("boolean")) {
1025 return sb.toString() + "Z";
1026 }
1027 else if (name.equals("byte")) {
1028 return sb.toString() + "B";
1029 }
1030 else if (name.equals("char")) {
1031 return sb.toString() + "C";
1032 }
1033 else if (name.equals("double")) {
1034 return sb.toString() + "D";
1035 }
1036 else if (name.equals("float")) {
1037 return sb.toString() + "F";
1038 }
1039 else if (name.equals("int")) {
1040 return sb.toString() + "I";
1041 }
1042 else if (name.equals("long")) {
1043 return sb.toString() + "J";
1044 }
1045 else if (name.equals("short")) {
1046 return sb.toString() + "S";
1047 }
1048 else {
1049 return sb.toString() + "L" + name + ";";
1050 }
1051 }
1052
1053 return name;
1054 }
1055
1056 public String getCreateTableSQL(Entity entity) {
1057 String createTableSQL = _getCreateTableSQL(entity);
1058
1059 createTableSQL = StringUtil.replace(createTableSQL, "\n", "");
1060 createTableSQL = StringUtil.replace(createTableSQL, "\t", "");
1061 createTableSQL = createTableSQL.substring(
1062 0, createTableSQL.length() - 1);
1063
1064 return createTableSQL;
1065 }
1066
1067 public String getDimensions(String dims) {
1068 return getDimensions(Integer.parseInt(dims));
1069 }
1070
1071 public String getDimensions(int dims) {
1072 String dimensions = "";
1073
1074 for (int i = 0; i < dims; i++) {
1075 dimensions += "[]";
1076 }
1077
1078 return dimensions;
1079 }
1080
1081 public Entity getEntity(String name) throws IOException {
1082 Entity entity = _entityPool.get(name);
1083
1084 if (entity != null) {
1085 return entity;
1086 }
1087
1088 int pos = name.lastIndexOf(".");
1089
1090 if (pos == -1) {
1091 pos = _ejbList.indexOf(new Entity(name));
1092
1093 entity = _ejbList.get(pos);
1094
1095 _entityPool.put(name, entity);
1096
1097 return entity;
1098 }
1099 else {
1100 String refPackage = name.substring(0, pos);
1101 String refPackageDir = StringUtil.replace(refPackage, ".", "/");
1102 String refEntity = name.substring(pos + 1, name.length());
1103 String refFileName =
1104 _implDir + "/" + refPackageDir + "/service.xml";
1105
1106 File refFile = new File(refFileName);
1107
1108 boolean useTempFile = false;
1109
1110 if (!refFile.exists()) {
1111 refFileName = Time.getTimestamp();
1112 refFile = new File(refFileName);
1113
1114 ClassLoader classLoader = getClass().getClassLoader();
1115
1116 FileUtil.write(
1117 refFileName,
1118 StringUtil.read(
1119 classLoader, refPackageDir + "/service.xml"));
1120
1121 useTempFile = true;
1122 }
1123
1124 ServiceBuilder serviceBuilder = new ServiceBuilder(
1125 refFileName, _hbmFileName, _modelHintsFileName, _springFileName,
1126 _springBaseFileName, _springDynamicDataSourceFileName,
1127 _springHibernateFileName, _springInfrastructureFileName,
1128 _apiDir, _implDir, _jsonFileName, _remotingFileName, _sqlDir,
1129 _sqlFileName, _sqlIndexesFileName,
1130 _sqlIndexesPropertiesFileName, _sqlSequencesFileName,
1131 _autoNamespaceTables, _beanLocatorUtil, _propsUtil, _pluginName,
1132 _testDir, false);
1133
1134 entity = serviceBuilder.getEntity(refEntity);
1135
1136 entity.setPortalReference(useTempFile);
1137
1138 _entityPool.put(name, entity);
1139
1140 if (useTempFile) {
1141 refFile.deleteOnExit();
1142 }
1143
1144 return entity;
1145 }
1146 }
1147
1148 public Entity getEntityByGenericsName(String genericsName) {
1149 try {
1150 String name = genericsName.substring(1, genericsName.length() - 1);
1151
1152 name = StringUtil.replace(name, ".model.", ".");
1153
1154 return getEntity(name);
1155 }
1156 catch (Exception e) {
1157 return null;
1158 }
1159 }
1160
1161 public Entity getEntityByParameterTypeValue(String parameterTypeValue) {
1162 try {
1163 String name = parameterTypeValue;
1164
1165 name = StringUtil.replace(name, ".model.", ".");
1166
1167 return getEntity(name);
1168 }
1169 catch (Exception e) {
1170 return null;
1171 }
1172 }
1173
1174 public String getGeneratorClass(String idType) {
1175 if (Validator.isNull(idType)) {
1176 idType = "assigned";
1177 }
1178
1179 return idType;
1180 }
1181
1182 public String getNoSuchEntityException(Entity entity) {
1183 String noSuchEntityException = entity.getName();
1184
1185 if (Validator.isNull(entity.getPortletShortName()) ||
1186 noSuchEntityException.startsWith(entity.getPortletShortName()) &&
1187 !noSuchEntityException.equals(entity.getPortletShortName())) {
1188
1189 noSuchEntityException = noSuchEntityException.substring(
1190 entity.getPortletShortName().length());
1191 }
1192
1193 noSuchEntityException = "NoSuch" + noSuchEntityException;
1194
1195 return noSuchEntityException;
1196 }
1197
1198 public String getPrimitiveObj(String type) {
1199 if (type.equals("boolean")) {
1200 return "Boolean";
1201 }
1202 else if (type.equals("double")) {
1203 return "Double";
1204 }
1205 else if (type.equals("float")) {
1206 return "Float";
1207 }
1208 else if (type.equals("int")) {
1209 return "Integer";
1210 }
1211 else if (type.equals("long")) {
1212 return "Long";
1213 }
1214 else if (type.equals("short")) {
1215 return "Short";
1216 }
1217 else {
1218 return type;
1219 }
1220 }
1221
1222 public String getPrimitiveObjValue(String colType) {
1223 if (colType.equals("Boolean")) {
1224 return ".booleanValue()";
1225 }
1226 else if (colType.equals("Double")) {
1227 return ".doubleValue()";
1228 }
1229 else if (colType.equals("Float")) {
1230 return ".floatValue()";
1231 }
1232 else if (colType.equals("Integer")) {
1233 return ".intValue()";
1234 }
1235 else if (colType.equals("Long")) {
1236 return ".longValue()";
1237 }
1238 else if (colType.equals("Short")) {
1239 return ".shortValue()";
1240 }
1241
1242 return StringPool.BLANK;
1243 }
1244
1245 public String getSqlType(String model, String field, String type) {
1246 if (type.equals("boolean") || type.equals("Boolean")) {
1247 return "BOOLEAN";
1248 }
1249 else if (type.equals("double") || type.equals("Double")) {
1250 return "DOUBLE";
1251 }
1252 else if (type.equals("float") || type.equals("Float")) {
1253 return "FLOAT";
1254 }
1255 else if (type.equals("int") || type.equals("Integer")) {
1256 return "INTEGER";
1257 }
1258 else if (type.equals("long") || type.equals("Long")) {
1259 return "BIGINT";
1260 }
1261 else if (type.equals("short") || type.equals("Short")) {
1262 return "INTEGER";
1263 }
1264 else if (type.equals("Date")) {
1265 return "TIMESTAMP";
1266 }
1267 else if (type.equals("String")) {
1268 Map<String, String> hints = ModelHintsUtil.getHints(model, field);
1269
1270 if (hints != null) {
1271 int maxLength = GetterUtil.getInteger(hints.get("max-length"));
1272
1273 if (maxLength == 2000000) {
1274 return "CLOB";
1275 }
1276 }
1277
1278 return "VARCHAR";
1279 }
1280 else {
1281 return null;
1282 }
1283 }
1284
1285 public boolean hasEntityByGenericsName(String genericsName) {
1286 if (Validator.isNull(genericsName)) {
1287 return false;
1288 }
1289
1290 if (genericsName.indexOf(".model.") == -1) {
1291 return false;
1292 }
1293
1294 if (getEntityByGenericsName(genericsName) == null) {
1295 return false;
1296 }
1297 else {
1298 return true;
1299 }
1300 }
1301
1302 public boolean hasEntityByParameterTypeValue(String parameterTypeValue) {
1303 if (Validator.isNull(parameterTypeValue)) {
1304 return false;
1305 }
1306
1307 if (parameterTypeValue.indexOf(".model.") == -1) {
1308 return false;
1309 }
1310
1311 if (getEntityByParameterTypeValue(parameterTypeValue) == null) {
1312 return false;
1313 }
1314 else {
1315 return true;
1316 }
1317 }
1318
1319 public boolean isCustomMethod(JavaMethod method) {
1320 String methodName = method.getName();
1321
1322 if (methodName.equals("afterPropertiesSet") ||
1323 methodName.equals("equals") ||
1324 methodName.equals("getClass") ||
1325 methodName.equals("hashCode") ||
1326 methodName.equals("notify") ||
1327 methodName.equals("notifyAll") ||
1328 methodName.equals("toString") ||
1329 methodName.equals("wait")) {
1330
1331 return false;
1332 }
1333 else if (methodName.equals("getPermissionChecker")) {
1334 return false;
1335 }
1336 else if (methodName.equals("getUser") &&
1337 method.getParameters().length == 0) {
1338
1339 return false;
1340 }
1341 else if (methodName.equals("getUserId") &&
1342 method.getParameters().length == 0) {
1343
1344 return false;
1345 }
1346 else if ((methodName.endsWith("Finder")) &&
1347 (methodName.startsWith("get") ||
1348 methodName.startsWith("set"))) {
1349
1350 return false;
1351 }
1352 else if ((methodName.endsWith("Persistence")) &&
1353 (methodName.startsWith("get") ||
1354 methodName.startsWith("set"))) {
1355
1356 return false;
1357 }
1358 else if ((methodName.endsWith("Service")) &&
1359 (methodName.startsWith("get") ||
1360 methodName.startsWith("set"))) {
1361
1362 return false;
1363 }
1364 else {
1365 return true;
1366 }
1367 }
1368
1369 public boolean isDuplicateMethod(
1370 JavaMethod method, Map<String, Object> tempMap) {
1371
1372 StringBuilder sb = new StringBuilder();
1373
1374 sb.append("isDuplicateMethod ");
1375 sb.append(method.getReturns().getValue());
1376 sb.append(method.getReturnsGenericsName());
1377 sb.append(getDimensions(method.getReturns().getDimensions()));
1378 sb.append(StringPool.SPACE);
1379 sb.append(method.getName());
1380 sb.append(StringPool.OPEN_PARENTHESIS);
1381
1382 JavaParameter[] parameters = method.getParameters();
1383
1384 for (int i = 0; i < parameters.length; i++) {
1385 JavaParameter javaParameter = parameters[i];
1386
1387 sb.append(javaParameter.getType().getValue());
1388 sb.append(javaParameter.getGenericsName());
1389 sb.append(getDimensions(javaParameter.getType().getDimensions()));
1390
1391 if ((i + 1) != parameters.length) {
1392 sb.append(StringPool.COMMA);
1393 }
1394 }
1395
1396 sb.append(StringPool.CLOSE_PARENTHESIS);
1397
1398 String key = sb.toString();
1399
1400 if (tempMap.containsKey(key)) {
1401 return true;
1402 }
1403 else {
1404 tempMap.put(key, key);
1405
1406 return false;
1407 }
1408 }
1409
1410 public boolean isPersistenceReadOnlyMethod(JavaMethod method) {
1411 return isReadOnlyMethod(
1412 method, null,
1413 PropsValues.SERVICE_BUILDER_PERSISTENCE_READ_ONLY_PREFIXES);
1414 }
1415
1416 public boolean isServiceReadOnlyMethod(
1417 JavaMethod method, List<String> txRequiredList) {
1418
1419 return isReadOnlyMethod(
1420 method, txRequiredList,
1421 PropsValues.SERVICE_BUILDER_SERVICE_READ_ONLY_PREFIXES);
1422 }
1423
1424 public boolean isReadOnlyMethod(
1425 JavaMethod method, List<String> txRequiredList, String[] prefixes) {
1426
1427 String methodName = method.getName();
1428
1429 if (isTxRequiredMethod(method, txRequiredList)) {
1430 return false;
1431 }
1432
1433 for (String prefix : prefixes) {
1434 if (methodName.startsWith(prefix)) {
1435 return true;
1436 }
1437 }
1438
1439 return false;
1440 }
1441
1442 public boolean isSoapMethod(JavaMethod method) {
1443 String returnValueName = method.getReturns().getValue();
1444
1445 if (returnValueName.startsWith("java.io") ||
1446 returnValueName.equals("java.util.Map") ||
1447 returnValueName.equals("java.util.Properties") ||
1448 returnValueName.startsWith("javax")) {
1449
1450 return false;
1451 }
1452
1453 JavaParameter[] parameters = method.getParameters();
1454
1455 for (int i = 0; i < parameters.length; i++) {
1456 JavaParameter javaParameter = parameters[i];
1457
1458 String parameterTypeName =
1459 javaParameter.getType().getValue() +
1460 _getDimensions(javaParameter.getType());
1461
1462 if (parameterTypeName.equals(
1463 "com.liferay.portal.theme.ThemeDisplay") ||
1464 parameterTypeName.equals(
1465 "com.liferay.portlet.PortletPreferencesImpl") ||
1466 parameterTypeName.startsWith("java.io") ||
1467 parameterTypeName.startsWith("java.util.Map") ||
1470 parameterTypeName.startsWith("java.util.Properties") ||
1471 parameterTypeName.startsWith("javax")) {
1472
1473 return false;
1474 }
1475 }
1476
1477 return true;
1478 }
1479
1480 public boolean isTxRequiredMethod(
1481 JavaMethod method, List<String> txRequiredList) {
1482
1483 if (txRequiredList == null) {
1484 return false;
1485 }
1486
1487 String methodName = method.getName();
1488
1489 for (String txRequired : txRequiredList) {
1490 if (methodName.equals(txRequired)) {
1491 return true;
1492 }
1493 }
1494
1495 return false;
1496 }
1497
1498 private static String _getPackagePath(File file) {
1499 String fileName = StringUtil.replace(file.toString(), "\\", "/");
1500
1501 int x = fileName.indexOf("src/");
1502
1503 if (x == -1) {
1504 x = fileName.indexOf("test/");
1505 }
1506
1507 int y = fileName.lastIndexOf("/");
1508
1509 fileName = fileName.substring(x + 4, y);
1510
1511 return StringUtil.replace(fileName, "/", ".");
1512 }
1513
1514 private void _createEJBPK(Entity entity) throws Exception {
1515 Map<String, Object> context = _getContext();
1516
1517 context.put("entity", entity);
1518
1519
1521 String content = _processTemplate(_tplEjbPk, context);
1522
1523
1525 File ejbFile = new File(
1526 _serviceOutputPath + "/service/persistence/" +
1527 entity.getPKClassName() + ".java");
1528
1529 writeFile(ejbFile, content, _author);
1530 }
1531
1532 private void _createExceptions(List<String> exceptions) throws Exception {
1533 for (int i = 0; i < _ejbList.size(); i++) {
1534 Entity entity = _ejbList.get(i);
1535
1536 if (entity.hasColumns()) {
1537 exceptions.add(getNoSuchEntityException(entity));
1538 }
1539 }
1540
1541 for (String exception : exceptions) {
1542 File exceptionFile = new File(
1543 _serviceOutputPath + "/" + exception + "Exception.java");
1544
1545 if (!exceptionFile.exists()) {
1546 Map<String, Object> context = _getContext();
1547
1548 context.put("exception", exception);
1549
1550 String content = _processTemplate(_tplException, context);
1551
1552 content = StringUtil.replace(content, "\r\n", "\n");
1553
1554 FileUtil.write(exceptionFile, content);
1555 }
1556
1557 if (!_serviceOutputPath.equals(_outputPath)) {
1558 exceptionFile = new File(
1559 _outputPath + "/" + exception + "Exception.java");
1560
1561 if (exceptionFile.exists()) {
1562 System.out.println("Relocating " + exceptionFile);
1563
1564 exceptionFile.delete();
1565 }
1566 }
1567 }
1568 }
1569
1570 private void _createExtendedModel(Entity entity) throws Exception {
1571 JavaClass javaClass = _getJavaClass(
1572 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
1573
1574 Map<String, Object> context = _getContext();
1575
1576 context.put("entity", entity);
1577 context.put("methods", _getMethods(javaClass));
1578
1579
1581 String content = _processTemplate(_tplExtendedModel, context);
1582
1583
1585 File modelFile = new File(
1586 _serviceOutputPath + "/model/" + entity.getName() + ".java");
1587
1588 Map<String, Object> jalopySettings = new HashMap<String, Object>();
1589
1590 jalopySettings.put("keepJavadoc", Boolean.TRUE);
1591
1592 writeFile(modelFile, content, _author, jalopySettings);
1593
1594 if (!_serviceOutputPath.equals(_outputPath)) {
1595 modelFile = new File(
1596 _outputPath + "/model/" + entity.getName() + ".java");
1597
1598 if (modelFile.exists()) {
1599 System.out.println("Relocating " + modelFile);
1600
1601 modelFile.delete();
1602 }
1603 }
1604 }
1605
1606 private void _createExtendedModelImpl(Entity entity) throws Exception {
1607 Map<String, Object> context = _getContext();
1608
1609 context.put("entity", entity);
1610
1611
1613 String content = _processTemplate(_tplExtendedModelImpl, context);
1614
1615
1617 File modelFile = new File(
1618 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
1619
1620 if (!modelFile.exists()) {
1621 writeFile(modelFile, content, _author);
1622 }
1623 }
1624
1625 private void _createFinder(Entity entity) throws Exception {
1626 if (!entity.hasFinderClass()) {
1627 return;
1628 }
1629
1630 JavaClass javaClass = _getJavaClass(
1631 _outputPath + "/service/persistence/" + entity.getName() +
1632 "FinderImpl.java");
1633
1634 Map<String, Object> context = _getContext();
1635
1636 context.put("entity", entity);
1637 context.put("methods", _getMethods(javaClass));
1638
1639
1641 String content = _processTemplate(_tplFinder, context);
1642
1643
1645 File ejbFile = new File(
1646 _serviceOutputPath + "/service/persistence/" + entity.getName() +
1647 "Finder.java");
1648
1649 writeFile(ejbFile, content, _author);
1650
1651 if (!_serviceOutputPath.equals(_outputPath)) {
1652 ejbFile = new File(
1653 _outputPath + "/service/persistence/" + entity.getName() +
1654 "Finder.java");
1655
1656 if (ejbFile.exists()) {
1657 System.out.println("Relocating " + ejbFile);
1658
1659 ejbFile.delete();
1660 }
1661 }
1662 }
1663
1664 private void _createFinderUtil(Entity entity) throws Exception {
1665 if (!entity.hasFinderClass()) {
1666 return;
1667 }
1668
1669 JavaClass javaClass = _getJavaClass(
1670 _outputPath + "/service/persistence/" + entity.getName() +
1671 "FinderImpl.java");
1672
1673 Map<String, Object> context = _getContext();
1674
1675 context.put("entity", entity);
1676 context.put("methods", _getMethods(javaClass));
1677
1678
1680 String content = _processTemplate(_tplFinderUtil, context);
1681
1682
1684 File ejbFile = new File(
1685 _serviceOutputPath + "/service/persistence/" + entity.getName() +
1686 "FinderUtil.java");
1687
1688 writeFile(ejbFile, content, _author);
1689
1690 if (!_serviceOutputPath.equals(_outputPath)) {
1691 ejbFile = new File(
1692 _outputPath + "/service/persistence/" + entity.getName() +
1693 "FinderUtil.java");
1694
1695 if (ejbFile.exists()) {
1696 System.out.println("Relocating " + ejbFile);
1697
1698 ejbFile.delete();
1699 }
1700 }
1701 }
1702
1703 private void _createHbm(Entity entity) {
1704 File ejbFile = new File(
1705 _outputPath + "/service/persistence/" + entity.getName() +
1706 "HBM.java");
1707
1708 if (ejbFile.exists()) {
1709 System.out.println("Removing deprecated " + ejbFile);
1710
1711 ejbFile.delete();
1712 }
1713 }
1714
1715 private void _createHbmUtil(Entity entity) {
1716 File ejbFile = new File(
1717 _outputPath + "/service/persistence/" + entity.getName() +
1718 "HBMUtil.java");
1719
1720 if (ejbFile.exists()) {
1721 System.out.println("Removing deprecated " + ejbFile);
1722
1723 ejbFile.delete();
1724 }
1725 }
1726
1727 private void _createHbmXml() throws Exception {
1728 Map<String, Object> context = _getContext();
1729
1730 context.put("entities", _ejbList);
1731
1732
1734 String content = _processTemplate(_tplHbmXml, context);
1735
1736 File xmlFile = new File(_hbmFileName);
1737
1738 if (!xmlFile.exists()) {
1739 String xml =
1740 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
1741 "<!DOCTYPE hibernate-mapping PUBLIC \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n" +
1742 "\n" +
1743 "<hibernate-mapping default-lazy=\"false\" auto-import=\"false\">\n" +
1744 "</hibernate-mapping>";
1745
1746 FileUtil.write(xmlFile, xml);
1747 }
1748
1749 String oldContent = FileUtil.read(xmlFile);
1750 String newContent = _fixHbmXml(oldContent);
1751
1752 int firstClass = newContent.indexOf(
1753 "<class name=\"" + _packagePath + ".model.impl.");
1754 int lastClass = newContent.lastIndexOf(
1755 "<class name=\"" + _packagePath + ".model.impl.");
1756
1757 if (firstClass == -1) {
1758 int x = newContent.indexOf("</hibernate-mapping>");
1759
1760 if (x != -1) {
1761 newContent =
1762 newContent.substring(0, x) + content +
1763 newContent.substring(x, newContent.length());
1764 }
1765 }
1766 else {
1767 firstClass = newContent.lastIndexOf("<class", firstClass) - 1;
1768 lastClass = newContent.indexOf("</class>", lastClass) + 9;
1769
1770 newContent =
1771 newContent.substring(0, firstClass) + content +
1772 newContent.substring(lastClass, newContent.length());
1773 }
1774
1775 newContent = _formatXml(newContent);
1776
1777 if (!oldContent.equals(newContent)) {
1778 FileUtil.write(xmlFile, newContent);
1779 }
1780 }
1781
1782 private void _createJsonJs() throws Exception {
1783 StringBuilder content = new StringBuilder();
1784
1785 if (_ejbList.size() > 0) {
1786 content.append(_processTemplate(_tplJsonJs));
1787 }
1788
1789 for (int i = 0; i < _ejbList.size(); i++) {
1790 Entity entity = _ejbList.get(i);
1791
1792 if (entity.hasRemoteService()) {
1793 JavaClass javaClass = _getJavaClass(
1794 _outputPath + "/service/http/" + entity.getName() +
1795 "ServiceJSON.java");
1796
1797 JavaMethod[] methods = _getMethods(javaClass);
1798
1799 Set<String> jsonMethods = new LinkedHashSet<String>();
1800
1801 for (int j = 0; j < methods.length; j++) {
1802 JavaMethod javaMethod = methods[j];
1803
1804 String methodName = javaMethod.getName();
1805
1806 if (javaMethod.isPublic()) {
1807 jsonMethods.add(methodName);
1808 }
1809 }
1810
1811 if (jsonMethods.size() > 0) {
1812 Map<String, Object> context = _getContext();
1813
1814 context.put("entity", entity);
1815 context.put("methods", jsonMethods);
1816
1817 content.append("\n\n");
1818 content.append(_processTemplate(_tplJsonJsMethod, context));
1819 }
1820 }
1821 }
1822
1823 File jsonFile = new File(_jsonFileName);
1824
1825 if (!jsonFile.exists()) {
1826 FileUtil.write(jsonFile, "");
1827 }
1828
1829 String oldContent = FileUtil.read(jsonFile);
1830 String newContent = new String(oldContent);
1831
1832 int oldBegin = oldContent.indexOf(
1833 "Liferay.Service.register(\"Liferay.Service." + _portletShortName);
1834
1835 int oldEnd = oldContent.lastIndexOf(
1836 "Liferay.Service." + _portletShortName);
1837
1838 oldEnd = oldContent.indexOf(");", oldEnd);
1839
1840 int newBegin = newContent.indexOf(
1841 "Liferay.Service.register(\"Liferay.Service." + _portletShortName);
1842
1843 int newEnd = newContent.lastIndexOf(
1844 "Liferay.Service." + _portletShortName);
1845
1846 newEnd = newContent.indexOf(");", newEnd);
1847
1848 if (newBegin == -1) {
1849 newContent = oldContent + "\n\n" + content.toString().trim();
1850 }
1851 else {
1852 newContent =
1853 newContent.substring(0, oldBegin) + content.toString().trim() +
1854 newContent.substring(oldEnd + 2, newContent.length());
1855 }
1856
1857 newContent = newContent.trim();
1858
1859 if (!oldContent.equals(newContent)) {
1860 FileUtil.write(jsonFile, newContent);
1861 }
1862 }
1863
1864 private void _createModel(Entity entity) throws Exception {
1865 Map<String, Object> context = _getContext();
1866
1867 context.put("entity", entity);
1868
1869
1871 String content = _processTemplate(_tplModel, context);
1872
1873
1875 File modelFile = new File(
1876 _serviceOutputPath + "/model/" + entity.getName() + "Model.java");
1877
1878 Map<String, Object> jalopySettings = new HashMap<String, Object>();
1879
1880 jalopySettings.put("keepJavadoc", Boolean.TRUE);
1881
1882 writeFile(modelFile, content, _author, jalopySettings);
1883
1884 if (!_serviceOutputPath.equals(_outputPath)) {
1885 modelFile = new File(
1886 _outputPath + "/model/" + entity.getName() + "Model.java");
1887
1888 if (modelFile.exists()) {
1889 System.out.println("Relocating " + modelFile);
1890
1891 modelFile.delete();
1892 }
1893 }
1894 }
1895
1896 private void _createModelClp(Entity entity) throws Exception {
1897 if (Validator.isNull(_pluginName)) {
1898 return;
1899 }
1900
1901 JavaClass javaClass = _getJavaClass(
1902 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
1903
1904 Map<String, Object> context = _getContext();
1905
1906 context.put("entity", entity);
1907 context.put("methods", _getMethods(javaClass));
1908
1909
1911 String content = _processTemplate(_tplModelClp, context);
1912
1913
1915 File modelFile = new File(
1916 _serviceOutputPath + "/model/" + entity.getName() + "Clp.java");
1917
1918 writeFile(modelFile, content, _author);
1919 }
1920
1921 private void _createModelHintsXml() throws Exception {
1922 Map<String, Object> context = _getContext();
1923
1924 context.put("entities", _ejbList);
1925
1926
1928 String content = _processTemplate(_tplModelHintsXml, context);
1929
1930 File xmlFile = new File(_modelHintsFileName);
1931
1932 if (!xmlFile.exists()) {
1933 String xml =
1934 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
1935 "\n" +
1936 "<model-hints>\n" +
1937 "</model-hints>";
1938
1939 FileUtil.write(xmlFile, xml);
1940 }
1941
1942 String oldContent = FileUtil.read(xmlFile);
1943 String newContent = new String(oldContent);
1944
1945 int firstModel = newContent.indexOf(
1946 "<model name=\"" + _packagePath + ".model.");
1947 int lastModel = newContent.lastIndexOf(
1948 "<model name=\"" + _packagePath + ".model.");
1949
1950 if (firstModel == -1) {
1951 int x = newContent.indexOf("</model-hints>");
1952
1953 newContent =
1954 newContent.substring(0, x) + content +
1955 newContent.substring(x, newContent.length());
1956 }
1957 else {
1958 firstModel = newContent.lastIndexOf("<model", firstModel) - 1;
1959 lastModel = newContent.indexOf("</model>", lastModel) + 9;
1960
1961 newContent =
1962 newContent.substring(0, firstModel) + content +
1963 newContent.substring(lastModel, newContent.length());
1964 }
1965
1966 newContent = _formatXml(newContent);
1967
1968 if (!oldContent.equals(newContent)) {
1969 FileUtil.write(xmlFile, newContent);
1970 }
1971 }
1972
1973 private void _createModelImpl(Entity entity) throws Exception {
1974 Map<String, Object> context = _getContext();
1975
1976 context.put("entity", entity);
1977
1978
1980 String content = _processTemplate(_tplModelImpl, context);
1981
1982
1984 File modelFile = new File(
1985 _outputPath + "/model/impl/" + entity.getName() + "ModelImpl.java");
1986
1987 Map<String, Object> jalopySettings = new HashMap<String, Object>();
1988
1989 jalopySettings.put("keepJavadoc", Boolean.TRUE);
1990
1991 writeFile(modelFile, content, _author, jalopySettings);
1992 }
1993
1994 private void _createModelSoap(Entity entity) throws Exception {
1995 Map<String, Object> context = _getContext();
1996
1997 context.put("entity", entity);
1998
1999
2001 String content = _processTemplate(_tplModelSoap, context);
2002
2003
2005 File modelFile = new File(
2006 _serviceOutputPath + "/model/" + entity.getName() + "Soap.java");
2007
2008 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2009
2010 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2011
2012 writeFile(modelFile, content, _author, jalopySettings);
2013
2014 if (!_serviceOutputPath.equals(_outputPath)) {
2015 modelFile = new File(
2016 _outputPath + "/model/" + entity.getName() + "Soap.java");
2017
2018 if (modelFile.exists()) {
2019 System.out.println("Relocating " + modelFile);
2020
2021 modelFile.delete();
2022 }
2023 }
2024 }
2025
2026 private void _createPersistence(Entity entity) throws Exception {
2027 JavaClass javaClass = _getJavaClass(
2028 _outputPath + "/service/persistence/" + entity.getName() +
2029 "PersistenceImpl.java");
2030
2031 Map<String, Object> context = _getContext();
2032
2033 context.put("entity", entity);
2034 context.put("methods", _getMethods(javaClass));
2035
2036
2038 String content = _processTemplate(_tplPersistence, context);
2039
2040
2042 File ejbFile = new File(
2043 _serviceOutputPath + "/service/persistence/" + entity.getName() +
2044 "Persistence.java");
2045
2046 writeFile(ejbFile, content, _author);
2047
2048 if (!_serviceOutputPath.equals(_outputPath)) {
2049 ejbFile = new File(
2050 _outputPath + "/service/persistence/" + entity.getName() +
2051 "Persistence.java");
2052
2053 if (ejbFile.exists()) {
2054 System.out.println("Relocating " + ejbFile);
2055
2056 ejbFile.delete();
2057 }
2058 }
2059 }
2060
2061 private void _createPersistenceImpl(Entity entity) throws Exception {
2062 Map<String, Object> context = _getContext();
2063
2064 context.put("entity", entity);
2065
2066
2068 String content = _processTemplate(_tplPersistenceImpl, context);
2069
2070
2072 File ejbFile = new File(
2073 _outputPath + "/service/persistence/" + entity.getName() +
2074 "PersistenceImpl.java");
2075
2076 writeFile(ejbFile, content, _author);
2077 }
2078
2079 private void _createPersistenceTest(Entity entity) throws Exception {
2080 Map<String, Object> context = _getContext();
2081
2082 context.put("entity", entity);
2083
2084
2086 String content = _processTemplate(_tplPersistenceTest, context);
2087
2088
2090 File ejbFile = new File(
2091 _testOutputPath + "/service/persistence/" + entity.getName() +
2092 "PersistenceTest.java");
2093
2094 writeFile(ejbFile, content, _author);
2095 }
2096
2097 private void _createPersistenceUtil(Entity entity) throws Exception {
2098 JavaClass javaClass = _getJavaClass(
2099 _outputPath + "/service/persistence/" + entity.getName() +
2100 "PersistenceImpl.java");
2101
2102 Map<String, Object> context = _getContext();
2103
2104 context.put("entity", entity);
2105 context.put("methods", _getMethods(javaClass));
2106
2107
2109 String content = _processTemplate(_tplPersistenceUtil, context);
2110
2111
2113 File ejbFile = new File(
2114 _serviceOutputPath + "/service/persistence/" + entity.getName() +
2115 "Util.java");
2116
2117 writeFile(ejbFile, content, _author);
2118
2119 if (!_serviceOutputPath.equals(_outputPath)) {
2120 ejbFile = new File(
2121 _outputPath + "/service/persistence/" + entity.getName() +
2122 "Util.java");
2123
2124 if (ejbFile.exists()) {
2125 System.out.println("Relocating " + ejbFile);
2126
2127 ejbFile.delete();
2128 }
2129 }
2130 }
2131
2132 private void _createPool(Entity entity) {
2133 File ejbFile = new File(
2134 _outputPath + "/service/persistence/" + entity.getName() +
2135 "Pool.java");
2136
2137 if (ejbFile.exists()) {
2138 System.out.println("Removing deprecated " + ejbFile);
2139
2140 ejbFile.delete();
2141 }
2142 }
2143
2144 private void _createProps() throws Exception {
2145 if (Validator.isNull(_pluginName)) {
2146 return;
2147 }
2148
2149
2151 File propsFile = new File(_implDir + "/service.properties");
2152
2153 long buildNumber = 1;
2154
2155 if (propsFile.exists()) {
2156 Properties props = PropertiesUtil.load(FileUtil.read(propsFile));
2157
2158 buildNumber = GetterUtil.getLong(
2159 props.getProperty("build.number")) + 1;
2160 }
2161
2162 Map<String, Object> context = _getContext();
2163
2164 context.put("buildNumber", new Long(buildNumber));
2165 context.put("currentTimeMillis", new Long(System.currentTimeMillis()));
2166
2167 String content = _processTemplate(_tplProps, context);
2168
2169
2171 FileUtil.write(propsFile, content, true);
2172 }
2173
2174 private void _createRemotingXml() throws Exception {
2175 StringBuilder sb = new StringBuilder();
2176
2177 Document doc = SAXReaderUtil.read(new File(_springFileName), true);
2178
2179 Iterator<Element> itr = doc.getRootElement().elements(
2180 "bean").iterator();
2181
2182 while (itr.hasNext()) {
2183 Element beanEl = itr.next();
2184
2185 String beanId = beanEl.attributeValue("id");
2186
2187 if (beanId.endsWith("ServiceFactory") &&
2188 !beanId.endsWith("LocalServiceFactory")) {
2189
2190 String entityName = beanId;
2191
2192 entityName = StringUtil.replace(entityName, ".service.", ".");
2193
2194 int pos = entityName.indexOf("LocalServiceFactory");
2195
2196 if (pos == -1) {
2197 pos = entityName.indexOf("ServiceFactory");
2198 }
2199
2200 entityName = entityName.substring(0, pos);
2201
2202 Entity entity = getEntity(entityName);
2203
2204 String serviceName = beanId.substring(0, beanId.length() - 7);
2205
2206 String serviceMapping = serviceName;
2207
2208 serviceMapping = StringUtil.replace(
2209 serviceMapping, ".service.", ".service.spring.");
2210 serviceMapping = StringUtil.replace(
2211 serviceMapping, StringPool.PERIOD, StringPool.UNDERLINE);
2212
2213 Map<String, Object> context = _getContext();
2214
2215 context.put("entity", entity);
2216 context.put("serviceName", serviceName);
2217 context.put("serviceMapping", serviceMapping);
2218
2219 sb.append(_processTemplate(_tplRemotingXml, context));
2220 }
2221 }
2222
2223 File outputFile = new File(_remotingFileName);
2224
2225 if (!outputFile.exists()) {
2226 return;
2227 }
2228
2229 String content = FileUtil.read(outputFile);
2230 String newContent = content;
2231
2232 int x = content.indexOf("<bean ");
2233 int y = content.lastIndexOf("</bean>") + 8;
2234
2235 if (x != -1) {
2236 newContent =
2237 content.substring(0, x - 1) + sb.toString() +
2238 content.substring(y, content.length());
2239 }
2240 else {
2241 x = content.indexOf("</beans>");
2242
2243 if (x != -1) {
2244 newContent =
2245 content.substring(0, x) + sb.toString() +
2246 content.substring(x, content.length());
2247 }
2248 else {
2249 newContent = "<beans>" + sb.toString() + "</beans>";
2250 }
2251 }
2252
2253 newContent = _formatXml(newContent);
2254
2255 if (!content.equals(newContent)) {
2256 FileUtil.write(outputFile, newContent);
2257
2258 System.out.println(outputFile.toString());
2259 }
2260 }
2261
2262 private void _createService(Entity entity, int sessionType)
2263 throws Exception {
2264
2265 String serviceComments = "This is a remote service. Methods of this service are expected to have security checks based on the propagated JAAS credentials because this service can be accessed remotely.";
2266
2267 JavaClass javaClass = _getJavaClass(_outputPath + "/service/impl/" + entity.getName() + (sessionType != _SESSION_TYPE_REMOTE ? "Local" : "") + "ServiceImpl.java");
2268
2269 JavaMethod[] methods = _getMethods(javaClass);
2270
2271 if (sessionType == _SESSION_TYPE_LOCAL) {
2272 if (javaClass.getSuperClass().getValue().endsWith(
2273 entity.getName() + "LocalServiceBaseImpl")) {
2274
2275 JavaClass parentJavaClass = _getJavaClass(
2276 _outputPath + "/service/base/" + entity.getName() +
2277 "LocalServiceBaseImpl.java");
2278
2279 JavaMethod[] parentMethods = parentJavaClass.getMethods();
2280
2281 JavaMethod[] allMethods = new JavaMethod[parentMethods.length + methods.length];
2282
2283 ArrayUtil.combine(parentMethods, methods, allMethods);
2284
2285 methods = allMethods;
2286 }
2287
2288 serviceComments = "This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.";
2289 }
2290
2291 Map<String, Object> context = _getContext();
2292
2293 context.put("entity", entity);
2294 context.put("methods", methods);
2295 context.put("sessionTypeName",_getSessionTypeName(sessionType));
2296 context.put("serviceComments", serviceComments);
2297
2298
2300 String content = _processTemplate(_tplService, context);
2301
2302
2304 File ejbFile = new File(
2305 _serviceOutputPath + "/service/" + entity.getName() +
2306 _getSessionTypeName(sessionType) + "Service.java");
2307
2308 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2309
2310 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2311
2312 writeFile(ejbFile, content, _author, jalopySettings);
2313
2314 if (!_serviceOutputPath.equals(_outputPath)) {
2315 ejbFile = new File(
2316 _outputPath + "/service/" + entity.getName() +
2317 _getSessionTypeName(sessionType) + "Service.java");
2318
2319 if (ejbFile.exists()) {
2320 System.out.println("Relocating " + ejbFile);
2321
2322 ejbFile.delete();
2323 }
2324 }
2325 }
2326
2327 private void _createServiceBaseImpl(Entity entity, int sessionType)
2328 throws Exception {
2329
2330 Map<String, Object> context = _getContext();
2331
2332 context.put("entity", entity);
2333 context.put("sessionTypeName",_getSessionTypeName(sessionType));
2334 context.put(
2335 "referenceList", _mergeReferenceList(entity.getReferenceList()));
2336
2337
2339 String content = _processTemplate(_tplServiceBaseImpl, context);
2340
2341
2343 File ejbFile = new File(
2344 _outputPath + "/service/base/" + entity.getName() +
2345 _getSessionTypeName(sessionType) + "ServiceBaseImpl.java");
2346
2347 writeFile(ejbFile, content, _author);
2348 }
2349
2350 private void _createServiceClp(Entity entity, int sessionType)
2351 throws Exception {
2352
2353 if (Validator.isNull(_pluginName)) {
2354 return;
2355 }
2356
2357 JavaClass javaClass = _getJavaClass(
2358 _serviceOutputPath + "/service/" + entity.getName() +
2359 _getSessionTypeName(sessionType) + "Service.java");
2360
2361 Map<String, Object> context = _getContext();
2362
2363 context.put("entity", entity);
2364 context.put("methods", _getMethods(javaClass));
2365 context.put("sessionTypeName", _getSessionTypeName(sessionType));
2366
2367
2369 String content = _processTemplate(_tplServiceClp, context);
2370
2371
2373 File ejbFile = new File(
2374 _serviceOutputPath + "/service/" + entity.getName() +
2375 _getSessionTypeName(sessionType) + "ServiceClp.java");
2376
2377 writeFile(ejbFile, content, _author);
2378 }
2379
2380 private void _createServiceClpSerializer() throws Exception {
2381 if (Validator.isNull(_pluginName)) {
2382 return;
2383 }
2384
2385 Map<String, Object> context = _getContext();
2386
2387 context.put("entities", _ejbList);
2388
2389
2391 String content = _processTemplate(_tplServiceClpSerializer, context);
2392
2393
2395 File ejbFile = new File(
2396 _serviceOutputPath + "/service/ClpSerializer.java");
2397
2398 writeFile(ejbFile, content);
2399 }
2400
2401 private void _createServiceFactory(Entity entity, int sessionType)
2402 throws Exception {
2403
2404 if (Validator.isNotNull(_pluginName)) {
2405 FileUtil.delete(
2406 _serviceOutputPath + "/service/" + entity.getName() +
2407 _getSessionTypeName(sessionType) + "ServiceFactory.java");
2408
2409 FileUtil.delete(
2410 _outputPath + "/service/" + entity.getName() +
2411 _getSessionTypeName(sessionType) + "ServiceFactory.java");
2412
2413 return;
2414 }
2415
2416 Map<String, Object> context = _getContext();
2417
2418 context.put("entity", entity);
2419 context.put("sessionTypeName", _getSessionTypeName(sessionType));
2420
2421
2423 String content = _processTemplate(_tplServiceFactory, context);
2424
2425
2427 File ejbFile = new File(
2428 _serviceOutputPath + "/service/" + entity.getName() +
2429 _getSessionTypeName(sessionType) + "ServiceFactory.java");
2430
2431 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2432
2433 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2434
2435 writeFile(ejbFile, content, _author, jalopySettings);
2436
2437 if (!_serviceOutputPath.equals(_outputPath)) {
2438 ejbFile = new File(
2439 _outputPath + "/service/" + entity.getName() +
2440 _getSessionTypeName(sessionType) + "ServiceFactory.java");
2441
2442 if (ejbFile.exists()) {
2443 System.out.println("Relocating " + ejbFile);
2444
2445 ejbFile.delete();
2446 }
2447 }
2448 }
2449
2450 private void _createServiceHttp(Entity entity) throws Exception {
2451 JavaClass javaClass = _getJavaClass(
2452 _outputPath + "/service/impl/" + entity.getName() +
2453 "ServiceImpl.java");
2454
2455 Map<String, Object> context = _getContext();
2456
2457 context.put("entity", entity);
2458 context.put("methods", _getMethods(javaClass));
2459 context.put("hasHttpMethods", new Boolean(_hasHttpMethods(javaClass)));
2460
2461
2463 String content = _processTemplate(_tplServiceHttp, context);
2464
2465
2467 File ejbFile = new File(
2468 _outputPath + "/service/http/" + entity.getName() +
2469 "ServiceHttp.java");
2470
2471 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2472
2473 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2474
2475 writeFile(ejbFile, content, _author, jalopySettings);
2476 }
2477
2478 private void _createServiceImpl(Entity entity, int sessionType)
2479 throws Exception {
2480
2481 Map<String, Object> context = _getContext();
2482
2483 context.put("entity", entity);
2484 context.put("sessionTypeName", _getSessionTypeName(sessionType));
2485
2486
2488 String content = _processTemplate(_tplServiceImpl, context);
2489
2490
2492 File ejbFile = new File(
2493 _outputPath + "/service/impl/" + entity.getName() +
2494 _getSessionTypeName(sessionType) + "ServiceImpl.java");
2495
2496 if (!ejbFile.exists()) {
2497 writeFile(ejbFile, content, _author);
2498 }
2499 }
2500
2501 private void _createServiceJson(Entity entity) throws Exception {
2502 JavaClass javaClass = _getJavaClass(
2503 _outputPath + "/service/impl/" + entity.getName() +
2504 "ServiceImpl.java");
2505
2506 Map<String, Object> context = _getContext();
2507
2508 context.put("entity", entity);
2509 context.put("methods", _getMethods(javaClass));
2510
2511
2513 String content = _processTemplate(_tplServiceJson, context);
2514
2515
2517 File ejbFile = new File(
2518 _outputPath + "/service/http/" + entity.getName() +
2519 "ServiceJSON.java");
2520
2521 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2522
2523 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2524
2525 writeFile(ejbFile, content, _author, jalopySettings);
2526 }
2527
2528 private void _createServiceJsonSerializer(Entity entity) throws Exception {
2529 Map<String, Object> context = _getContext();
2530
2531 context.put("entity", entity);
2532
2533
2535 String content = _processTemplate(_tplServiceJsonSerializer, context);
2536
2537
2539 File ejbFile = new File(
2540 _outputPath + "/service/http/" + entity.getName() +
2541 "JSONSerializer.java");
2542
2543 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2544
2545 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2546
2547 writeFile(ejbFile, content, _author, jalopySettings);
2548 }
2549
2550 private void _createServiceSoap(Entity entity) throws Exception {
2551 JavaClass javaClass = _getJavaClass(
2552 _outputPath + "/service/impl/" + entity.getName() +
2553 "ServiceImpl.java");
2554
2555 Map<String, Object> context = _getContext();
2556
2557 context.put("entity", entity);
2558 context.put("methods", _getMethods(javaClass));
2559
2560
2562 String content = _processTemplate(_tplServiceSoap, context);
2563
2564
2566 File ejbFile = new File(
2567 _outputPath + "/service/http/" + entity.getName() +
2568 "ServiceSoap.java");
2569
2570 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2571
2572 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2573
2574 writeFile(ejbFile, content, _author, jalopySettings);
2575 }
2576
2577 private void _createServiceUtil(Entity entity, int sessionType)
2578 throws Exception {
2579
2580 JavaClass javaClass = _getJavaClass(
2581 _serviceOutputPath + "/service/" + entity.getName() +
2582 _getSessionTypeName(sessionType) + "Service.java");
2583
2584 Map<String, Object> context = _getContext();
2585
2586 context.put("entity", entity);
2587 context.put("methods", _getMethods(javaClass));
2588 context.put("sessionTypeName", _getSessionTypeName(sessionType));
2589
2590
2592 String content = _processTemplate(_tplServiceUtil, context);
2593
2594
2596 File ejbFile = new File(
2597 _serviceOutputPath + "/service/" + entity.getName() +
2598 _getSessionTypeName(sessionType) + "ServiceUtil.java");
2599
2600 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2601
2602 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2603
2604 writeFile(ejbFile, content, _author, jalopySettings);
2605
2606 if (!_serviceOutputPath.equals(_outputPath)) {
2607 ejbFile = new File(
2608 _outputPath + "/service/" + entity.getName() +
2609 _getSessionTypeName(sessionType) + "ServiceUtil.java");
2610
2611 if (ejbFile.exists()) {
2612 System.out.println("Relocating " + ejbFile);
2613
2614 ejbFile.delete();
2615 }
2616 }
2617 }
2618
2619 private void _createSpringBaseXml() throws Exception {
2620 if (Validator.isNull(_springBaseFileName)) {
2621 return;
2622 }
2623
2624
2626 String content = _processTemplate(_tplSpringBaseXml);
2627
2628
2630 File ejbFile = new File(_springBaseFileName);
2631
2632 FileUtil.write(ejbFile, content, true);
2633
2634 if (Validator.isNotNull(_pluginName)) {
2635 FileUtil.delete(
2636 "docroot/WEB-INF/src/META-INF/data-source-spring.xml");
2637 FileUtil.delete("docroot/WEB-INF/src/META-INF/misc-spring.xml");
2638 }
2639 }
2640
2641 private void _createSpringDynamicDataSourceXml() throws Exception {
2642 if (Validator.isNull(_springDynamicDataSourceFileName)) {
2643 return;
2644 }
2645
2646
2648 String content = _processTemplate(_tplSpringDynamicDataSourceXml);
2649
2650
2652 File ejbFile = new File(_springDynamicDataSourceFileName);
2653
2654 FileUtil.write(ejbFile, content, true);
2655 }
2656
2657 private void _createSpringHibernateXml() throws Exception {
2658 if (Validator.isNull(_springHibernateFileName)) {
2659 return;
2660 }
2661
2662
2664 String content = _processTemplate(_tplSpringHibernateXml);
2665
2666
2668 File ejbFile = new File(_springHibernateFileName);
2669
2670 FileUtil.write(ejbFile, content, true);
2671 }
2672
2673 private void _createSpringInfrastructureXml() throws Exception {
2674 if (Validator.isNull(_springInfrastructureFileName)) {
2675 return;
2676 }
2677
2678
2680 String content = _processTemplate(_tplSpringInfrastructureXml);
2681
2682
2684 File ejbFile = new File(_springInfrastructureFileName);
2685
2686 FileUtil.write(ejbFile, content, true);
2687 }
2688
2689 private void _createSpringXml() throws Exception {
2690 Map<String, Object> context = _getContext();
2691
2692 context.put("entities", _ejbList);
2693
2694
2696 String content = _processTemplate(_tplSpringXml, context);
2697
2698 File xmlFile = new File(_springFileName);
2699
2700 if (!xmlFile.exists()) {
2701 String xml =
2702 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
2703 "\n" +
2704 "<beans\n" +
2705 "\tdefault-init-method=\"afterPropertiesSet\"\n" +
2706 "\txmlns=\"http://www.springframework.org/schema/beans\"\n" +
2707 "\txmlns:aop=\"http://www.springframework.org/schema/aop\"\n" +
2708 "\txmlns:tx=\"http://www.springframework.org/schema/tx\"\n" +
2709 "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
2710 "\txsi:schemaLocation=\"http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd\"\n" +
2711 ">\n" +
2712 "</beans>";
2713
2714 FileUtil.write(xmlFile, xml);
2715 }
2716
2717 String oldContent = FileUtil.read(xmlFile);
2718 String newContent = _fixSpringXml(oldContent);
2719
2720 int x = oldContent.indexOf("<beans");
2721 int y = oldContent.lastIndexOf("</beans>");
2722
2723 int firstSession = newContent.indexOf(
2724 "<bean id=\"" + _packagePath + ".service.", x);
2725
2726 int lastSession = newContent.lastIndexOf(
2727 "<bean id=\"" + _packagePath + ".service.", y);
2728
2729 if ((firstSession == -1) || (firstSession > y)) {
2730 x = newContent.indexOf("</beans>");
2731
2732 newContent =
2733 newContent.substring(0, x) + content +
2734 newContent.substring(x, newContent.length());
2735 }
2736 else {
2737 firstSession = newContent.lastIndexOf("<bean", firstSession) - 1;
2738 lastSession = newContent.indexOf("</bean>", lastSession) + 8;
2739
2740 newContent =
2741 newContent.substring(0, firstSession) + content +
2742 newContent.substring(lastSession, newContent.length());
2743 }
2744
2745 newContent = _formatXml(newContent);
2746
2747 if (!oldContent.equals(newContent)) {
2748 FileUtil.write(xmlFile, newContent);
2749 }
2750 }
2751
2752 private void _createSQLIndexes() throws IOException {
2753 if (!FileUtil.exists(_sqlDir)) {
2754 return;
2755 }
2756
2757
2759 File sqlFile = new File(_sqlDir + "/" + _sqlIndexesFileName);
2760
2761 if (!sqlFile.exists()) {
2762 FileUtil.write(sqlFile, "");
2763 }
2764
2765 Map<String, String> indexSQLs = new TreeMap<String, String>();
2766
2767 BufferedReader br = new BufferedReader(new FileReader(sqlFile));
2768
2769 while (true) {
2770 String indexSQL = br.readLine();
2771
2772 if (indexSQL == null) {
2773 break;
2774 }
2775
2776 if (Validator.isNotNull(indexSQL.trim())) {
2777 int pos = indexSQL.indexOf(" on ");
2778
2779 String indexSpec = indexSQL.substring(pos + 4);
2780
2781 indexSQLs.put(indexSpec, indexSQL);
2782 }
2783 }
2784
2785 br.close();
2786
2787
2789 File propsFile = new File(
2790 _sqlDir + "/" + _sqlIndexesPropertiesFileName);
2791
2792 if (!propsFile.exists()) {
2793 FileUtil.write(propsFile, "");
2794 }
2795
2796 Map<String, String> indexProps = new TreeMap<String, String>();
2797
2798 br = new BufferedReader(new FileReader(propsFile));
2799
2800 while (true) {
2801 String indexMapping = br.readLine();
2802
2803 if (indexMapping == null) {
2804 break;
2805 }
2806
2807 if (Validator.isNotNull(indexMapping.trim())) {
2808 String[] splitIndexMapping = indexMapping.split("\\=");
2809
2810 indexProps.put(splitIndexMapping[1], splitIndexMapping[0]);
2811 }
2812 }
2813
2814 br.close();
2815
2816
2818 for (int i = 0; i < _ejbList.size(); i++) {
2819 Entity entity = _ejbList.get(i);
2820
2821 if (!entity.isDefaultDataSource()) {
2822 continue;
2823 }
2824
2825 List<EntityFinder> finderList = entity.getFinderList();
2826
2827 for (int j = 0; j < finderList.size(); j++) {
2828 EntityFinder finder = finderList.get(j);
2829
2830 if (finder.isDBIndex()) {
2831 StringBuilder sb = new StringBuilder();
2832
2833 sb.append(entity.getTable() + " (");
2834
2835 List<EntityColumn> finderColsList = finder.getColumns();
2836
2837 for (int k = 0; k < finderColsList.size(); k++) {
2838 EntityColumn col = finderColsList.get(k);
2839
2840 sb.append(col.getDBName());
2841
2842 if ((k + 1) != finderColsList.size()) {
2843 sb.append(", ");
2844 }
2845 }
2846
2847 sb.append(");");
2848
2849 String indexSpec = sb.toString();
2850
2851 String indexHash =
2852 Integer.toHexString(indexSpec.hashCode()).toUpperCase();
2853
2854 String indexName = "IX_" + indexHash;
2855
2856 sb = new StringBuilder();
2857
2858 sb.append("create index " + indexName + " on ");
2859 sb.append(indexSpec);
2860
2861 indexSQLs.put(indexSpec, sb.toString());
2862
2863 String finderName =
2864 entity.getTable() + StringPool.PERIOD +
2865 finder.getName();
2866
2867 indexProps.put(finderName, indexName);
2868 }
2869 }
2870 }
2871
2872 StringBuilder sb = new StringBuilder();
2873
2874 Iterator<String> itr = indexSQLs.values().iterator();
2875
2876 String prevEntityName = null;
2877
2878 while (itr.hasNext()) {
2879 String indexSQL = itr.next();
2880
2881 String entityName = indexSQL.split(" ")[4];
2882
2883 if ((prevEntityName != null) &&
2884 (!prevEntityName.equals(entityName))) {
2885
2886 sb.append("\n");
2887 }
2888
2889 sb.append(indexSQL);
2890
2891 if (itr.hasNext()) {
2892 sb.append("\n");
2893 }
2894
2895 prevEntityName = entityName;
2896 }
2897
2898 FileUtil.write(sqlFile, sb.toString(), true);
2899
2900
2902 sb = new StringBuilder();
2903
2904 itr = indexProps.keySet().iterator();
2905
2906 prevEntityName = null;
2907
2908 while (itr.hasNext()) {
2909 String finderName = itr.next();
2910
2911 String indexName = indexProps.get(finderName);
2912
2913 String entityName = finderName.split("\\.")[0];
2914
2915 if ((prevEntityName != null) &&
2916 (!prevEntityName.equals(entityName))) {
2917
2918 sb.append("\n");
2919 }
2920
2921 sb.append(indexName + StringPool.EQUAL + finderName);
2922
2923 if (itr.hasNext()) {
2924 sb.append("\n");
2925 }
2926
2927 prevEntityName = entityName;
2928 }
2929
2930 FileUtil.write(propsFile, sb.toString(), true);
2931 }
2932
2933 private void _createSQLMappingTables(
2934 File sqlFile, String newCreateTableString,
2935 EntityMapping entityMapping, boolean addMissingTables)
2936 throws IOException {
2937
2938 if (!sqlFile.exists()) {
2939 FileUtil.write(sqlFile, StringPool.BLANK);
2940 }
2941
2942 String content = FileUtil.read(sqlFile);
2943
2944 int x = content.indexOf(
2945 _SQL_CREATE_TABLE + entityMapping.getTable() + " (");
2946 int y = content.indexOf(");", x);
2947
2948 if (x != -1) {
2949 String oldCreateTableString = content.substring(x + 1, y);
2950
2951 if (!oldCreateTableString.equals(newCreateTableString)) {
2952 content =
2953 content.substring(0, x) + newCreateTableString +
2954 content.substring(y + 2, content.length());
2955
2956 FileUtil.write(sqlFile, content);
2957 }
2958 }
2959 else if (addMissingTables) {
2960 StringBuilder sb = new StringBuilder();
2961
2962 BufferedReader br = new BufferedReader(new StringReader(content));
2963
2964 String line = null;
2965 boolean appendNewTable = true;
2966
2967 while ((line = br.readLine()) != null) {
2968 if (appendNewTable && line.startsWith(_SQL_CREATE_TABLE)) {
2969 x = _SQL_CREATE_TABLE.length();
2970 y = line.indexOf(" ", x);
2971
2972 String tableName = line.substring(x, y);
2973
2974 if (tableName.compareTo(entityMapping.getTable()) > 0) {
2975 sb.append(newCreateTableString + "\n\n");
2976
2977 appendNewTable = false;
2978 }
2979 }
2980
2981 sb.append(line);
2982 sb.append("\n");
2983 }
2984
2985 if (appendNewTable) {
2986 sb.append("\n" + newCreateTableString);
2987 }
2988
2989 br.close();
2990
2991 FileUtil.write(sqlFile, sb.toString(), true);
2992 }
2993 }
2994
2995 private void _createSQLSequences() throws IOException {
2996 if (!FileUtil.exists(_sqlDir)) {
2997 return;
2998 }
2999
3000 File sqlFile = new File(_sqlDir + "/" + _sqlSequencesFileName);
3001
3002 if (!sqlFile.exists()) {
3003 FileUtil.write(sqlFile, "");
3004 }
3005
3006 Set<String> sequenceSQLs = new TreeSet<String>();
3007
3008 BufferedReader br = new BufferedReader(new FileReader(sqlFile));
3009
3010 while (true) {
3011 String sequenceSQL = br.readLine();
3012
3013 if (sequenceSQL == null) {
3014 break;
3015 }
3016
3017 if (Validator.isNotNull(sequenceSQL)) {
3018 sequenceSQLs.add(sequenceSQL);
3019 }
3020 }
3021
3022 br.close();
3023
3024 for (int i = 0; i < _ejbList.size(); i++) {
3025 Entity entity = _ejbList.get(i);
3026
3027 if (!entity.isDefaultDataSource()) {
3028 continue;
3029 }
3030
3031 List<EntityColumn> columnList = entity.getColumnList();
3032
3033 for (int j = 0; j < columnList.size(); j++) {
3034 EntityColumn column = columnList.get(j);
3035
3036 if ("sequence".equals(column.getIdType())) {
3037 StringBuilder sb = new StringBuilder();
3038
3039 String sequenceName = column.getIdParam();
3040
3041 if (sequenceName.length() > 30) {
3042 sequenceName = sequenceName.substring(0, 30);
3043 }
3044
3045 sb.append("create sequence " + sequenceName + ";");
3046
3047 String sequenceSQL = sb.toString();
3048
3049 if (!sequenceSQLs.contains(sequenceSQL)) {
3050 sequenceSQLs.add(sequenceSQL);
3051 }
3052 }
3053 }
3054 }
3055
3056 StringBuilder sb = new StringBuilder();
3057
3058 Iterator<String> itr = sequenceSQLs.iterator();
3059
3060 while (itr.hasNext()) {
3061 String sequenceSQL = itr.next();
3062
3063 sb.append(sequenceSQL);
3064
3065 if (itr.hasNext()) {
3066 sb.append("\n");
3067 }
3068 }
3069
3070 FileUtil.write(sqlFile, sb.toString(), true);
3071 }
3072
3073 private void _createSQLTables() throws IOException {
3074 if (!FileUtil.exists(_sqlDir)) {
3075 return;
3076 }
3077
3078 File sqlFile = new File(_sqlDir + "/" + _sqlFileName);
3079
3080 if (!sqlFile.exists()) {
3081 FileUtil.write(sqlFile, StringPool.BLANK);
3082 }
3083
3084 for (int i = 0; i < _ejbList.size(); i++) {
3085 Entity entity = _ejbList.get(i);
3086
3087 if (!entity.isDefaultDataSource()) {
3088 continue;
3089 }
3090
3091 String createTableSQL = _getCreateTableSQL(entity);
3092
3093 if (Validator.isNotNull(createTableSQL)) {
3094 _createSQLTables(sqlFile, createTableSQL, entity, true);
3095
3096 File updateSQLFile = new File(
3097 _sqlDir + "/update-5.1.1-5.1.2.sql");
3098
3099 if (updateSQLFile.exists()) {
3100 _createSQLTables(
3101 updateSQLFile, createTableSQL, entity, false);
3102 }
3103 }
3104 }
3105
3106 for (Map.Entry<String, EntityMapping> entry :
3107 _entityMappings.entrySet()) {
3108
3109 EntityMapping entityMapping = entry.getValue();
3110
3111 String createMappingTableSQL = _getCreateMappingTableSQL(
3112 entityMapping);
3113
3114 if (Validator.isNotNull(createMappingTableSQL)) {
3115 _createSQLMappingTables(
3116 sqlFile, createMappingTableSQL, entityMapping, true);
3117 }
3118 }
3119 }
3120
3121 private void _createSQLTables(
3122 File sqlFile, String newCreateTableString, Entity entity,
3123 boolean addMissingTables)
3124 throws IOException {
3125
3126 if (!sqlFile.exists()) {
3127 FileUtil.write(sqlFile, StringPool.BLANK);
3128 }
3129
3130 String content = FileUtil.read(sqlFile);
3131
3132 int x = content.indexOf(_SQL_CREATE_TABLE + entity.getTable() + " (");
3133 int y = content.indexOf(");", x);
3134
3135 if (x != -1) {
3136 String oldCreateTableString = content.substring(x + 1, y);
3137
3138 if (!oldCreateTableString.equals(newCreateTableString)) {
3139 content =
3140 content.substring(0, x) + newCreateTableString +
3141 content.substring(y + 2, content.length());
3142
3143 FileUtil.write(sqlFile, content);
3144 }
3145 }
3146 else if (addMissingTables) {
3147 StringBuilder sb = new StringBuilder();
3148
3149 BufferedReader br = new BufferedReader(new StringReader(content));
3150
3151 String line = null;
3152 boolean appendNewTable = true;
3153
3154 while ((line = br.readLine()) != null) {
3155 if (appendNewTable && line.startsWith(_SQL_CREATE_TABLE)) {
3156 x = _SQL_CREATE_TABLE.length();
3157 y = line.indexOf(" ", x);
3158
3159 String tableName = line.substring(x, y);
3160
3161 if (tableName.compareTo(entity.getTable()) > 0) {
3162 sb.append(newCreateTableString + "\n\n");
3163
3164 appendNewTable = false;
3165 }
3166 }
3167
3168 sb.append(line);
3169 sb.append("\n");
3170 }
3171
3172 if (appendNewTable) {
3173 sb.append("\n" + newCreateTableString);
3174 }
3175
3176 br.close();
3177
3178 FileUtil.write(sqlFile, sb.toString(), true);
3179 }
3180 }
3181
3182 private String _fixHbmXml(String content) throws IOException {
3183 StringBuilder sb = new StringBuilder();
3184
3185 BufferedReader br = new BufferedReader(new StringReader(content));
3186
3187 String line = null;
3188
3189 while ((line = br.readLine()) != null) {
3190 if (line.startsWith("\t<class name=\"")) {
3191 line = StringUtil.replace(
3192 line,
3193 new String[] {
3194 ".service.persistence.", "HBM\" table=\""
3195 },
3196 new String[] {
3197 ".model.", "\" table=\""
3198 });
3199
3200 if (line.indexOf(".model.impl.") == -1) {
3201 line = StringUtil.replace(
3202 line,
3203 new String[] {
3204 ".model.", "\" table=\""
3205 },
3206 new String[] {
3207 ".model.impl.", "Impl\" table=\""
3208 });
3209 }
3210 }
3211
3212 sb.append(line);
3213 sb.append('\n');
3214 }
3215
3216 br.close();
3217
3218 return sb.toString().trim();
3219 }
3220
3221 private String _fixSpringXml(String content) {
3222 return StringUtil.replace(content, ".service.spring.", ".service.");
3223 }
3224
3225 private String _formatXml(String xml)
3226 throws DocumentException, IOException {
3227
3228 String doctype = null;
3229
3230 int x = xml.indexOf("<!DOCTYPE");
3231
3232 if (x != -1) {
3233 int y = xml.indexOf(">", x) + 1;
3234
3235 doctype = xml.substring(x, y);
3236
3237 xml = xml.substring(0, x) + "\n" + xml.substring(y);
3238 }
3239
3240 xml = StringUtil.replace(xml, '\r', "");
3241 xml = XMLFormatter.toString(xml);
3242 xml = StringUtil.replace(xml, "\"/>", "\" />");
3243
3244 if (Validator.isNotNull(doctype)) {
3245 x = xml.indexOf("?>") + 2;
3246
3247 xml = xml.substring(0, x) + "\n" + doctype + xml.substring(x);
3248 }
3249
3250 return xml;
3251 }
3252
3253 private Map<String, Object> _getContext() throws TemplateModelException {
3254 BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
3255
3256 TemplateHashModel staticModels = wrapper.getStaticModels();
3257
3258 Map<String, Object> context = new HashMap<String, Object>();
3259
3260 context.put("hbmFileName", _hbmFileName);
3261 context.put("modelHintsFileName", _modelHintsFileName);
3262 context.put("springFileName", _springFileName);
3263 context.put("springBaseFileName", _springBaseFileName);
3264 context.put("springHibernateFileName", _springHibernateFileName);
3265 context.put("springInfrastructureFileName", _springInfrastructureFileName);
3266 context.put("apiDir", _apiDir);
3267 context.put("implDir", _implDir);
3268 context.put("jsonFileName", _jsonFileName);
3269 context.put("sqlDir", _sqlDir);
3270 context.put("sqlFileName", _sqlFileName);
3271 context.put("beanLocatorUtil", _beanLocatorUtil);
3272 context.put("beanLocatorUtilShortName", _beanLocatorUtilShortName);
3273 context.put("propsUtil", _propsUtil);
3274 context.put("portletName", _portletName);
3275 context.put("portletShortName", _portletShortName);
3276 context.put("portletPackageName", _portletPackageName);
3277 context.put("outputPath", _outputPath);
3278 context.put("serviceOutputPath", _serviceOutputPath);
3279 context.put("packagePath", _packagePath);
3280 context.put("pluginName", _pluginName);
3281 context.put("serviceBuilder", this);
3282
3283 context.put("arrayUtil", ArrayUtil_IW.getInstance());
3284 context.put(
3285 "modelHintsUtil",
3286 staticModels.get("com.liferay.portal.model.ModelHintsUtil"));
3287 context.put("stringUtil", StringUtil_IW.getInstance());
3288 context.put("system", staticModels.get("java.lang.System"));
3289 context.put("tempMap", wrapper.wrap(new HashMap<String, Object>()));
3290 context.put(
3291 "validator",
3292 staticModels.get("com.liferay.portal.kernel.util.Validator"));
3293
3294 return context;
3295 }
3296
3297 private String _getCreateMappingTableSQL(EntityMapping entityMapping)
3298 throws IOException {
3299
3300 Entity[] entities = new Entity[2];
3301
3302 for (int i = 0; i < entities.length; i++) {
3303 entities[i] = getEntity(entityMapping.getEntity(i));
3304
3305 if (entities[i] == null) {
3306 return null;
3307 }
3308 }
3309
3310 StringBuilder sb = new StringBuilder();
3311
3312 sb.append(_SQL_CREATE_TABLE + entityMapping.getTable() + " (\n");
3313
3314 for (Entity entity : entities) {
3315 List<EntityColumn> pkList = entity.getPKList();
3316
3317 for (int i = 0; i < pkList.size(); i++) {
3318 EntityColumn col = pkList.get(i);
3319
3320 String colName = col.getName();
3321 String colType = col.getType();
3322
3323 sb.append("\t" + col.getDBName());
3324 sb.append(" ");
3325
3326 if (colType.equalsIgnoreCase("boolean")) {
3327 sb.append("BOOLEAN");
3328 }
3329 else if (colType.equalsIgnoreCase("double") ||
3330 colType.equalsIgnoreCase("float")) {
3331
3332 sb.append("DOUBLE");
3333 }
3334 else if (colType.equals("int") ||
3335 colType.equals("Integer") ||
3336 colType.equalsIgnoreCase("short")) {
3337
3338 sb.append("INTEGER");
3339 }
3340 else if (colType.equalsIgnoreCase("long")) {
3341 sb.append("LONG");
3342 }
3343 else if (colType.equals("String")) {
3344 Map<String, String> hints = ModelHintsUtil.getHints(
3345 _packagePath + ".model." + entity.getName(), colName);
3346
3347 int maxLength = 75;
3348
3349 if (hints != null) {
3350 maxLength = GetterUtil.getInteger(
3351 hints.get("max-length"), maxLength);
3352 }
3353
3354 if (maxLength < 4000) {
3355 sb.append("VARCHAR(" + maxLength + ")");
3356 }
3357 else if (maxLength == 4000) {
3358 sb.append("STRING");
3359 }
3360 else if (maxLength > 4000) {
3361 sb.append("TEXT");
3362 }
3363 }
3364 else if (colType.equals("Date")) {
3365 sb.append("DATE null");
3366 }
3367 else {
3368 sb.append("invalid");
3369 }
3370
3371 if (col.isPrimary()) {
3372 sb.append(" not null");
3373 }
3374
3375 sb.append(",\n");
3376 }
3377 }
3378
3379 sb.append("\tprimary key (");
3380
3381 for (int i = 0; i < entities.length; i++) {
3382 Entity entity = entities[i];
3383
3384 List<EntityColumn> pkList = entity.getPKList();
3385
3386 for (int j = 0; j < pkList.size(); j++) {
3387 EntityColumn col = pkList.get(j);
3388
3389 String colName = col.getName();
3390
3391 if ((i != 0) || (j != 0)) {
3392 sb.append(", ");
3393 }
3394
3395 sb.append(colName);
3396 }
3397 }
3398
3399 sb.append(")\n");
3400 sb.append(");");
3401
3402 return sb.toString();
3403 }
3404
3405 private String _getCreateTableSQL(Entity entity) {
3406 List<EntityColumn> pkList = entity.getPKList();
3407 List<EntityColumn> regularColList = entity.getRegularColList();
3408
3409 if (regularColList.size() == 0) {
3410 return null;
3411 }
3412
3413 StringBuilder sb = new StringBuilder();
3414
3415 sb.append(_SQL_CREATE_TABLE + entity.getTable() + " (\n");
3416
3417 for (int i = 0; i < regularColList.size(); i++) {
3418 EntityColumn col = regularColList.get(i);
3419
3420 String colName = col.getName();
3421 String colType = col.getType();
3422 String colIdType = col.getIdType();
3423
3424 sb.append("\t" + col.getDBName());
3425 sb.append(" ");
3426
3427 if (colType.equalsIgnoreCase("boolean")) {
3428 sb.append("BOOLEAN");
3429 }
3430 else if (colType.equalsIgnoreCase("double") ||
3431 colType.equalsIgnoreCase("float")) {
3432
3433 sb.append("DOUBLE");
3434 }
3435 else if (colType.equals("int") ||
3436 colType.equals("Integer") ||
3437 colType.equalsIgnoreCase("short")) {
3438
3439 sb.append("INTEGER");
3440 }
3441 else if (colType.equalsIgnoreCase("long")) {
3442 sb.append("LONG");
3443 }
3444 else if (colType.equals("String")) {
3445 Map<String, String> hints = ModelHintsUtil.getHints(
3446 _packagePath + ".model." + entity.getName(), colName);
3447
3448 int maxLength = 75;
3449
3450 if (hints != null) {
3451 maxLength = GetterUtil.getInteger(
3452 hints.get("max-length"), maxLength);
3453 }
3454
3455 if (maxLength < 4000) {
3456 sb.append("VARCHAR(" + maxLength + ")");
3457 }
3458 else if (maxLength == 4000) {
3459 sb.append("STRING");
3460 }
3461 else if (maxLength > 4000) {
3462 sb.append("TEXT");
3463 }
3464 }
3465 else if (colType.equals("Date")) {
3466 sb.append("DATE null");
3467 }
3468 else {
3469 sb.append("invalid");
3470 }
3471
3472 if (col.isPrimary()) {
3473 sb.append(" not null");
3474
3475 if (!entity.hasCompoundPK()) {
3476 sb.append(" primary key");
3477 }
3478 }
3479 else if (colType.equals("String")) {
3480 sb.append(" null");
3481 }
3482
3483 if (Validator.isNotNull(colIdType) &&
3484 colIdType.equals("identity")) {
3485
3486 sb.append(" IDENTITY");
3487 }
3488
3489 if (((i + 1) != regularColList.size()) ||
3490 (entity.hasCompoundPK())) {
3491
3492 sb.append(",");
3493 }
3494
3495 sb.append("\n");
3496 }
3497
3498 if (entity.hasCompoundPK()) {
3499 sb.append("\tprimary key (");
3500
3501 for (int j = 0; j < pkList.size(); j++) {
3502 EntityColumn pk = pkList.get(j);
3503
3504 sb.append(pk.getDBName());
3505
3506 if ((j + 1) != pkList.size()) {
3507 sb.append(", ");
3508 }
3509 }
3510
3511 sb.append(")\n");
3512 }
3513
3514 sb.append(");");
3515
3516 return sb.toString();
3517 }
3518
3519 private String _getDimensions(Type type) {
3520 String dimensions = "";
3521
3522 for (int i = 0; i < type.getDimensions(); i++) {
3523 dimensions += "[]";
3524 }
3525
3526 return dimensions;
3527 }
3528
3529 private JavaClass _getJavaClass(String fileName) throws IOException {
3530 int pos = fileName.indexOf(_implDir + "/");
3531
3532 if (pos != -1) {
3533 pos += _implDir.length();
3534 }
3535 else {
3536 pos = fileName.indexOf(_apiDir + "/") + _apiDir.length();
3537 }
3538
3539 String srcFile = fileName.substring(pos + 1, fileName.length());
3540 String className = StringUtil.replace(
3541 srcFile.substring(0, srcFile.length() - 5), "/", ".");
3542
3543 JavaDocBuilder builder = new JavaDocBuilder();
3544
3545 File file = new File(fileName);
3546
3547 if (!file.exists()) {
3548 return null;
3549 }
3550
3551 builder.addSource(file);
3552
3553 return builder.getClassByName(className);
3554 }
3555
3556 private JavaMethod[] _getMethods(JavaClass javaClass) {
3557 JavaMethod[] methods = javaClass.getMethods();
3558
3559 for (JavaMethod method : methods) {
3560 Arrays.sort(method.getExceptions());
3561 }
3562
3563 return methods;
3564 }
3565
3566 private String _getSessionTypeName(int sessionType) {
3567 if (sessionType == _SESSION_TYPE_LOCAL) {
3568 return "Local";
3569 }
3570 else {
3571 return "";
3572 }
3573 }
3574
3575 private String _getTplProperty(String key, String defaultValue) {
3576 return System.getProperty("service.tpl." + key, defaultValue);
3577 }
3578
3579 private boolean _hasHttpMethods(JavaClass javaClass) {
3580 JavaMethod[] methods = _getMethods(javaClass);
3581
3582 for (int i = 0; i < methods.length; i++) {
3583 JavaMethod javaMethod = methods[i];
3584
3585 if (!javaMethod.isConstructor() && javaMethod.isPublic() &&
3586 isCustomMethod(javaMethod)) {
3587
3588 return true;
3589 }
3590 }
3591
3592 return false;
3593 }
3594
3595 private List<Entity> _mergeReferenceList(List<Entity> referenceList) {
3596 List<Entity> list = new ArrayList<Entity>(
3597 _ejbList.size() + referenceList.size());
3598
3599 list.addAll(_ejbList);
3600 list.addAll(referenceList);
3601
3602 return list;
3603 }
3604
3605 private String _processTemplate(String name) throws Exception {
3606 return _processTemplate(name, _getContext());
3607 }
3608
3609 private String _processTemplate(String name, Map<String, Object> context)
3610 throws Exception {
3611
3612 return FreeMarkerUtil.process(name, context);
3613 }
3614
3615 private static final String _AUTHOR = "Brian Wing Shun Chan";
3616
3617 private static final int _SESSION_TYPE_REMOTE = 0;
3618
3619 private static final int _SESSION_TYPE_LOCAL = 1;
3620
3621 private static final String _SQL_CREATE_TABLE = "create table ";
3622
3623 private static final String _TPL_ROOT =
3624 "com/liferay/portal/tools/servicebuilder/dependencies/";
3625
3626 private String _tplBadColumnNames = _TPL_ROOT + "bad_column_names.txt";
3627 private String _tplBadTableNames = _TPL_ROOT + "bad_table_names.txt";
3628 private String _tplEjbPk = _TPL_ROOT + "ejb_pk.ftl";
3629 private String _tplException = _TPL_ROOT + "exception.ftl";
3630 private String _tplExtendedModel = _TPL_ROOT + "extended_model.ftl";
3631 private String _tplExtendedModelImpl =
3632 _TPL_ROOT + "extended_model_impl.ftl";
3633 private String _tplFinder = _TPL_ROOT + "finder.ftl";
3634 private String _tplFinderUtil = _TPL_ROOT + "finder_util.ftl";
3635 private String _tplHbmXml = _TPL_ROOT + "hbm_xml.ftl";
3636 private String _tplJsonJs = _TPL_ROOT + "json_js.ftl";
3637 private String _tplJsonJsMethod = _TPL_ROOT + "json_js_method.ftl";
3638 private String _tplModel = _TPL_ROOT + "model.ftl";
3639 private String _tplModelClp = _TPL_ROOT + "model_clp.ftl";
3640 private String _tplModelHintsXml = _TPL_ROOT + "model_hints_xml.ftl";
3641 private String _tplModelImpl = _TPL_ROOT + "model_impl.ftl";
3642 private String _tplModelSoap = _TPL_ROOT + "model_soap.ftl";
3643 private String _tplPersistence = _TPL_ROOT + "persistence.ftl";
3644 private String _tplPersistenceImpl = _TPL_ROOT + "persistence_impl.ftl";
3645 private String _tplPersistenceTest = _TPL_ROOT + "persistence_test.ftl";
3646 private String _tplPersistenceUtil = _TPL_ROOT + "persistence_util.ftl";
3647 private String _tplProps = _TPL_ROOT + "props.ftl";
3648 private String _tplRemotingXml = _TPL_ROOT + "remoting_xml.ftl";
3649 private String _tplService = _TPL_ROOT + "service.ftl";
3650 private String _tplServiceBaseImpl = _TPL_ROOT + "service_base_impl.ftl";
3651 private String _tplServiceClp = _TPL_ROOT + "service_clp.ftl";
3652 private String _tplServiceClpSerializer =
3653 _TPL_ROOT + "service_clp_serializer.ftl";
3654 private String _tplServiceFactory = _TPL_ROOT + "service_factory.ftl";
3655 private String _tplServiceHttp = _TPL_ROOT + "service_http.ftl";
3656 private String _tplServiceImpl = _TPL_ROOT + "service_impl.ftl";
3657 private String _tplServiceJson = _TPL_ROOT + "service_json.ftl";
3658 private String _tplServiceJsonSerializer =
3659 _TPL_ROOT + "service_json_serializer.ftl";
3660 private String _tplServiceSoap = _TPL_ROOT + "service_soap.ftl";
3661 private String _tplServiceUtil = _TPL_ROOT + "service_util.ftl";
3662 private String _tplSpringBaseXml = _TPL_ROOT + "spring_base_xml.ftl";
3663 private String _tplSpringDynamicDataSourceXml =
3664 _TPL_ROOT + "spring_dynamic_data_source_xml.ftl";
3665 private String _tplSpringHibernateXml =
3666 _TPL_ROOT + "spring_hibernate_xml.ftl";
3667 private String _tplSpringInfrastructureXml =
3668 _TPL_ROOT + "spring_infrastructure_xml.ftl";
3669 private String _tplSpringXml = _TPL_ROOT + "spring_xml.ftl";
3670 private Set<String> _badTableNames;
3671 private Set<String> _badColumnNames;
3672 private String _hbmFileName;
3673 private String _modelHintsFileName;
3674 private String _springFileName;
3675 private String _springBaseFileName;
3676 private String _springDynamicDataSourceFileName;
3677 private String _springHibernateFileName;
3678 private String _springInfrastructureFileName;
3679 private String _apiDir;
3680 private String _implDir;
3681 private String _jsonFileName;
3682 private String _remotingFileName;
3683 private String _sqlDir;
3684 private String _sqlFileName;
3685 private String _sqlIndexesFileName;
3686 private String _sqlIndexesPropertiesFileName;
3687 private String _sqlSequencesFileName;
3688 private boolean _autoNamespaceTables;
3689 private String _beanLocatorUtil;
3690 private String _beanLocatorUtilShortName;
3691 private String _propsUtil;
3692 private String _pluginName;
3693 private String _testDir;
3694 private String _author;
3695 private String _portletName = StringPool.BLANK;
3696 private String _portletShortName = StringPool.BLANK;
3697 private String _portletPackageName = StringPool.BLANK;
3698 private String _outputPath;
3699 private String _serviceOutputPath;
3700 private String _testOutputPath;
3701 private String _packagePath;
3702 private List<Entity> _ejbList;
3703 private Map<String, EntityMapping> _entityMappings;
3704 private Map<String, Entity> _entityPool = new HashMap<String, Entity>();
3705
3706}