All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ys.artifact-orm.1.0.1.source-code.bean.btl Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
<%
	var packages = [];
	var imp = "";
	array.addOnly(packages,"lombok.experimental.Accessors");
	array.addOnly(packages,"lombok.Data");
	array.addOnly(packages,"lombok.AccessLevel");
	array.addOnly(packages,"lombok.Setter");
	array.addOnly(packages,"java.util.Date");
	array.addOnly(packages,"org.artifact.core.db.BaseBean");
	array.addOnly(packages,"org.artifact.core.lang.IServer");
	array.addOnly(packages,"java.util.Map");
	array.addOnly(packages,"cn.hutool.core.bean.BeanUtil");
	array.addOnly(packages,"cn.hutool.core.map.MapUtil");
    array.addOnly(packages,"java.util.HashMap");
	if(@table.getMethods().contains("map") || @table.getMethods().contains("hashmap")){

	}
	if(@table.getMethods().contains("byte")){
		array.addOnly(packages,"org.artifact.core.lang.IServer");
		array.addOnly(packages,"org.artifact.core.context.bytebuf.IByteBuff");
		array.addOnly(packages,"org.artifact.core.lang.ISerializable");
		imp = ",ISerializable<"+table.className+">";
	}
	if(@table.getMethods().contains("json")){
		array.addOnly(packages,"org.artifact.core.context.json.IJson");
		array.addOnly(packages,"org.artifact.core.context.json.IJsonFactory");
		array.addOnly(packages,"cn.hutool.core.map.MapUtil");
		array.addOnly(packages,"java.util.HashMap");
	}
	if(table.pk.type == "String"){
		array.addOnly(packages,"cn.hutool.core.lang.ObjectId");
	}
%>
package ${templates['BeanTemplate'].package};

<%for(package in packages){%>
import ${package};
<%}%>
@Data
@Accessors(chain = true)
@SuppressWarnings({"rawtypes","unchecked"})
/** ${table.remark} */
public abstract class ${table.className} implements BaseBean<${table.className}>${imp} {
	<%for(column in table.columns){%>
	/** ${column.remark} */
	<%if(column.fieldName == "createDate" || column.fieldName == "modifiedDate"){%>
	@Setter(value = AccessLevel.PROTECTED)
	<%}%>
	<%if(strutil.isNotBlank(column.defaults)){%>
	private ${column.type} ${column.fieldName} = ${column.defaults};
	<%}else{%>
	private ${column.type} ${column.fieldName};
	<%}%>
	<%}%>
	
	<%if(@table.getMethods().contains("array")){%>
	public Object[] toArray(){
		Object[] values = new Object[${table.columns.~size}];
		<%for(column in table.columns){%>
		values[${columnLP.index-1}] = this.${column.fieldName};
	    <%}%>
		return values;
	}
	
	public ${table.className} forArray(Object[] array){
		<%for(column in table.columns){%>
		this.${column.fieldName} = (${column.type})array[${columnLP.index-1}];
	    <%}%>
		return this;
	}
	<%}%>
	
	<%if(@table.getMethods().contains("map")){%>
	public Map toMap(){
		Map map = new HashMap<>();
		<%for(column in table.columns){%>
		map.put("${column.fieldName}",this.${column.fieldName});
	    <%}%>
		return map;
	}
	
	public ${table.className} forMap(Map map){
		<%
		for(column in table.columns){
		if(column.type =="byte"){
		%>
		this.${column.fieldName} = MapUtil.getByte(map ,"${column.fieldName}");
		<%}else if(column.type =="short"){%>
		this.${column.fieldName} = MapUtil.getShort(map ,"${column.fieldName}");
		<%}else if(column.type =="int"){%>
		this.${column.fieldName} = MapUtil.getInt(map ,"${column.fieldName}");
		<%}else if(column.type =="long"){%>
		this.${column.fieldName} = MapUtil.getLong(map ,"${column.fieldName}");
		<%}else if(column.type =="float"){%>
		this.${column.fieldName} = MapUtil.getFloat(map ,"${column.fieldName}");
		<%}else if(column.type =="double"){%>
		this.${column.fieldName} = MapUtil.getDouble(map ,"${column.fieldName}");
		<%}else if(column.type=="java.util.Date"){%>
		this.${column.fieldName} = MapUtil.getDate(map ,"${column.fieldName}");
		<%}else if(column.type=="String"){%>
		this.${column.fieldName} = MapUtil.getStr(map ,"${column.fieldName}");
		<%}else if(column.type=="boolean"){%>
		this.${column.fieldName} = MapUtil.getBoolean(map ,"${column.fieldName}");
		<%}else{%>
		this.${column.fieldName} = MapUtil.get(map ,"${column.fieldName}",${column.type}.class);
		<%}%>
	    <%}%>
		return this;
	}
	<%}%>

	<%if(@table.getMethods().contains("hashmap")){%>
	public Map toHashMap(){
		Map map = new HashMap<>();
		<%
		var hashArray = [];
		for(column in table.columns){
		var hashCode = @column.getFieldName().hashCode()+"";
		if(array.contain(hashArray,hashCode)){
			println("		"+column.fieldName+":HashCode重复,请重命名");
			continue;
		}else{
			array.add(hashArray,hashCode);
		}
		%>
		map.put(${hashCode},this.${column.fieldName});
	    <%}%>
		return map;
	}
	
	public ${table.className} forHashMap(Map map){
		<%
		for(column in table.columns){
		if(column.type =="byte"){
		%>
		this.${column.fieldName} = MapUtil.getByte(map ,${@column.getFieldName().hashCode()});
		<%}else if(column.type =="short"){%>
		this.${column.fieldName} = MapUtil.getShort(map ,${@column.getFieldName().hashCode()});
		<%}else if(column.type =="int"){%>
		this.${column.fieldName} = MapUtil.getInt(map ,${@column.getFieldName().hashCode()});
		<%}else if(column.type =="long"){%>
		this.${column.fieldName} = MapUtil.getLong(map ,${@column.getFieldName().hashCode()});
		<%}else if(column.type =="float"){%>
		this.${column.fieldName} = MapUtil.getFloat(map ,${@column.getFieldName().hashCode()});
		<%}else if(column.type =="double"){%>
		this.${column.fieldName} = MapUtil.getDouble(map ,${@column.getFieldName().hashCode()});
		<%}else if(column.type=="java.util.Date"){%>
		this.${column.fieldName} = MapUtil.getDate(map ,${@column.getFieldName().hashCode()});
		<%}else if(column.type=="String"){%>
		this.${column.fieldName} = MapUtil.getStr(map ,${@column.getFieldName().hashCode()});
		<%}else if(column.type=="boolean"){%>
		this.${column.fieldName} = MapUtil.getBoolean(map ,${@column.getFieldName().hashCode()});
		<%}else{%>
		this.${column.fieldName} = MapUtil.get(map ,${@column.getFieldName().hashCode()},${column.type}.class);
		<%}%>
	    <%}%>
		return this;
	}
	<%}%>

	<%if(@table.getMethods().contains("byte")){%>
	public IByteBuff toByteBuff(IByteBuff byteBuff){
		<%
		for(column in table.columns){
		if(column.type =="byte"){
		%>
		byteBuff.writeByte(this.${column.fieldName});
		<%}else if(column.type =="short"){%>
		byteBuff.writeShort(this.${column.fieldName});
		<%}else if(column.type =="int"){%>
		byteBuff.writeInt(this.${column.fieldName});
		<%}else if(column.type =="long"){%>
		byteBuff.writeLong(this.${column.fieldName});
		<%}else if(column.type =="float"){%>
		byteBuff.writeFloat(this.${column.fieldName});
		<%}else if(column.type =="double"){%>
		byteBuff.writeDouble(this.${column.fieldName});
		<%}else if(column.type=="java.util.Date"){%>
		byteBuff.writeDate(this.${column.fieldName});
		<%}else if(column.type=="String"){%>
		byteBuff.writeString(this.${column.fieldName});
		<%}else if(column.type=="boolean"){%>
		byteBuff.writeBoolean(this.${column.fieldName});
		<%}else if(clzUtil.isCollection(column.type)){%>
		byteBuff.writeCollection(this.${column.fieldName});
		<%}else if(clzUtil.isMap(column.type)){%>
		byteBuff.writeMap(this.${column.fieldName});
		<%}else{%>
		byteBuff.write(this.${column.fieldName});
		<%}%>
	    <%}%>
		return byteBuff;
	}
	
	public ${table.className} forByteBuff(IByteBuff byteBuff){
		<%
		for(column in table.columns){
		if(column.type =="byte"){
		%>
		this.${column.fieldName} = byteBuff.readByte();
		<%}else if(column.type =="short"){%>
		this.${column.fieldName} = byteBuff.readShort();
		<%}else if(column.type =="int"){%>
		this.${column.fieldName} = byteBuff.readInt();
		<%}else if(column.type =="long"){%>
		this.${column.fieldName} = byteBuff.readLong();
		<%}else if(column.type =="float"){%>
		this.${column.fieldName} = byteBuff.readFloat();
		<%}else if(column.type =="double"){%>
		this.${column.fieldName} = byteBuff.readDouble();
		<%}else if(column.type=="java.util.Date"){%>
		this.${column.fieldName} = byteBuff.readDate();
		<%}else if(column.type=="String"){%>
		this.${column.fieldName} = byteBuff.readString();
		<%}else if(column.type=="boolean"){%>
		this.${column.fieldName} = byteBuff.readBoolean();
		<%}else if(clzUtil.isCollection(column.type)){%>
		this.${column.fieldName} = byteBuff.readCollection();
		<%}else if(clzUtil.isMap(column.type)){%>
		this.${column.fieldName} = byteBuff.readMap();
		<%}else{%>
		this.${column.fieldName} = (${column.type})byteBuff.readObject();
		<%}%>
	    <%}%>
		return this;
	}
	<%}%>
	
	<%if(@table.getMethods().contains("json")){%>
	public String toJson(){
		Map map = new HashMap<>();
		<%for(column in table.columns){%>
		map.put("${column.fieldName}",this.${column.fieldName});
	    <%}%>
		IJsonFactory factory = IServer.me().getContext().getJsonFactory();
		IJson json = factory.getJson();
		return json.toJson(map);
	}
	
	public ${table.className} forJson(String content){
		IJsonFactory factory = IServer.me().getContext().getJsonFactory();
		IJson json = factory.getJson();
		Map map = json.parse(content,Map.class);
		<%
		for(column in table.columns){
		if(column.type =="byte"){
		%>
		this.${column.fieldName} = MapUtil.getByte(map ,${column.fieldName});
		<%}else if(column.type =="short"){%>
		this.${column.fieldName} = MapUtil.getShort(map ,${column.fieldName});
		<%}else if(column.type =="int"){%>
		this.${column.fieldName} = MapUtil.getInt(map ,${column.fieldName});
		<%}else if(column.type =="long"){%>
		this.${column.fieldName} = MapUtil.getLong(map ,${column.fieldName});
		<%}else if(column.type =="float"){%>
		this.${column.fieldName} = MapUtil.getFloat(map ,${column.fieldName});
		<%}else if(column.type =="double"){%>
		this.${column.fieldName} = MapUtil.getDouble(map ,${column.fieldName});
		<%}else if(column.type=="java.util.Date"){%>
		this.${column.fieldName} = MapUtil.getDate(map ,${column.fieldName});
		<%}else if(column.type=="String"){%>
		this.${column.fieldName} = MapUtil.getStr(map ,${column.fieldName});
		<%}else if(column.type=="boolean"){%>
		this.${column.fieldName} = MapUtil.getBoolean(map ,${column.fieldName});
		<%}else{%>
		this.${column.fieldName} = MapUtil.get(map ,${column.fieldName},${column.type}.class);
		<%}%>
	    <%}%>
		return this;
	}
	<%}%>


	@Override
	public ${table.className} deepCopy() {
		${table.className} ${lf(table.className)} = null;
		try {
			${lf(table.className)} = (${table.className}) super.clone();
			<%for(column in table.columns){%>
			<%if(column.type =="java.util.Date" || clzUtil.isMap(column.type) || clzUtil.isCollection(column.type)){%>
			${lf(table.className)}.${column.fieldName} = (${column.type})this.${column.fieldName}.clone();
			<%}%>
	    	<%}%>
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return ${lf(table.className)};
	}

	public ${table.className} onLoad(${table.className} t){
	    BeanUtil.copyProperties(t, this);
        onRead();
        return this;
	}

	public ${table.className} toSave() {
	    <%if(table.pk.auto){%>
        <%if(table.pk.type == "String"){%>
        this.${table.pk.fieldName} = ObjectId.next();
        <%}else{%>
        this.${table.pk.fieldName} = (${table.pk.type})IServer.me().getContext().getIdFactory().getId();
        <%}%>
        <%}%>
		Date now = new Date();
		if (createDate==null) {
			createDate = now;
			modifiedDate = now;
		}else {
			modifiedDate = now;
		}
		onSave();
		onWrite();
		return deepCopy();
	}
	
	/**
	 * 存储之前
	 */
	protected void onSave(){
		
	}

    <%
    switch(database.designConfig.type){
    	case "mysql":
    		include("bean-mysql.btl"){}
    		break;
    	case "mongodb":
    		include("bean-mongodb.btl"){}
    		break;
    	case "redis":
    		include("bean-redis.btl"){}
    		break;
    }
    %>

    /**
     * 写数据之前
     */
    protected void onWrite(){
    }

    /**
     * 读数据之前
     */
    protected void onRead(){
    }

	@Override
	public Object pk() {
		return this.${table.pk.fieldName};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy