📄 servicebuilder.java
字号:
newContent =
newContent.substring(0, x) + sm.toString() +
newContent.substring(x, newContent.length());
}
else {
firstClass = newContent.lastIndexOf(
"<class", firstClass) - 1;
lastClass = newContent.indexOf(
"</class>", lastClass) + 9;
newContent =
newContent.substring(0, firstClass) + sm.toString() +
newContent.substring(lastClass, newContent.length());
}
if (!oldContent.equals(newContent)) {
FileUtil.write(xmlFile, newContent);
}
}
private void _createHibernateConfiguration() throws IOException {
if (_springHibernatePackage.equals("com.bkgd.platform.system.spring.hibernate")) {
return;
}
// Content
String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/HibernateConfiguration.java");
content = StringUtil.replace(
content,
new String[] {
"package com.bkgd.platform.system.spring.hibernate;",
"import com.bkgd.platform.system.util.PropsUtil;",
"extends TransactionAwareConfiguration"
},
new String[] {
"package " + _springHibernatePackage + ";",
"import " + _propsUtilPackage + ".PropsUtil;",
"extends org.springframework.orm.hibernate3.LocalSessionFactoryBean"
});
// Write file
File ejbFile = new File(_implDir + "/" + StringUtil.replace(_springHibernatePackage, ".", "/") + "/HibernateConfiguration.java");
FileUtil.write(ejbFile, content, true);
}
private void _createHibernateUtil() throws IOException {
if (_springHibernatePackage.equals("com.bkgd.platform.system.spring.hibernate")) {
return;
}
// Content
String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/HibernateUtil.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, ".", "/") + "/HibernateUtil.java");
FileUtil.write(ejbFile, content, true);
}
private void _createJSONJS() throws IOException {
StringMaker sm = new StringMaker();
if (_ejbList.size() > 0) {
sm.append("Liferay.Service." + _portletShortName + " = {\n");
sm.append("\tservicePackage: \"" + _packagePath + ".service.http.\"\n");
sm.append("};\n\n");
}
for (int i = 0; i < _ejbList.size(); i++) {
Entity entity = (Entity)_ejbList.get(i);
if (entity.hasRemoteService()) {
JavaClass javaClass = _getJavaClass(_outputPath + "/service/http/" + entity.getName() + "ServiceJSON.java");
JavaMethod[] methods = javaClass.getMethods();
Set jsonMethods = new LinkedHashSet();
for (int j = 0; j < methods.length; j++) {
JavaMethod javaMethod = methods[j];
String methodName = javaMethod.getName();
if (javaMethod.isPublic()) {
jsonMethods.add(methodName);
}
}
if (jsonMethods.size() > 0) {
sm.append("Liferay.Service." + _portletShortName + "." + entity.getName() + " = {\n");
sm.append("\tserviceClassName: Liferay.Service." + _portletShortName + ".servicePackage + \"" + entity.getName() + "\" + Liferay.Service.classNameSuffix,\n\n");
Iterator itr = jsonMethods.iterator();
while (itr.hasNext()) {
String methodName = (String)itr.next();
sm.append("\t" + methodName + ": function(params, callback) {\n");
sm.append("\t\tparams.serviceClassName = this.serviceClassName;\n");
sm.append("\t\tparams.serviceMethodName = \"" + methodName + "\";\n\n");
sm.append("\t\treturn Liferay.Service.ajax(params, callback);\n");
sm.append("\t}");
if (itr.hasNext()) {
sm.append(",\n");
}
sm.append("\n");
}
sm.append("};\n\n");
}
}
}
File xmlFile = new File(_jsonFileName);
if (!xmlFile.exists()) {
String content = "";
FileUtil.write(xmlFile, content);
}
String oldContent = FileUtil.read(xmlFile);
String newContent = new String(oldContent);
int oldBegin = oldContent.indexOf(
"Liferay.Service." + _portletShortName);
int oldEnd = oldContent.lastIndexOf(
"Liferay.Service." + _portletShortName);
oldEnd = oldContent.indexOf("};", oldEnd);
int newBegin = newContent.indexOf(
"Liferay.Service." + _portletShortName);
int newEnd = newContent.lastIndexOf(
"Liferay.Service." + _portletShortName);
newEnd = newContent.indexOf("};", newEnd);
if (newBegin == -1) {
newContent = oldContent + "\n\n" + sm.toString().trim();
}
else {
newContent =
newContent.substring(0, oldBegin) + sm.toString().trim() +
newContent.substring(oldEnd + 2, newContent.length());
}
if (!oldContent.equals(newContent)) {
FileUtil.write(xmlFile, newContent);
}
}
private void _createModel(Entity entity) throws IOException {
List regularColList = entity.getRegularColList();
StringMaker sm = new StringMaker();
// Package
sm.append("package " + _packagePath + ".model;");
// Imports
if (entity.hasCompoundPK()) {
sm.append("import " + _packagePath + ".service.persistence." + entity.getName() + "PK;");
}
sm.append("import com.bkgd.platform.system.model.BaseModel;");
sm.append("import java.util.Date;");
// Interface declaration
sm.append("public interface " + entity.getName() + "Model extends BaseModel {");
// Primary key accessor
sm.append("public " + entity.getPKClassName() + " getPrimaryKey();");
sm.append("public void setPrimaryKey(" + entity.getPKClassName() + " pk);");
// Getter and setter methods
for (int i = 0; i < regularColList.size(); i++) {
EntityColumn col = (EntityColumn)regularColList.get(i);
String colType = col.getType();
sm.append("public " + colType + " get" + col.getMethodName() + "();");
if (colType.equals("boolean")) {
sm.append("public " + colType + " is" + col.getMethodName() + "();");
}
sm.append("public void set" + col.getMethodName() + "(" + colType + " " + col.getName() + ");");
}
// Interface close brace
sm.append("}");
// Write file
File modelFile = new File(_serviceOutputPath + "/model/" + entity.getName() + "Model.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."
};
String[] see = {
_packagePath + ".service.model." + entity.getName(),
_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() + "Model.java");
if (modelFile.exists()) {
System.out.println("Relocating " + modelFile);
modelFile.delete();
}*/
}
private void _createModelHintsXML() throws IOException {
StringMaker sm = new StringMaker();
for (int i = 0; i < _ejbList.size(); i++) {
Entity entity = (Entity)_ejbList.get(i);
List columnList = entity.getColumnList();
if (entity.hasColumns()) {
sm.append("\t<model name=\"" + _packagePath + ".model." + entity.getName() + "\">\n");
Map defaultHints = ModelHintsUtil.getDefaultHints(_packagePath + ".model." + entity.getName());
if ((defaultHints != null) && (defaultHints.size() > 0)) {
sm.append("\t\t<default-hints>\n");
Iterator itr = defaultHints.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry entry = (Map.Entry)itr.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
sm.append("\t\t\t<hint name=\"" + key + "\">" + value + "</hint>\n");
}
sm.append("\t\t</default-hints>\n");
}
for (int j = 0; j < columnList.size(); j++) {
EntityColumn col = (EntityColumn)columnList.get(j);
if (!col.isCollection()) {
sm.append("\t\t<field name=\"" + col.getName() + "\" type=\"" + col.getType() + "\"");
Element field = ModelHintsUtil.getFieldsEl(_packagePath + ".model." + entity.getName(), col.getName());
List hints = null;
if (field != null) {
hints = field.elements();
}
if ((hints == null) || (hints.size() == 0)) {
sm.append(" />\n");
}
else {
sm.append(">\n");
Iterator itr = hints.iterator();
while (itr.hasNext()) {
Element hint = (Element)itr.next();
if (hint.getName().equals("hint")) {
sm.append("\t\t\t<hint name=\"" + hint.attributeValue("name") + "\">" + hint.getText() + "</hint>\n");
}
else {
sm.append("\t\t\t<hint-collection name=\"" + hint.attributeValue("name") + "\" />\n");
}
}
sm.append("\t\t</field>\n");
}
}
}
sm.append("\t</model>\n");
}
}
File xmlFile = new File(_modelHintsFileName);
if (!xmlFile.exists()) {
String content =
"<?xml version=\"1.0\"?>\n" +
"\n" +
"<model-hints>\n" +
"</model-hints>";
FileUtil.write(xmlFile, content);
}
String oldContent = FileUtil.read(xmlFile);
String newContent = new String(oldContent);
int firstModel = newContent.indexOf(
"<model name=\"" + _packagePath + ".model.");
int lastModel = newContent.lastIndexOf(
"<model name=\"" + _packagePath + ".model.");
if (firstModel == -1) {
int x = newContent.indexOf("</model-hints>");
newContent =
newContent.substring(0, x) + sm.toString() +
newContent.substring(x, newContent.length());
}
else {
firstModel = newContent.lastIndexOf(
"<model", firstModel) - 1;
lastModel = newContent.indexOf(
"</model>", lastModel) + 9;
newContent =
newContent.substring(0, firstModel) + sm.toString() +
newContent.substring(lastModel, newContent.length());
}
if (!oldContent.equals(newContent)) {
FileUtil.write(xmlFile, newContent);
}
}
private void _createModelImpl(Entity entity) throws IOException {
List pkList = entity.getPKList();
List regularColList = entity.getRegularColList();
StringMaker sm = new StringMaker();
// Package
sm.append("package " + _packagePath + ".model.impl;");
// Imports
if (entity.hasCompoundPK()) {
sm.append("import " + _packagePath + ".service.persistence." + entity.getName() + "PK;");
}
sm.append("import " + _baseModelImplPackage + ".BaseModelImpl;");
sm.append("import " + _propsUtilPackage + ".PropsUtil;");
sm.append("import com.bkgd.platform.system.kernel.util.DateUtil;");
sm.append("import com.bkgd.platform.system.kernel.util.GetterUtil;");
sm.append("import com.bkgd.platform.system.util.XSSUtil;");
sm.append("import java.io.Serializable;");
sm.append("import java.sql.Types;");
sm.append("import java.util.Date;");
// Class declaration
sm.append("public class " + entity.getName() + "ModelImpl extends BaseModelImpl {");
// Fields
sm.append("public static String TABLE_NAME = \"" + entity.getTable() + "\";");
sm.append("public static Object[][] TABLE_COLUMNS = {");
for (int i = 0; i < regularColList.size(); i++) {
EntityColumn col = (EntityColumn)regularColList.get(i);
String sqlType = _getSqlType(_packagePath + ".model." + entity.getName(), col.getName(), col.getType());
sm.append("{\"" + col.getDBName() + "\", new Integer(Types." + sqlType + ")}");
if ((i + 1) < regularColList.size()) {
sm.append(",");
}
}
sm.append("};");
String createTableSQL = _getCreateTableSQL(entity);
createTableSQL = StringUtil.replace(createTableSQL, "\n", "");
createTableSQL = StringUtil.replace(createTableSQL, "\t", "");
createTableSQL = createTableSQL.substring(0, createTableSQL.length() - 1);
sm.append("public static String TABLE_SQL_CREATE = \"" + createTableSQL + "\";");
sm.append("public static String TABLE_SQL_DROP = \"drop table " + entity.getTable() + "\";");
sm.append("public static boolean XSS_ALLOW_BY_MODEL = GetterUtil.getBoolean(PropsUtil.get(\"xss.allow." + _packagePath + ".model." + entity.getName() + "\"), XSS_ALLOW);");
for (int i = 0; i < regularColList.size(); i++) {
EntityColumn col = (EntityColumn)regularColList.get(i);
if (col.getType().equals("String")) {
sm.append("public static boolean XSS_ALLOW_" + col.getName().toUpperCase() + " = GetterUtil.getBoolean(PropsUtil.g
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -