📄 servicebuilder.java
字号:
}
if (!col.isPrimitiveType() && !col.getType().equals("String")) {
sm.append(col.getName() + ".toString()");
}
else {
sm.append("String.valueOf(" + col.getName() + ")");
}
}
sm.append(").hashCode();");
sm.append("}");
// To string method
sm.append("public String toString() {");
sm.append("StringMaker sm = new StringMaker();");
sm.append("sm.append(StringPool.OPEN_CURLY_BRACE);");
for (int i = 0; i < pkList.size(); i++) {
EntityColumn col = (EntityColumn)pkList.get(i);
sm.append("sm.append(\"" + col.getName() + "\");");
sm.append("sm.append(StringPool.EQUAL);");
sm.append("sm.append(" + col.getName() + ");");
if ((i + 1) != pkList.size()) {
sm.append("sm.append(StringPool.COMMA);");
sm.append("sm.append(StringPool.SPACE);");
}
}
sm.append("sm.append(StringPool.CLOSE_CURLY_BRACE);");
sm.append("return sm.toString();");
sm.append("}");
// Class close brace
sm.append("}");
// Write file
File ejbFile = new File(_serviceOutputPath + "/service/persistence/" + entity.getPKClassName() + ".java");
writeFile(ejbFile, sm.toString());
/*ejbFile = new File(_outputPath + "/service/persistence/" + entity.getPKClassName() + ".java");
if (ejbFile.exists()) {
System.out.println("Relocating " + ejbFile);
ejbFile.delete();
}*/
}
private void _createExceptions(List exceptions) throws IOException {
String copyright = null;
try {
copyright = FileUtil.read("../copyright.txt");
}
catch (FileNotFoundException fnfe) {
}
for (int i = 0; i < _ejbList.size(); i++) {
Entity entity = (Entity)_ejbList.get(i);
if (entity.hasColumns()) {
exceptions.add(_getNoSuchEntityException(entity));
}
}
for (int i = 0; i < exceptions.size(); i++) {
String exception = (String)exceptions.get(i);
StringMaker sm = new StringMaker();
if (Validator.isNotNull(copyright)) {
sm.append(copyright + "\n");
sm.append("\n");
}
sm.append("package " + _packagePath + ";\n");
sm.append("\n");
sm.append("import com.bkgd.platform.system.BkPlatException;\n");
sm.append("\n");
if (Validator.isNotNull(copyright)) {
sm.append("/**\n");
sm.append(" * <a href=\"" + exception + "Exception.java.html\"><b><i>View Source</i></b></a>\n");
sm.append(" *\n");
sm.append(" * @author Brian Wing Shun Chan\n");
sm.append(" *\n");
sm.append(" */\n");
}
sm.append("public class " + exception + "Exception extends BkPlatException {\n");
sm.append("\n");
sm.append("\tpublic " + exception + "Exception() {\n");
sm.append("\t\tsuper();\n");
sm.append("\t}\n");
sm.append("\n");
sm.append("\tpublic " + exception + "Exception(String msg) {\n");
sm.append("\t\tsuper(msg);\n");
sm.append("\t}\n");
sm.append("\n");
sm.append("\tpublic " + exception + "Exception(String msg, Throwable cause) {\n");
sm.append("\t\tsuper(msg, cause);\n");
sm.append("\t}\n");
sm.append("\n");
sm.append("\tpublic " + exception + "Exception(Throwable cause) {\n");
sm.append("\t\tsuper(cause);\n");
sm.append("\t}\n");
sm.append("\n");
sm.append("}");
File exceptionFile = new File(_serviceOutputPath + "/" + exception + "Exception.java");
if (!exceptionFile.exists()) {
FileUtil.write(exceptionFile, sm.toString());
}
/*exceptionFile = new File(_outputPath + "/" + exception + "Exception.java");
if (exceptionFile.exists()) {
System.out.println("Relocating " + exceptionFile);
exceptionFile.delete();
}*/
}
}
private void _createExtendedModel(Entity entity) throws IOException {
JavaClass javaClass = _getJavaClass(_outputPath + "/model/impl/" + entity.getName() + "Impl.java");
JavaMethod[] methods = javaClass.getMethods();
StringMaker sm = new StringMaker();
// Package
sm.append("package " + _packagePath + ".model;");
// Interface declaration
sm.append("public interface " + entity.getName() + " extends " + entity.getName() + "Model {");
// Methods
for (int i = 0; i < methods.length; i++) {
JavaMethod javaMethod = methods[i];
String methodName = javaMethod.getName();
if (!javaMethod.isConstructor() && !javaMethod.isStatic() && javaMethod.isPublic()) {
sm.append("public " + javaMethod.getReturns().getValue() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
JavaParameter[] parameters = javaMethod.getParameters();
for (int j = 0; j < parameters.length; j++) {
JavaParameter javaParameter = parameters[j];
sm.append(javaParameter.getType().getValue() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
if ((j + 1) != parameters.length) {
sm.append(", ");
}
}
sm.append(")");
Type[] thrownExceptions = javaMethod.getExceptions();
Set newExceptions = new LinkedHashSet();
for (int j = 0; j < thrownExceptions.length; j++) {
Type thrownException = thrownExceptions[j];
newExceptions.add(thrownException.getValue());
}
if (newExceptions.size() > 0) {
sm.append(" throws ");
Iterator itr = newExceptions.iterator();
while (itr.hasNext()) {
sm.append(itr.next());
if (itr.hasNext()) {
sm.append(", ");
}
}
}
sm.append(";");
}
}
// Interface close brace
sm.append("}");
// Write file
File modelFile = new File(_serviceOutputPath + "/model/" + entity.getName() + ".java");
Map jalopySettings = new HashMap();
String[] classComments = {
_DEFAULT_CLASS_COMMENTS,
"This interface is a model that represents the <code>" + entity.getTable() + "</code> table in the database.",
"Customize <code>" + _packagePath + ".service.model.impl." + entity.getName() + "Impl</code> and rerun the ServiceBuilder to generate the new methods."
};
String[] see = {
_packagePath + ".service.model." + entity.getName() + "Model",
_packagePath + ".service.model.impl." + entity.getName() + "Impl",
_packagePath + ".service.model.impl." + entity.getName() + "ModelImpl"
};
jalopySettings.put("classComments", classComments);
jalopySettings.put("see", see);
writeFile(modelFile, sm.toString(), jalopySettings);
/*modelFile = new File(_outputPath + "/model/" + entity.getName() + ".java");
if (modelFile.exists()) {
System.out.println("Relocating " + modelFile);
modelFile.delete();
}*/
}
private void _createExtendedModelImpl(Entity entity) throws IOException {
StringMaker sm = new StringMaker();
// Package
sm.append("package " + _packagePath + ".model.impl;");
// Imports
sm.append("import " + _packagePath + ".model." + entity.getName() + ";");
// Class declaration
sm.append("public class " + entity.getName() + "Impl extends " + entity.getName() + "ModelImpl implements " + entity.getName() + " {");
// Empty constructor
sm.append("public " + entity.getName() + "Impl() {");
sm.append("}");
// Class close brace
sm.append("}");
// Write file
File modelFile = new File(_outputPath + "/model/impl/" + entity.getName() + "Impl.java");
if (!modelFile.exists()) {
writeFile(modelFile, sm.toString());
}
}
private void _createFinderCache() throws IOException {
if (_springHibernatePackage.equals("com.bkgd.platform.system.spring.hibernate")) {
return;
}
// Content
String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/FinderCache.java");
content = StringUtil.replace(
content,
new String[] {
"package com.bkgd.platform.system.spring.hibernate;",
"import com.bkgd.platform.system.util.PropsUtil;"
},
new String[] {
"package " + _springHibernatePackage + ";",
"import " + _propsUtilPackage + ".PropsUtil;"
});
// Write file
File ejbFile = new File(_implDir + "/" + StringUtil.replace(_springHibernatePackage, ".", "/") + "/FinderCache.java");
FileUtil.write(ejbFile, content, true);
}
private void _createHBM(Entity entity) throws IOException {
File ejbFile = new File(_outputPath + "/service/persistence/" + entity.getName() + "HBM.java");
if (ejbFile.exists()) {
System.out.println("Removing deprecated " + ejbFile);
ejbFile.delete();
}
}
private void _createHBMUtil(Entity entity) throws IOException {
File ejbFile = new File(_outputPath + "/service/persistence/" + entity.getName() + "HBMUtil.java");
if (ejbFile.exists()) {
System.out.println("Removing deprecated " + ejbFile);
ejbFile.delete();
}
}
private void _createHBMXML() throws IOException {
StringMaker sm = new StringMaker();
for (int i = 0; i < _ejbList.size(); i++) {
Entity entity = (Entity)_ejbList.get(i);
List pkList = entity.getPKList();
List columnList = entity.getColumnList();
if (entity.hasColumns()) {
sm.append("\t<class name=\"" + _packagePath + ".model.impl." + entity.getName() + "Impl\" table=\"" + entity.getTable() + "\">\n");
sm.append("\t\t<cache usage=\"read-write\" />\n");
if (entity.hasCompoundPK()) {
sm.append("\t\t<composite-id name=\"primaryKey\" class=\"" + _packagePath + ".service.persistence." + entity.getName() + "PK\">\n");
for (int j = 0; j < pkList.size(); j++) {
EntityColumn col = (EntityColumn)pkList.get(j);
sm.append("\t\t\t<key-property name=\"" + col.getName() + "\" ");
if (!col.getName().equals(col.getDBName())) {
sm.append("column=\"" + col.getDBName() + "\" />\n");
}
else {
sm.append("/>\n");
}
}
sm.append("\t\t</composite-id>\n");
}
else {
EntityColumn col = (EntityColumn)pkList.get(0);
sm.append("\t\t<id name=\"" + col.getName() + "\" ");
if (!col.getName().equals(col.getDBName())) {
sm.append("column=\"" + col.getDBName() + "\" ");
}
sm.append("type=\"");
if (!entity.hasPrimitivePK()) {
sm.append("java.lang.");
}
sm.append(col.getType() + "\">\n");
String colIdType = col.getIdType();
if (Validator.isNull(colIdType)) {
sm.append("\t\t\t<generator class=\"assigned\" />\n");
}
else if (colIdType.equals("class")) {
sm.append("\t\t\t<generator class=\"" + col.getIdParam() + "\" />\n");
}
else if (colIdType.equals("sequence")) {
sm.append("\t\t\t<generator class=\"sequence\">\n");
sm.append("\t\t\t\t<param name=\"sequence\">" + col.getIdParam() + "</param>\n");
sm.append("\t\t\t</generator>\n");
}
else {
sm.append("\t\t\t<generator class=\"" + colIdType + "\" />\n");
}
sm.append("\t\t</id>\n");
}
for (int j = 0; j < columnList.size(); j++) {
EntityColumn col = (EntityColumn)columnList.get(j);
String colType = col.getType();
if (!col.isPrimary() && !col.isCollection() && col.getEJBName() == null) {
sm.append("\t\t<property name=\"" + col.getName() + "\" ");
if (!col.getName().equals(col.getDBName())) {
sm.append("column=\"" + col.getDBName() + "\" ");
}
if (col.isPrimitiveType() || colType.equals("String")) {
sm.append("type=\"com.bkgd.platform.util.dao.hibernate.");
sm.append(_getPrimitiveObj(colType));
sm.append("Type\" ");
}
sm.append("/>\n");
}
}
sm.append("\t</class>\n");
}
}
File xmlFile = new File(_hbmFileName);
if (!xmlFile.exists()) {
String content =
"<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE hibernate-mapping PUBLIC \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n" +
"\n" +
"<hibernate-mapping default-lazy=\"false\" auto-import=\"false\">\n" +
"</hibernate-mapping>";
FileUtil.write(xmlFile, content);
}
String oldContent = FileUtil.read(xmlFile);
String newContent = _fixHBMXML(oldContent);
int firstClass = newContent.indexOf(
"<class name=\"" + _packagePath + ".model.impl.");
int lastClass = newContent.lastIndexOf(
"<class name=\"" + _packagePath + ".model.impl.");
if (firstClass == -1) {
int x = newContent.indexOf("</hibernate-mapping>");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -