0001: /**
0002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
0003: *
0004: * Permission is hereby granted, free of charge, to any person obtaining a copy
0005: * of this software and associated documentation files (the "Software"), to deal
0006: * in the Software without restriction, including without limitation the rights
0007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008: * copies of the Software, and to permit persons to whom the Software is
0009: * furnished to do so, subject to the following conditions:
0010: *
0011: * The above copyright notice and this permission notice shall be included in
0012: * all copies or substantial portions of the Software.
0013: *
0014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0020: * SOFTWARE.
0021: */package com.liferay.portal.tools.servicebuilder;
0022:
0023: import com.liferay.portal.freemarker.FreeMarkerUtil;
0024: import com.liferay.portal.kernel.util.ArrayUtil;
0025: import com.liferay.portal.kernel.util.GetterUtil;
0026: import com.liferay.portal.kernel.util.PropertiesUtil;
0027: import com.liferay.portal.kernel.util.StringMaker;
0028: import com.liferay.portal.kernel.util.StringPool;
0029: import com.liferay.portal.kernel.util.StringUtil;
0030: import com.liferay.portal.kernel.util.StringUtil_IW;
0031: import com.liferay.portal.kernel.util.Validator;
0032: import com.liferay.portal.model.ModelHintsUtil;
0033: import com.liferay.portal.tools.SourceFormatter;
0034: import com.liferay.portal.util.PortalUtil;
0035: import com.liferay.util.FileUtil;
0036: import com.liferay.util.TextFormatter;
0037: import com.liferay.util.Time;
0038: import com.liferay.util.log4j.Log4JUtil;
0039: import com.liferay.util.xml.XMLFormatter;
0040:
0041: import com.thoughtworks.qdox.JavaDocBuilder;
0042: import com.thoughtworks.qdox.model.JavaClass;
0043: import com.thoughtworks.qdox.model.JavaMethod;
0044: import com.thoughtworks.qdox.model.JavaParameter;
0045: import com.thoughtworks.qdox.model.Type;
0046:
0047: import de.hunsicker.io.FileFormat;
0048: import de.hunsicker.jalopy.Jalopy;
0049: import de.hunsicker.jalopy.storage.Convention;
0050: import de.hunsicker.jalopy.storage.ConventionKeys;
0051: import de.hunsicker.jalopy.storage.Environment;
0052:
0053: import freemarker.ext.beans.BeansWrapper;
0054: import freemarker.template.TemplateHashModel;
0055: import freemarker.template.TemplateModelException;
0056:
0057: import java.io.BufferedReader;
0058: import java.io.File;
0059: import java.io.FileNotFoundException;
0060: import java.io.FileReader;
0061: import java.io.IOException;
0062: import java.io.StringReader;
0063:
0064: import java.util.ArrayList;
0065: import java.util.HashMap;
0066: import java.util.HashSet;
0067: import java.util.Iterator;
0068: import java.util.LinkedHashSet;
0069: import java.util.List;
0070: import java.util.Map;
0071: import java.util.Properties;
0072: import java.util.Set;
0073: import java.util.TreeMap;
0074: import java.util.TreeSet;
0075:
0076: import org.dom4j.Document;
0077: import org.dom4j.DocumentException;
0078: import org.dom4j.DocumentHelper;
0079: import org.dom4j.Element;
0080:
0081: /**
0082: * <a href="ServiceBuilder.java.html"><b><i>View Source</i></b></a>
0083: *
0084: * @author Brian Wing Shun Chan
0085: * @author Charles May
0086: * @author Alexander Chow
0087: * @author Harry Mark
0088: * @author Tariq Dweik
0089: *
0090: */
0091: public class ServiceBuilder {
0092:
0093: public static void main(String[] args) {
0094: ClassLoader classLoader = ClassLoader.getSystemClassLoader();
0095:
0096: Log4JUtil.configureLog4J(classLoader
0097: .getResource("META-INF/portal-log4j.xml"));
0098: Log4JUtil.configureLog4J(classLoader
0099: .getResource("META-INF/portal-log4j-ext.xml"));
0100:
0101: ServiceBuilder serviceBuilder = null;
0102:
0103: if (args.length == 7) {
0104: String fileName = args[0];
0105: String hbmFileName = args[1];
0106: String modelHintsFileName = args[2];
0107: String springFileName = args[3];
0108: String springDataSourceFileName = "";
0109: String apiDir = args[5];
0110: String implDir = "src";
0111: String jsonFileName = args[6];
0112: String remotingFileName = "../tunnel-web/docroot/WEB-INF/remoting-servlet.xml";
0113: String sqlDir = "../sql";
0114: String sqlFileName = "portal-tables.sql";
0115: boolean autoNamespaceTables = false;
0116: String baseModelImplPackage = "com.liferay.portal.model.impl";
0117: String basePersistencePackage = "com.liferay.portal.service.persistence";
0118: String beanLocatorUtilPackage = "com.liferay.portal.kernel.bean";
0119: String principalBeanPackage = "com.liferay.portal.service.impl";
0120: String propsUtilPackage = "com.liferay.portal.util";
0121: String springHibernatePackage = "com.liferay.portal.spring.hibernate";
0122: String springUtilPackage = "com.liferay.portal.spring.util";
0123: String testDir = "";
0124:
0125: serviceBuilder = new ServiceBuilder(fileName, hbmFileName,
0126: modelHintsFileName, springFileName,
0127: springDataSourceFileName, apiDir, implDir,
0128: jsonFileName, remotingFileName, sqlDir,
0129: sqlFileName, autoNamespaceTables,
0130: baseModelImplPackage, basePersistencePackage,
0131: beanLocatorUtilPackage, principalBeanPackage,
0132: propsUtilPackage, springHibernatePackage,
0133: springUtilPackage, testDir);
0134: } else if (args.length == 0) {
0135: String fileName = System.getProperty("service.input.file");
0136: String hbmFileName = System.getProperty("service.hbm.file");
0137: String modelHintsFileName = System
0138: .getProperty("service.model.hints.file");
0139: String springFileName = System
0140: .getProperty("service.spring.file");
0141: String springDataSourceFileName = System
0142: .getProperty("service.spring.data.source.file");
0143: String apiDir = System.getProperty("service.api.dir");
0144: String implDir = System.getProperty("service.impl.dir");
0145: String jsonFileName = System
0146: .getProperty("service.json.file");
0147: String remotingFileName = System
0148: .getProperty("service.remoting.file");
0149: String sqlDir = System.getProperty("service.sql.dir");
0150: String sqlFileName = System.getProperty("service.sql.file");
0151: boolean autoNamespaceTables = GetterUtil.getBoolean(System
0152: .getProperty("service.auto.namespace.tables"));
0153: String baseModelImplPackage = System
0154: .getProperty("service.base.model.impl.package");
0155: String basePersistencePackage = System
0156: .getProperty("service.base.persistence.package");
0157: String beanLocatorUtilPackage = System
0158: .getProperty("service.bean.locator.util.package");
0159: String principalBeanPackage = System
0160: .getProperty("service.principal.bean.package");
0161: String propsUtilPackage = System
0162: .getProperty("service.props.util.package");
0163: String springHibernatePackage = System
0164: .getProperty("service.spring.hibernate.package");
0165: String springUtilPackage = System
0166: .getProperty("service.spring.util.package");
0167: String testDir = System.getProperty("service.test.dir");
0168:
0169: serviceBuilder = new ServiceBuilder(fileName, hbmFileName,
0170: modelHintsFileName, springFileName,
0171: springDataSourceFileName, apiDir, implDir,
0172: jsonFileName, remotingFileName, sqlDir,
0173: sqlFileName, autoNamespaceTables,
0174: baseModelImplPackage, basePersistencePackage,
0175: beanLocatorUtilPackage, principalBeanPackage,
0176: propsUtilPackage, springHibernatePackage,
0177: springUtilPackage, testDir);
0178: }
0179:
0180: if (serviceBuilder == null) {
0181: System.out
0182: .println("Please set these required system properties. Sample values are:\n"
0183: + "\n"
0184: + "\t-Dservice.input.file=${service.file}\n"
0185: + "\t-Dservice.hbm.file=classes/META-INF/portal-hbm.xml\n"
0186: + "\t-Dservice.model.hints.file=classes/META-INF/portal-model-hints.xml\n"
0187: + "\t-Dservice.spring.file=classes/META-INF/portal-spring.xml\n"
0188: + "\t-Dservice.api.dir=${project.dir}/portal-service/src\n"
0189: + "\t-Dservice.impl.dir=src\n"
0190: + "\t-Dservice.json.file=${project.dir}/portal-web/docroot/html/js/liferay/service_unpacked.js\n"
0191: + "\t-Dservice.remoting.file=${project.dir}/tunnel-web/docroot/WEB-INF/remoting-servlet.xml\n"
0192: + "\t-Dservice.sql.dir=../sql\n"
0193: + "\t-Dservice.sql.file=portal-tables.sql\n"
0194: + "\t-Dservice.base.model.impl.package=com.liferay.portal.model.impl\n"
0195: + "\t-Dservice.base.persistence.package=com.liferay.portal.service.persistence\n"
0196: + "\t-Dservice.bean.locator.util.package=com.liferay.portal.kernel.bean\n"
0197: + "\t-Dservice.principal.bean.package=com.liferay.portal.service.impl\n"
0198: + "\t-Dservice.props.util.package=com.liferay.portal.util\n"
0199: + "\t-Dservice.spring.hibernate.package=com.liferay.portal.spring.hibernate\n"
0200: + "\t-Dservice.spring.util.package=com.liferay.portal.spring.util\n"
0201: + "\n"
0202: + "You can also customize the generated code by overriding the default templates with these optional properties:\n"
0203: + "\n"
0204: + "\t-Dservice.tpl.base_mode_impl.ftl="
0205: + _TPL_ROOT
0206: + "base_mode_impl.ftl\n"
0207: + "\t-Dservice.tpl.base_persistence.ftl="
0208: + _TPL_ROOT
0209: + "base_persistence.ftl\n"
0210: + "\t-Dservice.tpl.bean_locator_util.ftl="
0211: + _TPL_ROOT
0212: + "bean_locator_util.ftl\n"
0213: + "\t-Dservice.tpl.copyright.txt=copyright.txt\n"
0214: + "\t-Dservice.tpl.dynamic_dialect.ftl="
0215: + _TPL_ROOT
0216: + "dynamic_dialect.ftl\n"
0217: + "\t-Dservice.tpl.ejb_pk.ftl="
0218: + _TPL_ROOT
0219: + "ejb_pk.ftl\n"
0220: + "\t-Dservice.tpl.exception.ftl="
0221: + _TPL_ROOT
0222: + "exception.ftl\n"
0223: + "\t-Dservice.tpl.extended_model.ftl="
0224: + _TPL_ROOT
0225: + "extended_model.ftl\n"
0226: + "\t-Dservice.tpl.extended_model_impl.ftl="
0227: + _TPL_ROOT
0228: + "extended_model_impl.ftl\n"
0229: + "\t-Dservice.tpl.finder.ftl="
0230: + _TPL_ROOT
0231: + "finder.ftl\n"
0232: + "\t-Dservice.tpl.finder_cache.ftl="
0233: + _TPL_ROOT
0234: + "finder_cache.ftl\n"
0235: + "\t-Dservice.tpl.finder_util.ftl="
0236: + _TPL_ROOT
0237: + "finder_util.ftl\n"
0238: + "\t-Dservice.tpl.hbm_xml.ftl="
0239: + _TPL_ROOT
0240: + "hbm_xml.ftl\n"
0241: + "\t-Dservice.tpl.hibernate_configuration.ftl="
0242: + _TPL_ROOT
0243: + "hibernate_configuration.ftl\n"
0244: + "\t-Dservice.tpl.hibernate_util.ftl="
0245: + _TPL_ROOT
0246: + "hibernate_util.ftl\n"
0247: + "\t-Dservice.tpl.json_js.ftl="
0248: + _TPL_ROOT
0249: + "json_js.ftl\n"
0250: + "\t-Dservice.tpl.json_js_method.ftl="
0251: + _TPL_ROOT
0252: + "json_js_method.ftl\n"
0253: + "\t-Dservice.tpl.model.ftl="
0254: + _TPL_ROOT
0255: + "model.ftl\n"
0256: + "\t-Dservice.tpl.model_hints_xml.ftl="
0257: + _TPL_ROOT
0258: + "model_hints_xml.ftl\n"
0259: + "\t-Dservice.tpl.model_impl.ftl="
0260: + _TPL_ROOT
0261: + "model_impl.ftl\n"
0262: + "\t-Dservice.tpl.model_soap.ftl="
0263: + _TPL_ROOT
0264: + "model_soap.ftl\n"
0265: + "\t-Dservice.tpl.persistence.ftl="
0266: + _TPL_ROOT
0267: + "persistence.ftl\n"
0268: + "\t-Dservice.tpl.persistence_impl.ftl="
0269: + _TPL_ROOT
0270: + "persistence_impl.ftl\n"
0271: + "\t-Dservice.tpl.persistence_util.ftl="
0272: + _TPL_ROOT
0273: + "persistence_util.ftl\n"
0274: + "\t-Dservice.tpl.principal_bean.ftl="
0275: + _TPL_ROOT
0276: + "principal_bean.ftl\n"
0277: + "\t-Dservice.tpl.props.ftl="
0278: + _TPL_ROOT
0279: + "props.ftl\n"
0280: + "\t-Dservice.tpl.props_util.ftl="
0281: + _TPL_ROOT
0282: + "props_util.ftl\n"
0283: + "\t-Dservice.tpl.remoting_xml.ftl="
0284: + _TPL_ROOT
0285: + "remoting_xml.ftl\n"
0286: + "\t-Dservice.tpl.service.ftl="
0287: + _TPL_ROOT
0288: + "service.ftl\n"
0289: + "\t-Dservice.tpl.service_base_impl.ftl="
0290: + _TPL_ROOT
0291: + "service_base_impl.ftl\n"
0292: + "\t-Dservice.tpl.service_factory.ftl="
0293: + _TPL_ROOT
0294: + "service_factory.ftl\n"
0295: + "\t-Dservice.tpl.service_http.ftl="
0296: + _TPL_ROOT
0297: + "service_http.ftl\n"
0298: + "\t-Dservice.tpl.service_impl.ftl="
0299: + _TPL_ROOT
0300: + "service_impl.ftl\n"
0301: + "\t-Dservice.tpl.service_json.ftl="
0302: + _TPL_ROOT
0303: + "service_json.ftl\n"
0304: + "\t-Dservice.tpl.service_json_serializer.ftl="
0305: + _TPL_ROOT
0306: + "service_json_serializer.ftl\n"
0307: + "\t-Dservice.tpl.service_soap.ftl="
0308: + _TPL_ROOT
0309: + "service_soap.ftl\n"
0310: + "\t-Dservice.tpl.service_util.ftl="
0311: + _TPL_ROOT
0312: + "service_util.ftl\n"
0313: + "\t-Dservice.tpl.spring_data_source_xml.ftl="
0314: + _TPL_ROOT
0315: + "spring_data_source_xml.ftl\n"
0316: + "\t-Dservice.tpl.spring_util.ftl="
0317: + _TPL_ROOT
0318: + "spring_util.ftl\n"
0319: + "\t-Dservice.tpl.spring_xml.ftl="
0320: + _TPL_ROOT
0321: + "spring_xml.ftl\n"
0322: + "\t-Dservice.tpl.spring_xml_session.ftl="
0323: + _TPL_ROOT + "spring_xml_session.ftl");
0324: }
0325: }
0326:
0327: public static Set getBadCmpFields() {
0328: Set badCmpFields = new HashSet();
0329:
0330: badCmpFields.add("access");
0331: badCmpFields.add("active");
0332: badCmpFields.add("alias");
0333: badCmpFields.add("code");
0334: badCmpFields.add("data");
0335: badCmpFields.add("date");
0336: badCmpFields.add("end");
0337: badCmpFields.add("idd");
0338: badCmpFields.add("featured");
0339: badCmpFields.add("fields");
0340: badCmpFields.add("from");
0341: badCmpFields.add("hidden");
0342: badCmpFields.add("id");
0343: badCmpFields.add("index");
0344: badCmpFields.add("internal");
0345: badCmpFields.add("interval");
0346: badCmpFields.add("join");
0347: badCmpFields.add("key");
0348: badCmpFields.add("log");
0349: badCmpFields.add("number");
0350: badCmpFields.add("password");
0351: badCmpFields.add("path");
0352: badCmpFields.add("primary");
0353: badCmpFields.add("sale");
0354: badCmpFields.add("settings");
0355: badCmpFields.add("size");
0356: badCmpFields.add("start");
0357: badCmpFields.add("text");
0358: badCmpFields.add("to");
0359: badCmpFields.add("type");
0360: badCmpFields.add("uuid");
0361: badCmpFields.add("values");
0362:
0363: return badCmpFields;
0364: }
0365:
0366: public static Set getBadTableNames() {
0367: Set badTableNames = new HashSet();
0368:
0369: badTableNames.add("Account");
0370: badTableNames.add("Action");
0371: badTableNames.add("Cache");
0372: badTableNames.add("ClassName");
0373: badTableNames.add("Contact");
0374: badTableNames.add("Group");
0375: badTableNames.add("Organization");
0376: badTableNames.add("Permission");
0377: badTableNames.add("Release");
0378: badTableNames.add("Resource");
0379: badTableNames.add("Role");
0380: badTableNames.add("User");
0381:
0382: return badTableNames;
0383: }
0384:
0385: public static void writeFile(File file, String content)
0386: throws IOException {
0387: writeFile(file, content, null);
0388: }
0389:
0390: public static void writeFile(File file, String content,
0391: Map jalopySettings) throws IOException {
0392:
0393: String packagePath = _getPackagePath(file);
0394:
0395: String className = file.getName();
0396:
0397: className = className.substring(0, className.length() - 5);
0398:
0399: content = SourceFormatter.stripImports(content, packagePath,
0400: className);
0401:
0402: File tempFile = new File("ServiceBuilder.temp");
0403:
0404: FileUtil.write(tempFile, content);
0405:
0406: // Beautify
0407:
0408: StringBuffer sb = new StringBuffer();
0409:
0410: Jalopy jalopy = new Jalopy();
0411:
0412: jalopy.setFileFormat(FileFormat.UNIX);
0413: jalopy.setInput(tempFile);
0414: jalopy.setOutput(sb);
0415:
0416: try {
0417: Jalopy.setConvention("../tools/jalopy.xml");
0418: } catch (FileNotFoundException fnne) {
0419: }
0420:
0421: try {
0422: Jalopy.setConvention("../../misc/jalopy.xml");
0423: } catch (FileNotFoundException fnne) {
0424: }
0425:
0426: if (jalopySettings == null) {
0427: jalopySettings = new HashMap();
0428: }
0429:
0430: Environment env = Environment.getInstance();
0431:
0432: // Author
0433:
0434: String author = GetterUtil.getString((String) jalopySettings
0435: .get("author"), "Brian Wing Shun Chan");
0436:
0437: env.set("author", author);
0438:
0439: // File name
0440:
0441: env.set("fileName", file.getName());
0442:
0443: Convention convention = Convention.getInstance();
0444:
0445: String classMask = "/**\n"
0446: + " * <a href=\"$fileName$.html\"><b><i>View Source</i></b></a>\n"
0447: + " *\n";
0448:
0449: String[] classCommentsArray = (String[]) jalopySettings
0450: .get("classComments");
0451:
0452: if ((classCommentsArray != null)
0453: && (classCommentsArray.length > 0)) {
0454: for (int i = 0; i < classCommentsArray.length; i++) {
0455: String classComments = classCommentsArray[i];
0456:
0457: //classComments = "The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.";
0458: classComments = StringUtil.wrap(classComments, 76,
0459: "\n * ");
0460:
0461: if (classComments.startsWith("\n")) {
0462: classComments = classComments.substring(1,
0463: classComments.length());
0464: }
0465:
0466: classMask += " * <p>\n" + classComments
0467: + "\n * </p>\n *\n";
0468: }
0469: }
0470:
0471: classMask += " * @author $author$\n" + " *\n";
0472:
0473: String[] seeArray = (String[]) jalopySettings.get("see");
0474:
0475: if ((classCommentsArray != null)
0476: && (classCommentsArray.length > 0)) {
0477: for (int i = 0; i < seeArray.length; i++) {
0478: String see = seeArray[i];
0479:
0480: classMask += " * @see " + see + "\n";
0481: }
0482:
0483: classMask += " *\n";
0484: }
0485:
0486: classMask += " */";
0487:
0488: convention.put(ConventionKeys.COMMENT_JAVADOC_TEMPLATE_CLASS,
0489: env.interpolate(classMask));
0490:
0491: convention.put(
0492: ConventionKeys.COMMENT_JAVADOC_TEMPLATE_INTERFACE, env
0493: .interpolate(classMask));
0494:
0495: if (jalopySettings.get("keepJavadoc") != null) {
0496: convention.put(ConventionKeys.COMMENT_JAVADOC_REMOVE,
0497: Boolean.FALSE.toString());
0498: }
0499:
0500: jalopy.format();
0501:
0502: String newContent = sb.toString();
0503:
0504: /*
0505: // Remove blank lines after try {
0506:
0507: newContent = StringUtil.replace(newContent, "try {\n\n", "try {\n");
0508:
0509: // Remove blank lines after ) {
0510:
0511: newContent = StringUtil.replace(newContent, ") {\n\n", ") {\n");
0512:
0513: // Remove blank lines empty braces { }
0514:
0515: newContent = StringUtil.replace(newContent, "\n\n\t}", "\n\t}");
0516:
0517: // Add space to last }
0518:
0519: newContent = newContent.substring(0, newContent.length() - 2) + "\n\n}";
0520: */
0521:
0522: // Write file if and only if the file has changed
0523: String oldContent = null;
0524:
0525: if (file.exists()) {
0526:
0527: // Read file
0528:
0529: oldContent = FileUtil.read(file);
0530:
0531: // Keep old version number
0532:
0533: int x = oldContent.indexOf("@version $Revision:");
0534:
0535: if (x != -1) {
0536: int y = oldContent.indexOf("$", x);
0537: y = oldContent.indexOf("$", y + 1);
0538:
0539: String oldVersion = oldContent.substring(x, y + 1);
0540:
0541: newContent = StringUtil.replace(newContent,
0542: "@version $Rev: $", oldVersion);
0543: }
0544: } else {
0545: newContent = StringUtil.replace(newContent,
0546: "@version $Rev: $", "@version $Revision: 1.183 $");
0547: }
0548:
0549: if (oldContent == null || !oldContent.equals(newContent)) {
0550: FileUtil.write(file, newContent);
0551:
0552: System.out.println("Writing " + file);
0553:
0554: // Workaround for bug with XJavaDoc
0555:
0556: file.setLastModified(System.currentTimeMillis()
0557: - (Time.SECOND * 5));
0558: }
0559:
0560: tempFile.deleteOnExit();
0561: }
0562:
0563: public ServiceBuilder(String fileName, String hbmFileName,
0564: String modelHintsFileName, String springFileName,
0565: String springDataSourceFileName, String apiDir,
0566: String implDir, String jsonFileName,
0567: String remotingFileName, String sqlDir, String sqlFileName,
0568: boolean autoNamespaceTables, String baseModelImplPackage,
0569: String basePersistencePackage,
0570: String beanLocatorUtilPackage, String principalBeanPackage,
0571: String propsUtilPackage, String springHibernatePackage,
0572: String springUtilPackage, String testDir) {
0573:
0574: new ServiceBuilder(fileName, hbmFileName, modelHintsFileName,
0575: springFileName, springDataSourceFileName, apiDir,
0576: implDir, jsonFileName, remotingFileName, sqlDir,
0577: sqlFileName, autoNamespaceTables, baseModelImplPackage,
0578: basePersistencePackage, beanLocatorUtilPackage,
0579: principalBeanPackage, propsUtilPackage,
0580: springHibernatePackage, springUtilPackage, testDir,
0581: true);
0582: }
0583:
0584: public ServiceBuilder(String fileName, String hbmFileName,
0585: String modelHintsFileName, String springFileName,
0586: String springDataSourceFileName, String apiDir,
0587: String implDir, String jsonFileName,
0588: String remotingFileName, String sqlDir, String sqlFileName,
0589: boolean autoNamespaceTables, String baseModelImplPackage,
0590: String basePersistencePackage,
0591: String beanLocatorUtilPackage, String principalBeanPackage,
0592: String propsUtilPackage, String springHibernatePackage,
0593: String springUtilPackage, String testDir, boolean build) {
0594:
0595: _tplBaseModeImpl = _getTplProperty("base_mode_impl",
0596: _tplBaseModeImpl);
0597: _tplBasePersistence = _getTplProperty("base_persistence",
0598: _tplBasePersistence);
0599: _tplBeanLocatorUtil = _getTplProperty("bean_locator_util",
0600: _tplBeanLocatorUtil);
0601: _tplDynamicDialect = _getTplProperty("dynamic_dialect",
0602: _tplDynamicDialect);
0603: _tplEjbPk = _getTplProperty("ejb_pk", _tplEjbPk);
0604: _tplException = _getTplProperty("exception", _tplException);
0605: _tplExtendedModel = _getTplProperty("extended_model",
0606: _tplExtendedModel);
0607: _tplExtendedModelImpl = _getTplProperty("extended_model_impl",
0608: _tplExtendedModelImpl);
0609: _tplFinder = _getTplProperty("finder", _tplFinder);
0610: _tplFinderCache = _getTplProperty("finder_cache",
0611: _tplFinderCache);
0612: _tplFinderUtil = _getTplProperty("finder_util", _tplFinderUtil);
0613: _tplHbmXml = _getTplProperty("hbm_xml", _tplHbmXml);
0614: _tplHibernateConfiguration = _getTplProperty(
0615: "hibernate_configuration", _tplHibernateConfiguration);
0616: _tplHibernateUtil = _getTplProperty("hibernate_util",
0617: _tplHibernateUtil);
0618: _tplJsonJs = _getTplProperty("json_js", _tplJsonJs);
0619: _tplJsonJsMethod = _getTplProperty("json_js_method",
0620: _tplJsonJsMethod);
0621: _tplModel = _getTplProperty("model", _tplModel);
0622: _tplModelHintsXml = _getTplProperty("model_hints_xml",
0623: _tplModelHintsXml);
0624: _tplModelImpl = _getTplProperty("model_impl", _tplModelImpl);
0625: _tplModelSoap = _getTplProperty("model_soap", _tplModelSoap);
0626: _tplPersistence = _getTplProperty("persistence",
0627: _tplPersistence);
0628: _tplPersistenceImpl = _getTplProperty("persistence_impl",
0629: _tplPersistenceImpl);
0630: _tplPersistenceUtil = _getTplProperty("persistence_util",
0631: _tplPersistenceUtil);
0632: _tplPrincipalBean = _getTplProperty("principal_bean",
0633: _tplPrincipalBean);
0634: _tplProps = _getTplProperty("props", _tplProps);
0635: _tplPropsUtil = _getTplProperty("props_util", _tplPropsUtil);
0636: _tplRemotingXml = _getTplProperty("remoting_xml",
0637: _tplRemotingXml);
0638: _tplService = _getTplProperty("service", _tplService);
0639: _tplServiceBaseImpl = _getTplProperty("service_base_impl",
0640: _tplServiceBaseImpl);
0641: _tplServiceFactory = _getTplProperty("service_factory",
0642: _tplServiceFactory);
0643: _tplServiceHttp = _getTplProperty("service_http",
0644: _tplServiceHttp);
0645: _tplServiceImpl = _getTplProperty("service_impl",
0646: _tplServiceImpl);
0647: _tplServiceJson = _getTplProperty("service_json",
0648: _tplServiceJson);
0649: _tplServiceJsonSerializer = _getTplProperty(
0650: "service_json_serializer", _tplServiceJsonSerializer);
0651: _tplServiceSoap = _getTplProperty("service_soap",
0652: _tplServiceSoap);
0653: _tplServiceUtil = _getTplProperty("service_util",
0654: _tplServiceUtil);
0655: _tplSpringDataSourceXml = _getTplProperty(
0656: "spring_data_source_xml", _tplSpringDataSourceXml);
0657: _tplSpringUtil = _getTplProperty("spring_util", _tplSpringUtil);
0658: _tplSpringXml = _getTplProperty("spring_xml", _tplSpringXml);
0659:
0660: try {
0661: _badTableNames = ServiceBuilder.getBadTableNames();
0662: _badCmpFields = ServiceBuilder.getBadCmpFields();
0663:
0664: _hbmFileName = hbmFileName;
0665: _modelHintsFileName = modelHintsFileName;
0666: _springFileName = springFileName;
0667: _springDataSourceFileName = springDataSourceFileName;
0668: _apiDir = apiDir;
0669: _implDir = implDir;
0670: _jsonFileName = jsonFileName;
0671: _remotingFileName = remotingFileName;
0672: _sqlDir = sqlDir;
0673: _sqlFileName = sqlFileName;
0674: _autoNamespaceTables = autoNamespaceTables;
0675: _baseModelImplPackage = baseModelImplPackage;
0676: _basePersistencePackage = basePersistencePackage;
0677: _beanLocatorUtilPackage = beanLocatorUtilPackage;
0678: _principalBeanPackage = principalBeanPackage;
0679: _propsUtilPackage = propsUtilPackage;
0680: _springHibernatePackage = springHibernatePackage;
0681: _springUtilPackage = springUtilPackage;
0682: _testDir = testDir;
0683:
0684: Document doc = PortalUtil.readDocumentFromFile(new File(
0685: fileName), true);
0686:
0687: Element root = doc.getRootElement();
0688:
0689: String packagePath = root.attributeValue("package-path");
0690:
0691: _outputPath = _implDir + "/"
0692: + StringUtil.replace(packagePath, ".", "/");
0693:
0694: _serviceOutputPath = _apiDir + "/"
0695: + StringUtil.replace(packagePath, ".", "/");
0696:
0697: if (Validator.isNotNull(_testDir)) {
0698: _testOutputPath = _testDir + "/"
0699: + StringUtil.replace(packagePath, ".", "/");
0700: }
0701:
0702: _packagePath = packagePath;
0703:
0704: Element portlet = root.element("portlet");
0705: Element namespace = root.element("namespace");
0706:
0707: if (portlet != null) {
0708: _portletName = portlet.attributeValue("name");
0709:
0710: _portletShortName = portlet
0711: .attributeValue("short-name");
0712:
0713: _portletPackageName = TextFormatter.format(
0714: _portletName, TextFormatter.B);
0715:
0716: _outputPath += "/" + _portletPackageName;
0717:
0718: _serviceOutputPath += "/" + _portletPackageName;
0719:
0720: _testOutputPath += "/" + _portletPackageName;
0721:
0722: _packagePath += "." + _portletPackageName;
0723: } else {
0724: _portletShortName = namespace.getText();
0725: }
0726:
0727: _portletShortName = _portletShortName.trim();
0728:
0729: if (!Validator.isChar(_portletShortName)) {
0730: throw new RuntimeException(
0731: "The namespace element must be a valid keyword");
0732: }
0733:
0734: _ejbList = new ArrayList();
0735:
0736: List entities = root.elements("entity");
0737:
0738: Iterator itr1 = entities.iterator();
0739:
0740: while (itr1.hasNext()) {
0741: Element entityEl = (Element) itr1.next();
0742:
0743: String ejbName = entityEl.attributeValue("name");
0744:
0745: String table = entityEl.attributeValue("table");
0746:
0747: if (Validator.isNull(table)) {
0748: table = ejbName;
0749:
0750: if (_badTableNames.contains(ejbName)) {
0751: table += "_";
0752: }
0753: }
0754:
0755: if (_autoNamespaceTables) {
0756: table = _portletShortName + "_" + table;
0757: }
0758:
0759: boolean uuid = GetterUtil.getBoolean(entityEl
0760: .attributeValue("uuid"), false);
0761: boolean localService = GetterUtil.getBoolean(entityEl
0762: .attributeValue("local-service"), false);
0763: boolean remoteService = GetterUtil.getBoolean(entityEl
0764: .attributeValue("remote-service"), true);
0765: String persistenceClass = GetterUtil.getString(entityEl
0766: .attributeValue("persistence-class"),
0767: _packagePath + ".service.persistence."
0768: + ejbName + "PersistenceImpl");
0769:
0770: String finderClass = "";
0771:
0772: if (FileUtil.exists(_outputPath
0773: + "/service/persistence/" + ejbName
0774: + "FinderImpl.java")) {
0775:
0776: finderClass = _packagePath
0777: + ".service.persistence." + ejbName
0778: + "FinderImpl";
0779: }
0780:
0781: String dataSource = entityEl
0782: .attributeValue("data-source");
0783: String sessionFactory = entityEl
0784: .attributeValue("session-factory");
0785: String txManager = entityEl
0786: .attributeValue("tx-manager");
0787:
0788: Iterator itr2 = null;
0789:
0790: List pkList = new ArrayList();
0791: List regularColList = new ArrayList();
0792: List collectionList = new ArrayList();
0793: List columnList = new ArrayList();
0794:
0795: List columns = entityEl.elements("column");
0796:
0797: if (uuid) {
0798: Element column = DocumentHelper
0799: .createElement("column");
0800:
0801: column.addAttribute("name", "uuid");
0802: column.addAttribute("type", "String");
0803:
0804: columns.add(0, column);
0805: }
0806:
0807: itr2 = columns.iterator();
0808:
0809: while (itr2.hasNext()) {
0810: Element column = (Element) itr2.next();
0811:
0812: String columnName = column.attributeValue("name");
0813:
0814: String columnDBName = column
0815: .attributeValue("db-name");
0816:
0817: if (Validator.isNull(columnDBName)) {
0818: columnDBName = columnName;
0819:
0820: if (_badCmpFields.contains(columnName)) {
0821: columnDBName += "_";
0822: }
0823: }
0824:
0825: String columnType = column.attributeValue("type");
0826: boolean primary = GetterUtil.getBoolean(column
0827: .attributeValue("primary"), false);
0828: String collectionEntity = column
0829: .attributeValue("entity");
0830: String mappingKey = column
0831: .attributeValue("mapping-key");
0832: String mappingTable = column
0833: .attributeValue("mapping-table");
0834: String idType = column.attributeValue("id-type");
0835: String idParam = column.attributeValue("id-param");
0836: boolean convertNull = GetterUtil.getBoolean(column
0837: .attributeValue("convert-null"), true);
0838:
0839: EntityColumn col = new EntityColumn(columnName,
0840: columnDBName, columnType, primary,
0841: collectionEntity, mappingKey, mappingTable,
0842: idType, idParam, convertNull);
0843:
0844: if (primary) {
0845: pkList.add(col);
0846: }
0847:
0848: if (columnType.equals("Collection")) {
0849: collectionList.add(col);
0850: } else {
0851: regularColList.add(col);
0852: }
0853:
0854: columnList.add(col);
0855: }
0856:
0857: EntityOrder order = null;
0858:
0859: Element orderEl = entityEl.element("order");
0860:
0861: if (orderEl != null) {
0862: boolean asc = true;
0863:
0864: if ((orderEl.attribute("by") != null)
0865: && (orderEl.attributeValue("by")
0866: .equals("desc"))) {
0867:
0868: asc = false;
0869: }
0870:
0871: List orderColsList = new ArrayList();
0872:
0873: order = new EntityOrder(asc, orderColsList);
0874:
0875: List orderCols = orderEl.elements("order-column");
0876:
0877: Iterator itr3 = orderCols.iterator();
0878:
0879: while (itr3.hasNext()) {
0880: Element orderColEl = (Element) itr3.next();
0881:
0882: String orderColName = orderColEl
0883: .attributeValue("name");
0884: boolean orderColCaseSensitive = GetterUtil
0885: .getBoolean(
0886: orderColEl
0887: .attributeValue("case-sensitive"),
0888: true);
0889:
0890: boolean orderColByAscending = asc;
0891:
0892: String orderColBy = GetterUtil
0893: .getString(orderColEl
0894: .attributeValue("order-by"));
0895:
0896: if (orderColBy.equals("asc")) {
0897: orderColByAscending = true;
0898: } else if (orderColBy.equals("desc")) {
0899: orderColByAscending = false;
0900: }
0901:
0902: EntityColumn col = Entity.getColumn(
0903: orderColName, columnList);
0904:
0905: col = (EntityColumn) col.clone();
0906:
0907: col.setCaseSensitive(orderColCaseSensitive);
0908: col.setOrderByAscending(orderColByAscending);
0909:
0910: orderColsList.add(col);
0911: }
0912: }
0913:
0914: List finderList = new ArrayList();
0915:
0916: List finders = entityEl.elements("finder");
0917:
0918: if (uuid) {
0919: Element finderEl = DocumentHelper
0920: .createElement("finder");
0921:
0922: finderEl.addAttribute("name", "Uuid");
0923: finderEl.addAttribute("return-type", "Collection");
0924:
0925: Element finderColEl = finderEl
0926: .addElement("finder-column");
0927:
0928: finderColEl.addAttribute("name", "uuid");
0929:
0930: finders.add(0, finderEl);
0931:
0932: if (columnList
0933: .contains(new EntityColumn("groupId"))) {
0934: finderEl = DocumentHelper
0935: .createElement("finder");
0936:
0937: finderEl.addAttribute("name", "UUID_G");
0938: finderEl.addAttribute("return-type", ejbName);
0939:
0940: finderColEl = finderEl
0941: .addElement("finder-column");
0942:
0943: finderColEl.addAttribute("name", "uuid");
0944:
0945: finderColEl = finderEl
0946: .addElement("finder-column");
0947:
0948: finderColEl.addAttribute("name", "groupId");
0949:
0950: finders.add(1, finderEl);
0951: }
0952: }
0953:
0954: itr2 = finders.iterator();
0955:
0956: while (itr2.hasNext()) {
0957: Element finderEl = (Element) itr2.next();
0958:
0959: String finderName = finderEl.attributeValue("name");
0960: String finderReturn = finderEl
0961: .attributeValue("return-type");
0962: String finderWhere = finderEl
0963: .attributeValue("where");
0964: boolean finderDBIndex = GetterUtil.getBoolean(
0965: finderEl.attributeValue("db-index"), true);
0966:
0967: List finderColsList = new ArrayList();
0968:
0969: List finderCols = finderEl
0970: .elements("finder-column");
0971:
0972: Iterator itr3 = finderCols.iterator();
0973:
0974: while (itr3.hasNext()) {
0975: Element finderColEl = (Element) itr3.next();
0976:
0977: String finderColName = finderColEl
0978: .attributeValue("name");
0979:
0980: String finderColDBName = finderColEl
0981: .attributeValue("db-name");
0982:
0983: if (Validator.isNull(finderColDBName)) {
0984: finderColDBName = finderColName;
0985:
0986: if (_badCmpFields.contains(finderColName)) {
0987: finderColDBName += "_";
0988: }
0989: }
0990:
0991: boolean finderColCaseSensitive = GetterUtil
0992: .getBoolean(
0993: finderColEl
0994: .attributeValue("case-sensitive"),
0995: true);
0996:
0997: String finderColComparator = GetterUtil
0998: .getString(finderColEl
0999: .attributeValue("comparator"),
1000: "=");
1001:
1002: EntityColumn col = Entity.getColumn(
1003: finderColName, columnList);
1004:
1005: col = (EntityColumn) col.clone();
1006:
1007: col.setDBName(finderColDBName);
1008: col.setCaseSensitive(finderColCaseSensitive);
1009: col.setComparator(finderColComparator);
1010:
1011: finderColsList.add(col);
1012: }
1013:
1014: finderList.add(new EntityFinder(finderName,
1015: finderReturn, finderColsList, finderWhere,
1016: finderDBIndex));
1017: }
1018:
1019: List referenceList = new ArrayList();
1020:
1021: if (build) {
1022: List references = entityEl.elements("reference");
1023:
1024: itr2 = references.iterator();
1025:
1026: while (itr2.hasNext()) {
1027: Element reference = (Element) itr2.next();
1028:
1029: String refPackage = reference
1030: .attributeValue("package-path");
1031: String refEntity = reference
1032: .attributeValue("entity");
1033:
1034: referenceList.add(getEntity(refPackage + "."
1035: + refEntity));
1036: }
1037: }
1038:
1039: List txRequiredList = new ArrayList();
1040:
1041: itr2 = entityEl.elements("tx-required").iterator();
1042:
1043: while (itr2.hasNext()) {
1044: Element txRequiredEl = (Element) itr2.next();
1045:
1046: String txRequired = txRequiredEl.getText();
1047:
1048: txRequiredList.add(txRequired);
1049: }
1050:
1051: _ejbList.add(new Entity(_packagePath, _portletName,
1052: _portletShortName, ejbName, table, uuid,
1053: localService, remoteService, persistenceClass,
1054: finderClass, dataSource, sessionFactory,
1055: txManager, pkList, regularColList,
1056: collectionList, columnList, order, finderList,
1057: referenceList, txRequiredList));
1058: }
1059:
1060: List exceptionList = new ArrayList();
1061:
1062: if (root.element("exceptions") != null) {
1063: List exceptions = root.element("exceptions").elements(
1064: "exception");
1065:
1066: itr1 = exceptions.iterator();
1067:
1068: while (itr1.hasNext()) {
1069: Element exception = (Element) itr1.next();
1070:
1071: exceptionList.add(exception.getText());
1072: }
1073: }
1074:
1075: if (build) {
1076: for (int x = 0; x < _ejbList.size(); x++) {
1077: Entity entity = (Entity) _ejbList.get(x);
1078:
1079: System.out.println("Building " + entity.getName());
1080:
1081: if (true || entity.getName().equals("EmailAddress")
1082: || entity.getName().equals("User")) {
1083:
1084: if (entity.hasColumns()) {
1085: _createHBM(entity);
1086: _createHBMUtil(entity);
1087:
1088: _createPersistenceImpl(entity);
1089: _createPersistence(entity);
1090: _createPersistenceUtil(entity);
1091:
1092: if (Validator.isNotNull(_testDir)) {
1093: _createPersistenceTest(entity);
1094: }
1095:
1096: _createModelImpl(entity);
1097: _createExtendedModelImpl(entity);
1098:
1099: _createModel(entity);
1100: _createExtendedModel(entity);
1101:
1102: _createModelSoap(entity);
1103:
1104: _createPool(entity);
1105:
1106: if (entity.getPKList().size() > 1) {
1107: _createEJBPK(entity);
1108: }
1109: }
1110:
1111: _createFinder(entity);
1112: _createFinderUtil(entity);
1113:
1114: if (entity.hasLocalService()) {
1115: _createServiceBaseImpl(entity, _LOCAL);
1116: _createServiceImpl(entity, _LOCAL);
1117: _createService(entity, _LOCAL);
1118: _createServiceFactory(entity, _LOCAL);
1119: _createServiceUtil(entity, _LOCAL);
1120: }
1121:
1122: if (entity.hasRemoteService()) {
1123: _createServiceBaseImpl(entity, _REMOTE);
1124: _createServiceImpl(entity, _REMOTE);
1125: _createService(entity, _REMOTE);
1126: _createServiceFactory(entity, _REMOTE);
1127: _createServiceUtil(entity, _REMOTE);
1128:
1129: if (Validator.isNotNull(_jsonFileName)) {
1130: _createServiceHttp(entity);
1131: _createServiceJSON(entity);
1132:
1133: if (entity.hasColumns()) {
1134: _createServiceJSONSerializer(entity);
1135: }
1136:
1137: _createServiceSoap(entity);
1138: }
1139: }
1140: }
1141: }
1142:
1143: _createHBMXML();
1144: _createModelHintsXML();
1145: _createSpringXML();
1146:
1147: if (Validator.isNotNull(_jsonFileName)) {
1148: _createJSONJS();
1149: }
1150:
1151: if (Validator.isNotNull(_remotingFileName)) {
1152: _createRemotingXML();
1153: }
1154:
1155: _createSQLIndexes();
1156: _createSQLTables();
1157: _createSQLSequences();
1158:
1159: _createExceptions(exceptionList);
1160:
1161: _createBaseModelImpl();
1162: _createBasePersistence();
1163: _createBeanLocatorUtil();
1164: _createDynamicDialect();
1165: _createFinderCache();
1166: _createHibernateConfiguration();
1167: _createHibernateUtil();
1168: _createPrincipalBean();
1169: _createProps();
1170: _createPropsUtil();
1171: _createSpringDataSourceXML();
1172: _createSpringUtil();
1173: }
1174: } catch (Exception e) {
1175: e.printStackTrace();
1176: }
1177: }
1178:
1179: public String getClassName(Type type) {
1180: int dimensions = type.getDimensions();
1181: String name = type.getValue();
1182:
1183: if (dimensions > 0) {
1184: StringMaker sm = new StringMaker();
1185:
1186: for (int i = 0; i < dimensions; i++) {
1187: sm.append("[");
1188: }
1189:
1190: if (name.equals("boolean")) {
1191: return sm.toString() + "Z";
1192: } else if (name.equals("byte")) {
1193: return sm.toString() + "B";
1194: } else if (name.equals("char")) {
1195: return sm.toString() + "C";
1196: } else if (name.equals("double")) {
1197: return sm.toString() + "D";
1198: } else if (name.equals("float")) {
1199: return sm.toString() + "F";
1200: } else if (name.equals("int")) {
1201: return sm.toString() + "I";
1202: } else if (name.equals("long")) {
1203: return sm.toString() + "J";
1204: } else if (name.equals("short")) {
1205: return sm.toString() + "S";
1206: } else {
1207: return sm.toString() + "L" + name + ";";
1208: }
1209: }
1210:
1211: return name;
1212: }
1213:
1214: public String getCreateTableSQL(Entity entity) {
1215: String createTableSQL = _getCreateTableSQL(entity);
1216:
1217: createTableSQL = StringUtil.replace(createTableSQL, "\n", "");
1218: createTableSQL = StringUtil.replace(createTableSQL, "\t", "");
1219: createTableSQL = createTableSQL.substring(0, createTableSQL
1220: .length() - 1);
1221:
1222: return createTableSQL;
1223: }
1224:
1225: public String getDimensions(String dims) {
1226: return getDimensions(Integer.parseInt(dims));
1227: }
1228:
1229: public String getDimensions(int dims) {
1230: String dimensions = "";
1231:
1232: for (int i = 0; i < dims; i++) {
1233: dimensions += "[]";
1234: }
1235:
1236: return dimensions;
1237: }
1238:
1239: public Entity getEntity(String name) throws IOException {
1240: int pos = name.lastIndexOf(".");
1241:
1242: if (pos == -1) {
1243: pos = _ejbList.indexOf(new Entity(name));
1244:
1245: return (Entity) _ejbList.get(pos);
1246: } else {
1247: String refPackage = name.substring(0, pos);
1248: String refPackageDir = StringUtil.replace(refPackage, ".",
1249: "/");
1250: String refEntity = name.substring(pos + 1, name.length());
1251: String refFileName = _implDir + "/" + refPackageDir
1252: + "/service.xml";
1253:
1254: File refFile = new File(refFileName);
1255:
1256: boolean useTempFile = false;
1257:
1258: if (!refFile.exists()) {
1259: refFileName = Time.getTimestamp();
1260: refFile = new File(refFileName);
1261:
1262: ClassLoader classLoader = getClass().getClassLoader();
1263:
1264: FileUtil.write(refFileName, StringUtil.read(
1265: classLoader, refPackageDir + "/service.xml"));
1266:
1267: useTempFile = true;
1268: }
1269:
1270: ServiceBuilder serviceBuilder = new ServiceBuilder(
1271: refFileName, _hbmFileName, _modelHintsFileName,
1272: _springFileName, _springDataSourceFileName,
1273: _apiDir, _implDir, _jsonFileName,
1274: _remotingFileName, _sqlDir, _sqlFileName,
1275: _autoNamespaceTables, _baseModelImplPackage,
1276: _basePersistencePackage, _beanLocatorUtilPackage,
1277: _principalBeanPackage, _propsUtilPackage,
1278: _springHibernatePackage, _springUtilPackage,
1279: _testDir, false);
1280:
1281: Entity entity = serviceBuilder.getEntity(refEntity);
1282:
1283: if (useTempFile) {
1284: refFile.deleteOnExit();
1285: }
1286:
1287: return entity;
1288: }
1289: }
1290:
1291: public String getGeneratorClass(String idType) {
1292: if (Validator.isNull(idType)) {
1293: idType = "assigned";
1294: }
1295:
1296: return idType;
1297: }
1298:
1299: public String getNoSuchEntityException(Entity entity) {
1300: String noSuchEntityException = entity.getName();
1301:
1302: if (Validator.isNull(entity.getPortletShortName())
1303: || noSuchEntityException.startsWith(entity
1304: .getPortletShortName())) {
1305:
1306: noSuchEntityException = noSuchEntityException
1307: .substring(entity.getPortletShortName().length());
1308: }
1309:
1310: noSuchEntityException = "NoSuch" + noSuchEntityException;
1311:
1312: return noSuchEntityException;
1313: }
1314:
1315: public String getPrimitiveObj(String type) {
1316: if (type.equals("boolean")) {
1317: return "Boolean";
1318: } else if (type.equals("double")) {
1319: return "Double";
1320: } else if (type.equals("float")) {
1321: return "Float";
1322: } else if (type.equals("int")) {
1323: return "Integer";
1324: } else if (type.equals("long")) {
1325: return "Long";
1326: } else if (type.equals("short")) {
1327: return "Short";
1328: } else {
1329: return type;
1330: }
1331: }
1332:
1333: public String getPrimitiveObjValue(String colType) {
1334: if (colType.equals("Boolean")) {
1335: return ".booleanValue()";
1336: } else if (colType.equals("Double")) {
1337: return ".doubleValue()";
1338: } else if (colType.equals("Float")) {
1339: return ".floatValue()";
1340: } else if (colType.equals("Integer")) {
1341: return ".intValue()";
1342: } else if (colType.equals("Long")) {
1343: return ".longValue()";
1344: } else if (colType.equals("Short")) {
1345: return ".shortValue()";
1346: }
1347:
1348: return StringPool.BLANK;
1349: }
1350:
1351: public String getSqlType(String model, String field, String type) {
1352: if (type.equals("boolean") || type.equals("Boolean")) {
1353: return "BOOLEAN";
1354: } else if (type.equals("double") || type.equals("Double")) {
1355: return "DOUBLE";
1356: } else if (type.equals("float") || type.equals("Float")) {
1357: return "FLOAT";
1358: } else if (type.equals("int") || type.equals("Integer")) {
1359: return "INTEGER";
1360: } else if (type.equals("long") || type.equals("Long")) {
1361: return "BIGINT";
1362: } else if (type.equals("short") || type.equals("Short")) {
1363: return "INTEGER";
1364: } else if (type.equals("Date")) {
1365: return "TIMESTAMP";
1366: } else if (type.equals("String")) {
1367: Map hints = ModelHintsUtil.getHints(model, field);
1368:
1369: if (hints != null) {
1370: int maxLength = GetterUtil.getInteger((String) hints
1371: .get("max-length"));
1372:
1373: if (maxLength == 2000000) {
1374: return "CLOB";
1375: }
1376: }
1377:
1378: return "VARCHAR";
1379: } else {
1380: return null;
1381: }
1382: }
1383:
1384: public boolean isCustomMethod(JavaMethod method) {
1385: String methodName = method.getName();
1386:
1387: if (methodName.equals("hasAdministrator")
1388: || methodName.equals("ejbCreate")
1389: || methodName.equals("ejbRemove")
1390: || methodName.equals("ejbActivate")
1391: || methodName.equals("ejbPassivate")
1392: || methodName.equals("getSessionContext")
1393: || methodName.equals("setSessionContext")
1394: || methodName.equals("hashCode")
1395: || methodName.equals("getClass")
1396: || methodName.equals("wait")
1397: || methodName.equals("equals")
1398: || methodName.equals("toString")
1399: || methodName.equals("notify")
1400: || methodName.equals("notifyAll")) {
1401:
1402: return false;
1403: } else if (methodName.equals("getPermissionChecker")) {
1404: return false;
1405: } else if (methodName.equals("getUser")
1406: && method.getParameters().length == 0) {
1407:
1408: return false;
1409: } else if (methodName.equals("getUserId")
1410: && method.getParameters().length == 0) {
1411:
1412: return false;
1413: } else if ((methodName.endsWith("Service"))
1414: && (methodName.startsWith("get") || methodName
1415: .startsWith("set"))) {
1416:
1417: return false;
1418: } else {
1419: return true;
1420: }
1421: }
1422:
1423: public boolean isSoapMethod(JavaMethod method) {
1424: String returnValueName = method.getReturns().getValue();
1425:
1426: if (returnValueName.startsWith("java.io")
1427: || returnValueName.equals("java.util.Map")
1428: || returnValueName.equals("java.util.Properties")
1429: || returnValueName.startsWith("javax")) {
1430:
1431: return false;
1432: }
1433:
1434: JavaParameter[] parameters = method.getParameters();
1435:
1436: for (int i = 0; i < parameters.length; i++) {
1437: JavaParameter javaParameter = parameters[i];
1438:
1439: String parameterTypeName = javaParameter.getType()
1440: .getValue()
1441: + _getDimensions(javaParameter.getType());
1442:
1443: if ((parameterTypeName.indexOf("com.liferay.portal.model.") != -1)
1444: || (parameterTypeName
1445: .equals("com.liferay.portal.theme.ThemeDisplay"))
1446: || (parameterTypeName
1447: .equals("com.liferay.portlet.PortletPreferencesImpl"))
1448: || parameterTypeName.startsWith("java.io")
1449: ||
1450: //parameterTypeName.startsWith("java.util.List") ||
1451: //parameterTypeName.startsWith("java.util.Locale") ||
1452: parameterTypeName.startsWith("java.util.Map")
1453: || parameterTypeName
1454: .startsWith("java.util.Properties")
1455: || parameterTypeName.startsWith("javax")) {
1456:
1457: return false;
1458: }
1459: }
1460:
1461: return true;
1462: }
1463:
1464: private static String _getPackagePath(File file) throws IOException {
1465: String fileName = StringUtil
1466: .replace(file.toString(), "\\", "/");
1467:
1468: int x = fileName.indexOf("src/");
1469:
1470: if (x == -1) {
1471: x = fileName.indexOf("test/");
1472: }
1473:
1474: int y = fileName.lastIndexOf("/");
1475:
1476: fileName = fileName.substring(x + 4, y);
1477:
1478: return StringUtil.replace(fileName, "/", ".");
1479: }
1480:
1481: private void _createBaseModelImpl() throws Exception {
1482: if (_baseModelImplPackage
1483: .equals("com.liferay.portal.model.impl")) {
1484: return;
1485: }
1486:
1487: // Content
1488:
1489: String content = _processTemplate(_tplBaseModeImpl);
1490:
1491: // Write file
1492:
1493: File ejbFile = new File(_implDir + "/"
1494: + StringUtil.replace(_baseModelImplPackage, ".", "/")
1495: + "/BaseModelImpl.java");
1496:
1497: FileUtil.write(ejbFile, content, true);
1498: }
1499:
1500: private void _createBasePersistence() throws Exception {
1501: if (_basePersistencePackage
1502: .equals("com.liferay.portal.service.persistence")) {
1503:
1504: return;
1505: }
1506:
1507: // Content
1508:
1509: String content = _processTemplate(_tplBasePersistence);
1510:
1511: // Write file
1512:
1513: File ejbFile = new File(_implDir + "/"
1514: + StringUtil.replace(_basePersistencePackage, ".", "/")
1515: + "/BasePersistence.java");
1516:
1517: FileUtil.write(ejbFile, content, true);
1518: }
1519:
1520: private void _createBeanLocatorUtil() throws Exception {
1521: if (_beanLocatorUtilPackage
1522: .equals("com.liferay.portal.kernel.bean")) {
1523: return;
1524: }
1525:
1526: // Content
1527:
1528: String content = _processTemplate(_tplBeanLocatorUtil);
1529:
1530: // Write file
1531:
1532: File ejbFile = new File(_implDir + "/"
1533: + StringUtil.replace(_beanLocatorUtilPackage, ".", "/")
1534: + "/BeanLocatorUtil.java");
1535:
1536: writeFile(ejbFile, content);
1537: }
1538:
1539: private void _createDynamicDialect() throws Exception {
1540: if (_springHibernatePackage
1541: .equals("com.liferay.portal.spring.hibernate")) {
1542:
1543: return;
1544: }
1545:
1546: // Content
1547:
1548: String content = _processTemplate(_tplDynamicDialect);
1549:
1550: // Write file
1551:
1552: File ejbFile = new File(_implDir + "/"
1553: + StringUtil.replace(_springHibernatePackage, ".", "/")
1554: + "/DynamicDialect.java");
1555:
1556: FileUtil.write(ejbFile, content, true);
1557: }
1558:
1559: private void _createEJBPK(Entity entity) throws Exception {
1560: Map context = _getContext();
1561:
1562: context.put("entity", entity);
1563:
1564: // Content
1565:
1566: String content = _processTemplate(_tplEjbPk, context);
1567:
1568: // Write file
1569:
1570: File ejbFile = new File(_serviceOutputPath
1571: + "/service/persistence/" + entity.getPKClassName()
1572: + ".java");
1573:
1574: writeFile(ejbFile, content);
1575: }
1576:
1577: private void _createExceptions(List exceptions) throws Exception {
1578: for (int i = 0; i < _ejbList.size(); i++) {
1579: Entity entity = (Entity) _ejbList.get(i);
1580:
1581: if (entity.hasColumns()) {
1582: exceptions.add(getNoSuchEntityException(entity));
1583: }
1584: }
1585:
1586: for (int i = 0; i < exceptions.size(); i++) {
1587: String exception = (String) exceptions.get(i);
1588:
1589: File exceptionFile = new File(_serviceOutputPath + "/"
1590: + exception + "Exception.java");
1591:
1592: if (!exceptionFile.exists()) {
1593: Map context = _getContext();
1594:
1595: context.put("exception", exception);
1596:
1597: String content = _processTemplate(_tplException,
1598: context);
1599:
1600: FileUtil.write(exceptionFile, content);
1601: }
1602: }
1603: }
1604:
1605: private void _createExtendedModel(Entity entity) throws Exception {
1606: JavaClass javaClass = _getJavaClass(_outputPath
1607: + "/model/impl/" + entity.getName() + "Impl.java");
1608:
1609: Map context = _getContext();
1610:
1611: context.put("entity", entity);
1612: context.put("methods", javaClass.getMethods());
1613:
1614: // Content
1615:
1616: String content = _processTemplate(_tplExtendedModel, context);
1617:
1618: // Write file
1619:
1620: File modelFile = new File(_serviceOutputPath + "/model/"
1621: + entity.getName() + ".java");
1622:
1623: Map jalopySettings = new HashMap();
1624:
1625: jalopySettings.put("keepJavadoc", Boolean.TRUE);
1626:
1627: writeFile(modelFile, content, jalopySettings);
1628: }
1629:
1630: private void _createExtendedModelImpl(Entity entity)
1631: throws Exception {
1632: Map context = _getContext();
1633:
1634: context.put("entity", entity);
1635:
1636: // Content
1637:
1638: String content = _processTemplate(_tplExtendedModelImpl,
1639: context);
1640:
1641: // Write file
1642:
1643: File modelFile = new File(_outputPath + "/model/impl/"
1644: + entity.getName() + "Impl.java");
1645:
1646: if (!modelFile.exists()) {
1647: writeFile(modelFile, content);
1648: }
1649: }
1650:
1651: private void _createFinder(Entity entity) throws Exception {
1652: if (!entity.hasFinderClass()) {
1653: return;
1654: }
1655:
1656: JavaClass javaClass = _getJavaClass(_outputPath
1657: + "/service/persistence/" + entity.getName()
1658: + "FinderImpl.java");
1659:
1660: Map context = _getContext();
1661:
1662: context.put("entity", entity);
1663: context.put("methods", javaClass.getMethods());
1664:
1665: // Content
1666:
1667: String content = _processTemplate(_tplFinder, context);
1668:
1669: // Write file
1670:
1671: File ejbFile = new File(_serviceOutputPath
1672: + "/service/persistence/" + entity.getName()
1673: + "Finder.java");
1674:
1675: writeFile(ejbFile, content);
1676: }
1677:
1678: private void _createFinderCache() throws Exception {
1679: if (_springHibernatePackage
1680: .equals("com.liferay.portal.spring.hibernate")) {
1681: return;
1682: }
1683:
1684: // Content
1685:
1686: String content = _processTemplate(_tplFinderCache);
1687:
1688: // Write file
1689:
1690: File ejbFile = new File(_implDir + "/"
1691: + StringUtil.replace(_springHibernatePackage, ".", "/")
1692: + "/FinderCache.java");
1693:
1694: FileUtil.write(ejbFile, content, true);
1695: }
1696:
1697: private void _createFinderUtil(Entity entity) throws Exception {
1698: if (!entity.hasFinderClass()) {
1699: return;
1700: }
1701:
1702: JavaClass javaClass = _getJavaClass(_outputPath
1703: + "/service/persistence/" + entity.getName()
1704: + "FinderImpl.java");
1705:
1706: Map context = _getContext();
1707:
1708: context.put("entity", entity);
1709: context.put("methods", javaClass.getMethods());
1710:
1711: // Content
1712:
1713: String content = _processTemplate(_tplFinderUtil, context);
1714:
1715: // Write file
1716:
1717: File ejbFile = new File(_serviceOutputPath
1718: + "/service/persistence/" + entity.getName()
1719: + "FinderUtil.java");
1720:
1721: writeFile(ejbFile, content);
1722: }
1723:
1724: private void _createHBM(Entity entity) throws IOException {
1725: File ejbFile = new File(_outputPath + "/service/persistence/"
1726: + entity.getName() + "HBM.java");
1727:
1728: if (ejbFile.exists()) {
1729: System.out.println("Removing deprecated " + ejbFile);
1730:
1731: ejbFile.delete();
1732: }
1733: }
1734:
1735: private void _createHBMUtil(Entity entity) throws IOException {
1736: File ejbFile = new File(_outputPath + "/service/persistence/"
1737: + entity.getName() + "HBMUtil.java");
1738:
1739: if (ejbFile.exists()) {
1740: System.out.println("Removing deprecated " + ejbFile);
1741:
1742: ejbFile.delete();
1743: }
1744: }
1745:
1746: private void _createHBMXML() throws Exception {
1747: Map context = _getContext();
1748:
1749: context.put("entities", _ejbList);
1750:
1751: // Content
1752:
1753: String content = _processTemplate(_tplHbmXml, context);
1754:
1755: File xmlFile = new File(_hbmFileName);
1756:
1757: if (!xmlFile.exists()) {
1758: String xml = "<?xml version=\"1.0\"?>\n"
1759: + "<!DOCTYPE hibernate-mapping PUBLIC \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n"
1760: + "\n"
1761: + "<hibernate-mapping default-lazy=\"false\" auto-import=\"false\">\n"
1762: + "</hibernate-mapping>";
1763:
1764: FileUtil.write(xmlFile, xml);
1765: }
1766:
1767: String oldContent = FileUtil.read(xmlFile);
1768: String newContent = _fixHBMXML(oldContent);
1769:
1770: int firstClass = newContent.indexOf("<class name=\""
1771: + _packagePath + ".model.impl.");
1772: int lastClass = newContent.lastIndexOf("<class name=\""
1773: + _packagePath + ".model.impl.");
1774:
1775: if (firstClass == -1) {
1776: int x = newContent.indexOf("</hibernate-mapping>");
1777:
1778: newContent = newContent.substring(0, x) + content
1779: + newContent.substring(x, newContent.length());
1780: } else {
1781: firstClass = newContent.lastIndexOf("<class", firstClass) - 1;
1782: lastClass = newContent.indexOf("</class>", lastClass) + 9;
1783:
1784: newContent = newContent.substring(0, firstClass)
1785: + content
1786: + newContent.substring(lastClass, newContent
1787: .length());
1788: }
1789:
1790: newContent = _formatXML(newContent);
1791:
1792: if (!oldContent.equals(newContent)) {
1793: FileUtil.write(xmlFile, newContent);
1794: }
1795: }
1796:
1797: private void _createHibernateConfiguration() throws Exception {
1798: if (_springHibernatePackage
1799: .equals("com.liferay.portal.spring.hibernate")) {
1800:
1801: return;
1802: }
1803:
1804: // Content
1805:
1806: String content = _processTemplate(_tplHibernateConfiguration);
1807:
1808: // Write file
1809:
1810: File ejbFile = new File(_implDir + "/"
1811: + StringUtil.replace(_springHibernatePackage, ".", "/")
1812: + "/HibernateConfiguration.java");
1813:
1814: FileUtil.write(ejbFile, content, true);
1815: }
1816:
1817: private void _createHibernateUtil() throws Exception {
1818: if (_springHibernatePackage
1819: .equals("com.liferay.portal.spring.hibernate")) {
1820:
1821: return;
1822: }
1823:
1824: // Content
1825:
1826: String content = _processTemplate(_tplHibernateUtil);
1827:
1828: // Write file
1829:
1830: File ejbFile = new File(_implDir + "/"
1831: + StringUtil.replace(_springHibernatePackage, ".", "/")
1832: + "/HibernateUtil.java");
1833:
1834: FileUtil.write(ejbFile, content, true);
1835: }
1836:
1837: private void _createJSONJS() throws Exception {
1838: StringMaker content = new StringMaker();
1839:
1840: if (_ejbList.size() > 0) {
1841: content.append(_processTemplate(_tplJsonJs));
1842: }
1843:
1844: for (int i = 0; i < _ejbList.size(); i++) {
1845: Entity entity = (Entity) _ejbList.get(i);
1846:
1847: if (entity.hasRemoteService()) {
1848: JavaClass javaClass = _getJavaClass(_outputPath
1849: + "/service/http/" + entity.getName()
1850: + "ServiceJSON.java");
1851:
1852: JavaMethod[] methods = javaClass.getMethods();
1853:
1854: Set jsonMethods = new LinkedHashSet();
1855:
1856: for (int j = 0; j < methods.length; j++) {
1857: JavaMethod javaMethod = methods[j];
1858:
1859: String methodName = javaMethod.getName();
1860:
1861: if (javaMethod.isPublic()) {
1862: jsonMethods.add(methodName);
1863: }
1864: }
1865:
1866: if (jsonMethods.size() > 0) {
1867: Map context = _getContext();
1868:
1869: context.put("entity", entity);
1870: context.put("methods", jsonMethods);
1871:
1872: content.append("\n\n");
1873: content.append(_processTemplate(_tplJsonJsMethod,
1874: context));
1875: }
1876: }
1877: }
1878:
1879: File jsonFile = new File(_jsonFileName);
1880:
1881: if (!jsonFile.exists()) {
1882: FileUtil.write(jsonFile, "");
1883: }
1884:
1885: String oldContent = FileUtil.read(jsonFile);
1886: String newContent = new String(oldContent);
1887:
1888: int oldBegin = oldContent.indexOf("Liferay.Service."
1889: + _portletShortName);
1890:
1891: int oldEnd = oldContent.lastIndexOf("Liferay.Service."
1892: + _portletShortName);
1893:
1894: oldEnd = oldContent.indexOf("};", oldEnd);
1895:
1896: int newBegin = newContent.indexOf("Liferay.Service."
1897: + _portletShortName);
1898:
1899: int newEnd = newContent.lastIndexOf("Liferay.Service."
1900: + _portletShortName);
1901:
1902: newEnd = newContent.indexOf("};", newEnd);
1903:
1904: if (newBegin == -1) {
1905: newContent = oldContent + "\n\n"
1906: + content.toString().trim();
1907: } else {
1908: newContent = newContent.substring(0, oldBegin)
1909: + content.toString().trim()
1910: + newContent.substring(oldEnd + 2, newContent
1911: .length());
1912: }
1913:
1914: if (!oldContent.equals(newContent)) {
1915: FileUtil.write(jsonFile, newContent);
1916: }
1917: }
1918:
1919: private void _createModel(Entity entity) throws Exception {
1920: Map context = _getContext();
1921:
1922: context.put("entity", entity);
1923:
1924: // Content
1925:
1926: String content = _processTemplate(_tplModel, context);
1927:
1928: // Write file
1929:
1930: File modelFile = new File(_serviceOutputPath + "/model/"
1931: + entity.getName() + "Model.java");
1932:
1933: Map jalopySettings = new HashMap();
1934:
1935: jalopySettings.put("keepJavadoc", Boolean.TRUE);
1936:
1937: writeFile(modelFile, content, jalopySettings);
1938: }
1939:
1940: private void _createModelHintsXML() throws Exception {
1941: Map context = _getContext();
1942:
1943: context.put("entities", _ejbList);
1944:
1945: // Content
1946:
1947: String content = _processTemplate(_tplModelHintsXml, context);
1948:
1949: File xmlFile = new File(_modelHintsFileName);
1950:
1951: if (!xmlFile.exists()) {
1952: String xml = "<?xml version=\"1.0\"?>\n" + "\n"
1953: + "<model-hints>\n" + "</model-hints>";
1954:
1955: FileUtil.write(xmlFile, xml);
1956: }
1957:
1958: String oldContent = FileUtil.read(xmlFile);
1959: String newContent = new String(oldContent);
1960:
1961: int firstModel = newContent.indexOf("<model name=\""
1962: + _packagePath + ".model.");
1963: int lastModel = newContent.lastIndexOf("<model name=\""
1964: + _packagePath + ".model.");
1965:
1966: if (firstModel == -1) {
1967: int x = newContent.indexOf("</model-hints>");
1968:
1969: newContent = newContent.substring(0, x) + content
1970: + newContent.substring(x, newContent.length());
1971: } else {
1972: firstModel = newContent.lastIndexOf("<model", firstModel) - 1;
1973: lastModel = newContent.indexOf("</model>", lastModel) + 9;
1974:
1975: newContent = newContent.substring(0, firstModel)
1976: + content
1977: + newContent.substring(lastModel, newContent
1978: .length());
1979: }
1980:
1981: newContent = _formatXML(newContent);
1982:
1983: if (!oldContent.equals(newContent)) {
1984: FileUtil.write(xmlFile, newContent);
1985: }
1986: }
1987:
1988: private void _createModelImpl(Entity entity) throws Exception {
1989: Map context = _getContext();
1990:
1991: context.put("entity", entity);
1992:
1993: // Content
1994:
1995: String content = _processTemplate(_tplModelImpl, context);
1996:
1997: // Write file
1998:
1999: File modelFile = new File(_outputPath + "/model/impl/"
2000: + entity.getName() + "ModelImpl.java");
2001:
2002: Map jalopySettings = new HashMap();
2003:
2004: jalopySettings.put("keepJavadoc", Boolean.TRUE);
2005:
2006: writeFile(modelFile, content, jalopySettings);
2007: }
2008:
2009: private void _createModelSoap(Entity entity) throws Exception {
2010: Map context = _getContext();
2011:
2012: context.put("entity", entity);
2013:
2014: // Content
2015:
2016: String content = _processTemplate(_tplModelSoap, context);
2017:
2018: // Write file
2019:
2020: File modelFile = new File(_serviceOutputPath + "/model/"
2021: + entity.getName() + "Soap.java");
2022:
2023: Map jalopySettings = new HashMap();
2024:
2025: jalopySettings.put("keepJavadoc", Boolean.TRUE);
2026:
2027: writeFile(modelFile, content, jalopySettings);
2028: }
2029:
2030: private void _createPersistence(Entity entity) throws Exception {
2031: JavaClass javaClass = _getJavaClass(_outputPath
2032: + "/service/persistence/" + entity.getName()
2033: + "PersistenceImpl.java");
2034:
2035: Map context = _getContext();
2036:
2037: context.put("entity", entity);
2038: context.put("methods", javaClass.getMethods());
2039:
2040: // Content
2041:
2042: String content = _processTemplate(_tplPersistence, context);
2043:
2044: // Write file
2045:
2046: File ejbFile = new File(_serviceOutputPath
2047: + "/service/persistence/" + entity.getName()
2048: + "Persistence.java");
2049:
2050: writeFile(ejbFile, content);
2051:
2052: if (!_serviceOutputPath.equals(_outputPath)) {
2053: ejbFile = new File(_outputPath + "/service/persistence/"
2054: + entity.getName() + "Persistence.java");
2055:
2056: if (ejbFile.exists()) {
2057: System.out.println("Relocating " + ejbFile);
2058:
2059: ejbFile.delete();
2060: }
2061: }
2062: }
2063:
2064: private void _createPersistenceImpl(Entity entity) throws Exception {
2065: Map context = _getContext();
2066:
2067: context.put("entity", entity);
2068:
2069: // Content
2070:
2071: String content = _processTemplate(_tplPersistenceImpl, context);
2072:
2073: // Write file
2074:
2075: File ejbFile = new File(_outputPath + "/service/persistence/"
2076: + entity.getName() + "PersistenceImpl.java");
2077:
2078: writeFile(ejbFile, content);
2079: }
2080:
2081: private void _createPersistenceTest(Entity entity) throws Exception {
2082: Map context = _getContext();
2083:
2084: context.put("entity", entity);
2085:
2086: // Content
2087:
2088: String content = _processTemplate(_tplPersistenceTest, context);
2089:
2090: // Write file
2091:
2092: File ejbFile = new File(_testOutputPath
2093: + "/service/persistence/" + entity.getName()
2094: + "PersistenceTest.java");
2095:
2096: writeFile(ejbFile, content);
2097: }
2098:
2099: private void _createPersistenceUtil(Entity entity) throws Exception {
2100: JavaClass javaClass = _getJavaClass(_outputPath
2101: + "/service/persistence/" + entity.getName()
2102: + "PersistenceImpl.java");
2103:
2104: Map context = _getContext();
2105:
2106: context.put("entity", entity);
2107: context.put("methods", javaClass.getMethods());
2108:
2109: // Content
2110:
2111: String content = _processTemplate(_tplPersistenceUtil, context);
2112:
2113: // Write file
2114:
2115: File ejbFile = new File(_serviceOutputPath
2116: + "/service/persistence/" + entity.getName()
2117: + "Util.java");
2118:
2119: writeFile(ejbFile, content);
2120:
2121: if (!_serviceOutputPath.equals(_outputPath)) {
2122: ejbFile = new File(_outputPath + "/service/persistence/"
2123: + entity.getName() + "Util.java");
2124:
2125: if (ejbFile.exists()) {
2126: System.out.println("Relocating " + ejbFile);
2127:
2128: ejbFile.delete();
2129: }
2130: }
2131: }
2132:
2133: private void _createPool(Entity entity) throws IOException {
2134: File ejbFile = new File(_outputPath + "/service/persistence/"
2135: + entity.getName() + "Pool.java");
2136:
2137: if (ejbFile.exists()) {
2138: System.out.println("Removing deprecated " + ejbFile);
2139:
2140: ejbFile.delete();
2141: }
2142: }
2143:
2144: private void _createPrincipalBean() throws Exception {
2145: if (_principalBeanPackage
2146: .equals("com.liferay.portal.service.impl")) {
2147: return;
2148: }
2149:
2150: // Content
2151:
2152: String content = _processTemplate(_tplPrincipalBean);
2153:
2154: // Write file
2155:
2156: File ejbFile = new File(_implDir + "/"
2157: + StringUtil.replace(_principalBeanPackage, ".", "/")
2158: + "/PrincipalBean.java");
2159:
2160: FileUtil.write(ejbFile, content, true);
2161: }
2162:
2163: private void _createProps() throws Exception {
2164: if (_propsUtilPackage.equals("com.liferay.portal.util")) {
2165: return;
2166: }
2167:
2168: // Content
2169:
2170: File propsFile = new File(_implDir
2171: + "/portlet-service.properties");
2172:
2173: long buildNumber = 1;
2174:
2175: if (propsFile.exists()) {
2176: Properties props = PropertiesUtil.load(FileUtil
2177: .read(propsFile));
2178:
2179: buildNumber = GetterUtil.getLong(props
2180: .getProperty("build.number")) + 1;
2181: }
2182:
2183: Map context = _getContext();
2184:
2185: context.put("buildNumber", new Long(buildNumber));
2186: context.put("currentTimeMillis", new Long(System
2187: .currentTimeMillis()));
2188:
2189: String content = _processTemplate(_tplProps, context);
2190:
2191: // Write file
2192:
2193: FileUtil.write(propsFile, content, true);
2194: }
2195:
2196: private void _createPropsUtil() throws Exception {
2197: if (_propsUtilPackage.equals("com.liferay.portal.util")) {
2198: return;
2199: }
2200:
2201: // Content
2202:
2203: String content = _processTemplate(_tplPropsUtil);
2204:
2205: // Write file
2206:
2207: File ejbFile = new File(_implDir + "/"
2208: + StringUtil.replace(_propsUtilPackage, ".", "/")
2209: + "/PropsUtil.java");
2210:
2211: writeFile(ejbFile, content);
2212: }
2213:
2214: private void _createRemotingXML() throws Exception {
2215: StringMaker sm = new StringMaker();
2216:
2217: Document doc = PortalUtil.readDocumentFromFile(new File(
2218: _springFileName), true);
2219:
2220: Iterator itr = doc.getRootElement().elements("bean").iterator();
2221:
2222: while (itr.hasNext()) {
2223: Element beanEl = (Element) itr.next();
2224:
2225: String beanId = beanEl.attributeValue("id");
2226:
2227: if (beanId.endsWith("ServiceFactory")
2228: && !beanId.endsWith("LocalServiceFactory")) {
2229:
2230: String serviceName = beanId.substring(0, beanId
2231: .length() - 7);
2232:
2233: String serviceMapping = serviceName;
2234: serviceMapping = StringUtil.replace(serviceMapping,
2235: ".service.", ".service.spring.");
2236: serviceMapping = StringUtil.replace(serviceMapping,
2237: ".", "_");
2238:
2239: Map context = _getContext();
2240:
2241: context.put("serviceName", serviceName);
2242: context.put("serviceMapping", serviceMapping);
2243:
2244: sm.append(_processTemplate(_tplRemotingXml, context));
2245: }
2246: }
2247:
2248: File outputFile = new File(_remotingFileName);
2249:
2250: if (!outputFile.exists()) {
2251: return;
2252: }
2253:
2254: String content = FileUtil.read(outputFile);
2255: String newContent = content;
2256:
2257: int x = content.indexOf("<bean ");
2258: int y = content.lastIndexOf("</bean>") + 8;
2259:
2260: if (x != -1) {
2261: newContent = content.substring(0, x - 1) + sm.toString()
2262: + content.substring(y, content.length());
2263: } else {
2264: x = content.indexOf("</beans>");
2265:
2266: newContent = content.substring(0, x) + sm.toString()
2267: + content.substring(x, content.length());
2268: }
2269:
2270: newContent = _formatXML(newContent);
2271:
2272: if (!content.equals(newContent)) {
2273: FileUtil.write(outputFile, newContent);
2274:
2275: System.out.println(outputFile.toString());
2276: }
2277: }
2278:
2279: private void _createService(Entity entity, int sessionType)
2280: throws Exception {
2281:
2282: String serviceComments = "This is a remote service. Methods of this service are expected to have security checks based on the propagated JAAS credentials because this service can be accessed remotely.";
2283:
2284: JavaClass javaClass = _getJavaClass(_outputPath
2285: + "/service/impl/" + entity.getName()
2286: + (sessionType != _REMOTE ? "Local" : "")
2287: + "ServiceImpl.java");
2288:
2289: JavaMethod[] methods = javaClass.getMethods();
2290:
2291: if (sessionType == _LOCAL) {
2292: if (javaClass.getSuperClass().getValue().endsWith(
2293: entity.getName() + "LocalServiceBaseImpl")) {
2294:
2295: JavaClass parentJavaClass = _getJavaClass(_outputPath
2296: + "/service/base/" + entity.getName()
2297: + "LocalServiceBaseImpl.java");
2298:
2299: JavaMethod[] parentMethods = parentJavaClass
2300: .getMethods();
2301:
2302: JavaMethod[] allMethods = new JavaMethod[parentMethods.length
2303: + methods.length];
2304:
2305: ArrayUtil.combine(parentMethods, methods, allMethods);
2306:
2307: methods = allMethods;
2308: }
2309:
2310: serviceComments = "This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.";
2311: }
2312:
2313: Map context = _getContext();
2314:
2315: context.put("entity", entity);
2316: context.put("methods", methods);
2317: context
2318: .put("sessionTypeName",
2319: _getSessionTypeName(sessionType));
2320: context.put("serviceComments", serviceComments);
2321:
2322: // Content
2323:
2324: String content = _processTemplate(_tplService, context);
2325:
2326: // Write file
2327:
2328: File ejbFile = new File(_serviceOutputPath + "/service/"
2329: + entity.getName() + _getSessionTypeName(sessionType)
2330: + "Service.java");
2331:
2332: Map jalopySettings = new HashMap();
2333:
2334: jalopySettings.put("keepJavadoc", Boolean.TRUE);
2335:
2336: writeFile(ejbFile, content, jalopySettings);
2337: }
2338:
2339: private void _createServiceBaseImpl(Entity entity, int sessionType)
2340: throws Exception {
2341:
2342: Map context = _getContext();
2343:
2344: context.put("entity", entity);
2345: context
2346: .put("sessionTypeName",
2347: _getSessionTypeName(sessionType));
2348: context.put("referenceList", _mergeReferenceList(entity
2349: .getReferenceList()));
2350:
2351: // Content
2352:
2353: String content = _processTemplate(_tplServiceBaseImpl, context);
2354:
2355: // Write file
2356:
2357: File ejbFile = new File(_outputPath + "/service/base/"
2358: + entity.getName() + _getSessionTypeName(sessionType)
2359: + "ServiceBaseImpl.java");
2360:
2361: writeFile(ejbFile, content);
2362: }
2363:
2364: private void _createServiceFactory(Entity entity, int sessionType)
2365: throws Exception {
2366:
2367: Map context = _getContext();
2368:
2369: context.put("entity", entity);
2370: context
2371: .put("sessionTypeName",
2372: _getSessionTypeName(sessionType));
2373:
2374: // Content
2375:
2376: String content = _processTemplate(_tplServiceFactory, context);
2377:
2378: // Write file
2379:
2380: File ejbFile = new File(_serviceOutputPath + "/service/"
2381: + entity.getName() + _getSessionTypeName(sessionType)
2382: + "ServiceFactory.java");
2383:
2384: Map jalopySettings = new HashMap();
2385:
2386: jalopySettings.put("keepJavadoc", Boolean.TRUE);
2387:
2388: writeFile(ejbFile, content, jalopySettings);
2389: }
2390:
2391: private void _createServiceHttp(Entity entity) throws Exception {
2392: JavaClass javaClass = _getJavaClass(_outputPath
2393: + "/service/impl/" + entity.getName()
2394: + "ServiceImpl.java");
2395:
2396: Map context = _getContext();
2397:
2398: context.put("entity", entity);
2399: context.put("methods", javaClass.getMethods());
2400: context.put("hasHttpMethods", new Boolean(
2401: _hasHttpMethods(javaClass)));
2402:
2403: // Content
2404:
2405: String content = _processTemplate(_tplServiceHttp, context);
2406:
2407: // Write file
2408:
2409: File ejbFile = new File(_outputPath + "/service/http/"
2410: + entity.getName() + "ServiceHttp.java");
2411:
2412: Map jalopySettings = new HashMap();
2413:
2414: jalopySettings.put("keepJavadoc", Boolean.TRUE);
2415:
2416: writeFile(ejbFile, content, jalopySettings);
2417: }
2418:
2419: private void _createServiceImpl(Entity entity, int sessionType)
2420: throws Exception {
2421:
2422: Map context = _getContext();
2423:
2424: context.put("entity", entity);
2425: context
2426: .put("sessionTypeName",
2427: _getSessionTypeName(sessionType));
2428:
2429: // Content
2430:
2431: String content = _processTemplate(_tplServiceImpl, context);
2432:
2433: // Write file
2434:
2435: File ejbFile = new File(_outputPath + "/service/impl/"
2436: + entity.getName() + _getSessionTypeName(sessionType)
2437: + "ServiceImpl.java");
2438:
2439: if (!ejbFile.exists()) {
2440: writeFile(ejbFile, content);
2441: }
2442: }
2443:
2444: private void _createServiceJSON(Entity entity) throws Exception {
2445: JavaClass javaClass = _getJavaClass(_outputPath
2446: + "/service/impl/" + entity.getName()
2447: + "ServiceImpl.java");
2448:
2449: Map context = _getContext();
2450:
2451: context.put("entity", entity);
2452: context.put("methods", javaClass.getMethods());
2453:
2454: // Content
2455:
2456: String content = _processTemplate(_tplServiceJson, context);
2457:
2458: // Write file
2459:
2460: File ejbFile = new File(_outputPath + "/service/http/"
2461: + entity.getName() + "ServiceJSON.java");
2462:
2463: Map jalopySettings = new HashMap();
2464:
2465: jalopySettings.put("keepJavadoc", Boolean.TRUE);
2466:
2467: writeFile(ejbFile, content, jalopySettings);
2468: }
2469:
2470: private void _createServiceJSONSerializer(Entity entity)
2471: throws Exception {
2472: Map context = _getContext();
2473:
2474: context.put("entity", entity);
2475:
2476: // Content
2477:
2478: String content = _processTemplate(_tplServiceJsonSerializer,
2479: context);
2480:
2481: // Write file
2482:
2483: File ejbFile = new File(_outputPath + "/service/http/"
2484: + entity.getName() + "JSONSerializer.java");
2485:
2486: Map jalopySettings = new HashMap();
2487:
2488: jalopySettings.put("keepJavadoc", Boolean.TRUE);
2489:
2490: writeFile(ejbFile, content, jalopySettings);
2491: }
2492:
2493: private void _createServiceSoap(Entity entity) throws Exception {
2494: JavaClass javaClass = _getJavaClass(_outputPath
2495: + "/service/impl/" + entity.getName()
2496: + "ServiceImpl.java");
2497:
2498: Map context = _getContext();
2499:
2500: context.put("entity", entity);
2501: context.put("methods", javaClass.getMethods());
2502:
2503: // Content
2504:
2505: String content = _processTemplate(_tplServiceSoap, context);
2506:
2507: // Write file
2508:
2509: File ejbFile = new File(_outputPath + "/service/http/"
2510: + entity.getName() + "ServiceSoap.java");
2511:
2512: Map jalopySettings = new HashMap();
2513:
2514: jalopySettings.put("keepJavadoc", Boolean.TRUE);
2515:
2516: writeFile(ejbFile, content, jalopySettings);
2517: }
2518:
2519: private void _createServiceUtil(Entity entity, int sessionType)
2520: throws Exception {
2521:
2522: JavaClass javaClass = _getJavaClass(_serviceOutputPath
2523: + "/service/" + entity.getName()
2524: + (sessionType != _REMOTE ? "Local" : "")
2525: + "Service.java");
2526:
2527: Map context = _getContext();
2528:
2529: context.put("entity", entity);
2530: context.put("methods", javaClass.getMethods());
2531: context
2532: .put("sessionTypeName",
2533: _getSessionTypeName(sessionType));
2534:
2535: // Content
2536:
2537: String content = _processTemplate(_tplServiceUtil, context);
2538:
2539: // Write file
2540:
2541: File ejbFile = new File(_serviceOutputPath + "/service/"
2542: + entity.getName() + _getSessionTypeName(sessionType)
2543: + "ServiceUtil.java");
2544:
2545: Map jalopySettings = new HashMap();
2546:
2547: jalopySettings.put("keepJavadoc", Boolean.TRUE);
2548:
2549: writeFile(ejbFile, content, jalopySettings);
2550: }
2551:
2552: private void _createSpringDataSourceXML() throws Exception {
2553: if (Validator.isNull(_springDataSourceFileName)) {
2554: return;
2555: }
2556:
2557: // Content
2558:
2559: String content = _processTemplate(_tplSpringDataSourceXml);
2560:
2561: // Write file
2562:
2563: File ejbFile = new File(_springDataSourceFileName);
2564:
2565: FileUtil.write(ejbFile, content, true);
2566: }
2567:
2568: private void _createSpringUtil() throws Exception {
2569: if (_springUtilPackage.equals("com.liferay.portal.spring.util")) {
2570: return;
2571: }
2572:
2573: // Content
2574:
2575: String content = _processTemplate(_tplSpringUtil);
2576:
2577: // Write file
2578:
2579: File ejbFile = new File(_implDir + "/"
2580: + StringUtil.replace(_springUtilPackage, ".", "/")
2581: + "/SpringUtil.java");
2582:
2583: FileUtil.write(ejbFile, content, true);
2584: }
2585:
2586: private void _createSpringXML() throws Exception {
2587: Map context = _getContext();
2588:
2589: context.put("entities", _ejbList);
2590:
2591: // Content
2592:
2593: String content = _processTemplate(_tplSpringXml, context);
2594:
2595: File xmlFile = new File(_springFileName);
2596:
2597: if (!xmlFile.exists()) {
2598: String xml = "<?xml version=\"1.0\"?>\n"
2599: + "<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">\n"
2600: + "\n" + "<beans>\n" + "</beans>";
2601:
2602: FileUtil.write(xmlFile, xml);
2603: }
2604:
2605: String oldContent = FileUtil.read(xmlFile);
2606: String newContent = _fixSpringXML(oldContent);
2607:
2608: int x = oldContent.indexOf("<beans>");
2609: int y = oldContent.lastIndexOf("</beans>");
2610:
2611: int firstSession = newContent.indexOf("<bean id=\""
2612: + _packagePath + ".service.", x);
2613:
2614: int lastSession = newContent.lastIndexOf("<bean id=\""
2615: + _packagePath + ".service.", y);
2616:
2617: if (firstSession == -1 || firstSession > y) {
2618: x = newContent.indexOf("</beans>");
2619: newContent = newContent.substring(0, x) + content
2620: + newContent.substring(x, newContent.length());
2621: } else {
2622: firstSession = newContent
2623: .lastIndexOf("<bean", firstSession) - 1;
2624: lastSession = newContent.indexOf("</bean>", lastSession) + 8;
2625:
2626: newContent = newContent.substring(0, firstSession)
2627: + content
2628: + newContent.substring(lastSession, newContent
2629: .length());
2630: }
2631:
2632: newContent = _formatXML(newContent);
2633:
2634: if (!oldContent.equals(newContent)) {
2635: FileUtil.write(xmlFile, newContent);
2636: }
2637: }
2638:
2639: private void _createSQLIndexes() throws IOException {
2640: if (!FileUtil.exists(_sqlDir)) {
2641: return;
2642: }
2643:
2644: // indexes.sql
2645:
2646: File sqlFile = new File(_sqlDir + "/indexes.sql");
2647:
2648: if (!sqlFile.exists()) {
2649: FileUtil.write(sqlFile, "");
2650: }
2651:
2652: Map indexSQLs = new TreeMap();
2653:
2654: BufferedReader br = new BufferedReader(new FileReader(sqlFile));
2655:
2656: while (true) {
2657: String indexSQL = br.readLine();
2658:
2659: if (indexSQL == null) {
2660: break;
2661: }
2662:
2663: if (Validator.isNotNull(indexSQL.trim())) {
2664: int pos = indexSQL.indexOf(" on ");
2665:
2666: String indexSpec = indexSQL.substring(pos + 4);
2667:
2668: indexSQLs.put(indexSpec, indexSQL);
2669: }
2670: }
2671:
2672: br.close();
2673:
2674: // indexes.properties
2675:
2676: File propsFile = new File(_sqlDir + "/indexes.properties");
2677:
2678: if (!propsFile.exists()) {
2679: FileUtil.write(propsFile, "");
2680: }
2681:
2682: Map indexProps = new TreeMap();
2683:
2684: br = new BufferedReader(new FileReader(propsFile));
2685:
2686: while (true) {
2687: String indexMapping = br.readLine();
2688:
2689: if (indexMapping == null) {
2690: break;
2691: }
2692:
2693: if (Validator.isNotNull(indexMapping.trim())) {
2694: String[] splitIndexMapping = indexMapping.split("\\=");
2695:
2696: indexProps.put(splitIndexMapping[1],
2697: splitIndexMapping[0]);
2698: }
2699: }
2700:
2701: br.close();
2702:
2703: // indexes.sql
2704:
2705: for (int i = 0; i < _ejbList.size(); i++) {
2706: Entity entity = (Entity) _ejbList.get(i);
2707:
2708: List finderList = entity.getFinderList();
2709:
2710: for (int j = 0; j < finderList.size(); j++) {
2711: EntityFinder finder = (EntityFinder) finderList.get(j);
2712:
2713: if (finder.isDBIndex()) {
2714: StringMaker sm = new StringMaker();
2715:
2716: sm.append(entity.getTable() + " (");
2717:
2718: List finderColsList = finder.getColumns();
2719:
2720: for (int k = 0; k < finderColsList.size(); k++) {
2721: EntityColumn col = (EntityColumn) finderColsList
2722: .get(k);
2723:
2724: sm.append(col.getDBName());
2725:
2726: if ((k + 1) != finderColsList.size()) {
2727: sm.append(", ");
2728: }
2729: }
2730:
2731: sm.append(");");
2732:
2733: String indexSpec = sm.toString();
2734:
2735: String indexHash = Integer.toHexString(
2736: indexSpec.hashCode()).toUpperCase();
2737:
2738: String indexName = "IX_" + indexHash;
2739:
2740: sm = new StringMaker();
2741:
2742: sm.append("create index " + indexName + " on ");
2743: sm.append(indexSpec);
2744:
2745: indexSQLs.put(indexSpec, sm.toString());
2746:
2747: String finderName = entity.getTable()
2748: + StringPool.PERIOD + finder.getName();
2749:
2750: indexProps.put(finderName, indexName);
2751: }
2752: }
2753: }
2754:
2755: StringMaker sm = new StringMaker();
2756:
2757: Iterator itr = indexSQLs.values().iterator();
2758:
2759: String prevEntityName = null;
2760:
2761: while (itr.hasNext()) {
2762: String indexSQL = (String) itr.next();
2763:
2764: String entityName = indexSQL.split(" ")[4];
2765:
2766: if ((prevEntityName != null)
2767: && (!prevEntityName.equals(entityName))) {
2768:
2769: sm.append("\n");
2770: }
2771:
2772: sm.append(indexSQL);
2773:
2774: if (itr.hasNext()) {
2775: sm.append("\n");
2776: }
2777:
2778: prevEntityName = entityName;
2779: }
2780:
2781: FileUtil.write(sqlFile, sm.toString(), true);
2782:
2783: // indexes.properties
2784:
2785: sm = new StringMaker();
2786:
2787: itr = indexProps.keySet().iterator();
2788:
2789: prevEntityName = null;
2790:
2791: while (itr.hasNext()) {
2792: String finderName = (String) itr.next();
2793:
2794: String indexName = (String) indexProps.get(finderName);
2795:
2796: String entityName = finderName.split("\\.")[0];
2797:
2798: if ((prevEntityName != null)
2799: && (!prevEntityName.equals(entityName))) {
2800:
2801: sm.append("\n");
2802: }
2803:
2804: sm.append(indexName + StringPool.EQUAL + finderName);
2805:
2806: if (itr.hasNext()) {
2807: sm.append("\n");
2808: }
2809:
2810: prevEntityName = entityName;
2811: }
2812:
2813: FileUtil.write(propsFile, sm.toString(), true);
2814: }
2815:
2816: private void _createSQLSequences() throws IOException {
2817: if (!FileUtil.exists(_sqlDir)) {
2818: return;
2819: }
2820:
2821: File sqlFile = new File(_sqlDir + "/sequences.sql");
2822:
2823: if (!sqlFile.exists()) {
2824: FileUtil.write(sqlFile, "");
2825: }
2826:
2827: Set sequenceSQLs = new TreeSet();
2828:
2829: BufferedReader br = new BufferedReader(new FileReader(sqlFile));
2830:
2831: while (true) {
2832: String sequenceSQL = br.readLine();
2833:
2834: if (sequenceSQL == null) {
2835: break;
2836: }
2837:
2838: if (Validator.isNotNull(sequenceSQL)) {
2839: sequenceSQLs.add(sequenceSQL);
2840: }
2841: }
2842:
2843: br.close();
2844:
2845: for (int i = 0; i < _ejbList.size(); i++) {
2846: Entity entity = (Entity) _ejbList.get(i);
2847:
2848: List columnList = entity.getColumnList();
2849:
2850: for (int j = 0; j < columnList.size(); j++) {
2851: EntityColumn column = (EntityColumn) columnList.get(j);
2852:
2853: if ("sequence".equals(column.getIdType())) {
2854: StringMaker sm = new StringMaker();
2855:
2856: String sequenceName = column.getIdParam();
2857:
2858: if (sequenceName.length() > 30) {
2859: sequenceName = sequenceName.substring(0, 30);
2860: }
2861:
2862: sm.append("create sequence " + sequenceName + ";");
2863:
2864: String sequenceSQL = sm.toString();
2865:
2866: if (!sequenceSQLs.contains(sequenceSQL)) {
2867: sequenceSQLs.add(sequenceSQL);
2868: }
2869: }
2870: }
2871: }
2872:
2873: StringMaker sm = new StringMaker();
2874:
2875: Iterator itr = sequenceSQLs.iterator();
2876:
2877: while (itr.hasNext()) {
2878: String sequenceSQL = (String) itr.next();
2879:
2880: sm.append(sequenceSQL);
2881:
2882: if (itr.hasNext()) {
2883: sm.append("\n");
2884: }
2885: }
2886:
2887: FileUtil.write(sqlFile, sm.toString(), true);
2888: }
2889:
2890: private void _createSQLTables() throws IOException {
2891: if (!FileUtil.exists(_sqlDir)) {
2892: return;
2893: }
2894:
2895: File sqlFile = new File(_sqlDir + "/" + _sqlFileName);
2896:
2897: if (!sqlFile.exists()) {
2898: FileUtil.write(sqlFile, StringPool.BLANK);
2899: }
2900:
2901: for (int i = 0; i < _ejbList.size(); i++) {
2902: Entity entity = (Entity) _ejbList.get(i);
2903:
2904: String createTableSQL = _getCreateTableSQL(entity);
2905:
2906: if (Validator.isNotNull(createTableSQL)) {
2907: _createSQLTables(sqlFile, createTableSQL, entity, true);
2908:
2909: File updateSQLFile = new File(_sqlDir
2910: + "/update-4.3.6-4.4.0.sql");
2911:
2912: if (updateSQLFile.exists()) {
2913: _createSQLTables(updateSQLFile, createTableSQL,
2914: entity, false);
2915: }
2916: }
2917: }
2918: }
2919:
2920: private void _createSQLTables(File sqlFile,
2921: String newCreateTableString, Entity entity,
2922: boolean addMissingTables) throws IOException {
2923:
2924: if (!sqlFile.exists()) {
2925: FileUtil.write(sqlFile, StringPool.BLANK);
2926: }
2927:
2928: String content = FileUtil.read(sqlFile);
2929:
2930: int x = content.indexOf(_CREATE_TABLE + entity.getTable()
2931: + " (");
2932: int y = content.indexOf(");", x);
2933:
2934: if (x != -1) {
2935: String oldCreateTableString = content.substring(x + 1, y);
2936:
2937: if (!oldCreateTableString.equals(newCreateTableString)) {
2938: content = content.substring(0, x)
2939: + newCreateTableString
2940: + content.substring(y + 2, content.length());
2941:
2942: FileUtil.write(sqlFile, content);
2943: }
2944: } else if (addMissingTables) {
2945: StringMaker sm = new StringMaker();
2946:
2947: BufferedReader br = new BufferedReader(new StringReader(
2948: content));
2949:
2950: String line = null;
2951: boolean appendNewTable = true;
2952:
2953: while ((line = br.readLine()) != null) {
2954: if (appendNewTable && line.startsWith(_CREATE_TABLE)) {
2955: x = _CREATE_TABLE.length();
2956: y = line.indexOf(" ", x);
2957:
2958: String tableName = line.substring(x, y);
2959:
2960: if (tableName.compareTo(entity.getTable()) > 0) {
2961: sm.append(newCreateTableString + "\n\n");
2962:
2963: appendNewTable = false;
2964: }
2965: }
2966:
2967: sm.append(line).append('\n');
2968: }
2969:
2970: if (appendNewTable) {
2971: sm.append("\n" + newCreateTableString);
2972: }
2973:
2974: br.close();
2975:
2976: FileUtil.write(sqlFile, sm.toString(), true);
2977: }
2978: }
2979:
2980: private String _fixHBMXML(String content) throws IOException {
2981: StringMaker sm = new StringMaker();
2982:
2983: BufferedReader br = new BufferedReader(
2984: new StringReader(content));
2985:
2986: String line = null;
2987:
2988: while ((line = br.readLine()) != null) {
2989: if (line.startsWith("\t<class name=\"")) {
2990: line = StringUtil.replace(line, new String[] {
2991: ".service.persistence.", "HBM\" table=\"" },
2992: new String[] { ".model.", "\" table=\"" });
2993:
2994: if (line.indexOf(".model.impl.") == -1) {
2995: line = StringUtil.replace(line, new String[] {
2996: ".model.", "\" table=\"" }, new String[] {
2997: ".model.impl.", "Impl\" table=\"" });
2998: }
2999: }
3000:
3001: sm.append(line);
3002: sm.append('\n');
3003: }
3004:
3005: br.close();
3006:
3007: return sm.toString().trim();
3008: }
3009:
3010: private String _fixSpringXML(String content) throws IOException {
3011: return StringUtil.replace(content, ".service.spring.",
3012: ".service.");
3013: }
3014:
3015: private String _formatXML(String xml) throws DocumentException,
3016: IOException {
3017:
3018: String doctype = null;
3019:
3020: int x = xml.indexOf("<!DOCTYPE");
3021:
3022: if (x != -1) {
3023: int y = xml.indexOf(">", x) + 1;
3024:
3025: doctype = xml.substring(x, y);
3026:
3027: xml = xml.substring(0, x) + "\n" + xml.substring(y);
3028: }
3029:
3030: xml = StringUtil.replace(xml, '\r', "");
3031: xml = XMLFormatter.toString(xml);
3032: xml = StringUtil.replace(xml, "\"/>", "\" />");
3033:
3034: if (Validator.isNotNull(doctype)) {
3035: x = xml.indexOf("?>") + 2;
3036:
3037: xml = xml.substring(0, x) + "\n" + doctype
3038: + xml.substring(x);
3039: }
3040:
3041: return xml;
3042: }
3043:
3044: private Map _getContext() {
3045: Map context = new HashMap();
3046:
3047: context.put("hbmFileName", _hbmFileName);
3048: context.put("modelHintsFileName", _modelHintsFileName);
3049: context.put("springFileName", _springFileName);
3050: context.put("springDataSourceFileName",
3051: _springDataSourceFileName);
3052: context.put("apiDir", _apiDir);
3053: context.put("implDir", _implDir);
3054: context.put("jsonFileName", _jsonFileName);
3055: context.put("sqlDir", _sqlDir);
3056: context.put("sqlFileName", _sqlFileName);
3057: context.put("baseModelImplPackage", _baseModelImplPackage);
3058: context.put("basePersistencePackage", _basePersistencePackage);
3059: context.put("beanLocatorUtilPackage", _beanLocatorUtilPackage);
3060: context.put("principalBeanPackage", _principalBeanPackage);
3061: context.put("propsUtilPackage", _propsUtilPackage);
3062: context.put("springHibernatePackage", _springHibernatePackage);
3063: context.put("springUtilPackage", _springUtilPackage);
3064: context.put("portletName", _portletName);
3065: context.put("portletShortName", _portletShortName);
3066: context.put("portletPackageName", _portletPackageName);
3067: context.put("outputPath", _outputPath);
3068: context.put("serviceOutputPath", _serviceOutputPath);
3069: context.put("packagePath", _packagePath);
3070: context.put("serviceBuilder", this );
3071:
3072: context.put("stringUtil", StringUtil_IW.getInstance());
3073:
3074: try {
3075: BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
3076:
3077: TemplateHashModel staticModels = wrapper.getStaticModels();
3078:
3079: context.put("modelHintsUtil", staticModels
3080: .get("com.liferay.portal.model.ModelHintsUtil"));
3081: context.put("validator", staticModels
3082: .get("com.liferay.portal.kernel.util.Validator"));
3083: } catch (TemplateModelException tme) {
3084: }
3085:
3086: return context;
3087: }
3088:
3089: private String _getCreateTableSQL(Entity entity) {
3090: List pkList = entity.getPKList();
3091: List regularColList = entity.getRegularColList();
3092:
3093: if (regularColList.size() == 0) {
3094: return null;
3095: }
3096:
3097: StringMaker sm = new StringMaker();
3098:
3099: sm.append(_CREATE_TABLE + entity.getTable() + " (\n");
3100:
3101: for (int i = 0; i < regularColList.size(); i++) {
3102: EntityColumn col = (EntityColumn) regularColList.get(i);
3103:
3104: String colName = col.getName();
3105: String colType = col.getType();
3106: String colIdType = col.getIdType();
3107:
3108: sm.append("\t" + col.getDBName());
3109: sm.append(" ");
3110:
3111: if (colType.equalsIgnoreCase("boolean")) {
3112: sm.append("BOOLEAN");
3113: } else if (colType.equalsIgnoreCase("double")
3114: || colType.equalsIgnoreCase("float")) {
3115:
3116: sm.append("DOUBLE");
3117: } else if (colType.equals("int")
3118: || colType.equals("Integer")
3119: || colType.equalsIgnoreCase("short")) {
3120:
3121: sm.append("INTEGER");
3122: } else if (colType.equalsIgnoreCase("long")) {
3123: sm.append("LONG");
3124: } else if (colType.equals("String")) {
3125: Map hints = ModelHintsUtil.getHints(_packagePath
3126: + ".model." + entity.getName(), colName);
3127:
3128: int maxLength = 75;
3129:
3130: if (hints != null) {
3131: maxLength = GetterUtil.getInteger((String) hints
3132: .get("max-length"), maxLength);
3133: }
3134:
3135: if (maxLength < 4000) {
3136: sm.append("VARCHAR(" + maxLength + ")");
3137: } else if (maxLength == 4000) {
3138: sm.append("STRING");
3139: } else if (maxLength > 4000) {
3140: sm.append("TEXT");
3141: }
3142: } else if (colType.equals("Date")) {
3143: sm.append("DATE null");
3144: } else {
3145: sm.append("invalid");
3146: }
3147:
3148: if (col.isPrimary()) {
3149: sm.append(" not null");
3150:
3151: if (!entity.hasCompoundPK()) {
3152: sm.append(" primary key");
3153: }
3154: } else if (colType.equals("String")) {
3155: sm.append(" null");
3156: }
3157:
3158: if (Validator.isNotNull(colIdType)
3159: && colIdType.equals("identity")) {
3160:
3161: sm.append(" IDENTITY");
3162: }
3163:
3164: if (((i + 1) != regularColList.size())
3165: || (entity.hasCompoundPK())) {
3166:
3167: sm.append(",");
3168: }
3169:
3170: sm.append("\n");
3171: }
3172:
3173: if (entity.hasCompoundPK()) {
3174: sm.append("\tprimary key (");
3175:
3176: for (int j = 0; j < pkList.size(); j++) {
3177: EntityColumn pk = (EntityColumn) pkList.get(j);
3178:
3179: sm.append(pk.getDBName());
3180:
3181: if ((j + 1) != pkList.size()) {
3182: sm.append(", ");
3183: }
3184: }
3185:
3186: sm.append(")\n");
3187: }
3188:
3189: sm.append(");");
3190:
3191: return sm.toString();
3192: }
3193:
3194: private String _getDimensions(Type type) {
3195: String dimensions = "";
3196:
3197: for (int i = 0; i < type.getDimensions(); i++) {
3198: dimensions += "[]";
3199: }
3200:
3201: return dimensions;
3202: }
3203:
3204: private JavaClass _getJavaClass(String fileName) throws IOException {
3205: int pos = fileName.indexOf(_implDir + "/") + _implDir.length();
3206:
3207: String srcFile = fileName.substring(pos + 1, fileName.length());
3208: String className = StringUtil.replace(srcFile.substring(0,
3209: srcFile.length() - 5), "/", ".");
3210:
3211: JavaDocBuilder builder = new JavaDocBuilder();
3212:
3213: File file = new File(fileName);
3214:
3215: if (!file.exists()) {
3216: return null;
3217: }
3218:
3219: builder.addSource(file);
3220:
3221: return builder.getClassByName(className);
3222: }
3223:
3224: private String _getSessionTypeName(int sessionType) {
3225: if (sessionType == _LOCAL) {
3226: return "Local";
3227: } else {
3228: return "";
3229: }
3230: }
3231:
3232: private String _getTplProperty(String key, String defaultValue) {
3233: return System.getProperty("service.tpl." + key, defaultValue);
3234: }
3235:
3236: private boolean _hasHttpMethods(JavaClass javaClass) {
3237: JavaMethod[] methods = javaClass.getMethods();
3238:
3239: for (int i = 0; i < methods.length; i++) {
3240: JavaMethod javaMethod = methods[i];
3241:
3242: if (!javaMethod.isConstructor() && javaMethod.isPublic()
3243: && isCustomMethod(javaMethod)) {
3244:
3245: return true;
3246: }
3247: }
3248:
3249: return false;
3250: }
3251:
3252: private List _mergeReferenceList(List referenceList) {
3253: List list = new ArrayList(_ejbList.size()
3254: + referenceList.size());
3255:
3256: list.addAll(_ejbList);
3257: list.addAll(referenceList);
3258:
3259: return list;
3260: }
3261:
3262: private String _processTemplate(String name) throws Exception {
3263: return _processTemplate(name, _getContext());
3264: }
3265:
3266: private String _processTemplate(String name, Map context)
3267: throws Exception {
3268: return FreeMarkerUtil.process(name, context);
3269: }
3270:
3271: private static final int _REMOTE = 0;
3272:
3273: private static final int _LOCAL = 1;
3274:
3275: private static final String _CREATE_TABLE = "create table ";
3276:
3277: private static final String _TPL_ROOT = "com/liferay/portal/tools/servicebuilder/dependencies/";
3278:
3279: private String _tplBaseModeImpl = _TPL_ROOT + "base_mode_impl.ftl";
3280: private String _tplBasePersistence = _TPL_ROOT
3281: + "base_persistence.ftl";
3282: private String _tplBeanLocatorUtil = _TPL_ROOT
3283: + "bean_locator_util.ftl";
3284: private String _tplDynamicDialect = _TPL_ROOT
3285: + "dynamic_dialect.ftl";
3286: private String _tplEjbPk = _TPL_ROOT + "ejb_pk.ftl";
3287: private String _tplException = _TPL_ROOT + "exception.ftl";
3288: private String _tplExtendedModel = _TPL_ROOT + "extended_model.ftl";
3289: private String _tplExtendedModelImpl = _TPL_ROOT
3290: + "extended_model_impl.ftl";
3291: private String _tplFinder = _TPL_ROOT + "finder.ftl";
3292: private String _tplFinderCache = _TPL_ROOT + "finder_cache.ftl";
3293: private String _tplFinderUtil = _TPL_ROOT + "finder_util.ftl";
3294: private String _tplHbmXml = _TPL_ROOT + "hbm_xml.ftl";
3295: private String _tplHibernateConfiguration = _TPL_ROOT
3296: + "hibernate_configuration.ftl";
3297: private String _tplHibernateUtil = _TPL_ROOT + "hibernate_util.ftl";
3298: private String _tplJsonJs = _TPL_ROOT + "json_js.ftl";
3299: private String _tplJsonJsMethod = _TPL_ROOT + "json_js_method.ftl";
3300: private String _tplModel = _TPL_ROOT + "model.ftl";
3301: private String _tplModelHintsXml = _TPL_ROOT
3302: + "model_hints_xml.ftl";
3303: private String _tplModelImpl = _TPL_ROOT + "model_impl.ftl";
3304: private String _tplModelSoap = _TPL_ROOT + "model_soap.ftl";
3305: private String _tplPersistence = _TPL_ROOT + "persistence.ftl";
3306: private String _tplPersistenceImpl = _TPL_ROOT
3307: + "persistence_impl.ftl";
3308: private String _tplPersistenceTest = _TPL_ROOT
3309: + "persistence_test.ftl";
3310: private String _tplPersistenceUtil = _TPL_ROOT
3311: + "persistence_util.ftl";
3312: private String _tplPrincipalBean = _TPL_ROOT + "principal_bean.ftl";
3313: private String _tplProps = _TPL_ROOT + "props.ftl";
3314: private String _tplPropsUtil = _TPL_ROOT + "props_util.ftl";
3315: private String _tplRemotingXml = _TPL_ROOT + "remoting_xml.ftl";
3316: private String _tplService = _TPL_ROOT + "service.ftl";
3317: private String _tplServiceBaseImpl = _TPL_ROOT
3318: + "service_base_impl.ftl";
3319: private String _tplServiceFactory = _TPL_ROOT
3320: + "service_factory.ftl";
3321: private String _tplServiceHttp = _TPL_ROOT + "service_http.ftl";
3322: private String _tplServiceImpl = _TPL_ROOT + "service_impl.ftl";
3323: private String _tplServiceJson = _TPL_ROOT + "service_json.ftl";
3324: private String _tplServiceJsonSerializer = _TPL_ROOT
3325: + "service_json_serializer.ftl";
3326: private String _tplServiceSoap = _TPL_ROOT + "service_soap.ftl";
3327: private String _tplServiceUtil = _TPL_ROOT + "service_util.ftl";
3328: private String _tplSpringDataSourceXml = _TPL_ROOT
3329: + "spring_data_source_xml.ftl";
3330: private String _tplSpringUtil = _TPL_ROOT + "spring_util.ftl";
3331: private String _tplSpringXml = _TPL_ROOT + "spring_xml.ftl";
3332: private Set _badTableNames;
3333: private Set _badCmpFields;
3334: private String _hbmFileName;
3335: private String _modelHintsFileName;
3336: private String _springFileName;
3337: private String _springDataSourceFileName;
3338: private String _apiDir;
3339: private String _implDir;
3340: private String _jsonFileName;
3341: private String _remotingFileName;
3342: private String _sqlDir;
3343: private String _sqlFileName;
3344: private boolean _autoNamespaceTables;
3345: private String _baseModelImplPackage;
3346: private String _basePersistencePackage;
3347: private String _beanLocatorUtilPackage;
3348: private String _principalBeanPackage;
3349: private String _propsUtilPackage;
3350: private String _springHibernatePackage;
3351: private String _springUtilPackage;
3352: private String _testDir;
3353: private String _portletName = StringPool.BLANK;
3354: private String _portletShortName = StringPool.BLANK;
3355: private String _portletPackageName = StringPool.BLANK;
3356: private String _outputPath;
3357: private String _serviceOutputPath;
3358: private String _testOutputPath;
3359: private String _packagePath;
3360: private List _ejbList;
3361:
3362: }
|