UltraEdit生成hibernate实体类的脚本

// ----------------------------------------------------------------------------
// Script Name: 
// Creation Date: 
// Last Modified: 
// Copyright (c)2007
// Purpose: 
// ----------------------------------------------------------------------------
var lineTerminator = "\n";
var fieldSp = "\t";
var StringArray = new Array();
var doc = UltraEdit.activeDocument;
UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.showOutput=true;

function trim(s){
	return s.replace(/(\s*$)/g,"");
}

//把user_name转换成userName
function javaStyleName(name){
	return name.toLowerCase().replace(/_([a-z])/g,function($0,$1,$2,$3){		
		return $1.toUpperCase();
	});
}
//首字母大写
function firstToUpperCase(s){
	return s.replace(/^([a-z])/, function($0,$1){
		return $1.toUpperCase();
	});
}

function getType(dbType){
	var type = dbType.toLowerCase();
	if(type=="number(1)"){
		return "Boolean";
	}
	var i = type.indexOf("(");
	if(i>0){
		type = type.substring(0,i);
	}
	return type;
}

function getJavaType(dbType){
	var type = getType(dbType);	
	switch(type){
		case "number":
			return "Long";		
		case "varchar":
		case "varchar2":
		case "clob":
			return "String";
		case "date":			
			return "Date";
		default:
			return "String";
	}
}

function getASType(dbType){
	var type = getType(dbType);
	switch(type){
		case "number":
			return "Number";		
		case "varchar":
		case "varchar2":
		case "clob":
			return "String";
		case "date":			
			return "Date";
		default:
			return "String";
	}
}

function getXMLType(dbType){
	var type = getType(dbType);
	switch(type){
		case "number":
			return "Long";		
		case "varchar":
		case "varchar2":
			return "String";
		case "clob":
			return "org.springframework.orm.hibernate3.support.ClobStringType";
		case "date":
			return "java.util.Date";
		default:
			return "String";
	}
}

var className = UltraEdit.getString("输入类名",1);
className = trim(className);

if(className != ""){  
	doc.selectAll();
	var content = doc.selection;
	if(content.length > 0){		
		var importDate = false; //是否需要导入Date
		var javaCode = "public class "+className+"{\n\r";
		var asCode = javaCode;
		var configXml = "<?xml version=\"1.0\"?>\n\r<!DOCTYPE hibernate-mapping PUBLIC \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\n\r\"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n\r";
		configXml += '<hibernate-mapping>\n\r';
		configXml += '<class name="'+className+'" table="'+className+'">\n\r';
		stringArray = content.split(lineTerminator);
		var fields;
		
		for(var i=0;i<stringArray.length;i++){
			fields = stringArray[i].split(fieldSp);
			var fieldName = trim(javaStyleName(fields[0]));
			var methodName = firstToUpperCase(fieldName);
			var fieldType = trim(getJavaType(fields[1]));
			var comment = trim(fields[4]);
			if(!importDate){
				importDate = fieldType == "Date";
			}
			javaCode += '@Column(name="'+fields[0].toUpperCase()+'")\n\r'
			javaCode += ("private "+fieldType+" "+fieldName+";\n\r");			
			if(comment != ""){
				javaCode += ("/**\n\r*"+fields[4]+"*/\n\r");
			}
			javaCode += ("public void set"+methodName+"("+fieldType+" "+fieldName+"){\n\rthis."+fieldName+"="+fieldName+";\n\r}\n\r");
			if(comment != ""){
				javaCode += ("/**\n\r*"+fields[4]+"*/\n\r");
			}
			javaCode += ("public "+fieldType+" get"+methodName+"(){\n\rreturn this."+fieldName+";\n\r}\n\r");
			//ActionScript
			asCode += ("public var "+fieldName+":"+getASType(fields[1])+";\n\r");
			//xml
			var xmlType = getXMLType(fields[1]);
			if(i==0){
				//主键
				configXml += '<id name="'+fieldName+'" type="'+xmlType+'">\n\r';
				configXml += '<column name="'+fields[0].toUpperCase()+'" precision="22" scale="0" />\n\r';
				configXml += '<generator class="sequence">\n\r';
				configXml += '<param name="sequence">SEQ_'+className.toUpperCase()+'</param>\n\r';
				configXml += '</generator>\n\r</id>\n\r\n\r';				
			}else{
				configXml += '<property name="'+fieldName+'" type="'+xmlType+'">\n\r';
        configXml += '<column name="'+fields[0].toUpperCase()+'"/>\n\r';
        configXml += '</property>\n\r\n\r'
			}
		}
		javaCode += "}";
		asCode += "}";
		configXml += '</class>\n\r</hibernate-mapping>\n\r';
		if(importDate){
			javaCode = "import java.util.Date;\n\r"+javaCode;
		}
		UltraEdit.newFile();
		UltraEdit.activeDocument.write(javaCode);
		UltraEdit.activeDocument.write("\n\r\n\r\n\r");
		UltraEdit.activeDocument.write(configXml);
		UltraEdit.activeDocument.write("\n\r\n\r\n\r");
		UltraEdit.activeDocument.write("//ActionScript类\n\r");
		UltraEdit.activeDocument.write(asCode);
		//UltraEdit.activeDocument.selectAll();
		//UltraEdit.activeDocument.copy();
	}
}

你可能感兴趣的:(UltraEdit生成hibernate实体类的脚本)