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