2 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
\r
4 * This library is free software; you can redistribute it and/or modify it under
\r
5 * the terms of the GNU Lesser General Public License as published by the Free
\r
6 * Software Foundation; either version 2.1 of the License, or (at your option)
\r
9 * This library is distributed in the hope that it will be useful, but WITHOUT
\r
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
\r
15 package com.liferay.portal.tools.servicebuilder;
\r
17 import com.liferay.portal.freemarker.FreeMarkerUtil;
\r
18 import com.liferay.portal.kernel.exception.PortalException;
\r
19 import com.liferay.portal.kernel.exception.SystemException;
\r
20 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
\r
21 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
\r
22 import com.liferay.portal.kernel.util.ArrayUtil;
\r
23 import com.liferay.portal.kernel.util.ArrayUtil_IW;
\r
24 import com.liferay.portal.kernel.util.CharPool;
\r
25 import com.liferay.portal.kernel.util.ClearThreadLocalUtil;
\r
26 import com.liferay.portal.kernel.util.FileUtil;
\r
27 import com.liferay.portal.kernel.util.GetterUtil;
\r
28 import com.liferay.portal.kernel.util.ListUtil;
\r
29 import com.liferay.portal.kernel.util.PropertiesUtil;
\r
30 import com.liferay.portal.kernel.util.StringBundler;
\r
31 import com.liferay.portal.kernel.util.StringPool;
\r
32 import com.liferay.portal.kernel.util.StringUtil;
\r
33 import com.liferay.portal.kernel.util.StringUtil_IW;
\r
34 import com.liferay.portal.kernel.util.TextFormatter;
\r
35 import com.liferay.portal.kernel.util.Time;
\r
36 import com.liferay.portal.kernel.util.Validator;
\r
37 import com.liferay.portal.kernel.util.Validator_IW;
\r
38 import com.liferay.portal.kernel.xml.Document;
\r
39 import com.liferay.portal.kernel.xml.Element;
\r
40 import com.liferay.portal.kernel.xml.SAXReaderUtil;
\r
41 import com.liferay.portal.model.CacheField;
\r
42 import com.liferay.portal.model.ModelHintsUtil;
\r
43 import com.liferay.portal.security.permission.ResourceActionsUtil;
\r
44 import com.liferay.portal.tools.ArgumentsUtil;
\r
45 import com.liferay.portal.tools.SourceFormatter;
\r
46 import com.liferay.portal.util.InitUtil;
\r
47 import com.liferay.portal.util.PropsValues;
\r
48 import com.liferay.util.xml.XMLFormatter;
\r
50 import com.thoughtworks.qdox.JavaDocBuilder;
\r
51 import com.thoughtworks.qdox.model.Annotation;
\r
52 import com.thoughtworks.qdox.model.ClassLibrary;
\r
53 import com.thoughtworks.qdox.model.DocletTag;
\r
54 import com.thoughtworks.qdox.model.JavaClass;
\r
55 import com.thoughtworks.qdox.model.JavaField;
\r
56 import com.thoughtworks.qdox.model.JavaMethod;
\r
57 import com.thoughtworks.qdox.model.JavaParameter;
\r
58 import com.thoughtworks.qdox.model.Type;
\r
60 import de.hunsicker.io.FileFormat;
\r
61 import de.hunsicker.jalopy.Jalopy;
\r
62 import de.hunsicker.jalopy.storage.Convention;
\r
63 import de.hunsicker.jalopy.storage.ConventionKeys;
\r
64 import de.hunsicker.jalopy.storage.Environment;
\r
66 import freemarker.ext.beans.BeansWrapper;
\r
68 import freemarker.log.Logger;
\r
70 import freemarker.template.TemplateHashModel;
\r
71 import freemarker.template.TemplateModelException;
\r
73 import java.beans.Introspector;
\r
75 import java.io.File;
\r
76 import java.io.FileInputStream;
\r
77 import java.io.FileNotFoundException;
\r
78 import java.io.FileReader;
\r
79 import java.io.IOException;
\r
80 import java.io.InputStream;
\r
82 import java.util.ArrayList;
\r
83 import java.util.Arrays;
\r
84 import java.util.Collections;
\r
85 import java.util.HashMap;
\r
86 import java.util.HashSet;
\r
87 import java.util.Iterator;
\r
88 import java.util.LinkedHashSet;
\r
89 import java.util.List;
\r
90 import java.util.Locale;
\r
91 import java.util.Map;
\r
92 import java.util.Properties;
\r
93 import java.util.Set;
\r
94 import java.util.TreeMap;
\r
95 import java.util.TreeSet;
\r
96 import java.util.regex.Matcher;
\r
97 import java.util.regex.Pattern;
\r
99 import org.dom4j.DocumentException;
\r
102 * @author Brian Wing Shun Chan
\r
103 * @author Charles May
\r
104 * @author Alexander Chow
\r
105 * @author Harry Mark
\r
106 * @author Tariq Dweik
\r
107 * @author Glenn Powell
\r
108 * @author Raymond Augé
\r
109 * @author Prashant Dighe
\r
110 * @author Shuyang Zhou
\r
112 public class ServiceBuilder {
\r
114 public static final String AUTHOR = "Brian Wing Shun Chan";
\r
116 public static void main(String[] args) {
\r
117 Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
\r
119 InitUtil.initWithSpring(true);
\r
121 String fileName = arguments.get("service.input.file");
\r
122 String hbmFileName = arguments.get("service.hbm.file");
\r
123 String ormFileName = arguments.get("service.orm.file");
\r
124 String modelHintsFileName = arguments.get("service.model.hints.file");
\r
125 String springFileName = arguments.get("service.spring.file");
\r
126 String springBaseFileName = arguments.get("service.spring.base.file");
\r
127 String springClusterFileName = arguments.get("service.spring.cluster.file");
\r
128 String springDynamicDataSourceFileName = arguments.get("service.spring.dynamic.data.source.file");
\r
129 String springHibernateFileName = arguments.get("service.spring.hibernate.file");
\r
130 String springInfrastructureFileName = arguments.get("service.spring.infrastructure.file");
\r
131 String springShardDataSourceFileName = arguments.get("service.spring.shard.data.source.file");
\r
132 String apiDir = arguments.get("service.api.dir");
\r
133 String implDir = arguments.get("service.impl.dir");
\r
134 String jsonFileName = arguments.get("service.json.file");
\r
135 String remotingFileName = arguments.get("service.remoting.file");
\r
136 String sqlDir = arguments.get("service.sql.dir");
\r
137 String sqlFileName = arguments.get("service.sql.file");
\r
138 String sqlIndexesFileName = arguments.get("service.sql.indexes.file");
\r
139 String sqlIndexesPropertiesFileName = arguments.get("service.sql.indexes.properties.file");
\r
140 String sqlSequencesFileName = arguments.get("service.sql.sequences.file");
\r
141 boolean autoNamespaceTables = GetterUtil.getBoolean(arguments.get("service.auto.namespace.tables"));
\r
142 String beanLocatorUtil = arguments.get("service.bean.locator.util");
\r
143 String propsUtil = arguments.get("service.props.util");
\r
144 String pluginName = arguments.get("service.plugin.name");
\r
145 String targetEntityName = arguments.get("service.target.entity.name");
\r
146 String testDir = arguments.get("service.test.dir");
\r
147 long buildNumber = GetterUtil.getLong(arguments.get("service.build.number"), 1);
\r
148 boolean buildNumberIncrement = GetterUtil.getBoolean(arguments.get("service.build.number.increment"), true);
\r
151 new ServiceBuilder(
\r
152 fileName, hbmFileName, ormFileName, modelHintsFileName,
\r
153 springFileName, springBaseFileName, springClusterFileName,
\r
154 springDynamicDataSourceFileName, springHibernateFileName,
\r
155 springInfrastructureFileName, springShardDataSourceFileName,
\r
156 apiDir, implDir, jsonFileName, remotingFileName, sqlDir,
\r
157 sqlFileName, sqlIndexesFileName, sqlIndexesPropertiesFileName,
\r
158 sqlSequencesFileName, autoNamespaceTables, beanLocatorUtil,
\r
159 propsUtil, pluginName, targetEntityName, testDir, true,
\r
160 buildNumber, buildNumberIncrement);
\r
162 catch (RuntimeException re) {
\r
163 System.out.println(
\r
164 "Please set these required arguments. Sample values are:\n" +
\r
166 "\tservice.input.file=${service.file}\n" +
\r
167 "\tservice.hbm.file=${basedir}/src/META-INF/portal-hbm.xml\n" +
\r
168 "\tservice.orm.file=${basedir}/src/META-INF/portal-orm.xml\n" +
\r
169 "\tservice.model.hints.file=${basedir}/src/META-INF/portal-model-hints.xml\n" +
\r
170 "\tservice.spring.file=${basedir}/src/META-INF/portal-spring.xml\n" +
\r
171 "\tservice.api.dir=${basedir}/../portal-service/src\n" +
\r
172 "\tservice.impl.dir=${basedir}/src\n" +
\r
173 "\tservice.json.file=${basedir}/../portal-web/docroot/html/js/liferay/service.js\n" +
\r
174 "\tservice.remoting.file=${basedir}/../portal-web/docroot/WEB-INF/remoting-servlet.xml\n" +
\r
175 "\tservice.sql.dir=${basedir}/../sql\n" +
\r
176 "\tservice.sql.file=portal-tables.sql\n" +
\r
177 "\tservice.sql.indexes.file=indexes.sql\n" +
\r
178 "\tservice.sql.indexes.properties.file=indexes.properties\n" +
\r
179 "\tservice.sql.sequences.file=sequences.sql\n" +
\r
180 "\tservice.bean.locator.util=com.liferay.portal.kernel.bean.PortalBeanLocatorUtil\n" +
\r
181 "\tservice.props.util=com.liferay.portal.util.PropsUtil\n" +
\r
182 "\tservice.target.entity.name=${service.target.entity.name}\n" +
\r
183 "\tservice.test.dir=${basedir}/test/integration\n" +
\r
184 "\tservice.build.number=1\n" +
\r
185 "\tservice.build.number.increment=true\n" +
\r
187 "You can also customize the generated code by overriding the default templates with these optional system properties:\n" +
\r
189 "\t-Dservice.tpl.bad_alias_names=" + _TPL_ROOT + "bad_alias_names.txt\n"+
\r
190 "\t-Dservice.tpl.bad_column_names=" + _TPL_ROOT + "bad_column_names.txt\n"+
\r
191 "\t-Dservice.tpl.bad_json_types=" + _TPL_ROOT + "bad_json_types.txt\n"+
\r
192 "\t-Dservice.tpl.bad_table_names=" + _TPL_ROOT + "bad_table_names.txt\n"+
\r
193 "\t-Dservice.tpl.base_mode_impl=" + _TPL_ROOT + "base_mode_impl.ftl\n"+
\r
194 "\t-Dservice.tpl.blob_model=" + _TPL_ROOT + "blob_model.ftl\n"+
\r
195 "\t-Dservice.tpl.copyright.txt=copyright.txt\n"+
\r
196 "\t-Dservice.tpl.ejb_pk=" + _TPL_ROOT + "ejb_pk.ftl\n"+
\r
197 "\t-Dservice.tpl.exception=" + _TPL_ROOT + "exception.ftl\n"+
\r
198 "\t-Dservice.tpl.extended_model=" + _TPL_ROOT + "extended_model.ftl\n"+
\r
199 "\t-Dservice.tpl.extended_model_base_impl=" + _TPL_ROOT + "extended_model_base_impl.ftl\n"+
\r
200 "\t-Dservice.tpl.extended_model_impl=" + _TPL_ROOT + "extended_model_impl.ftl\n"+
\r
201 "\t-Dservice.tpl.finder=" + _TPL_ROOT + "finder.ftl\n"+
\r
202 "\t-Dservice.tpl.finder_util=" + _TPL_ROOT + "finder_util.ftl\n"+
\r
203 "\t-Dservice.tpl.hbm_xml=" + _TPL_ROOT + "hbm_xml.ftl\n"+
\r
204 "\t-Dservice.tpl.orm_xml=" + _TPL_ROOT + "orm_xml.ftl\n"+
\r
205 "\t-Dservice.tpl.json_js=" + _TPL_ROOT + "json_js.ftl\n"+
\r
206 "\t-Dservice.tpl.json_js_method=" + _TPL_ROOT + "json_js_method.ftl\n"+
\r
207 "\t-Dservice.tpl.model=" + _TPL_ROOT + "model.ftl\n"+
\r
208 "\t-Dservice.tpl.model_cache=" + _TPL_ROOT + "model_cache.ftl\n"+
\r
209 "\t-Dservice.tpl.model_hints_xml=" + _TPL_ROOT + "model_hints_xml.ftl\n"+
\r
210 "\t-Dservice.tpl.model_impl=" + _TPL_ROOT + "model_impl.ftl\n"+
\r
211 "\t-Dservice.tpl.model_soap=" + _TPL_ROOT + "model_soap.ftl\n"+
\r
212 "\t-Dservice.tpl.model_wrapper=" + _TPL_ROOT + "model_wrapper.ftl\n"+
\r
213 "\t-Dservice.tpl.persistence=" + _TPL_ROOT + "persistence.ftl\n"+
\r
214 "\t-Dservice.tpl.persistence_impl=" + _TPL_ROOT + "persistence_impl.ftl\n"+
\r
215 "\t-Dservice.tpl.persistence_util=" + _TPL_ROOT + "persistence_util.ftl\n"+
\r
216 "\t-Dservice.tpl.props=" + _TPL_ROOT + "props.ftl\n"+
\r
217 "\t-Dservice.tpl.remoting_xml=" + _TPL_ROOT + "remoting_xml.ftl\n"+
\r
218 "\t-Dservice.tpl.service=" + _TPL_ROOT + "service.ftl\n"+
\r
219 "\t-Dservice.tpl.service_base_impl=" + _TPL_ROOT + "service_base_impl.ftl\n"+
\r
220 "\t-Dservice.tpl.service_clp=" + _TPL_ROOT + "service_clp.ftl\n"+
\r
221 "\t-Dservice.tpl.service_clp_invoker=" + _TPL_ROOT + "service_clp_invoker.ftl\n"+
\r
222 "\t-Dservice.tpl.service_clp_message_listener=" + _TPL_ROOT + "service_clp_message_listener.ftl\n"+
\r
223 "\t-Dservice.tpl.service_clp_serializer=" + _TPL_ROOT + "service_clp_serializer.ftl\n"+
\r
224 "\t-Dservice.tpl.service_http=" + _TPL_ROOT + "service_http.ftl\n"+
\r
225 "\t-Dservice.tpl.service_impl=" + _TPL_ROOT + "service_impl.ftl\n"+
\r
226 "\t-Dservice.tpl.service_soap=" + _TPL_ROOT + "service_soap.ftl\n"+
\r
227 "\t-Dservice.tpl.service_util=" + _TPL_ROOT + "service_util.ftl\n"+
\r
228 "\t-Dservice.tpl.service_wrapper=" + _TPL_ROOT + "service_wrapper.ftl\n"+
\r
229 "\t-Dservice.tpl.spring_base_xml=" + _TPL_ROOT + "spring_base_xml.ftl\n"+
\r
230 "\t-Dservice.tpl.spring_dynamic_data_source_xml=" + _TPL_ROOT + "spring_dynamic_data_source_xml.ftl\n"+
\r
231 "\t-Dservice.tpl.spring_hibernate_xml=" + _TPL_ROOT + "spring_hibernate_xml.ftl\n"+
\r
232 "\t-Dservice.tpl.spring_infrastructure_xml=" + _TPL_ROOT + "spring_infrastructure_xml.ftl\n"+
\r
233 "\t-Dservice.tpl.spring_xml=" + _TPL_ROOT + "spring_xml.ftl\n"+
\r
234 "\t-Dservice.tpl.spring_xml_session=" + _TPL_ROOT + "spring_xml_session.ftl");
\r
240 ClearThreadLocalUtil.clearThreadLocal();
\r
242 catch (Exception e) {
\r
243 e.printStackTrace();
\r
246 Introspector.flushCaches();
\r
249 public static String toHumanName(String name) {
\r
250 if (name == null) {
\r
254 String humanName = TextFormatter.format(name, TextFormatter.H);
\r
256 if (humanName.equals("id")) {
\r
259 else if (humanName.equals("ids")) {
\r
263 if (humanName.endsWith(" id")) {
\r
264 humanName = humanName.substring(0, humanName.length() - 3) + " ID";
\r
266 else if (humanName.endsWith(" ids")) {
\r
267 humanName = humanName.substring(0, humanName.length() - 4) + " IDs";
\r
270 if (humanName.contains(" id ")) {
\r
271 humanName = StringUtil.replace(humanName, " id ", " ID ");
\r
273 else if (humanName.contains(" ids ")) {
\r
274 humanName = StringUtil.replace(humanName, " ids ", " IDs ");
\r
280 public static void writeFile(File file, String content)
\r
281 throws IOException {
\r
283 writeFile(file, content, AUTHOR);
\r
286 public static void writeFile(File file, String content, String author)
\r
287 throws IOException {
\r
289 writeFile(file, content, author, null);
\r
292 public static void writeFile(
\r
293 File file, String content, String author,
\r
294 Map<String, Object> jalopySettings)
\r
295 throws IOException {
\r
297 String packagePath = _getPackagePath(file);
\r
299 String className = file.getName();
\r
301 className = className.substring(0, className.length() - 5);
\r
303 content = SourceFormatter.stripJavaImports(
\r
304 content, packagePath, className);
\r
306 File tempFile = new File("ServiceBuilder.temp");
\r
308 FileUtil.write(tempFile, content);
\r
312 StringBuffer sb = new StringBuffer();
\r
314 Jalopy jalopy = new Jalopy();
\r
316 jalopy.setFileFormat(FileFormat.UNIX);
\r
317 jalopy.setInput(tempFile);
\r
318 jalopy.setOutput(sb);
\r
320 File jalopyXmlFile = new File("tools/jalopy.xml");
\r
322 if (!jalopyXmlFile.exists()) {
\r
323 jalopyXmlFile = new File("../tools/jalopy.xml");
\r
326 if (!jalopyXmlFile.exists()) {
\r
327 jalopyXmlFile = new File("misc/jalopy.xml");
\r
330 if (!jalopyXmlFile.exists()) {
\r
331 jalopyXmlFile = new File("../misc/jalopy.xml");
\r
334 if (!jalopyXmlFile.exists()) {
\r
335 jalopyXmlFile = new File("../../misc/jalopy.xml");
\r
339 Jalopy.setConvention(jalopyXmlFile);
\r
341 catch (FileNotFoundException fnfe) {
\r
344 if (jalopySettings == null) {
\r
345 jalopySettings = new HashMap<String, Object>();
\r
348 Environment env = Environment.getInstance();
\r
352 author = GetterUtil.getString(
\r
353 (String)jalopySettings.get("author"), author);
\r
355 env.set("author", author);
\r
359 env.set("fileName", file.getName());
\r
361 Convention convention = Convention.getInstance();
\r
363 String classMask = "/**\n * @author $author$\n*/";
\r
366 ConventionKeys.COMMENT_JAVADOC_TEMPLATE_CLASS,
\r
367 env.interpolate(classMask));
\r
370 ConventionKeys.COMMENT_JAVADOC_TEMPLATE_INTERFACE,
\r
371 env.interpolate(classMask));
\r
375 String newContent = sb.toString();
\r
377 // Remove double blank lines after the package or last import
\r
379 newContent = newContent.replaceFirst(
\r
380 "(?m)^[ \t]*((?:package|import) .*;)\\s*^[ \t]*/\\*\\*",
\r
384 // Remove blank lines after try {
\r
386 newContent = StringUtil.replace(newContent, "try {\n\n", "try {\n");
\r
388 // Remove blank lines after ) {
\r
390 newContent = StringUtil.replace(newContent, ") {\n\n", ") {\n");
\r
392 // Remove blank lines empty braces { }
\r
394 newContent = StringUtil.replace(newContent, "\n\n\t}", "\n\t}");
\r
396 // Add space to last }
\r
398 newContent = newContent.substring(0, newContent.length() - 2) + "\n\n}";
\r
401 writeFileRaw(file, newContent);
\r
403 tempFile.deleteOnExit();
\r
406 public static void writeFileRaw(File file, String content)
\r
407 throws IOException {
\r
409 // Write file if and only if the file has changed
\r
411 if (!file.exists() || !FileUtil.isSameContent(file, content)) {
\r
412 FileUtil.write(file, content);
\r
414 System.out.println("Writing " + file);
\r
418 public ServiceBuilder(
\r
419 String fileName, String hbmFileName, String ormFileName,
\r
420 String modelHintsFileName, String springFileName,
\r
421 String springBaseFileName, String springClusterFileName,
\r
422 String springDynamicDataSourceFileName, String springHibernateFileName,
\r
423 String springInfrastructureFileName,
\r
424 String springShardDataSourceFileName, String apiDir, String implDir,
\r
425 String jsonFileName, String remotingFileName, String sqlDir,
\r
426 String sqlFileName, String sqlIndexesFileName,
\r
427 String sqlIndexesPropertiesFileName, String sqlSequencesFileName,
\r
428 boolean autoNamespaceTables, String beanLocatorUtil, String propsUtil,
\r
429 String pluginName, String targetEntityName, String testDir) {
\r
432 fileName, hbmFileName, ormFileName, modelHintsFileName,
\r
433 springFileName, springBaseFileName, springClusterFileName,
\r
434 springDynamicDataSourceFileName, springHibernateFileName,
\r
435 springInfrastructureFileName, springShardDataSourceFileName, apiDir,
\r
436 implDir, jsonFileName, remotingFileName, sqlDir, sqlFileName,
\r
437 sqlIndexesFileName, sqlIndexesPropertiesFileName,
\r
438 sqlSequencesFileName, autoNamespaceTables, beanLocatorUtil,
\r
439 propsUtil, pluginName, targetEntityName, testDir, true, 1, true);
\r
442 public ServiceBuilder(
\r
443 String fileName, String hbmFileName, String ormFileName,
\r
444 String modelHintsFileName, String springFileName,
\r
445 String springBaseFileName, String springClusterFileName,
\r
446 String springDynamicDataSourceFileName, String springHibernateFileName,
\r
447 String springInfrastructureFileName,
\r
448 String springShardDataSourceFileName, String apiDir, String implDir,
\r
449 String jsonFileName, String remotingFileName, String sqlDir,
\r
450 String sqlFileName, String sqlIndexesFileName,
\r
451 String sqlIndexesPropertiesFileName, String sqlSequencesFileName,
\r
452 boolean autoNamespaceTables, String beanLocatorUtil, String propsUtil,
\r
453 String pluginName, String targetEntityName, String testDir,
\r
454 boolean build, long buildNumber, boolean buildNumberIncrement) {
\r
456 _tplBadAliasNames = _getTplProperty(
\r
457 "bad_alias_names", _tplBadAliasNames);
\r
458 _tplBadColumnNames = _getTplProperty(
\r
459 "bad_column_names", _tplBadColumnNames);
\r
460 _tplBadJsonTypes = _getTplProperty("bad_json_types", _tplBadJsonTypes);
\r
461 _tplBadTableNames = _getTplProperty(
\r
462 "bad_table_names", _tplBadTableNames);
\r
463 _tplBlobModel = _getTplProperty("blob_model", _tplBlobModel);
\r
464 _tplEjbPk = _getTplProperty("ejb_pk", _tplEjbPk);
\r
465 _tplException = _getTplProperty("exception", _tplException);
\r
466 _tplExtendedModel = _getTplProperty(
\r
467 "extended_model", _tplExtendedModel);
\r
468 _tplExtendedModelBaseImpl = _getTplProperty(
\r
469 "extended_model_base_impl", _tplExtendedModelBaseImpl);
\r
470 _tplExtendedModelImpl = _getTplProperty(
\r
471 "extended_model_impl", _tplExtendedModelImpl);
\r
472 _tplFinder = _getTplProperty("finder", _tplFinder);
\r
473 _tplFinderUtil = _getTplProperty("finder_util", _tplFinderUtil);
\r
474 _tplHbmXml = _getTplProperty("hbm_xml", _tplHbmXml);
\r
475 _tplOrmXml = _getTplProperty("orm_xml", _tplOrmXml);
\r
476 _tplJsonJs = _getTplProperty("json_js", _tplJsonJs);
\r
477 _tplJsonJsMethod = _getTplProperty("json_js_method", _tplJsonJsMethod);
\r
478 _tplModel = _getTplProperty("model", _tplModel);
\r
479 _tplModelCache = _getTplProperty("model_cache", _tplModelCache);
\r
480 _tplModelClp = _getTplProperty("model", _tplModelClp);
\r
481 _tplModelHintsXml = _getTplProperty(
\r
482 "model_hints_xml", _tplModelHintsXml);
\r
483 _tplModelImpl = _getTplProperty("model_impl", _tplModelImpl);
\r
484 _tplModelSoap = _getTplProperty("model_soap", _tplModelSoap);
\r
485 _tplModelWrapper = _getTplProperty("model_wrapper", _tplModelWrapper);
\r
486 _tplPersistence = _getTplProperty("persistence", _tplPersistence);
\r
487 _tplPersistenceImpl = _getTplProperty(
\r
488 "persistence_impl", _tplPersistenceImpl);
\r
489 _tplPersistenceUtil = _getTplProperty(
\r
490 "persistence_util", _tplPersistenceUtil);
\r
491 _tplProps = _getTplProperty("props", _tplProps);
\r
492 _tplRemotingXml = _getTplProperty("remoting_xml", _tplRemotingXml);
\r
493 _tplService = _getTplProperty("service", _tplService);
\r
494 _tplServiceBaseImpl = _getTplProperty(
\r
495 "service_base_impl", _tplServiceBaseImpl);
\r
496 _tplServiceClp = _getTplProperty("service_clp", _tplServiceClp);
\r
497 _tplServiceClpInvoker = _getTplProperty(
\r
498 "service_clp_invoker", _tplServiceClpInvoker);
\r
499 _tplServiceClpMessageListener = _getTplProperty(
\r
500 "service_clp_message_listener", _tplServiceClpMessageListener);
\r
501 _tplServiceClpSerializer = _getTplProperty(
\r
502 "service_clp_serializer", _tplServiceClpSerializer);
\r
503 _tplServiceHttp = _getTplProperty("service_http", _tplServiceHttp);
\r
504 _tplServiceImpl = _getTplProperty("service_impl", _tplServiceImpl);
\r
505 _tplServiceSoap = _getTplProperty("service_soap", _tplServiceSoap);
\r
506 _tplServiceUtil = _getTplProperty("service_util", _tplServiceUtil);
\r
507 _tplServiceWrapper = _getTplProperty(
\r
508 "service_wrapper", _tplServiceWrapper);
\r
509 _tplSpringBaseXml = _getTplProperty(
\r
510 "spring_base_xml", _tplSpringBaseXml);
\r
511 _tplSpringClusterXml = _getTplProperty(
\r
512 "spring_cluster_xml", _tplSpringClusterXml);
\r
513 _tplSpringDynamicDataSourceXml = _getTplProperty(
\r
514 "spring_dynamic_data_source_xml", _tplSpringDynamicDataSourceXml);
\r
515 _tplSpringHibernateXml = _getTplProperty(
\r
516 "spring_hibernate_xml", _tplSpringHibernateXml);
\r
517 _tplSpringInfrastructureXml = _getTplProperty(
\r
518 "spring_infrastructure_xml", _tplSpringInfrastructureXml);
\r
519 _tplSpringShardDataSourceXml = _getTplProperty(
\r
520 "spring_shard_data_source_xml", _tplSpringShardDataSourceXml);
\r
521 _tplSpringXml = _getTplProperty("spring_xml", _tplSpringXml);
\r
522 _tplSpringClearService = _getTplProperty(
\r
523 "spring_clear_service", _tplSpringClearService);
\r
525 _badTableNames = _readLines(_tplBadTableNames);
\r
526 _badAliasNames = _readLines(_tplBadAliasNames);
\r
527 _badColumnNames = _readLines(_tplBadColumnNames);
\r
528 _badJsonTypes = _readLines(_tplBadJsonTypes);
\r
529 _hbmFileName = hbmFileName;
\r
530 _ormFileName = ormFileName;
\r
531 _modelHintsFileName = modelHintsFileName;
\r
532 _springFileName = springFileName;
\r
533 _springBaseFileName = springBaseFileName;
\r
534 _springClusterFileName = springClusterFileName;
\r
535 _springDynamicDataSourceFileName = springDynamicDataSourceFileName;
\r
536 _springHibernateFileName = springHibernateFileName;
\r
537 _springInfrastructureFileName = springInfrastructureFileName;
\r
538 _springShardDataSourceFileName = springShardDataSourceFileName;
\r
540 _implDir = implDir;
\r
541 _jsonFileName = jsonFileName;
\r
542 _remotingFileName = remotingFileName;
\r
544 _sqlFileName = sqlFileName;
\r
545 _sqlIndexesFileName = sqlIndexesFileName;
\r
546 _sqlIndexesPropertiesFileName = sqlIndexesPropertiesFileName;
\r
547 _sqlSequencesFileName = sqlSequencesFileName;
\r
548 _autoNamespaceTables = autoNamespaceTables;
\r
549 _beanLocatorUtil = beanLocatorUtil;
\r
550 _beanLocatorUtilShortName = _beanLocatorUtil.substring(
\r
551 _beanLocatorUtil.lastIndexOf(".") + 1);
\r
552 _propsUtil = propsUtil;
\r
553 _pluginName = GetterUtil.getString(pluginName);
\r
554 _targetEntityName = targetEntityName;
\r
555 _testDir = testDir;
\r
557 _buildNumber = buildNumber;
\r
558 _buildNumberIncrement = buildNumberIncrement;
\r
560 String content = _getContent(fileName);
\r
562 Document document = SAXReaderUtil.read(content, true);
\r
564 Element rootElement = document.getRootElement();
\r
566 String packagePath = rootElement.attributeValue("package-path");
\r
568 if (Validator.isNull(packagePath)) {
\r
569 throw new IllegalArgumentException(
\r
570 "The package-path attribute is required");
\r
574 _implDir + "/" + StringUtil.replace(packagePath, ".", "/");
\r
576 _serviceOutputPath =
\r
577 _apiDir + "/" + StringUtil.replace(packagePath, ".", "/");
\r
579 if (Validator.isNotNull(_testDir)) {
\r
581 _testDir + "/" + StringUtil.replace(packagePath, ".", "/");
\r
584 _packagePath = packagePath;
\r
586 _autoNamespaceTables = GetterUtil.getBoolean(
\r
587 rootElement.attributeValue("auto-namespace-tables"),
\r
588 _autoNamespaceTables);
\r
590 Element authorElement = rootElement.element("author");
\r
592 if (authorElement != null) {
\r
593 _author = authorElement.getText();
\r
599 Element portletElement = rootElement.element("portlet");
\r
600 Element namespaceElement = rootElement.element("namespace");
\r
602 if (portletElement != null) {
\r
603 _portletName = portletElement.attributeValue("name");
\r
605 _portletShortName = portletElement.attributeValue("short-name");
\r
607 _portletPackageName = TextFormatter.format(
\r
608 _portletName, TextFormatter.B);
\r
610 _outputPath += "/" + _portletPackageName;
\r
612 _serviceOutputPath += "/" + _portletPackageName;
\r
614 _testOutputPath += "/" + _portletPackageName;
\r
616 _packagePath += "." + _portletPackageName;
\r
619 _portletShortName = namespaceElement.getText();
\r
622 _portletShortName = _portletShortName.trim();
\r
624 for (char c : _portletShortName.toCharArray()) {
\r
625 if (!Validator.isChar(c) && (c != CharPool.UNDERLINE)) {
\r
626 throw new RuntimeException(
\r
627 "The namespace element must be a valid keyword");
\r
631 _ejbList = new ArrayList<Entity>();
\r
632 _entityMappings = new HashMap<String, EntityMapping>();
\r
634 List<Element> entityElements = rootElement.elements("entity");
\r
636 for (Element entityElement : entityElements) {
\r
637 _parseEntity(entityElement);
\r
640 List<String> exceptionList = new ArrayList<String>();
\r
642 Element exceptionsElement = rootElement.element("exceptions");
\r
644 if (exceptionsElement != null) {
\r
645 List<Element> exceptionElements = exceptionsElement.elements(
\r
648 for (Element exceptionElement : exceptionElements) {
\r
649 exceptionList.add(exceptionElement.getText());
\r
654 for (int x = 0; x < _ejbList.size(); x++) {
\r
655 Entity entity = _ejbList.get(x);
\r
657 if (_isTargetEntity(entity)) {
\r
658 System.out.println("Building " + entity.getName());
\r
660 if (entity.hasColumns()) {
\r
661 _createHbm(entity);
\r
662 _createHbmUtil(entity);
\r
664 _createPersistenceImpl(entity);
\r
665 _createPersistence(entity);
\r
666 _createPersistenceUtil(entity);
\r
668 if (Validator.isNotNull(_testDir)) {
\r
669 _createPersistenceTest(entity);
\r
672 _createModelImpl(entity);
\r
673 _createExtendedModelBaseImpl(entity);
\r
674 _createExtendedModelImpl(entity);
\r
676 entity.setTransients(_getTransients(entity, false));
\r
677 entity.setParentTransients(
\r
678 _getTransients(entity, true));
\r
680 _createModel(entity);
\r
681 _createExtendedModel(entity);
\r
683 _createModelCache(entity);
\r
684 _createModelClp(entity);
\r
685 _createModelWrapper(entity);
\r
687 _createModelSoap(entity);
\r
689 _createBlobModels(entity);
\r
691 _createPool(entity);
\r
693 if (entity.getPKList().size() > 1) {
\r
694 _createEJBPK(entity);
\r
698 _createFinder(entity);
\r
699 _createFinderUtil(entity);
\r
701 if (entity.hasLocalService()) {
\r
702 _createServiceImpl(entity, _SESSION_TYPE_LOCAL);
\r
703 _createServiceBaseImpl(entity, _SESSION_TYPE_LOCAL);
\r
704 _createService(entity, _SESSION_TYPE_LOCAL);
\r
705 _createServiceFactory(entity, _SESSION_TYPE_LOCAL);
\r
706 _createServiceUtil(entity, _SESSION_TYPE_LOCAL);
\r
708 _createServiceClp(entity, _SESSION_TYPE_LOCAL);
\r
709 _createServiceClpInvoker(
\r
710 entity, _SESSION_TYPE_LOCAL);
\r
711 _createServiceWrapper(entity, _SESSION_TYPE_LOCAL);
\r
714 if (entity.hasRemoteService()) {
\r
715 _createServiceImpl(entity, _SESSION_TYPE_REMOTE);
\r
716 _createServiceBaseImpl(
\r
717 entity, _SESSION_TYPE_REMOTE);
\r
718 _createService(entity, _SESSION_TYPE_REMOTE);
\r
719 _createServiceFactory(entity, _SESSION_TYPE_REMOTE);
\r
720 _createServiceUtil(entity, _SESSION_TYPE_REMOTE);
\r
722 _createServiceClp(entity, _SESSION_TYPE_REMOTE);
\r
723 _createServiceClpInvoker(
\r
724 entity, _SESSION_TYPE_REMOTE);
\r
725 _createServiceWrapper(entity, _SESSION_TYPE_REMOTE);
\r
727 if (Validator.isNotNull(_remotingFileName)) {
\r
728 _createServiceHttp(entity);
\r
731 _createServiceJson(entity);
\r
733 if (entity.hasColumns()) {
\r
734 _createServiceJsonSerializer(entity);
\r
737 _createServiceSoap(entity);
\r
741 if (entity.hasColumns()) {
\r
742 entity.setTransients(_getTransients(entity, false));
\r
743 entity.setParentTransients(
\r
744 _getTransients(entity, true));
\r
751 _createModelHintsXml();
\r
752 _createSpringXml();
\r
753 _createSpringClearService();
\r
755 _createExceptions(exceptionList);
\r
757 _createServiceClpMessageListener();
\r
758 _createServiceClpSerializer(exceptionList);
\r
762 if (Validator.isNotNull(_remotingFileName)) {
\r
763 _createRemotingXml();
\r
766 _createSQLIndexes();
\r
767 _createSQLTables();
\r
768 _createSQLSequences();
\r
771 _createSpringBaseXml();
\r
772 _createSpringClusterXml();
\r
773 _createSpringDynamicDataSourceXml();
\r
774 _createSpringHibernateXml();
\r
775 _createSpringInfrastructureXml();
\r
776 _createSpringShardDataSourceXml();
\r
779 catch (FileNotFoundException fnfe) {
\r
780 System.out.println(fnfe.getMessage());
\r
782 catch (Exception e) {
\r
783 e.printStackTrace();
\r
787 public String getClassName(Type type) {
\r
788 int dimensions = type.getDimensions();
\r
789 String name = type.getValue();
\r
791 if (dimensions > 0) {
\r
792 StringBundler sb = new StringBundler();
\r
794 for (int i = 0; i < dimensions; i++) {
\r
798 if (name.equals("boolean")) {
\r
799 return sb.append("Z").toString();
\r
801 else if (name.equals("byte")) {
\r
802 return sb.append("B").toString();
\r
804 else if (name.equals("char")) {
\r
805 return sb.append("C").toString();
\r
807 else if (name.equals("double")) {
\r
808 return sb.append("D").toString();
\r
810 else if (name.equals("float")) {
\r
811 return sb.append("F").toString();
\r
813 else if (name.equals("int")) {
\r
814 return sb.append("I").toString();
\r
816 else if (name.equals("long")) {
\r
817 return sb.append("J").toString();
\r
819 else if (name.equals("short")) {
\r
820 return sb.append("S").toString();
\r
823 return sb.append("L").append(name).append(";").toString();
\r
830 public String getCreateMappingTableSQL(EntityMapping entityMapping)
\r
831 throws IOException {
\r
833 String createMappingTableSQL = _getCreateMappingTableSQL(entityMapping);
\r
835 createMappingTableSQL = StringUtil.replace(
\r
836 createMappingTableSQL, "\n", "");
\r
837 createMappingTableSQL = StringUtil.replace(
\r
838 createMappingTableSQL, "\t", "");
\r
839 createMappingTableSQL = createMappingTableSQL.substring(
\r
840 0, createMappingTableSQL.length() - 1);
\r
842 return createMappingTableSQL;
\r
845 public String getCreateTableSQL(Entity entity) {
\r
846 String createTableSQL = _getCreateTableSQL(entity);
\r
848 createTableSQL = StringUtil.replace(createTableSQL, "\n", "");
\r
849 createTableSQL = StringUtil.replace(createTableSQL, "\t", "");
\r
850 createTableSQL = createTableSQL.substring(
\r
851 0, createTableSQL.length() - 1);
\r
853 return createTableSQL;
\r
856 public String getDimensions(int dims) {
\r
857 String dimensions = "";
\r
859 for (int i = 0; i < dims; i++) {
\r
860 dimensions += "[]";
\r
866 public String getDimensions(String dims) {
\r
867 return getDimensions(GetterUtil.getInteger(dims));
\r
870 public Entity getEntity(String name) throws IOException {
\r
871 Entity entity = _entityPool.get(name);
\r
873 if (entity != null) {
\r
877 int pos = name.lastIndexOf(".");
\r
880 pos = _ejbList.indexOf(new Entity(name));
\r
883 throw new RuntimeException(
\r
884 "Cannot find " + name + " in " +
\r
885 ListUtil.toString(_ejbList, Entity.NAME_ACCESSOR));
\r
888 entity = _ejbList.get(pos);
\r
890 _entityPool.put(name, entity);
\r
895 String refPackage = name.substring(0, pos);
\r
896 String refEntity = name.substring(pos + 1);
\r
898 if (refPackage.equals(_packagePath)) {
\r
899 pos = _ejbList.indexOf(new Entity(refEntity));
\r
902 throw new RuntimeException(
\r
903 "Cannot find " + refEntity + " in " +
\r
904 ListUtil.toString(_ejbList, Entity.NAME_ACCESSOR));
\r
907 entity = _ejbList.get(pos);
\r
909 _entityPool.put(name, entity);
\r
914 String refPackageDir = StringUtil.replace(refPackage, ".", "/");
\r
916 String refFileName =
\r
917 _implDir + "/" + refPackageDir + "/service.xml";
\r
919 File refFile = new File(refFileName);
\r
921 boolean useTempFile = false;
\r
923 if (!refFile.exists()) {
\r
924 refFileName = Time.getTimestamp();
\r
925 refFile = new File(refFileName);
\r
927 ClassLoader classLoader = getClass().getClassLoader();
\r
932 classLoader, refPackageDir + "/service.xml"));
\r
934 useTempFile = true;
\r
937 ServiceBuilder serviceBuilder = new ServiceBuilder(
\r
938 refFileName, _hbmFileName, _ormFileName, _modelHintsFileName,
\r
939 _springFileName, _springBaseFileName, _springClusterFileName,
\r
940 _springDynamicDataSourceFileName, _springHibernateFileName,
\r
941 _springInfrastructureFileName, _springShardDataSourceFileName,
\r
942 _apiDir, _implDir, _jsonFileName, _remotingFileName, _sqlDir,
\r
943 _sqlFileName, _sqlIndexesFileName,
\r
944 _sqlIndexesPropertiesFileName, _sqlSequencesFileName,
\r
945 _autoNamespaceTables, _beanLocatorUtil, _propsUtil, _pluginName,
\r
946 _targetEntityName, _testDir, false, _buildNumber,
\r
947 _buildNumberIncrement);
\r
949 entity = serviceBuilder.getEntity(refEntity);
\r
951 entity.setPortalReference(useTempFile);
\r
953 _entityPool.put(name, entity);
\r
956 refFile.deleteOnExit();
\r
963 public Entity getEntityByGenericsName(String genericsName) {
\r
965 String name = genericsName;
\r
967 if (name.startsWith("<")) {
\r
968 name = name.substring(1, name.length() - 1);
\r
971 name = StringUtil.replace(name, ".model.", ".");
\r
973 return getEntity(name);
\r
975 catch (Exception e) {
\r
980 public Entity getEntityByParameterTypeValue(String parameterTypeValue) {
\r
982 String name = parameterTypeValue;
\r
984 name = StringUtil.replace(name, ".model.", ".");
\r
986 return getEntity(name);
\r
988 catch (Exception e) {
\r
993 public EntityMapping getEntityMapping(String mappingTable) {
\r
994 return _entityMappings.get(mappingTable);
\r
997 public String getGeneratorClass(String idType) {
\r
998 if (Validator.isNull(idType)) {
\r
999 idType = "assigned";
\r
1005 public String getJavadocComment(JavaClass javaClass) {
\r
1006 return _formatComment(
\r
1007 javaClass.getComment(), javaClass.getTags(), StringPool.BLANK);
\r
1010 public String getJavadocComment(JavaMethod javaMethod) {
\r
1011 return _formatComment(
\r
1012 javaMethod.getComment(), javaMethod.getTags(), StringPool.TAB);
\r
1015 public String getListActualTypeArguments(Type type) {
\r
1016 if (type.getValue().equals("java.util.List")) {
\r
1017 Type[] types = type.getActualTypeArguments();
\r
1019 if (types != null) {
\r
1020 return getTypeGenericsName(types[0]);
\r
1024 return getTypeGenericsName(type);
\r
1027 public String getLiteralClass(Type type) {
\r
1028 StringBundler sb = new StringBundler(type.getDimensions() + 2);
\r
1030 sb.append(type.getValue());
\r
1032 for (int i = 0; i < type.getDimensions(); i++) {
\r
1036 sb.append(".class");
\r
1038 return sb.toString();
\r
1041 public List<EntityColumn> getMappingEntities(String mappingTable)
\r
1042 throws IOException {
\r
1044 List<EntityColumn> mappingEntitiesPKList =
\r
1045 new ArrayList<EntityColumn>();
\r
1047 EntityMapping entityMapping = _entityMappings.get(mappingTable);
\r
1049 for (int i = 0; i < 2; i++) {
\r
1050 Entity entity = getEntity(entityMapping.getEntity(i));
\r
1052 if (entity == null) {
\r
1056 mappingEntitiesPKList.addAll(entity.getPKList());
\r
1059 return mappingEntitiesPKList;
\r
1062 public String getNoSuchEntityException(Entity entity) {
\r
1063 String noSuchEntityException = entity.getName();
\r
1065 if (Validator.isNull(entity.getPortletShortName()) ||
\r
1066 (noSuchEntityException.startsWith(entity.getPortletShortName()) &&
\r
1067 !noSuchEntityException.equals(entity.getPortletShortName()))) {
\r
1069 noSuchEntityException = noSuchEntityException.substring(
\r
1070 entity.getPortletShortName().length());
\r
1073 noSuchEntityException = "NoSuch" + noSuchEntityException;
\r
1075 return noSuchEntityException;
\r
1078 public String getParameterType(JavaParameter parameter) {
\r
1079 Type returnType = parameter.getType();
\r
1081 return getTypeGenericsName(returnType);
\r
1084 public String getPrimitiveObj(String type) {
\r
1085 if (type.equals("boolean")) {
\r
1088 else if (type.equals("double")) {
\r
1091 else if (type.equals("float")) {
\r
1094 else if (type.equals("int")) {
\r
1097 else if (type.equals("long")) {
\r
1100 else if (type.equals("short")) {
\r
1108 public String getPrimitiveObjValue(String colType) {
\r
1109 if (colType.equals("Boolean")) {
\r
1110 return ".booleanValue()";
\r
1112 else if (colType.equals("Double")) {
\r
1113 return ".doubleValue()";
\r
1115 else if (colType.equals("Float")) {
\r
1116 return ".floatValue()";
\r
1118 else if (colType.equals("Integer")) {
\r
1119 return ".intValue()";
\r
1121 else if (colType.equals("Long")) {
\r
1122 return ".longValue()";
\r
1124 else if (colType.equals("Short")) {
\r
1125 return ".shortValue()";
\r
1128 return StringPool.BLANK;
\r
1131 public String getReturnType(JavaMethod method) {
\r
1132 Type returnType = method.getReturns();
\r
1134 return getTypeGenericsName(returnType);
\r
1137 public List<String> getServiceBaseExceptions(
\r
1138 List<JavaMethod> methods, String methodName, List<String> args,
\r
1139 List<String> exceptions) {
\r
1141 boolean foundMethod = false;
\r
1143 for (JavaMethod method : methods) {
\r
1144 JavaParameter[] parameters = method.getParameters();
\r
1146 if (method.getName().equals(methodName) &&
\r
1147 (parameters.length == args.size())) {
\r
1149 for (int i = 0; i < parameters.length; i++) {
\r
1150 JavaParameter parameter = parameters[i];
\r
1152 String arg = args.get(i);
\r
1154 if (getParameterType(parameter).equals(arg)) {
\r
1155 exceptions = ListUtil.copy(exceptions);
\r
1157 Type[] methodExceptions = method.getExceptions();
\r
1159 for (Type methodException : methodExceptions) {
\r
1160 String exception = methodException.getValue();
\r
1162 if (exception.equals(
\r
1163 PortalException.class.getName())) {
\r
1165 exception = "PortalException";
\r
1168 if (exception.equals(
\r
1169 SystemException.class.getName())) {
\r
1171 exception = "SystemException";
\r
1174 if (!exceptions.contains(exception)) {
\r
1175 exceptions.add(exception);
\r
1179 Collections.sort(exceptions);
\r
1181 foundMethod = true;
\r
1188 if (foundMethod) {
\r
1193 if (!exceptions.isEmpty()) {
\r
1194 return exceptions;
\r
1197 return Collections.emptyList();
\r
1201 public String getSqlType(String type) {
\r
1202 if (type.equals("boolean") || type.equals("Boolean")) {
\r
1205 else if (type.equals("double") || type.equals("Double")) {
\r
1208 else if (type.equals("float") || type.equals("Float")) {
\r
1211 else if (type.equals("int") || type.equals("Integer")) {
\r
1214 else if (type.equals("long") || type.equals("Long")) {
\r
1217 else if (type.equals("short") || type.equals("Short")) {
\r
1220 else if (type.equals("Date")) {
\r
1221 return "TIMESTAMP";
\r
1228 public String getSqlType(String model, String field, String type) {
\r
1229 if (type.equals("boolean") || type.equals("Boolean")) {
\r
1232 else if (type.equals("double") || type.equals("Double")) {
\r
1235 else if (type.equals("float") || type.equals("Float")) {
\r
1238 else if (type.equals("int") || type.equals("Integer")) {
\r
1241 else if (type.equals("long") || type.equals("Long")) {
\r
1244 else if (type.equals("short") || type.equals("Short")) {
\r
1247 else if (type.equals("Blob")) {
\r
1250 else if (type.equals("Date")) {
\r
1251 return "TIMESTAMP";
\r
1253 else if (type.equals("String")) {
\r
1254 Map<String, String> hints = ModelHintsUtil.getHints(model, field);
\r
1256 if (hints != null) {
\r
1257 int maxLength = GetterUtil.getInteger(hints.get("max-length"));
\r
1259 if (maxLength == 2000000) {
\r
1271 public String getTypeGenericsName(Type type) {
\r
1272 StringBundler sb = new StringBundler();
\r
1274 sb.append(type.getValue());
\r
1276 Type[] actualTypeArguments = type.getActualTypeArguments();
\r
1278 if (actualTypeArguments != null) {
\r
1279 sb.append(StringPool.LESS_THAN);
\r
1281 for (int i = 0; i < actualTypeArguments.length; i++) {
\r
1286 sb.append(getTypeGenericsName(actualTypeArguments[i]));
\r
1289 sb.append(StringPool.GREATER_THAN);
\r
1292 sb.append(getDimensions(type.getDimensions()));
\r
1294 return sb.toString();
\r
1297 public String getVariableName(JavaField field) {
\r
1298 String fieldName = field.getName();
\r
1300 if ((fieldName.length() > 0) && (fieldName.charAt(0) == '_')) {
\r
1301 fieldName = fieldName.substring(1);
\r
1307 public boolean hasEntityByGenericsName(String genericsName) {
\r
1308 if (Validator.isNull(genericsName)) {
\r
1312 if (genericsName.indexOf(".model.") == -1) {
\r
1316 if (getEntityByGenericsName(genericsName) == null) {
\r
1324 public boolean hasEntityByParameterTypeValue(String parameterTypeValue) {
\r
1325 if (Validator.isNull(parameterTypeValue)) {
\r
1329 if (parameterTypeValue.indexOf(".model.") == -1) {
\r
1333 if (getEntityByParameterTypeValue(parameterTypeValue) == null) {
\r
1341 public boolean isBasePersistenceMethod(JavaMethod method) {
\r
1342 String methodName = method.getName();
\r
1344 if (methodName.equals("clearCache") ||
\r
1345 methodName.equals("findWithDynamicQuery")) {
\r
1349 else if (methodName.equals("findByPrimaryKey") ||
\r
1350 methodName.equals("fetchByPrimaryKey") ||
\r
1351 methodName.equals("remove")) {
\r
1353 JavaParameter[] parameters = method.getParameters();
\r
1355 if ((parameters.length == 1) &&
\r
1356 parameters[0].getName().equals("primaryKey")) {
\r
1361 if (methodName.equals("remove")) {
\r
1362 Type[] methodExceptions = method.getExceptions();
\r
1364 for (Type methodException : methodExceptions) {
\r
1365 String exception = methodException.getValue();
\r
1367 if (exception.contains("NoSuch")) {
\r
1379 public boolean isCustomMethod(JavaMethod method) {
\r
1380 String methodName = method.getName();
\r
1382 if (methodName.equals("afterPropertiesSet") ||
\r
1383 methodName.equals("destroy") ||
\r
1384 methodName.equals("equals") ||
\r
1385 methodName.equals("getClass") ||
\r
1386 methodName.equals("hashCode") ||
\r
1387 methodName.equals("notify") ||
\r
1388 methodName.equals("notifyAll") ||
\r
1389 methodName.equals("toString") ||
\r
1390 methodName.equals("wait")) {
\r
1394 else if (methodName.equals("getPermissionChecker")) {
\r
1397 else if (methodName.equals("getUser") &&
\r
1398 (method.getParameters().length == 0)) {
\r
1402 else if (methodName.equals("getUserId") &&
\r
1403 (method.getParameters().length == 0)) {
\r
1407 else if (methodName.endsWith("Finder") &&
\r
1408 (methodName.startsWith("get") ||
\r
1409 methodName.startsWith("set"))) {
\r
1413 else if (methodName.endsWith("Persistence") &&
\r
1414 (methodName.startsWith("get") ||
\r
1415 methodName.startsWith("set"))) {
\r
1419 else if (methodName.endsWith("Service") &&
\r
1420 (methodName.startsWith("get") ||
\r
1421 methodName.startsWith("set"))) {
\r
1430 public boolean isDuplicateMethod(
\r
1431 JavaMethod method, Map<String, Object> tempMap) {
\r
1433 StringBundler sb = new StringBundler();
\r
1435 sb.append("isDuplicateMethod ");
\r
1436 sb.append(getTypeGenericsName(method.getReturns()));
\r
1437 sb.append(StringPool.SPACE);
\r
1438 sb.append(method.getName());
\r
1439 sb.append(StringPool.OPEN_PARENTHESIS);
\r
1441 JavaParameter[] parameters = method.getParameters();
\r
1443 for (int i = 0; i < parameters.length; i++) {
\r
1444 JavaParameter javaParameter = parameters[i];
\r
1446 sb.append(getTypeGenericsName(javaParameter.getType()));
\r
1448 if ((i + 1) != parameters.length) {
\r
1449 sb.append(StringPool.COMMA);
\r
1453 sb.append(StringPool.CLOSE_PARENTHESIS);
\r
1455 String key = sb.toString();
\r
1457 if (tempMap.put(key, key) != null) {
\r
1465 public boolean isHBMCamelCasePropertyAccessor(String propertyName) {
\r
1466 if (propertyName.length() < 3) {
\r
1470 char[] chars = propertyName.toCharArray();
\r
1472 char c0 = chars[0];
\r
1473 char c1 = chars[1];
\r
1474 char c2 = chars[2];
\r
1476 if (Character.isLowerCase(c0) && Character.isUpperCase(c1) &&
\r
1477 Character.isLowerCase(c2)) {
\r
1485 public boolean isReadOnlyMethod(
\r
1486 JavaMethod method, List<String> txRequiredList, String[] prefixes) {
\r
1488 String methodName = method.getName();
\r
1490 if (isTxRequiredMethod(method, txRequiredList)) {
\r
1494 for (String prefix : prefixes) {
\r
1495 if (methodName.startsWith(prefix)) {
\r
1503 public boolean isServiceReadOnlyMethod(
\r
1504 JavaMethod method, List<String> txRequiredList) {
\r
1506 return isReadOnlyMethod(
\r
1507 method, txRequiredList,
\r
1508 PropsValues.SERVICE_BUILDER_SERVICE_READ_ONLY_PREFIXES);
\r
1511 public boolean isSoapMethod(JavaMethod method) {
\r
1512 Type returnType = method.getReturns();
\r
1514 String returnTypeGenericsName = getTypeGenericsName(returnType);
\r
1515 String returnValueName = returnType.getValue();
\r
1517 if (returnTypeGenericsName.contains(
\r
1518 "com.liferay.portal.kernel.search.") ||
\r
1519 returnTypeGenericsName.contains("com.liferay.portal.model.Theme") ||
\r
1520 returnTypeGenericsName.contains(
\r
1521 "com.liferay.portlet.social.model.SocialActivityDefinition") ||
\r
1522 returnTypeGenericsName.equals("java.util.List<java.lang.Object>") ||
\r
1523 returnValueName.equals("com.liferay.portal.model.Lock") ||
\r
1524 returnValueName.equals(
\r
1525 "com.liferay.portlet.messageboards.model.MBMessageDisplay") ||
\r
1526 returnValueName.startsWith("java.io") ||
\r
1527 returnValueName.equals("java.util.Map") ||
\r
1528 returnValueName.equals("java.util.Properties") ||
\r
1529 returnValueName.startsWith("javax")) {
\r
1534 if (returnTypeGenericsName.contains(
\r
1535 "com.liferay.portal.kernel.repository.model.FileEntry") ||
\r
1536 returnTypeGenericsName.contains(
\r
1537 "com.liferay.portal.kernel.repository.model.Folder")) {
\r
1539 else if (returnTypeGenericsName.contains(
\r
1540 "com.liferay.portal.kernel.repository.")) {
\r
1545 JavaParameter[] parameters = method.getParameters();
\r
1547 for (JavaParameter javaParameter : parameters) {
\r
1548 Type type = javaParameter.getType();
\r
1550 String parameterTypeName = type.getValue() + _getDimensions(type);
\r
1552 if (parameterTypeName.equals(
\r
1553 "com.liferay.portal.kernel.util.UnicodeProperties") ||
\r
1554 parameterTypeName.equals(
\r
1555 "com.liferay.portal.theme.ThemeDisplay") ||
\r
1556 parameterTypeName.equals(
\r
1557 "com.liferay.portlet.PortletPreferencesImpl") ||
\r
1558 parameterTypeName.equals(
\r
1559 "com.liferay.portlet.dynamicdatamapping.storage.Fields") ||
\r
1560 parameterTypeName.startsWith("java.io") ||
\r
1561 //parameterTypeName.startsWith("java.util.List") ||
\r
1562 //parameterTypeName.startsWith("java.util.Locale") ||
\r
1563 (parameterTypeName.startsWith("java.util.Map") &&
\r
1564 !_isStringLocaleMap(javaParameter)) ||
\r
1565 parameterTypeName.startsWith("java.util.Properties") ||
\r
1566 parameterTypeName.startsWith("javax")) {
\r
1575 public boolean isTxRequiredMethod(
\r
1576 JavaMethod method, List<String> txRequiredList) {
\r
1578 if (txRequiredList == null) {
\r
1582 String methodName = method.getName();
\r
1584 for (String txRequired : txRequiredList) {
\r
1585 if (methodName.equals(txRequired)) {
\r
1593 private static String _getPackagePath(File file) {
\r
1594 String fileName = StringUtil.replace(file.toString(), "\\", "/");
\r
1596 int x = fileName.indexOf("src/");
\r
1599 x = fileName.indexOf("test/");
\r
1602 int y = fileName.lastIndexOf("/");
\r
1604 fileName = fileName.substring(x + 4, y);
\r
1606 return StringUtil.replace(fileName, "/", ".");
\r
1609 private void _addElements(Element element, Map<String, Element> elements) {
\r
1610 for (Map.Entry<String, Element> entry : elements.entrySet()) {
\r
1611 Element childElement = entry.getValue();
\r
1613 element.add(childElement);
\r
1617 private void _createBlobModels(Entity entity) throws Exception {
\r
1618 List<EntityColumn> blobList = new ArrayList<EntityColumn>(
\r
1619 entity.getBlobList());
\r
1621 Iterator<EntityColumn> itr = blobList.iterator();
\r
1623 while (itr.hasNext()) {
\r
1624 EntityColumn col = itr.next();
\r
1626 if (!col.isLazy()) {
\r
1631 if (blobList.isEmpty()) {
\r
1635 Map<String, Object> context = _getContext();
\r
1637 context.put("entity", entity);
\r
1639 for (EntityColumn col : blobList) {
\r
1640 context.put("column", col);
\r
1644 String content = _processTemplate(_tplBlobModel, context);
\r
1648 File blobModelFile = new File(
\r
1649 _serviceOutputPath + "/model/" + entity.getName() +
\r
1650 col.getMethodName() + "BlobModel.java");
\r
1652 writeFile(blobModelFile, content, _author);
\r
1656 private void _createEJBPK(Entity entity) throws Exception {
\r
1657 Map<String, Object> context = _getContext();
\r
1659 context.put("entity", entity);
\r
1663 String content = _processTemplate(_tplEjbPk, context);
\r
1667 File ejbFile = new File(
\r
1668 _serviceOutputPath + "/service/persistence/" +
\r
1669 entity.getPKClassName() + ".java");
\r
1671 writeFile(ejbFile, content, _author);
\r
1674 private void _createExceptions(List<String> exceptions) throws Exception {
\r
1675 for (int i = 0; i < _ejbList.size(); i++) {
\r
1676 Entity entity = _ejbList.get(i);
\r
1678 if (!_isTargetEntity(entity)) {
\r
1682 if (entity.hasColumns()) {
\r
1683 exceptions.add(getNoSuchEntityException(entity));
\r
1687 for (String exception : exceptions) {
\r
1688 File exceptionFile = new File(
\r
1689 _serviceOutputPath + "/" + exception + "Exception.java");
\r
1691 if (!exceptionFile.exists()) {
\r
1692 Map<String, Object> context = _getContext();
\r
1694 context.put("exception", exception);
\r
1696 String content = _processTemplate(_tplException, context);
\r
1698 if (exception.startsWith("NoSuch")) {
\r
1699 content = StringUtil.replace(
\r
1700 content, "PortalException", "NoSuchModelException");
\r
1701 content = StringUtil.replace(
\r
1702 content, "kernel.exception.NoSuchModelException",
\r
1703 "NoSuchModelException");
\r
1706 content = StringUtil.replace(content, "\r\n", "\n");
\r
1708 FileUtil.write(exceptionFile, content);
\r
1711 if (exception.startsWith("NoSuch")) {
\r
1712 String content = FileUtil.read(exceptionFile);
\r
1714 if (!content.contains("NoSuchModelException")) {
\r
1715 content = StringUtil.replace(
\r
1716 content, "PortalException", "NoSuchModelException");
\r
1717 content = StringUtil.replace(
\r
1718 content, "kernel.exception.NoSuchModelException",
\r
1719 "NoSuchModelException");
\r
1721 FileUtil.write(exceptionFile, content);
\r
1725 if (!_serviceOutputPath.equals(_outputPath)) {
\r
1726 exceptionFile = new File(
\r
1727 _outputPath + "/" + exception + "Exception.java");
\r
1729 if (exceptionFile.exists()) {
\r
1730 System.out.println("Relocating " + exceptionFile);
\r
1732 exceptionFile.delete();
\r
1738 private void _createExtendedModel(Entity entity) throws Exception {
\r
1739 JavaClass javaClass = _getJavaClass(
\r
1740 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
\r
1742 Map<String, Object> context = _getContext();
\r
1744 context.put("entity", entity);
\r
1745 context.put("methods", _getMethods(javaClass));
\r
1749 String content = _processTemplate(_tplExtendedModel, context);
\r
1753 File modelFile = new File(
\r
1754 _serviceOutputPath + "/model/" + entity.getName() + ".java");
\r
1756 writeFile(modelFile, content, _author);
\r
1758 if (!_serviceOutputPath.equals(_outputPath)) {
\r
1759 modelFile = new File(
\r
1760 _outputPath + "/model/" + entity.getName() + ".java");
\r
1762 if (modelFile.exists()) {
\r
1763 System.out.println("Relocating " + modelFile);
\r
1765 modelFile.delete();
\r
1770 private void _createExtendedModelBaseImpl(Entity entity) throws Exception {
\r
1771 Map<String, Object> context = _getContext();
\r
1773 context.put("entity", entity);
\r
1777 String content = _processTemplate(_tplExtendedModelBaseImpl, context);
\r
1781 File modelFile = new File(
\r
1782 _outputPath + "/model/impl/" + entity.getName() + "BaseImpl.java");
\r
1784 writeFile(modelFile, content, _author);
\r
1787 private void _createExtendedModelImpl(Entity entity) throws Exception {
\r
1788 Map<String, Object> context = _getContext();
\r
1790 context.put("entity", entity);
\r
1794 String content = _processTemplate(_tplExtendedModelImpl, context);
\r
1798 File modelFile = new File(
\r
1799 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
\r
1801 if (modelFile.exists()) {
\r
1802 content = FileUtil.read(modelFile);
\r
1804 content = content.replaceAll(
\r
1805 "extends\\s+" + entity.getName() +
\r
1806 "ModelImpl\\s+implements\\s+" + entity.getName(),
\r
1807 "extends " + entity.getName() + "BaseImpl");
\r
1809 writeFileRaw(modelFile, content);
\r
1812 writeFile(modelFile, content, _author);
\r
1816 private void _createFinder(Entity entity) throws Exception {
\r
1817 if (!entity.hasFinderClass()) {
\r
1821 JavaClass javaClass = _getJavaClass(
\r
1822 _outputPath + "/service/persistence/" + entity.getName() +
\r
1823 "FinderImpl.java");
\r
1825 Map<String, Object> context = _getContext();
\r
1827 context.put("entity", entity);
\r
1828 context.put("methods", _getMethods(javaClass));
\r
1832 String content = _processTemplate(_tplFinder, context);
\r
1836 File ejbFile = new File(
\r
1837 _serviceOutputPath + "/service/persistence/" + entity.getName() +
\r
1840 writeFile(ejbFile, content, _author);
\r
1842 if (!_serviceOutputPath.equals(_outputPath)) {
\r
1843 ejbFile = new File(
\r
1844 _outputPath + "/service/persistence/" + entity.getName() +
\r
1847 if (ejbFile.exists()) {
\r
1848 System.out.println("Relocating " + ejbFile);
\r
1855 private void _createFinderUtil(Entity entity) throws Exception {
\r
1856 if (!entity.hasFinderClass()) {
\r
1860 JavaClass javaClass = _getJavaClass(
\r
1861 _outputPath + "/service/persistence/" + entity.getName() +
\r
1862 "FinderImpl.java");
\r
1864 Map<String, Object> context = _getContext();
\r
1866 context.put("entity", entity);
\r
1867 context.put("methods", _getMethods(javaClass));
\r
1871 String content = _processTemplate(_tplFinderUtil, context);
\r
1875 File ejbFile = new File(
\r
1876 _serviceOutputPath + "/service/persistence/" + entity.getName() +
\r
1877 "FinderUtil.java");
\r
1879 writeFile(ejbFile, content, _author);
\r
1881 if (!_serviceOutputPath.equals(_outputPath)) {
\r
1882 ejbFile = new File(
\r
1883 _outputPath + "/service/persistence/" + entity.getName() +
\r
1884 "FinderUtil.java");
\r
1886 if (ejbFile.exists()) {
\r
1887 System.out.println("Relocating " + ejbFile);
\r
1894 private void _createHbm(Entity entity) {
\r
1895 File ejbFile = new File(
\r
1896 _outputPath + "/service/persistence/" + entity.getName() +
\r
1899 if (ejbFile.exists()) {
\r
1900 System.out.println("Removing deprecated " + ejbFile);
\r
1906 private void _createHbmUtil(Entity entity) {
\r
1907 File ejbFile = new File(
\r
1908 _outputPath + "/service/persistence/" + entity.getName() +
\r
1911 if (ejbFile.exists()) {
\r
1912 System.out.println("Removing deprecated " + ejbFile);
\r
1918 private void _createHbmXml() throws Exception {
\r
1919 Map<String, Object> context = _getContext();
\r
1921 context.put("entities", _ejbList);
\r
1925 String content = _processTemplate(_tplHbmXml, context);
\r
1927 int lastImportStart = content.lastIndexOf("<import class=");
\r
1928 int lastImportEnd = content.indexOf("/>", lastImportStart) + 3;
\r
1930 String imports = content.substring(0, lastImportEnd);
\r
1932 content = content.substring(lastImportEnd + 1);
\r
1934 File xmlFile = new File(_hbmFileName);
\r
1936 if (!xmlFile.exists()) {
\r
1938 "<?xml version=\"1.0\"?>\n" +
\r
1939 "<!DOCTYPE hibernate-mapping PUBLIC \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n" +
\r
1941 "<hibernate-mapping default-lazy=\"false\" auto-import=\"false\">\n" +
\r
1942 "</hibernate-mapping>";
\r
1944 FileUtil.write(xmlFile, xml);
\r
1947 String oldContent = FileUtil.read(xmlFile);
\r
1948 String newContent = _fixHbmXml(oldContent);
\r
1950 int firstImport = newContent.indexOf(
\r
1951 "<import class=\"" + _packagePath + ".model.");
\r
1952 int lastImport = newContent.lastIndexOf(
\r
1953 "<import class=\"" + _packagePath + ".model.");
\r
1955 if (firstImport == -1) {
\r
1956 int x = newContent.indexOf("<class");
\r
1960 newContent.substring(0, x) + imports +
\r
1961 newContent.substring(x);
\r
1964 content = imports + content;
\r
1968 firstImport = newContent.indexOf("<import", firstImport) - 1;
\r
1969 lastImport = newContent.indexOf("/>", lastImport) + 3;
\r
1972 newContent.substring(0, firstImport) + imports +
\r
1973 newContent.substring(lastImport);
\r
1976 int firstClass = newContent.indexOf(
\r
1977 "<class name=\"" + _packagePath + ".model.impl.");
\r
1978 int lastClass = newContent.lastIndexOf(
\r
1979 "<class name=\"" + _packagePath + ".model.impl.");
\r
1981 if (firstClass == -1) {
\r
1982 int x = newContent.indexOf("</hibernate-mapping>");
\r
1986 newContent.substring(0, x) + content +
\r
1987 newContent.substring(x);
\r
1991 firstClass = newContent.lastIndexOf("<class", firstClass) - 1;
\r
1992 lastClass = newContent.indexOf("</class>", lastClass) + 9;
\r
1995 newContent.substring(0, firstClass) + content +
\r
1996 newContent.substring(lastClass);
\r
1999 newContent = _formatXml(newContent);
\r
2001 if (!oldContent.equals(newContent)) {
\r
2002 FileUtil.write(xmlFile, newContent);
\r
2006 private void _createJsonJs() throws Exception {
\r
2007 if (_packagePath.equals("com.liferay.counter")) {
\r
2011 if (Validator.isNotNull(_pluginName)) {
\r
2012 boolean hasRemoteService = false;
\r
2014 for (int i = 0; i < _ejbList.size(); i++) {
\r
2015 Entity entity = _ejbList.get(i);
\r
2017 if (entity.hasRemoteService()) {
\r
2018 hasRemoteService = true;
\r
2024 if (!hasRemoteService) {
\r
2029 StringBundler sb = new StringBundler();
\r
2031 if (_ejbList.size() > 0) {
\r
2032 sb.append(_processTemplate(_tplJsonJs));
\r
2035 for (int i = 0; i < _ejbList.size(); i++) {
\r
2036 Entity entity = _ejbList.get(i);
\r
2038 if (entity.hasRemoteService()) {
\r
2039 JavaClass javaClass = _getJavaClass(
\r
2040 _serviceOutputPath + "/service/" + entity.getName() +
\r
2043 JavaMethod[] methods = _getMethods(javaClass);
\r
2045 Set<String> jsonMethods = new LinkedHashSet<String>();
\r
2047 for (JavaMethod method : methods) {
\r
2048 String methodName = method.getName();
\r
2050 if (methodName.equals("getBeanIdentifier") ||
\r
2051 methodName.equals("invokeMethod") ||
\r
2052 methodName.equals("setBeanIdentifier")) {
\r
2057 String returnValue = getReturnType(method);
\r
2059 boolean badJsonType = false;
\r
2061 for (JavaParameter parameter : method.getParameters()) {
\r
2062 String parameterType = getParameterType(parameter);
\r
2064 if (_badJsonTypes.contains(parameterType)) {
\r
2065 badJsonType = true;
\r
2069 if (method.isPublic() &&
\r
2070 !_badJsonTypes.contains(returnValue) && !badJsonType) {
\r
2072 jsonMethods.add(methodName);
\r
2076 if (jsonMethods.size() > 0) {
\r
2077 Map<String, Object> context = _getContext();
\r
2079 context.put("entity", entity);
\r
2080 context.put("methods", jsonMethods);
\r
2082 sb.append("\n\n");
\r
2083 sb.append(_processTemplate(_tplJsonJsMethod, context));
\r
2088 File jsonFile = new File(_jsonFileName);
\r
2090 if (!jsonFile.exists()) {
\r
2091 FileUtil.write(jsonFile, "");
\r
2094 String oldContent = FileUtil.read(jsonFile);
\r
2095 String newContent = oldContent;
\r
2097 int oldBegin = oldContent.indexOf(
\r
2098 "Liferay.Service.register(\"Liferay.Service." + _portletShortName);
\r
2100 int oldEnd = oldContent.lastIndexOf(
\r
2101 "Liferay.Service." + _portletShortName);
\r
2103 oldEnd = oldContent.indexOf(");", oldEnd);
\r
2105 int newBegin = newContent.indexOf(
\r
2106 "Liferay.Service.register(\"Liferay.Service." + _portletShortName);
\r
2108 int newEnd = newContent.lastIndexOf(
\r
2109 "Liferay.Service." + _portletShortName);
\r
2111 newEnd = newContent.indexOf(");", newEnd);
\r
2113 if (newBegin == -1) {
\r
2114 newContent = oldContent + "\n\n" + sb.toString().trim();
\r
2118 newContent.substring(0, oldBegin) + sb.toString().trim() +
\r
2119 newContent.substring(oldEnd + 2);
\r
2122 newContent = newContent.trim();
\r
2124 if (!oldContent.equals(newContent)) {
\r
2125 FileUtil.write(jsonFile, newContent);
\r
2129 private void _createModel(Entity entity) throws Exception {
\r
2130 Map<String, Object> context = _getContext();
\r
2132 context.put("entity", entity);
\r
2136 String content = _processTemplate(_tplModel, context);
\r
2140 File modelFile = new File(
\r
2141 _serviceOutputPath + "/model/" + entity.getName() + "Model.java");
\r
2143 writeFile(modelFile, content, _author);
\r
2145 if (!_serviceOutputPath.equals(_outputPath)) {
\r
2146 modelFile = new File(
\r
2147 _outputPath + "/model/" + entity.getName() + "Model.java");
\r
2149 if (modelFile.exists()) {
\r
2150 System.out.println("Relocating " + modelFile);
\r
2152 modelFile.delete();
\r
2157 private void _createModelCache(Entity entity) throws Exception {
\r
2158 JavaClass javaClass = _getJavaClass(
\r
2159 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
\r
2161 Map<String, Object> context = _getContext();
\r
2163 context.put("entity", entity);
\r
2164 context.put("cacheFields", _getCacheFields(javaClass));
\r
2168 String content = _processTemplate(_tplModelCache, context);
\r
2172 File modelFile = new File(
\r
2173 _outputPath + "/model/impl/" + entity.getName() +
\r
2174 "CacheModel.java");
\r
2176 writeFile(modelFile, content, _author);
\r
2179 private void _createModelClp(Entity entity) throws Exception {
\r
2180 if (Validator.isNull(_pluginName)) {
\r
2184 JavaClass javaClass = _getJavaClass(
\r
2185 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
\r
2187 Map<String, JavaMethod> methods = new HashMap<String, JavaMethod>();
\r
2189 for (JavaMethod method : javaClass.getMethods()) {
\r
2190 methods.put(method.getDeclarationSignature(false), method);
\r
2193 Type superClass = javaClass.getSuperClass();
\r
2195 String superClassValue = superClass.getValue();
\r
2197 while (!superClassValue.endsWith("BaseModelImpl")) {
\r
2198 int pos = superClassValue.lastIndexOf(StringPool.PERIOD);
\r
2201 superClassValue = superClassValue.substring(pos + 1);
\r
2204 javaClass = _getJavaClass(
\r
2205 _outputPath + "/model/impl/" + superClassValue + ".java");
\r
2207 for (JavaMethod method : _getMethods(javaClass)) {
\r
2208 methods.remove(method.getDeclarationSignature(false));
\r
2211 superClass = javaClass.getSuperClass();
\r
2212 superClassValue = superClass.getValue();
\r
2215 Map<String, Object> context = _getContext();
\r
2217 context.put("entity", entity);
\r
2218 context.put("methods", methods.values());
\r
2222 String content = _processTemplate(_tplModelClp, context);
\r
2226 File modelFile = new File(
\r
2227 _serviceOutputPath + "/model/" + entity.getName() + "Clp.java");
\r
2229 writeFile(modelFile, content, _author);
\r
2232 private void _createModelHintsXml() throws Exception {
\r
2233 Map<String, Object> context = _getContext();
\r
2235 context.put("entities", _ejbList);
\r
2239 String content = _processTemplate(_tplModelHintsXml, context);
\r
2241 File xmlFile = new File(_modelHintsFileName);
\r
2243 if (!xmlFile.exists()) {
\r
2245 "<?xml version=\"1.0\"?>\n" +
\r
2247 "<model-hints>\n" +
\r
2250 FileUtil.write(xmlFile, xml);
\r
2253 String oldContent = FileUtil.read(xmlFile);
\r
2254 String newContent = oldContent;
\r
2256 int firstModel = newContent.indexOf(
\r
2257 "<model name=\"" + _packagePath + ".model.");
\r
2258 int lastModel = newContent.lastIndexOf(
\r
2259 "<model name=\"" + _packagePath + ".model.");
\r
2261 if (firstModel == -1) {
\r
2262 int x = newContent.indexOf("</model-hints>");
\r
2265 newContent.substring(0, x) + content +
\r
2266 newContent.substring(x);
\r
2269 firstModel = newContent.lastIndexOf("<model", firstModel) - 1;
\r
2270 lastModel = newContent.indexOf("</model>", lastModel) + 9;
\r
2273 newContent.substring(0, firstModel) + content +
\r
2274 newContent.substring(lastModel);
\r
2277 newContent = _formatXml(newContent);
\r
2279 if (!oldContent.equals(newContent)) {
\r
2280 FileUtil.write(xmlFile, newContent);
\r
2284 private void _createModelImpl(Entity entity) throws Exception {
\r
2285 JavaClass javaClass = _getJavaClass(
\r
2286 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
\r
2288 Map<String, Object> context = _getContext();
\r
2290 context.put("entity", entity);
\r
2291 context.put("cacheFields", _getCacheFields(javaClass));
\r
2295 String content = _processTemplate(_tplModelImpl, context);
\r
2299 File modelFile = new File(
\r
2300 _outputPath + "/model/impl/" + entity.getName() + "ModelImpl.java");
\r
2302 writeFile(modelFile, content, _author);
\r
2305 private void _createModelSoap(Entity entity) throws Exception {
\r
2306 File modelFile = null;
\r
2308 if (!_serviceOutputPath.equals(_outputPath)) {
\r
2309 modelFile = new File(
\r
2310 _outputPath + "/model/" + entity.getName() + "Soap.java");
\r
2312 if (modelFile.exists()) {
\r
2313 System.out.println("Relocating " + modelFile);
\r
2315 modelFile.delete();
\r
2319 modelFile = new File(
\r
2320 _serviceOutputPath + "/model/" + entity.getName() + "Soap.java");
\r
2322 Map<String, Object> context = _getContext();
\r
2324 context.put("entity", entity);
\r
2328 String content = _processTemplate(_tplModelSoap, context);
\r
2332 writeFile(modelFile, content, _author);
\r
2335 private void _createModelWrapper(Entity entity) throws Exception {
\r
2336 JavaClass modelJavaClass = _getJavaClass(
\r
2337 _serviceOutputPath + "/model/" + entity.getName() + "Model.java");
\r
2339 Object[] methods = _getMethods(modelJavaClass);
\r
2341 JavaClass extendedModelBaseImplJavaClass = _getJavaClass(
\r
2342 _outputPath + "/model/impl/" + entity.getName() + "BaseImpl.java");
\r
2344 methods = ArrayUtil.append(
\r
2345 methods, _getMethods(extendedModelBaseImplJavaClass));
\r
2347 JavaClass extendedModelJavaClass = _getJavaClass(
\r
2348 _serviceOutputPath + "/model/" + entity.getName() + ".java");
\r
2350 methods = ArrayUtil.append(
\r
2351 methods, _getMethods(extendedModelJavaClass));
\r
2353 Map<String, Object> context = _getContext();
\r
2355 context.put("entity", entity);
\r
2356 context.put("methods", methods);
\r
2360 String content = _processTemplate(_tplModelWrapper, context);
\r
2364 File modelFile = new File(
\r
2365 _serviceOutputPath + "/model/" + entity.getName() + "Wrapper.java");
\r
2367 writeFile(modelFile, content, _author);
\r
2370 private void _createOrmXml() throws Exception {
\r
2371 Map<String, Object> context = _getContext();
\r
2373 context.put("entities", _ejbList);
\r
2377 String content = _processTemplate(_tplOrmXml, context);
\r
2379 String mappedClasses = "";
\r
2381 int lastMappedClassStart = content.lastIndexOf("<mapped-superclass");
\r
2383 if (lastMappedClassStart != -1) {
\r
2384 int lastMappedClassEnd = content.indexOf(
\r
2385 "</mapped-superclass>", lastMappedClassStart) + 20;
\r
2387 mappedClasses = content.substring(0, lastMappedClassEnd);
\r
2389 content = content.substring(lastMappedClassEnd + 1);
\r
2392 File xmlFile = new File(_ormFileName);
\r
2394 if (!xmlFile.exists()) {
\r
2396 "<?xml version=\"1.0\"?>\n" +
\r
2397 "<entity-mappings version=\"1.0\" xmlns=\"http://java.sun.com/xml/ns/persistence/orm\"\n" +
\r
2398 "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
\r
2399 "\txsi:schemaLocation=\"http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd\"\n" +
\r
2401 "<persistence-unit-metadata>\n" +
\r
2402 "\t<xml-mapping-metadata-complete />\n" +
\r
2403 "\t<persistence-unit-defaults>\n" +
\r
2404 "\t\t<access>PROPERTY</access>\n" +
\r
2405 "\t</persistence-unit-defaults>\n" +
\r
2406 "</persistence-unit-metadata>\n" +
\r
2407 "</entity-mappings>";
\r
2409 FileUtil.write(xmlFile, xml);
\r
2412 String oldContent = FileUtil.read(xmlFile);
\r
2413 String newContent = oldContent;
\r
2415 int firstMappedClass = newContent.indexOf(
\r
2416 "<mapped-superclass class=\"" + _packagePath + ".model.");
\r
2417 int lastMappedClass = newContent.lastIndexOf(
\r
2418 "<mapped-superclass class=\"" + _packagePath + ".model.");
\r
2420 if (firstMappedClass == -1) {
\r
2421 int x = newContent.indexOf("<entity class=");
\r
2425 newContent.substring(0, x) + mappedClasses +
\r
2426 newContent.substring(x);
\r
2429 content = mappedClasses + content;
\r
2433 firstMappedClass = newContent.indexOf(
\r
2434 "<mapped-superclass", firstMappedClass) - 1;
\r
2435 lastMappedClass = newContent.indexOf(
\r
2436 "</mapped-superclass>", lastMappedClass) + 20;
\r
2439 newContent.substring(0, firstMappedClass) + mappedClasses +
\r
2440 newContent.substring(lastMappedClass);
\r
2443 int firstEntity = newContent.indexOf(
\r
2444 "<entity class=\"" + _packagePath + ".model.impl.");
\r
2445 int lastEntity = newContent.lastIndexOf(
\r
2446 "<entity class=\"" + _packagePath + ".model.impl.");
\r
2448 if (firstEntity == -1) {
\r
2449 int x = newContent.indexOf("</entity-mappings>");
\r
2453 newContent.substring(0, x) + content +
\r
2454 newContent.substring(x);
\r
2458 firstEntity = newContent.lastIndexOf("<entity", firstEntity) - 1;
\r
2459 lastEntity = newContent.indexOf("</entity>", lastEntity) + 9;
\r
2462 newContent.substring(0, firstEntity) + content +
\r
2463 newContent.substring(lastEntity);
\r
2466 newContent = _formatXml(newContent);
\r
2468 newContent = StringUtil.replace(
\r
2470 new String[] {"<attributes></attributes>", "<attributes/>"},
\r
2471 new String[] {"<attributes />", "<attributes />"});
\r
2473 if (!oldContent.equals(newContent)) {
\r
2474 FileUtil.write(xmlFile, newContent);
\r
2478 private void _createPersistence(Entity entity) throws Exception {
\r
2479 JavaClass javaClass = _getJavaClass(
\r
2480 _outputPath + "/service/persistence/" + entity.getName() +
\r
2481 "PersistenceImpl.java");
\r
2483 Map<String, Object> context = _getContext();
\r
2485 context.put("entity", entity);
\r
2486 context.put("methods", _getMethods(javaClass));
\r
2490 String content = _processTemplate(_tplPersistence, context);
\r
2494 File ejbFile = new File(
\r
2495 _serviceOutputPath + "/service/persistence/" + entity.getName() +
\r
2496 "Persistence.java");
\r
2498 writeFile(ejbFile, content, _author);
\r
2500 if (!_serviceOutputPath.equals(_outputPath)) {
\r
2501 ejbFile = new File(
\r
2502 _outputPath + "/service/persistence/" + entity.getName() +
\r
2503 "Persistence.java");
\r
2505 if (ejbFile.exists()) {
\r
2506 System.out.println("Relocating " + ejbFile);
\r
2513 private void _createPersistenceImpl(Entity entity) throws Exception {
\r
2514 Map<String, Object> context = _getContext();
\r
2516 context.put("entity", entity);
\r
2518 "referenceList", _mergeReferenceList(entity.getReferenceList()));
\r
2522 Logger.selectLoggerLibrary(Logger.LIBRARY_NONE);
\r
2524 String content = _processTemplate(_tplPersistenceImpl, context);
\r
2526 Logger.selectLoggerLibrary(Logger.LIBRARY_AUTO);
\r
2530 File ejbFile = new File(
\r
2531 _outputPath + "/service/persistence/" + entity.getName() +
\r
2532 "PersistenceImpl.java");
\r
2534 writeFile(ejbFile, content, _author);
\r
2537 private void _createPersistenceTest(Entity entity) throws Exception {
\r
2538 Map<String, Object> context = _getContext();
\r
2540 context.put("entity", entity);
\r
2544 String content = _processTemplate(_tplPersistenceTest, context);
\r
2548 File ejbFile = new File(
\r
2549 _testOutputPath + "/service/persistence/" + entity.getName() +
\r
2550 "PersistenceTest.java");
\r
2552 writeFile(ejbFile, content, _author);
\r
2555 private void _createPersistenceUtil(Entity entity) throws Exception {
\r
2556 JavaClass javaClass = _getJavaClass(
\r
2557 _outputPath + "/service/persistence/" + entity.getName() +
\r
2558 "PersistenceImpl.java");
\r
2560 Map<String, Object> context = _getContext();
\r
2562 context.put("entity", entity);
\r
2563 context.put("methods", _getMethods(javaClass));
\r
2567 String content = _processTemplate(_tplPersistenceUtil, context);
\r
2571 File ejbFile = new File(
\r
2572 _serviceOutputPath + "/service/persistence/" + entity.getName() +
\r
2575 writeFile(ejbFile, content, _author);
\r
2577 if (!_serviceOutputPath.equals(_outputPath)) {
\r
2578 ejbFile = new File(
\r
2579 _outputPath + "/service/persistence/" + entity.getName() +
\r
2582 if (ejbFile.exists()) {
\r
2583 System.out.println("Relocating " + ejbFile);
\r
2590 private void _createPool(Entity entity) {
\r
2591 File ejbFile = new File(
\r
2592 _outputPath + "/service/persistence/" + entity.getName() +
\r
2595 if (ejbFile.exists()) {
\r
2596 System.out.println("Removing deprecated " + ejbFile);
\r
2602 private void _createProps() throws Exception {
\r
2603 if (Validator.isNull(_pluginName)) {
\r
2609 File propsFile = new File(_implDir + "/service.properties");
\r
2611 long buildNumber = 1;
\r
2612 long buildDate = System.currentTimeMillis();
\r
2614 if (propsFile.exists()) {
\r
2615 Properties properties = PropertiesUtil.load(
\r
2616 FileUtil.read(propsFile));
\r
2618 if (!_buildNumberIncrement) {
\r
2619 buildDate = GetterUtil.getLong(
\r
2620 properties.getProperty("build.date"));
\r
2621 buildNumber = GetterUtil.getLong(
\r
2622 properties.getProperty("build.number"));
\r
2625 buildNumber = GetterUtil.getLong(
\r
2626 properties.getProperty("build.number")) + 1;
\r
2630 if (!_buildNumberIncrement && (buildNumber < _buildNumber)) {
\r
2631 buildNumber = _buildNumber;
\r
2632 buildDate = System.currentTimeMillis();
\r
2635 Map<String, Object> context = _getContext();
\r
2637 context.put("buildNumber", buildNumber);
\r
2638 context.put("currentTimeMillis", buildDate);
\r
2640 String content = _processTemplate(_tplProps, context);
\r
2644 FileUtil.write(propsFile, content, true);
\r
2646 // Add service-portlet.properties properties in service.properties
\r
2648 if(FileUtil.exists(_implDir + "/service-portlet.properties")){
\r
2649 String portletProps = FileUtil.read(_implDir + "/service-portlet.properties");
\r
2650 FileUtil.write(propsFile, portletProps, true, true);
\r
2654 private void _createRemotingXml() throws Exception {
\r
2655 StringBundler sb = new StringBundler();
\r
2657 Document document = SAXReaderUtil.read(new File(_springFileName));
\r
2659 Element rootElement = document.getRootElement();
\r
2661 List<Element> beanElements = rootElement.elements("bean");
\r
2663 for (Element beanElement : beanElements) {
\r
2664 String beanId = beanElement.attributeValue("id");
\r
2666 if (beanId.endsWith("Service") &&
\r
2667 !beanId.endsWith("LocalService")) {
\r
2669 String entityName = beanId;
\r
2671 entityName = StringUtil.replaceLast(
\r
2672 entityName, ".service.", ".");
\r
2674 int pos = entityName.lastIndexOf("Service");
\r
2676 entityName = entityName.substring(0, pos);
\r
2678 Entity entity = getEntity(entityName);
\r
2680 String serviceName = beanId;
\r
2682 String serviceMapping = serviceName;
\r
2684 serviceMapping = StringUtil.replaceLast(
\r
2685 serviceMapping, ".service.", ".service.spring.");
\r
2686 serviceMapping = StringUtil.replace(
\r
2687 serviceMapping, StringPool.PERIOD, StringPool.UNDERLINE);
\r
2689 Map<String, Object> context = _getContext();
\r
2691 context.put("entity", entity);
\r
2692 context.put("serviceName", serviceName);
\r
2693 context.put("serviceMapping", serviceMapping);
\r
2695 sb.append(_processTemplate(_tplRemotingXml, context));
\r
2699 File outputFile = new File(_remotingFileName);
\r
2701 if (!outputFile.exists()) {
\r
2705 String content = FileUtil.read(outputFile);
\r
2706 String newContent = content;
\r
2708 int x = content.indexOf("<bean ");
\r
2709 int y = content.lastIndexOf("</bean>") + 8;
\r
2713 content.substring(0, x - 1) + sb.toString() +
\r
2714 content.substring(y);
\r
2717 x = content.indexOf("</beans>");
\r
2721 content.substring(0, x) + sb.toString() +
\r
2722 content.substring(x);
\r
2725 x = content.indexOf("<beans/>");
\r
2729 content.substring(0, x) + "<beans>" + sb.toString() +
\r
2730 "</beans>" + content.substring(y);
\r
2734 newContent = _formatXml(newContent);
\r
2736 if (!content.equals(newContent)) {
\r
2737 FileUtil.write(outputFile, newContent);
\r
2739 System.out.println(outputFile.toString());
\r
2743 private void _createService(Entity entity, int sessionType)
\r
2744 throws Exception {
\r
2746 JavaClass javaClass = _getJavaClass(
\r
2747 _outputPath + "/service/impl/" + entity.getName() +
\r
2748 _getSessionTypeName(sessionType) + "ServiceImpl.java");
\r
2750 JavaMethod[] methods = _getMethods(javaClass);
\r
2752 Type superClass = javaClass.getSuperClass();
\r
2754 String superClassValue = superClass.getValue();
\r
2756 if (superClassValue.endsWith(
\r
2757 entity.getName() + _getSessionTypeName(sessionType) +
\r
2758 "ServiceBaseImpl")) {
\r
2760 JavaClass parentJavaClass = _getJavaClass(
\r
2761 _outputPath + "/service/base/" + entity.getName() +
\r
2762 _getSessionTypeName(sessionType) + "ServiceBaseImpl.java");
\r
2764 methods = ArrayUtil.append(
\r
2765 parentJavaClass.getMethods(), methods);
\r
2768 Map<String, Object> context = _getContext();
\r
2770 context.put("entity", entity);
\r
2771 context.put("methods", methods);
\r
2772 context.put("sessionTypeName", _getSessionTypeName(sessionType));
\r
2776 String content = _processTemplate(_tplService, context);
\r
2780 File ejbFile = new File(
\r
2781 _serviceOutputPath + "/service/" + entity.getName() +
\r
2782 _getSessionTypeName(sessionType) + "Service.java");
\r
2784 writeFile(ejbFile, content, _author);
\r
2786 if (!_serviceOutputPath.equals(_outputPath)) {
\r
2787 ejbFile = new File(
\r
2788 _outputPath + "/service/" + entity.getName() +
\r
2789 _getSessionTypeName(sessionType) + "Service.java");
\r
2791 if (ejbFile.exists()) {
\r
2792 System.out.println("Relocating " + ejbFile);
\r
2799 private void _createServiceBaseImpl(Entity entity, int sessionType)
\r
2800 throws Exception {
\r
2802 JavaClass javaClass = _getJavaClass(
\r
2803 _outputPath + "/service/impl/" + entity.getName() +
\r
2804 (sessionType != _SESSION_TYPE_REMOTE ? "Local" : "") +
\r
2805 "ServiceImpl.java");
\r
2807 JavaMethod[] methods = _getMethods(javaClass);
\r
2809 Map<String, Object> context = _getContext();
\r
2811 context.put("entity", entity);
\r
2812 context.put("methods", methods);
\r
2813 context.put("sessionTypeName",_getSessionTypeName(sessionType));
\r
2815 "referenceList", _mergeReferenceList(entity.getReferenceList()));
\r
2819 String content = _processTemplate(_tplServiceBaseImpl, context);
\r
2823 File ejbFile = new File(
\r
2824 _outputPath + "/service/base/" + entity.getName() +
\r
2825 _getSessionTypeName(sessionType) + "ServiceBaseImpl.java");
\r
2827 writeFile(ejbFile, content, _author);
\r
2830 private void _createServiceClp(Entity entity, int sessionType)
\r
2831 throws Exception {
\r
2833 if (Validator.isNull(_pluginName)) {
\r
2837 JavaClass javaClass = _getJavaClass(
\r
2838 _serviceOutputPath + "/service/" + entity.getName() +
\r
2839 _getSessionTypeName(sessionType) + "Service.java");
\r
2841 Map<String, Object> context = _getContext();
\r
2843 context.put("entity", entity);
\r
2844 context.put("methods", _getMethods(javaClass));
\r
2845 context.put("sessionTypeName", _getSessionTypeName(sessionType));
\r
2849 String content = _processTemplate(_tplServiceClp, context);
\r
2853 File ejbFile = new File(
\r
2854 _serviceOutputPath + "/service/" + entity.getName() +
\r
2855 _getSessionTypeName(sessionType) + "ServiceClp.java");
\r
2857 writeFile(ejbFile, content, _author);
\r
2860 private void _createServiceClpInvoker(Entity entity, int sessionType)
\r
2861 throws Exception {
\r
2863 if (Validator.isNull(_pluginName)) {
\r
2867 JavaClass javaClass = _getJavaClass(
\r
2868 _outputPath + "/service/impl/" + entity.getName() +
\r
2869 _getSessionTypeName(sessionType) + "ServiceImpl.java");
\r
2871 JavaMethod[] methods = _getMethods(javaClass);
\r
2873 Type superClass = javaClass.getSuperClass();
\r
2875 String superClassValue = superClass.getValue();
\r
2877 if (superClassValue.endsWith(
\r
2878 entity.getName() + _getSessionTypeName(sessionType) +
\r
2879 "ServiceBaseImpl")) {
\r
2881 JavaClass parentJavaClass = _getJavaClass(
\r
2882 _outputPath + "/service/base/" + entity.getName() +
\r
2883 _getSessionTypeName(sessionType) + "ServiceBaseImpl.java");
\r
2885 methods = ArrayUtil.append(
\r
2886 parentJavaClass.getMethods(), methods);
\r
2889 Map<String, Object> context = _getContext();
\r
2891 context.put("entity", entity);
\r
2892 context.put("methods", methods);
\r
2893 context.put("sessionTypeName", _getSessionTypeName(sessionType));
\r
2897 String content = _processTemplate(_tplServiceClpInvoker, context);
\r
2901 File ejbFile = new File(
\r
2902 _outputPath + "/service/base/" + entity.getName() +
\r
2903 _getSessionTypeName(sessionType) + "ServiceClpInvoker.java");
\r
2905 writeFile(ejbFile, content);
\r
2908 private void _createServiceClpMessageListener() throws Exception {
\r
2909 if (Validator.isNull(_pluginName)) {
\r
2913 Map<String, Object> context = _getContext();
\r
2915 context.put("entities", _ejbList);
\r
2919 String content = _processTemplate(
\r
2920 _tplServiceClpMessageListener, context);
\r
2924 File ejbFile = new File(
\r
2925 _serviceOutputPath + "/service/messaging/ClpMessageListener.java");
\r
2927 writeFile(ejbFile, content);
\r
2930 private void _createServiceClpSerializer(List<String> exceptions)
\r
2931 throws Exception {
\r
2933 if (Validator.isNull(_pluginName)) {
\r
2937 Map<String, Object> context = _getContext();
\r
2939 context.put("entities", _ejbList);
\r
2940 context.put("exceptions", exceptions);
\r
2944 String content = _processTemplate(_tplServiceClpSerializer, context);
\r
2948 File ejbFile = new File(
\r
2949 _serviceOutputPath + "/service/ClpSerializer.java");
\r
2951 writeFile(ejbFile, content);
\r
2954 private void _createServiceFactory(Entity entity, int sessionType)
\r
2955 throws Exception {
\r
2957 File ejbFile = new File(
\r
2958 _serviceOutputPath + "/service/" + entity.getName() +
\r
2959 _getSessionTypeName(sessionType) + "ServiceFactory.java");
\r
2961 if (ejbFile.exists()) {
\r
2962 System.out.println("Removing deprecated " + ejbFile);
\r
2967 ejbFile = new File(
\r
2968 _outputPath + "/service/" + entity.getName() +
\r
2969 _getSessionTypeName(sessionType) + "ServiceFactory.java");
\r
2971 if (ejbFile.exists()) {
\r
2972 System.out.println("Removing deprecated " + ejbFile);
\r
2978 private void _createServiceHttp(Entity entity) throws Exception {
\r
2979 JavaClass javaClass = _getJavaClass(
\r
2980 _outputPath + "/service/impl/" + entity.getName() +
\r
2981 "ServiceImpl.java");
\r
2983 Map<String, Object> context = _getContext();
\r
2985 context.put("entity", entity);
\r
2986 context.put("methods", _getMethods(javaClass));
\r
2987 context.put("hasHttpMethods", new Boolean(_hasHttpMethods(javaClass)));
\r
2991 String content = _processTemplate(_tplServiceHttp, context);
\r
2995 File ejbFile = new File(
\r
2996 _outputPath + "/service/http/" + entity.getName() +
\r
2997 "ServiceHttp.java");
\r
2999 writeFile(ejbFile, content, _author);
\r
3002 private void _createServiceImpl(Entity entity, int sessionType)
\r
3003 throws Exception {
\r
3005 Map<String, Object> context = _getContext();
\r
3007 context.put("entity", entity);
\r
3008 context.put("sessionTypeName", _getSessionTypeName(sessionType));
\r
3012 String content = _processTemplate(_tplServiceImpl, context);
\r
3016 File ejbFile = new File(
\r
3017 _outputPath + "/service/impl/" + entity.getName() +
\r
3018 _getSessionTypeName(sessionType) + "ServiceImpl.java");
\r
3020 if (!ejbFile.exists()) {
\r
3021 writeFile(ejbFile, content, _author);
\r
3025 private void _createServiceJson(Entity entity) throws Exception {
\r
3026 File ejbFile = new File(
\r
3027 _outputPath + "/service/http/" + entity.getName() +
\r
3028 "ServiceJSON.java");
\r
3030 if (ejbFile.exists()) {
\r
3031 System.out.println("Removing deprecated " + ejbFile);
\r
3037 private void _createServiceJsonSerializer(Entity entity) throws Exception {
\r
3038 File ejbFile = new File(
\r
3039 _serviceOutputPath + "/service/http/" + entity.getName() +
\r
3040 "JSONSerializer.java");
\r
3042 if (ejbFile.exists()) {
\r
3043 System.out.println("Removing deprecated " + ejbFile);
\r
3048 if (!_serviceOutputPath.equals(_outputPath)) {
\r
3049 ejbFile = new File(
\r
3050 _outputPath + "/service/http/" + entity.getName() +
\r
3051 "JSONSerializer.java");
\r
3053 if (ejbFile.exists()) {
\r
3054 System.out.println("Removing deprecated " + ejbFile);
\r
3061 private void _createServiceSoap(Entity entity) throws Exception {
\r
3062 JavaClass javaClass = _getJavaClass(
\r
3063 _outputPath + "/service/impl/" + entity.getName() +
\r
3064 "ServiceImpl.java");
\r
3066 Map<String, Object> context = _getContext();
\r
3068 context.put("entity", entity);
\r
3069 context.put("methods", _getMethods(javaClass));
\r
3073 String content = _processTemplate(_tplServiceSoap, context);
\r
3077 File ejbFile = new File(
\r
3078 _outputPath + "/service/http/" + entity.getName() +
\r
3079 "ServiceSoap.java");
\r
3081 writeFile(ejbFile, content, _author);
\r
3084 private void _createServiceUtil(Entity entity, int sessionType)
\r
3085 throws Exception {
\r
3087 JavaClass javaClass = _getJavaClass(
\r
3088 _serviceOutputPath + "/service/" + entity.getName() +
\r
3089 _getSessionTypeName(sessionType) + "Service.java");
\r
3091 Map<String, Object> context = _getContext();
\r
3093 context.put("entity", entity);
\r
3094 context.put("methods", _getMethods(javaClass));
\r
3095 context.put("sessionTypeName", _getSessionTypeName(sessionType));
\r
3099 String content = _processTemplate(_tplServiceUtil, context);
\r
3103 File ejbFile = new File(
\r
3104 _serviceOutputPath + "/service/" + entity.getName() +
\r
3105 _getSessionTypeName(sessionType) + "ServiceUtil.java");
\r
3107 writeFile(ejbFile, content, _author);
\r
3109 if (!_serviceOutputPath.equals(_outputPath)) {
\r
3110 ejbFile = new File(
\r
3111 _outputPath + "/service/" + entity.getName() +
\r
3112 _getSessionTypeName(sessionType) + "ServiceUtil.java");
\r
3114 if (ejbFile.exists()) {
\r
3115 System.out.println("Relocating " + ejbFile);
\r
3122 private void _createServiceWrapper(Entity entity, int sessionType)
\r
3123 throws Exception {
\r
3125 JavaClass javaClass = _getJavaClass(
\r
3126 _serviceOutputPath + "/service/" + entity.getName() +
\r
3127 _getSessionTypeName(sessionType) + "Service.java");
\r
3129 Map<String, Object> context = _getContext();
\r
3131 context.put("entity", entity);
\r
3132 context.put("methods", _getMethods(javaClass));
\r
3133 context.put("sessionTypeName", _getSessionTypeName(sessionType));
\r
3137 String content = _processTemplate(_tplServiceWrapper, context);
\r
3141 File ejbFile = new File(
\r
3142 _serviceOutputPath + "/service/" + entity.getName() +
\r
3143 _getSessionTypeName(sessionType) + "ServiceWrapper.java");
\r
3145 writeFile(ejbFile, content, _author);
\r
3148 private void _createSpringBaseXml() throws Exception {
\r
3149 if (Validator.isNull(_springBaseFileName)) {
\r
3155 String content = _processTemplate(_tplSpringBaseXml);
\r
3159 File ejbFile = new File(_springBaseFileName);
\r
3161 FileUtil.write(ejbFile, content, true);
\r
3163 if (Validator.isNotNull(_pluginName)) {
\r
3165 "docroot/WEB-INF/src/META-INF/data-source-spring.xml");
\r
3166 FileUtil.delete("docroot/WEB-INF/src/META-INF/misc-spring.xml");
\r
3170 private void _createSpringClusterXml() throws Exception {
\r
3171 if (Validator.isNull(_springClusterFileName)) {
\r
3177 String content = _processTemplate(_tplSpringClusterXml);
\r
3181 File ejbFile = new File(_springClusterFileName);
\r
3183 FileUtil.write(ejbFile, content, true);
\r
3186 private void _createSpringDynamicDataSourceXml() throws Exception {
\r
3187 if (Validator.isNull(_springDynamicDataSourceFileName)) {
\r
3193 String content = _processTemplate(_tplSpringDynamicDataSourceXml);
\r
3197 File ejbFile = new File(_springDynamicDataSourceFileName);
\r
3199 FileUtil.write(ejbFile, content, true);
\r
3202 private void _createSpringHibernateXml() throws Exception {
\r
3203 if (Validator.isNull(_springHibernateFileName)) {
\r
3209 String content = _processTemplate(_tplSpringHibernateXml);
\r
3213 File ejbFile = new File(_springHibernateFileName);
\r
3215 FileUtil.write(ejbFile, content, true);
\r
3218 private void _createSpringInfrastructureXml() throws Exception {
\r
3219 if (Validator.isNull(_springInfrastructureFileName)) {
\r
3225 String content = _processTemplate(_tplSpringInfrastructureXml);
\r
3229 File ejbFile = new File(_springInfrastructureFileName);
\r
3231 FileUtil.write(ejbFile, content, true);
\r
3234 private void _createSpringShardDataSourceXml() throws Exception {
\r
3235 if (Validator.isNull(_springShardDataSourceFileName)) {
\r
3241 String content = _processTemplate(_tplSpringShardDataSourceXml);
\r
3245 File ejbFile = new File(_springShardDataSourceFileName);
\r
3247 FileUtil.write(ejbFile, content, true);
\r
3250 private void _createSpringXml() throws Exception {
\r
3251 if (_packagePath.equals("com.liferay.counter")) {
\r
3255 Map<String, Object> context = _getContext();
\r
3257 context.put("entities", _ejbList);
\r
3261 String content = _processTemplate(_tplSpringXml, context);
\r
3263 File xmlFile = new File(_springFileName);
\r
3266 "<?xml version=\"1.0\"?>\n" +
\r
3269 "\tdefault-destroy-method=\"destroy\"\n" +
\r
3270 "\tdefault-init-method=\"afterPropertiesSet\"\n" +
\r
3271 "\txmlns=\"http://www.springframework.org/schema/beans\"\n" +
\r
3272 "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
\r
3273 "\txsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\"\n" +
\r
3277 if (!xmlFile.exists()) {
\r
3278 FileUtil.write(xmlFile, xml);
\r
3281 String oldContent = FileUtil.read(xmlFile);
\r
3283 if (Validator.isNotNull(_pluginName) &&
\r
3284 oldContent.contains("DOCTYPE beans PUBLIC")) {
\r
3289 String newContent = _fixSpringXml(oldContent);
\r
3291 int x = oldContent.indexOf("<beans");
\r
3292 int y = oldContent.lastIndexOf("</beans>");
\r
3294 int firstSession = newContent.indexOf(
\r
3295 "<bean id=\"" + _packagePath + ".service.", x);
\r
3297 int lastSession = newContent.lastIndexOf(
\r
3298 "<bean id=\"" + _packagePath + ".service.", y);
\r
3300 if ((firstSession == -1) || (firstSession > y)) {
\r
3301 x = newContent.indexOf("</beans>");
\r
3304 newContent.substring(0, x) + content + newContent.substring(x);
\r
3307 firstSession = newContent.lastIndexOf("<bean", firstSession) - 1;
\r
3309 int tempLastSession = newContent.indexOf(
\r
3310 "<bean id=\"", lastSession + 1);
\r
3312 if (tempLastSession == -1) {
\r
3313 tempLastSession = newContent.indexOf("</beans>", lastSession);
\r
3316 lastSession = tempLastSession;
\r
3319 newContent.substring(0, firstSession) + content +
\r
3320 newContent.substring(lastSession);
\r
3323 newContent = _formatXml(newContent);
\r
3325 if (!oldContent.equals(newContent)) {
\r
3326 FileUtil.write(xmlFile, newContent);
\r
3330 private void _createSpringClearService() throws Exception {
\r
3331 if (Validator.isNull(_pluginName)) {
\r
3335 Map<String, Object> context = _getContext();
\r
3337 context.put("entities", _ejbList);
\r
3341 String content = _processTemplate(
\r
3342 _tplSpringClearService, context);
\r
3346 File ejbFile = new File(
\r
3347 _outputPath + "/hook/events/SpringClearServiceAction.java");
\r
3349 writeFile(ejbFile, content);
\r
3352 private void _createSQLIndexes() throws IOException {
\r
3353 if (!FileUtil.exists(_sqlDir)) {
\r
3359 File sqlFile = new File(_sqlDir + "/" + _sqlIndexesFileName);
\r
3361 if (!sqlFile.exists()) {
\r
3362 FileUtil.write(sqlFile, "");
\r
3365 Map<String, String> indexSQLs = new TreeMap<String, String>();
\r
3367 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
\r
3368 new FileReader(sqlFile));
\r
3371 String indexSQL = unsyncBufferedReader.readLine();
\r
3373 if (indexSQL == null) {
\r
3377 if (Validator.isNotNull(indexSQL.trim())) {
\r
3378 int pos = indexSQL.indexOf(" on ");
\r
3380 String indexSpec = indexSQL.substring(pos + 4);
\r
3382 indexSQLs.put(indexSpec, indexSQL);
\r
3386 unsyncBufferedReader.close();
\r
3388 // indexes.properties
\r
3390 File propsFile = new File(
\r
3391 _sqlDir + "/" + _sqlIndexesPropertiesFileName);
\r
3393 if (!propsFile.exists()) {
\r
3394 FileUtil.write(propsFile, "");
\r
3397 Map<String, String> indexProps = new TreeMap<String, String>();
\r
3399 unsyncBufferedReader = new UnsyncBufferedReader(
\r
3400 new FileReader(propsFile));
\r
3403 String indexMapping = unsyncBufferedReader.readLine();
\r
3405 if (indexMapping == null) {
\r
3409 if (Validator.isNotNull(indexMapping.trim())) {
\r
3410 String[] splitIndexMapping = indexMapping.split("\\=");
\r
3412 indexProps.put(splitIndexMapping[1], splitIndexMapping[0]);
\r
3416 unsyncBufferedReader.close();
\r
3420 for (int i = 0; i < _ejbList.size(); i++) {
\r
3421 Entity entity = _ejbList.get(i);
\r
3423 if (!_isTargetEntity(entity)) {
\r
3427 if (!entity.isDefaultDataSource()) {
\r
3431 List<EntityFinder> finderList = entity.getFinderList();
\r
3433 for (int j = 0; j < finderList.size(); j++) {
\r
3434 EntityFinder finder = finderList.get(j);
\r
3436 if (finder.isDBIndex()) {
\r
3437 StringBundler sb = new StringBundler();
\r
3439 sb.append(entity.getTable() + " (");
\r
3441 List<EntityColumn> finderColsList = finder.getColumns();
\r
3443 for (int k = 0; k < finderColsList.size(); k++) {
\r
3444 EntityColumn col = finderColsList.get(k);
\r
3446 sb.append(col.getDBName());
\r
3448 if ((k + 1) != finderColsList.size()) {
\r
3455 String indexSpec = sb.toString();
\r
3457 String indexHash = StringUtil.toHexString(
\r
3458 indexSpec.hashCode()).toUpperCase();
\r
3460 String indexName = "IX_" + indexHash;
\r
3464 sb.append("create ");
\r
3466 if (finder.isUnique()) {
\r
3467 sb.append("unique ");
\r
3470 sb.append("index " + indexName + " on ");
\r
3471 sb.append(indexSpec);
\r
3473 indexSQLs.put(indexSpec, sb.toString());
\r
3475 String finderName =
\r
3476 entity.getTable() + StringPool.PERIOD +
\r
3479 indexProps.put(finderName, indexName);
\r
3484 for (Map.Entry<String, EntityMapping> entry :
\r
3485 _entityMappings.entrySet()) {
\r
3487 EntityMapping entityMapping = entry.getValue();
\r
3489 _getCreateMappingTableIndex(entityMapping, indexSQLs, indexProps);
\r
3492 StringBundler sb = new StringBundler();
\r
3494 Iterator<String> itr = indexSQLs.values().iterator();
\r
3496 String prevEntityName = null;
\r
3498 while (itr.hasNext()) {
\r
3499 String indexSQL = itr.next();
\r
3501 int pos = indexSQL.indexOf(" on ");
\r
3503 String indexSQLSuffix = indexSQL.substring(pos + 4);
\r
3505 String entityName = indexSQLSuffix.split(" ")[0];
\r
3507 if ((prevEntityName != null) &&
\r
3508 !prevEntityName.equals(entityName)) {
\r
3513 sb.append(indexSQL);
\r
3515 if (itr.hasNext()) {
\r
3519 prevEntityName = entityName;
\r
3522 FileUtil.write(sqlFile, sb.toString(), true);
\r
3524 // indexes.properties
\r
3528 itr = indexProps.keySet().iterator();
\r
3530 prevEntityName = null;
\r
3532 while (itr.hasNext()) {
\r
3533 String finderName = itr.next();
\r
3535 String indexName = indexProps.get(finderName);
\r
3537 String entityName = finderName.split("\\.")[0];
\r
3539 if ((prevEntityName != null) &&
\r
3540 !prevEntityName.equals(entityName)) {
\r
3545 sb.append(indexName + StringPool.EQUAL + finderName);
\r
3547 if (itr.hasNext()) {
\r
3551 prevEntityName = entityName;
\r
3554 FileUtil.write(propsFile, sb.toString(), true);
\r
3557 private void _createSQLMappingTables(
\r
3558 File sqlFile, String newCreateTableString,
\r
3559 EntityMapping entityMapping, boolean addMissingTables)
\r
3560 throws IOException {
\r
3562 if (!sqlFile.exists()) {
\r
3563 FileUtil.write(sqlFile, StringPool.BLANK);
\r
3566 String content = FileUtil.read(sqlFile);
\r
3568 int x = content.indexOf(
\r
3569 _SQL_CREATE_TABLE + entityMapping.getTable() + " (");
\r
3570 int y = content.indexOf(");", x);
\r
3573 String oldCreateTableString = content.substring(x + 1, y);
\r
3575 if (!oldCreateTableString.equals(newCreateTableString)) {
\r
3577 content.substring(0, x) + newCreateTableString +
\r
3578 content.substring(y + 2);
\r
3580 FileUtil.write(sqlFile, content);
\r
3583 else if (addMissingTables) {
\r
3584 StringBundler sb = new StringBundler();
\r
3586 UnsyncBufferedReader unsyncBufferedReader =
\r
3587 new UnsyncBufferedReader(new UnsyncStringReader(content));
\r
3589 String line = null;
\r
3590 boolean appendNewTable = true;
\r
3592 while ((line = unsyncBufferedReader.readLine()) != null) {
\r
3593 if (appendNewTable && line.startsWith(_SQL_CREATE_TABLE)) {
\r
3594 x = _SQL_CREATE_TABLE.length();
\r
3595 y = line.indexOf(" ", x);
\r
3597 String tableName = line.substring(x, y);
\r
3599 if (tableName.compareTo(entityMapping.getTable()) > 0) {
\r
3600 sb.append(newCreateTableString + "\n\n");
\r
3602 appendNewTable = false;
\r
3610 if (appendNewTable) {
\r
3611 sb.append("\n" + newCreateTableString);
\r
3614 unsyncBufferedReader.close();
\r
3616 FileUtil.write(sqlFile, sb.toString(), true);
\r
3620 private void _createSQLSequences() throws IOException {
\r
3621 if (!FileUtil.exists(_sqlDir)) {
\r
3625 File sqlFile = new File(_sqlDir + "/" + _sqlSequencesFileName);
\r
3627 if (!sqlFile.exists()) {
\r
3628 FileUtil.write(sqlFile, "");
\r
3631 Set<String> sequenceSQLs = new TreeSet<String>();
\r
3633 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
\r
3634 new FileReader(sqlFile));
\r
3637 String sequenceSQL = unsyncBufferedReader.readLine();
\r
3639 if (sequenceSQL == null) {
\r
3643 if (Validator.isNotNull(sequenceSQL)) {
\r
3644 sequenceSQLs.add(sequenceSQL);
\r
3648 unsyncBufferedReader.close();
\r
3650 for (int i = 0; i < _ejbList.size(); i++) {
\r
3651 Entity entity = _ejbList.get(i);
\r
3653 if (!_isTargetEntity(entity)) {
\r
3657 if (!entity.isDefaultDataSource()) {
\r
3661 List<EntityColumn> columnList = entity.getColumnList();
\r
3663 for (int j = 0; j < columnList.size(); j++) {
\r
3664 EntityColumn column = columnList.get(j);
\r
3666 if ("sequence".equals(column.getIdType())) {
\r
3667 StringBundler sb = new StringBundler();
\r
3669 String sequenceName = column.getIdParam();
\r
3671 if (sequenceName.length() > 30) {
\r
3672 sequenceName = sequenceName.substring(0, 30);
\r
3675 sb.append("create sequence " + sequenceName + ";");
\r
3677 String sequenceSQL = sb.toString();
\r
3679 if (!sequenceSQLs.contains(sequenceSQL)) {
\r
3680 sequenceSQLs.add(sequenceSQL);
\r
3686 StringBundler sb = new StringBundler();
\r
3688 Iterator<String> itr = sequenceSQLs.iterator();
\r
3690 while (itr.hasNext()) {
\r
3691 String sequenceSQL = itr.next();
\r
3693 sb.append(sequenceSQL);
\r
3695 if (itr.hasNext()) {
\r
3700 FileUtil.write(sqlFile, sb.toString(), true);
\r
3703 private void _createSQLTables() throws IOException {
\r
3704 if (!FileUtil.exists(_sqlDir)) {
\r
3708 File sqlFile = new File(_sqlDir + "/" + _sqlFileName);
\r
3710 if (!sqlFile.exists()) {
\r
3711 FileUtil.write(sqlFile, StringPool.BLANK);
\r
3714 for (int i = 0; i < _ejbList.size(); i++) {
\r
3715 Entity entity = _ejbList.get(i);
\r
3717 if (!_isTargetEntity(entity)) {
\r
3721 if (!entity.isDefaultDataSource()) {
\r
3725 String createTableSQL = _getCreateTableSQL(entity);
\r
3727 if (Validator.isNotNull(createTableSQL)) {
\r
3728 _createSQLTables(sqlFile, createTableSQL, entity, true);
\r
3731 "update-6.1.0-6.1.1.sql", createTableSQL, entity);
\r
3735 for (Map.Entry<String, EntityMapping> entry :
\r
3736 _entityMappings.entrySet()) {
\r
3738 EntityMapping entityMapping = entry.getValue();
\r
3740 String createMappingTableSQL = _getCreateMappingTableSQL(
\r
3743 if (Validator.isNotNull(createMappingTableSQL)) {
\r
3744 _createSQLMappingTables(
\r
3745 sqlFile, createMappingTableSQL, entityMapping, true);
\r
3749 String content = FileUtil.read(sqlFile);
\r
3751 FileUtil.write(sqlFile, content.trim());
\r
3754 private void _createSQLTables(
\r
3755 File sqlFile, String newCreateTableString, Entity entity,
\r
3756 boolean addMissingTables)
\r
3757 throws IOException {
\r
3759 if (!sqlFile.exists()) {
\r
3760 FileUtil.write(sqlFile, StringPool.BLANK);
\r
3763 String content = FileUtil.read(sqlFile);
\r
3765 int x = content.indexOf(_SQL_CREATE_TABLE + entity.getTable() + " (");
\r
3766 int y = content.indexOf(");", x);
\r
3769 String oldCreateTableString = content.substring(x + 1, y);
\r
3771 if (!oldCreateTableString.equals(newCreateTableString)) {
\r
3773 content.substring(0, x) + newCreateTableString +
\r
3774 content.substring(y + 2);
\r
3776 FileUtil.write(sqlFile, content);
\r
3779 else if (addMissingTables) {
\r
3780 StringBundler sb = new StringBundler();
\r
3782 UnsyncBufferedReader unsyncBufferedReader =
\r
3783 new UnsyncBufferedReader(new UnsyncStringReader(content));
\r
3785 String line = null;
\r
3786 boolean appendNewTable = true;
\r
3788 while ((line = unsyncBufferedReader.readLine()) != null) {
\r
3789 if (appendNewTable && line.startsWith(_SQL_CREATE_TABLE)) {
\r
3790 x = _SQL_CREATE_TABLE.length();
\r
3791 y = line.indexOf(" ", x);
\r
3793 String tableName = line.substring(x, y);
\r
3795 if (tableName.compareTo(entity.getTable()) > 0) {
\r
3796 sb.append(newCreateTableString + "\n\n");
\r
3798 appendNewTable = false;
\r
3806 if (appendNewTable) {
\r
3807 sb.append("\n" + newCreateTableString);
\r
3810 unsyncBufferedReader.close();
\r
3812 FileUtil.write(sqlFile, sb.toString(), true);
\r
3816 private String _fixHbmXml(String content) throws IOException {
\r
3817 StringBundler sb = new StringBundler();
\r
3819 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
\r
3820 new UnsyncStringReader(content));
\r
3822 String line = null;
\r
3824 while ((line = unsyncBufferedReader.readLine()) != null) {
\r
3825 if (line.startsWith("\t<class name=\"")) {
\r
3826 line = StringUtil.replace(
\r
3829 ".service.persistence.", "HBM\" table=\""
\r
3832 ".model.", "\" table=\""
\r
3835 if (!line.contains(".model.impl.") &&
\r
3836 !line.contains("BlobModel")) {
\r
3838 line = StringUtil.replace(
\r
3841 ".model.", "\" table=\""
\r
3844 ".model.impl.", "Impl\" table=\""
\r
3853 unsyncBufferedReader.close();
\r
3855 return sb.toString().trim();
\r
3858 private String _fixSpringXml(String content) {
\r
3859 return StringUtil.replace(content, ".service.spring.", ".service.");
\r
3862 private String _formatComment(
\r
3863 String comment, DocletTag[] tags, String indentation) {
\r
3865 StringBundler sb = new StringBundler();
\r
3867 if (Validator.isNull(comment) && (tags.length <= 0)) {
\r
3868 return sb.toString();
\r
3871 sb.append(indentation);
\r
3872 sb.append("/**\n");
\r
3874 if (Validator.isNotNull(comment)) {
\r
3875 comment = comment.replaceAll("(?m)^", indentation + " * ");
\r
3877 sb.append(comment);
\r
3880 if (tags.length > 0) {
\r
3881 sb.append(indentation);
\r
3882 sb.append(" *\n");
\r
3886 for (DocletTag tag : tags) {
\r
3887 sb.append(indentation);
\r
3888 sb.append(" * @");
\r
3889 sb.append(tag.getName());
\r
3891 sb.append(tag.getValue());
\r
3895 sb.append(indentation);
\r
3896 sb.append(" */\n");
\r
3898 return sb.toString();
\r
3901 private String _formatXml(String xml)
\r
3902 throws DocumentException, IOException {
\r
3904 String doctype = null;
\r
3906 int x = xml.indexOf("<!DOCTYPE");
\r
3909 int y = xml.indexOf(">", x) + 1;
\r
3911 doctype = xml.substring(x, y);
\r
3913 xml = xml.substring(0, x) + "\n" + xml.substring(y);
\r
3916 xml = StringUtil.replace(xml, '\r', "");
\r
3917 xml = XMLFormatter.toString(xml);
\r
3918 xml = StringUtil.replace(xml, "\"/>", "\" />");
\r
3920 if (Validator.isNotNull(doctype)) {
\r
3921 x = xml.indexOf("?>") + 2;
\r
3923 xml = xml.substring(0, x) + "\n" + doctype + xml.substring(x);
\r
3929 private JavaField[] _getCacheFields(JavaClass javaClass) {
\r
3930 if (javaClass == null) {
\r
3931 return new JavaField[0];
\r
3934 List<JavaField> javaFields = new ArrayList<JavaField>();
\r
3936 for (JavaField javaField : javaClass.getFields()) {
\r
3937 Annotation[] annotations = javaField.getAnnotations();
\r
3939 for (Annotation annotation : annotations) {
\r
3940 Type type = annotation.getType();
\r
3942 String className = type.getFullyQualifiedName();
\r
3944 if (className.equals(CacheField.class.getName())) {
\r
3945 javaFields.add(javaField);
\r
3952 return javaFields.toArray(new JavaField[javaFields.size()]);
\r
3955 private String _getContent(String fileName) throws Exception {
\r
3956 Document document = _getContentDocument(fileName);
\r
3958 Element rootElement = document.getRootElement();
\r
3960 Element authorElement = null;
\r
3961 Element namespaceElement = null;
\r
3962 Map<String, Element> entityElements = new TreeMap<String, Element>();
\r
3963 Map<String, Element> exceptionElements = new TreeMap<String, Element>();
\r
3965 for (Element element : rootElement.elements()) {
\r
3966 String elementName = element.getName();
\r
3968 if (elementName.equals("author")) {
\r
3971 if (authorElement != null) {
\r
3972 throw new IllegalArgumentException(
\r
3973 "There can only be one author element");
\r
3976 authorElement = element;
\r
3978 else if (elementName.equals("namespace")) {
\r
3981 if (namespaceElement != null) {
\r
3982 throw new IllegalArgumentException(
\r
3983 "There can only be one namespace element");
\r
3986 namespaceElement = element;
\r
3988 else if (elementName.equals("entity")) {
\r
3991 String name = element.attributeValue("name");
\r
3993 entityElements.put(name.toLowerCase(), element);
\r
3995 else if (elementName.equals("exceptions")) {
\r
3998 for (Element exceptionElement : element.elements("exception")) {
\r
3999 exceptionElement.detach();
\r
4001 exceptionElements.put(
\r
4002 exceptionElement.getText(), exceptionElement);
\r
4007 if (authorElement != null) {
\r
4008 rootElement.add(authorElement);
\r
4011 if (namespaceElement == null) {
\r
4012 throw new IllegalArgumentException(
\r
4013 "The namespace element is required");
\r
4016 rootElement.add(namespaceElement);
\r
4019 _addElements(rootElement, entityElements);
\r
4021 if (!exceptionElements.isEmpty()) {
\r
4022 Element exceptionsElement = rootElement.addElement("exceptions");
\r
4024 _addElements(exceptionsElement, exceptionElements);
\r
4027 return document.asXML();
\r
4030 private Document _getContentDocument(String fileName) throws Exception {
\r
4031 String content = FileUtil.read(new File(fileName));
\r
4033 Document document = SAXReaderUtil.read(content);
\r
4035 Element rootElement = document.getRootElement();
\r
4037 for (Element element : rootElement.elements()) {
\r
4038 String elementName = element.getName();
\r
4040 if (!elementName.equals("service-builder-import")) {
\r
4046 String dirName = fileName.substring(
\r
4047 0, fileName.lastIndexOf(StringPool.SLASH) + 1);
\r
4048 String serviceBuilderImportFileName = element.attributeValue(
\r
4051 Document serviceBuilderImportDocument = _getContentDocument(
\r
4052 dirName + serviceBuilderImportFileName);
\r
4054 Element serviceBuilderImportRootElement =
\r
4055 serviceBuilderImportDocument.getRootElement();
\r
4057 for (Element serviceBuilderImportElement :
\r
4058 serviceBuilderImportRootElement.elements()) {
\r
4060 serviceBuilderImportElement.detach();
\r
4062 rootElement.add(serviceBuilderImportElement);
\r
4069 private Map<String, Object> _getContext() throws TemplateModelException {
\r
4070 BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
\r
4072 TemplateHashModel staticModels = wrapper.getStaticModels();
\r
4074 Map<String, Object> context = new HashMap<String, Object>();
\r
4076 context.put("hbmFileName", _hbmFileName);
\r
4077 context.put("ormFileName", _ormFileName);
\r
4078 context.put("modelHintsFileName", _modelHintsFileName);
\r
4079 context.put("springFileName", _springFileName);
\r
4080 context.put("springBaseFileName", _springBaseFileName);
\r
4081 context.put("springHibernateFileName", _springHibernateFileName);
\r
4083 "springInfrastructureFileName", _springInfrastructureFileName);
\r
4084 context.put("apiDir", _apiDir);
\r
4085 context.put("implDir", _implDir);
\r
4086 context.put("jsonFileName", _jsonFileName);
\r
4087 context.put("sqlDir", _sqlDir);
\r
4088 context.put("sqlFileName", _sqlFileName);
\r
4089 context.put("beanLocatorUtil", _beanLocatorUtil);
\r
4090 context.put("beanLocatorUtilShortName", _beanLocatorUtilShortName);
\r
4091 context.put("propsUtil", _propsUtil);
\r
4092 context.put("portletName", _portletName);
\r
4093 context.put("portletShortName", _portletShortName);
\r
4094 context.put("portletPackageName", _portletPackageName);
\r
4095 context.put("outputPath", _outputPath);
\r
4096 context.put("serviceOutputPath", _serviceOutputPath);
\r
4097 context.put("packagePath", _packagePath);
\r
4098 context.put("pluginName", _pluginName);
\r
4099 context.put("author", _author);
\r
4100 context.put("serviceBuilder", this);
\r
4102 context.put("arrayUtil", ArrayUtil_IW.getInstance());
\r
4103 context.put("modelHintsUtil", ModelHintsUtil.getModelHints());
\r
4105 "resourceActionsUtil", ResourceActionsUtil.getResourceActions());
\r
4106 context.put("stringUtil", StringUtil_IW.getInstance());
\r
4107 context.put("system", staticModels.get("java.lang.System"));
\r
4108 context.put("tempMap", wrapper.wrap(new HashMap<String, Object>()));
\r
4110 "textFormatter", staticModels.get(TextFormatter.class.getName()));
\r
4111 context.put("validator", Validator_IW.getInstance());
\r
4116 private void _getCreateMappingTableIndex(
\r
4117 EntityMapping entityMapping, Map<String, String> indexSQLs,
\r
4118 Map<String, String> indexProps)
\r
4119 throws IOException {
\r
4121 Entity[] entities = new Entity[2];
\r
4123 for (int i = 0; i < entities.length; i++) {
\r
4124 entities[i] = getEntity(entityMapping.getEntity(i));
\r
4126 if (entities[i] == null) {
\r
4131 for (Entity entity : entities) {
\r
4132 List<EntityColumn> pkList = entity.getPKList();
\r
4134 for (int j = 0; j < pkList.size(); j++) {
\r
4135 EntityColumn col = pkList.get(j);
\r
4137 String colDBName = col.getDBName();
\r
4139 String indexSpec =
\r
4140 entityMapping.getTable() + " (" + colDBName + ");";
\r
4142 String indexHash = StringUtil.toHexString(
\r
4143 indexSpec.hashCode()).toUpperCase();
\r
4145 String indexName = "IX_" + indexHash;
\r
4147 StringBundler sb = new StringBundler();
\r
4149 sb.append("create index ");
\r
4150 sb.append(indexName);
\r
4151 sb.append(" on ");
\r
4152 sb.append(indexSpec);
\r
4154 indexSQLs.put(indexSpec, sb.toString());
\r
4156 String finderName =
\r
4157 entityMapping.getTable() + StringPool.PERIOD + colDBName;
\r
4159 indexProps.put(finderName, indexName);
\r
4164 private String _getCreateMappingTableSQL(EntityMapping entityMapping)
\r
4165 throws IOException {
\r
4167 Entity[] entities = new Entity[2];
\r
4169 for (int i = 0; i < entities.length; i++) {
\r
4170 entities[i] = getEntity(entityMapping.getEntity(i));
\r
4172 if (entities[i] == null) {
\r
4177 StringBundler sb = new StringBundler();
\r
4179 sb.append(_SQL_CREATE_TABLE);
\r
4180 sb.append(entityMapping.getTable());
\r
4181 sb.append(" (\n");
\r
4183 for (Entity entity : entities) {
\r
4184 List<EntityColumn> pkList = entity.getPKList();
\r
4186 for (int i = 0; i < pkList.size(); i++) {
\r
4187 EntityColumn col = pkList.get(i);
\r
4189 String colName = col.getName();
\r
4190 String colType = col.getType();
\r
4192 sb.append("\t" + col.getDBName());
\r
4195 if (colType.equalsIgnoreCase("boolean")) {
\r
4196 sb.append("BOOLEAN");
\r
4198 else if (colType.equalsIgnoreCase("double") ||
\r
4199 colType.equalsIgnoreCase("float")) {
\r
4201 sb.append("DOUBLE");
\r
4203 else if (colType.equals("int") ||
\r
4204 colType.equals("Integer") ||
\r
4205 colType.equalsIgnoreCase("short")) {
\r
4207 sb.append("INTEGER");
\r
4209 else if (colType.equalsIgnoreCase("long")) {
\r
4210 sb.append("LONG");
\r
4212 else if (colType.equals("String")) {
\r
4213 Map<String, String> hints = ModelHintsUtil.getHints(
\r
4214 _packagePath + ".model." + entity.getName(), colName);
\r
4216 int maxLength = 75;
\r
4218 if (hints != null) {
\r
4219 maxLength = GetterUtil.getInteger(
\r
4220 hints.get("max-length"), maxLength);
\r
4223 if (col.isLocalized()) {
\r
4227 if (maxLength < 4000) {
\r
4228 sb.append("VARCHAR(" + maxLength + ")");
\r
4230 else if (maxLength == 4000) {
\r
4231 sb.append("STRING");
\r
4233 else if (maxLength > 4000) {
\r
4234 sb.append("TEXT");
\r
4237 else if (colType.equals("Date")) {
\r
4238 sb.append("DATE");
\r
4241 sb.append("invalid");
\r
4244 if (col.isPrimary()) {
\r
4245 sb.append(" not null");
\r
4247 else if (colType.equals("Date") || colType.equals("String")) {
\r
4248 sb.append(" null");
\r
4255 sb.append("\tprimary key (");
\r
4257 for (int i = 0; i < entities.length; i++) {
\r
4258 Entity entity = entities[i];
\r
4260 List<EntityColumn> pkList = entity.getPKList();
\r
4262 for (int j = 0; j < pkList.size(); j++) {
\r
4263 EntityColumn col = pkList.get(j);
\r
4265 String colDBName = col.getDBName();
\r
4267 if ((i != 0) || (j != 0)) {
\r
4271 sb.append(colDBName);
\r
4278 return sb.toString();
\r
4281 private String _getCreateTableSQL(Entity entity) {
\r
4282 List<EntityColumn> pkList = entity.getPKList();
\r
4283 List<EntityColumn> regularColList = entity.getRegularColList();
\r
4285 if (regularColList.size() == 0) {
\r
4289 StringBundler sb = new StringBundler();
\r
4291 sb.append(_SQL_CREATE_TABLE);
\r
4292 sb.append(entity.getTable());
\r
4293 sb.append(" (\n");
\r
4295 for (int i = 0; i < regularColList.size(); i++) {
\r
4296 EntityColumn col = regularColList.get(i);
\r
4298 String colName = col.getName();
\r
4299 String colType = col.getType();
\r
4300 String colIdType = col.getIdType();
\r
4302 sb.append("\t" + col.getDBName());
\r
4305 if (colType.equalsIgnoreCase("boolean")) {
\r
4306 sb.append("BOOLEAN");
\r
4308 else if (colType.equalsIgnoreCase("double") ||
\r
4309 colType.equalsIgnoreCase("float")) {
\r
4311 sb.append("DOUBLE");
\r
4313 else if (colType.equals("int") ||
\r
4314 colType.equals("Integer") ||
\r
4315 colType.equalsIgnoreCase("short")) {
\r
4317 sb.append("INTEGER");
\r
4319 else if (colType.equalsIgnoreCase("long")) {
\r
4320 sb.append("LONG");
\r
4322 else if (colType.equals("Blob")) {
\r
4323 sb.append("BLOB");
\r
4325 else if (colType.equals("Date")) {
\r
4326 sb.append("DATE");
\r
4328 else if (colType.equals("String")) {
\r
4329 Map<String, String> hints = ModelHintsUtil.getHints(
\r
4330 _packagePath + ".model." + entity.getName(), colName);
\r
4332 int maxLength = 75;
\r
4334 if (hints != null) {
\r
4335 maxLength = GetterUtil.getInteger(
\r
4336 hints.get("max-length"), maxLength);
\r
4339 if (col.isLocalized()) {
\r
4343 if (maxLength < 4000) {
\r
4344 sb.append("VARCHAR(" + maxLength + ")");
\r
4346 else if (maxLength == 4000) {
\r
4347 sb.append("STRING");
\r
4349 else if (maxLength > 4000) {
\r
4350 sb.append("TEXT");
\r
4354 sb.append("invalid");
\r
4357 if (col.isPrimary()) {
\r
4358 sb.append(" not null");
\r
4360 if (!entity.hasCompoundPK()) {
\r
4361 sb.append(" primary key");
\r
4364 else if (colType.equals("Date") || colType.equals("String")) {
\r
4365 sb.append(" null");
\r
4368 if (Validator.isNotNull(colIdType) &&
\r
4369 colIdType.equals("identity")) {
\r
4371 sb.append(" IDENTITY");
\r
4374 if (((i + 1) != regularColList.size()) ||
\r
4375 entity.hasCompoundPK()) {
\r
4383 if (entity.hasCompoundPK()) {
\r
4384 sb.append("\tprimary key (");
\r
4386 for (int j = 0; j < pkList.size(); j++) {
\r
4387 EntityColumn pk = pkList.get(j);
\r
4389 sb.append(pk.getDBName());
\r
4391 if ((j + 1) != pkList.size()) {
\r
4401 return sb.toString();
\r
4404 private String _getDimensions(Type type) {
\r
4405 String dimensions = "";
\r
4407 for (int i = 0; i < type.getDimensions(); i++) {
\r
4408 dimensions += "[]";
\r
4411 return dimensions;
\r
4414 private JavaClass _getJavaClass(String fileName) throws IOException {
\r
4415 int pos = fileName.indexOf(_implDir + "/");
\r
4418 pos += _implDir.length();
\r
4421 pos = fileName.indexOf(_apiDir + "/") + _apiDir.length();
\r
4424 String srcFile = fileName.substring(pos + 1);
\r
4425 String className = StringUtil.replace(
\r
4426 srcFile.substring(0, srcFile.length() - 5), "/", ".");
\r
4428 JavaClass javaClass = _javaClasses.get(className);
\r
4430 if (javaClass == null) {
\r
4431 ClassLibrary classLibrary = new ClassLibrary();
\r
4433 classLibrary.addClassLoader(getClass().getClassLoader());
\r
4435 JavaDocBuilder builder = new JavaDocBuilder(classLibrary);
\r
4437 File file = new File(fileName);
\r
4439 if (!file.exists()) {
\r
4443 builder.addSource(file);
\r
4445 javaClass = builder.getClassByName(className);
\r
4447 _javaClasses.put(className, javaClass);
\r
4453 private JavaMethod[] _getMethods(JavaClass javaClass) {
\r
4454 return _getMethods(javaClass, false);
\r
4457 private JavaMethod[] _getMethods(
\r
4458 JavaClass javaClass, boolean superclasses) {
\r
4460 JavaMethod[] methods = javaClass.getMethods(superclasses);
\r
4462 for (JavaMethod method : methods) {
\r
4463 Arrays.sort(method.getExceptions());
\r
4469 private String _getSessionTypeName(int sessionType) {
\r
4470 if (sessionType == _SESSION_TYPE_LOCAL) {
\r
4478 private String _getTplProperty(String key, String defaultValue) {
\r
4479 return System.getProperty("service.tpl." + key, defaultValue);
\r
4482 private List<String> _getTransients(Entity entity, boolean parent)
\r
4483 throws Exception {
\r
4485 File modelFile = null;
\r
4488 modelFile = new File(
\r
4489 _outputPath + "/model/impl/" + entity.getName() +
\r
4490 "ModelImpl.java");
\r
4493 modelFile = new File(
\r
4494 _outputPath + "/model/impl/" + entity.getName() + "Impl.java");
\r
4497 String content = FileUtil.read(modelFile);
\r
4499 Matcher matcher = _getterPattern.matcher(content);
\r
4501 Set<String> getters = new HashSet<String>();
\r
4503 while (!matcher.hitEnd()) {
\r
4504 boolean found = matcher.find();
\r
4507 String property = matcher.group();
\r
4509 if (property.indexOf("get") != -1) {
\r
4510 property = property.substring(
\r
4511 property.indexOf("get") + 3, property.length() - 1);
\r
4514 property = property.substring(
\r
4515 property.indexOf("is") + 2, property.length() - 1);
\r
4518 if (!entity.hasColumn(property) &&
\r
4519 !entity.hasColumn(Introspector.decapitalize(property))) {
\r
4521 property = Introspector.decapitalize(property);
\r
4523 getters.add(property);
\r
4528 matcher = _setterPattern.matcher(content);
\r
4530 Set<String> setters = new HashSet<String>();
\r
4532 while (!matcher.hitEnd()) {
\r
4533 boolean found = matcher.find();
\r
4536 String property = matcher.group();
\r
4538 property = property.substring(
\r
4539 property.indexOf("set") + 3, property.length() - 1);
\r
4541 if (!entity.hasColumn(property) &&
\r
4542 !entity.hasColumn(Introspector.decapitalize(property))) {
\r
4544 property = Introspector.decapitalize(property);
\r
4546 setters.add(property);
\r
4551 getters.retainAll(setters);
\r
4553 List<String> transients = new ArrayList<String>(getters);
\r
4555 Collections.sort(transients);
\r
4557 return transients;
\r
4560 private boolean _hasHttpMethods(JavaClass javaClass) {
\r
4561 JavaMethod[] methods = _getMethods(javaClass);
\r
4563 for (JavaMethod javaMethod : methods) {
\r
4564 if (!javaMethod.isConstructor() && javaMethod.isPublic() &&
\r
4565 isCustomMethod(javaMethod)) {
\r
4574 private boolean _isStringLocaleMap(JavaParameter javaParameter) {
\r
4575 Type type = javaParameter.getType();
\r
4577 Type[] actualArgumentTypes = type.getActualTypeArguments();
\r
4579 if (actualArgumentTypes.length != 2) {
\r
4583 if (!_isTypeValue(actualArgumentTypes[0], Locale.class.getName()) ||
\r
4584 !_isTypeValue(actualArgumentTypes[1], String.class.getName())) {
\r
4592 private boolean _isTargetEntity(Entity entity) {
\r
4593 if ((_targetEntityName == null) || _targetEntityName.startsWith("$")) {
\r
4597 return _targetEntityName.equals(entity.getName());
\r
4600 private boolean _isTypeValue(Type type, String value) {
\r
4601 return value.equals(type.getValue());
\r
4604 private List<Entity> _mergeReferenceList(List<Entity> referenceList) {
\r
4605 List<Entity> list = new ArrayList<Entity>(
\r
4606 _ejbList.size() + referenceList.size());
\r
4608 list.addAll(_ejbList);
\r
4609 list.addAll(referenceList);
\r
4614 private void _parseEntity(Element entityElement) throws Exception {
\r
4615 String ejbName = entityElement.attributeValue("name");
\r
4616 String humanName = entityElement.attributeValue("human-name");
\r
4618 String table = entityElement.attributeValue("table");
\r
4620 if (Validator.isNull(table)) {
\r
4623 if (_badTableNames.contains(ejbName)) {
\r
4624 table += StringPool.UNDERLINE;
\r
4627 if (_autoNamespaceTables) {
\r
4628 table = _portletShortName + StringPool.UNDERLINE + ejbName;
\r
4632 boolean uuid = GetterUtil.getBoolean(
\r
4633 entityElement.attributeValue("uuid"));
\r
4634 boolean uuidAccessor = GetterUtil.getBoolean(
\r
4635 entityElement.attributeValue("uuid-accessor"));
\r
4636 boolean localService = GetterUtil.getBoolean(
\r
4637 entityElement.attributeValue("local-service"));
\r
4638 boolean remoteService = GetterUtil.getBoolean(
\r
4639 entityElement.attributeValue("remote-service"), true);
\r
4640 String persistenceClass = GetterUtil.getString(
\r
4641 entityElement.attributeValue("persistence-class"),
\r
4642 _packagePath + ".service.persistence." + ejbName +
\r
4643 "PersistenceImpl");
\r
4645 String finderClass = "";
\r
4647 if (FileUtil.exists(
\r
4648 _outputPath + "/service/persistence/" + ejbName +
\r
4649 "FinderImpl.java")) {
\r
4652 _packagePath + ".service.persistence." + ejbName + "FinderImpl";
\r
4655 String dataSource = entityElement.attributeValue("data-source");
\r
4656 String sessionFactory = entityElement.attributeValue("session-factory");
\r
4657 String txManager = entityElement.attributeValue("tx-manager");
\r
4658 boolean cacheEnabled = GetterUtil.getBoolean(
\r
4659 entityElement.attributeValue("cache-enabled"), true);
\r
4660 boolean jsonEnabled = GetterUtil.getBoolean(
\r
4661 entityElement.attributeValue("json-enabled"), remoteService);
\r
4663 List<EntityColumn> pkList = new ArrayList<EntityColumn>();
\r
4664 List<EntityColumn> regularColList = new ArrayList<EntityColumn>();
\r
4665 List<EntityColumn> blobList = new ArrayList<EntityColumn>();
\r
4666 List<EntityColumn> collectionList = new ArrayList<EntityColumn>();
\r
4667 List<EntityColumn> columnList = new ArrayList<EntityColumn>();
\r
4669 List<Element> columnElements = entityElement.elements("column");
\r
4671 boolean permissionedModel = false;
\r
4674 Element columnElement = SAXReaderUtil.createElement("column");
\r
4676 columnElement.addAttribute("name", "uuid");
\r
4677 columnElement.addAttribute("type", "String");
\r
4679 columnElements.add(0, columnElement);
\r
4682 for (Element columnElement : columnElements) {
\r
4683 String columnName = columnElement.attributeValue("name");
\r
4685 if (columnName.equals("resourceBlockId") &&
\r
4686 !ejbName.equals("ResourceBlock")) {
\r
4688 permissionedModel = true;
\r
4691 String columnDBName = columnElement.attributeValue("db-name");
\r
4693 if (Validator.isNull(columnDBName)) {
\r
4694 columnDBName = columnName;
\r
4696 if (_badColumnNames.contains(columnName)) {
\r
4697 columnDBName += StringPool.UNDERLINE;
\r
4701 String columnType = columnElement.attributeValue("type");
\r
4702 boolean primary = GetterUtil.getBoolean(
\r
4703 columnElement.attributeValue("primary"));
\r
4704 boolean accessor = GetterUtil.getBoolean(
\r
4705 columnElement.attributeValue("accessor"));
\r
4706 boolean filterPrimary = GetterUtil.getBoolean(
\r
4707 columnElement.attributeValue("filter-primary"));
\r
4708 String collectionEntity = columnElement.attributeValue("entity");
\r
4709 String mappingKey = columnElement.attributeValue("mapping-key");
\r
4711 String mappingTable = columnElement.attributeValue("mapping-table");
\r
4713 if (Validator.isNotNull(mappingTable)) {
\r
4714 if (_badTableNames.contains(mappingTable)) {
\r
4715 mappingTable += StringPool.UNDERLINE;
\r
4718 if (_autoNamespaceTables) {
\r
4720 _portletShortName + StringPool.UNDERLINE + mappingTable;
\r
4724 String idType = columnElement.attributeValue("id-type");
\r
4725 String idParam = columnElement.attributeValue("id-param");
\r
4726 boolean convertNull = GetterUtil.getBoolean(
\r
4727 columnElement.attributeValue("convert-null"), true);
\r
4728 boolean lazy = GetterUtil.getBoolean(
\r
4729 columnElement.attributeValue("lazy"), true);
\r
4730 boolean localized = GetterUtil.getBoolean(
\r
4731 columnElement.attributeValue("localized"));
\r
4732 boolean colJsonEnabled = GetterUtil.getBoolean(
\r
4733 columnElement.attributeValue("json-enabled"), jsonEnabled);
\r
4735 EntityColumn col = new EntityColumn(
\r
4736 columnName, columnDBName, columnType, primary, accessor,
\r
4737 filterPrimary, collectionEntity, mappingKey, mappingTable,
\r
4738 idType, idParam, convertNull, lazy, localized, colJsonEnabled);
\r
4744 if (columnType.equals("Collection")) {
\r
4745 collectionList.add(col);
\r
4748 regularColList.add(col);
\r
4750 if (columnType.equals("Blob")) {
\r
4751 blobList.add(col);
\r
4755 columnList.add(col);
\r
4757 if (Validator.isNotNull(collectionEntity) &&
\r
4758 Validator.isNotNull(mappingTable)) {
\r
4760 EntityMapping entityMapping = new EntityMapping(
\r
4761 mappingTable, ejbName, collectionEntity);
\r
4763 int ejbNameWeight = StringUtil.startsWithWeight(
\r
4764 mappingTable, ejbName);
\r
4765 int collectionEntityWeight = StringUtil.startsWithWeight(
\r
4766 mappingTable, collectionEntity);
\r
4768 if ((ejbNameWeight > collectionEntityWeight) ||
\r
4769 ((ejbNameWeight == collectionEntityWeight) &&
\r
4770 (ejbName.compareTo(collectionEntity) > 0))) {
\r
4772 _entityMappings.put(mappingTable, entityMapping);
\r
4777 EntityOrder order = null;
\r
4779 Element orderElement = entityElement.element("order");
\r
4781 if (orderElement != null) {
\r
4782 boolean asc = true;
\r
4784 if ((orderElement.attribute("by") != null) &&
\r
4785 orderElement.attributeValue("by").equals("desc")) {
\r
4790 List<EntityColumn> orderColsList = new ArrayList<EntityColumn>();
\r
4792 order = new EntityOrder(asc, orderColsList);
\r
4794 List<Element> orderColumnElements = orderElement.elements(
\r
4797 for (Element orderColElement : orderColumnElements) {
\r
4798 String orderColName = orderColElement.attributeValue("name");
\r
4799 boolean orderColCaseSensitive = GetterUtil.getBoolean(
\r
4800 orderColElement.attributeValue("case-sensitive"), true);
\r
4802 boolean orderColByAscending = asc;
\r
4804 String orderColBy = GetterUtil.getString(
\r
4805 orderColElement.attributeValue("order-by"));
\r
4807 if (orderColBy.equals("asc")) {
\r
4808 orderColByAscending = true;
\r
4810 else if (orderColBy.equals("desc")) {
\r
4811 orderColByAscending = false;
\r
4814 EntityColumn col = Entity.getColumn(orderColName, columnList);
\r
4816 col.setOrderColumn(true);
\r
4818 col = (EntityColumn)col.clone();
\r
4820 col.setCaseSensitive(orderColCaseSensitive);
\r
4821 col.setOrderByAscending(orderColByAscending);
\r
4823 orderColsList.add(col);
\r
4827 List<EntityFinder> finderList = new ArrayList<EntityFinder>();
\r
4829 List<Element> finderElements = entityElement.elements("finder");
\r
4832 Element finderElement = SAXReaderUtil.createElement("finder");
\r
4834 finderElement.addAttribute("name", "Uuid");
\r
4835 finderElement.addAttribute("return-type", "Collection");
\r
4837 Element finderColumnElement = finderElement.addElement(
\r
4840 finderColumnElement.addAttribute("name", "uuid");
\r
4842 finderElements.add(0, finderElement);
\r
4844 if (columnList.contains(new EntityColumn("groupId"))) {
\r
4845 finderElement = SAXReaderUtil.createElement("finder");
\r
4847 finderElement.addAttribute("name", "UUID_G");
\r
4848 finderElement.addAttribute("return-type", ejbName);
\r
4849 finderElement.addAttribute("unique", "true");
\r
4851 finderColumnElement = finderElement.addElement("finder-column");
\r
4853 finderColumnElement.addAttribute("name", "uuid");
\r
4855 finderColumnElement = finderElement.addElement("finder-column");
\r
4857 finderColumnElement.addAttribute("name", "groupId");
\r
4859 finderElements.add(1, finderElement);
\r
4863 if (permissionedModel) {
\r
4864 Element finderElement = SAXReaderUtil.createElement("finder");
\r
4866 finderElement.addAttribute("name", "ResourceBlockId");
\r
4867 finderElement.addAttribute("return-type", "Collection");
\r
4869 Element finderColumnElement = finderElement.addElement(
\r
4872 finderColumnElement.addAttribute("name", "resourceBlockId");
\r
4874 finderElements.add(0, finderElement);
\r
4877 String alias = TextFormatter.format(ejbName, TextFormatter.I);
\r
4879 if (_badAliasNames.contains(alias.toLowerCase())) {
\r
4880 alias += StringPool.UNDERLINE;
\r
4883 for (Element finderElement : finderElements) {
\r
4884 String finderName = finderElement.attributeValue("name");
\r
4885 String finderReturn = finderElement.attributeValue("return-type");
\r
4886 boolean finderUnique = GetterUtil.getBoolean(
\r
4887 finderElement.attributeValue("unique"));
\r
4889 String finderWhere = finderElement.attributeValue("where");
\r
4891 if (Validator.isNotNull(finderWhere)) {
\r
4892 for (EntityColumn column : columnList) {
\r
4893 String name = column.getName();
\r
4895 if (finderWhere.indexOf(name) != -1) {
\r
4896 finderWhere = finderWhere.replaceAll(
\r
4897 name, alias + "." + name);
\r
4902 boolean finderDBIndex = GetterUtil.getBoolean(
\r
4903 finderElement.attributeValue("db-index"), true);
\r
4905 List<EntityColumn> finderColsList = new ArrayList<EntityColumn>();
\r
4907 List<Element> finderColumnElements = finderElement.elements(
\r
4910 for (Element finderColumnElement : finderColumnElements) {
\r
4911 String finderColName = finderColumnElement.attributeValue(
\r
4913 boolean finderColCaseSensitive = GetterUtil.getBoolean(
\r
4914 finderColumnElement.attributeValue("case-sensitive"), true);
\r
4915 String finderColComparator = GetterUtil.getString(
\r
4916 finderColumnElement.attributeValue("comparator"), "=");
\r
4917 String finderColArrayableOperator =
\r
4918 GetterUtil.getString(
\r
4919 finderColumnElement.attributeValue(
\r
4920 "arrayable-operator"));
\r
4922 EntityColumn col = Entity.getColumn(finderColName, columnList);
\r
4924 if (!col.isFinderPath()) {
\r
4925 col.setFinderPath(true);
\r
4928 col = (EntityColumn)col.clone();
\r
4930 col.setCaseSensitive(finderColCaseSensitive);
\r
4931 col.setComparator(finderColComparator);
\r
4932 col.setArrayableOperator(finderColArrayableOperator);
\r
4934 finderColsList.add(col);
\r
4939 finderName, finderReturn, finderUnique, finderWhere,
\r
4940 finderDBIndex, finderColsList));
\r
4943 List<Entity> referenceList = new ArrayList<Entity>();
\r
4946 if (Validator.isNotNull(_pluginName)) {
\r
4947 for (String config : PropsValues.RESOURCE_ACTIONS_CONFIGS) {
\r
4948 File file = new File(_implDir + "/" + config);
\r
4950 if (file.exists()) {
\r
4951 InputStream inputStream = new FileInputStream(file);
\r
4953 ResourceActionsUtil.read(_pluginName, inputStream);
\r
4958 List<Element> referenceElements = entityElement.elements(
\r
4961 Set<String> referenceSet = new TreeSet<String>();
\r
4963 for (Element referenceElement : referenceElements) {
\r
4964 String referencePackage = referenceElement.attributeValue(
\r
4966 String referenceEntity = referenceElement.attributeValue(
\r
4969 referenceSet.add(referencePackage + "." + referenceEntity);
\r
4972 if (!_packagePath.equals("com.liferay.counter")) {
\r
4973 referenceSet.add("com.liferay.counter.Counter");
\r
4976 if (!_packagePath.equals("com.liferay.portal")) {
\r
4977 referenceSet.add("com.liferay.portal.Resource");
\r
4978 referenceSet.add("com.liferay.portal.User");
\r
4981 for (String referenceName : referenceSet) {
\r
4982 referenceList.add(getEntity(referenceName));
\r
4986 List<String> txRequiredList = new ArrayList<String>();
\r
4988 List<Element> txRequiredElements = entityElement.elements(
\r
4991 for (Element txRequiredEl : txRequiredElements) {
\r
4992 String txRequired = txRequiredEl.getText();
\r
4994 txRequiredList.add(txRequired);
\r
4999 _packagePath, _portletName, _portletShortName, ejbName,
\r
5000 humanName, table, alias, uuid, uuidAccessor, localService,
\r
5001 remoteService, persistenceClass, finderClass, dataSource,
\r
5002 sessionFactory, txManager, cacheEnabled, jsonEnabled, pkList,
\r
5003 regularColList, blobList, collectionList, columnList, order,
\r
5004 finderList, referenceList, txRequiredList));
\r
5007 private String _processTemplate(String name) throws Exception {
\r
5008 return _processTemplate(name, _getContext());
\r
5011 private String _processTemplate(String name, Map<String, Object> context)
\r
5012 throws Exception {
\r
5014 return StringUtil.strip(FreeMarkerUtil.process(name, context), '\r');
\r
5017 private Set<String> _readLines(String fileName) throws Exception {
\r
5018 ClassLoader classLoader = getClass().getClassLoader();
\r
5020 Set<String> lines = new HashSet<String>();
\r
5022 StringUtil.readLines(classLoader.getResourceAsStream(fileName), lines);
\r
5027 private void _updateSQLFile(
\r
5028 String sqlFileName, String createTableSQL, Entity entity)
\r
5029 throws IOException {
\r
5031 File updateSQLFile = new File(_sqlDir + "/" + sqlFileName);
\r
5033 if (updateSQLFile.exists()) {
\r
5034 _createSQLTables(updateSQLFile, createTableSQL, entity, false);
\r
5038 private static final int _SESSION_TYPE_LOCAL = 1;
\r
5040 private static final int _SESSION_TYPE_REMOTE = 0;
\r
5042 private static final String _SQL_CREATE_TABLE = "create table ";
\r
5044 private static final String _TPL_ROOT =
\r
5045 "com/liferay/portal/tools/servicebuilder/dependencies/";
\r
5047 private static Pattern _getterPattern = Pattern.compile(
\r
5048 "public .* get.*" + Pattern.quote("(") + "|public boolean is.*" +
\r
5049 Pattern.quote("("));
\r
5051 private static Pattern _setterPattern = Pattern.compile(
\r
5052 "public void set.*" + Pattern.quote("("));
\r
5054 private String _apiDir;
\r
5055 private String _author;
\r
5056 private boolean _autoNamespaceTables;
\r
5057 private Set<String> _badAliasNames;
\r
5058 private Set<String> _badColumnNames;
\r
5059 private Set<String> _badJsonTypes;
\r
5060 private Set<String> _badTableNames;
\r
5061 private String _beanLocatorUtil;
\r
5062 private String _beanLocatorUtilShortName;
\r
5063 private boolean _build;
\r
5064 private long _buildNumber;
\r
5065 private boolean _buildNumberIncrement;
\r
5066 private List<Entity> _ejbList;
\r
5067 private Map<String, EntityMapping> _entityMappings;
\r
5068 private Map<String, Entity> _entityPool = new HashMap<String, Entity>();
\r
5069 private String _hbmFileName;
\r
5070 private String _implDir;
\r
5071 private Map<String, JavaClass> _javaClasses =
\r
5072 new HashMap<String, JavaClass>();
\r
5073 private String _jsonFileName;
\r
5074 private String _modelHintsFileName;
\r
5075 private String _ormFileName;
\r
5076 private String _outputPath;
\r
5077 private String _packagePath;
\r
5078 private String _pluginName;
\r
5079 private String _portletName = StringPool.BLANK;
\r
5080 private String _portletPackageName = StringPool.BLANK;
\r
5081 private String _portletShortName = StringPool.BLANK;
\r
5082 private String _propsUtil;
\r
5083 private String _remotingFileName;
\r
5084 private String _serviceOutputPath;
\r
5085 private String _springBaseFileName;
\r
5086 private String _springClusterFileName;
\r
5087 private String _springDynamicDataSourceFileName;
\r
5088 private String _springFileName;
\r
5089 private String _springHibernateFileName;
\r
5090 private String _springInfrastructureFileName;
\r
5091 private String _springShardDataSourceFileName;
\r
5092 private String _sqlDir;
\r
5093 private String _sqlFileName;
\r
5094 private String _sqlIndexesFileName;
\r
5095 private String _sqlIndexesPropertiesFileName;
\r
5096 private String _sqlSequencesFileName;
\r
5097 private String _targetEntityName;
\r
5098 private String _testDir;
\r
5099 private String _testOutputPath;
\r
5100 private String _tplBadAliasNames = _TPL_ROOT + "bad_alias_names.txt";
\r
5101 private String _tplBadColumnNames = _TPL_ROOT + "bad_column_names.txt";
\r
5102 private String _tplBadJsonTypes = _TPL_ROOT + "bad_json_types.txt";
\r
5103 private String _tplBadTableNames = _TPL_ROOT + "bad_table_names.txt";
\r
5104 private String _tplBlobModel = _TPL_ROOT + "blob_model.ftl";
\r
5105 private String _tplEjbPk = _TPL_ROOT + "ejb_pk.ftl";
\r
5106 private String _tplException = _TPL_ROOT + "exception.ftl";
\r
5107 private String _tplExtendedModel = _TPL_ROOT + "extended_model.ftl";
\r
5108 private String _tplExtendedModelBaseImpl =
\r
5109 _TPL_ROOT + "extended_model_base_impl.ftl";
\r
5110 private String _tplExtendedModelImpl =
\r
5111 _TPL_ROOT + "extended_model_impl.ftl";
\r
5112 private String _tplFinder = _TPL_ROOT + "finder.ftl";
\r
5113 private String _tplFinderUtil = _TPL_ROOT + "finder_util.ftl";
\r
5114 private String _tplHbmXml = _TPL_ROOT + "hbm_xml.ftl";
\r
5115 private String _tplJsonJs = _TPL_ROOT + "json_js.ftl";
\r
5116 private String _tplJsonJsMethod = _TPL_ROOT + "json_js_method.ftl";
\r
5117 private String _tplModel = _TPL_ROOT + "model.ftl";
\r
5118 private String _tplModelCache = _TPL_ROOT + "model_cache.ftl";
\r
5119 private String _tplModelClp = _TPL_ROOT + "model_clp.ftl";
\r
5120 private String _tplModelHintsXml = _TPL_ROOT + "model_hints_xml.ftl";
\r
5121 private String _tplModelImpl = _TPL_ROOT + "model_impl.ftl";
\r
5122 private String _tplModelSoap = _TPL_ROOT + "model_soap.ftl";
\r
5123 private String _tplModelWrapper = _TPL_ROOT + "model_wrapper.ftl";
\r
5124 private String _tplOrmXml = _TPL_ROOT + "orm_xml.ftl";
\r
5125 private String _tplPersistence = _TPL_ROOT + "persistence.ftl";
\r
5126 private String _tplPersistenceImpl = _TPL_ROOT + "persistence_impl.ftl";
\r
5127 private String _tplPersistenceTest = _TPL_ROOT + "persistence_test.ftl";
\r
5128 private String _tplPersistenceUtil = _TPL_ROOT + "persistence_util.ftl";
\r
5129 private String _tplProps = _TPL_ROOT + "props.ftl";
\r
5130 private String _tplRemotingXml = _TPL_ROOT + "remoting_xml.ftl";
\r
5131 private String _tplService = _TPL_ROOT + "service.ftl";
\r
5132 private String _tplServiceBaseImpl = _TPL_ROOT + "service_base_impl.ftl";
\r
5133 private String _tplServiceClp = _TPL_ROOT + "service_clp.ftl";
\r
5134 private String _tplServiceClpInvoker =
\r
5135 _TPL_ROOT + "service_clp_invoker.ftl";
\r
5136 private String _tplServiceClpMessageListener =
\r
5137 _TPL_ROOT + "service_clp_message_listener.ftl";
\r
5138 private String _tplServiceClpSerializer =
\r
5139 _TPL_ROOT + "service_clp_serializer.ftl";
\r
5140 private String _tplServiceHttp = _TPL_ROOT + "service_http.ftl";
\r
5141 private String _tplServiceImpl = _TPL_ROOT + "service_impl.ftl";
\r
5142 private String _tplServiceSoap = _TPL_ROOT + "service_soap.ftl";
\r
5143 private String _tplServiceUtil = _TPL_ROOT + "service_util.ftl";
\r
5144 private String _tplServiceWrapper = _TPL_ROOT + "service_wrapper.ftl";
\r
5145 private String _tplSpringBaseXml = _TPL_ROOT + "spring_base_xml.ftl";
\r
5146 private String _tplSpringClusterXml = _TPL_ROOT + "spring_cluster_xml.ftl";
\r
5147 private String _tplSpringDynamicDataSourceXml =
\r
5148 _TPL_ROOT + "spring_dynamic_data_source_xml.ftl";
\r
5149 private String _tplSpringHibernateXml =
\r
5150 _TPL_ROOT + "spring_hibernate_xml.ftl";
\r
5151 private String _tplSpringInfrastructureXml =
\r
5152 _TPL_ROOT + "spring_infrastructure_xml.ftl";
\r
5153 private String _tplSpringShardDataSourceXml =
\r
5154 _TPL_ROOT + "spring_shard_data_source_xml.ftl";
\r
5155 private String _tplSpringXml = _TPL_ROOT + "spring_xml.ftl";
\r
5156 private String _tplSpringClearService = _TPL_ROOT + "spring_clear_service.ftl";
\r