001 /** 002 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. 003 * 004 * This library is free software; you can redistribute it and/or modify it under 005 * the terms of the GNU Lesser General Public License as published by the Free 006 * Software Foundation; either version 2.1 of the License, or (at your option) 007 * any later version. 008 * 009 * This library is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 012 * details. 013 */ 014 015 package com.liferay.portal.tools.servicebuilder; 016 017 import com.liferay.portal.freemarker.FreeMarkerUtil; 018 import com.liferay.portal.kernel.exception.PortalException; 019 import com.liferay.portal.kernel.exception.SystemException; 020 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader; 021 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader; 022 import com.liferay.portal.kernel.util.ArrayUtil; 023 import com.liferay.portal.kernel.util.ArrayUtil_IW; 024 import com.liferay.portal.kernel.util.ClearThreadLocalUtil; 025 import com.liferay.portal.kernel.util.FileUtil; 026 import com.liferay.portal.kernel.util.GetterUtil; 027 import com.liferay.portal.kernel.util.ListUtil; 028 import com.liferay.portal.kernel.util.PropertiesUtil; 029 import com.liferay.portal.kernel.util.StringBundler; 030 import com.liferay.portal.kernel.util.StringPool; 031 import com.liferay.portal.kernel.util.StringUtil; 032 import com.liferay.portal.kernel.util.StringUtil_IW; 033 import com.liferay.portal.kernel.util.TextFormatter; 034 import com.liferay.portal.kernel.util.Time; 035 import com.liferay.portal.kernel.util.Validator; 036 import com.liferay.portal.kernel.util.Validator_IW; 037 import com.liferay.portal.kernel.xml.Document; 038 import com.liferay.portal.kernel.xml.Element; 039 import com.liferay.portal.kernel.xml.SAXReaderUtil; 040 import com.liferay.portal.model.CacheField; 041 import com.liferay.portal.model.ModelHintsUtil; 042 import com.liferay.portal.security.permission.ResourceActionsUtil; 043 import com.liferay.portal.tools.ArgumentsUtil; 044 import com.liferay.portal.tools.SourceFormatter; 045 import com.liferay.portal.util.InitUtil; 046 import com.liferay.portal.util.PropsValues; 047 import com.liferay.util.xml.XMLFormatter; 048 049 import com.thoughtworks.qdox.JavaDocBuilder; 050 import com.thoughtworks.qdox.model.Annotation; 051 import com.thoughtworks.qdox.model.ClassLibrary; 052 import com.thoughtworks.qdox.model.DocletTag; 053 import com.thoughtworks.qdox.model.JavaClass; 054 import com.thoughtworks.qdox.model.JavaField; 055 import com.thoughtworks.qdox.model.JavaMethod; 056 import com.thoughtworks.qdox.model.JavaParameter; 057 import com.thoughtworks.qdox.model.Type; 058 059 import de.hunsicker.io.FileFormat; 060 import de.hunsicker.jalopy.Jalopy; 061 import de.hunsicker.jalopy.storage.Convention; 062 import de.hunsicker.jalopy.storage.ConventionKeys; 063 import de.hunsicker.jalopy.storage.Environment; 064 065 import freemarker.ext.beans.BeansWrapper; 066 067 import freemarker.log.Logger; 068 069 import freemarker.template.TemplateHashModel; 070 import freemarker.template.TemplateModelException; 071 072 import java.beans.Introspector; 073 074 import java.io.File; 075 import java.io.FileInputStream; 076 import java.io.FileNotFoundException; 077 import java.io.FileReader; 078 import java.io.IOException; 079 import java.io.InputStream; 080 081 import java.util.ArrayList; 082 import java.util.Arrays; 083 import java.util.Collections; 084 import java.util.HashMap; 085 import java.util.HashSet; 086 import java.util.Iterator; 087 import java.util.LinkedHashSet; 088 import java.util.List; 089 import java.util.Map; 090 import java.util.Properties; 091 import java.util.Set; 092 import java.util.TreeMap; 093 import java.util.TreeSet; 094 import java.util.regex.Matcher; 095 import java.util.regex.Pattern; 096 097 import org.dom4j.DocumentException; 098 099 /** 100 * @author Brian Wing Shun Chan 101 * @author Charles May 102 * @author Alexander Chow 103 * @author Harry Mark 104 * @author Tariq Dweik 105 * @author Glenn Powell 106 * @author Raymond Augé 107 * @author Prashant Dighe 108 * @author Shuyang Zhou 109 */ 110 public class ServiceBuilder { 111 112 public static final String AUTHOR = "Brian Wing Shun Chan"; 113 114 public static void main(String[] args) { 115 Map<String, String> arguments = ArgumentsUtil.parseArguments(args); 116 117 InitUtil.initWithSpring(true); 118 119 String fileName = arguments.get("service.input.file"); 120 String hbmFileName = arguments.get("service.hbm.file"); 121 String ormFileName = arguments.get("service.orm.file"); 122 String modelHintsFileName = arguments.get("service.model.hints.file"); 123 String springFileName = arguments.get("service.spring.file"); 124 String springBaseFileName = arguments.get("service.spring.base.file"); 125 String springClusterFileName = arguments.get("service.spring.cluster.file"); 126 String springDynamicDataSourceFileName = arguments.get("service.spring.dynamic.data.source.file"); 127 String springHibernateFileName = arguments.get("service.spring.hibernate.file"); 128 String springInfrastructureFileName = arguments.get("service.spring.infrastructure.file"); 129 String springShardDataSourceFileName = arguments.get("service.spring.shard.data.source.file"); 130 String apiDir = arguments.get("service.api.dir"); 131 String implDir = arguments.get("service.impl.dir"); 132 String jsonFileName = arguments.get("service.json.file"); 133 String remotingFileName = arguments.get("service.remoting.file"); 134 String sqlDir = arguments.get("service.sql.dir"); 135 String sqlFileName = arguments.get("service.sql.file"); 136 String sqlIndexesFileName = arguments.get("service.sql.indexes.file"); 137 String sqlIndexesPropertiesFileName = arguments.get("service.sql.indexes.properties.file"); 138 String sqlSequencesFileName = arguments.get("service.sql.sequences.file"); 139 boolean autoNamespaceTables = GetterUtil.getBoolean(arguments.get("service.auto.namespace.tables")); 140 String beanLocatorUtil = arguments.get("service.bean.locator.util"); 141 String propsUtil = arguments.get("service.props.util"); 142 String pluginName = arguments.get("service.plugin.name"); 143 String testDir = arguments.get("service.test.dir"); 144 145 try { 146 new ServiceBuilder( 147 fileName, hbmFileName, ormFileName, modelHintsFileName, 148 springFileName, springBaseFileName, springClusterFileName, 149 springDynamicDataSourceFileName, springHibernateFileName, 150 springInfrastructureFileName, springShardDataSourceFileName, 151 apiDir, implDir, jsonFileName, remotingFileName, sqlDir, 152 sqlFileName, sqlIndexesFileName, sqlIndexesPropertiesFileName, 153 sqlSequencesFileName, autoNamespaceTables, beanLocatorUtil, 154 propsUtil, pluginName, testDir); 155 } 156 catch (RuntimeException re) { 157 System.out.println( 158 "Please set these required arguments. Sample values are:\n" + 159 "\n" + 160 "\tservice.input.file=${service.file}\n" + 161 "\tservice.hbm.file=src/META-INF/portal-hbm.xml\n" + 162 "\tservice.orm.file=src/META-INF/portal-orm.xml\n" + 163 "\tservice.model.hints.file=src/META-INF/portal-model-hints.xml\n" + 164 "\tservice.spring.file=src/META-INF/portal-spring.xml\n" + 165 "\tservice.api.dir=${project.dir}/portal-service/src\n" + 166 "\tservice.impl.dir=src\n" + 167 "\tservice.json.file=${project.dir}/portal-web/docroot/html/js/liferay/service_unpacked.js\n" + 168 "\tservice.remoting.file=${project.dir}/portal-web/docroot/WEB-INF/remoting-servlet.xml\n" + 169 "\tservice.sql.dir=../sql\n" + 170 "\tservice.sql.file=portal-tables.sql\n" + 171 "\tservice.sql.indexes.file=indexes.sql\n" + 172 "\tservice.sql.indexes.properties.file=indexes.properties\n" + 173 "\tservice.sql.sequences.file=sequences.sql\n" + 174 "\tservice.bean.locator.util.package=com.liferay.portal.kernel.bean\n" + 175 "\tservice.props.util.package=com.liferay.portal.util\n" + 176 "\n" + 177 "You can also customize the generated code by overriding the default templates with these optional system properties:\n" + 178 "\n" + 179 "\t-Dservice.tpl.bad_alias_names=" + _TPL_ROOT + "bad_alias_names.txt\n"+ 180 "\t-Dservice.tpl.bad_column_names=" + _TPL_ROOT + "bad_column_names.txt\n"+ 181 "\t-Dservice.tpl.bad_json_types=" + _TPL_ROOT + "bad_json_types.txt\n"+ 182 "\t-Dservice.tpl.bad_table_names=" + _TPL_ROOT + "bad_table_names.txt\n"+ 183 "\t-Dservice.tpl.base_mode_impl=" + _TPL_ROOT + "base_mode_impl.ftl\n"+ 184 "\t-Dservice.tpl.blob_model=" + _TPL_ROOT + "blob_model.ftl\n"+ 185 "\t-Dservice.tpl.copyright.txt=copyright.txt\n"+ 186 "\t-Dservice.tpl.ejb_pk=" + _TPL_ROOT + "ejb_pk.ftl\n"+ 187 "\t-Dservice.tpl.exception=" + _TPL_ROOT + "exception.ftl\n"+ 188 "\t-Dservice.tpl.extended_model=" + _TPL_ROOT + "extended_model.ftl\n"+ 189 "\t-Dservice.tpl.extended_model_base_impl=" + _TPL_ROOT + "extended_model_base_impl.ftl\n"+ 190 "\t-Dservice.tpl.extended_model_impl=" + _TPL_ROOT + "extended_model_impl.ftl\n"+ 191 "\t-Dservice.tpl.finder=" + _TPL_ROOT + "finder.ftl\n"+ 192 "\t-Dservice.tpl.finder_util=" + _TPL_ROOT + "finder_util.ftl\n"+ 193 "\t-Dservice.tpl.hbm_xml=" + _TPL_ROOT + "hbm_xml.ftl\n"+ 194 "\t-Dservice.tpl.orm_xml=" + _TPL_ROOT + "orm_xml.ftl\n"+ 195 "\t-Dservice.tpl.json_js=" + _TPL_ROOT + "json_js.ftl\n"+ 196 "\t-Dservice.tpl.json_js_method=" + _TPL_ROOT + "json_js_method.ftl\n"+ 197 "\t-Dservice.tpl.model=" + _TPL_ROOT + "model.ftl\n"+ 198 "\t-Dservice.tpl.model_cache=" + _TPL_ROOT + "model_cache.ftl\n"+ 199 "\t-Dservice.tpl.model_hints_xml=" + _TPL_ROOT + "model_hints_xml.ftl\n"+ 200 "\t-Dservice.tpl.model_impl=" + _TPL_ROOT + "model_impl.ftl\n"+ 201 "\t-Dservice.tpl.model_soap=" + _TPL_ROOT + "model_soap.ftl\n"+ 202 "\t-Dservice.tpl.model_wrapper=" + _TPL_ROOT + "model_wrapper.ftl\n"+ 203 "\t-Dservice.tpl.persistence=" + _TPL_ROOT + "persistence.ftl\n"+ 204 "\t-Dservice.tpl.persistence_impl=" + _TPL_ROOT + "persistence_impl.ftl\n"+ 205 "\t-Dservice.tpl.persistence_util=" + _TPL_ROOT + "persistence_util.ftl\n"+ 206 "\t-Dservice.tpl.props=" + _TPL_ROOT + "props.ftl\n"+ 207 "\t-Dservice.tpl.remoting_xml=" + _TPL_ROOT + "remoting_xml.ftl\n"+ 208 "\t-Dservice.tpl.service=" + _TPL_ROOT + "service.ftl\n"+ 209 "\t-Dservice.tpl.service_base_impl=" + _TPL_ROOT + "service_base_impl.ftl\n"+ 210 "\t-Dservice.tpl.service_clp=" + _TPL_ROOT + "service_clp.ftl\n"+ 211 "\t-Dservice.tpl.service_clp_message_listener=" + _TPL_ROOT + "service_clp_message_listener.ftl\n"+ 212 "\t-Dservice.tpl.service_clp_serializer=" + _TPL_ROOT + "service_clp_serializer.ftl\n"+ 213 "\t-Dservice.tpl.service_http=" + _TPL_ROOT + "service_http.ftl\n"+ 214 "\t-Dservice.tpl.service_impl=" + _TPL_ROOT + "service_impl.ftl\n"+ 215 "\t-Dservice.tpl.service_soap=" + _TPL_ROOT + "service_soap.ftl\n"+ 216 "\t-Dservice.tpl.service_util=" + _TPL_ROOT + "service_util.ftl\n"+ 217 "\t-Dservice.tpl.service_wrapper=" + _TPL_ROOT + "service_wrapper.ftl\n"+ 218 "\t-Dservice.tpl.spring_base_xml=" + _TPL_ROOT + "spring_base_xml.ftl\n"+ 219 "\t-Dservice.tpl.spring_dynamic_data_source_xml=" + _TPL_ROOT + "spring_dynamic_data_source_xml.ftl\n"+ 220 "\t-Dservice.tpl.spring_hibernate_xml=" + _TPL_ROOT + "spring_hibernate_xml.ftl\n"+ 221 "\t-Dservice.tpl.spring_infrastructure_xml=" + _TPL_ROOT + "spring_infrastructure_xml.ftl\n"+ 222 "\t-Dservice.tpl.spring_xml=" + _TPL_ROOT + "spring_xml.ftl\n"+ 223 "\t-Dservice.tpl.spring_xml_session=" + _TPL_ROOT + "spring_xml_session.ftl"); 224 225 throw re; 226 } 227 228 try { 229 ClearThreadLocalUtil.clearThreadLocal(); 230 } 231 catch (Exception e) { 232 e.printStackTrace(); 233 } 234 235 Introspector.flushCaches(); 236 } 237 238 public static String toHumanName(String name) { 239 if (name == null) { 240 return null; 241 } 242 243 String humanName = TextFormatter.format(name, TextFormatter.H); 244 245 if (humanName.equals("id")) { 246 humanName = "ID"; 247 } 248 else if (humanName.equals("ids")) { 249 humanName = "IDs"; 250 } 251 252 if (humanName.endsWith(" id")) { 253 humanName = humanName.substring(0, humanName.length() - 3) + " ID"; 254 } 255 else if (humanName.endsWith(" ids")) { 256 humanName = humanName.substring(0, humanName.length() - 4) + " IDs"; 257 } 258 259 if (humanName.contains(" id ")) { 260 humanName = StringUtil.replace(humanName, " id ", " ID "); 261 } 262 else if (humanName.contains(" ids ")) { 263 humanName = StringUtil.replace(humanName, " ids ", " IDs "); 264 } 265 266 return humanName; 267 } 268 269 public static void writeFile(File file, String content) 270 throws IOException { 271 272 writeFile(file, content, AUTHOR); 273 } 274 275 public static void writeFile(File file, String content, String author) 276 throws IOException { 277 278 writeFile(file, content, author, null); 279 } 280 281 public static void writeFile( 282 File file, String content, String author, 283 Map<String, Object> jalopySettings) 284 throws IOException { 285 286 String packagePath = _getPackagePath(file); 287 288 String className = file.getName(); 289 290 className = className.substring(0, className.length() - 5); 291 292 content = SourceFormatter.stripJavaImports( 293 content, packagePath, className); 294 295 File tempFile = new File("ServiceBuilder.temp"); 296 297 FileUtil.write(tempFile, content); 298 299 // Beautify 300 301 StringBuffer sb = new StringBuffer(); 302 303 Jalopy jalopy = new Jalopy(); 304 305 jalopy.setFileFormat(FileFormat.UNIX); 306 jalopy.setInput(tempFile); 307 jalopy.setOutput(sb); 308 309 File jalopyXmlFile = new File("tools/jalopy.xml"); 310 311 if (!jalopyXmlFile.exists()) { 312 jalopyXmlFile = new File("../tools/jalopy.xml"); 313 } 314 315 if (!jalopyXmlFile.exists()) { 316 jalopyXmlFile = new File("misc/jalopy.xml"); 317 } 318 319 if (!jalopyXmlFile.exists()) { 320 jalopyXmlFile = new File("../misc/jalopy.xml"); 321 } 322 323 if (!jalopyXmlFile.exists()) { 324 jalopyXmlFile = new File("../../misc/jalopy.xml"); 325 } 326 327 try { 328 Jalopy.setConvention(jalopyXmlFile); 329 } 330 catch (FileNotFoundException fnne) { 331 } 332 333 if (jalopySettings == null) { 334 jalopySettings = new HashMap<String, Object>(); 335 } 336 337 Environment env = Environment.getInstance(); 338 339 // Author 340 341 author = GetterUtil.getString( 342 (String)jalopySettings.get("author"), author); 343 344 env.set("author", author); 345 346 // File name 347 348 env.set("fileName", file.getName()); 349 350 Convention convention = Convention.getInstance(); 351 352 String classMask = "/**\n" + 353 " * @author $author$\n" + 354 "*/"; 355 356 convention.put( 357 ConventionKeys.COMMENT_JAVADOC_TEMPLATE_CLASS, 358 env.interpolate(classMask)); 359 360 convention.put( 361 ConventionKeys.COMMENT_JAVADOC_TEMPLATE_INTERFACE, 362 env.interpolate(classMask)); 363 364 jalopy.format(); 365 366 String newContent = sb.toString(); 367 368 // Remove double blank lines after the package or last import 369 370 newContent = newContent.replaceFirst( 371 "(?m)^[ \t]*((?:package|import) .*;)\\s*^[ \t]*/\\*\\*", 372 "$1\n\n/**"); 373 374 /* 375 // Remove blank lines after try { 376 377 newContent = StringUtil.replace(newContent, "try {\n\n", "try {\n"); 378 379 // Remove blank lines after ) { 380 381 newContent = StringUtil.replace(newContent, ") {\n\n", ") {\n"); 382 383 // Remove blank lines empty braces { } 384 385 newContent = StringUtil.replace(newContent, "\n\n\t}", "\n\t}"); 386 387 // Add space to last } 388 389 newContent = newContent.substring(0, newContent.length() - 2) + "\n\n}"; 390 */ 391 392 writeFileRaw(file, newContent); 393 394 tempFile.deleteOnExit(); 395 } 396 397 public static void writeFileRaw(File file, String content) 398 throws IOException { 399 400 // Write file if and only if the file has changed 401 402 if (!file.exists() || !FileUtil.isSameContent(file, content)) { 403 FileUtil.write(file, content); 404 405 System.out.println("Writing " + file); 406 } 407 } 408 409 public ServiceBuilder( 410 String fileName, String hbmFileName, String ormFileName, 411 String modelHintsFileName, String springFileName, 412 String springBaseFileName, String springClusterFileName, 413 String springDynamicDataSourceFileName, String springHibernateFileName, 414 String springInfrastructureFileName, 415 String springShardDataSourceFileName, String apiDir, String implDir, 416 String jsonFileName, String remotingFileName, String sqlDir, 417 String sqlFileName, String sqlIndexesFileName, 418 String sqlIndexesPropertiesFileName, String sqlSequencesFileName, 419 boolean autoNamespaceTables, String beanLocatorUtil, String propsUtil, 420 String pluginName, String testDir) { 421 422 this( 423 fileName, hbmFileName, ormFileName, modelHintsFileName, 424 springFileName, springBaseFileName, springClusterFileName, 425 springDynamicDataSourceFileName, springHibernateFileName, 426 springInfrastructureFileName, springShardDataSourceFileName, apiDir, 427 implDir, jsonFileName, remotingFileName, sqlDir, sqlFileName, 428 sqlIndexesFileName, sqlIndexesPropertiesFileName, 429 sqlSequencesFileName, autoNamespaceTables, beanLocatorUtil, 430 propsUtil, pluginName, testDir, true); 431 } 432 433 public ServiceBuilder( 434 String fileName, String hbmFileName, String ormFileName, 435 String modelHintsFileName, String springFileName, 436 String springBaseFileName, String springClusterFileName, 437 String springDynamicDataSourceFileName, String springHibernateFileName, 438 String springInfrastructureFileName, 439 String springShardDataSourceFileName, String apiDir, String implDir, 440 String jsonFileName, String remotingFileName, String sqlDir, 441 String sqlFileName, String sqlIndexesFileName, 442 String sqlIndexesPropertiesFileName, String sqlSequencesFileName, 443 boolean autoNamespaceTables, String beanLocatorUtil, String propsUtil, 444 String pluginName, String testDir, boolean build) { 445 446 _tplBadAliasNames = _getTplProperty( 447 "bad_alias_names", _tplBadAliasNames); 448 _tplBadColumnNames = _getTplProperty( 449 "bad_column_names", _tplBadColumnNames); 450 _tplBadJsonTypes = _getTplProperty("bad_json_types", _tplBadJsonTypes); 451 _tplBadTableNames = _getTplProperty( 452 "bad_table_names", _tplBadTableNames); 453 _tplBlobModel = _getTplProperty("blob_model", _tplBlobModel); 454 _tplEjbPk = _getTplProperty("ejb_pk", _tplEjbPk); 455 _tplException = _getTplProperty("exception", _tplException); 456 _tplExtendedModel = _getTplProperty( 457 "extended_model", _tplExtendedModel); 458 _tplExtendedModelBaseImpl = _getTplProperty( 459 "extended_model_base_impl", _tplExtendedModelBaseImpl); 460 _tplExtendedModelImpl = _getTplProperty( 461 "extended_model_impl", _tplExtendedModelImpl); 462 _tplFinder = _getTplProperty("finder", _tplFinder); 463 _tplFinderUtil = _getTplProperty("finder_util", _tplFinderUtil); 464 _tplHbmXml = _getTplProperty("hbm_xml", _tplHbmXml); 465 _tplOrmXml = _getTplProperty("orm_xml", _tplOrmXml); 466 _tplJsonJs = _getTplProperty("json_js", _tplJsonJs); 467 _tplJsonJsMethod = _getTplProperty("json_js_method", _tplJsonJsMethod); 468 _tplModel = _getTplProperty("model", _tplModel); 469 _tplModelCache = _getTplProperty("model_cache", _tplModelCache); 470 _tplModelClp = _getTplProperty("model", _tplModelClp); 471 _tplModelHintsXml = _getTplProperty( 472 "model_hints_xml", _tplModelHintsXml); 473 _tplModelImpl = _getTplProperty("model_impl", _tplModelImpl); 474 _tplModelSoap = _getTplProperty("model_soap", _tplModelSoap); 475 _tplModelWrapper = _getTplProperty("model_wrapper", _tplModelWrapper); 476 _tplPersistence = _getTplProperty("persistence", _tplPersistence); 477 _tplPersistenceImpl = _getTplProperty( 478 "persistence_impl", _tplPersistenceImpl); 479 _tplPersistenceUtil = _getTplProperty( 480 "persistence_util", _tplPersistenceUtil); 481 _tplProps = _getTplProperty("props", _tplProps); 482 _tplRemotingXml = _getTplProperty("remoting_xml", _tplRemotingXml); 483 _tplService = _getTplProperty("service", _tplService); 484 _tplServiceBaseImpl = _getTplProperty( 485 "service_base_impl", _tplServiceBaseImpl); 486 _tplServiceClp = _getTplProperty("service_clp", _tplServiceClp); 487 _tplServiceClpMessageListener = _getTplProperty( 488 "service_clp_message_listener", _tplServiceClpMessageListener); 489 _tplServiceClpSerializer = _getTplProperty( 490 "service_clp_serializer", _tplServiceClpSerializer); 491 _tplServiceHttp = _getTplProperty("service_http", _tplServiceHttp); 492 _tplServiceImpl = _getTplProperty("service_impl", _tplServiceImpl); 493 _tplServiceSoap = _getTplProperty("service_soap", _tplServiceSoap); 494 _tplServiceUtil = _getTplProperty("service_util", _tplServiceUtil); 495 _tplServiceWrapper = _getTplProperty( 496 "service_wrapper", _tplServiceWrapper); 497 _tplSpringBaseXml = _getTplProperty( 498 "spring_base_xml", _tplSpringBaseXml); 499 _tplSpringClusterXml = _getTplProperty( 500 "spring_cluster_xml", _tplSpringClusterXml); 501 _tplSpringDynamicDataSourceXml = _getTplProperty( 502 "spring_dynamic_data_source_xml", _tplSpringDynamicDataSourceXml); 503 _tplSpringHibernateXml = _getTplProperty( 504 "spring_hibernate_xml", _tplSpringHibernateXml); 505 _tplSpringInfrastructureXml = _getTplProperty( 506 "spring_infrastructure_xml", _tplSpringInfrastructureXml); 507 _tplSpringShardDataSourceXml = _getTplProperty( 508 "spring_shard_data_source_xml", _tplSpringShardDataSourceXml); 509 _tplSpringXml = _getTplProperty("spring_xml", _tplSpringXml); 510 511 try { 512 _badTableNames = _readLines(_tplBadTableNames); 513 _badAliasNames = _readLines(_tplBadAliasNames); 514 _badColumnNames = _readLines(_tplBadColumnNames); 515 _badJsonTypes = _readLines(_tplBadJsonTypes); 516 _hbmFileName = hbmFileName; 517 _ormFileName = ormFileName; 518 _modelHintsFileName = modelHintsFileName; 519 _springFileName = springFileName; 520 _springBaseFileName = springBaseFileName; 521 _springClusterFileName = springClusterFileName; 522 _springDynamicDataSourceFileName = springDynamicDataSourceFileName; 523 _springHibernateFileName = springHibernateFileName; 524 _springInfrastructureFileName = springInfrastructureFileName; 525 _springShardDataSourceFileName = springShardDataSourceFileName; 526 _apiDir = apiDir; 527 _implDir = implDir; 528 _jsonFileName = jsonFileName; 529 _remotingFileName = remotingFileName; 530 _sqlDir = sqlDir; 531 _sqlFileName = sqlFileName; 532 _sqlIndexesFileName = sqlIndexesFileName; 533 _sqlIndexesPropertiesFileName = sqlIndexesPropertiesFileName; 534 _sqlSequencesFileName = sqlSequencesFileName; 535 _autoNamespaceTables = autoNamespaceTables; 536 _beanLocatorUtil = beanLocatorUtil; 537 _beanLocatorUtilShortName = _beanLocatorUtil.substring( 538 _beanLocatorUtil.lastIndexOf(".") + 1); 539 _propsUtil = propsUtil; 540 _pluginName = GetterUtil.getString(pluginName); 541 _testDir = testDir; 542 _build = build; 543 544 String content = _getContent(fileName); 545 546 Document document = SAXReaderUtil.read(content, true); 547 548 Element rootElement = document.getRootElement(); 549 550 String packagePath = rootElement.attributeValue("package-path"); 551 552 if (Validator.isNull(packagePath)) { 553 throw new IllegalArgumentException( 554 "The package-path attribute is required"); 555 } 556 557 _outputPath = 558 _implDir + "/" + StringUtil.replace(packagePath, ".", "/"); 559 560 _serviceOutputPath = 561 _apiDir + "/" + StringUtil.replace(packagePath, ".", "/"); 562 563 if (Validator.isNotNull(_testDir)) { 564 _testOutputPath = 565 _testDir + "/" + StringUtil.replace(packagePath, ".", "/"); 566 } 567 568 _packagePath = packagePath; 569 570 _autoNamespaceTables = GetterUtil.getBoolean( 571 rootElement.attributeValue("auto-namespace-tables"), 572 _autoNamespaceTables); 573 574 Element authorElement = rootElement.element("author"); 575 576 if (authorElement != null) { 577 _author = authorElement.getText(); 578 } 579 else { 580 _author = AUTHOR; 581 } 582 583 Element portletElement = rootElement.element("portlet"); 584 Element namespaceElement = rootElement.element("namespace"); 585 586 if (portletElement != null) { 587 _portletName = portletElement.attributeValue("name"); 588 589 _portletShortName = portletElement.attributeValue("short-name"); 590 591 _portletPackageName = 592 TextFormatter.format(_portletName, TextFormatter.B); 593 594 _outputPath += "/" + _portletPackageName; 595 596 _serviceOutputPath += "/" + _portletPackageName; 597 598 _testOutputPath += "/" + _portletPackageName; 599 600 _packagePath += "." + _portletPackageName; 601 } 602 else { 603 _portletShortName = namespaceElement.getText(); 604 } 605 606 _portletShortName = _portletShortName.trim(); 607 608 if (!Validator.isChar(_portletShortName)) { 609 throw new RuntimeException( 610 "The namespace element must be a valid keyword"); 611 } 612 613 _ejbList = new ArrayList<Entity>(); 614 _entityMappings = new HashMap<String, EntityMapping>(); 615 616 List<Element> entityElements = rootElement.elements("entity"); 617 618 for (Element entityElement : entityElements) { 619 _parseEntity(entityElement); 620 } 621 622 List<String> exceptionList = new ArrayList<String>(); 623 624 Element exceptionsElement = rootElement.element("exceptions"); 625 626 if (exceptionsElement != null) { 627 List<Element> exceptionElements = exceptionsElement.elements( 628 "exception"); 629 630 for (Element exceptionElement : exceptionElements) { 631 exceptionList.add(exceptionElement.getText()); 632 } 633 } 634 635 if (build) { 636 for (int x = 0; x < _ejbList.size(); x++) { 637 Entity entity = _ejbList.get(x); 638 639 System.out.println("Building " + entity.getName()); 640 641 if (true) {/* || 642 entity.getName().equals("EmailAddress") || 643 entity.getName().equals("User")) {*/ 644 645 if (entity.hasColumns()) { 646 _createHbm(entity); 647 _createHbmUtil(entity); 648 649 _createPersistenceImpl(entity); 650 _createPersistence(entity); 651 _createPersistenceUtil(entity); 652 653 if (Validator.isNotNull(_testDir)) { 654 _createPersistenceTest(entity); 655 } 656 657 _createModelImpl(entity); 658 _createExtendedModelBaseImpl(entity); 659 _createExtendedModelImpl(entity); 660 661 entity.setTransients(_getTransients(entity, false)); 662 entity.setParentTransients( 663 _getTransients(entity, true)); 664 665 _createModel(entity); 666 _createExtendedModel(entity); 667 668 _createModelCache(entity); 669 _createModelClp(entity); 670 _createModelWrapper(entity); 671 672 _createModelSoap(entity); 673 674 _createBlobModels(entity); 675 676 _createPool(entity); 677 678 if (entity.getPKList().size() > 1) { 679 _createEJBPK(entity); 680 } 681 } 682 683 _createFinder(entity); 684 _createFinderUtil(entity); 685 686 if (entity.hasLocalService()) { 687 _createServiceImpl(entity, _SESSION_TYPE_LOCAL); 688 _createServiceBaseImpl(entity, _SESSION_TYPE_LOCAL); 689 _createService(entity, _SESSION_TYPE_LOCAL); 690 _createServiceFactory(entity, _SESSION_TYPE_LOCAL); 691 _createServiceUtil(entity, _SESSION_TYPE_LOCAL); 692 693 _createServiceClp(entity, _SESSION_TYPE_LOCAL); 694 _createServiceWrapper(entity, _SESSION_TYPE_LOCAL); 695 } 696 697 if (entity.hasRemoteService()) { 698 _createServiceImpl(entity, _SESSION_TYPE_REMOTE); 699 _createServiceBaseImpl( 700 entity, _SESSION_TYPE_REMOTE); 701 _createService(entity, _SESSION_TYPE_REMOTE); 702 _createServiceFactory(entity, _SESSION_TYPE_REMOTE); 703 _createServiceUtil(entity, _SESSION_TYPE_REMOTE); 704 705 _createServiceClp(entity, _SESSION_TYPE_REMOTE); 706 _createServiceWrapper(entity, _SESSION_TYPE_REMOTE); 707 708 if (Validator.isNotNull(_remotingFileName)) { 709 _createServiceHttp(entity); 710 } 711 712 _createServiceJson(entity); 713 714 if (entity.hasColumns()) { 715 _createServiceJsonSerializer(entity); 716 } 717 718 _createServiceSoap(entity); 719 } 720 } 721 } 722 723 _createHbmXml(); 724 _createOrmXml(); 725 _createModelHintsXml(); 726 _createSpringXml(); 727 728 _createServiceClpMessageListener(); 729 _createServiceClpSerializer(); 730 731 _createJsonJs(); 732 733 if (Validator.isNotNull(_remotingFileName)) { 734 _createRemotingXml(); 735 } 736 737 _createSQLIndexes(); 738 _createSQLTables(); 739 _createSQLSequences(); 740 741 _createExceptions(exceptionList); 742 743 _createProps(); 744 _createSpringBaseXml(); 745 _createSpringClusterXml(); 746 _createSpringDynamicDataSourceXml(); 747 _createSpringHibernateXml(); 748 _createSpringInfrastructureXml(); 749 _createSpringShardDataSourceXml(); 750 } 751 } 752 catch (FileNotFoundException fnfe) { 753 System.out.println(fnfe.getMessage()); 754 } 755 catch (Exception e) { 756 e.printStackTrace(); 757 } 758 } 759 760 public String getClassName(Type type) { 761 int dimensions = type.getDimensions(); 762 String name = type.getValue(); 763 764 if (dimensions > 0) { 765 StringBundler sb = new StringBundler(); 766 767 for (int i = 0; i < dimensions; i++) { 768 sb.append("["); 769 } 770 771 if (name.equals("boolean")) { 772 return sb.append("Z").toString(); 773 } 774 else if (name.equals("byte")) { 775 return sb.append("B").toString(); 776 } 777 else if (name.equals("char")) { 778 return sb.append("C").toString(); 779 } 780 else if (name.equals("double")) { 781 return sb.append("D").toString(); 782 } 783 else if (name.equals("float")) { 784 return sb.append("F").toString(); 785 } 786 else if (name.equals("int")) { 787 return sb.append("I").toString(); 788 } 789 else if (name.equals("long")) { 790 return sb.append("J").toString(); 791 } 792 else if (name.equals("short")) { 793 return sb.append("S").toString(); 794 } 795 else { 796 return sb.append("L").append(name).append(";").toString(); 797 } 798 } 799 800 return name; 801 } 802 803 public String getCreateMappingTableSQL(EntityMapping entityMapping) 804 throws IOException { 805 806 String createMappingTableSQL = _getCreateMappingTableSQL(entityMapping); 807 808 createMappingTableSQL = StringUtil.replace( 809 createMappingTableSQL, "\n", ""); 810 createMappingTableSQL = StringUtil.replace( 811 createMappingTableSQL, "\t", ""); 812 createMappingTableSQL = createMappingTableSQL.substring( 813 0, createMappingTableSQL.length() - 1); 814 815 return createMappingTableSQL; 816 } 817 818 public String getCreateTableSQL(Entity entity) { 819 String createTableSQL = _getCreateTableSQL(entity); 820 821 createTableSQL = StringUtil.replace(createTableSQL, "\n", ""); 822 createTableSQL = StringUtil.replace(createTableSQL, "\t", ""); 823 createTableSQL = createTableSQL.substring( 824 0, createTableSQL.length() - 1); 825 826 return createTableSQL; 827 } 828 829 public String getDimensions(int dims) { 830 String dimensions = ""; 831 832 for (int i = 0; i < dims; i++) { 833 dimensions += "[]"; 834 } 835 836 return dimensions; 837 } 838 839 public String getDimensions(String dims) { 840 return getDimensions(Integer.parseInt(dims)); 841 } 842 843 public Entity getEntity(String name) throws IOException { 844 Entity entity = _entityPool.get(name); 845 846 if (entity != null) { 847 return entity; 848 } 849 850 int pos = name.lastIndexOf("."); 851 852 if (pos == -1) { 853 pos = _ejbList.indexOf(new Entity(name)); 854 855 if (pos == -1) { 856 throw new RuntimeException( 857 "Cannot find " + name + " in " + 858 ListUtil.toString(_ejbList, Entity.NAME_ACCESSOR)); 859 } 860 861 entity = _ejbList.get(pos); 862 863 _entityPool.put(name, entity); 864 865 return entity; 866 } 867 else { 868 String refPackage = name.substring(0, pos); 869 String refPackageDir = StringUtil.replace(refPackage, ".", "/"); 870 String refEntity = name.substring(pos + 1, name.length()); 871 String refFileName = 872 _implDir + "/" + refPackageDir + "/service.xml"; 873 874 File refFile = new File(refFileName); 875 876 boolean useTempFile = false; 877 878 if (!refFile.exists()) { 879 refFileName = Time.getTimestamp(); 880 refFile = new File(refFileName); 881 882 ClassLoader classLoader = getClass().getClassLoader(); 883 884 FileUtil.write( 885 refFileName, 886 StringUtil.read( 887 classLoader, refPackageDir + "/service.xml")); 888 889 useTempFile = true; 890 } 891 892 ServiceBuilder serviceBuilder = new ServiceBuilder( 893 refFileName, _hbmFileName, _ormFileName, _modelHintsFileName, 894 _springFileName, _springBaseFileName, _springClusterFileName, 895 _springDynamicDataSourceFileName, _springHibernateFileName, 896 _springInfrastructureFileName, _springShardDataSourceFileName, 897 _apiDir, _implDir, _jsonFileName, _remotingFileName, _sqlDir, 898 _sqlFileName, _sqlIndexesFileName, 899 _sqlIndexesPropertiesFileName, _sqlSequencesFileName, 900 _autoNamespaceTables, _beanLocatorUtil, _propsUtil, _pluginName, 901 _testDir, false); 902 903 entity = serviceBuilder.getEntity(refEntity); 904 905 entity.setPortalReference(useTempFile); 906 907 _entityPool.put(name, entity); 908 909 if (useTempFile) { 910 refFile.deleteOnExit(); 911 } 912 913 return entity; 914 } 915 } 916 917 public Entity getEntityByGenericsName(String genericsName) { 918 try { 919 String name = genericsName; 920 921 if (name.startsWith("<")) { 922 name = name.substring(1, name.length() - 1); 923 } 924 925 name = StringUtil.replace(name, ".model.", "."); 926 927 return getEntity(name); 928 } 929 catch (Exception e) { 930 return null; 931 } 932 } 933 934 public Entity getEntityByParameterTypeValue(String parameterTypeValue) { 935 try { 936 String name = parameterTypeValue; 937 938 name = StringUtil.replace(name, ".model.", "."); 939 940 return getEntity(name); 941 } 942 catch (Exception e) { 943 return null; 944 } 945 } 946 947 public EntityMapping getEntityMapping(String mappingTable) { 948 return _entityMappings.get(mappingTable); 949 } 950 951 public String getGeneratorClass(String idType) { 952 if (Validator.isNull(idType)) { 953 idType = "assigned"; 954 } 955 956 return idType; 957 } 958 959 public String getJavadocComment(JavaClass javaClass) { 960 return _formatComment( 961 javaClass.getComment(), javaClass.getTags(), StringPool.BLANK); 962 } 963 964 public String getJavadocComment(JavaMethod javaMethod) { 965 return _formatComment( 966 javaMethod.getComment(), javaMethod.getTags(), StringPool.TAB); 967 } 968 969 public String getListActualTypeArguments(Type type) { 970 if (type.getValue().equals("java.util.List")) { 971 Type[] types = type.getActualTypeArguments(); 972 973 if (types != null) { 974 return getTypeGenericsName(types[0]); 975 } 976 } 977 978 return getTypeGenericsName(type); 979 } 980 981 public String getLiteralClass(Type type) { 982 StringBundler sb = new StringBundler(type.getDimensions() + 2); 983 984 sb.append(type.getValue()); 985 986 for (int i = 0; i < type.getDimensions(); i++) { 987 sb.append("[]"); 988 } 989 990 sb.append(".class"); 991 992 return sb.toString(); 993 } 994 995 public List<EntityColumn> getMappingEntities(String mappingTable) 996 throws IOException { 997 998 List<EntityColumn> mappingEntitiesPKList = 999 new ArrayList<EntityColumn>(); 1000 1001 EntityMapping entityMapping = _entityMappings.get(mappingTable); 1002 1003 for (int i = 0; i < 2; i++) { 1004 Entity entity = getEntity(entityMapping.getEntity(i)); 1005 1006 if (entity == null) { 1007 return null; 1008 } 1009 1010 mappingEntitiesPKList.addAll(entity.getPKList()); 1011 } 1012 1013 return mappingEntitiesPKList; 1014 } 1015 1016 public String getNoSuchEntityException(Entity entity) { 1017 String noSuchEntityException = entity.getName(); 1018 1019 if (Validator.isNull(entity.getPortletShortName()) || 1020 (noSuchEntityException.startsWith(entity.getPortletShortName()) && 1021 !noSuchEntityException.equals(entity.getPortletShortName()))) { 1022 1023 noSuchEntityException = noSuchEntityException.substring( 1024 entity.getPortletShortName().length()); 1025 } 1026 1027 noSuchEntityException = "NoSuch" + noSuchEntityException; 1028 1029 return noSuchEntityException; 1030 } 1031 1032 public String getParameterType(JavaParameter parameter) { 1033 Type returnType = parameter.getType(); 1034 1035 return getTypeGenericsName(returnType); 1036 } 1037 1038 public String getPrimitiveObj(String type) { 1039 if (type.equals("boolean")) { 1040 return "Boolean"; 1041 } 1042 else if (type.equals("double")) { 1043 return "Double"; 1044 } 1045 else if (type.equals("float")) { 1046 return "Float"; 1047 } 1048 else if (type.equals("int")) { 1049 return "Integer"; 1050 } 1051 else if (type.equals("long")) { 1052 return "Long"; 1053 } 1054 else if (type.equals("short")) { 1055 return "Short"; 1056 } 1057 else { 1058 return type; 1059 } 1060 } 1061 1062 public String getPrimitiveObjValue(String colType) { 1063 if (colType.equals("Boolean")) { 1064 return ".booleanValue()"; 1065 } 1066 else if (colType.equals("Double")) { 1067 return ".doubleValue()"; 1068 } 1069 else if (colType.equals("Float")) { 1070 return ".floatValue()"; 1071 } 1072 else if (colType.equals("Integer")) { 1073 return ".intValue()"; 1074 } 1075 else if (colType.equals("Long")) { 1076 return ".longValue()"; 1077 } 1078 else if (colType.equals("Short")) { 1079 return ".shortValue()"; 1080 } 1081 1082 return StringPool.BLANK; 1083 } 1084 1085 public String getReturnType(JavaMethod method) { 1086 Type returnType = method.getReturns(); 1087 1088 return getTypeGenericsName(returnType); 1089 } 1090 1091 public List<String> getServiceBaseExceptions( 1092 List<JavaMethod> methods, String methodName, List<String> args, 1093 List<String> exceptions) { 1094 1095 boolean foundMethod = false; 1096 1097 for (JavaMethod method : methods) { 1098 JavaParameter[] parameters = method.getParameters(); 1099 1100 if ((method.getName().equals(methodName)) && 1101 (parameters.length == args.size())) { 1102 1103 for (int i = 0; i < parameters.length; i++) { 1104 JavaParameter parameter = parameters[i]; 1105 1106 String arg = args.get(i); 1107 1108 if (getParameterType(parameter).equals(arg)) { 1109 exceptions = ListUtil.copy(exceptions); 1110 1111 Type[] methodExceptions = method.getExceptions(); 1112 1113 for (Type methodException : methodExceptions) { 1114 String exception = methodException.getValue(); 1115 1116 if (exception.equals( 1117 PortalException.class.getName())) { 1118 1119 exception = "PortalException"; 1120 } 1121 1122 if (exception.equals( 1123 SystemException.class.getName())) { 1124 1125 exception = "SystemException"; 1126 } 1127 1128 if (!exceptions.contains(exception)) { 1129 exceptions.add(exception); 1130 } 1131 } 1132 1133 Collections.sort(exceptions); 1134 1135 foundMethod = true; 1136 1137 break; 1138 } 1139 } 1140 } 1141 1142 if (foundMethod) { 1143 break; 1144 } 1145 } 1146 1147 if (!exceptions.isEmpty()) { 1148 return exceptions; 1149 } 1150 else { 1151 return Collections.emptyList(); 1152 } 1153 } 1154 1155 public String getSqlType(String type) { 1156 if (type.equals("boolean") || type.equals("Boolean")) { 1157 return "BOOLEAN"; 1158 } 1159 else if (type.equals("double") || type.equals("Double")) { 1160 return "DOUBLE"; 1161 } 1162 else if (type.equals("float") || type.equals("Float")) { 1163 return "FLOAT"; 1164 } 1165 else if (type.equals("int") || type.equals("Integer")) { 1166 return "INTEGER"; 1167 } 1168 else if (type.equals("long") || type.equals("Long")) { 1169 return "BIGINT"; 1170 } 1171 else if (type.equals("short") || type.equals("Short")) { 1172 return "INTEGER"; 1173 } 1174 else if (type.equals("Date")) { 1175 return "TIMESTAMP"; 1176 } 1177 else { 1178 return null; 1179 } 1180 } 1181 1182 public String getSqlType(String model, String field, String type) { 1183 if (type.equals("boolean") || type.equals("Boolean")) { 1184 return "BOOLEAN"; 1185 } 1186 else if (type.equals("double") || type.equals("Double")) { 1187 return "DOUBLE"; 1188 } 1189 else if (type.equals("float") || type.equals("Float")) { 1190 return "FLOAT"; 1191 } 1192 else if (type.equals("int") || type.equals("Integer")) { 1193 return "INTEGER"; 1194 } 1195 else if (type.equals("long") || type.equals("Long")) { 1196 return "BIGINT"; 1197 } 1198 else if (type.equals("short") || type.equals("Short")) { 1199 return "INTEGER"; 1200 } 1201 else if (type.equals("Blob")) { 1202 return "BLOB"; 1203 } 1204 else if (type.equals("Date")) { 1205 return "TIMESTAMP"; 1206 } 1207 else if (type.equals("String")) { 1208 Map<String, String> hints = ModelHintsUtil.getHints(model, field); 1209 1210 if (hints != null) { 1211 int maxLength = GetterUtil.getInteger(hints.get("max-length")); 1212 1213 if (maxLength == 2000000) { 1214 return "CLOB"; 1215 } 1216 } 1217 1218 return "VARCHAR"; 1219 } 1220 else { 1221 return null; 1222 } 1223 } 1224 1225 public String getTypeGenericsName(Type type) { 1226 StringBundler sb = new StringBundler(); 1227 1228 sb.append(type.getValue()); 1229 1230 Type[] actualTypeArguments = type.getActualTypeArguments(); 1231 1232 if (actualTypeArguments != null) { 1233 sb.append(StringPool.LESS_THAN); 1234 1235 for (int i = 0; i < actualTypeArguments.length; i++) { 1236 if (i > 0) { 1237 sb.append(", "); 1238 } 1239 1240 sb.append(getTypeGenericsName(actualTypeArguments[i])); 1241 } 1242 1243 sb.append(StringPool.GREATER_THAN); 1244 } 1245 1246 sb.append(getDimensions(type.getDimensions())); 1247 1248 return sb.toString(); 1249 } 1250 1251 public String getVariableName(JavaField field) { 1252 String fieldName = field.getName(); 1253 1254 if ((fieldName.length() > 0) && (fieldName.charAt(0) == '_')) { 1255 fieldName = fieldName.substring(1); 1256 } 1257 1258 return fieldName; 1259 } 1260 1261 public boolean hasEntityByGenericsName(String genericsName) { 1262 if (Validator.isNull(genericsName)) { 1263 return false; 1264 } 1265 1266 if (genericsName.indexOf(".model.") == -1) { 1267 return false; 1268 } 1269 1270 if (getEntityByGenericsName(genericsName) == null) { 1271 return false; 1272 } 1273 else { 1274 return true; 1275 } 1276 } 1277 1278 public boolean hasEntityByParameterTypeValue(String parameterTypeValue) { 1279 if (Validator.isNull(parameterTypeValue)) { 1280 return false; 1281 } 1282 1283 if (parameterTypeValue.indexOf(".model.") == -1) { 1284 return false; 1285 } 1286 1287 if (getEntityByParameterTypeValue(parameterTypeValue) == null) { 1288 return false; 1289 } 1290 else { 1291 return true; 1292 } 1293 } 1294 1295 public boolean isBasePersistenceMethod(JavaMethod method) { 1296 String methodName = method.getName(); 1297 1298 if (methodName.equals("clearCache") || 1299 methodName.equals("findWithDynamicQuery")) { 1300 1301 return true; 1302 } 1303 else if (methodName.equals("findByPrimaryKey") || 1304 methodName.equals("fetchByPrimaryKey") || 1305 methodName.equals("remove")) { 1306 1307 JavaParameter[] parameters = method.getParameters(); 1308 1309 if ((parameters.length == 1) && 1310 (parameters[0].getName().equals("primaryKey"))) { 1311 1312 return true; 1313 } 1314 1315 if (methodName.equals("remove")) { 1316 Type[] methodExceptions = method.getExceptions(); 1317 1318 for (Type methodException : methodExceptions) { 1319 String exception = methodException.getValue(); 1320 1321 if (exception.contains("NoSuch")) { 1322 return false; 1323 } 1324 } 1325 1326 return true; 1327 } 1328 } 1329 1330 return false; 1331 } 1332 1333 public boolean isCustomMethod(JavaMethod method) { 1334 String methodName = method.getName(); 1335 1336 if (methodName.equals("afterPropertiesSet") || 1337 methodName.equals("destroy") || 1338 methodName.equals("equals") || 1339 methodName.equals("getClass") || 1340 methodName.equals("hashCode") || 1341 methodName.equals("notify") || 1342 methodName.equals("notifyAll") || 1343 methodName.equals("toString") || 1344 methodName.equals("wait")) { 1345 1346 return false; 1347 } 1348 else if (methodName.equals("getPermissionChecker")) { 1349 return false; 1350 } 1351 else if ((methodName.equals("getUser")) && 1352 (method.getParameters().length == 0)) { 1353 1354 return false; 1355 } 1356 else if (methodName.equals("getUserId") && 1357 (method.getParameters().length == 0)) { 1358 1359 return false; 1360 } 1361 else if ((methodName.endsWith("Finder")) && 1362 (methodName.startsWith("get") || 1363 methodName.startsWith("set"))) { 1364 1365 return false; 1366 } 1367 else if ((methodName.endsWith("Persistence")) && 1368 (methodName.startsWith("get") || 1369 methodName.startsWith("set"))) { 1370 1371 return false; 1372 } 1373 else if ((methodName.endsWith("Service")) && 1374 (methodName.startsWith("get") || 1375 methodName.startsWith("set"))) { 1376 1377 return false; 1378 } 1379 else { 1380 return true; 1381 } 1382 } 1383 1384 public boolean isDuplicateMethod( 1385 JavaMethod method, Map<String, Object> tempMap) { 1386 1387 StringBundler sb = new StringBundler(); 1388 1389 sb.append("isDuplicateMethod "); 1390 sb.append(getTypeGenericsName(method.getReturns())); 1391 sb.append(StringPool.SPACE); 1392 sb.append(method.getName()); 1393 sb.append(StringPool.OPEN_PARENTHESIS); 1394 1395 JavaParameter[] parameters = method.getParameters(); 1396 1397 for (int i = 0; i < parameters.length; i++) { 1398 JavaParameter javaParameter = parameters[i]; 1399 1400 sb.append(getTypeGenericsName(javaParameter.getType())); 1401 1402 if ((i + 1) != parameters.length) { 1403 sb.append(StringPool.COMMA); 1404 } 1405 } 1406 1407 sb.append(StringPool.CLOSE_PARENTHESIS); 1408 1409 String key = sb.toString(); 1410 1411 if (tempMap.put(key, key) != null) { 1412 return true; 1413 } 1414 else { 1415 return false; 1416 } 1417 } 1418 1419 public boolean isHBMCamelCasePropertyAccessor(String propertyName) { 1420 if (propertyName.length() < 3) { 1421 return false; 1422 } 1423 1424 char[] chars = propertyName.toCharArray(); 1425 1426 char c0 = chars[0]; 1427 char c1 = chars[1]; 1428 char c2 = chars[2]; 1429 1430 if (Character.isLowerCase(c0) && Character.isUpperCase(c1) && 1431 Character.isLowerCase(c2)) { 1432 1433 return true; 1434 } 1435 1436 return false; 1437 } 1438 1439 public boolean isReadOnlyMethod( 1440 JavaMethod method, List<String> txRequiredList, String[] prefixes) { 1441 1442 String methodName = method.getName(); 1443 1444 if (isTxRequiredMethod(method, txRequiredList)) { 1445 return false; 1446 } 1447 1448 for (String prefix : prefixes) { 1449 if (methodName.startsWith(prefix)) { 1450 return true; 1451 } 1452 } 1453 1454 return false; 1455 } 1456 1457 public boolean isServiceReadOnlyMethod( 1458 JavaMethod method, List<String> txRequiredList) { 1459 1460 return isReadOnlyMethod( 1461 method, txRequiredList, 1462 PropsValues.SERVICE_BUILDER_SERVICE_READ_ONLY_PREFIXES); 1463 } 1464 1465 public boolean isSoapMethod(JavaMethod method) { 1466 Type returnType = method.getReturns(); 1467 1468 String returnTypeGenericsName = getTypeGenericsName(returnType); 1469 String returnValueName = returnType.getValue(); 1470 1471 if (returnTypeGenericsName.contains( 1472 "com.liferay.portal.kernel.repository.") || 1473 returnTypeGenericsName.contains( 1474 "com.liferay.portal.kernel.search.") || 1475 returnTypeGenericsName.contains("com.liferay.portal.model.Theme") || 1476 returnTypeGenericsName.contains( 1477 "com.liferay.portlet.social.model.SocialActivityDefinition") || 1478 returnTypeGenericsName.equals("java.util.List<java.lang.Object>") || 1479 returnValueName.equals("com.liferay.portal.model.Lock") || 1480 returnValueName.equals( 1481 "com.liferay.portlet.messageboards.model.MBMessageDisplay") || 1482 returnValueName.startsWith("java.io") || 1483 returnValueName.equals("java.util.Map") || 1484 returnValueName.equals("java.util.Properties") || 1485 returnValueName.startsWith("javax")) { 1486 1487 return false; 1488 } 1489 1490 JavaParameter[] parameters = method.getParameters(); 1491 1492 for (JavaParameter javaParameter : parameters) { 1493 String parameterTypeName = 1494 javaParameter.getType().getValue() + 1495 _getDimensions(javaParameter.getType()); 1496 1497 if (parameterTypeName.equals( 1498 "com.liferay.portal.kernel.util.UnicodeProperties") || 1499 parameterTypeName.equals( 1500 "com.liferay.portal.theme.ThemeDisplay") || 1501 parameterTypeName.equals( 1502 "com.liferay.portlet.PortletPreferencesImpl") || 1503 parameterTypeName.equals( 1504 "com.liferay.portlet.dynamicdatamapping.storage.Fields") || 1505 parameterTypeName.startsWith("java.io") || 1506 //parameterTypeName.startsWith("java.util.List") || 1507 //parameterTypeName.startsWith("java.util.Locale") || 1508 parameterTypeName.startsWith("java.util.Map") || 1509 parameterTypeName.startsWith("java.util.Properties") || 1510 parameterTypeName.startsWith("javax")) { 1511 1512 return false; 1513 } 1514 } 1515 1516 return true; 1517 } 1518 1519 public boolean isTxRequiredMethod( 1520 JavaMethod method, List<String> txRequiredList) { 1521 1522 if (txRequiredList == null) { 1523 return false; 1524 } 1525 1526 String methodName = method.getName(); 1527 1528 for (String txRequired : txRequiredList) { 1529 if (methodName.equals(txRequired)) { 1530 return true; 1531 } 1532 } 1533 1534 return false; 1535 } 1536 1537 private static String _getPackagePath(File file) { 1538 String fileName = StringUtil.replace(file.toString(), "\\", "/"); 1539 1540 int x = fileName.indexOf("src/"); 1541 1542 if (x == -1) { 1543 x = fileName.indexOf("test/"); 1544 } 1545 1546 int y = fileName.lastIndexOf("/"); 1547 1548 fileName = fileName.substring(x + 4, y); 1549 1550 return StringUtil.replace(fileName, "/", "."); 1551 } 1552 1553 private void _addElements( 1554 Element element, Map<String, Element> elements) { 1555 1556 for (Map.Entry<String, Element> entry : elements.entrySet()) { 1557 Element childElement = entry.getValue(); 1558 1559 element.add(childElement); 1560 } 1561 } 1562 1563 private void _createBlobModels(Entity entity) throws Exception { 1564 List<EntityColumn> blobList = new ArrayList<EntityColumn>( 1565 entity.getBlobList()); 1566 1567 Iterator<EntityColumn> itr = blobList.iterator(); 1568 1569 while (itr.hasNext()) { 1570 EntityColumn col = itr.next(); 1571 1572 if (!col.isLazy()) { 1573 itr.remove(); 1574 } 1575 } 1576 1577 if (blobList.isEmpty()) { 1578 return; 1579 } 1580 1581 Map<String, Object> context = _getContext(); 1582 1583 context.put("entity", entity); 1584 1585 for (EntityColumn col : blobList) { 1586 context.put("column", col); 1587 1588 // Content 1589 1590 String content = _processTemplate(_tplBlobModel, context); 1591 1592 // Write file 1593 1594 File blobModelFile = new File( 1595 _serviceOutputPath + "/model/" + entity.getName() + 1596 col.getMethodName() + "BlobModel.java"); 1597 1598 writeFile(blobModelFile, content, _author); 1599 } 1600 } 1601 1602 private void _createEJBPK(Entity entity) throws Exception { 1603 Map<String, Object> context = _getContext(); 1604 1605 context.put("entity", entity); 1606 1607 // Content 1608 1609 String content = _processTemplate(_tplEjbPk, context); 1610 1611 // Write file 1612 1613 File ejbFile = new File( 1614 _serviceOutputPath + "/service/persistence/" + 1615 entity.getPKClassName() + ".java"); 1616 1617 writeFile(ejbFile, content, _author); 1618 } 1619 1620 private void _createExceptions(List<String> exceptions) throws Exception { 1621 for (int i = 0; i < _ejbList.size(); i++) { 1622 Entity entity = _ejbList.get(i); 1623 1624 if (entity.hasColumns()) { 1625 exceptions.add(getNoSuchEntityException(entity)); 1626 } 1627 } 1628 1629 for (String exception : exceptions) { 1630 File exceptionFile = new File( 1631 _serviceOutputPath + "/" + exception + "Exception.java"); 1632 1633 if (!exceptionFile.exists()) { 1634 Map<String, Object> context = _getContext(); 1635 1636 context.put("exception", exception); 1637 1638 String content = _processTemplate(_tplException, context); 1639 1640 if (exception.startsWith("NoSuch")) { 1641 content = StringUtil.replace( 1642 content, "PortalException", "NoSuchModelException"); 1643 content = StringUtil.replace( 1644 content, "kernel.exception.NoSuchModelException", 1645 "NoSuchModelException"); 1646 } 1647 1648 content = StringUtil.replace(content, "\r\n", "\n"); 1649 1650 FileUtil.write(exceptionFile, content); 1651 } 1652 1653 if (exception.startsWith("NoSuch")) { 1654 String content = FileUtil.read(exceptionFile); 1655 1656 if (!content.contains("NoSuchModelException")) { 1657 content = StringUtil.replace( 1658 content, "PortalException", "NoSuchModelException"); 1659 content = StringUtil.replace( 1660 content, "kernel.exception.NoSuchModelException", 1661 "NoSuchModelException"); 1662 1663 FileUtil.write(exceptionFile, content); 1664 } 1665 } 1666 1667 if (!_serviceOutputPath.equals(_outputPath)) { 1668 exceptionFile = new File( 1669 _outputPath + "/" + exception + "Exception.java"); 1670 1671 if (exceptionFile.exists()) { 1672 System.out.println("Relocating " + exceptionFile); 1673 1674 exceptionFile.delete(); 1675 } 1676 } 1677 } 1678 } 1679 1680 private void _createExtendedModel(Entity entity) throws Exception { 1681 JavaClass javaClass = _getJavaClass( 1682 _outputPath + "/model/impl/" + entity.getName() + "Impl.java"); 1683 1684 Map<String, Object> context = _getContext(); 1685 1686 context.put("entity", entity); 1687 context.put("methods", _getMethods(javaClass)); 1688 1689 // Content 1690 1691 String content = _processTemplate(_tplExtendedModel, context); 1692 1693 // Write file 1694 1695 File modelFile = new File( 1696 _serviceOutputPath + "/model/" + entity.getName() + ".java"); 1697 1698 writeFile(modelFile, content, _author); 1699 1700 if (!_serviceOutputPath.equals(_outputPath)) { 1701 modelFile = new File( 1702 _outputPath + "/model/" + entity.getName() + ".java"); 1703 1704 if (modelFile.exists()) { 1705 System.out.println("Relocating " + modelFile); 1706 1707 modelFile.delete(); 1708 } 1709 } 1710 } 1711 1712 private void _createExtendedModelBaseImpl(Entity entity) throws Exception { 1713 Map<String, Object> context = _getContext(); 1714 1715 context.put("entity", entity); 1716 1717 // Content 1718 1719 String content = _processTemplate(_tplExtendedModelBaseImpl, context); 1720 1721 // Write file 1722 1723 File modelFile = new File( 1724 _outputPath + "/model/impl/" + entity.getName() + "BaseImpl.java"); 1725 1726 writeFile(modelFile, content, _author); 1727 } 1728 1729 private void _createExtendedModelImpl(Entity entity) throws Exception { 1730 Map<String, Object> context = _getContext(); 1731 1732 context.put("entity", entity); 1733 1734 // Content 1735 1736 String content = _processTemplate(_tplExtendedModelImpl, context); 1737 1738 // Write file 1739 1740 File modelFile = new File( 1741 _outputPath + "/model/impl/" + entity.getName() + "Impl.java"); 1742 1743 if (modelFile.exists()) { 1744 content = FileUtil.read(modelFile); 1745 1746 content = content.replaceAll( 1747 "extends\\s+" + entity.getName() + 1748 "ModelImpl\\s+implements\\s+" + entity.getName(), 1749 "extends " + entity.getName() + "BaseImpl"); 1750 1751 writeFileRaw(modelFile, content); 1752 } 1753 else { 1754 writeFile(modelFile, content, _author); 1755 } 1756 } 1757 1758 private void _createFinder(Entity entity) throws Exception { 1759 if (!entity.hasFinderClass()) { 1760 return; 1761 } 1762 1763 JavaClass javaClass = _getJavaClass( 1764 _outputPath + "/service/persistence/" + entity.getName() + 1765 "FinderImpl.java"); 1766 1767 Map<String, Object> context = _getContext(); 1768 1769 context.put("entity", entity); 1770 context.put("methods", _getMethods(javaClass)); 1771 1772 // Content 1773 1774 String content = _processTemplate(_tplFinder, context); 1775 1776 // Write file 1777 1778 File ejbFile = new File( 1779 _serviceOutputPath + "/service/persistence/" + entity.getName() + 1780 "Finder.java"); 1781 1782 writeFile(ejbFile, content, _author); 1783 1784 if (!_serviceOutputPath.equals(_outputPath)) { 1785 ejbFile = new File( 1786 _outputPath + "/service/persistence/" + entity.getName() + 1787 "Finder.java"); 1788 1789 if (ejbFile.exists()) { 1790 System.out.println("Relocating " + ejbFile); 1791 1792 ejbFile.delete(); 1793 } 1794 } 1795 } 1796 1797 private void _createFinderUtil(Entity entity) throws Exception { 1798 if (!entity.hasFinderClass()) { 1799 return; 1800 } 1801 1802 JavaClass javaClass = _getJavaClass( 1803 _outputPath + "/service/persistence/" + entity.getName() + 1804 "FinderImpl.java"); 1805 1806 Map<String, Object> context = _getContext(); 1807 1808 context.put("entity", entity); 1809 context.put("methods", _getMethods(javaClass)); 1810 1811 // Content 1812 1813 String content = _processTemplate(_tplFinderUtil, context); 1814 1815 // Write file 1816 1817 File ejbFile = new File( 1818 _serviceOutputPath + "/service/persistence/" + entity.getName() + 1819 "FinderUtil.java"); 1820 1821 writeFile(ejbFile, content, _author); 1822 1823 if (!_serviceOutputPath.equals(_outputPath)) { 1824 ejbFile = new File( 1825 _outputPath + "/service/persistence/" + entity.getName() + 1826 "FinderUtil.java"); 1827 1828 if (ejbFile.exists()) { 1829 System.out.println("Relocating " + ejbFile); 1830 1831 ejbFile.delete(); 1832 } 1833 } 1834 } 1835 1836 private void _createHbm(Entity entity) { 1837 File ejbFile = new File( 1838 _outputPath + "/service/persistence/" + entity.getName() + 1839 "HBM.java"); 1840 1841 if (ejbFile.exists()) { 1842 System.out.println("Removing deprecated " + ejbFile); 1843 1844 ejbFile.delete(); 1845 } 1846 } 1847 1848 private void _createHbmUtil(Entity entity) { 1849 File ejbFile = new File( 1850 _outputPath + "/service/persistence/" + entity.getName() + 1851 "HBMUtil.java"); 1852 1853 if (ejbFile.exists()) { 1854 System.out.println("Removing deprecated " + ejbFile); 1855 1856 ejbFile.delete(); 1857 } 1858 } 1859 1860 private void _createHbmXml() throws Exception { 1861 Map<String, Object> context = _getContext(); 1862 1863 context.put("entities", _ejbList); 1864 1865 // Content 1866 1867 String content = _processTemplate(_tplHbmXml, context); 1868 1869 int lastImportStart = content.lastIndexOf("<import class="); 1870 int lastImportEnd = content.indexOf("/>", lastImportStart) + 3; 1871 1872 String imports = content.substring(0, lastImportEnd); 1873 1874 content = content.substring(lastImportEnd + 1); 1875 1876 File xmlFile = new File(_hbmFileName); 1877 1878 if (!xmlFile.exists()) { 1879 String xml = 1880 "<?xml version=\"1.0\"?>\n" + 1881 "<!DOCTYPE hibernate-mapping PUBLIC \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n" + 1882 "\n" + 1883 "<hibernate-mapping default-lazy=\"false\" auto-import=\"false\">\n" + 1884 "</hibernate-mapping>"; 1885 1886 FileUtil.write(xmlFile, xml); 1887 } 1888 1889 String oldContent = FileUtil.read(xmlFile); 1890 String newContent = _fixHbmXml(oldContent); 1891 1892 int firstImport = newContent.indexOf( 1893 "<import class=\"" + _packagePath + ".model."); 1894 int lastImport = newContent.lastIndexOf( 1895 "<import class=\"" + _packagePath + ".model."); 1896 1897 if (firstImport == -1) { 1898 int x = newContent.indexOf("<class"); 1899 1900 if (x != -1) { 1901 newContent = 1902 newContent.substring(0, x) + imports + 1903 newContent.substring(x); 1904 } 1905 else { 1906 content = imports + content; 1907 } 1908 } 1909 else { 1910 firstImport = newContent.indexOf("<import", firstImport) - 1; 1911 lastImport = newContent.indexOf("/>", lastImport) + 3; 1912 1913 newContent = 1914 newContent.substring(0, firstImport) + imports + 1915 newContent.substring(lastImport); 1916 } 1917 1918 int firstClass = newContent.indexOf( 1919 "<class name=\"" + _packagePath + ".model.impl."); 1920 int lastClass = newContent.lastIndexOf( 1921 "<class name=\"" + _packagePath + ".model.impl."); 1922 1923 if (firstClass == -1) { 1924 int x = newContent.indexOf("</hibernate-mapping>"); 1925 1926 if (x != -1) { 1927 newContent = 1928 newContent.substring(0, x) + content + 1929 newContent.substring(x, newContent.length()); 1930 } 1931 } 1932 else { 1933 firstClass = newContent.lastIndexOf("<class", firstClass) - 1; 1934 lastClass = newContent.indexOf("</class>", lastClass) + 9; 1935 1936 newContent = 1937 newContent.substring(0, firstClass) + content + 1938 newContent.substring(lastClass, newContent.length()); 1939 } 1940 1941 newContent = _formatXml(newContent); 1942 1943 if (!oldContent.equals(newContent)) { 1944 FileUtil.write(xmlFile, newContent); 1945 } 1946 } 1947 1948 private void _createJsonJs() throws Exception { 1949 if (_packagePath.equals("com.liferay.counter")) { 1950 return; 1951 } 1952 1953 if (Validator.isNotNull(_pluginName)) { 1954 boolean hasRemoteService = false; 1955 1956 for (int i = 0; i < _ejbList.size(); i++) { 1957 Entity entity = _ejbList.get(i); 1958 1959 if (entity.hasRemoteService()) { 1960 hasRemoteService = true; 1961 1962 break; 1963 } 1964 } 1965 1966 if (!hasRemoteService) { 1967 return; 1968 } 1969 } 1970 1971 StringBundler sb = new StringBundler(); 1972 1973 if (_ejbList.size() > 0) { 1974 sb.append(_processTemplate(_tplJsonJs)); 1975 } 1976 1977 for (int i = 0; i < _ejbList.size(); i++) { 1978 Entity entity = _ejbList.get(i); 1979 1980 if (entity.hasRemoteService()) { 1981 JavaClass javaClass = _getJavaClass( 1982 _serviceOutputPath + "/service/" + entity.getName() + 1983 "Service.java"); 1984 1985 JavaMethod[] methods = _getMethods(javaClass); 1986 1987 Set<String> jsonMethods = new LinkedHashSet<String>(); 1988 1989 for (JavaMethod method : methods) { 1990 String methodName = method.getName(); 1991 String returnValue = getReturnType(method); 1992 1993 boolean badJsonType = false; 1994 1995 for (JavaParameter parameter: method.getParameters()) { 1996 String parameterType = getParameterType(parameter); 1997 1998 if (_badJsonTypes.contains(parameterType)) { 1999 badJsonType = true; 2000 } 2001 } 2002 2003 if (method.isPublic() && 2004 !_badJsonTypes.contains(returnValue) && !badJsonType) { 2005 2006 jsonMethods.add(methodName); 2007 } 2008 } 2009 2010 if (jsonMethods.size() > 0) { 2011 Map<String, Object> context = _getContext(); 2012 2013 context.put("entity", entity); 2014 context.put("methods", jsonMethods); 2015 2016 sb.append("\n\n"); 2017 sb.append(_processTemplate(_tplJsonJsMethod, context)); 2018 } 2019 } 2020 } 2021 2022 File jsonFile = new File(_jsonFileName); 2023 2024 if (!jsonFile.exists()) { 2025 FileUtil.write(jsonFile, ""); 2026 } 2027 2028 String oldContent = FileUtil.read(jsonFile); 2029 String newContent = oldContent; 2030 2031 int oldBegin = oldContent.indexOf( 2032 "Liferay.Service.register(\"Liferay.Service." + _portletShortName); 2033 2034 int oldEnd = oldContent.lastIndexOf( 2035 "Liferay.Service." + _portletShortName); 2036 2037 oldEnd = oldContent.indexOf(");", oldEnd); 2038 2039 int newBegin = newContent.indexOf( 2040 "Liferay.Service.register(\"Liferay.Service." + _portletShortName); 2041 2042 int newEnd = newContent.lastIndexOf( 2043 "Liferay.Service." + _portletShortName); 2044 2045 newEnd = newContent.indexOf(");", newEnd); 2046 2047 if (newBegin == -1) { 2048 newContent = oldContent + "\n\n" + sb.toString().trim(); 2049 } 2050 else { 2051 newContent = 2052 newContent.substring(0, oldBegin) + sb.toString().trim() + 2053 newContent.substring(oldEnd + 2, newContent.length()); 2054 } 2055 2056 newContent = newContent.trim(); 2057 2058 if (!oldContent.equals(newContent)) { 2059 FileUtil.write(jsonFile, newContent); 2060 } 2061 } 2062 2063 private void _createModel(Entity entity) throws Exception { 2064 Map<String, Object> context = _getContext(); 2065 2066 context.put("entity", entity); 2067 2068 // Content 2069 2070 String content = _processTemplate(_tplModel, context); 2071 2072 // Write file 2073 2074 File modelFile = new File( 2075 _serviceOutputPath + "/model/" + entity.getName() + "Model.java"); 2076 2077 writeFile(modelFile, content, _author); 2078 2079 if (!_serviceOutputPath.equals(_outputPath)) { 2080 modelFile = new File( 2081 _outputPath + "/model/" + entity.getName() + "Model.java"); 2082 2083 if (modelFile.exists()) { 2084 System.out.println("Relocating " + modelFile); 2085 2086 modelFile.delete(); 2087 } 2088 } 2089 } 2090 2091 private void _createModelCache(Entity entity) throws Exception { 2092 JavaClass javaClass = _getJavaClass( 2093 _outputPath + "/model/impl/" + entity.getName() + "Impl.java"); 2094 2095 Map<String, Object> context = _getContext(); 2096 2097 context.put("entity", entity); 2098 context.put("cacheFields", _getCacheFields(javaClass)); 2099 2100 // Content 2101 2102 String content = _processTemplate(_tplModelCache, context); 2103 2104 // Write file 2105 2106 File modelFile = new File( 2107 _outputPath + "/model/impl/" + entity.getName() + 2108 "CacheModel.java"); 2109 2110 writeFile(modelFile, content, _author); 2111 } 2112 2113 private void _createModelClp(Entity entity) throws Exception { 2114 if (Validator.isNull(_pluginName)) { 2115 return; 2116 } 2117 2118 JavaClass javaClass = _getJavaClass( 2119 _outputPath + "/model/impl/" + entity.getName() + "Impl.java"); 2120 2121 Map<String, Object> context = _getContext(); 2122 2123 context.put("entity", entity); 2124 context.put("methods", _getMethods(javaClass)); 2125 2126 // Content 2127 2128 String content = _processTemplate(_tplModelClp, context); 2129 2130 // Write file 2131 2132 File modelFile = new File( 2133 _serviceOutputPath + "/model/" + entity.getName() + "Clp.java"); 2134 2135 writeFile(modelFile, content, _author); 2136 } 2137 2138 private void _createModelHintsXml() throws Exception { 2139 Map<String, Object> context = _getContext(); 2140 2141 context.put("entities", _ejbList); 2142 2143 // Content 2144 2145 String content = _processTemplate(_tplModelHintsXml, context); 2146 2147 File xmlFile = new File(_modelHintsFileName); 2148 2149 if (!xmlFile.exists()) { 2150 String xml = 2151 "<?xml version=\"1.0\"?>\n" + 2152 "\n" + 2153 "<model-hints>\n" + 2154 "</model-hints>"; 2155 2156 FileUtil.write(xmlFile, xml); 2157 } 2158 2159 String oldContent = FileUtil.read(xmlFile); 2160 String newContent = oldContent; 2161 2162 int firstModel = newContent.indexOf( 2163 "<model name=\"" + _packagePath + ".model."); 2164 int lastModel = newContent.lastIndexOf( 2165 "<model name=\"" + _packagePath + ".model."); 2166 2167 if (firstModel == -1) { 2168 int x = newContent.indexOf("</model-hints>"); 2169 2170 newContent = 2171 newContent.substring(0, x) + content + 2172 newContent.substring(x, newContent.length()); 2173 } 2174 else { 2175 firstModel = newContent.lastIndexOf("<model", firstModel) - 1; 2176 lastModel = newContent.indexOf("</model>", lastModel) + 9; 2177 2178 newContent = 2179 newContent.substring(0, firstModel) + content + 2180 newContent.substring(lastModel, newContent.length()); 2181 } 2182 2183 newContent = _formatXml(newContent); 2184 2185 if (!oldContent.equals(newContent)) { 2186 FileUtil.write(xmlFile, newContent); 2187 } 2188 } 2189 2190 private void _createModelImpl(Entity entity) throws Exception { 2191 JavaClass javaClass = _getJavaClass( 2192 _outputPath + "/model/impl/" + entity.getName() + "Impl.java"); 2193 2194 Map<String, Object> context = _getContext(); 2195 2196 context.put("entity", entity); 2197 context.put("cacheFields", _getCacheFields(javaClass)); 2198 2199 // Content 2200 2201 String content = _processTemplate(_tplModelImpl, context); 2202 2203 // Write file 2204 2205 File modelFile = new File( 2206 _outputPath + "/model/impl/" + entity.getName() + "ModelImpl.java"); 2207 2208 writeFile(modelFile, content, _author); 2209 } 2210 2211 private void _createModelSoap(Entity entity) throws Exception { 2212 File modelFile = null; 2213 2214 if (!_serviceOutputPath.equals(_outputPath)) { 2215 modelFile = new File( 2216 _outputPath + "/model/" + entity.getName() + "Soap.java"); 2217 2218 if (modelFile.exists()) { 2219 System.out.println("Relocating " + modelFile); 2220 2221 modelFile.delete(); 2222 } 2223 } 2224 2225 modelFile = new File( 2226 _serviceOutputPath + "/model/" + entity.getName() + "Soap.java"); 2227 2228 Map<String, Object> context = _getContext(); 2229 2230 context.put("entity", entity); 2231 2232 // Content 2233 2234 String content = _processTemplate(_tplModelSoap, context); 2235 2236 // Write file 2237 2238 writeFile(modelFile, content, _author); 2239 } 2240 2241 private void _createModelWrapper(Entity entity) throws Exception { 2242 JavaClass modelJavaClass = _getJavaClass( 2243 _serviceOutputPath + "/model/" + entity.getName() + "Model.java"); 2244 2245 Object[] methods = _getMethods(modelJavaClass); 2246 2247 JavaClass extendedModelBaseImplJavaClass = _getJavaClass( 2248 _outputPath + "/model/impl/" + entity.getName() + "BaseImpl.java"); 2249 2250 methods = ArrayUtil.append( 2251 methods, _getMethods(extendedModelBaseImplJavaClass)); 2252 2253 JavaClass extendedModelJavaClass = _getJavaClass( 2254 _serviceOutputPath + "/model/" + entity.getName() + ".java"); 2255 2256 methods = ArrayUtil.append( 2257 methods, _getMethods(extendedModelJavaClass)); 2258 2259 Map<String, Object> context = _getContext(); 2260 2261 context.put("entity", entity); 2262 context.put("methods", methods); 2263 2264 // Content 2265 2266 String content = _processTemplate(_tplModelWrapper, context); 2267 2268 // Write file 2269 2270 File modelFile = new File( 2271 _serviceOutputPath + "/model/" + entity.getName() + "Wrapper.java"); 2272 2273 writeFile(modelFile, content, _author); 2274 } 2275 2276 private void _createOrmXml() throws Exception { 2277 Map<String, Object> context = _getContext(); 2278 2279 context.put("entities", _ejbList); 2280 2281 // Content 2282 2283 String content = _processTemplate(_tplOrmXml, context); 2284 2285 String mappedClasses = ""; 2286 2287 int lastMappedClassStart = content.lastIndexOf("<mapped-superclass"); 2288 2289 if (lastMappedClassStart != -1) { 2290 int lastMappedClassEnd = content.indexOf( 2291 "</mapped-superclass>", lastMappedClassStart) + 20; 2292 2293 mappedClasses = content.substring(0, lastMappedClassEnd); 2294 2295 content = content.substring(lastMappedClassEnd + 1); 2296 } 2297 2298 File xmlFile = new File(_ormFileName); 2299 2300 if (!xmlFile.exists()) { 2301 String xml = 2302 "<?xml version=\"1.0\"?>\n" + 2303 "<entity-mappings version=\"1.0\" xmlns=\"http://java.sun.com/xml/ns/persistence/orm\"\n" + 2304 "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + 2305 "\txsi:schemaLocation=\"http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd\"\n" + 2306 ">\n" + 2307 "<persistence-unit-metadata>\n" + 2308 "\t<xml-mapping-metadata-complete />\n" + 2309 "\t<persistence-unit-defaults>\n" + 2310 "\t\t<access>PROPERTY</access>\n" + 2311 "\t</persistence-unit-defaults>\n" + 2312 "</persistence-unit-metadata>\n" + 2313 "</entity-mappings>"; 2314 2315 FileUtil.write(xmlFile, xml); 2316 } 2317 2318 String oldContent = FileUtil.read(xmlFile); 2319 String newContent = oldContent; 2320 2321 int firstMappedClass = newContent.indexOf( 2322 "<mapped-superclass class=\"" + _packagePath + ".model."); 2323 int lastMappedClass = newContent.lastIndexOf( 2324 "<mapped-superclass class=\"" + _packagePath + ".model."); 2325 2326 if (firstMappedClass == -1) { 2327 int x = newContent.indexOf("<entity class="); 2328 2329 if (x != -1) { 2330 newContent = 2331 newContent.substring(0, x) + mappedClasses + 2332 newContent.substring(x); 2333 } 2334 else { 2335 content = mappedClasses + content; 2336 } 2337 } 2338 else { 2339 firstMappedClass = newContent.indexOf( 2340 "<mapped-superclass", firstMappedClass) - 1; 2341 lastMappedClass = newContent.indexOf( 2342 "</mapped-superclass>", lastMappedClass) + 20; 2343 2344 newContent = 2345 newContent.substring(0, firstMappedClass) + mappedClasses + 2346 newContent.substring(lastMappedClass); 2347 } 2348 2349 int firstEntity = newContent.indexOf( 2350 "<entity class=\"" + _packagePath + ".model.impl."); 2351 int lastEntity = newContent.lastIndexOf( 2352 "<entity class=\"" + _packagePath + ".model.impl."); 2353 2354 if (firstEntity == -1) { 2355 int x = newContent.indexOf("</entity-mappings>"); 2356 2357 if (x != -1) { 2358 newContent = 2359 newContent.substring(0, x) + content + 2360 newContent.substring(x, newContent.length()); 2361 } 2362 } 2363 else { 2364 firstEntity = newContent.lastIndexOf("<entity", firstEntity) - 1; 2365 lastEntity = newContent.indexOf("</entity>", lastEntity) + 9; 2366 2367 newContent = 2368 newContent.substring(0, firstEntity) + content + 2369 newContent.substring(lastEntity, newContent.length()); 2370 } 2371 2372 newContent = _formatXml(newContent); 2373 2374 newContent = StringUtil.replace( 2375 newContent, 2376 new String[] {"<attributes></attributes>", "<attributes/>"}, 2377 new String[] {"<attributes />", "<attributes />"}); 2378 2379 if (!oldContent.equals(newContent)) { 2380 FileUtil.write(xmlFile, newContent); 2381 } 2382 } 2383 2384 private void _createPersistence(Entity entity) throws Exception { 2385 JavaClass javaClass = _getJavaClass( 2386 _outputPath + "/service/persistence/" + entity.getName() + 2387 "PersistenceImpl.java"); 2388 2389 Map<String, Object> context = _getContext(); 2390 2391 context.put("entity", entity); 2392 context.put("methods", _getMethods(javaClass)); 2393 2394 // Content 2395 2396 String content = _processTemplate(_tplPersistence, context); 2397 2398 // Write file 2399 2400 File ejbFile = new File( 2401 _serviceOutputPath + "/service/persistence/" + entity.getName() + 2402 "Persistence.java"); 2403 2404 writeFile(ejbFile, content, _author); 2405 2406 if (!_serviceOutputPath.equals(_outputPath)) { 2407 ejbFile = new File( 2408 _outputPath + "/service/persistence/" + entity.getName() + 2409 "Persistence.java"); 2410 2411 if (ejbFile.exists()) { 2412 System.out.println("Relocating " + ejbFile); 2413 2414 ejbFile.delete(); 2415 } 2416 } 2417 } 2418 2419 private void _createPersistenceImpl(Entity entity) throws Exception { 2420 Map<String, Object> context = _getContext(); 2421 2422 context.put("entity", entity); 2423 context.put( 2424 "referenceList", _mergeReferenceList(entity.getReferenceList())); 2425 2426 // Content 2427 2428 Logger.selectLoggerLibrary(Logger.LIBRARY_NONE); 2429 2430 String content = _processTemplate(_tplPersistenceImpl, context); 2431 2432 Logger.selectLoggerLibrary(Logger.LIBRARY_AUTO); 2433 2434 // Write file 2435 2436 File ejbFile = new File( 2437 _outputPath + "/service/persistence/" + entity.getName() + 2438 "PersistenceImpl.java"); 2439 2440 writeFile(ejbFile, content, _author); 2441 } 2442 2443 private void _createPersistenceTest(Entity entity) throws Exception { 2444 Map<String, Object> context = _getContext(); 2445 2446 context.put("entity", entity); 2447 2448 // Content 2449 2450 String content = _processTemplate(_tplPersistenceTest, context); 2451 2452 // Write file 2453 2454 File ejbFile = new File( 2455 _testOutputPath + "/service/persistence/" + entity.getName() + 2456 "PersistenceTest.java"); 2457 2458 writeFile(ejbFile, content, _author); 2459 } 2460 2461 private void _createPersistenceUtil(Entity entity) throws Exception { 2462 JavaClass javaClass = _getJavaClass( 2463 _outputPath + "/service/persistence/" + entity.getName() + 2464 "PersistenceImpl.java"); 2465 2466 Map<String, Object> context = _getContext(); 2467 2468 context.put("entity", entity); 2469 context.put("methods", _getMethods(javaClass)); 2470 2471 // Content 2472 2473 String content = _processTemplate(_tplPersistenceUtil, context); 2474 2475 // Write file 2476 2477 File ejbFile = new File( 2478 _serviceOutputPath + "/service/persistence/" + entity.getName() + 2479 "Util.java"); 2480 2481 writeFile(ejbFile, content, _author); 2482 2483 if (!_serviceOutputPath.equals(_outputPath)) { 2484 ejbFile = new File( 2485 _outputPath + "/service/persistence/" + entity.getName() + 2486 "Util.java"); 2487 2488 if (ejbFile.exists()) { 2489 System.out.println("Relocating " + ejbFile); 2490 2491 ejbFile.delete(); 2492 } 2493 } 2494 } 2495 2496 private void _createPool(Entity entity) { 2497 File ejbFile = new File( 2498 _outputPath + "/service/persistence/" + entity.getName() + 2499 "Pool.java"); 2500 2501 if (ejbFile.exists()) { 2502 System.out.println("Removing deprecated " + ejbFile); 2503 2504 ejbFile.delete(); 2505 } 2506 } 2507 2508 private void _createProps() throws Exception { 2509 if (Validator.isNull(_pluginName)) { 2510 return; 2511 } 2512 2513 // Content 2514 2515 File propsFile = new File(_implDir + "/service.properties"); 2516 2517 long buildNumber = 1; 2518 2519 if (propsFile.exists()) { 2520 Properties properties = PropertiesUtil.load( 2521 FileUtil.read(propsFile)); 2522 2523 buildNumber = GetterUtil.getLong( 2524 properties.getProperty("build.number")) + 1; 2525 } 2526 2527 Map<String, Object> context = _getContext(); 2528 2529 context.put("buildNumber", new Long(buildNumber)); 2530 context.put("currentTimeMillis", new Long(System.currentTimeMillis())); 2531 2532 String content = _processTemplate(_tplProps, context); 2533 2534 // Write file 2535 2536 FileUtil.write(propsFile, content, true); 2537 } 2538 2539 private void _createRemotingXml() throws Exception { 2540 StringBundler sb = new StringBundler(); 2541 2542 Document document = SAXReaderUtil.read(new File(_springFileName)); 2543 2544 Element rootElement = document.getRootElement(); 2545 2546 List<Element> beanElements = rootElement.elements("bean"); 2547 2548 for (Element beanElement : beanElements) { 2549 String beanId = beanElement.attributeValue("id"); 2550 2551 if (beanId.endsWith("Service") && 2552 !beanId.endsWith("LocalService")) { 2553 2554 String entityName = beanId; 2555 2556 entityName = StringUtil.replaceLast( 2557 entityName, ".service.", "."); 2558 2559 int pos = entityName.lastIndexOf("Service"); 2560 2561 entityName = entityName.substring(0, pos); 2562 2563 Entity entity = getEntity(entityName); 2564 2565 String serviceName = beanId; 2566 2567 String serviceMapping = serviceName; 2568 2569 serviceMapping = StringUtil.replaceLast( 2570 serviceMapping, ".service.", ".service.spring."); 2571 serviceMapping = StringUtil.replace( 2572 serviceMapping, StringPool.PERIOD, StringPool.UNDERLINE); 2573 2574 Map<String, Object> context = _getContext(); 2575 2576 context.put("entity", entity); 2577 context.put("serviceName", serviceName); 2578 context.put("serviceMapping", serviceMapping); 2579 2580 sb.append(_processTemplate(_tplRemotingXml, context)); 2581 } 2582 } 2583 2584 File outputFile = new File(_remotingFileName); 2585 2586 if (!outputFile.exists()) { 2587 return; 2588 } 2589 2590 String content = FileUtil.read(outputFile); 2591 String newContent = content; 2592 2593 int x = content.indexOf("<bean "); 2594 int y = content.lastIndexOf("</bean>") + 8; 2595 2596 if (x != -1) { 2597 newContent = 2598 content.substring(0, x - 1) + sb.toString() + 2599 content.substring(y, content.length()); 2600 } 2601 else { 2602 x = content.indexOf("</beans>"); 2603 2604 if (x != -1) { 2605 newContent = 2606 content.substring(0, x) + sb.toString() + 2607 content.substring(x, content.length()); 2608 } 2609 else { 2610 x = content.indexOf("<beans/>"); 2611 y = x + 8; 2612 2613 newContent = 2614 content.substring(0, x) + "<beans>" + sb.toString() + 2615 "</beans>" + content.substring(y, content.length()); 2616 } 2617 } 2618 2619 newContent = _formatXml(newContent); 2620 2621 if (!content.equals(newContent)) { 2622 FileUtil.write(outputFile, newContent); 2623 2624 System.out.println(outputFile.toString()); 2625 } 2626 } 2627 2628 private void _createService(Entity entity, int sessionType) 2629 throws Exception { 2630 2631 JavaClass javaClass = _getJavaClass(_outputPath + "/service/impl/" + entity.getName() + (sessionType != _SESSION_TYPE_REMOTE ? "Local" : "") + "ServiceImpl.java"); 2632 2633 JavaMethod[] methods = _getMethods(javaClass); 2634 2635 if (sessionType == _SESSION_TYPE_LOCAL) { 2636 if (javaClass.getSuperClass().getValue().endsWith( 2637 entity.getName() + "LocalServiceBaseImpl")) { 2638 2639 JavaClass parentJavaClass = _getJavaClass( 2640 _outputPath + "/service/base/" + entity.getName() + 2641 "LocalServiceBaseImpl.java"); 2642 2643 JavaMethod[] parentMethods = parentJavaClass.getMethods(); 2644 2645 JavaMethod[] allMethods = new JavaMethod[parentMethods.length + methods.length]; 2646 2647 ArrayUtil.combine(parentMethods, methods, allMethods); 2648 2649 methods = allMethods; 2650 } 2651 } 2652 2653 Map<String, Object> context = _getContext(); 2654 2655 context.put("entity", entity); 2656 context.put("methods", methods); 2657 context.put("sessionTypeName",_getSessionTypeName(sessionType)); 2658 2659 // Content 2660 2661 String content = _processTemplate(_tplService, context); 2662 2663 // Write file 2664 2665 File ejbFile = new File( 2666 _serviceOutputPath + "/service/" + entity.getName() + 2667 _getSessionTypeName(sessionType) + "Service.java"); 2668 2669 writeFile(ejbFile, content, _author); 2670 2671 if (!_serviceOutputPath.equals(_outputPath)) { 2672 ejbFile = new File( 2673 _outputPath + "/service/" + entity.getName() + 2674 _getSessionTypeName(sessionType) + "Service.java"); 2675 2676 if (ejbFile.exists()) { 2677 System.out.println("Relocating " + ejbFile); 2678 2679 ejbFile.delete(); 2680 } 2681 } 2682 } 2683 2684 private void _createServiceBaseImpl(Entity entity, int sessionType) 2685 throws Exception { 2686 2687 JavaClass javaClass = _getJavaClass(_outputPath + "/service/impl/" + entity.getName() + (sessionType != _SESSION_TYPE_REMOTE ? "Local" : "") + "ServiceImpl.java"); 2688 2689 JavaMethod[] methods = _getMethods(javaClass); 2690 2691 Map<String, Object> context = _getContext(); 2692 2693 context.put("entity", entity); 2694 context.put("methods", methods); 2695 context.put("sessionTypeName",_getSessionTypeName(sessionType)); 2696 context.put( 2697 "referenceList", _mergeReferenceList(entity.getReferenceList())); 2698 2699 // Content 2700 2701 String content = _processTemplate(_tplServiceBaseImpl, context); 2702 2703 // Write file 2704 2705 File ejbFile = new File( 2706 _outputPath + "/service/base/" + entity.getName() + 2707 _getSessionTypeName(sessionType) + "ServiceBaseImpl.java"); 2708 2709 writeFile(ejbFile, content, _author); 2710 } 2711 2712 private void _createServiceClp(Entity entity, int sessionType) 2713 throws Exception { 2714 2715 if (Validator.isNull(_pluginName)) { 2716 return; 2717 } 2718 2719 JavaClass javaClass = _getJavaClass( 2720 _serviceOutputPath + "/service/" + entity.getName() + 2721 _getSessionTypeName(sessionType) + "Service.java"); 2722 2723 Map<String, Object> context = _getContext(); 2724 2725 context.put("entity", entity); 2726 context.put("methods", _getMethods(javaClass)); 2727 context.put("sessionTypeName", _getSessionTypeName(sessionType)); 2728 2729 // Content 2730 2731 String content = _processTemplate(_tplServiceClp, context); 2732 2733 // Write file 2734 2735 File ejbFile = new File( 2736 _serviceOutputPath + "/service/" + entity.getName() + 2737 _getSessionTypeName(sessionType) + "ServiceClp.java"); 2738 2739 writeFile(ejbFile, content, _author); 2740 } 2741 2742 private void _createServiceClpMessageListener() throws Exception { 2743 if (Validator.isNull(_pluginName)) { 2744 return; 2745 } 2746 2747 Map<String, Object> context = _getContext(); 2748 2749 context.put("entities", _ejbList); 2750 2751 // Content 2752 2753 String content = _processTemplate( 2754 _tplServiceClpMessageListener, context); 2755 2756 // Write file 2757 2758 File ejbFile = new File( 2759 _serviceOutputPath + "/service/messaging/ClpMessageListener.java"); 2760 2761 writeFile(ejbFile, content); 2762 } 2763 2764 private void _createServiceClpSerializer() throws Exception { 2765 if (Validator.isNull(_pluginName)) { 2766 return; 2767 } 2768 2769 Map<String, Object> context = _getContext(); 2770 2771 context.put("entities", _ejbList); 2772 2773 // Content 2774 2775 String content = _processTemplate(_tplServiceClpSerializer, context); 2776 2777 // Write file 2778 2779 File ejbFile = new File( 2780 _serviceOutputPath + "/service/ClpSerializer.java"); 2781 2782 writeFile(ejbFile, content); 2783 } 2784 2785 private void _createServiceFactory(Entity entity, int sessionType) 2786 throws Exception { 2787 2788 File ejbFile = new File( 2789 _serviceOutputPath + "/service/" + entity.getName() + 2790 _getSessionTypeName(sessionType) + "ServiceFactory.java"); 2791 2792 if (ejbFile.exists()) { 2793 System.out.println("Removing deprecated " + ejbFile); 2794 2795 ejbFile.delete(); 2796 } 2797 2798 ejbFile = new File( 2799 _outputPath + "/service/" + entity.getName() + 2800 _getSessionTypeName(sessionType) + "ServiceFactory.java"); 2801 2802 if (ejbFile.exists()) { 2803 System.out.println("Removing deprecated " + ejbFile); 2804 2805 ejbFile.delete(); 2806 } 2807 } 2808 2809 private void _createServiceHttp(Entity entity) throws Exception { 2810 JavaClass javaClass = _getJavaClass( 2811 _outputPath + "/service/impl/" + entity.getName() + 2812 "ServiceImpl.java"); 2813 2814 Map<String, Object> context = _getContext(); 2815 2816 context.put("entity", entity); 2817 context.put("methods", _getMethods(javaClass)); 2818 context.put("hasHttpMethods", new Boolean(_hasHttpMethods(javaClass))); 2819 2820 // Content 2821 2822 String content = _processTemplate(_tplServiceHttp, context); 2823 2824 // Write file 2825 2826 File ejbFile = new File( 2827 _outputPath + "/service/http/" + entity.getName() + 2828 "ServiceHttp.java"); 2829 2830 writeFile(ejbFile, content, _author); 2831 } 2832 2833 private void _createServiceImpl(Entity entity, int sessionType) 2834 throws Exception { 2835 2836 Map<String, Object> context = _getContext(); 2837 2838 context.put("entity", entity); 2839 context.put("sessionTypeName", _getSessionTypeName(sessionType)); 2840 2841 // Content 2842 2843 String content = _processTemplate(_tplServiceImpl, context); 2844 2845 // Write file 2846 2847 File ejbFile = new File( 2848 _outputPath + "/service/impl/" + entity.getName() + 2849 _getSessionTypeName(sessionType) + "ServiceImpl.java"); 2850 2851 if (!ejbFile.exists()) { 2852 writeFile(ejbFile, content, _author); 2853 } 2854 } 2855 2856 private void _createServiceJson(Entity entity) throws Exception { 2857 File ejbFile = new File( 2858 _outputPath + "/service/http/" + entity.getName() + 2859 "ServiceJSON.java"); 2860 2861 if (ejbFile.exists()) { 2862 System.out.println("Removing deprecated " + ejbFile); 2863 2864 ejbFile.delete(); 2865 } 2866 } 2867 2868 private void _createServiceJsonSerializer(Entity entity) throws Exception { 2869 File ejbFile = new File( 2870 _serviceOutputPath + "/service/http/" + entity.getName() + 2871 "JSONSerializer.java"); 2872 2873 if (ejbFile.exists()) { 2874 System.out.println("Removing deprecated " + ejbFile); 2875 2876 ejbFile.delete(); 2877 } 2878 2879 if (!_serviceOutputPath.equals(_outputPath)) { 2880 ejbFile = new File( 2881 _outputPath + "/service/http/" + entity.getName() + 2882 "JSONSerializer.java"); 2883 2884 if (ejbFile.exists()) { 2885 System.out.println("Removing deprecated " + ejbFile); 2886 2887 ejbFile.delete(); 2888 } 2889 } 2890 } 2891 2892 private void _createServiceSoap(Entity entity) throws Exception { 2893 JavaClass javaClass = _getJavaClass( 2894 _outputPath + "/service/impl/" + entity.getName() + 2895 "ServiceImpl.java"); 2896 2897 Map<String, Object> context = _getContext(); 2898 2899 context.put("entity", entity); 2900 context.put("methods", _getMethods(javaClass)); 2901 2902 // Content 2903 2904 String content = _processTemplate(_tplServiceSoap, context); 2905 2906 // Write file 2907 2908 File ejbFile = new File( 2909 _outputPath + "/service/http/" + entity.getName() + 2910 "ServiceSoap.java"); 2911 2912 writeFile(ejbFile, content, _author); 2913 } 2914 2915 private void _createServiceUtil(Entity entity, int sessionType) 2916 throws Exception { 2917 2918 JavaClass javaClass = _getJavaClass( 2919 _serviceOutputPath + "/service/" + entity.getName() + 2920 _getSessionTypeName(sessionType) + "Service.java"); 2921 2922 Map<String, Object> context = _getContext(); 2923 2924 context.put("entity", entity); 2925 context.put("methods", _getMethods(javaClass)); 2926 context.put("sessionTypeName", _getSessionTypeName(sessionType)); 2927 2928 // Content 2929 2930 String content = _processTemplate(_tplServiceUtil, context); 2931 2932 // Write file 2933 2934 File ejbFile = new File( 2935 _serviceOutputPath + "/service/" + entity.getName() + 2936 _getSessionTypeName(sessionType) + "ServiceUtil.java"); 2937 2938 writeFile(ejbFile, content, _author); 2939 2940 if (!_serviceOutputPath.equals(_outputPath)) { 2941 ejbFile = new File( 2942 _outputPath + "/service/" + entity.getName() + 2943 _getSessionTypeName(sessionType) + "ServiceUtil.java"); 2944 2945 if (ejbFile.exists()) { 2946 System.out.println("Relocating " + ejbFile); 2947 2948 ejbFile.delete(); 2949 } 2950 } 2951 } 2952 2953 private void _createServiceWrapper(Entity entity, int sessionType) 2954 throws Exception { 2955 2956 JavaClass javaClass = _getJavaClass( 2957 _serviceOutputPath + "/service/" + entity.getName() + 2958 _getSessionTypeName(sessionType) + "Service.java"); 2959 2960 Map<String, Object> context = _getContext(); 2961 2962 context.put("entity", entity); 2963 context.put("methods", _getMethods(javaClass)); 2964 context.put("sessionTypeName", _getSessionTypeName(sessionType)); 2965 2966 // Content 2967 2968 String content = _processTemplate(_tplServiceWrapper, context); 2969 2970 // Write file 2971 2972 File ejbFile = new File( 2973 _serviceOutputPath + "/service/" + entity.getName() + 2974 _getSessionTypeName(sessionType) + "ServiceWrapper.java"); 2975 2976 writeFile(ejbFile, content, _author); 2977 } 2978 2979 private void _createSpringBaseXml() throws Exception { 2980 if (Validator.isNull(_springBaseFileName)) { 2981 return; 2982 } 2983 2984 // Content 2985 2986 String content = _processTemplate(_tplSpringBaseXml); 2987 2988 // Write file 2989 2990 File ejbFile = new File(_springBaseFileName); 2991 2992 FileUtil.write(ejbFile, content, true); 2993 2994 if (Validator.isNotNull(_pluginName)) { 2995 FileUtil.delete( 2996 "docroot/WEB-INF/src/META-INF/data-source-spring.xml"); 2997 FileUtil.delete("docroot/WEB-INF/src/META-INF/misc-spring.xml"); 2998 } 2999 } 3000 3001 private void _createSpringClusterXml() throws Exception { 3002 if (Validator.isNull(_springClusterFileName)) { 3003 return; 3004 } 3005 3006 // Content 3007 3008 String content = _processTemplate(_tplSpringClusterXml); 3009 3010 // Write file 3011 3012 File ejbFile = new File(_springClusterFileName); 3013 3014 FileUtil.write(ejbFile, content, true); 3015 } 3016 3017 private void _createSpringDynamicDataSourceXml() throws Exception { 3018 if (Validator.isNull(_springDynamicDataSourceFileName)) { 3019 return; 3020 } 3021 3022 // Content 3023 3024 String content = _processTemplate(_tplSpringDynamicDataSourceXml); 3025 3026 // Write file 3027 3028 File ejbFile = new File(_springDynamicDataSourceFileName); 3029 3030 FileUtil.write(ejbFile, content, true); 3031 } 3032 3033 private void _createSpringHibernateXml() throws Exception { 3034 if (Validator.isNull(_springHibernateFileName)) { 3035 return; 3036 } 3037 3038 // Content 3039 3040 String content = _processTemplate(_tplSpringHibernateXml); 3041 3042 // Write file 3043 3044 File ejbFile = new File(_springHibernateFileName); 3045 3046 FileUtil.write(ejbFile, content, true); 3047 } 3048 3049 private void _createSpringInfrastructureXml() throws Exception { 3050 if (Validator.isNull(_springInfrastructureFileName)) { 3051 return; 3052 } 3053 3054 // Content 3055 3056 String content = _processTemplate(_tplSpringInfrastructureXml); 3057 3058 // Write file 3059 3060 File ejbFile = new File(_springInfrastructureFileName); 3061 3062 FileUtil.write(ejbFile, content, true); 3063 } 3064 3065 private void _createSpringShardDataSourceXml() throws Exception { 3066 if (Validator.isNull(_springShardDataSourceFileName)) { 3067 return; 3068 } 3069 3070 // Content 3071 3072 String content = _processTemplate(_tplSpringShardDataSourceXml); 3073 3074 // Write file 3075 3076 File ejbFile = new File(_springShardDataSourceFileName); 3077 3078 FileUtil.write(ejbFile, content, true); 3079 } 3080 3081 private void _createSpringXml() throws Exception { 3082 if (_packagePath.equals("com.liferay.counter")) { 3083 return; 3084 } 3085 3086 Map<String, Object> context = _getContext(); 3087 3088 context.put("entities", _ejbList); 3089 3090 // Content 3091 3092 String content = _processTemplate(_tplSpringXml, context); 3093 3094 File xmlFile = new File(_springFileName); 3095 3096 String xml = 3097 "<?xml version=\"1.0\"?>\n" + 3098 "\n" + 3099 "<beans\n" + 3100 "\tdefault-destroy-method=\"destroy\"\n" + 3101 "\tdefault-init-method=\"afterPropertiesSet\"\n" + 3102 "\txmlns=\"http://www.springframework.org/schema/beans\"\n" + 3103 "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + 3104 "\txsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\"\n" + 3105 ">\n" + 3106 "</beans>"; 3107 3108 if (!xmlFile.exists()) { 3109 FileUtil.write(xmlFile, xml); 3110 } 3111 3112 String oldContent = FileUtil.read(xmlFile); 3113 3114 if (Validator.isNotNull(_pluginName) && 3115 oldContent.contains("DOCTYPE beans PUBLIC")) { 3116 3117 oldContent = xml; 3118 } 3119 3120 String newContent = _fixSpringXml(oldContent); 3121 3122 int x = oldContent.indexOf("<beans"); 3123 int y = oldContent.lastIndexOf("</beans>"); 3124 3125 int firstSession = newContent.indexOf( 3126 "<bean id=\"" + _packagePath + ".service.", x); 3127 3128 int lastSession = newContent.lastIndexOf( 3129 "<bean id=\"" + _packagePath + ".service.", y); 3130 3131 if ((firstSession == -1) || (firstSession > y)) { 3132 x = newContent.indexOf("</beans>"); 3133 3134 newContent = 3135 newContent.substring(0, x) + content + 3136 newContent.substring(x, newContent.length()); 3137 } 3138 else { 3139 firstSession = newContent.lastIndexOf("<bean", firstSession) - 1; 3140 3141 int tempLastSession = newContent.indexOf( 3142 "<bean id=\"", lastSession + 1); 3143 3144 if (tempLastSession == -1) { 3145 tempLastSession = newContent.indexOf("</beans>", lastSession); 3146 } 3147 3148 lastSession = tempLastSession; 3149 3150 newContent = 3151 newContent.substring(0, firstSession) + content + 3152 newContent.substring(lastSession, newContent.length()); 3153 } 3154 3155 newContent = _formatXml(newContent); 3156 3157 if (!oldContent.equals(newContent)) { 3158 FileUtil.write(xmlFile, newContent); 3159 } 3160 } 3161 3162 private void _createSQLIndexes() throws IOException { 3163 if (!FileUtil.exists(_sqlDir)) { 3164 return; 3165 } 3166 3167 // indexes.sql 3168 3169 File sqlFile = new File(_sqlDir + "/" + _sqlIndexesFileName); 3170 3171 if (!sqlFile.exists()) { 3172 FileUtil.write(sqlFile, ""); 3173 } 3174 3175 Map<String, String> indexSQLs = new TreeMap<String, String>(); 3176 3177 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( 3178 new FileReader(sqlFile)); 3179 3180 while (true) { 3181 String indexSQL = unsyncBufferedReader.readLine(); 3182 3183 if (indexSQL == null) { 3184 break; 3185 } 3186 3187 if (Validator.isNotNull(indexSQL.trim())) { 3188 int pos = indexSQL.indexOf(" on "); 3189 3190 String indexSpec = indexSQL.substring(pos + 4); 3191 3192 indexSQLs.put(indexSpec, indexSQL); 3193 } 3194 } 3195 3196 unsyncBufferedReader.close(); 3197 3198 // indexes.properties 3199 3200 File propsFile = new File( 3201 _sqlDir + "/" + _sqlIndexesPropertiesFileName); 3202 3203 if (!propsFile.exists()) { 3204 FileUtil.write(propsFile, ""); 3205 } 3206 3207 Map<String, String> indexProps = new TreeMap<String, String>(); 3208 3209 unsyncBufferedReader = new UnsyncBufferedReader( 3210 new FileReader(propsFile)); 3211 3212 while (true) { 3213 String indexMapping = unsyncBufferedReader.readLine(); 3214 3215 if (indexMapping == null) { 3216 break; 3217 } 3218 3219 if (Validator.isNotNull(indexMapping.trim())) { 3220 String[] splitIndexMapping = indexMapping.split("\\="); 3221 3222 indexProps.put(splitIndexMapping[1], splitIndexMapping[0]); 3223 } 3224 } 3225 3226 unsyncBufferedReader.close(); 3227 3228 // indexes.sql 3229 3230 for (int i = 0; i < _ejbList.size(); i++) { 3231 Entity entity = _ejbList.get(i); 3232 3233 if (!entity.isDefaultDataSource()) { 3234 continue; 3235 } 3236 3237 List<EntityFinder> finderList = entity.getFinderList(); 3238 3239 for (int j = 0; j < finderList.size(); j++) { 3240 EntityFinder finder = finderList.get(j); 3241 3242 if (finder.isDBIndex()) { 3243 StringBundler sb = new StringBundler(); 3244 3245 sb.append(entity.getTable() + " ("); 3246 3247 List<EntityColumn> finderColsList = finder.getColumns(); 3248 3249 for (int k = 0; k < finderColsList.size(); k++) { 3250 EntityColumn col = finderColsList.get(k); 3251 3252 sb.append(col.getDBName()); 3253 3254 if ((k + 1) != finderColsList.size()) { 3255 sb.append(", "); 3256 } 3257 } 3258 3259 sb.append(");"); 3260 3261 String indexSpec = sb.toString(); 3262 3263 String indexHash = StringUtil.toHexString( 3264 indexSpec.hashCode()).toUpperCase(); 3265 3266 String indexName = "IX_" + indexHash; 3267 3268 sb.setIndex(0); 3269 3270 sb.append("create "); 3271 3272 if (finder.isUnique()) { 3273 sb.append("unique "); 3274 } 3275 3276 sb.append("index " + indexName + " on "); 3277 sb.append(indexSpec); 3278 3279 indexSQLs.put(indexSpec, sb.toString()); 3280 3281 String finderName = 3282 entity.getTable() + StringPool.PERIOD + 3283 finder.getName(); 3284 3285 indexProps.put(finderName, indexName); 3286 } 3287 } 3288 } 3289 3290 for (Map.Entry<String, EntityMapping> entry : 3291 _entityMappings.entrySet()) { 3292 3293 EntityMapping entityMapping = entry.getValue(); 3294 3295 _getCreateMappingTableIndex(entityMapping, indexSQLs, indexProps); 3296 } 3297 3298 StringBundler sb = new StringBundler(); 3299 3300 Iterator<String> itr = indexSQLs.values().iterator(); 3301 3302 String prevEntityName = null; 3303 3304 while (itr.hasNext()) { 3305 String indexSQL = itr.next(); 3306 3307 int pos = indexSQL.indexOf(" on "); 3308 3309 String indexSQLSuffix = indexSQL.substring(pos + 4); 3310 3311 String entityName = indexSQLSuffix.split(" ")[0]; 3312 3313 if ((prevEntityName != null) && 3314 (!prevEntityName.equals(entityName))) { 3315 3316 sb.append("\n"); 3317 } 3318 3319 sb.append(indexSQL); 3320 3321 if (itr.hasNext()) { 3322 sb.append("\n"); 3323 } 3324 3325 prevEntityName = entityName; 3326 } 3327 3328 FileUtil.write(sqlFile, sb.toString(), true); 3329 3330 // indexes.properties 3331 3332 sb.setIndex(0); 3333 3334 itr = indexProps.keySet().iterator(); 3335 3336 prevEntityName = null; 3337 3338 while (itr.hasNext()) { 3339 String finderName = itr.next(); 3340 3341 String indexName = indexProps.get(finderName); 3342 3343 String entityName = finderName.split("\\.")[0]; 3344 3345 if ((prevEntityName != null) && 3346 (!prevEntityName.equals(entityName))) { 3347 3348 sb.append("\n"); 3349 } 3350 3351 sb.append(indexName + StringPool.EQUAL + finderName); 3352 3353 if (itr.hasNext()) { 3354 sb.append("\n"); 3355 } 3356 3357 prevEntityName = entityName; 3358 } 3359 3360 FileUtil.write(propsFile, sb.toString(), true); 3361 } 3362 3363 private void _createSQLMappingTables( 3364 File sqlFile, String newCreateTableString, 3365 EntityMapping entityMapping, boolean addMissingTables) 3366 throws IOException { 3367 3368 if (!sqlFile.exists()) { 3369 FileUtil.write(sqlFile, StringPool.BLANK); 3370 } 3371 3372 String content = FileUtil.read(sqlFile); 3373 3374 int x = content.indexOf( 3375 _SQL_CREATE_TABLE + entityMapping.getTable() + " ("); 3376 int y = content.indexOf(");", x); 3377 3378 if (x != -1) { 3379 String oldCreateTableString = content.substring(x + 1, y); 3380 3381 if (!oldCreateTableString.equals(newCreateTableString)) { 3382 content = 3383 content.substring(0, x) + newCreateTableString + 3384 content.substring(y + 2, content.length()); 3385 3386 FileUtil.write(sqlFile, content); 3387 } 3388 } 3389 else if (addMissingTables) { 3390 StringBundler sb = new StringBundler(); 3391 3392 UnsyncBufferedReader unsyncBufferedReader = 3393 new UnsyncBufferedReader(new UnsyncStringReader(content)); 3394 3395 String line = null; 3396 boolean appendNewTable = true; 3397 3398 while ((line = unsyncBufferedReader.readLine()) != null) { 3399 if (appendNewTable && line.startsWith(_SQL_CREATE_TABLE)) { 3400 x = _SQL_CREATE_TABLE.length(); 3401 y = line.indexOf(" ", x); 3402 3403 String tableName = line.substring(x, y); 3404 3405 if (tableName.compareTo(entityMapping.getTable()) > 0) { 3406 sb.append(newCreateTableString + "\n\n"); 3407 3408 appendNewTable = false; 3409 } 3410 } 3411 3412 sb.append(line); 3413 sb.append("\n"); 3414 } 3415 3416 if (appendNewTable) { 3417 sb.append("\n" + newCreateTableString); 3418 } 3419 3420 unsyncBufferedReader.close(); 3421 3422 FileUtil.write(sqlFile, sb.toString(), true); 3423 } 3424 } 3425 3426 private void _createSQLSequences() throws IOException { 3427 if (!FileUtil.exists(_sqlDir)) { 3428 return; 3429 } 3430 3431 File sqlFile = new File(_sqlDir + "/" + _sqlSequencesFileName); 3432 3433 if (!sqlFile.exists()) { 3434 FileUtil.write(sqlFile, ""); 3435 } 3436 3437 Set<String> sequenceSQLs = new TreeSet<String>(); 3438 3439 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( 3440 new FileReader(sqlFile)); 3441 3442 while (true) { 3443 String sequenceSQL = unsyncBufferedReader.readLine(); 3444 3445 if (sequenceSQL == null) { 3446 break; 3447 } 3448 3449 if (Validator.isNotNull(sequenceSQL)) { 3450 sequenceSQLs.add(sequenceSQL); 3451 } 3452 } 3453 3454 unsyncBufferedReader.close(); 3455 3456 for (int i = 0; i < _ejbList.size(); i++) { 3457 Entity entity = _ejbList.get(i); 3458 3459 if (!entity.isDefaultDataSource()) { 3460 continue; 3461 } 3462 3463 List<EntityColumn> columnList = entity.getColumnList(); 3464 3465 for (int j = 0; j < columnList.size(); j++) { 3466 EntityColumn column = columnList.get(j); 3467 3468 if ("sequence".equals(column.getIdType())) { 3469 StringBundler sb = new StringBundler(); 3470 3471 String sequenceName = column.getIdParam(); 3472 3473 if (sequenceName.length() > 30) { 3474 sequenceName = sequenceName.substring(0, 30); 3475 } 3476 3477 sb.append("create sequence " + sequenceName + ";"); 3478 3479 String sequenceSQL = sb.toString(); 3480 3481 if (!sequenceSQLs.contains(sequenceSQL)) { 3482 sequenceSQLs.add(sequenceSQL); 3483 } 3484 } 3485 } 3486 } 3487 3488 StringBundler sb = new StringBundler(); 3489 3490 Iterator<String> itr = sequenceSQLs.iterator(); 3491 3492 while (itr.hasNext()) { 3493 String sequenceSQL = itr.next(); 3494 3495 sb.append(sequenceSQL); 3496 3497 if (itr.hasNext()) { 3498 sb.append("\n"); 3499 } 3500 } 3501 3502 FileUtil.write(sqlFile, sb.toString(), true); 3503 } 3504 3505 private void _createSQLTables() throws IOException { 3506 if (!FileUtil.exists(_sqlDir)) { 3507 return; 3508 } 3509 3510 File sqlFile = new File(_sqlDir + "/" + _sqlFileName); 3511 3512 if (!sqlFile.exists()) { 3513 FileUtil.write(sqlFile, StringPool.BLANK); 3514 } 3515 3516 for (int i = 0; i < _ejbList.size(); i++) { 3517 Entity entity = _ejbList.get(i); 3518 3519 if (!entity.isDefaultDataSource()) { 3520 continue; 3521 } 3522 3523 String createTableSQL = _getCreateTableSQL(entity); 3524 3525 if (Validator.isNotNull(createTableSQL)) { 3526 _createSQLTables(sqlFile, createTableSQL, entity, true); 3527 3528 _updateSQLFile( 3529 "update-6.0.6-6.1.0.sql", createTableSQL, entity); 3530 _updateSQLFile( 3531 "update-6.0.12-6.1.0.sql", createTableSQL, entity); 3532 } 3533 } 3534 3535 for (Map.Entry<String, EntityMapping> entry : 3536 _entityMappings.entrySet()) { 3537 3538 EntityMapping entityMapping = entry.getValue(); 3539 3540 String createMappingTableSQL = _getCreateMappingTableSQL( 3541 entityMapping); 3542 3543 if (Validator.isNotNull(createMappingTableSQL)) { 3544 _createSQLMappingTables( 3545 sqlFile, createMappingTableSQL, entityMapping, true); 3546 } 3547 } 3548 3549 String content = FileUtil.read(sqlFile); 3550 3551 FileUtil.write(sqlFile, content.trim()); 3552 } 3553 3554 private void _createSQLTables( 3555 File sqlFile, String newCreateTableString, Entity entity, 3556 boolean addMissingTables) 3557 throws IOException { 3558 3559 if (!sqlFile.exists()) { 3560 FileUtil.write(sqlFile, StringPool.BLANK); 3561 } 3562 3563 String content = FileUtil.read(sqlFile); 3564 3565 int x = content.indexOf(_SQL_CREATE_TABLE + entity.getTable() + " ("); 3566 int y = content.indexOf(");", x); 3567 3568 if (x != -1) { 3569 String oldCreateTableString = content.substring(x + 1, y); 3570 3571 if (!oldCreateTableString.equals(newCreateTableString)) { 3572 content = 3573 content.substring(0, x) + newCreateTableString + 3574 content.substring(y + 2, content.length()); 3575 3576 FileUtil.write(sqlFile, content); 3577 } 3578 } 3579 else if (addMissingTables) { 3580 StringBundler sb = new StringBundler(); 3581 3582 UnsyncBufferedReader unsyncBufferedReader = 3583 new UnsyncBufferedReader(new UnsyncStringReader(content)); 3584 3585 String line = null; 3586 boolean appendNewTable = true; 3587 3588 while ((line = unsyncBufferedReader.readLine()) != null) { 3589 if (appendNewTable && line.startsWith(_SQL_CREATE_TABLE)) { 3590 x = _SQL_CREATE_TABLE.length(); 3591 y = line.indexOf(" ", x); 3592 3593 String tableName = line.substring(x, y); 3594 3595 if (tableName.compareTo(entity.getTable()) > 0) { 3596 sb.append(newCreateTableString + "\n\n"); 3597 3598 appendNewTable = false; 3599 } 3600 } 3601 3602 sb.append(line); 3603 sb.append("\n"); 3604 } 3605 3606 if (appendNewTable) { 3607 sb.append("\n" + newCreateTableString); 3608 } 3609 3610 unsyncBufferedReader.close(); 3611 3612 FileUtil.write(sqlFile, sb.toString(), true); 3613 } 3614 } 3615 3616 private String _fixHbmXml(String content) throws IOException { 3617 StringBundler sb = new StringBundler(); 3618 3619 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( 3620 new UnsyncStringReader(content)); 3621 3622 String line = null; 3623 3624 while ((line = unsyncBufferedReader.readLine()) != null) { 3625 if (line.startsWith("\t<class name=\"")) { 3626 line = StringUtil.replace( 3627 line, 3628 new String[] { 3629 ".service.persistence.", "HBM\" table=\"" 3630 }, 3631 new String[] { 3632 ".model.", "\" table=\"" 3633 }); 3634 3635 if (!line.contains(".model.impl.") && 3636 !line.contains("BlobModel")) { 3637 3638 line = StringUtil.replace( 3639 line, 3640 new String[] { 3641 ".model.", "\" table=\"" 3642 }, 3643 new String[] { 3644 ".model.impl.", "Impl\" table=\"" 3645 }); 3646 } 3647 } 3648 3649 sb.append(line); 3650 sb.append('\n'); 3651 } 3652 3653 unsyncBufferedReader.close(); 3654 3655 return sb.toString().trim(); 3656 } 3657 3658 private String _fixSpringXml(String content) { 3659 return StringUtil.replace(content, ".service.spring.", ".service."); 3660 } 3661 3662 private String _formatComment( 3663 String comment, DocletTag[] tags, String indentation) { 3664 3665 StringBundler sb = new StringBundler(); 3666 3667 if (Validator.isNull(comment) && (tags.length <= 0)) { 3668 return sb.toString(); 3669 } 3670 3671 sb.append(indentation); 3672 sb.append("/**\n"); 3673 3674 if (Validator.isNotNull(comment)) { 3675 comment = comment.replaceAll("(?m)^", indentation + " * "); 3676 3677 sb.append(comment); 3678 sb.append("\n"); 3679 3680 if (tags.length > 0) { 3681 sb.append(indentation); 3682 sb.append(" *\n"); 3683 } 3684 } 3685 3686 for (DocletTag tag : tags) { 3687 sb.append(indentation); 3688 sb.append(" * @"); 3689 sb.append(tag.getName()); 3690 sb.append(" "); 3691 sb.append(tag.getValue()); 3692 sb.append("\n"); 3693 } 3694 3695 sb.append(indentation); 3696 sb.append(" */\n"); 3697 3698 return sb.toString(); 3699 } 3700 3701 private String _formatXml(String xml) 3702 throws DocumentException, IOException { 3703 3704 String doctype = null; 3705 3706 int x = xml.indexOf("<!DOCTYPE"); 3707 3708 if (x != -1) { 3709 int y = xml.indexOf(">", x) + 1; 3710 3711 doctype = xml.substring(x, y); 3712 3713 xml = xml.substring(0, x) + "\n" + xml.substring(y); 3714 } 3715 3716 xml = StringUtil.replace(xml, '\r', ""); 3717 xml = XMLFormatter.toString(xml); 3718 xml = StringUtil.replace(xml, "\"/>", "\" />"); 3719 3720 if (Validator.isNotNull(doctype)) { 3721 x = xml.indexOf("?>") + 2; 3722 3723 xml = xml.substring(0, x) + "\n" + doctype + xml.substring(x); 3724 } 3725 3726 return xml; 3727 } 3728 3729 private JavaField[] _getCacheFields(JavaClass javaClass) { 3730 if (javaClass == null) { 3731 return new JavaField[0]; 3732 } 3733 3734 List<JavaField> javaFields = new ArrayList<JavaField>(); 3735 3736 for (JavaField javaField : javaClass.getFields()) { 3737 Annotation[] annotations = javaField.getAnnotations(); 3738 3739 for (Annotation annotation : annotations) { 3740 Type type = annotation.getType(); 3741 3742 String className = type.getFullyQualifiedName(); 3743 3744 if (className.equals(CacheField.class.getName())) { 3745 javaFields.add(javaField); 3746 3747 break; 3748 } 3749 } 3750 } 3751 3752 return javaFields.toArray(new JavaField[javaFields.size()]); 3753 } 3754 3755 private String _getContent(String fileName) throws Exception { 3756 Document document = _getContentDocument(fileName); 3757 3758 Element rootElement = document.getRootElement(); 3759 3760 Element authorElement = null; 3761 Element namespaceElement = null; 3762 Map<String, Element> entityElements = new TreeMap<String, Element>(); 3763 Map<String, Element> exceptionElements = new TreeMap<String, Element>(); 3764 3765 for (Element element : rootElement.elements()) { 3766 String elementName = element.getName(); 3767 3768 if (elementName.equals("author")) { 3769 element.detach(); 3770 3771 if (authorElement != null) { 3772 throw new IllegalArgumentException( 3773 "There can only be one author element"); 3774 } 3775 3776 authorElement = element; 3777 } 3778 else if (elementName.equals("namespace")) { 3779 element.detach(); 3780 3781 if (namespaceElement != null) { 3782 throw new IllegalArgumentException( 3783 "There can only be one namespace element"); 3784 } 3785 3786 namespaceElement = element; 3787 } 3788 else if (elementName.equals("entity")) { 3789 element.detach(); 3790 3791 String name = element.attributeValue("name"); 3792 3793 entityElements.put(name.toLowerCase(), element); 3794 } 3795 else if (elementName.equals("exceptions")) { 3796 element.detach(); 3797 3798 for (Element exceptionElement : element.elements("exception")) { 3799 exceptionElement.detach(); 3800 3801 exceptionElements.put( 3802 exceptionElement.getText(), exceptionElement); 3803 } 3804 } 3805 } 3806 3807 if (authorElement != null) { 3808 rootElement.add(authorElement); 3809 } 3810 3811 if (namespaceElement == null) { 3812 throw new IllegalArgumentException( 3813 "The namespace element is required"); 3814 } 3815 else { 3816 rootElement.add(namespaceElement); 3817 } 3818 3819 _addElements(rootElement, entityElements); 3820 3821 if (!exceptionElements.isEmpty()) { 3822 Element exceptionsElement = rootElement.addElement("exceptions"); 3823 3824 _addElements(exceptionsElement, exceptionElements); 3825 } 3826 3827 return document.asXML(); 3828 } 3829 3830 private Document _getContentDocument(String fileName) throws Exception { 3831 String content = FileUtil.read(new File(fileName)); 3832 3833 Document document = SAXReaderUtil.read(content); 3834 3835 Element rootElement = document.getRootElement(); 3836 3837 for (Element element : rootElement.elements()) { 3838 String elementName = element.getName(); 3839 3840 if (!elementName.equals("service-builder-import")) { 3841 continue; 3842 } 3843 3844 element.detach(); 3845 3846 String dirName = fileName.substring( 3847 0, fileName.lastIndexOf(StringPool.SLASH) + 1); 3848 String serviceBuilderImportFileName = element.attributeValue( 3849 "file"); 3850 3851 Document serviceBuilderImportDocument = _getContentDocument( 3852 dirName + serviceBuilderImportFileName); 3853 3854 Element serviceBuilderImportRootElement = 3855 serviceBuilderImportDocument.getRootElement(); 3856 3857 for (Element serviceBuilderImportElement : 3858 serviceBuilderImportRootElement.elements()) { 3859 3860 serviceBuilderImportElement.detach(); 3861 3862 rootElement.add(serviceBuilderImportElement); 3863 } 3864 } 3865 3866 return document; 3867 } 3868 3869 private Map<String, Object> _getContext() throws TemplateModelException { 3870 BeansWrapper wrapper = BeansWrapper.getDefaultInstance(); 3871 3872 TemplateHashModel staticModels = wrapper.getStaticModels(); 3873 3874 Map<String, Object> context = new HashMap<String, Object>(); 3875 3876 context.put("hbmFileName", _hbmFileName); 3877 context.put("ormFileName", _ormFileName); 3878 context.put("modelHintsFileName", _modelHintsFileName); 3879 context.put("springFileName", _springFileName); 3880 context.put("springBaseFileName", _springBaseFileName); 3881 context.put("springHibernateFileName", _springHibernateFileName); 3882 context.put( 3883 "springInfrastructureFileName", _springInfrastructureFileName); 3884 context.put("apiDir", _apiDir); 3885 context.put("implDir", _implDir); 3886 context.put("jsonFileName", _jsonFileName); 3887 context.put("sqlDir", _sqlDir); 3888 context.put("sqlFileName", _sqlFileName); 3889 context.put("beanLocatorUtil", _beanLocatorUtil); 3890 context.put("beanLocatorUtilShortName", _beanLocatorUtilShortName); 3891 context.put("propsUtil", _propsUtil); 3892 context.put("portletName", _portletName); 3893 context.put("portletShortName", _portletShortName); 3894 context.put("portletPackageName", _portletPackageName); 3895 context.put("outputPath", _outputPath); 3896 context.put("serviceOutputPath", _serviceOutputPath); 3897 context.put("packagePath", _packagePath); 3898 context.put("pluginName", _pluginName); 3899 context.put("author", _author); 3900 context.put("serviceBuilder", this); 3901 3902 context.put("arrayUtil", ArrayUtil_IW.getInstance()); 3903 context.put("modelHintsUtil", ModelHintsUtil.getModelHints()); 3904 context.put( 3905 "resourceActionsUtil", ResourceActionsUtil.getResourceActions()); 3906 context.put("stringUtil", StringUtil_IW.getInstance()); 3907 context.put("system", staticModels.get("java.lang.System")); 3908 context.put("tempMap", wrapper.wrap(new HashMap<String, Object>())); 3909 context.put( 3910 "textFormatter", staticModels.get(TextFormatter.class.getName())); 3911 context.put("validator", Validator_IW.getInstance()); 3912 3913 return context; 3914 } 3915 3916 private void _getCreateMappingTableIndex( 3917 EntityMapping entityMapping, Map<String, String> indexSQLs, 3918 Map<String, String> indexProps) 3919 throws IOException { 3920 3921 Entity[] entities = new Entity[2]; 3922 3923 for (int i = 0; i < entities.length; i++) { 3924 entities[i] = getEntity(entityMapping.getEntity(i)); 3925 3926 if (entities[i] == null) { 3927 return; 3928 } 3929 } 3930 3931 for (Entity entity : entities) { 3932 List<EntityColumn> pkList = entity.getPKList(); 3933 3934 for (int j = 0; j < pkList.size(); j++) { 3935 EntityColumn col = pkList.get(j); 3936 3937 String colDBName = col.getDBName(); 3938 3939 String indexSpec = 3940 entityMapping.getTable() + " (" + colDBName + ");"; 3941 3942 String indexHash = 3943 StringUtil.toHexString(indexSpec.hashCode()).toUpperCase(); 3944 3945 String indexName = "IX_" + indexHash; 3946 3947 StringBundler sb = new StringBundler(); 3948 3949 sb.append("create index "); 3950 sb.append(indexName); 3951 sb.append(" on "); 3952 sb.append(indexSpec); 3953 3954 indexSQLs.put(indexSpec, sb.toString()); 3955 3956 String finderName = 3957 entityMapping.getTable() + StringPool.PERIOD + colDBName; 3958 3959 indexProps.put(finderName, indexName); 3960 } 3961 } 3962 } 3963 3964 private String _getCreateMappingTableSQL(EntityMapping entityMapping) 3965 throws IOException { 3966 3967 Entity[] entities = new Entity[2]; 3968 3969 for (int i = 0; i < entities.length; i++) { 3970 entities[i] = getEntity(entityMapping.getEntity(i)); 3971 3972 if (entities[i] == null) { 3973 return null; 3974 } 3975 } 3976 3977 StringBundler sb = new StringBundler(); 3978 3979 sb.append(_SQL_CREATE_TABLE); 3980 sb.append(entityMapping.getTable()); 3981 sb.append(" (\n"); 3982 3983 for (Entity entity : entities) { 3984 List<EntityColumn> pkList = entity.getPKList(); 3985 3986 for (int i = 0; i < pkList.size(); i++) { 3987 EntityColumn col = pkList.get(i); 3988 3989 String colName = col.getName(); 3990 String colType = col.getType(); 3991 3992 sb.append("\t" + col.getDBName()); 3993 sb.append(" "); 3994 3995 if (colType.equalsIgnoreCase("boolean")) { 3996 sb.append("BOOLEAN"); 3997 } 3998 else if (colType.equalsIgnoreCase("double") || 3999 colType.equalsIgnoreCase("float")) { 4000 4001 sb.append("DOUBLE"); 4002 } 4003 else if (colType.equals("int") || 4004 colType.equals("Integer") || 4005 colType.equalsIgnoreCase("short")) { 4006 4007 sb.append("INTEGER"); 4008 } 4009 else if (colType.equalsIgnoreCase("long")) { 4010 sb.append("LONG"); 4011 } 4012 else if (colType.equals("String")) { 4013 Map<String, String> hints = ModelHintsUtil.getHints( 4014 _packagePath + ".model." + entity.getName(), colName); 4015 4016 int maxLength = 75; 4017 4018 if (hints != null) { 4019 maxLength = GetterUtil.getInteger( 4020 hints.get("max-length"), maxLength); 4021 } 4022 4023 if (col.isLocalized()) { 4024 maxLength = 4000; 4025 } 4026 4027 if (maxLength < 4000) { 4028 sb.append("VARCHAR(" + maxLength + ")"); 4029 } 4030 else if (maxLength == 4000) { 4031 sb.append("STRING"); 4032 } 4033 else if (maxLength > 4000) { 4034 sb.append("TEXT"); 4035 } 4036 } 4037 else if (colType.equals("Date")) { 4038 sb.append("DATE"); 4039 } 4040 else { 4041 sb.append("invalid"); 4042 } 4043 4044 if (col.isPrimary()) { 4045 sb.append(" not null"); 4046 } 4047 else if (colType.equals("Date") || colType.equals("String")) { 4048 sb.append(" null"); 4049 } 4050 4051 sb.append(",\n"); 4052 } 4053 } 4054 4055 sb.append("\tprimary key ("); 4056 4057 for (int i = 0; i < entities.length; i++) { 4058 Entity entity = entities[i]; 4059 4060 List<EntityColumn> pkList = entity.getPKList(); 4061 4062 for (int j = 0; j < pkList.size(); j++) { 4063 EntityColumn col = pkList.get(j); 4064 4065 String colDBName = col.getDBName(); 4066 4067 if ((i != 0) || (j != 0)) { 4068 sb.append(", "); 4069 } 4070 4071 sb.append(colDBName); 4072 } 4073 } 4074 4075 sb.append(")\n"); 4076 sb.append(");"); 4077 4078 return sb.toString(); 4079 } 4080 4081 private String _getCreateTableSQL(Entity entity) { 4082 List<EntityColumn> pkList = entity.getPKList(); 4083 List<EntityColumn> regularColList = entity.getRegularColList(); 4084 4085 if (regularColList.size() == 0) { 4086 return null; 4087 } 4088 4089 StringBundler sb = new StringBundler(); 4090 4091 sb.append(_SQL_CREATE_TABLE); 4092 sb.append(entity.getTable()); 4093 sb.append(" (\n"); 4094 4095 for (int i = 0; i < regularColList.size(); i++) { 4096 EntityColumn col = regularColList.get(i); 4097 4098 String colName = col.getName(); 4099 String colType = col.getType(); 4100 String colIdType = col.getIdType(); 4101 4102 sb.append("\t" + col.getDBName()); 4103 sb.append(" "); 4104 4105 if (colType.equalsIgnoreCase("boolean")) { 4106 sb.append("BOOLEAN"); 4107 } 4108 else if (colType.equalsIgnoreCase("double") || 4109 colType.equalsIgnoreCase("float")) { 4110 4111 sb.append("DOUBLE"); 4112 } 4113 else if (colType.equals("int") || 4114 colType.equals("Integer") || 4115 colType.equalsIgnoreCase("short")) { 4116 4117 sb.append("INTEGER"); 4118 } 4119 else if (colType.equalsIgnoreCase("long")) { 4120 sb.append("LONG"); 4121 } 4122 else if (colType.equals("Blob")) { 4123 sb.append("BLOB"); 4124 } 4125 else if (colType.equals("Date")) { 4126 sb.append("DATE"); 4127 } 4128 else if (colType.equals("String")) { 4129 Map<String, String> hints = ModelHintsUtil.getHints( 4130 _packagePath + ".model." + entity.getName(), colName); 4131 4132 int maxLength = 75; 4133 4134 if (hints != null) { 4135 maxLength = GetterUtil.getInteger( 4136 hints.get("max-length"), maxLength); 4137 } 4138 4139 if (col.isLocalized()) { 4140 maxLength = 4000; 4141 } 4142 4143 if (maxLength < 4000) { 4144 sb.append("VARCHAR(" + maxLength + ")"); 4145 } 4146 else if (maxLength == 4000) { 4147 sb.append("STRING"); 4148 } 4149 else if (maxLength > 4000) { 4150 sb.append("TEXT"); 4151 } 4152 } 4153 else { 4154 sb.append("invalid"); 4155 } 4156 4157 if (col.isPrimary()) { 4158 sb.append(" not null"); 4159 4160 if (!entity.hasCompoundPK()) { 4161 sb.append(" primary key"); 4162 } 4163 } 4164 else if (colType.equals("Date") || colType.equals("String")) { 4165 sb.append(" null"); 4166 } 4167 4168 if (Validator.isNotNull(colIdType) && 4169 colIdType.equals("identity")) { 4170 4171 sb.append(" IDENTITY"); 4172 } 4173 4174 if (((i + 1) != regularColList.size()) || 4175 (entity.hasCompoundPK())) { 4176 4177 sb.append(","); 4178 } 4179 4180 sb.append("\n"); 4181 } 4182 4183 if (entity.hasCompoundPK()) { 4184 sb.append("\tprimary key ("); 4185 4186 for (int j = 0; j < pkList.size(); j++) { 4187 EntityColumn pk = pkList.get(j); 4188 4189 sb.append(pk.getDBName()); 4190 4191 if ((j + 1) != pkList.size()) { 4192 sb.append(", "); 4193 } 4194 } 4195 4196 sb.append(")\n"); 4197 } 4198 4199 sb.append(");"); 4200 4201 return sb.toString(); 4202 } 4203 4204 private String _getDimensions(Type type) { 4205 String dimensions = ""; 4206 4207 for (int i = 0; i < type.getDimensions(); i++) { 4208 dimensions += "[]"; 4209 } 4210 4211 return dimensions; 4212 } 4213 4214 private JavaClass _getJavaClass(String fileName) throws IOException { 4215 int pos = fileName.indexOf(_implDir + "/"); 4216 4217 if (pos != -1) { 4218 pos += _implDir.length(); 4219 } 4220 else { 4221 pos = fileName.indexOf(_apiDir + "/") + _apiDir.length(); 4222 } 4223 4224 String srcFile = fileName.substring(pos + 1, fileName.length()); 4225 String className = StringUtil.replace( 4226 srcFile.substring(0, srcFile.length() - 5), "/", "."); 4227 4228 JavaClass javaClass = _javaClasses.get(className); 4229 4230 if (javaClass == null) { 4231 ClassLibrary classLibrary = new ClassLibrary(); 4232 4233 classLibrary.addClassLoader(getClass().getClassLoader()); 4234 4235 JavaDocBuilder builder = new JavaDocBuilder(classLibrary); 4236 4237 File file = new File(fileName); 4238 4239 if (!file.exists()) { 4240 return null; 4241 } 4242 4243 builder.addSource(file); 4244 4245 javaClass = builder.getClassByName(className); 4246 4247 _javaClasses.put(className, javaClass); 4248 } 4249 4250 return javaClass; 4251 } 4252 4253 private JavaMethod[] _getMethods(JavaClass javaClass) { 4254 return _getMethods(javaClass, false); 4255 } 4256 4257 private JavaMethod[] _getMethods( 4258 JavaClass javaClass, boolean superclasses) { 4259 4260 JavaMethod[] methods = javaClass.getMethods(superclasses); 4261 4262 for (JavaMethod method : methods) { 4263 Arrays.sort(method.getExceptions()); 4264 } 4265 4266 return methods; 4267 } 4268 4269 private String _getSessionTypeName(int sessionType) { 4270 if (sessionType == _SESSION_TYPE_LOCAL) { 4271 return "Local"; 4272 } 4273 else { 4274 return ""; 4275 } 4276 } 4277 4278 private String _getTplProperty(String key, String defaultValue) { 4279 return System.getProperty("service.tpl." + key, defaultValue); 4280 } 4281 4282 private List<String> _getTransients(Entity entity, boolean parent) 4283 throws Exception { 4284 4285 File modelFile = null; 4286 4287 if (parent) { 4288 modelFile = new File( 4289 _outputPath + "/model/impl/" + entity.getName() + 4290 "ModelImpl.java"); 4291 } 4292 else { 4293 modelFile = new File( 4294 _outputPath + "/model/impl/" + entity.getName() + "Impl.java"); 4295 } 4296 4297 String content = FileUtil.read(modelFile); 4298 4299 Matcher matcher = _getterPattern.matcher(content); 4300 4301 Set<String> getters = new HashSet<String>(); 4302 4303 while (!matcher.hitEnd()) { 4304 boolean found = matcher.find(); 4305 4306 if (found) { 4307 String property = matcher.group(); 4308 4309 if (property.indexOf("get") != -1) { 4310 property = property.substring( 4311 property.indexOf("get") + 3, property.length() - 1); 4312 } 4313 else { 4314 property = property.substring( 4315 property.indexOf("is") + 2, property.length() - 1); 4316 } 4317 4318 if (!entity.hasColumn(property) && 4319 !entity.hasColumn(Introspector.decapitalize(property))) { 4320 4321 property = Introspector.decapitalize(property); 4322 4323 getters.add(property); 4324 } 4325 } 4326 } 4327 4328 matcher = _setterPattern.matcher(content); 4329 4330 Set<String> setters = new HashSet<String>(); 4331 4332 while (!matcher.hitEnd()) { 4333 boolean found = matcher.find(); 4334 4335 if (found) { 4336 String property = matcher.group(); 4337 4338 property = property.substring( 4339 property.indexOf("set") + 3, property.length() - 1); 4340 4341 if (!entity.hasColumn(property) && 4342 !entity.hasColumn(Introspector.decapitalize(property))) { 4343 4344 property = Introspector.decapitalize(property); 4345 4346 setters.add(property); 4347 } 4348 } 4349 } 4350 4351 getters.retainAll(setters); 4352 4353 List<String> transients = new ArrayList<String>(getters); 4354 4355 Collections.sort(transients); 4356 4357 return transients; 4358 } 4359 4360 private boolean _hasHttpMethods(JavaClass javaClass) { 4361 JavaMethod[] methods = _getMethods(javaClass); 4362 4363 for (JavaMethod javaMethod : methods) { 4364 if (!javaMethod.isConstructor() && javaMethod.isPublic() && 4365 isCustomMethod(javaMethod)) { 4366 4367 return true; 4368 } 4369 } 4370 4371 return false; 4372 } 4373 4374 private List<Entity> _mergeReferenceList(List<Entity> referenceList) { 4375 List<Entity> list = new ArrayList<Entity>( 4376 _ejbList.size() + referenceList.size()); 4377 4378 list.addAll(_ejbList); 4379 list.addAll(referenceList); 4380 4381 return list; 4382 } 4383 4384 private void _parseEntity(Element entityElement) throws Exception { 4385 String ejbName = entityElement.attributeValue("name"); 4386 String humanName = entityElement.attributeValue("human-name"); 4387 4388 String table = entityElement.attributeValue("table"); 4389 4390 if (Validator.isNull(table)) { 4391 table = ejbName; 4392 4393 if (_badTableNames.contains(ejbName)) { 4394 table += StringPool.UNDERLINE; 4395 } 4396 4397 if (_autoNamespaceTables) { 4398 table = _portletShortName + StringPool.UNDERLINE + ejbName; 4399 } 4400 } 4401 4402 boolean uuid = GetterUtil.getBoolean( 4403 entityElement.attributeValue("uuid")); 4404 boolean uuidAccessor = GetterUtil.getBoolean( 4405 entityElement.attributeValue("uuid-accessor")); 4406 boolean localService = GetterUtil.getBoolean( 4407 entityElement.attributeValue("local-service")); 4408 boolean remoteService = GetterUtil.getBoolean( 4409 entityElement.attributeValue("remote-service"), true); 4410 String persistenceClass = GetterUtil.getString( 4411 entityElement.attributeValue("persistence-class"), 4412 _packagePath + ".service.persistence." + ejbName + 4413 "PersistenceImpl"); 4414 4415 String finderClass = ""; 4416 4417 if (FileUtil.exists( 4418 _outputPath + "/service/persistence/" + ejbName + 4419 "FinderImpl.java")) { 4420 4421 finderClass = 4422 _packagePath + ".service.persistence." + ejbName + "FinderImpl"; 4423 } 4424 4425 String dataSource = entityElement.attributeValue("data-source"); 4426 String sessionFactory = entityElement.attributeValue( 4427 "session-factory"); 4428 String txManager = entityElement.attributeValue( 4429 "tx-manager"); 4430 boolean cacheEnabled = GetterUtil.getBoolean( 4431 entityElement.attributeValue("cache-enabled"), true); 4432 boolean jsonEnabled = GetterUtil.getBoolean( 4433 entityElement.attributeValue("json-enabled"), remoteService); 4434 4435 List<EntityColumn> pkList = new ArrayList<EntityColumn>(); 4436 List<EntityColumn> regularColList = new ArrayList<EntityColumn>(); 4437 List<EntityColumn> blobList = new ArrayList<EntityColumn>(); 4438 List<EntityColumn> collectionList = new ArrayList<EntityColumn>(); 4439 List<EntityColumn> columnList = new ArrayList<EntityColumn>(); 4440 4441 List<Element> columnElements = entityElement.elements("column"); 4442 4443 boolean permissionedModel = false; 4444 4445 if (uuid) { 4446 Element columnElement = SAXReaderUtil.createElement("column"); 4447 4448 columnElement.addAttribute("name", "uuid"); 4449 columnElement.addAttribute("type", "String"); 4450 4451 columnElements.add(0, columnElement); 4452 } 4453 4454 for (Element columnElement : columnElements) { 4455 String columnName = columnElement.attributeValue("name"); 4456 4457 if (columnName.equals("resourceBlockId") && 4458 !ejbName.equals("ResourceBlock")) { 4459 4460 permissionedModel = true; 4461 } 4462 4463 String columnDBName = columnElement.attributeValue("db-name"); 4464 4465 if (Validator.isNull(columnDBName)) { 4466 columnDBName = columnName; 4467 4468 if (_badColumnNames.contains(columnName)) { 4469 columnDBName += StringPool.UNDERLINE; 4470 } 4471 } 4472 4473 String columnType = columnElement.attributeValue("type"); 4474 boolean primary = GetterUtil.getBoolean( 4475 columnElement.attributeValue("primary")); 4476 boolean accessor = GetterUtil.getBoolean( 4477 columnElement.attributeValue("accessor")); 4478 boolean filterPrimary = GetterUtil.getBoolean( 4479 columnElement.attributeValue("filter-primary")); 4480 String collectionEntity = columnElement.attributeValue("entity"); 4481 String mappingKey = columnElement.attributeValue("mapping-key"); 4482 4483 String mappingTable = columnElement.attributeValue("mapping-table"); 4484 4485 if (Validator.isNotNull(mappingTable)) { 4486 if (_badTableNames.contains(mappingTable)) { 4487 mappingTable += StringPool.UNDERLINE; 4488 } 4489 4490 if (_autoNamespaceTables) { 4491 mappingTable = 4492 _portletShortName + StringPool.UNDERLINE + mappingTable; 4493 } 4494 } 4495 4496 String idType = columnElement.attributeValue("id-type"); 4497 String idParam = columnElement.attributeValue("id-param"); 4498 boolean convertNull = GetterUtil.getBoolean( 4499 columnElement.attributeValue("convert-null"), true); 4500 boolean lazy = GetterUtil.getBoolean( 4501 columnElement.attributeValue("lazy"), true); 4502 boolean localized = GetterUtil.getBoolean( 4503 columnElement.attributeValue("localized")); 4504 boolean colJsonEnabled = GetterUtil.getBoolean( 4505 columnElement.attributeValue("json-enabled"), jsonEnabled); 4506 4507 EntityColumn col = new EntityColumn( 4508 columnName, columnDBName, columnType, primary, accessor, 4509 filterPrimary, collectionEntity, mappingKey, mappingTable, 4510 idType, idParam, convertNull, lazy, localized, colJsonEnabled); 4511 4512 if (primary) { 4513 pkList.add(col); 4514 } 4515 4516 if (columnType.equals("Collection")) { 4517 collectionList.add(col); 4518 } 4519 else { 4520 regularColList.add(col); 4521 4522 if (columnType.equals("Blob")) { 4523 blobList.add(col); 4524 } 4525 } 4526 4527 columnList.add(col); 4528 4529 if (Validator.isNotNull(collectionEntity) && 4530 Validator.isNotNull(mappingTable)) { 4531 4532 EntityMapping entityMapping = new EntityMapping( 4533 mappingTable, ejbName, collectionEntity); 4534 4535 int ejbNameWeight = StringUtil.startsWithWeight( 4536 mappingTable, ejbName); 4537 int collectionEntityWeight = StringUtil.startsWithWeight( 4538 mappingTable, collectionEntity); 4539 4540 if ((ejbNameWeight > collectionEntityWeight) || 4541 ((ejbNameWeight == collectionEntityWeight) && 4542 (ejbName.compareTo(collectionEntity) > 0))) { 4543 4544 _entityMappings.put(mappingTable, entityMapping); 4545 } 4546 } 4547 } 4548 4549 EntityOrder order = null; 4550 4551 Element orderElement = entityElement.element("order"); 4552 4553 if (orderElement != null) { 4554 boolean asc = true; 4555 4556 if ((orderElement.attribute("by") != null) && 4557 (orderElement.attributeValue("by").equals("desc"))) { 4558 4559 asc = false; 4560 } 4561 4562 List<EntityColumn> orderColsList = new ArrayList<EntityColumn>(); 4563 4564 order = new EntityOrder(asc, orderColsList); 4565 4566 List<Element> orderColumnElements = orderElement.elements( 4567 "order-column"); 4568 4569 for (Element orderColElement : orderColumnElements) { 4570 String orderColName = orderColElement.attributeValue("name"); 4571 boolean orderColCaseSensitive = GetterUtil.getBoolean( 4572 orderColElement.attributeValue("case-sensitive"), true); 4573 4574 boolean orderColByAscending = asc; 4575 4576 String orderColBy = GetterUtil.getString( 4577 orderColElement.attributeValue("order-by")); 4578 4579 if (orderColBy.equals("asc")) { 4580 orderColByAscending = true; 4581 } 4582 else if (orderColBy.equals("desc")) { 4583 orderColByAscending = false; 4584 } 4585 4586 EntityColumn col = Entity.getColumn(orderColName, columnList); 4587 4588 col.setOrderColumn(true); 4589 4590 col = (EntityColumn)col.clone(); 4591 4592 col.setCaseSensitive(orderColCaseSensitive); 4593 col.setOrderByAscending(orderColByAscending); 4594 4595 orderColsList.add(col); 4596 } 4597 } 4598 4599 List<EntityFinder> finderList = new ArrayList<EntityFinder>(); 4600 4601 List<Element> finderElements = entityElement.elements("finder"); 4602 4603 if (uuid) { 4604 Element finderElement = SAXReaderUtil.createElement("finder"); 4605 4606 finderElement.addAttribute("name", "Uuid"); 4607 finderElement.addAttribute("return-type", "Collection"); 4608 4609 Element finderColumnElement = finderElement.addElement( 4610 "finder-column"); 4611 4612 finderColumnElement.addAttribute("name", "uuid"); 4613 4614 finderElements.add(0, finderElement); 4615 4616 if (columnList.contains(new EntityColumn("groupId"))) { 4617 finderElement = SAXReaderUtil.createElement("finder"); 4618 4619 finderElement.addAttribute("name", "UUID_G"); 4620 finderElement.addAttribute("return-type", ejbName); 4621 finderElement.addAttribute("unique", "true"); 4622 4623 finderColumnElement = finderElement.addElement("finder-column"); 4624 4625 finderColumnElement.addAttribute("name", "uuid"); 4626 4627 finderColumnElement = finderElement.addElement("finder-column"); 4628 4629 finderColumnElement.addAttribute("name", "groupId"); 4630 4631 finderElements.add(1, finderElement); 4632 } 4633 } 4634 4635 if (permissionedModel) { 4636 Element finderElement = SAXReaderUtil.createElement("finder"); 4637 4638 finderElement.addAttribute("name", "ResourceBlockId"); 4639 finderElement.addAttribute("return-type", "Collection"); 4640 4641 Element finderColumnElement = finderElement.addElement( 4642 "finder-column"); 4643 4644 finderColumnElement.addAttribute("name", "resourceBlockId"); 4645 4646 finderElements.add(0, finderElement); 4647 } 4648 4649 String alias = TextFormatter.format(ejbName, TextFormatter.I); 4650 4651 if (_badAliasNames.contains(alias.toLowerCase())) { 4652 alias += StringPool.UNDERLINE; 4653 } 4654 4655 for (Element finderElement : finderElements) { 4656 String finderName = finderElement.attributeValue("name"); 4657 String finderReturn = finderElement.attributeValue("return-type"); 4658 boolean finderUnique = GetterUtil.getBoolean( 4659 finderElement.attributeValue("unique")); 4660 4661 String finderWhere = finderElement.attributeValue("where"); 4662 4663 if (Validator.isNotNull(finderWhere)) { 4664 for (EntityColumn column: columnList) { 4665 String name = column.getName(); 4666 4667 if (finderWhere.indexOf(name) != -1) { 4668 finderWhere = finderWhere.replaceAll( 4669 name, alias + "." + name); 4670 } 4671 } 4672 } 4673 4674 boolean finderDBIndex = GetterUtil.getBoolean( 4675 finderElement.attributeValue("db-index"), true); 4676 4677 List<EntityColumn> finderColsList = 4678 new ArrayList<EntityColumn>(); 4679 4680 List<Element> finderColumnElements = finderElement.elements( 4681 "finder-column"); 4682 4683 for (Element finderColumnElement : finderColumnElements) { 4684 String finderColName = finderColumnElement.attributeValue( 4685 "name"); 4686 boolean finderColCaseSensitive = GetterUtil.getBoolean( 4687 finderColumnElement.attributeValue("case-sensitive"), 4688 true); 4689 String finderColComparator = GetterUtil.getString( 4690 finderColumnElement.attributeValue("comparator"), "="); 4691 String finderColArrayableOperator = 4692 GetterUtil.getString( 4693 finderColumnElement.attributeValue( 4694 "arrayable-operator")); 4695 4696 EntityColumn col = Entity.getColumn(finderColName, columnList); 4697 4698 if (!col.isFinderPath()) { 4699 col.setFinderPath(true); 4700 } 4701 4702 col = (EntityColumn)col.clone(); 4703 4704 col.setCaseSensitive(finderColCaseSensitive); 4705 col.setComparator(finderColComparator); 4706 col.setArrayableOperator(finderColArrayableOperator); 4707 4708 finderColsList.add(col); 4709 } 4710 4711 finderList.add( 4712 new EntityFinder( 4713 finderName, finderReturn, finderUnique, finderWhere, 4714 finderDBIndex, finderColsList)); 4715 } 4716 4717 List<Entity> referenceList = new ArrayList<Entity>(); 4718 4719 if (_build) { 4720 if (Validator.isNotNull(_pluginName)) { 4721 for (String config : PropsValues.RESOURCE_ACTIONS_CONFIGS) { 4722 File file = new File(_implDir + "/" + config); 4723 4724 if (file.exists()) { 4725 InputStream inputStream = new FileInputStream(file); 4726 4727 ResourceActionsUtil.read(_pluginName, inputStream); 4728 } 4729 } 4730 } 4731 4732 List<Element> referenceElements = entityElement.elements( 4733 "reference"); 4734 4735 Set<String> referenceSet = new TreeSet<String>(); 4736 4737 for (Element referenceElement : referenceElements) { 4738 String referencePackage = referenceElement.attributeValue( 4739 "package-path"); 4740 String referenceEntity = referenceElement.attributeValue( 4741 "entity"); 4742 4743 referenceSet.add(referencePackage + "." + referenceEntity); 4744 } 4745 4746 if (!_packagePath.equals("com.liferay.counter")) { 4747 referenceSet.add("com.liferay.counter.Counter"); 4748 } 4749 4750 if (!_packagePath.equals("com.liferay.portal")) { 4751 referenceSet.add("com.liferay.portal.Resource"); 4752 referenceSet.add("com.liferay.portal.User"); 4753 } 4754 4755 for (String referenceName : referenceSet) { 4756 referenceList.add(getEntity(referenceName)); 4757 } 4758 } 4759 4760 List<String> txRequiredList = new ArrayList<String>(); 4761 4762 List<Element> txRequiredElements = entityElement.elements( 4763 "tx-required"); 4764 4765 for (Element txRequiredEl : txRequiredElements) { 4766 String txRequired = txRequiredEl.getText(); 4767 4768 txRequiredList.add(txRequired); 4769 } 4770 4771 _ejbList.add( 4772 new Entity( 4773 _packagePath, _portletName, _portletShortName, ejbName, 4774 humanName, table, alias, uuid, uuidAccessor, localService, 4775 remoteService, persistenceClass, finderClass, dataSource, 4776 sessionFactory, txManager, cacheEnabled, jsonEnabled, pkList, 4777 regularColList, blobList, collectionList, columnList, order, 4778 finderList, referenceList, txRequiredList)); 4779 } 4780 4781 private String _processTemplate(String name) throws Exception { 4782 return _processTemplate(name, _getContext()); 4783 } 4784 4785 private String _processTemplate(String name, Map<String, Object> context) 4786 throws Exception { 4787 4788 return StringUtil.strip(FreeMarkerUtil.process(name, context), '\r'); 4789 } 4790 4791 private Set<String> _readLines(String fileName) throws Exception { 4792 ClassLoader classLoader = getClass().getClassLoader(); 4793 4794 Set<String> lines = new HashSet<String>(); 4795 4796 StringUtil.readLines(classLoader.getResourceAsStream(fileName), lines); 4797 4798 return lines; 4799 } 4800 4801 private void _updateSQLFile( 4802 String sqlFileName, String createTableSQL, Entity entity) 4803 throws IOException { 4804 4805 File updateSQLFile = new File(_sqlDir + "/" + sqlFileName); 4806 4807 if (updateSQLFile.exists()) { 4808 _createSQLTables(updateSQLFile, createTableSQL, entity, false); 4809 } 4810 } 4811 4812 private static final int _SESSION_TYPE_LOCAL = 1; 4813 4814 private static final int _SESSION_TYPE_REMOTE = 0; 4815 4816 private static Pattern _setterPattern = Pattern.compile( 4817 "public void set.*" + Pattern.quote("(")); 4818 4819 private static final String _SQL_CREATE_TABLE = "create table "; 4820 4821 private static final String _TPL_ROOT = 4822 "com/liferay/portal/tools/servicebuilder/dependencies/"; 4823 4824 private static Pattern _getterPattern = Pattern.compile( 4825 "public .* get.*" + Pattern.quote("(") + "|public boolean is.*" + 4826 Pattern.quote("(")); 4827 4828 private String _apiDir; 4829 private String _author; 4830 private boolean _autoNamespaceTables; 4831 private Set<String> _badAliasNames; 4832 private Set<String> _badColumnNames; 4833 private Set<String> _badJsonTypes; 4834 private Set<String> _badTableNames; 4835 private String _beanLocatorUtil; 4836 private String _beanLocatorUtilShortName; 4837 private boolean _build; 4838 private List<Entity> _ejbList; 4839 private Map<String, EntityMapping> _entityMappings; 4840 private Map<String, Entity> _entityPool = new HashMap<String, Entity>(); 4841 private String _hbmFileName; 4842 private String _implDir; 4843 private Map<String, JavaClass> _javaClasses = 4844 new HashMap<String, JavaClass>(); 4845 private String _jsonFileName; 4846 private String _modelHintsFileName; 4847 private String _ormFileName; 4848 private String _outputPath; 4849 private String _packagePath; 4850 private String _pluginName; 4851 private String _portletName = StringPool.BLANK; 4852 private String _portletPackageName = StringPool.BLANK; 4853 private String _portletShortName = StringPool.BLANK; 4854 private String _propsUtil; 4855 private String _remotingFileName; 4856 private String _serviceOutputPath; 4857 private String _springBaseFileName; 4858 private String _springClusterFileName; 4859 private String _springDynamicDataSourceFileName; 4860 private String _springFileName; 4861 private String _springHibernateFileName; 4862 private String _springInfrastructureFileName; 4863 private String _springShardDataSourceFileName; 4864 private String _sqlDir; 4865 private String _sqlFileName; 4866 private String _sqlIndexesFileName; 4867 private String _sqlIndexesPropertiesFileName; 4868 private String _sqlSequencesFileName; 4869 private String _testDir; 4870 private String _testOutputPath; 4871 private String _tplBadAliasNames = _TPL_ROOT + "bad_alias_names.txt"; 4872 private String _tplBadColumnNames = _TPL_ROOT + "bad_column_names.txt"; 4873 private String _tplBadJsonTypes = _TPL_ROOT + "bad_json_types.txt"; 4874 private String _tplBadTableNames = _TPL_ROOT + "bad_table_names.txt"; 4875 private String _tplBlobModel = _TPL_ROOT + "blob_model.ftl"; 4876 private String _tplEjbPk = _TPL_ROOT + "ejb_pk.ftl"; 4877 private String _tplException = _TPL_ROOT + "exception.ftl"; 4878 private String _tplExtendedModel = _TPL_ROOT + "extended_model.ftl"; 4879 private String _tplExtendedModelBaseImpl = 4880 _TPL_ROOT + "extended_model_base_impl.ftl"; 4881 private String _tplExtendedModelImpl = 4882 _TPL_ROOT + "extended_model_impl.ftl"; 4883 private String _tplFinder = _TPL_ROOT + "finder.ftl"; 4884 private String _tplFinderUtil = _TPL_ROOT + "finder_util.ftl"; 4885 private String _tplHbmXml = _TPL_ROOT + "hbm_xml.ftl"; 4886 private String _tplJsonJs = _TPL_ROOT + "json_js.ftl"; 4887 private String _tplJsonJsMethod = _TPL_ROOT + "json_js_method.ftl"; 4888 private String _tplModel = _TPL_ROOT + "model.ftl"; 4889 private String _tplModelCache = _TPL_ROOT + "model_cache.ftl"; 4890 private String _tplModelClp = _TPL_ROOT + "model_clp.ftl"; 4891 private String _tplModelHintsXml = _TPL_ROOT + "model_hints_xml.ftl"; 4892 private String _tplModelImpl = _TPL_ROOT + "model_impl.ftl"; 4893 private String _tplModelSoap = _TPL_ROOT + "model_soap.ftl"; 4894 private String _tplModelWrapper = _TPL_ROOT + "model_wrapper.ftl"; 4895 private String _tplOrmXml = _TPL_ROOT + "orm_xml.ftl"; 4896 private String _tplPersistence = _TPL_ROOT + "persistence.ftl"; 4897 private String _tplPersistenceImpl = _TPL_ROOT + "persistence_impl.ftl"; 4898 private String _tplPersistenceTest = _TPL_ROOT + "persistence_test.ftl"; 4899 private String _tplPersistenceUtil = _TPL_ROOT + "persistence_util.ftl"; 4900 private String _tplProps = _TPL_ROOT + "props.ftl"; 4901 private String _tplRemotingXml = _TPL_ROOT + "remoting_xml.ftl"; 4902 private String _tplService = _TPL_ROOT + "service.ftl"; 4903 private String _tplServiceBaseImpl = _TPL_ROOT + "service_base_impl.ftl"; 4904 private String _tplServiceClp = _TPL_ROOT + "service_clp.ftl"; 4905 private String _tplServiceClpMessageListener = 4906 _TPL_ROOT + "service_clp_message_listener.ftl"; 4907 private String _tplServiceClpSerializer = 4908 _TPL_ROOT + "service_clp_serializer.ftl"; 4909 private String _tplServiceHttp = _TPL_ROOT + "service_http.ftl"; 4910 private String _tplServiceImpl = _TPL_ROOT + "service_impl.ftl"; 4911 private String _tplServiceSoap = _TPL_ROOT + "service_soap.ftl"; 4912 private String _tplServiceUtil = _TPL_ROOT + "service_util.ftl"; 4913 private String _tplServiceWrapper = _TPL_ROOT + "service_wrapper.ftl"; 4914 private String _tplSpringBaseXml = _TPL_ROOT + "spring_base_xml.ftl"; 4915 private String _tplSpringClusterXml = _TPL_ROOT + "spring_cluster_xml.ftl"; 4916 private String _tplSpringDynamicDataSourceXml = 4917 _TPL_ROOT + "spring_dynamic_data_source_xml.ftl"; 4918 private String _tplSpringHibernateXml = 4919 _TPL_ROOT + "spring_hibernate_xml.ftl"; 4920 private String _tplSpringInfrastructureXml = 4921 _TPL_ROOT + "spring_infrastructure_xml.ftl"; 4922 private String _tplSpringShardDataSourceXml = 4923 _TPL_ROOT + "spring_shard_data_source_xml.ftl"; 4924 private String _tplSpringXml = _TPL_ROOT + "spring_xml.ftl"; 4925 4926 }