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