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