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