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