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.StringPool;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.kernel.util.StringUtil_IW;
34 import com.liferay.portal.kernel.util.Validator;
35 import com.liferay.portal.model.ModelHintsUtil;
36 import com.liferay.portal.tools.SourceFormatter;
37 import com.liferay.portal.util.InitUtil;
38 import com.liferay.portal.util.PropsValues;
39 import com.liferay.util.SetUtil;
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
1190 noSuchEntityException = noSuchEntityException.substring(
1191 entity.getPortletShortName().length());
1192 }
1193
1194 noSuchEntityException = "NoSuch" + noSuchEntityException;
1195
1196 return noSuchEntityException;
1197 }
1198
1199 public String getPrimitiveObj(String type) {
1200 if (type.equals("boolean")) {
1201 return "Boolean";
1202 }
1203 else if (type.equals("double")) {
1204 return "Double";
1205 }
1206 else if (type.equals("float")) {
1207 return "Float";
1208 }
1209 else if (type.equals("int")) {
1210 return "Integer";
1211 }
1212 else if (type.equals("long")) {
1213 return "Long";
1214 }
1215 else if (type.equals("short")) {
1216 return "Short";
1217 }
1218 else {
1219 return type;
1220 }
1221 }
1222
1223 public String getPrimitiveObjValue(String colType) {
1224 if (colType.equals("Boolean")) {
1225 return ".booleanValue()";
1226 }
1227 else if (colType.equals("Double")) {
1228 return ".doubleValue()";
1229 }
1230 else if (colType.equals("Float")) {
1231 return ".floatValue()";
1232 }
1233 else if (colType.equals("Integer")) {
1234 return ".intValue()";
1235 }
1236 else if (colType.equals("Long")) {
1237 return ".longValue()";
1238 }
1239 else if (colType.equals("Short")) {
1240 return ".shortValue()";
1241 }
1242
1243 return StringPool.BLANK;
1244 }
1245
1246 public String getSqlType(String model, String field, String type) {
1247 if (type.equals("boolean") || type.equals("Boolean")) {
1248 return "BOOLEAN";
1249 }
1250 else if (type.equals("double") || type.equals("Double")) {
1251 return "DOUBLE";
1252 }
1253 else if (type.equals("float") || type.equals("Float")) {
1254 return "FLOAT";
1255 }
1256 else if (type.equals("int") || type.equals("Integer")) {
1257 return "INTEGER";
1258 }
1259 else if (type.equals("long") || type.equals("Long")) {
1260 return "BIGINT";
1261 }
1262 else if (type.equals("short") || type.equals("Short")) {
1263 return "INTEGER";
1264 }
1265 else if (type.equals("Date")) {
1266 return "TIMESTAMP";
1267 }
1268 else if (type.equals("String")) {
1269 Map<String, String> hints = ModelHintsUtil.getHints(model, field);
1270
1271 if (hints != null) {
1272 int maxLength = GetterUtil.getInteger(hints.get("max-length"));
1273
1274 if (maxLength == 2000000) {
1275 return "CLOB";
1276 }
1277 }
1278
1279 return "VARCHAR";
1280 }
1281 else {
1282 return null;
1283 }
1284 }
1285
1286 public boolean hasEntityByGenericsName(String genericsName) {
1287 if (Validator.isNull(genericsName)) {
1288 return false;
1289 }
1290
1291 if (genericsName.indexOf(".model.") == -1) {
1292 return false;
1293 }
1294
1295 if (getEntityByGenericsName(genericsName) == null) {
1296 return false;
1297 }
1298 else {
1299 return true;
1300 }
1301 }
1302
1303 public boolean hasEntityByParameterTypeValue(String parameterTypeValue) {
1304 if (Validator.isNull(parameterTypeValue)) {
1305 return false;
1306 }
1307
1308 if (parameterTypeValue.indexOf(".model.") == -1) {
1309 return false;
1310 }
1311
1312 if (getEntityByParameterTypeValue(parameterTypeValue) == null) {
1313 return false;
1314 }
1315 else {
1316 return true;
1317 }
1318 }
1319
1320 public boolean isCustomMethod(JavaMethod method) {
1321 String methodName = method.getName();
1322
1323 if (methodName.equals("afterPropertiesSet") ||
1324 methodName.equals("equals") ||
1325 methodName.equals("getClass") ||
1326 methodName.equals("hashCode") ||
1327 methodName.equals("notify") ||
1328 methodName.equals("notifyAll") ||
1329 methodName.equals("toString") ||
1330 methodName.equals("wait")) {
1331
1332 return false;
1333 }
1334 else if (methodName.equals("getPermissionChecker")) {
1335 return false;
1336 }
1337 else if (methodName.equals("getUser") &&
1338 method.getParameters().length == 0) {
1339
1340 return false;
1341 }
1342 else if (methodName.equals("getUserId") &&
1343 method.getParameters().length == 0) {
1344
1345 return false;
1346 }
1347 else if ((methodName.endsWith("Finder")) &&
1348 (methodName.startsWith("get") ||
1349 methodName.startsWith("set"))) {
1350
1351 return false;
1352 }
1353 else if ((methodName.endsWith("Persistence")) &&
1354 (methodName.startsWith("get") ||
1355 methodName.startsWith("set"))) {
1356
1357 return false;
1358 }
1359 else if ((methodName.endsWith("Service")) &&
1360 (methodName.startsWith("get") ||
1361 methodName.startsWith("set"))) {
1362
1363 return false;
1364 }
1365 else {
1366 return true;
1367 }
1368 }
1369
1370 public boolean isDuplicateMethod(
1371 JavaMethod method, Map<String, Object> tempMap) {
1372
1373 StringBuilder sb = new StringBuilder();
1374
1375 sb.append("isDuplicateMethod ");
1376 sb.append(method.getReturns().getValue());
1377 sb.append(method.getReturnsGenericsName());
1378 sb.append(getDimensions(method.getReturns().getDimensions()));
1379 sb.append(StringPool.SPACE);
1380 sb.append(method.getName());
1381 sb.append(StringPool.OPEN_PARENTHESIS);
1382
1383 JavaParameter[] parameters = method.getParameters();
1384
1385 for (int i = 0; i < parameters.length; i++) {
1386 JavaParameter javaParameter = parameters[i];
1387
1388 sb.append(javaParameter.getType().getValue());
1389 sb.append(javaParameter.getGenericsName());
1390 sb.append(getDimensions(javaParameter.getType().getDimensions()));
1391
1392 if ((i + 1) != parameters.length) {
1393 sb.append(StringPool.COMMA);
1394 }
1395 }
1396
1397 sb.append(StringPool.CLOSE_PARENTHESIS);
1398
1399 String key = sb.toString();
1400
1401 if (tempMap.containsKey(key)) {
1402 return true;
1403 }
1404 else {
1405 tempMap.put(key, key);
1406
1407 return false;
1408 }
1409 }
1410
1411 public boolean isPersistenceReadOnlyMethod(JavaMethod method) {
1412 return isReadOnlyMethod(
1413 method, null,
1414 PropsValues.SERVICE_BUILDER_PERSISTENCE_READ_ONLY_PREFIXES);
1415 }
1416
1417 public boolean isServiceReadOnlyMethod(
1418 JavaMethod method, List<String> txRequiredList) {
1419
1420 return isReadOnlyMethod(
1421 method, txRequiredList,
1422 PropsValues.SERVICE_BUILDER_SERVICE_READ_ONLY_PREFIXES);
1423 }
1424
1425 public boolean isReadOnlyMethod(
1426 JavaMethod method, List<String> txRequiredList, String[] prefixes) {
1427
1428 String methodName = method.getName();
1429
1430 if (isTxRequiredMethod(method, txRequiredList)) {
1431 return false;
1432 }
1433
1434 for (String prefix : prefixes) {
1435 if (methodName.startsWith(prefix)) {
1436 return true;
1437 }
1438 }
1439
1440 return false;
1441 }
1442
1443 public boolean isSoapMethod(JavaMethod method) {
1444 String returnValueName = method.getReturns().getValue();
1445
1446 if (returnValueName.startsWith("java.io") ||
1447 returnValueName.equals("java.util.Map") ||
1448 returnValueName.equals("java.util.Properties") ||
1449 returnValueName.startsWith("javax")) {
1450
1451 return false;
1452 }
1453
1454 JavaParameter[] parameters = method.getParameters();
1455
1456 for (int i = 0; i < parameters.length; i++) {
1457 JavaParameter javaParameter = parameters[i];
1458
1459 String parameterTypeName =
1460 javaParameter.getType().getValue() +
1461 _getDimensions(javaParameter.getType());
1462
1463 if (parameterTypeName.equals(
1464 "com.liferay.portal.theme.ThemeDisplay") ||
1465 parameterTypeName.equals(
1466 "com.liferay.portlet.PortletPreferencesImpl") ||
1467 parameterTypeName.startsWith("java.io") ||
1468 parameterTypeName.startsWith("java.util.Map") ||
1471 parameterTypeName.startsWith("java.util.Properties") ||
1472 parameterTypeName.startsWith("javax")) {
1473
1474 return false;
1475 }
1476 }
1477
1478 return true;
1479 }
1480
1481 public boolean isTxRequiredMethod(
1482 JavaMethod method, List<String> txRequiredList) {
1483
1484 if (txRequiredList == null) {
1485 return false;
1486 }
1487
1488 String methodName = method.getName();
1489
1490 for (String txRequired : txRequiredList) {
1491 if (methodName.equals(txRequired)) {
1492 return true;
1493 }
1494 }
1495
1496 return false;
1497 }
1498
1499 private static String _getPackagePath(File file) {
1500 String fileName = StringUtil.replace(file.toString(), "\\", "/");
1501
1502 int x = fileName.indexOf("src/");
1503
1504 if (x == -1) {
1505 x = fileName.indexOf("test/");
1506 }
1507
1508 int y = fileName.lastIndexOf("/");
1509
1510 fileName = fileName.substring(x + 4, y);
1511
1512 return StringUtil.replace(fileName, "/", ".");
1513 }
1514
1515 private void _createEJBPK(Entity entity) throws Exception {
1516 Map<String, Object> context = _getContext();
1517
1518 context.put("entity", entity);
1519
1520
1522 String content = _processTemplate(_tplEjbPk, context);
1523
1524
1526 File ejbFile = new File(
1527 _serviceOutputPath + "/service/persistence/" +
1528 entity.getPKClassName() + ".java");
1529
1530 writeFile(ejbFile, content, _author);
1531 }
1532
1533 private void _createExceptions(List<String> exceptions) throws Exception {
1534 for (int i = 0; i < _ejbList.size(); i++) {
1535 Entity entity = _ejbList.get(i);
1536
1537 if (entity.hasColumns()) {
1538 exceptions.add(getNoSuchEntityException(entity));
1539 }
1540 }
1541
1542 for (String exception : exceptions) {
1543 File exceptionFile = new File(
1544 _serviceOutputPath + "/" + exception + "Exception.java");
1545
1546 if (!exceptionFile.exists()) {
1547 Map<String, Object> context = _getContext();
1548
1549 context.put("exception", exception);
1550
1551 String content = _processTemplate(_tplException, context);
1552
1553 content = StringUtil.replace(content, "\r\n", "\n");
1554
1555 FileUtil.write(exceptionFile, content);
1556 }
1557
1558 if (!_serviceOutputPath.equals(_outputPath)) {
1559 exceptionFile = new File(
1560 _outputPath + "/" + exception + "Exception.java");
1561
1562 if (exceptionFile.exists()) {
1563 System.out.println("Relocating " + exceptionFile);
1564
1565 exceptionFile.delete();
1566 }
1567 }
1568 }
1569 }
1570
1571 private void _createExtendedModel(Entity entity) throws Exception {
1572 JavaClass javaClass = _getJavaClass(
1573 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
1574
1575 Map<String, Object> context = _getContext();
1576
1577 context.put("entity", entity);
1578 context.put("methods", _getMethods(javaClass));
1579
1580
1582 String content = _processTemplate(_tplExtendedModel, context);
1583
1584
1586 File modelFile = new File(
1587 _serviceOutputPath + "/model/" + entity.getName() + ".java");
1588
1589 Map<String, Object> jalopySettings = new HashMap<String, Object>();
1590
1591 jalopySettings.put("keepJavadoc", Boolean.TRUE);
1592
1593 writeFile(modelFile, content, _author, jalopySettings);
1594
1595 if (!_serviceOutputPath.equals(_outputPath)) {
1596 modelFile = new File(
1597 _outputPath + "/model/" + entity.getName() + ".java");
1598
1599 if (modelFile.exists()) {
1600 System.out.println("Relocating " + modelFile);
1601
1602 modelFile.delete();
1603 }
1604 }
1605 }
1606
1607 private void _createExtendedModelImpl(Entity entity) throws Exception {
1608 Map<String, Object> context = _getContext();
1609
1610 context.put("entity", entity);
1611
1612
1614 String content = _processTemplate(_tplExtendedModelImpl, context);
1615
1616
1618 File modelFile = new File(
1619 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
1620
1621 if (!modelFile.exists()) {
1622 writeFile(modelFile, content, _author);
1623 }
1624 }
1625
1626 private void _createFinder(Entity entity) throws Exception {
1627 if (!entity.hasFinderClass()) {
1628 return;
1629 }
1630
1631 JavaClass javaClass = _getJavaClass(
1632 _outputPath + "/service/persistence/" + entity.getName() +
1633 "FinderImpl.java");
1634
1635 Map<String, Object> context = _getContext();
1636
1637 context.put("entity", entity);
1638 context.put("methods", _getMethods(javaClass));
1639
1640
1642 String content = _processTemplate(_tplFinder, context);
1643
1644
1646 File ejbFile = new File(
1647 _serviceOutputPath + "/service/persistence/" + entity.getName() +
1648 "Finder.java");
1649
1650 writeFile(ejbFile, content, _author);
1651
1652 if (!_serviceOutputPath.equals(_outputPath)) {
1653 ejbFile = new File(
1654 _outputPath + "/service/persistence/" + entity.getName() +
1655 "Finder.java");
1656
1657 if (ejbFile.exists()) {
1658 System.out.println("Relocating " + ejbFile);
1659
1660 ejbFile.delete();
1661 }
1662 }
1663 }
1664
1665 private void _createFinderUtil(Entity entity) throws Exception {
1666 if (!entity.hasFinderClass()) {
1667 return;
1668 }
1669
1670 JavaClass javaClass = _getJavaClass(
1671 _outputPath + "/service/persistence/" + entity.getName() +
1672 "FinderImpl.java");
1673
1674 Map<String, Object> context = _getContext();
1675
1676 context.put("entity", entity);
1677 context.put("methods", _getMethods(javaClass));
1678
1679
1681 String content = _processTemplate(_tplFinderUtil, context);
1682
1683
1685 File ejbFile = new File(
1686 _serviceOutputPath + "/service/persistence/" + entity.getName() +
1687 "FinderUtil.java");
1688
1689 writeFile(ejbFile, content, _author);
1690
1691 if (!_serviceOutputPath.equals(_outputPath)) {
1692 ejbFile = new File(
1693 _outputPath + "/service/persistence/" + entity.getName() +
1694 "FinderUtil.java");
1695
1696 if (ejbFile.exists()) {
1697 System.out.println("Relocating " + ejbFile);
1698
1699 ejbFile.delete();
1700 }
1701 }
1702 }
1703
1704 private void _createHbm(Entity entity) {
1705 File ejbFile = new File(
1706 _outputPath + "/service/persistence/" + entity.getName() +
1707 "HBM.java");
1708
1709 if (ejbFile.exists()) {
1710 System.out.println("Removing deprecated " + ejbFile);
1711
1712 ejbFile.delete();
1713 }
1714 }
1715
1716 private void _createHbmUtil(Entity entity) {
1717 File ejbFile = new File(
1718 _outputPath + "/service/persistence/" + entity.getName() +
1719 "HBMUtil.java");
1720
1721 if (ejbFile.exists()) {
1722 System.out.println("Removing deprecated " + ejbFile);
1723
1724 ejbFile.delete();
1725 }
1726 }
1727
1728 private void _createHbmXml() throws Exception {
1729 Map<String, Object> context = _getContext();
1730
1731 context.put("entities", _ejbList);
1732
1733
1735 String content = _processTemplate(_tplHbmXml, context);
1736
1737 File xmlFile = new File(_hbmFileName);
1738
1739 if (!xmlFile.exists()) {
1740 String xml =
1741 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
1742 "<!DOCTYPE hibernate-mapping PUBLIC \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n" +
1743 "\n" +
1744 "<hibernate-mapping default-lazy=\"false\" auto-import=\"false\">\n" +
1745 "</hibernate-mapping>";
1746
1747 FileUtil.write(xmlFile, xml);
1748 }
1749
1750 String oldContent = FileUtil.read(xmlFile);
1751 String newContent = _fixHbmXml(oldContent);
1752
1753 int firstClass = newContent.indexOf(
1754 "<class name=\"" + _packagePath + ".model.impl.");
1755 int lastClass = newContent.lastIndexOf(
1756 "<class name=\"" + _packagePath + ".model.impl.");
1757
1758 if (firstClass == -1) {
1759 int x = newContent.indexOf("</hibernate-mapping>");
1760
1761 if (x != -1) {
1762 newContent =
1763 newContent.substring(0, x) + content +
1764 newContent.substring(x, newContent.length());
1765 }
1766 }
1767 else {
1768 firstClass = newContent.lastIndexOf("<class", firstClass) - 1;
1769 lastClass = newContent.indexOf("</class>", lastClass) + 9;
1770
1771 newContent =
1772 newContent.substring(0, firstClass) + content +
1773 newContent.substring(lastClass, newContent.length());
1774 }
1775
1776 newContent = _formatXml(newContent);
1777
1778 if (!oldContent.equals(newContent)) {
1779 FileUtil.write(xmlFile, newContent);
1780 }
1781 }
1782
1783 private void _createJsonJs() throws Exception {
1784 StringBuilder content = new StringBuilder();
1785
1786 if (_ejbList.size() > 0) {
1787 content.append(_processTemplate(_tplJsonJs));
1788 }
1789
1790 for (int i = 0; i < _ejbList.size(); i++) {
1791 Entity entity = _ejbList.get(i);
1792
1793 if (entity.hasRemoteService()) {
1794 JavaClass javaClass = _getJavaClass(
1795 _outputPath + "/service/http/" + entity.getName() +
1796 "ServiceJSON.java");
1797
1798 JavaMethod[] methods = _getMethods(javaClass);
1799
1800 Set<String> jsonMethods = new LinkedHashSet<String>();
1801
1802 for (int j = 0; j < methods.length; j++) {
1803 JavaMethod javaMethod = methods[j];
1804
1805 String methodName = javaMethod.getName();
1806
1807 if (javaMethod.isPublic()) {
1808 jsonMethods.add(methodName);
1809 }
1810 }
1811
1812 if (jsonMethods.size() > 0) {
1813 Map<String, Object> context = _getContext();
1814
1815 context.put("entity", entity);
1816 context.put("methods", jsonMethods);
1817
1818 content.append("\n\n");
1819 content.append(_processTemplate(_tplJsonJsMethod, context));
1820 }
1821 }
1822 }
1823
1824 File jsonFile = new File(_jsonFileName);
1825
1826 if (!jsonFile.exists()) {
1827 FileUtil.write(jsonFile, "");
1828 }
1829
1830 String oldContent = FileUtil.read(jsonFile);
1831 String newContent = new String(oldContent);
1832
1833 int oldBegin = oldContent.indexOf(
1834 "Liferay.Service." + _portletShortName);
1835
1836 int oldEnd = oldContent.lastIndexOf(
1837 "Liferay.Service." + _portletShortName);
1838
1839 oldEnd = oldContent.indexOf("};", oldEnd);
1840
1841 int newBegin = newContent.indexOf(
1842 "Liferay.Service." + _portletShortName);
1843
1844 int newEnd = newContent.lastIndexOf(
1845 "Liferay.Service." + _portletShortName);
1846
1847 newEnd = newContent.indexOf("};", newEnd);
1848
1849 if (newBegin == -1) {
1850 newContent = oldContent + "\n\n" + content.toString().trim();
1851 }
1852 else {
1853 newContent =
1854 newContent.substring(0, oldBegin) + content.toString().trim() +
1855 newContent.substring(oldEnd + 2, newContent.length());
1856 }
1857
1858 if (!oldContent.equals(newContent)) {
1859 FileUtil.write(jsonFile, newContent);
1860 }
1861 }
1862
1863 private void _createModel(Entity entity) throws Exception {
1864 Map<String, Object> context = _getContext();
1865
1866 context.put("entity", entity);
1867
1868
1870 String content = _processTemplate(_tplModel, context);
1871
1872
1874 File modelFile = new File(
1875 _serviceOutputPath + "/model/" + entity.getName() + "Model.java");
1876
1877 Map<String, Object> jalopySettings = new HashMap<String, Object>();
1878
1879 jalopySettings.put("keepJavadoc", Boolean.TRUE);
1880
1881 writeFile(modelFile, content, _author, jalopySettings);
1882
1883 if (!_serviceOutputPath.equals(_outputPath)) {
1884 modelFile = new File(
1885 _outputPath + "/model/" + entity.getName() + "Model.java");
1886
1887 if (modelFile.exists()) {
1888 System.out.println("Relocating " + modelFile);
1889
1890 modelFile.delete();
1891 }
1892 }
1893 }
1894
1895 private void _createModelClp(Entity entity) throws Exception {
1896 if (Validator.isNull(_pluginName)) {
1897 return;
1898 }
1899
1900 JavaClass javaClass = _getJavaClass(
1901 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
1902
1903 Map<String, Object> context = _getContext();
1904
1905 context.put("entity", entity);
1906 context.put("methods", _getMethods(javaClass));
1907
1908
1910 String content = _processTemplate(_tplModelClp, context);
1911
1912
1914 File modelFile = new File(
1915 _serviceOutputPath + "/model/" + entity.getName() + "Clp.java");
1916
1917 writeFile(modelFile, content, _author);
1918 }
1919
1920 private void _createModelHintsXml() throws Exception {
1921 Map<String, Object> context = _getContext();
1922
1923 context.put("entities", _ejbList);
1924
1925
1927 String content = _processTemplate(_tplModelHintsXml, context);
1928
1929 File xmlFile = new File(_modelHintsFileName);
1930
1931 if (!xmlFile.exists()) {
1932 String xml =
1933 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
1934 "\n" +
1935 "<model-hints>\n" +
1936 "</model-hints>";
1937
1938 FileUtil.write(xmlFile, xml);
1939 }
1940
1941 String oldContent = FileUtil.read(xmlFile);
1942 String newContent = new String(oldContent);
1943
1944 int firstModel = newContent.indexOf(
1945 "<model name=\"" + _packagePath + ".model.");
1946 int lastModel = newContent.lastIndexOf(
1947 "<model name=\"" + _packagePath + ".model.");
1948
1949 if (firstModel == -1) {
1950 int x = newContent.indexOf("</model-hints>");
1951
1952 newContent =
1953 newContent.substring(0, x) + content +
1954 newContent.substring(x, newContent.length());
1955 }
1956 else {
1957 firstModel = newContent.lastIndexOf("<model", firstModel) - 1;
1958 lastModel = newContent.indexOf("</model>", lastModel) + 9;
1959
1960 newContent =
1961 newContent.substring(0, firstModel) + content +
1962 newContent.substring(lastModel, newContent.length());
1963 }
1964
1965 newContent = _formatXml(newContent);
1966
1967 if (!oldContent.equals(newContent)) {
1968 FileUtil.write(xmlFile, newContent);
1969 }
1970 }
1971
1972 private void _createModelImpl(Entity entity) throws Exception {
1973 Map<String, Object> context = _getContext();
1974
1975 context.put("entity", entity);
1976
1977
1979 String content = _processTemplate(_tplModelImpl, context);
1980
1981
1983 File modelFile = new File(
1984 _outputPath + "/model/impl/" + entity.getName() + "ModelImpl.java");
1985
1986 Map<String, Object> jalopySettings = new HashMap<String, Object>();
1987
1988 jalopySettings.put("keepJavadoc", Boolean.TRUE);
1989
1990 writeFile(modelFile, content, _author, jalopySettings);
1991 }
1992
1993 private void _createModelSoap(Entity entity) throws Exception {
1994 Map<String, Object> context = _getContext();
1995
1996 context.put("entity", entity);
1997
1998
2000 String content = _processTemplate(_tplModelSoap, context);
2001
2002
2004 File modelFile = new File(
2005 _serviceOutputPath + "/model/" + entity.getName() + "Soap.java");
2006
2007 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2008
2009 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2010
2011 writeFile(modelFile, content, _author, jalopySettings);
2012
2013 if (!_serviceOutputPath.equals(_outputPath)) {
2014 modelFile = new File(
2015 _outputPath + "/model/" + entity.getName() + "Soap.java");
2016
2017 if (modelFile.exists()) {
2018 System.out.println("Relocating " + modelFile);
2019
2020 modelFile.delete();
2021 }
2022 }
2023 }
2024
2025 private void _createPersistence(Entity entity) throws Exception {
2026 JavaClass javaClass = _getJavaClass(
2027 _outputPath + "/service/persistence/" + entity.getName() +
2028 "PersistenceImpl.java");
2029
2030 Map<String, Object> context = _getContext();
2031
2032 context.put("entity", entity);
2033 context.put("methods", _getMethods(javaClass));
2034
2035
2037 String content = _processTemplate(_tplPersistence, context);
2038
2039
2041 File ejbFile = new File(
2042 _serviceOutputPath + "/service/persistence/" + entity.getName() +
2043 "Persistence.java");
2044
2045 writeFile(ejbFile, content, _author);
2046
2047 if (!_serviceOutputPath.equals(_outputPath)) {
2048 ejbFile = new File(
2049 _outputPath + "/service/persistence/" + entity.getName() +
2050 "Persistence.java");
2051
2052 if (ejbFile.exists()) {
2053 System.out.println("Relocating " + ejbFile);
2054
2055 ejbFile.delete();
2056 }
2057 }
2058 }
2059
2060 private void _createPersistenceImpl(Entity entity) throws Exception {
2061 Map<String, Object> context = _getContext();
2062
2063 context.put("entity", entity);
2064 context.put(
2065 "referenceList", _mergeReferenceList(entity.getReferenceList()));
2066
2067
2069 String content = _processTemplate(_tplPersistenceImpl, context);
2070
2071
2073 File ejbFile = new File(
2074 _outputPath + "/service/persistence/" + entity.getName() +
2075 "PersistenceImpl.java");
2076
2077 writeFile(ejbFile, content, _author);
2078 }
2079
2080 private void _createPersistenceTest(Entity entity) throws Exception {
2081 Map<String, Object> context = _getContext();
2082
2083 context.put("entity", entity);
2084
2085
2087 String content = _processTemplate(_tplPersistenceTest, context);
2088
2089
2091 File ejbFile = new File(
2092 _testOutputPath + "/service/persistence/" + entity.getName() +
2093 "PersistenceTest.java");
2094
2095 writeFile(ejbFile, content, _author);
2096 }
2097
2098 private void _createPersistenceUtil(Entity entity) throws Exception {
2099 JavaClass javaClass = _getJavaClass(
2100 _outputPath + "/service/persistence/" + entity.getName() +
2101 "PersistenceImpl.java");
2102
2103 Map<String, Object> context = _getContext();
2104
2105 context.put("entity", entity);
2106 context.put("methods", _getMethods(javaClass));
2107
2108
2110 String content = _processTemplate(_tplPersistenceUtil, context);
2111
2112
2114 File ejbFile = new File(
2115 _serviceOutputPath + "/service/persistence/" + entity.getName() +
2116 "Util.java");
2117
2118 writeFile(ejbFile, content, _author);
2119
2120 if (!_serviceOutputPath.equals(_outputPath)) {
2121 ejbFile = new File(
2122 _outputPath + "/service/persistence/" + entity.getName() +
2123 "Util.java");
2124
2125 if (ejbFile.exists()) {
2126 System.out.println("Relocating " + ejbFile);
2127
2128 ejbFile.delete();
2129 }
2130 }
2131 }
2132
2133 private void _createPool(Entity entity) {
2134 File ejbFile = new File(
2135 _outputPath + "/service/persistence/" + entity.getName() +
2136 "Pool.java");
2137
2138 if (ejbFile.exists()) {
2139 System.out.println("Removing deprecated " + ejbFile);
2140
2141 ejbFile.delete();
2142 }
2143 }
2144
2145 private void _createProps() throws Exception {
2146 if (Validator.isNull(_pluginName)) {
2147 return;
2148 }
2149
2150
2152 File propsFile = new File(_implDir + "/service.properties");
2153
2154 long buildNumber = 1;
2155
2156 if (propsFile.exists()) {
2157 Properties props = PropertiesUtil.load(FileUtil.read(propsFile));
2158
2159 buildNumber = GetterUtil.getLong(
2160 props.getProperty("build.number")) + 1;
2161 }
2162
2163 Map<String, Object> context = _getContext();
2164
2165 context.put("buildNumber", new Long(buildNumber));
2166 context.put("currentTimeMillis", new Long(System.currentTimeMillis()));
2167
2168 String content = _processTemplate(_tplProps, context);
2169
2170
2172 FileUtil.write(propsFile, content, true);
2173 }
2174
2175 private void _createRemotingXml() throws Exception {
2176 StringBuilder sb = new StringBuilder();
2177
2178 Document doc = SAXReaderUtil.read(new File(_springFileName), true);
2179
2180 Iterator<Element> itr = doc.getRootElement().elements(
2181 "bean").iterator();
2182
2183 while (itr.hasNext()) {
2184 Element beanEl = itr.next();
2185
2186 String beanId = beanEl.attributeValue("id");
2187
2188 if (beanId.endsWith("ServiceFactory") &&
2189 !beanId.endsWith("LocalServiceFactory")) {
2190
2191 String entityName = beanId;
2192
2193 entityName = StringUtil.replace(entityName, ".service.", ".");
2194
2195 int pos = entityName.indexOf("LocalServiceFactory");
2196
2197 if (pos == -1) {
2198 pos = entityName.indexOf("ServiceFactory");
2199 }
2200
2201 entityName = entityName.substring(0, pos);
2202
2203 Entity entity = getEntity(entityName);
2204
2205 String serviceName = beanId.substring(0, beanId.length() - 7);
2206
2207 String serviceMapping = serviceName;
2208
2209 serviceMapping = StringUtil.replace(
2210 serviceMapping, ".service.", ".service.spring.");
2211 serviceMapping = StringUtil.replace(
2212 serviceMapping, StringPool.PERIOD, StringPool.UNDERLINE);
2213
2214 Map<String, Object> context = _getContext();
2215
2216 context.put("entity", entity);
2217 context.put("serviceName", serviceName);
2218 context.put("serviceMapping", serviceMapping);
2219
2220 sb.append(_processTemplate(_tplRemotingXml, context));
2221 }
2222 }
2223
2224 File outputFile = new File(_remotingFileName);
2225
2226 if (!outputFile.exists()) {
2227 return;
2228 }
2229
2230 String content = FileUtil.read(outputFile);
2231 String newContent = content;
2232
2233 int x = content.indexOf("<bean ");
2234 int y = content.lastIndexOf("</bean>") + 8;
2235
2236 if (x != -1) {
2237 newContent =
2238 content.substring(0, x - 1) + sb.toString() +
2239 content.substring(y, content.length());
2240 }
2241 else {
2242 x = content.indexOf("</beans>");
2243
2244 if (x != -1) {
2245 newContent =
2246 content.substring(0, x) + sb.toString() +
2247 content.substring(x, content.length());
2248 }
2249 else {
2250 x = content.indexOf("<beans/>");
2251 y = x + 8;
2252
2253 newContent =
2254 content.substring(0, x) + "<beans>" + sb.toString() +
2255 "</beans>" + content.substring(y, content.length());
2256 }
2257 }
2258
2259 newContent = _formatXml(newContent);
2260
2261 if (!content.equals(newContent)) {
2262 FileUtil.write(outputFile, newContent);
2263
2264 System.out.println(outputFile.toString());
2265 }
2266 }
2267
2268 private void _createService(Entity entity, int sessionType)
2269 throws Exception {
2270
2271 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.";
2272
2273 JavaClass javaClass = _getJavaClass(_outputPath + "/service/impl/" + entity.getName() + (sessionType != _SESSION_TYPE_REMOTE ? "Local" : "") + "ServiceImpl.java");
2274
2275 JavaMethod[] methods = _getMethods(javaClass);
2276
2277 if (sessionType == _SESSION_TYPE_LOCAL) {
2278 if (javaClass.getSuperClass().getValue().endsWith(
2279 entity.getName() + "LocalServiceBaseImpl")) {
2280
2281 JavaClass parentJavaClass = _getJavaClass(
2282 _outputPath + "/service/base/" + entity.getName() +
2283 "LocalServiceBaseImpl.java");
2284
2285 JavaMethod[] parentMethods = parentJavaClass.getMethods();
2286
2287 JavaMethod[] allMethods = new JavaMethod[parentMethods.length + methods.length];
2288
2289 ArrayUtil.combine(parentMethods, methods, allMethods);
2290
2291 methods = allMethods;
2292 }
2293
2294 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.";
2295 }
2296
2297 Map<String, Object> context = _getContext();
2298
2299 context.put("entity", entity);
2300 context.put("methods", methods);
2301 context.put("sessionTypeName",_getSessionTypeName(sessionType));
2302 context.put("serviceComments", serviceComments);
2303
2304
2306 String content = _processTemplate(_tplService, context);
2307
2308
2310 File ejbFile = new File(
2311 _serviceOutputPath + "/service/" + entity.getName() +
2312 _getSessionTypeName(sessionType) + "Service.java");
2313
2314 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2315
2316 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2317
2318 writeFile(ejbFile, content, _author, jalopySettings);
2319
2320 if (!_serviceOutputPath.equals(_outputPath)) {
2321 ejbFile = new File(
2322 _outputPath + "/service/" + entity.getName() +
2323 _getSessionTypeName(sessionType) + "Service.java");
2324
2325 if (ejbFile.exists()) {
2326 System.out.println("Relocating " + ejbFile);
2327
2328 ejbFile.delete();
2329 }
2330 }
2331 }
2332
2333 private void _createServiceBaseImpl(Entity entity, int sessionType)
2334 throws Exception {
2335
2336 Map<String, Object> context = _getContext();
2337
2338 context.put("entity", entity);
2339 context.put("sessionTypeName",_getSessionTypeName(sessionType));
2340 context.put(
2341 "referenceList", _mergeReferenceList(entity.getReferenceList()));
2342
2343
2345 String content = _processTemplate(_tplServiceBaseImpl, context);
2346
2347
2349 File ejbFile = new File(
2350 _outputPath + "/service/base/" + entity.getName() +
2351 _getSessionTypeName(sessionType) + "ServiceBaseImpl.java");
2352
2353 writeFile(ejbFile, content, _author);
2354 }
2355
2356 private void _createServiceClp(Entity entity, int sessionType)
2357 throws Exception {
2358
2359 if (Validator.isNull(_pluginName)) {
2360 return;
2361 }
2362
2363 JavaClass javaClass = _getJavaClass(
2364 _serviceOutputPath + "/service/" + entity.getName() +
2365 _getSessionTypeName(sessionType) + "Service.java");
2366
2367 Map<String, Object> context = _getContext();
2368
2369 context.put("entity", entity);
2370 context.put("methods", _getMethods(javaClass));
2371 context.put("sessionTypeName", _getSessionTypeName(sessionType));
2372
2373
2375 String content = _processTemplate(_tplServiceClp, context);
2376
2377
2379 File ejbFile = new File(
2380 _serviceOutputPath + "/service/" + entity.getName() +
2381 _getSessionTypeName(sessionType) + "ServiceClp.java");
2382
2383 writeFile(ejbFile, content, _author);
2384 }
2385
2386 private void _createServiceClpSerializer() throws Exception {
2387 if (Validator.isNull(_pluginName)) {
2388 return;
2389 }
2390
2391 Map<String, Object> context = _getContext();
2392
2393 context.put("entities", _ejbList);
2394
2395
2397 String content = _processTemplate(_tplServiceClpSerializer, context);
2398
2399
2401 File ejbFile = new File(
2402 _serviceOutputPath + "/service/ClpSerializer.java");
2403
2404 writeFile(ejbFile, content);
2405 }
2406
2407 private void _createServiceFactory(Entity entity, int sessionType)
2408 throws Exception {
2409
2410 if (Validator.isNotNull(_pluginName)) {
2411 FileUtil.delete(
2412 _serviceOutputPath + "/service/" + entity.getName() +
2413 _getSessionTypeName(sessionType) + "ServiceFactory.java");
2414
2415 FileUtil.delete(
2416 _outputPath + "/service/" + entity.getName() +
2417 _getSessionTypeName(sessionType) + "ServiceFactory.java");
2418
2419 return;
2420 }
2421
2422 Map<String, Object> context = _getContext();
2423
2424 context.put("entity", entity);
2425 context.put("sessionTypeName", _getSessionTypeName(sessionType));
2426
2427
2429 String content = _processTemplate(_tplServiceFactory, context);
2430
2431
2433 File ejbFile = new File(
2434 _serviceOutputPath + "/service/" + entity.getName() +
2435 _getSessionTypeName(sessionType) + "ServiceFactory.java");
2436
2437 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2438
2439 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2440
2441 writeFile(ejbFile, content, _author, jalopySettings);
2442
2443 if (!_serviceOutputPath.equals(_outputPath)) {
2444 ejbFile = new File(
2445 _outputPath + "/service/" + entity.getName() +
2446 _getSessionTypeName(sessionType) + "ServiceFactory.java");
2447
2448 if (ejbFile.exists()) {
2449 System.out.println("Relocating " + ejbFile);
2450
2451 ejbFile.delete();
2452 }
2453 }
2454 }
2455
2456 private void _createServiceHttp(Entity entity) throws Exception {
2457 JavaClass javaClass = _getJavaClass(
2458 _outputPath + "/service/impl/" + entity.getName() +
2459 "ServiceImpl.java");
2460
2461 Map<String, Object> context = _getContext();
2462
2463 context.put("entity", entity);
2464 context.put("methods", _getMethods(javaClass));
2465 context.put("hasHttpMethods", new Boolean(_hasHttpMethods(javaClass)));
2466
2467
2469 String content = _processTemplate(_tplServiceHttp, context);
2470
2471
2473 File ejbFile = new File(
2474 _outputPath + "/service/http/" + entity.getName() +
2475 "ServiceHttp.java");
2476
2477 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2478
2479 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2480
2481 writeFile(ejbFile, content, _author, jalopySettings);
2482 }
2483
2484 private void _createServiceImpl(Entity entity, int sessionType)
2485 throws Exception {
2486
2487 Map<String, Object> context = _getContext();
2488
2489 context.put("entity", entity);
2490 context.put("sessionTypeName", _getSessionTypeName(sessionType));
2491
2492
2494 String content = _processTemplate(_tplServiceImpl, context);
2495
2496
2498 File ejbFile = new File(
2499 _outputPath + "/service/impl/" + entity.getName() +
2500 _getSessionTypeName(sessionType) + "ServiceImpl.java");
2501
2502 if (!ejbFile.exists()) {
2503 writeFile(ejbFile, content, _author);
2504 }
2505 }
2506
2507 private void _createServiceJson(Entity entity) throws Exception {
2508 JavaClass javaClass = _getJavaClass(
2509 _outputPath + "/service/impl/" + entity.getName() +
2510 "ServiceImpl.java");
2511
2512 Map<String, Object> context = _getContext();
2513
2514 context.put("entity", entity);
2515 context.put("methods", _getMethods(javaClass));
2516
2517
2519 String content = _processTemplate(_tplServiceJson, context);
2520
2521
2523 File ejbFile = new File(
2524 _outputPath + "/service/http/" + entity.getName() +
2525 "ServiceJSON.java");
2526
2527 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2528
2529 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2530
2531 writeFile(ejbFile, content, _author, jalopySettings);
2532 }
2533
2534 private void _createServiceJsonSerializer(Entity entity) throws Exception {
2535 Map<String, Object> context = _getContext();
2536
2537 context.put("entity", entity);
2538
2539
2541 String content = _processTemplate(_tplServiceJsonSerializer, context);
2542
2543
2545 File ejbFile = new File(
2546 _outputPath + "/service/http/" + entity.getName() +
2547 "JSONSerializer.java");
2548
2549 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2550
2551 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2552
2553 writeFile(ejbFile, content, _author, jalopySettings);
2554 }
2555
2556 private void _createServiceSoap(Entity entity) throws Exception {
2557 JavaClass javaClass = _getJavaClass(
2558 _outputPath + "/service/impl/" + entity.getName() +
2559 "ServiceImpl.java");
2560
2561 Map<String, Object> context = _getContext();
2562
2563 context.put("entity", entity);
2564 context.put("methods", _getMethods(javaClass));
2565
2566
2568 String content = _processTemplate(_tplServiceSoap, context);
2569
2570
2572 File ejbFile = new File(
2573 _outputPath + "/service/http/" + entity.getName() +
2574 "ServiceSoap.java");
2575
2576 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2577
2578 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2579
2580 writeFile(ejbFile, content, _author, jalopySettings);
2581 }
2582
2583 private void _createServiceUtil(Entity entity, int sessionType)
2584 throws Exception {
2585
2586 JavaClass javaClass = _getJavaClass(
2587 _serviceOutputPath + "/service/" + entity.getName() +
2588 _getSessionTypeName(sessionType) + "Service.java");
2589
2590 Map<String, Object> context = _getContext();
2591
2592 context.put("entity", entity);
2593 context.put("methods", _getMethods(javaClass));
2594 context.put("sessionTypeName", _getSessionTypeName(sessionType));
2595
2596
2598 String content = _processTemplate(_tplServiceUtil, context);
2599
2600
2602 File ejbFile = new File(
2603 _serviceOutputPath + "/service/" + entity.getName() +
2604 _getSessionTypeName(sessionType) + "ServiceUtil.java");
2605
2606 Map<String, Object> jalopySettings = new HashMap<String, Object>();
2607
2608 jalopySettings.put("keepJavadoc", Boolean.TRUE);
2609
2610 writeFile(ejbFile, content, _author, jalopySettings);
2611
2612 if (!_serviceOutputPath.equals(_outputPath)) {
2613 ejbFile = new File(
2614 _outputPath + "/service/" + entity.getName() +
2615 _getSessionTypeName(sessionType) + "ServiceUtil.java");
2616
2617 if (ejbFile.exists()) {
2618 System.out.println("Relocating " + ejbFile);
2619
2620 ejbFile.delete();
2621 }
2622 }
2623 }
2624
2625 private void _createSpringBaseXml() throws Exception {
2626 if (Validator.isNull(_springBaseFileName)) {
2627 return;
2628 }
2629
2630
2632 String content = _processTemplate(_tplSpringBaseXml);
2633
2634
2636 File ejbFile = new File(_springBaseFileName);
2637
2638 FileUtil.write(ejbFile, content, true);
2639
2640 if (Validator.isNotNull(_pluginName)) {
2641 FileUtil.delete(
2642 "docroot/WEB-INF/src/META-INF/data-source-spring.xml");
2643 FileUtil.delete("docroot/WEB-INF/src/META-INF/misc-spring.xml");
2644 }
2645 }
2646
2647 private void _createSpringDynamicDataSourceXml() throws Exception {
2648 if (Validator.isNull(_springDynamicDataSourceFileName)) {
2649 return;
2650 }
2651
2652
2654 String content = _processTemplate(_tplSpringDynamicDataSourceXml);
2655
2656
2658 File ejbFile = new File(_springDynamicDataSourceFileName);
2659
2660 FileUtil.write(ejbFile, content, true);
2661 }
2662
2663 private void _createSpringHibernateXml() throws Exception {
2664 if (Validator.isNull(_springHibernateFileName)) {
2665 return;
2666 }
2667
2668
2670 String content = _processTemplate(_tplSpringHibernateXml);
2671
2672
2674 File ejbFile = new File(_springHibernateFileName);
2675
2676 FileUtil.write(ejbFile, content, true);
2677 }
2678
2679 private void _createSpringInfrastructureXml() throws Exception {
2680 if (Validator.isNull(_springInfrastructureFileName)) {
2681 return;
2682 }
2683
2684
2686 String content = _processTemplate(_tplSpringInfrastructureXml);
2687
2688
2690 File ejbFile = new File(_springInfrastructureFileName);
2691
2692 FileUtil.write(ejbFile, content, true);
2693 }
2694
2695 private void _createSpringXml() throws Exception {
2696 Map<String, Object> context = _getContext();
2697
2698 context.put("entities", _ejbList);
2699
2700
2702 String content = _processTemplate(_tplSpringXml, context);
2703
2704 File xmlFile = new File(_springFileName);
2705
2706 if (!xmlFile.exists()) {
2707 String xml =
2708 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
2709 "\n" +
2710 "<beans\n" +
2711 "\tdefault-init-method=\"afterPropertiesSet\"\n" +
2712 "\txmlns=\"http://www.springframework.org/schema/beans\"\n" +
2713 "\txmlns:aop=\"http://www.springframework.org/schema/aop\"\n" +
2714 "\txmlns:tx=\"http://www.springframework.org/schema/tx\"\n" +
2715 "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
2716 "\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" +
2717 ">\n" +
2718 "</beans>";
2719
2720 FileUtil.write(xmlFile, xml);
2721 }
2722
2723 String oldContent = FileUtil.read(xmlFile);
2724 String newContent = _fixSpringXml(oldContent);
2725
2726 int x = oldContent.indexOf("<beans");
2727 int y = oldContent.lastIndexOf("</beans>");
2728
2729 int firstSession = newContent.indexOf(
2730 "<bean id=\"" + _packagePath + ".service.", x);
2731
2732 int lastSession = newContent.lastIndexOf(
2733 "<bean id=\"" + _packagePath + ".service.", y);
2734
2735 if ((firstSession == -1) || (firstSession > y)) {
2736 x = newContent.indexOf("</beans>");
2737
2738 newContent =
2739 newContent.substring(0, x) + content +
2740 newContent.substring(x, newContent.length());
2741 }
2742 else {
2743 firstSession = newContent.lastIndexOf("<bean", firstSession) - 1;
2744 lastSession = newContent.indexOf("</bean>", lastSession) + 8;
2745
2746 newContent =
2747 newContent.substring(0, firstSession) + content +
2748 newContent.substring(lastSession, newContent.length());
2749 }
2750
2751 newContent = _formatXml(newContent);
2752
2753 if (!oldContent.equals(newContent)) {
2754 FileUtil.write(xmlFile, newContent);
2755 }
2756 }
2757
2758 private void _createSQLIndexes() throws IOException {
2759 if (!FileUtil.exists(_sqlDir)) {
2760 return;
2761 }
2762
2763
2765 File sqlFile = new File(_sqlDir + "/" + _sqlIndexesFileName);
2766
2767 if (!sqlFile.exists()) {
2768 FileUtil.write(sqlFile, "");
2769 }
2770
2771 Map<String, String> indexSQLs = new TreeMap<String, String>();
2772
2773 BufferedReader br = new BufferedReader(new FileReader(sqlFile));
2774
2775 while (true) {
2776 String indexSQL = br.readLine();
2777
2778 if (indexSQL == null) {
2779 break;
2780 }
2781
2782 if (Validator.isNotNull(indexSQL.trim())) {
2783 int pos = indexSQL.indexOf(" on ");
2784
2785 String indexSpec = indexSQL.substring(pos + 4);
2786
2787 indexSQLs.put(indexSpec, indexSQL);
2788 }
2789 }
2790
2791 br.close();
2792
2793
2795 File propsFile = new File(
2796 _sqlDir + "/" + _sqlIndexesPropertiesFileName);
2797
2798 if (!propsFile.exists()) {
2799 FileUtil.write(propsFile, "");
2800 }
2801
2802 Map<String, String> indexProps = new TreeMap<String, String>();
2803
2804 br = new BufferedReader(new FileReader(propsFile));
2805
2806 while (true) {
2807 String indexMapping = br.readLine();
2808
2809 if (indexMapping == null) {
2810 break;
2811 }
2812
2813 if (Validator.isNotNull(indexMapping.trim())) {
2814 String[] splitIndexMapping = indexMapping.split("\\=");
2815
2816 indexProps.put(splitIndexMapping[1], splitIndexMapping[0]);
2817 }
2818 }
2819
2820 br.close();
2821
2822
2824 for (int i = 0; i < _ejbList.size(); i++) {
2825 Entity entity = _ejbList.get(i);
2826
2827 if (!entity.isDefaultDataSource()) {
2828 continue;
2829 }
2830
2831 List<EntityFinder> finderList = entity.getFinderList();
2832
2833 for (int j = 0; j < finderList.size(); j++) {
2834 EntityFinder finder = finderList.get(j);
2835
2836 if (finder.isDBIndex()) {
2837 StringBuilder sb = new StringBuilder();
2838
2839 sb.append(entity.getTable() + " (");
2840
2841 List<EntityColumn> finderColsList = finder.getColumns();
2842
2843 for (int k = 0; k < finderColsList.size(); k++) {
2844 EntityColumn col = finderColsList.get(k);
2845
2846 sb.append(col.getDBName());
2847
2848 if ((k + 1) != finderColsList.size()) {
2849 sb.append(", ");
2850 }
2851 }
2852
2853 sb.append(");");
2854
2855 String indexSpec = sb.toString();
2856
2857 String indexHash =
2858 Integer.toHexString(indexSpec.hashCode()).toUpperCase();
2859
2860 String indexName = "IX_" + indexHash;
2861
2862 sb = new StringBuilder();
2863
2864 sb.append("create index " + indexName + " on ");
2865 sb.append(indexSpec);
2866
2867 indexSQLs.put(indexSpec, sb.toString());
2868
2869 String finderName =
2870 entity.getTable() + StringPool.PERIOD +
2871 finder.getName();
2872
2873 indexProps.put(finderName, indexName);
2874 }
2875 }
2876 }
2877
2878 StringBuilder sb = new StringBuilder();
2879
2880 Iterator<String> itr = indexSQLs.values().iterator();
2881
2882 String prevEntityName = null;
2883
2884 while (itr.hasNext()) {
2885 String indexSQL = itr.next();
2886
2887 String entityName = indexSQL.split(" ")[4];
2888
2889 if ((prevEntityName != null) &&
2890 (!prevEntityName.equals(entityName))) {
2891
2892 sb.append("\n");
2893 }
2894
2895 sb.append(indexSQL);
2896
2897 if (itr.hasNext()) {
2898 sb.append("\n");
2899 }
2900
2901 prevEntityName = entityName;
2902 }
2903
2904 FileUtil.write(sqlFile, sb.toString(), true);
2905
2906
2908 sb = new StringBuilder();
2909
2910 itr = indexProps.keySet().iterator();
2911
2912 prevEntityName = null;
2913
2914 while (itr.hasNext()) {
2915 String finderName = itr.next();
2916
2917 String indexName = indexProps.get(finderName);
2918
2919 String entityName = finderName.split("\\.")[0];
2920
2921 if ((prevEntityName != null) &&
2922 (!prevEntityName.equals(entityName))) {
2923
2924 sb.append("\n");
2925 }
2926
2927 sb.append(indexName + StringPool.EQUAL + finderName);
2928
2929 if (itr.hasNext()) {
2930 sb.append("\n");
2931 }
2932
2933 prevEntityName = entityName;
2934 }
2935
2936 FileUtil.write(propsFile, sb.toString(), true);
2937 }
2938
2939 private void _createSQLMappingTables(
2940 File sqlFile, String newCreateTableString,
2941 EntityMapping entityMapping, boolean addMissingTables)
2942 throws IOException {
2943
2944 if (!sqlFile.exists()) {
2945 FileUtil.write(sqlFile, StringPool.BLANK);
2946 }
2947
2948 String content = FileUtil.read(sqlFile);
2949
2950 int x = content.indexOf(
2951 _SQL_CREATE_TABLE + entityMapping.getTable() + " (");
2952 int y = content.indexOf(");", x);
2953
2954 if (x != -1) {
2955 String oldCreateTableString = content.substring(x + 1, y);
2956
2957 if (!oldCreateTableString.equals(newCreateTableString)) {
2958 content =
2959 content.substring(0, x) + newCreateTableString +
2960 content.substring(y + 2, content.length());
2961
2962 FileUtil.write(sqlFile, content);
2963 }
2964 }
2965 else if (addMissingTables) {
2966 StringBuilder sb = new StringBuilder();
2967
2968 BufferedReader br = new BufferedReader(new StringReader(content));
2969
2970 String line = null;
2971 boolean appendNewTable = true;
2972
2973 while ((line = br.readLine()) != null) {
2974 if (appendNewTable && line.startsWith(_SQL_CREATE_TABLE)) {
2975 x = _SQL_CREATE_TABLE.length();
2976 y = line.indexOf(" ", x);
2977
2978 String tableName = line.substring(x, y);
2979
2980 if (tableName.compareTo(entityMapping.getTable()) > 0) {
2981 sb.append(newCreateTableString + "\n\n");
2982
2983 appendNewTable = false;
2984 }
2985 }
2986
2987 sb.append(line);
2988 sb.append("\n");
2989 }
2990
2991 if (appendNewTable) {
2992 sb.append("\n" + newCreateTableString);
2993 }
2994
2995 br.close();
2996
2997 FileUtil.write(sqlFile, sb.toString(), true);
2998 }
2999 }
3000
3001 private void _createSQLSequences() throws IOException {
3002 if (!FileUtil.exists(_sqlDir)) {
3003 return;
3004 }
3005
3006 File sqlFile = new File(_sqlDir + "/" + _sqlSequencesFileName);
3007
3008 if (!sqlFile.exists()) {
3009 FileUtil.write(sqlFile, "");
3010 }
3011
3012 Set<String> sequenceSQLs = new TreeSet<String>();
3013
3014 BufferedReader br = new BufferedReader(new FileReader(sqlFile));
3015
3016 while (true) {
3017 String sequenceSQL = br.readLine();
3018
3019 if (sequenceSQL == null) {
3020 break;
3021 }
3022
3023 if (Validator.isNotNull(sequenceSQL)) {
3024 sequenceSQLs.add(sequenceSQL);
3025 }
3026 }
3027
3028 br.close();
3029
3030 for (int i = 0; i < _ejbList.size(); i++) {
3031 Entity entity = _ejbList.get(i);
3032
3033 if (!entity.isDefaultDataSource()) {
3034 continue;
3035 }
3036
3037 List<EntityColumn> columnList = entity.getColumnList();
3038
3039 for (int j = 0; j < columnList.size(); j++) {
3040 EntityColumn column = columnList.get(j);
3041
3042 if ("sequence".equals(column.getIdType())) {
3043 StringBuilder sb = new StringBuilder();
3044
3045 String sequenceName = column.getIdParam();
3046
3047 if (sequenceName.length() > 30) {
3048 sequenceName = sequenceName.substring(0, 30);
3049 }
3050
3051 sb.append("create sequence " + sequenceName + ";");
3052
3053 String sequenceSQL = sb.toString();
3054
3055 if (!sequenceSQLs.contains(sequenceSQL)) {
3056 sequenceSQLs.add(sequenceSQL);
3057 }
3058 }
3059 }
3060 }
3061
3062 StringBuilder sb = new StringBuilder();
3063
3064 Iterator<String> itr = sequenceSQLs.iterator();
3065
3066 while (itr.hasNext()) {
3067 String sequenceSQL = itr.next();
3068
3069 sb.append(sequenceSQL);
3070
3071 if (itr.hasNext()) {
3072 sb.append("\n");
3073 }
3074 }
3075
3076 FileUtil.write(sqlFile, sb.toString(), true);
3077 }
3078
3079 private void _createSQLTables() throws IOException {
3080 if (!FileUtil.exists(_sqlDir)) {
3081 return;
3082 }
3083
3084 File sqlFile = new File(_sqlDir + "/" + _sqlFileName);
3085
3086 if (!sqlFile.exists()) {
3087 FileUtil.write(sqlFile, StringPool.BLANK);
3088 }
3089
3090 for (int i = 0; i < _ejbList.size(); i++) {
3091 Entity entity = _ejbList.get(i);
3092
3093 if (!entity.isDefaultDataSource()) {
3094 continue;
3095 }
3096
3097 String createTableSQL = _getCreateTableSQL(entity);
3098
3099 if (Validator.isNotNull(createTableSQL)) {
3100 _createSQLTables(sqlFile, createTableSQL, entity, true);
3101
3102 File updateSQLFile = new File(
3103 _sqlDir + "/update-5.1.2-5.2.0.sql");
3104
3105 if (updateSQLFile.exists()) {
3106 _createSQLTables(
3107 updateSQLFile, createTableSQL, entity, false);
3108 }
3109 }
3110 }
3111
3112 for (Map.Entry<String, EntityMapping> entry :
3113 _entityMappings.entrySet()) {
3114
3115 EntityMapping entityMapping = entry.getValue();
3116
3117 String createMappingTableSQL = _getCreateMappingTableSQL(
3118 entityMapping);
3119
3120 if (Validator.isNotNull(createMappingTableSQL)) {
3121 _createSQLMappingTables(
3122 sqlFile, createMappingTableSQL, entityMapping, true);
3123 }
3124 }
3125 }
3126
3127 private void _createSQLTables(
3128 File sqlFile, String newCreateTableString, Entity entity,
3129 boolean addMissingTables)
3130 throws IOException {
3131
3132 if (!sqlFile.exists()) {
3133 FileUtil.write(sqlFile, StringPool.BLANK);
3134 }
3135
3136 String content = FileUtil.read(sqlFile);
3137
3138 int x = content.indexOf(_SQL_CREATE_TABLE + entity.getTable() + " (");
3139 int y = content.indexOf(");", x);
3140
3141 if (x != -1) {
3142 String oldCreateTableString = content.substring(x + 1, y);
3143
3144 if (!oldCreateTableString.equals(newCreateTableString)) {
3145 content =
3146 content.substring(0, x) + newCreateTableString +
3147 content.substring(y + 2, content.length());
3148
3149 FileUtil.write(sqlFile, content);
3150 }
3151 }
3152 else if (addMissingTables) {
3153 StringBuilder sb = new StringBuilder();
3154
3155 BufferedReader br = new BufferedReader(new StringReader(content));
3156
3157 String line = null;
3158 boolean appendNewTable = true;
3159
3160 while ((line = br.readLine()) != null) {
3161 if (appendNewTable && line.startsWith(_SQL_CREATE_TABLE)) {
3162 x = _SQL_CREATE_TABLE.length();
3163 y = line.indexOf(" ", x);
3164
3165 String tableName = line.substring(x, y);
3166
3167 if (tableName.compareTo(entity.getTable()) > 0) {
3168 sb.append(newCreateTableString + "\n\n");
3169
3170 appendNewTable = false;
3171 }
3172 }
3173
3174 sb.append(line);
3175 sb.append("\n");
3176 }
3177
3178 if (appendNewTable) {
3179 sb.append("\n" + newCreateTableString);
3180 }
3181
3182 br.close();
3183
3184 FileUtil.write(sqlFile, sb.toString(), true);
3185 }
3186 }
3187
3188 private String _fixHbmXml(String content) throws IOException {
3189 StringBuilder sb = new StringBuilder();
3190
3191 BufferedReader br = new BufferedReader(new StringReader(content));
3192
3193 String line = null;
3194
3195 while ((line = br.readLine()) != null) {
3196 if (line.startsWith("\t<class name=\"")) {
3197 line = StringUtil.replace(
3198 line,
3199 new String[] {
3200 ".service.persistence.", "HBM\" table=\""
3201 },
3202 new String[] {
3203 ".model.", "\" table=\""
3204 });
3205
3206 if (line.indexOf(".model.impl.") == -1) {
3207 line = StringUtil.replace(
3208 line,
3209 new String[] {
3210 ".model.", "\" table=\""
3211 },
3212 new String[] {
3213 ".model.impl.", "Impl\" table=\""
3214 });
3215 }
3216 }
3217
3218 sb.append(line);
3219 sb.append('\n');
3220 }
3221
3222 br.close();
3223
3224 return sb.toString().trim();
3225 }
3226
3227 private String _fixSpringXml(String content) {
3228 return StringUtil.replace(content, ".service.spring.", ".service.");
3229 }
3230
3231 private String _formatXml(String xml)
3232 throws DocumentException, IOException {
3233
3234 String doctype = null;
3235
3236 int x = xml.indexOf("<!DOCTYPE");
3237
3238 if (x != -1) {
3239 int y = xml.indexOf(">", x) + 1;
3240
3241 doctype = xml.substring(x, y);
3242
3243 xml = xml.substring(0, x) + "\n" + xml.substring(y);
3244 }
3245
3246 xml = StringUtil.replace(xml, '\r', "");
3247 xml = XMLFormatter.toString(xml);
3248 xml = StringUtil.replace(xml, "\"/>", "\" />");
3249
3250 if (Validator.isNotNull(doctype)) {
3251 x = xml.indexOf("?>") + 2;
3252
3253 xml = xml.substring(0, x) + "\n" + doctype + xml.substring(x);
3254 }
3255
3256 return xml;
3257 }
3258
3259 private Map<String, Object> _getContext() throws TemplateModelException {
3260 BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
3261
3262 TemplateHashModel staticModels = wrapper.getStaticModels();
3263
3264 Map<String, Object> context = new HashMap<String, Object>();
3265
3266 context.put("hbmFileName", _hbmFileName);
3267 context.put("modelHintsFileName", _modelHintsFileName);
3268 context.put("springFileName", _springFileName);
3269 context.put("springBaseFileName", _springBaseFileName);
3270 context.put("springHibernateFileName", _springHibernateFileName);
3271 context.put("springInfrastructureFileName", _springInfrastructureFileName);
3272 context.put("apiDir", _apiDir);
3273 context.put("implDir", _implDir);
3274 context.put("jsonFileName", _jsonFileName);
3275 context.put("sqlDir", _sqlDir);
3276 context.put("sqlFileName", _sqlFileName);
3277 context.put("beanLocatorUtil", _beanLocatorUtil);
3278 context.put("beanLocatorUtilShortName", _beanLocatorUtilShortName);
3279 context.put("propsUtil", _propsUtil);
3280 context.put("portletName", _portletName);
3281 context.put("portletShortName", _portletShortName);
3282 context.put("portletPackageName", _portletPackageName);
3283 context.put("outputPath", _outputPath);
3284 context.put("serviceOutputPath", _serviceOutputPath);
3285 context.put("packagePath", _packagePath);
3286 context.put("pluginName", _pluginName);
3287 context.put("serviceBuilder", this);
3288
3289 context.put("arrayUtil", ArrayUtil_IW.getInstance());
3290 context.put(
3291 "modelHintsUtil",
3292 staticModels.get("com.liferay.portal.model.ModelHintsUtil"));
3293 context.put("stringUtil", StringUtil_IW.getInstance());
3294 context.put("system", staticModels.get("java.lang.System"));
3295 context.put("tempMap", wrapper.wrap(new HashMap<String, Object>()));
3296 context.put(
3297 "validator",
3298 staticModels.get("com.liferay.portal.kernel.util.Validator"));
3299
3300 return context;
3301 }
3302
3303 private String _getCreateMappingTableSQL(EntityMapping entityMapping)
3304 throws IOException {
3305
3306 Entity[] entities = new Entity[2];
3307
3308 for (int i = 0; i < entities.length; i++) {
3309 entities[i] = getEntity(entityMapping.getEntity(i));
3310
3311 if (entities[i] == null) {
3312 return null;
3313 }
3314 }
3315
3316 StringBuilder sb = new StringBuilder();
3317
3318 sb.append(_SQL_CREATE_TABLE + entityMapping.getTable() + " (\n");
3319
3320 for (Entity entity : entities) {
3321 List<EntityColumn> pkList = entity.getPKList();
3322
3323 for (int i = 0; i < pkList.size(); i++) {
3324 EntityColumn col = pkList.get(i);
3325
3326 String colName = col.getName();
3327 String colType = col.getType();
3328
3329 sb.append("\t" + col.getDBName());
3330 sb.append(" ");
3331
3332 if (colType.equalsIgnoreCase("boolean")) {
3333 sb.append("BOOLEAN");
3334 }
3335 else if (colType.equalsIgnoreCase("double") ||
3336 colType.equalsIgnoreCase("float")) {
3337
3338 sb.append("DOUBLE");
3339 }
3340 else if (colType.equals("int") ||
3341 colType.equals("Integer") ||
3342 colType.equalsIgnoreCase("short")) {
3343
3344 sb.append("INTEGER");
3345 }
3346 else if (colType.equalsIgnoreCase("long")) {
3347 sb.append("LONG");
3348 }
3349 else if (colType.equals("String")) {
3350 Map<String, String> hints = ModelHintsUtil.getHints(
3351 _packagePath + ".model." + entity.getName(), colName);
3352
3353 int maxLength = 75;
3354
3355 if (hints != null) {
3356 maxLength = GetterUtil.getInteger(
3357 hints.get("max-length"), maxLength);
3358 }
3359
3360 if (maxLength < 4000) {
3361 sb.append("VARCHAR(" + maxLength + ")");
3362 }
3363 else if (maxLength == 4000) {
3364 sb.append("STRING");
3365 }
3366 else if (maxLength > 4000) {
3367 sb.append("TEXT");
3368 }
3369 }
3370 else if (colType.equals("Date")) {
3371 sb.append("DATE null");
3372 }
3373 else {
3374 sb.append("invalid");
3375 }
3376
3377 if (col.isPrimary()) {
3378 sb.append(" not null");
3379 }
3380
3381 sb.append(",\n");
3382 }
3383 }
3384
3385 sb.append("\tprimary key (");
3386
3387 for (int i = 0; i < entities.length; i++) {
3388 Entity entity = entities[i];
3389
3390 List<EntityColumn> pkList = entity.getPKList();
3391
3392 for (int j = 0; j < pkList.size(); j++) {
3393 EntityColumn col = pkList.get(j);
3394
3395 String colName = col.getName();
3396
3397 if ((i != 0) || (j != 0)) {
3398 sb.append(", ");
3399 }
3400
3401 sb.append(colName);
3402 }
3403 }
3404
3405 sb.append(")\n");
3406 sb.append(");");
3407
3408 return sb.toString();
3409 }
3410
3411 private String _getCreateTableSQL(Entity entity) {
3412 List<EntityColumn> pkList = entity.getPKList();
3413 List<EntityColumn> regularColList = entity.getRegularColList();
3414
3415 if (regularColList.size() == 0) {
3416 return null;
3417 }
3418
3419 StringBuilder sb = new StringBuilder();
3420
3421 sb.append(_SQL_CREATE_TABLE + entity.getTable() + " (\n");
3422
3423 for (int i = 0; i < regularColList.size(); i++) {
3424 EntityColumn col = regularColList.get(i);
3425
3426 String colName = col.getName();
3427 String colType = col.getType();
3428 String colIdType = col.getIdType();
3429
3430 sb.append("\t" + col.getDBName());
3431 sb.append(" ");
3432
3433 if (colType.equalsIgnoreCase("boolean")) {
3434 sb.append("BOOLEAN");
3435 }
3436 else if (colType.equalsIgnoreCase("double") ||
3437 colType.equalsIgnoreCase("float")) {
3438
3439 sb.append("DOUBLE");
3440 }
3441 else if (colType.equals("int") ||
3442 colType.equals("Integer") ||
3443 colType.equalsIgnoreCase("short")) {
3444
3445 sb.append("INTEGER");
3446 }
3447 else if (colType.equalsIgnoreCase("long")) {
3448 sb.append("LONG");
3449 }
3450 else if (colType.equals("String")) {
3451 Map<String, String> hints = ModelHintsUtil.getHints(
3452 _packagePath + ".model." + entity.getName(), colName);
3453
3454 int maxLength = 75;
3455
3456 if (hints != null) {
3457 maxLength = GetterUtil.getInteger(
3458 hints.get("max-length"), maxLength);
3459 }
3460
3461 if (maxLength < 4000) {
3462 sb.append("VARCHAR(" + maxLength + ")");
3463 }
3464 else if (maxLength == 4000) {
3465 sb.append("STRING");
3466 }
3467 else if (maxLength > 4000) {
3468 sb.append("TEXT");
3469 }
3470 }
3471 else if (colType.equals("Date")) {
3472 sb.append("DATE null");
3473 }
3474 else {
3475 sb.append("invalid");
3476 }
3477
3478 if (col.isPrimary()) {
3479 sb.append(" not null");
3480
3481 if (!entity.hasCompoundPK()) {
3482 sb.append(" primary key");
3483 }
3484 }
3485 else if (colType.equals("String")) {
3486 sb.append(" null");
3487 }
3488
3489 if (Validator.isNotNull(colIdType) &&
3490 colIdType.equals("identity")) {
3491
3492 sb.append(" IDENTITY");
3493 }
3494
3495 if (((i + 1) != regularColList.size()) ||
3496 (entity.hasCompoundPK())) {
3497
3498 sb.append(",");
3499 }
3500
3501 sb.append("\n");
3502 }
3503
3504 if (entity.hasCompoundPK()) {
3505 sb.append("\tprimary key (");
3506
3507 for (int j = 0; j < pkList.size(); j++) {
3508 EntityColumn pk = pkList.get(j);
3509
3510 sb.append(pk.getDBName());
3511
3512 if ((j + 1) != pkList.size()) {
3513 sb.append(", ");
3514 }
3515 }
3516
3517 sb.append(")\n");
3518 }
3519
3520 sb.append(");");
3521
3522 return sb.toString();
3523 }
3524
3525 private String _getDimensions(Type type) {
3526 String dimensions = "";
3527
3528 for (int i = 0; i < type.getDimensions(); i++) {
3529 dimensions += "[]";
3530 }
3531
3532 return dimensions;
3533 }
3534
3535 private JavaClass _getJavaClass(String fileName) throws IOException {
3536 int pos = fileName.indexOf(_implDir + "/");
3537
3538 if (pos != -1) {
3539 pos += _implDir.length();
3540 }
3541 else {
3542 pos = fileName.indexOf(_apiDir + "/") + _apiDir.length();
3543 }
3544
3545 String srcFile = fileName.substring(pos + 1, fileName.length());
3546 String className = StringUtil.replace(
3547 srcFile.substring(0, srcFile.length() - 5), "/", ".");
3548
3549 JavaDocBuilder builder = new JavaDocBuilder();
3550
3551 File file = new File(fileName);
3552
3553 if (!file.exists()) {
3554 return null;
3555 }
3556
3557 builder.addSource(file);
3558
3559 return builder.getClassByName(className);
3560 }
3561
3562 private JavaMethod[] _getMethods(JavaClass javaClass) {
3563 JavaMethod[] methods = javaClass.getMethods();
3564
3565 for (JavaMethod method : methods) {
3566 Arrays.sort(method.getExceptions());
3567 }
3568
3569 return methods;
3570 }
3571
3572 private String _getSessionTypeName(int sessionType) {
3573 if (sessionType == _SESSION_TYPE_LOCAL) {
3574 return "Local";
3575 }
3576 else {
3577 return "";
3578 }
3579 }
3580
3581 private String _getTplProperty(String key, String defaultValue) {
3582 return System.getProperty("service.tpl." + key, defaultValue);
3583 }
3584
3585 private boolean _hasHttpMethods(JavaClass javaClass) {
3586 JavaMethod[] methods = _getMethods(javaClass);
3587
3588 for (int i = 0; i < methods.length; i++) {
3589 JavaMethod javaMethod = methods[i];
3590
3591 if (!javaMethod.isConstructor() && javaMethod.isPublic() &&
3592 isCustomMethod(javaMethod)) {
3593
3594 return true;
3595 }
3596 }
3597
3598 return false;
3599 }
3600
3601 private List<Entity> _mergeReferenceList(List<Entity> referenceList) {
3602 List<Entity> list = new ArrayList<Entity>(
3603 _ejbList.size() + referenceList.size());
3604
3605 list.addAll(_ejbList);
3606 list.addAll(referenceList);
3607
3608 return list;
3609 }
3610
3611 private String _processTemplate(String name) throws Exception {
3612 return _processTemplate(name, _getContext());
3613 }
3614
3615 private String _processTemplate(String name, Map<String, Object> context)
3616 throws Exception {
3617
3618 return FreeMarkerUtil.process(name, context);
3619 }
3620
3621 private static final String _AUTHOR = "Brian Wing Shun Chan";
3622
3623 private static final int _SESSION_TYPE_REMOTE = 0;
3624
3625 private static final int _SESSION_TYPE_LOCAL = 1;
3626
3627 private static final String _SQL_CREATE_TABLE = "create table ";
3628
3629 private static final String _TPL_ROOT =
3630 "com/liferay/portal/tools/servicebuilder/dependencies/";
3631
3632 private String _tplBadColumnNames = _TPL_ROOT + "bad_column_names.txt";
3633 private String _tplBadTableNames = _TPL_ROOT + "bad_table_names.txt";
3634 private String _tplEjbPk = _TPL_ROOT + "ejb_pk.ftl";
3635 private String _tplException = _TPL_ROOT + "exception.ftl";
3636 private String _tplExtendedModel = _TPL_ROOT + "extended_model.ftl";
3637 private String _tplExtendedModelImpl =
3638 _TPL_ROOT + "extended_model_impl.ftl";
3639 private String _tplFinder = _TPL_ROOT + "finder.ftl";
3640 private String _tplFinderUtil = _TPL_ROOT + "finder_util.ftl";
3641 private String _tplHbmXml = _TPL_ROOT + "hbm_xml.ftl";
3642 private String _tplJsonJs = _TPL_ROOT + "json_js.ftl";
3643 private String _tplJsonJsMethod = _TPL_ROOT + "json_js_method.ftl";
3644 private String _tplModel = _TPL_ROOT + "model.ftl";
3645 private String _tplModelClp = _TPL_ROOT + "model_clp.ftl";
3646 private String _tplModelHintsXml = _TPL_ROOT + "model_hints_xml.ftl";
3647 private String _tplModelImpl = _TPL_ROOT + "model_impl.ftl";
3648 private String _tplModelSoap = _TPL_ROOT + "model_soap.ftl";
3649 private String _tplPersistence = _TPL_ROOT + "persistence.ftl";
3650 private String _tplPersistenceImpl = _TPL_ROOT + "persistence_impl.ftl";
3651 private String _tplPersistenceTest = _TPL_ROOT + "persistence_test.ftl";
3652 private String _tplPersistenceUtil = _TPL_ROOT + "persistence_util.ftl";
3653 private String _tplProps = _TPL_ROOT + "props.ftl";
3654 private String _tplRemotingXml = _TPL_ROOT + "remoting_xml.ftl";
3655 private String _tplService = _TPL_ROOT + "service.ftl";
3656 private String _tplServiceBaseImpl = _TPL_ROOT + "service_base_impl.ftl";
3657 private String _tplServiceClp = _TPL_ROOT + "service_clp.ftl";
3658 private String _tplServiceClpSerializer =
3659 _TPL_ROOT + "service_clp_serializer.ftl";
3660 private String _tplServiceFactory = _TPL_ROOT + "service_factory.ftl";
3661 private String _tplServiceHttp = _TPL_ROOT + "service_http.ftl";
3662 private String _tplServiceImpl = _TPL_ROOT + "service_impl.ftl";
3663 private String _tplServiceJson = _TPL_ROOT + "service_json.ftl";
3664 private String _tplServiceJsonSerializer =
3665 _TPL_ROOT + "service_json_serializer.ftl";
3666 private String _tplServiceSoap = _TPL_ROOT + "service_soap.ftl";
3667 private String _tplServiceUtil = _TPL_ROOT + "service_util.ftl";
3668 private String _tplSpringBaseXml = _TPL_ROOT + "spring_base_xml.ftl";
3669 private String _tplSpringDynamicDataSourceXml =
3670 _TPL_ROOT + "spring_dynamic_data_source_xml.ftl";
3671 private String _tplSpringHibernateXml =
3672 _TPL_ROOT + "spring_hibernate_xml.ftl";
3673 private String _tplSpringInfrastructureXml =
3674 _TPL_ROOT + "spring_infrastructure_xml.ftl";
3675 private String _tplSpringXml = _TPL_ROOT + "spring_xml.ftl";
3676 private Set<String> _badTableNames;
3677 private Set<String> _badColumnNames;
3678 private String _hbmFileName;
3679 private String _modelHintsFileName;
3680 private String _springFileName;
3681 private String _springBaseFileName;
3682 private String _springDynamicDataSourceFileName;
3683 private String _springHibernateFileName;
3684 private String _springInfrastructureFileName;
3685 private String _apiDir;
3686 private String _implDir;
3687 private String _jsonFileName;
3688 private String _remotingFileName;
3689 private String _sqlDir;
3690 private String _sqlFileName;
3691 private String _sqlIndexesFileName;
3692 private String _sqlIndexesPropertiesFileName;
3693 private String _sqlSequencesFileName;
3694 private boolean _autoNamespaceTables;
3695 private String _beanLocatorUtil;
3696 private String _beanLocatorUtilShortName;
3697 private String _propsUtil;
3698 private String _pluginName;
3699 private String _testDir;
3700 private String _author;
3701 private String _portletName = StringPool.BLANK;
3702 private String _portletShortName = StringPool.BLANK;
3703 private String _portletPackageName = StringPool.BLANK;
3704 private String _outputPath;
3705 private String _serviceOutputPath;
3706 private String _testOutputPath;
3707 private String _packagePath;
3708 private List<Entity> _ejbList;
3709 private Map<String, EntityMapping> _entityMappings;
3710 private Map<String, Entity> _entityPool = new HashMap<String, Entity>();
3711
3712}