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